blob: c7509c51e9d94cfa18c856b468ca1a809a48c299 [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.
Sara Sharon6eb031d2015-07-13 14:50:47 +03006 * Copyright(c) 2015 Intel Deutschland GmbH
Wey-Yi Guy792bc3c2010-03-16 10:23:29 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
20 * USA
21 *
22 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020023 * in the file called COPYING.
Wey-Yi Guy792bc3c2010-03-16 10:23:29 -070024 *
25 * Contact Information:
Emmanuel Grumbachd01c5362015-11-17 15:39:56 +020026 * Intel Linux Wireless <linuxwifi@intel.com>
Wey-Yi Guy792bc3c2010-03-16 10:23:29 -070027 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28 *
29 *****************************************************************************/
30
31#include <linux/kernel.h>
Wey-Yi Guy792bc3c2010-03-16 10:23:29 -070032
Wey-Yi Guy81b81762010-03-16 10:23:30 -070033#include "iwl-io.h"
Wey-Yi Guy19e6cda2010-03-16 17:41:23 -070034#include "iwl-agn-hw.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070035#include "iwl-trans.h"
Emmanuel Grumbachdda61a42011-08-25 23:11:11 -070036#include "iwl-fh.h"
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +020037#include "iwl-op-mode.h"
Wey-Yi Guy741a6262010-03-16 12:37:24 -070038
Johannes Berg1023fdc2012-05-15 12:16:34 +020039#include "dev.h"
40#include "agn.h"
41#include "calib.h"
42
Don Fryde7f5f92011-11-10 06:55:10 -080043/******************************************************************************
44 *
45 * uCode download functions
46 *
47 ******************************************************************************/
48
Wey-Yi Guy741a6262010-03-16 12:37:24 -070049/*
50 * Calibration
51 */
Johannes Berge1991882012-03-06 13:30:36 -080052static int iwl_set_Xtal_calib(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -070053{
54 struct iwl_calib_xtal_freq_cmd cmd;
Eytan Lifshitzb7998c82012-12-01 20:59:49 +020055 __le16 *xtal_calib = priv->nvm_data->xtal_calib;
Wey-Yi Guy741a6262010-03-16 12:37:24 -070056
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -070057 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
Wey-Yi Guy741a6262010-03-16 12:37:24 -070058 cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
59 cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
Johannes Berge1991882012-03-06 13:30:36 -080060 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Wey-Yi Guy741a6262010-03-16 12:37:24 -070061}
62
Johannes Berge1991882012-03-06 13:30:36 -080063static int iwl_set_temperature_offset_calib(struct iwl_priv *priv)
Shanyu Zhaobf53f932010-09-21 16:54:01 -070064{
65 struct iwl_calib_temperature_offset_cmd cmd;
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -070066
67 memset(&cmd, 0, sizeof(cmd));
68 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
Eytan Lifshitzb7998c82012-12-01 20:59:49 +020069 cmd.radio_sensor_offset = priv->nvm_data->raw_temperature;
Shanyu Zhaobf53f932010-09-21 16:54:01 -070070 if (!(cmd.radio_sensor_offset))
71 cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -070072
Johannes Berge1991882012-03-06 13:30:36 -080073 IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
Wey-Yi Guy2e277992011-07-08 14:29:48 -070074 le16_to_cpu(cmd.radio_sensor_offset));
Johannes Berge1991882012-03-06 13:30:36 -080075 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Shanyu Zhaobf53f932010-09-21 16:54:01 -070076}
77
Johannes Berge1991882012-03-06 13:30:36 -080078static int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
Wey-Yi Guyc6f30342011-09-15 11:46:50 -070079{
80 struct iwl_calib_temperature_offset_v2_cmd cmd;
Wey-Yi Guyc6f30342011-09-15 11:46:50 -070081
82 memset(&cmd, 0, sizeof(cmd));
83 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
Eytan Lifshitzb7998c82012-12-01 20:59:49 +020084 cmd.radio_sensor_offset_high = priv->nvm_data->kelvin_temperature;
85 cmd.radio_sensor_offset_low = priv->nvm_data->raw_temperature;
Johannes Berg26a7ca92012-05-21 11:55:54 +020086 if (!cmd.radio_sensor_offset_low) {
Johannes Berge1991882012-03-06 13:30:36 -080087 IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
Wey-Yi Guyc6f30342011-09-15 11:46:50 -070088 cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
89 cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
90 }
Eytan Lifshitzb7998c82012-12-01 20:59:49 +020091 cmd.burntVoltageRef = priv->nvm_data->calib_voltage;
Wey-Yi Guyc6f30342011-09-15 11:46:50 -070092
Johannes Berge1991882012-03-06 13:30:36 -080093 IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -070094 le16_to_cpu(cmd.radio_sensor_offset_high));
Johannes Berge1991882012-03-06 13:30:36 -080095 IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -070096 le16_to_cpu(cmd.radio_sensor_offset_low));
Johannes Berge1991882012-03-06 13:30:36 -080097 IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -070098 le16_to_cpu(cmd.burntVoltageRef));
99
Johannes Berge1991882012-03-06 13:30:36 -0800100 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700101}
102
Johannes Berge1991882012-03-06 13:30:36 -0800103static int iwl_send_calib_cfg(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700104{
105 struct iwl_calib_cfg_cmd calib_cfg_cmd;
106 struct iwl_host_cmd cmd = {
107 .id = CALIBRATION_CFG_CMD,
Johannes Berg3fa50732011-05-04 07:50:38 -0700108 .len = { sizeof(struct iwl_calib_cfg_cmd), },
109 .data = { &calib_cfg_cmd, },
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700110 };
111
112 memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
113 calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
114 calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
115 calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
Wey-Yi Guydf2a4dc2011-07-08 14:29:45 -0700116 calib_cfg_cmd.ucd_calib_cfg.flags =
117 IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700118
Johannes Berge10a0532012-03-06 13:30:39 -0800119 return iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700120}
121
Johannes Berge1991882012-03-06 13:30:36 -0800122int iwl_init_alive_start(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700123{
Johannes Bergca7966c2011-04-22 10:15:23 -0700124 int ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700125
Johannes Berg0d8877a2013-05-17 10:36:29 +0200126 if (priv->lib->bt_params &&
127 priv->lib->bt_params->advanced_bt_coexist) {
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700128 /*
129 * Tell uCode we are ready to perform calibration
130 * need to perform this before any calibration
131 * no need to close the envlope since we are going
132 * to load the runtime uCode later.
133 */
Johannes Berge1991882012-03-06 13:30:36 -0800134 ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700135 BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
Johannes Bergca7966c2011-04-22 10:15:23 -0700136 if (ret)
137 return ret;
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700138
139 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700140
Johannes Berge1991882012-03-06 13:30:36 -0800141 ret = iwl_send_calib_cfg(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700142 if (ret)
143 return ret;
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700144
145 /**
146 * temperature offset calibration is only needed for runtime ucode,
147 * so prepare the value now.
148 */
Johannes Berg0d8877a2013-05-17 10:36:29 +0200149 if (priv->lib->need_temp_offset_calib) {
150 if (priv->lib->temp_offset_v2)
Johannes Berge1991882012-03-06 13:30:36 -0800151 return iwl_set_temperature_offset_calib_v2(priv);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700152 else
Johannes Berge1991882012-03-06 13:30:36 -0800153 return iwl_set_temperature_offset_calib(priv);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700154 }
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700155
Johannes Bergca7966c2011-04-22 10:15:23 -0700156 return 0;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700157}
158
Johannes Bergaca86262012-05-14 09:08:50 +0200159static int iwl_send_wimax_coex(struct iwl_priv *priv)
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700160{
161 struct iwl_wimax_coex_cmd coex_cmd;
162
Johannes Berg85560af2012-03-15 13:26:48 -0700163 /* coexistence is disabled */
164 memset(&coex_cmd, 0, sizeof(coex_cmd));
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700165
Johannes Berge10a0532012-03-06 13:30:39 -0800166 return iwl_dvm_send_cmd_pdu(priv,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300167 COEX_PRIORITY_TABLE_CMD, 0,
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700168 sizeof(coex_cmd), &coex_cmd);
169}
170
Don Fry66128b12011-11-28 14:35:14 -0800171static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700172 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
173 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
174 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
175 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
176 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
177 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
178 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
179 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
180 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
181 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
182 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
183 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
184 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
185 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
186 ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
187 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
188 ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
189 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
190 0, 0, 0, 0, 0, 0, 0
191};
192
Johannes Berge1991882012-03-06 13:30:36 -0800193void iwl_send_prio_tbl(struct iwl_priv *priv)
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700194{
195 struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
196
Don Fry66128b12011-11-28 14:35:14 -0800197 memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
198 sizeof(iwl_bt_prio_tbl));
Johannes Berge10a0532012-03-06 13:30:39 -0800199 if (iwl_dvm_send_cmd_pdu(priv,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300200 REPLY_BT_COEX_PRIO_TABLE, 0,
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700201 sizeof(prio_tbl_cmd), &prio_tbl_cmd))
Johannes Berge1991882012-03-06 13:30:36 -0800202 IWL_ERR(priv, "failed to send BT prio tbl command\n");
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700203}
204
Johannes Berge1991882012-03-06 13:30:36 -0800205int iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700206{
207 struct iwl_bt_coex_prot_env_cmd env_cmd;
Johannes Bergca7966c2011-04-22 10:15:23 -0700208 int ret;
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700209
210 env_cmd.action = action;
211 env_cmd.type = type;
Johannes Berge10a0532012-03-06 13:30:39 -0800212 ret = iwl_dvm_send_cmd_pdu(priv,
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300213 REPLY_BT_COEX_PROT_ENV, 0,
Johannes Bergca7966c2011-04-22 10:15:23 -0700214 sizeof(env_cmd), &env_cmd);
215 if (ret)
Johannes Berge1991882012-03-06 13:30:36 -0800216 IWL_ERR(priv, "failed to send BT env command\n");
Johannes Bergca7966c2011-04-22 10:15:23 -0700217 return ret;
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700218}
219
Emmanuel Grumbachb04db9a2012-06-21 11:53:44 +0300220static const u8 iwlagn_default_queue_to_tx_fifo[] = {
221 IWL_TX_FIFO_VO,
222 IWL_TX_FIFO_VI,
223 IWL_TX_FIFO_BE,
224 IWL_TX_FIFO_BK,
225};
226
227static const u8 iwlagn_ipan_queue_to_tx_fifo[] = {
228 IWL_TX_FIFO_VO,
229 IWL_TX_FIFO_VI,
230 IWL_TX_FIFO_BE,
231 IWL_TX_FIFO_BK,
232 IWL_TX_FIFO_BK_IPAN,
233 IWL_TX_FIFO_BE_IPAN,
234 IWL_TX_FIFO_VI_IPAN,
235 IWL_TX_FIFO_VO_IPAN,
236 IWL_TX_FIFO_BE_IPAN,
237 IWL_TX_FIFO_UNUSED,
238 IWL_TX_FIFO_AUX,
239};
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700240
Johannes Berge1991882012-03-06 13:30:36 -0800241static int iwl_alive_notify(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700242{
Emmanuel Grumbachb04db9a2012-06-21 11:53:44 +0300243 const u8 *queue_to_txf;
244 u8 n_queues;
Wey-Yi Guy74159522011-04-05 09:42:01 -0700245 int ret;
Emmanuel Grumbachb04db9a2012-06-21 11:53:44 +0300246 int i;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700247
Emmanuel Grumbachadca1232012-10-25 23:08:27 +0200248 iwl_trans_fw_alive(priv->trans, 0);
Johannes Berge755f882012-03-07 09:52:16 -0800249
Emmanuel Grumbachb04db9a2012-06-21 11:53:44 +0300250 if (priv->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN &&
Eytan Lifshitzb7998c82012-12-01 20:59:49 +0200251 priv->nvm_data->sku_cap_ipan_enable) {
Emmanuel Grumbachb04db9a2012-06-21 11:53:44 +0300252 n_queues = ARRAY_SIZE(iwlagn_ipan_queue_to_tx_fifo);
253 queue_to_txf = iwlagn_ipan_queue_to_tx_fifo;
254 } else {
255 n_queues = ARRAY_SIZE(iwlagn_default_queue_to_tx_fifo);
256 queue_to_txf = iwlagn_default_queue_to_tx_fifo;
257 }
258
259 for (i = 0; i < n_queues; i++)
260 if (queue_to_txf[i] != IWL_TX_FIFO_UNUSED)
261 iwl_trans_ac_txq_enable(priv->trans, i,
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200262 queue_to_txf[i], 0);
Emmanuel Grumbachb04db9a2012-06-21 11:53:44 +0300263
Johannes Berge755f882012-03-07 09:52:16 -0800264 priv->passive_no_rx = false;
265 priv->transport_queue_stop = 0;
Grumbach, Emmanuele7cad692010-11-18 03:47:38 -0800266
Johannes Berge1991882012-03-06 13:30:36 -0800267 ret = iwl_send_wimax_coex(priv);
Wey-Yi Guy74159522011-04-05 09:42:01 -0700268 if (ret)
269 return ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700270
Johannes Berg0d8877a2013-05-17 10:36:29 +0200271 if (!priv->lib->no_xtal_calib) {
Johannes Berge1991882012-03-06 13:30:36 -0800272 ret = iwl_set_Xtal_calib(priv);
Johannes Berg93b64102011-11-17 08:51:53 -0800273 if (ret)
274 return ret;
275 }
Wey-Yi Guy74159522011-04-05 09:42:01 -0700276
Johannes Berge1991882012-03-06 13:30:36 -0800277 return iwl_send_calib_results(priv);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700278}
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700279
Don Fry69a679b2011-12-07 08:50:46 -0800280struct iwl_alive_data {
Johannes Bergca7966c2011-04-22 10:15:23 -0700281 bool valid;
282 u8 subtype;
283};
284
Johannes Bergdb662d42012-03-15 13:26:44 -0700285static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
286 struct iwl_rx_packet *pkt, void *data)
Johannes Bergca7966c2011-04-22 10:15:23 -0700287{
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800288 struct iwl_priv *priv =
289 container_of(notif_wait, struct iwl_priv, notif_wait);
Don Fry69a679b2011-12-07 08:50:46 -0800290 struct iwl_alive_data *alive_data = data;
Johannes Bergca7966c2011-04-22 10:15:23 -0700291 struct iwl_alive_resp *palive;
292
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800293 palive = (void *)pkt->data;
Johannes Bergca7966c2011-04-22 10:15:23 -0700294
Johannes Berge1991882012-03-06 13:30:36 -0800295 IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
Johannes Bergca7966c2011-04-22 10:15:23 -0700296 "0x%01X 0x%01X\n",
297 palive->is_valid, palive->ver_type,
298 palive->ver_subtype);
299
Meenakshi Venkataraman2fdfc472012-03-15 13:26:56 -0700300 priv->device_pointers.error_event_table =
Johannes Bergca7966c2011-04-22 10:15:23 -0700301 le32_to_cpu(palive->error_event_table_ptr);
Meenakshi Venkataraman2fdfc472012-03-15 13:26:56 -0700302 priv->device_pointers.log_event_table =
Johannes Bergca7966c2011-04-22 10:15:23 -0700303 le32_to_cpu(palive->log_event_table_ptr);
304
305 alive_data->subtype = palive->ver_subtype;
306 alive_data->valid = palive->is_valid == UCODE_VALID_OK;
Johannes Bergdb662d42012-03-15 13:26:44 -0700307
308 return true;
Johannes Bergca7966c2011-04-22 10:15:23 -0700309}
310
311#define UCODE_ALIVE_TIMEOUT HZ
312#define UCODE_CALIB_TIMEOUT (2*HZ)
313
Johannes Berge1991882012-03-06 13:30:36 -0800314int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
Don Fryde7f5f92011-11-10 06:55:10 -0800315 enum iwl_ucode_type ucode_type)
Johannes Bergca7966c2011-04-22 10:15:23 -0700316{
317 struct iwl_notification_wait alive_wait;
Don Fry69a679b2011-12-07 08:50:46 -0800318 struct iwl_alive_data alive_data;
Johannes Berg0692fe42012-03-06 13:30:37 -0800319 const struct fw_img *fw;
Johannes Bergca7966c2011-04-22 10:15:23 -0700320 int ret;
Don Fryde7f5f92011-11-10 06:55:10 -0800321 enum iwl_ucode_type old_type;
Sara Sharon6eb031d2015-07-13 14:50:47 +0300322 static const u16 alive_cmd[] = { REPLY_ALIVE };
Johannes Bergca7966c2011-04-22 10:15:23 -0700323
Sharon Dvir612da1e2016-08-03 10:55:45 +0300324 fw = iwl_get_ucode_image(priv->fw, ucode_type);
Johannes Bergbefe9b62013-10-25 12:32:51 +0200325 if (WARN_ON(!fw))
326 return -EINVAL;
327
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700328 old_type = priv->cur_ucode;
329 priv->cur_ucode = ucode_type;
David Spinadel8f7ffbe2012-03-10 13:00:10 -0800330 priv->ucode_loaded = false;
331
Johannes Bergdb662d42012-03-15 13:26:44 -0700332 iwl_init_notification_wait(&priv->notif_wait, &alive_wait,
333 alive_cmd, ARRAY_SIZE(alive_cmd),
334 iwl_alive_fn, &alive_data);
Johannes Bergf4720892012-03-06 13:30:57 -0800335
Emmanuel Grumbach6ae02f32012-12-24 11:10:43 +0200336 ret = iwl_trans_start_fw(priv->trans, fw, false);
Johannes Bergca7966c2011-04-22 10:15:23 -0700337 if (ret) {
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700338 priv->cur_ucode = old_type;
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800339 iwl_remove_notification(&priv->notif_wait, &alive_wait);
Johannes Bergca7966c2011-04-22 10:15:23 -0700340 return ret;
341 }
342
Johannes Bergca7966c2011-04-22 10:15:23 -0700343 /*
344 * Some things may run in the background now, but we
345 * just wait for the ALIVE notification here.
346 */
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800347 ret = iwl_wait_notification(&priv->notif_wait, &alive_wait,
Don Frydd5fe102011-11-28 16:13:19 -0800348 UCODE_ALIVE_TIMEOUT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700349 if (ret) {
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700350 priv->cur_ucode = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700351 return ret;
352 }
353
354 if (!alive_data.valid) {
Johannes Berge1991882012-03-06 13:30:36 -0800355 IWL_ERR(priv, "Loaded ucode is not valid!\n");
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700356 priv->cur_ucode = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700357 return -EIO;
358 }
359
Emmanuel Grumbach2d5d50e2013-01-31 15:03:55 +0200360 priv->ucode_loaded = true;
361
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700362 if (ucode_type != IWL_UCODE_WOWLAN) {
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700363 /* delay a bit to give rfkill time to run */
364 msleep(5);
365 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700366
Johannes Berge1991882012-03-06 13:30:36 -0800367 ret = iwl_alive_notify(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700368 if (ret) {
Johannes Berge1991882012-03-06 13:30:36 -0800369 IWL_WARN(priv,
Johannes Bergca7966c2011-04-22 10:15:23 -0700370 "Could not complete ALIVE transition: %d\n", ret);
Meenakshi Venkataramana42506e2012-03-15 13:26:57 -0700371 priv->cur_ucode = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700372 return ret;
373 }
374
375 return 0;
376}
377
Johannes Berge1f0c502012-03-15 13:26:45 -0700378static bool iwlagn_wait_calib(struct iwl_notif_wait_data *notif_wait,
379 struct iwl_rx_packet *pkt, void *data)
380{
381 struct iwl_priv *priv = data;
382 struct iwl_calib_hdr *hdr;
Johannes Berge1f0c502012-03-15 13:26:45 -0700383
384 if (pkt->hdr.cmd != CALIBRATION_RES_NOTIFICATION) {
385 WARN_ON(pkt->hdr.cmd != CALIBRATION_COMPLETE_NOTIFICATION);
386 return true;
387 }
388
389 hdr = (struct iwl_calib_hdr *)pkt->data;
Johannes Berge1f0c502012-03-15 13:26:45 -0700390
Johannes Berg65b30342014-01-08 13:16:33 +0100391 if (iwl_calib_set(priv, hdr, iwl_rx_packet_payload_len(pkt)))
Johannes Berge1f0c502012-03-15 13:26:45 -0700392 IWL_ERR(priv, "Failed to record calibration data %d\n",
393 hdr->op_code);
394
395 return false;
396}
397
Johannes Berge1991882012-03-06 13:30:36 -0800398int iwl_run_init_ucode(struct iwl_priv *priv)
Johannes Bergca7966c2011-04-22 10:15:23 -0700399{
400 struct iwl_notification_wait calib_wait;
Sara Sharon6eb031d2015-07-13 14:50:47 +0300401 static const u16 calib_complete[] = {
Johannes Berge1f0c502012-03-15 13:26:45 -0700402 CALIBRATION_RES_NOTIFICATION,
Johannes Bergdb662d42012-03-15 13:26:44 -0700403 CALIBRATION_COMPLETE_NOTIFICATION
404 };
Johannes Bergca7966c2011-04-22 10:15:23 -0700405 int ret;
406
Johannes Bergb1eea292012-03-06 13:30:42 -0800407 lockdep_assert_held(&priv->mutex);
Johannes Bergca7966c2011-04-22 10:15:23 -0700408
409 /* No init ucode required? Curious, but maybe ok */
David Spinadel6dfa8d02012-03-10 13:00:14 -0800410 if (!priv->fw->img[IWL_UCODE_INIT].sec[0].len)
Johannes Bergca7966c2011-04-22 10:15:23 -0700411 return 0;
412
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800413 iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
Johannes Bergdb662d42012-03-15 13:26:44 -0700414 calib_complete, ARRAY_SIZE(calib_complete),
Johannes Berge1f0c502012-03-15 13:26:45 -0700415 iwlagn_wait_calib, priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700416
417 /* Will also start the device */
Johannes Berge1991882012-03-06 13:30:36 -0800418 ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700419 if (ret)
420 goto error;
421
Johannes Berge1991882012-03-06 13:30:36 -0800422 ret = iwl_init_alive_start(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700423 if (ret)
424 goto error;
425
426 /*
427 * Some things may run in the background now, but we
428 * just wait for the calibration complete notification.
429 */
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800430 ret = iwl_wait_notification(&priv->notif_wait, &calib_wait,
Don Frydd5fe102011-11-28 16:13:19 -0800431 UCODE_CALIB_TIMEOUT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700432
433 goto out;
434
435 error:
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800436 iwl_remove_notification(&priv->notif_wait, &calib_wait);
Johannes Bergca7966c2011-04-22 10:15:23 -0700437 out:
438 /* Whatever happened, stop the device */
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700439 iwl_trans_stop_device(priv->trans);
David Spinadel8f7ffbe2012-03-10 13:00:10 -0800440 priv->ucode_loaded = false;
441
Johannes Bergca7966c2011-04-22 10:15:23 -0700442 return ret;
443}