blob: 404fd8e7958b23839333b7d50d2a867a0c626daf [file] [log] [blame]
Wey-Yi Guy792bc3c2010-03-16 10:23:29 -07001/******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
Wey-Yi Guy4e318262011-12-27 11:21:32 -08005 * Copyright(c) 2008 - 2012 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
22 * in the file called LICENSE.GPL.
23 *
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#include <linux/init.h>
32
33#include "iwl-dev.h"
34#include "iwl-core.h"
Wey-Yi Guy81b81762010-03-16 10:23:30 -070035#include "iwl-io.h"
Wey-Yi Guy19e6cda2010-03-16 17:41:23 -070036#include "iwl-agn-hw.h"
Wey-Yi Guy741a6262010-03-16 12:37:24 -070037#include "iwl-agn.h"
Johannes Berg0de76732010-09-22 18:02:11 +020038#include "iwl-agn-calib.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070039#include "iwl-trans.h"
Emmanuel Grumbachdda61a42011-08-25 23:11:11 -070040#include "iwl-fh.h"
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +020041#include "iwl-op-mode.h"
Wey-Yi Guy741a6262010-03-16 12:37:24 -070042
Wey-Yi Guyf4012412010-04-27 14:10:00 -070043static struct iwl_wimax_coex_event_entry cu_priorities[COEX_NUM_OF_EVENTS] = {
44 {COEX_CU_UNASSOC_IDLE_RP, COEX_CU_UNASSOC_IDLE_WP,
45 0, COEX_UNASSOC_IDLE_FLAGS},
46 {COEX_CU_UNASSOC_MANUAL_SCAN_RP, COEX_CU_UNASSOC_MANUAL_SCAN_WP,
47 0, COEX_UNASSOC_MANUAL_SCAN_FLAGS},
48 {COEX_CU_UNASSOC_AUTO_SCAN_RP, COEX_CU_UNASSOC_AUTO_SCAN_WP,
49 0, COEX_UNASSOC_AUTO_SCAN_FLAGS},
50 {COEX_CU_CALIBRATION_RP, COEX_CU_CALIBRATION_WP,
51 0, COEX_CALIBRATION_FLAGS},
52 {COEX_CU_PERIODIC_CALIBRATION_RP, COEX_CU_PERIODIC_CALIBRATION_WP,
53 0, COEX_PERIODIC_CALIBRATION_FLAGS},
54 {COEX_CU_CONNECTION_ESTAB_RP, COEX_CU_CONNECTION_ESTAB_WP,
55 0, COEX_CONNECTION_ESTAB_FLAGS},
56 {COEX_CU_ASSOCIATED_IDLE_RP, COEX_CU_ASSOCIATED_IDLE_WP,
57 0, COEX_ASSOCIATED_IDLE_FLAGS},
58 {COEX_CU_ASSOC_MANUAL_SCAN_RP, COEX_CU_ASSOC_MANUAL_SCAN_WP,
59 0, COEX_ASSOC_MANUAL_SCAN_FLAGS},
60 {COEX_CU_ASSOC_AUTO_SCAN_RP, COEX_CU_ASSOC_AUTO_SCAN_WP,
61 0, COEX_ASSOC_AUTO_SCAN_FLAGS},
62 {COEX_CU_ASSOC_ACTIVE_LEVEL_RP, COEX_CU_ASSOC_ACTIVE_LEVEL_WP,
63 0, COEX_ASSOC_ACTIVE_LEVEL_FLAGS},
64 {COEX_CU_RF_ON_RP, COEX_CU_RF_ON_WP, 0, COEX_CU_RF_ON_FLAGS},
65 {COEX_CU_RF_OFF_RP, COEX_CU_RF_OFF_WP, 0, COEX_RF_OFF_FLAGS},
66 {COEX_CU_STAND_ALONE_DEBUG_RP, COEX_CU_STAND_ALONE_DEBUG_WP,
67 0, COEX_STAND_ALONE_DEBUG_FLAGS},
68 {COEX_CU_IPAN_ASSOC_LEVEL_RP, COEX_CU_IPAN_ASSOC_LEVEL_WP,
69 0, COEX_IPAN_ASSOC_LEVEL_FLAGS},
70 {COEX_CU_RSRVD1_RP, COEX_CU_RSRVD1_WP, 0, COEX_RSRVD1_FLAGS},
71 {COEX_CU_RSRVD2_RP, COEX_CU_RSRVD2_WP, 0, COEX_RSRVD2_FLAGS}
72};
73
Don Fryde7f5f92011-11-10 06:55:10 -080074/******************************************************************************
75 *
76 * uCode download functions
77 *
78 ******************************************************************************/
79
Johannes Berg0692fe42012-03-06 13:30:37 -080080static inline const struct fw_img *
81iwl_get_ucode_image(struct iwl_priv *priv, enum iwl_ucode_type ucode_type)
Don Fry8929c242011-11-10 06:55:08 -080082{
83 switch (ucode_type) {
84 case IWL_UCODE_INIT:
Johannes Berg0692fe42012-03-06 13:30:37 -080085 return &priv->fw->ucode_init;
Don Fry8929c242011-11-10 06:55:08 -080086 case IWL_UCODE_WOWLAN:
Johannes Berg0692fe42012-03-06 13:30:37 -080087 return &priv->fw->ucode_wowlan;
Don Fry8929c242011-11-10 06:55:08 -080088 case IWL_UCODE_REGULAR:
Johannes Berg0692fe42012-03-06 13:30:37 -080089 return &priv->fw->ucode_rt;
Don Fry8929c242011-11-10 06:55:08 -080090 case IWL_UCODE_NONE:
91 break;
92 }
93 return NULL;
94}
95
Wey-Yi Guy741a6262010-03-16 12:37:24 -070096/*
97 * Calibration
98 */
Johannes Berge1991882012-03-06 13:30:36 -080099static int iwl_set_Xtal_calib(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700100{
101 struct iwl_calib_xtal_freq_cmd cmd;
102 __le16 *xtal_calib =
Johannes Berge1991882012-03-06 13:30:36 -0800103 (__le16 *)iwl_eeprom_query_addr(priv->shrd, EEPROM_XTAL);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700104
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -0700105 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700106 cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
107 cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
Johannes Berge1991882012-03-06 13:30:36 -0800108 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700109}
110
Johannes Berge1991882012-03-06 13:30:36 -0800111static int iwl_set_temperature_offset_calib(struct iwl_priv *priv)
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700112{
113 struct iwl_calib_temperature_offset_cmd cmd;
114 __le16 *offset_calib =
Johannes Berge1991882012-03-06 13:30:36 -0800115 (__le16 *)iwl_eeprom_query_addr(priv->shrd,
Don Fryab36eab2011-11-30 15:37:32 -0800116 EEPROM_RAW_TEMPERATURE);
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -0700117
118 memset(&cmd, 0, sizeof(cmd));
119 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
Johannes Berg456fc372011-09-12 21:08:25 +0200120 memcpy(&cmd.radio_sensor_offset, offset_calib, sizeof(*offset_calib));
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700121 if (!(cmd.radio_sensor_offset))
122 cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -0700123
Johannes Berge1991882012-03-06 13:30:36 -0800124 IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
Wey-Yi Guy2e277992011-07-08 14:29:48 -0700125 le16_to_cpu(cmd.radio_sensor_offset));
Johannes Berge1991882012-03-06 13:30:36 -0800126 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700127}
128
Johannes Berge1991882012-03-06 13:30:36 -0800129static int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700130{
131 struct iwl_calib_temperature_offset_v2_cmd cmd;
Johannes Berge1991882012-03-06 13:30:36 -0800132 __le16 *offset_calib_high = (__le16 *)iwl_eeprom_query_addr(priv->shrd,
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700133 EEPROM_KELVIN_TEMPERATURE);
134 __le16 *offset_calib_low =
Johannes Berge1991882012-03-06 13:30:36 -0800135 (__le16 *)iwl_eeprom_query_addr(priv->shrd,
Don Fryab36eab2011-11-30 15:37:32 -0800136 EEPROM_RAW_TEMPERATURE);
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -0700137 struct iwl_eeprom_calib_hdr *hdr;
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700138
139 memset(&cmd, 0, sizeof(cmd));
140 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
Johannes Berge1991882012-03-06 13:30:36 -0800141 hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv->shrd,
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -0700142 EEPROM_CALIB_ALL);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700143 memcpy(&cmd.radio_sensor_offset_high, offset_calib_high,
Wey-Yi Guy00085002011-09-15 11:46:53 -0700144 sizeof(*offset_calib_high));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700145 memcpy(&cmd.radio_sensor_offset_low, offset_calib_low,
Wey-Yi Guy00085002011-09-15 11:46:53 -0700146 sizeof(*offset_calib_low));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700147 if (!(cmd.radio_sensor_offset_low)) {
Johannes Berge1991882012-03-06 13:30:36 -0800148 IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700149 cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
150 cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
151 }
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -0700152 memcpy(&cmd.burntVoltageRef, &hdr->voltage,
153 sizeof(hdr->voltage));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700154
Johannes Berge1991882012-03-06 13:30:36 -0800155 IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700156 le16_to_cpu(cmd.radio_sensor_offset_high));
Johannes Berge1991882012-03-06 13:30:36 -0800157 IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700158 le16_to_cpu(cmd.radio_sensor_offset_low));
Johannes Berge1991882012-03-06 13:30:36 -0800159 IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700160 le16_to_cpu(cmd.burntVoltageRef));
161
Johannes Berge1991882012-03-06 13:30:36 -0800162 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700163}
164
Johannes Berge1991882012-03-06 13:30:36 -0800165static int iwl_send_calib_cfg(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700166{
167 struct iwl_calib_cfg_cmd calib_cfg_cmd;
168 struct iwl_host_cmd cmd = {
169 .id = CALIBRATION_CFG_CMD,
Johannes Berg3fa50732011-05-04 07:50:38 -0700170 .len = { sizeof(struct iwl_calib_cfg_cmd), },
171 .data = { &calib_cfg_cmd, },
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700172 };
173
174 memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
175 calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
176 calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
177 calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
Wey-Yi Guydf2a4dc2011-07-08 14:29:45 -0700178 calib_cfg_cmd.ucd_calib_cfg.flags =
179 IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700180
Johannes Berge10a0532012-03-06 13:30:39 -0800181 return iwl_dvm_send_cmd(priv, &cmd);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700182}
183
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700184int iwlagn_rx_calib_result(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800185 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700186 struct iwl_device_cmd *cmd)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700187{
188 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800189 struct iwl_calib_hdr *hdr = (struct iwl_calib_hdr *)pkt->data;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700190 int len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700191
192 /* reduce the size of the length field itself */
193 len -= 4;
194
Johannes Berge1991882012-03-06 13:30:36 -0800195 if (iwl_calib_set(priv, hdr, len))
Johannes Bergf02c2fd2011-11-17 08:51:57 -0800196 IWL_ERR(priv, "Failed to record calibration data %d\n",
197 hdr->op_code);
198
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700199 return 0;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700200}
201
Johannes Berge1991882012-03-06 13:30:36 -0800202int iwl_init_alive_start(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700203{
Johannes Bergca7966c2011-04-22 10:15:23 -0700204 int ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700205
Johannes Berge1991882012-03-06 13:30:36 -0800206 if (cfg(priv)->bt_params &&
207 cfg(priv)->bt_params->advanced_bt_coexist) {
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700208 /*
209 * Tell uCode we are ready to perform calibration
210 * need to perform this before any calibration
211 * no need to close the envlope since we are going
212 * to load the runtime uCode later.
213 */
Johannes Berge1991882012-03-06 13:30:36 -0800214 ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700215 BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
Johannes Bergca7966c2011-04-22 10:15:23 -0700216 if (ret)
217 return ret;
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700218
219 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700220
Johannes Berge1991882012-03-06 13:30:36 -0800221 ret = iwl_send_calib_cfg(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700222 if (ret)
223 return ret;
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700224
225 /**
226 * temperature offset calibration is only needed for runtime ucode,
227 * so prepare the value now.
228 */
Johannes Berge1991882012-03-06 13:30:36 -0800229 if (cfg(priv)->need_temp_offset_calib) {
230 if (cfg(priv)->temp_offset_v2)
231 return iwl_set_temperature_offset_calib_v2(priv);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700232 else
Johannes Berge1991882012-03-06 13:30:36 -0800233 return iwl_set_temperature_offset_calib(priv);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700234 }
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700235
Johannes Bergca7966c2011-04-22 10:15:23 -0700236 return 0;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700237}
238
Johannes Berge1991882012-03-06 13:30:36 -0800239static int iwl_send_wimax_coex(struct iwl_priv *priv)
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700240{
241 struct iwl_wimax_coex_cmd coex_cmd;
242
Johannes Berge1991882012-03-06 13:30:36 -0800243 if (cfg(priv)->base_params->support_wimax_coexist) {
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700244 /* UnMask wake up src at associated sleep */
245 coex_cmd.flags = COEX_FLAGS_ASSOC_WA_UNMASK_MSK;
246
247 /* UnMask wake up src at unassociated sleep */
248 coex_cmd.flags |= COEX_FLAGS_UNASSOC_WA_UNMASK_MSK;
249 memcpy(coex_cmd.sta_prio, cu_priorities,
250 sizeof(struct iwl_wimax_coex_event_entry) *
251 COEX_NUM_OF_EVENTS);
252
253 /* enabling the coexistence feature */
254 coex_cmd.flags |= COEX_FLAGS_COEX_ENABLE_MSK;
255
256 /* enabling the priorities tables */
257 coex_cmd.flags |= COEX_FLAGS_STA_TABLE_VALID_MSK;
258 } else {
259 /* coexistence is disabled */
260 memset(&coex_cmd, 0, sizeof(coex_cmd));
261 }
Johannes Berge10a0532012-03-06 13:30:39 -0800262 return iwl_dvm_send_cmd_pdu(priv,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700263 COEX_PRIORITY_TABLE_CMD, CMD_SYNC,
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700264 sizeof(coex_cmd), &coex_cmd);
265}
266
Don Fry66128b12011-11-28 14:35:14 -0800267static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700268 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
269 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
270 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
271 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
272 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
273 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
274 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
275 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
276 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
277 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
278 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
279 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
280 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
281 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
282 ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
283 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
284 ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
285 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
286 0, 0, 0, 0, 0, 0, 0
287};
288
Johannes Berge1991882012-03-06 13:30:36 -0800289void iwl_send_prio_tbl(struct iwl_priv *priv)
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700290{
291 struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
292
Don Fry66128b12011-11-28 14:35:14 -0800293 memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
294 sizeof(iwl_bt_prio_tbl));
Johannes Berge10a0532012-03-06 13:30:39 -0800295 if (iwl_dvm_send_cmd_pdu(priv,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700296 REPLY_BT_COEX_PRIO_TABLE, CMD_SYNC,
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700297 sizeof(prio_tbl_cmd), &prio_tbl_cmd))
Johannes Berge1991882012-03-06 13:30:36 -0800298 IWL_ERR(priv, "failed to send BT prio tbl command\n");
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700299}
300
Johannes Berge1991882012-03-06 13:30:36 -0800301int iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700302{
303 struct iwl_bt_coex_prot_env_cmd env_cmd;
Johannes Bergca7966c2011-04-22 10:15:23 -0700304 int ret;
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700305
306 env_cmd.action = action;
307 env_cmd.type = type;
Johannes Berge10a0532012-03-06 13:30:39 -0800308 ret = iwl_dvm_send_cmd_pdu(priv,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700309 REPLY_BT_COEX_PROT_ENV, CMD_SYNC,
Johannes Bergca7966c2011-04-22 10:15:23 -0700310 sizeof(env_cmd), &env_cmd);
311 if (ret)
Johannes Berge1991882012-03-06 13:30:36 -0800312 IWL_ERR(priv, "failed to send BT env command\n");
Johannes Bergca7966c2011-04-22 10:15:23 -0700313 return ret;
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700314}
315
316
Johannes Berge1991882012-03-06 13:30:36 -0800317static int iwl_alive_notify(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700318{
Wey-Yi Guy74159522011-04-05 09:42:01 -0700319 int ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700320
Emmanuel Grumbachdfa2bdb2011-08-25 23:11:23 -0700321 if (!priv->tx_cmd_pool)
322 priv->tx_cmd_pool =
Don Fry66128b12011-11-28 14:35:14 -0800323 kmem_cache_create("iwl_dev_cmd",
Emmanuel Grumbachdfa2bdb2011-08-25 23:11:23 -0700324 sizeof(struct iwl_device_cmd),
325 sizeof(void *), 0, NULL);
326
327 if (!priv->tx_cmd_pool)
328 return -ENOMEM;
329
Johannes Berge1991882012-03-06 13:30:36 -0800330 iwl_trans_fw_alive(trans(priv));
Johannes Berge755f882012-03-07 09:52:16 -0800331
332 priv->passive_no_rx = false;
333 priv->transport_queue_stop = 0;
Grumbach, Emmanuele7cad692010-11-18 03:47:38 -0800334
Johannes Berge1991882012-03-06 13:30:36 -0800335 ret = iwl_send_wimax_coex(priv);
Wey-Yi Guy74159522011-04-05 09:42:01 -0700336 if (ret)
337 return ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700338
Don Fry38622412011-12-16 07:07:36 -0800339 if (!cfg(priv)->no_xtal_calib) {
Johannes Berge1991882012-03-06 13:30:36 -0800340 ret = iwl_set_Xtal_calib(priv);
Johannes Berg93b64102011-11-17 08:51:53 -0800341 if (ret)
342 return ret;
343 }
Wey-Yi Guy74159522011-04-05 09:42:01 -0700344
Johannes Berge1991882012-03-06 13:30:36 -0800345 return iwl_send_calib_results(priv);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700346}
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700347
348
349/**
350 * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
351 * using sample data 100 bytes apart. If these sample points are good,
352 * it's a pretty good bet that everything between them is good, too.
353 */
Johannes Berge1991882012-03-06 13:30:36 -0800354static int iwl_verify_inst_sparse(struct iwl_priv *priv,
Johannes Berg0692fe42012-03-06 13:30:37 -0800355 const struct fw_desc *fw_desc)
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700356{
Johannes Berg35b1d922011-04-05 09:41:56 -0700357 __le32 *image = (__le32 *)fw_desc->v_addr;
358 u32 len = fw_desc->len;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700359 u32 val;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700360 u32 i;
361
Johannes Berge1991882012-03-06 13:30:36 -0800362 IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700363
364 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
365 /* read data comes through single port, auto-incr addr */
366 /* NOTE: Use the debugless read so we don't flood kernel log
367 * if IWL_DL_IO is set */
Johannes Berge1991882012-03-06 13:30:36 -0800368 iwl_write_direct32(trans(priv), HBUS_TARG_MEM_RADDR,
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700369 i + IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berge1991882012-03-06 13:30:36 -0800370 val = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
Johannes Bergfb662162011-04-05 09:41:55 -0700371 if (val != le32_to_cpu(*image))
372 return -EIO;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700373 }
374
Johannes Bergfb662162011-04-05 09:41:55 -0700375 return 0;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700376}
377
Johannes Berge1991882012-03-06 13:30:36 -0800378static void iwl_print_mismatch_inst(struct iwl_priv *priv,
Johannes Berg0692fe42012-03-06 13:30:37 -0800379 const struct fw_desc *fw_desc)
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700380{
Johannes Berg35b1d922011-04-05 09:41:56 -0700381 __le32 *image = (__le32 *)fw_desc->v_addr;
382 u32 len = fw_desc->len;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700383 u32 val;
Johannes Bergfb662162011-04-05 09:41:55 -0700384 u32 offs;
385 int errors = 0;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700386
Johannes Berge1991882012-03-06 13:30:36 -0800387 IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700388
Johannes Berge1991882012-03-06 13:30:36 -0800389 iwl_write_direct32(trans(priv), HBUS_TARG_MEM_RADDR,
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700390 IWLAGN_RTC_INST_LOWER_BOUND);
391
Johannes Bergfb662162011-04-05 09:41:55 -0700392 for (offs = 0;
393 offs < len && errors < 20;
394 offs += sizeof(u32), image++) {
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700395 /* read data comes through single port, auto-incr addr */
Johannes Berge1991882012-03-06 13:30:36 -0800396 val = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700397 if (val != le32_to_cpu(*image)) {
Johannes Berge1991882012-03-06 13:30:36 -0800398 IWL_ERR(priv, "uCode INST section at "
Johannes Bergfb662162011-04-05 09:41:55 -0700399 "offset 0x%x, is 0x%x, s/b 0x%x\n",
400 offs, val, le32_to_cpu(*image));
401 errors++;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700402 }
403 }
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700404}
405
406/**
407 * iwl_verify_ucode - determine which instruction image is in SRAM,
408 * and verify its contents
409 */
Johannes Berge1991882012-03-06 13:30:36 -0800410static int iwl_verify_ucode(struct iwl_priv *priv,
Don Fryde7f5f92011-11-10 06:55:10 -0800411 enum iwl_ucode_type ucode_type)
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700412{
Johannes Berg0692fe42012-03-06 13:30:37 -0800413 const struct fw_img *img = iwl_get_ucode_image(priv, ucode_type);
Don Frybaa00052011-11-10 06:55:09 -0800414
415 if (!img) {
Johannes Berge1991882012-03-06 13:30:36 -0800416 IWL_ERR(priv, "Invalid ucode requested (%d)\n", ucode_type);
Don Frybaa00052011-11-10 06:55:09 -0800417 return -EINVAL;
418 }
419
Johannes Berge1991882012-03-06 13:30:36 -0800420 if (!iwl_verify_inst_sparse(priv, &img->code)) {
421 IWL_DEBUG_FW(priv, "uCode is good in inst SRAM\n");
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700422 return 0;
423 }
424
Johannes Berge1991882012-03-06 13:30:36 -0800425 IWL_ERR(priv, "UCODE IMAGE IN INSTRUCTION SRAM NOT VALID!!\n");
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700426
Johannes Berge1991882012-03-06 13:30:36 -0800427 iwl_print_mismatch_inst(priv, &img->code);
Johannes Bergfb662162011-04-05 09:41:55 -0700428 return -EIO;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700429}
Johannes Bergca7966c2011-04-22 10:15:23 -0700430
Don Fry69a679b2011-12-07 08:50:46 -0800431struct iwl_alive_data {
Johannes Bergca7966c2011-04-22 10:15:23 -0700432 bool valid;
433 u8 subtype;
434};
435
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800436static void iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
Johannes Bergca7966c2011-04-22 10:15:23 -0700437 struct iwl_rx_packet *pkt,
438 void *data)
439{
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800440 struct iwl_priv *priv =
441 container_of(notif_wait, struct iwl_priv, notif_wait);
Don Fry69a679b2011-12-07 08:50:46 -0800442 struct iwl_alive_data *alive_data = data;
Johannes Bergca7966c2011-04-22 10:15:23 -0700443 struct iwl_alive_resp *palive;
444
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800445 palive = (void *)pkt->data;
Johannes Bergca7966c2011-04-22 10:15:23 -0700446
Johannes Berge1991882012-03-06 13:30:36 -0800447 IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
Johannes Bergca7966c2011-04-22 10:15:23 -0700448 "0x%01X 0x%01X\n",
449 palive->is_valid, palive->ver_type,
450 palive->ver_subtype);
451
Johannes Berge1991882012-03-06 13:30:36 -0800452 priv->shrd->device_pointers.error_event_table =
Johannes Bergca7966c2011-04-22 10:15:23 -0700453 le32_to_cpu(palive->error_event_table_ptr);
Johannes Berge1991882012-03-06 13:30:36 -0800454 priv->shrd->device_pointers.log_event_table =
Johannes Bergca7966c2011-04-22 10:15:23 -0700455 le32_to_cpu(palive->log_event_table_ptr);
456
457 alive_data->subtype = palive->ver_subtype;
458 alive_data->valid = palive->is_valid == UCODE_VALID_OK;
459}
460
461#define UCODE_ALIVE_TIMEOUT HZ
462#define UCODE_CALIB_TIMEOUT (2*HZ)
463
Johannes Berge1991882012-03-06 13:30:36 -0800464int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
Don Fryde7f5f92011-11-10 06:55:10 -0800465 enum iwl_ucode_type ucode_type)
Johannes Bergca7966c2011-04-22 10:15:23 -0700466{
467 struct iwl_notification_wait alive_wait;
Don Fry69a679b2011-12-07 08:50:46 -0800468 struct iwl_alive_data alive_data;
Johannes Berg0692fe42012-03-06 13:30:37 -0800469 const struct fw_img *fw;
Johannes Bergca7966c2011-04-22 10:15:23 -0700470 int ret;
Don Fryde7f5f92011-11-10 06:55:10 -0800471 enum iwl_ucode_type old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700472
Johannes Berge1991882012-03-06 13:30:36 -0800473 old_type = priv->shrd->ucode_type;
474 priv->shrd->ucode_type = ucode_type;
475 fw = iwl_get_ucode_image(priv, ucode_type);
Johannes Bergca7966c2011-04-22 10:15:23 -0700476
Emmanuel Grumbachcf614292012-01-08 16:33:58 +0200477 if (!fw)
478 return -EINVAL;
479
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800480 iwl_init_notification_wait(&priv->notif_wait, &alive_wait, REPLY_ALIVE,
Johannes Bergf4720892012-03-06 13:30:57 -0800481 iwl_alive_fn, &alive_data);
482
Johannes Berge1991882012-03-06 13:30:36 -0800483 ret = iwl_trans_start_fw(trans(priv), fw);
Johannes Bergca7966c2011-04-22 10:15:23 -0700484 if (ret) {
Johannes Berge1991882012-03-06 13:30:36 -0800485 priv->shrd->ucode_type = old_type;
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800486 iwl_remove_notification(&priv->notif_wait, &alive_wait);
Johannes Bergca7966c2011-04-22 10:15:23 -0700487 return ret;
488 }
489
Johannes Bergca7966c2011-04-22 10:15:23 -0700490 /*
491 * Some things may run in the background now, but we
492 * just wait for the ALIVE notification here.
493 */
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800494 ret = iwl_wait_notification(&priv->notif_wait, &alive_wait,
Don Frydd5fe102011-11-28 16:13:19 -0800495 UCODE_ALIVE_TIMEOUT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700496 if (ret) {
Johannes Berge1991882012-03-06 13:30:36 -0800497 priv->shrd->ucode_type = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700498 return ret;
499 }
500
501 if (!alive_data.valid) {
Johannes Berge1991882012-03-06 13:30:36 -0800502 IWL_ERR(priv, "Loaded ucode is not valid!\n");
503 priv->shrd->ucode_type = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700504 return -EIO;
505 }
506
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700507 /*
508 * This step takes a long time (60-80ms!!) and
509 * WoWLAN image should be loaded quickly, so
510 * skip it for WoWLAN.
511 */
512 if (ucode_type != IWL_UCODE_WOWLAN) {
Johannes Berge1991882012-03-06 13:30:36 -0800513 ret = iwl_verify_ucode(priv, ucode_type);
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700514 if (ret) {
Johannes Berge1991882012-03-06 13:30:36 -0800515 priv->shrd->ucode_type = old_type;
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700516 return ret;
517 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700518
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700519 /* delay a bit to give rfkill time to run */
520 msleep(5);
521 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700522
Johannes Berge1991882012-03-06 13:30:36 -0800523 ret = iwl_alive_notify(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700524 if (ret) {
Johannes Berge1991882012-03-06 13:30:36 -0800525 IWL_WARN(priv,
Johannes Bergca7966c2011-04-22 10:15:23 -0700526 "Could not complete ALIVE transition: %d\n", ret);
Johannes Berge1991882012-03-06 13:30:36 -0800527 priv->shrd->ucode_type = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700528 return ret;
529 }
530
531 return 0;
532}
533
Johannes Berge1991882012-03-06 13:30:36 -0800534int iwl_run_init_ucode(struct iwl_priv *priv)
Johannes Bergca7966c2011-04-22 10:15:23 -0700535{
536 struct iwl_notification_wait calib_wait;
537 int ret;
538
Johannes Bergb1eea292012-03-06 13:30:42 -0800539 lockdep_assert_held(&priv->mutex);
Johannes Bergca7966c2011-04-22 10:15:23 -0700540
541 /* No init ucode required? Curious, but maybe ok */
Johannes Berg0692fe42012-03-06 13:30:37 -0800542 if (!priv->fw->ucode_init.code.len)
Johannes Bergca7966c2011-04-22 10:15:23 -0700543 return 0;
544
Johannes Berge1991882012-03-06 13:30:36 -0800545 if (priv->shrd->ucode_type != IWL_UCODE_NONE)
Johannes Bergca7966c2011-04-22 10:15:23 -0700546 return 0;
547
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800548 iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
Johannes Bergca7966c2011-04-22 10:15:23 -0700549 CALIBRATION_COMPLETE_NOTIFICATION,
550 NULL, NULL);
551
552 /* Will also start the device */
Johannes Berge1991882012-03-06 13:30:36 -0800553 ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700554 if (ret)
555 goto error;
556
Johannes Berge1991882012-03-06 13:30:36 -0800557 ret = iwl_init_alive_start(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700558 if (ret)
559 goto error;
560
561 /*
562 * Some things may run in the background now, but we
563 * just wait for the calibration complete notification.
564 */
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800565 ret = iwl_wait_notification(&priv->notif_wait, &calib_wait,
Don Frydd5fe102011-11-28 16:13:19 -0800566 UCODE_CALIB_TIMEOUT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700567
568 goto out;
569
570 error:
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800571 iwl_remove_notification(&priv->notif_wait, &calib_wait);
Johannes Bergca7966c2011-04-22 10:15:23 -0700572 out:
573 /* Whatever happened, stop the device */
Johannes Berge1991882012-03-06 13:30:36 -0800574 iwl_trans_stop_device(trans(priv));
Johannes Bergca7966c2011-04-22 10:15:23 -0700575 return ret;
576}