blob: 340575a01eade233547585089ff97f2262439f75 [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>
Wey-Yi Guy81b81762010-03-16 10:23:30 -070032#include <linux/sched.h>
Wey-Yi Guy792bc3c2010-03-16 10:23:29 -070033
Don Fryedf38332012-01-25 16:18:52 -080034#include "iwl-ucode.h"
Don Fry69a679b2011-12-07 08:50:46 -080035#include "iwl-wifi.h"
Wey-Yi Guy792bc3c2010-03-16 10:23:29 -070036#include "iwl-dev.h"
37#include "iwl-core.h"
Wey-Yi Guy81b81762010-03-16 10:23:30 -070038#include "iwl-io.h"
Wey-Yi Guy19e6cda2010-03-16 17:41:23 -070039#include "iwl-agn-hw.h"
Wey-Yi Guy741a6262010-03-16 12:37:24 -070040#include "iwl-agn.h"
Johannes Berg0de76732010-09-22 18:02:11 +020041#include "iwl-agn-calib.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070042#include "iwl-trans.h"
Emmanuel Grumbachdda61a42011-08-25 23:11:11 -070043#include "iwl-fh.h"
Emmanuel Grumbachd0f76d62012-02-09 16:08:15 +020044#include "iwl-op-mode.h"
Wey-Yi Guy741a6262010-03-16 12:37:24 -070045
Wey-Yi Guyf4012412010-04-27 14:10:00 -070046static struct iwl_wimax_coex_event_entry cu_priorities[COEX_NUM_OF_EVENTS] = {
47 {COEX_CU_UNASSOC_IDLE_RP, COEX_CU_UNASSOC_IDLE_WP,
48 0, COEX_UNASSOC_IDLE_FLAGS},
49 {COEX_CU_UNASSOC_MANUAL_SCAN_RP, COEX_CU_UNASSOC_MANUAL_SCAN_WP,
50 0, COEX_UNASSOC_MANUAL_SCAN_FLAGS},
51 {COEX_CU_UNASSOC_AUTO_SCAN_RP, COEX_CU_UNASSOC_AUTO_SCAN_WP,
52 0, COEX_UNASSOC_AUTO_SCAN_FLAGS},
53 {COEX_CU_CALIBRATION_RP, COEX_CU_CALIBRATION_WP,
54 0, COEX_CALIBRATION_FLAGS},
55 {COEX_CU_PERIODIC_CALIBRATION_RP, COEX_CU_PERIODIC_CALIBRATION_WP,
56 0, COEX_PERIODIC_CALIBRATION_FLAGS},
57 {COEX_CU_CONNECTION_ESTAB_RP, COEX_CU_CONNECTION_ESTAB_WP,
58 0, COEX_CONNECTION_ESTAB_FLAGS},
59 {COEX_CU_ASSOCIATED_IDLE_RP, COEX_CU_ASSOCIATED_IDLE_WP,
60 0, COEX_ASSOCIATED_IDLE_FLAGS},
61 {COEX_CU_ASSOC_MANUAL_SCAN_RP, COEX_CU_ASSOC_MANUAL_SCAN_WP,
62 0, COEX_ASSOC_MANUAL_SCAN_FLAGS},
63 {COEX_CU_ASSOC_AUTO_SCAN_RP, COEX_CU_ASSOC_AUTO_SCAN_WP,
64 0, COEX_ASSOC_AUTO_SCAN_FLAGS},
65 {COEX_CU_ASSOC_ACTIVE_LEVEL_RP, COEX_CU_ASSOC_ACTIVE_LEVEL_WP,
66 0, COEX_ASSOC_ACTIVE_LEVEL_FLAGS},
67 {COEX_CU_RF_ON_RP, COEX_CU_RF_ON_WP, 0, COEX_CU_RF_ON_FLAGS},
68 {COEX_CU_RF_OFF_RP, COEX_CU_RF_OFF_WP, 0, COEX_RF_OFF_FLAGS},
69 {COEX_CU_STAND_ALONE_DEBUG_RP, COEX_CU_STAND_ALONE_DEBUG_WP,
70 0, COEX_STAND_ALONE_DEBUG_FLAGS},
71 {COEX_CU_IPAN_ASSOC_LEVEL_RP, COEX_CU_IPAN_ASSOC_LEVEL_WP,
72 0, COEX_IPAN_ASSOC_LEVEL_FLAGS},
73 {COEX_CU_RSRVD1_RP, COEX_CU_RSRVD1_WP, 0, COEX_RSRVD1_FLAGS},
74 {COEX_CU_RSRVD2_RP, COEX_CU_RSRVD2_WP, 0, COEX_RSRVD2_FLAGS}
75};
76
Don Fryde7f5f92011-11-10 06:55:10 -080077/******************************************************************************
78 *
79 * uCode download functions
80 *
81 ******************************************************************************/
82
Johannes Berge1991882012-03-06 13:30:36 -080083static inline struct fw_img *iwl_get_ucode_image(struct iwl_priv *priv,
84 enum iwl_ucode_type ucode_type)
Don Fry8929c242011-11-10 06:55:08 -080085{
86 switch (ucode_type) {
87 case IWL_UCODE_INIT:
Johannes Berge1991882012-03-06 13:30:36 -080088 return &nic(priv)->fw.ucode_init;
Don Fry8929c242011-11-10 06:55:08 -080089 case IWL_UCODE_WOWLAN:
Johannes Berge1991882012-03-06 13:30:36 -080090 return &nic(priv)->fw.ucode_wowlan;
Don Fry8929c242011-11-10 06:55:08 -080091 case IWL_UCODE_REGULAR:
Johannes Berge1991882012-03-06 13:30:36 -080092 return &nic(priv)->fw.ucode_rt;
Don Fry8929c242011-11-10 06:55:08 -080093 case IWL_UCODE_NONE:
94 break;
95 }
96 return NULL;
97}
98
Wey-Yi Guy741a6262010-03-16 12:37:24 -070099/*
100 * Calibration
101 */
Johannes Berge1991882012-03-06 13:30:36 -0800102static int iwl_set_Xtal_calib(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700103{
104 struct iwl_calib_xtal_freq_cmd cmd;
105 __le16 *xtal_calib =
Johannes Berge1991882012-03-06 13:30:36 -0800106 (__le16 *)iwl_eeprom_query_addr(priv->shrd, EEPROM_XTAL);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700107
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -0700108 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700109 cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
110 cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
Johannes Berge1991882012-03-06 13:30:36 -0800111 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700112}
113
Johannes Berge1991882012-03-06 13:30:36 -0800114static int iwl_set_temperature_offset_calib(struct iwl_priv *priv)
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700115{
116 struct iwl_calib_temperature_offset_cmd cmd;
117 __le16 *offset_calib =
Johannes Berge1991882012-03-06 13:30:36 -0800118 (__le16 *)iwl_eeprom_query_addr(priv->shrd,
Don Fryab36eab2011-11-30 15:37:32 -0800119 EEPROM_RAW_TEMPERATURE);
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -0700120
121 memset(&cmd, 0, sizeof(cmd));
122 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
Johannes Berg456fc372011-09-12 21:08:25 +0200123 memcpy(&cmd.radio_sensor_offset, offset_calib, sizeof(*offset_calib));
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700124 if (!(cmd.radio_sensor_offset))
125 cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -0700126
Johannes Berge1991882012-03-06 13:30:36 -0800127 IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
Wey-Yi Guy2e277992011-07-08 14:29:48 -0700128 le16_to_cpu(cmd.radio_sensor_offset));
Johannes Berge1991882012-03-06 13:30:36 -0800129 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700130}
131
Johannes Berge1991882012-03-06 13:30:36 -0800132static int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700133{
134 struct iwl_calib_temperature_offset_v2_cmd cmd;
Johannes Berge1991882012-03-06 13:30:36 -0800135 __le16 *offset_calib_high = (__le16 *)iwl_eeprom_query_addr(priv->shrd,
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700136 EEPROM_KELVIN_TEMPERATURE);
137 __le16 *offset_calib_low =
Johannes Berge1991882012-03-06 13:30:36 -0800138 (__le16 *)iwl_eeprom_query_addr(priv->shrd,
Don Fryab36eab2011-11-30 15:37:32 -0800139 EEPROM_RAW_TEMPERATURE);
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -0700140 struct iwl_eeprom_calib_hdr *hdr;
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700141
142 memset(&cmd, 0, sizeof(cmd));
143 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
Johannes Berge1991882012-03-06 13:30:36 -0800144 hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv->shrd,
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -0700145 EEPROM_CALIB_ALL);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700146 memcpy(&cmd.radio_sensor_offset_high, offset_calib_high,
Wey-Yi Guy00085002011-09-15 11:46:53 -0700147 sizeof(*offset_calib_high));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700148 memcpy(&cmd.radio_sensor_offset_low, offset_calib_low,
Wey-Yi Guy00085002011-09-15 11:46:53 -0700149 sizeof(*offset_calib_low));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700150 if (!(cmd.radio_sensor_offset_low)) {
Johannes Berge1991882012-03-06 13:30:36 -0800151 IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700152 cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
153 cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
154 }
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -0700155 memcpy(&cmd.burntVoltageRef, &hdr->voltage,
156 sizeof(hdr->voltage));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700157
Johannes Berge1991882012-03-06 13:30:36 -0800158 IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700159 le16_to_cpu(cmd.radio_sensor_offset_high));
Johannes Berge1991882012-03-06 13:30:36 -0800160 IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700161 le16_to_cpu(cmd.radio_sensor_offset_low));
Johannes Berge1991882012-03-06 13:30:36 -0800162 IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700163 le16_to_cpu(cmd.burntVoltageRef));
164
Johannes Berge1991882012-03-06 13:30:36 -0800165 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700166}
167
Johannes Berge1991882012-03-06 13:30:36 -0800168static int iwl_send_calib_cfg(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700169{
170 struct iwl_calib_cfg_cmd calib_cfg_cmd;
171 struct iwl_host_cmd cmd = {
172 .id = CALIBRATION_CFG_CMD,
Johannes Berg3fa50732011-05-04 07:50:38 -0700173 .len = { sizeof(struct iwl_calib_cfg_cmd), },
174 .data = { &calib_cfg_cmd, },
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700175 };
176
177 memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
178 calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
179 calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
180 calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
Wey-Yi Guydf2a4dc2011-07-08 14:29:45 -0700181 calib_cfg_cmd.ucd_calib_cfg.flags =
182 IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700183
Johannes Berge1991882012-03-06 13:30:36 -0800184 return iwl_trans_send_cmd(trans(priv), &cmd);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700185}
186
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700187int iwlagn_rx_calib_result(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800188 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700189 struct iwl_device_cmd *cmd)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700190{
191 struct iwl_rx_packet *pkt = rxb_addr(rxb);
192 struct iwl_calib_hdr *hdr = (struct iwl_calib_hdr *)pkt->u.raw;
193 int len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700194
195 /* reduce the size of the length field itself */
196 len -= 4;
197
Johannes Berge1991882012-03-06 13:30:36 -0800198 if (iwl_calib_set(priv, hdr, len))
Johannes Bergf02c2fd2011-11-17 08:51:57 -0800199 IWL_ERR(priv, "Failed to record calibration data %d\n",
200 hdr->op_code);
201
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700202 return 0;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700203}
204
Johannes Berge1991882012-03-06 13:30:36 -0800205int iwl_init_alive_start(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700206{
Johannes Bergca7966c2011-04-22 10:15:23 -0700207 int ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700208
Johannes Berge1991882012-03-06 13:30:36 -0800209 if (cfg(priv)->bt_params &&
210 cfg(priv)->bt_params->advanced_bt_coexist) {
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700211 /*
212 * Tell uCode we are ready to perform calibration
213 * need to perform this before any calibration
214 * no need to close the envlope since we are going
215 * to load the runtime uCode later.
216 */
Johannes Berge1991882012-03-06 13:30:36 -0800217 ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700218 BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
Johannes Bergca7966c2011-04-22 10:15:23 -0700219 if (ret)
220 return ret;
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700221
222 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700223
Johannes Berge1991882012-03-06 13:30:36 -0800224 ret = iwl_send_calib_cfg(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700225 if (ret)
226 return ret;
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700227
228 /**
229 * temperature offset calibration is only needed for runtime ucode,
230 * so prepare the value now.
231 */
Johannes Berge1991882012-03-06 13:30:36 -0800232 if (cfg(priv)->need_temp_offset_calib) {
233 if (cfg(priv)->temp_offset_v2)
234 return iwl_set_temperature_offset_calib_v2(priv);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700235 else
Johannes Berge1991882012-03-06 13:30:36 -0800236 return iwl_set_temperature_offset_calib(priv);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700237 }
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700238
Johannes Bergca7966c2011-04-22 10:15:23 -0700239 return 0;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700240}
241
Johannes Berge1991882012-03-06 13:30:36 -0800242static int iwl_send_wimax_coex(struct iwl_priv *priv)
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700243{
244 struct iwl_wimax_coex_cmd coex_cmd;
245
Johannes Berge1991882012-03-06 13:30:36 -0800246 if (cfg(priv)->base_params->support_wimax_coexist) {
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700247 /* UnMask wake up src at associated sleep */
248 coex_cmd.flags = COEX_FLAGS_ASSOC_WA_UNMASK_MSK;
249
250 /* UnMask wake up src at unassociated sleep */
251 coex_cmd.flags |= COEX_FLAGS_UNASSOC_WA_UNMASK_MSK;
252 memcpy(coex_cmd.sta_prio, cu_priorities,
253 sizeof(struct iwl_wimax_coex_event_entry) *
254 COEX_NUM_OF_EVENTS);
255
256 /* enabling the coexistence feature */
257 coex_cmd.flags |= COEX_FLAGS_COEX_ENABLE_MSK;
258
259 /* enabling the priorities tables */
260 coex_cmd.flags |= COEX_FLAGS_STA_TABLE_VALID_MSK;
261 } else {
262 /* coexistence is disabled */
263 memset(&coex_cmd, 0, sizeof(coex_cmd));
264 }
Johannes Berge1991882012-03-06 13:30:36 -0800265 return iwl_trans_send_cmd_pdu(trans(priv),
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700266 COEX_PRIORITY_TABLE_CMD, CMD_SYNC,
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700267 sizeof(coex_cmd), &coex_cmd);
268}
269
Don Fry66128b12011-11-28 14:35:14 -0800270static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700271 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
272 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
273 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
274 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
275 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
276 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
277 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
278 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
279 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
280 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
281 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
282 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
283 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
284 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
285 ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
286 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
287 ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
288 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
289 0, 0, 0, 0, 0, 0, 0
290};
291
Johannes Berge1991882012-03-06 13:30:36 -0800292void iwl_send_prio_tbl(struct iwl_priv *priv)
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700293{
294 struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
295
Don Fry66128b12011-11-28 14:35:14 -0800296 memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
297 sizeof(iwl_bt_prio_tbl));
Johannes Berge1991882012-03-06 13:30:36 -0800298 if (iwl_trans_send_cmd_pdu(trans(priv),
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700299 REPLY_BT_COEX_PRIO_TABLE, CMD_SYNC,
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700300 sizeof(prio_tbl_cmd), &prio_tbl_cmd))
Johannes Berge1991882012-03-06 13:30:36 -0800301 IWL_ERR(priv, "failed to send BT prio tbl command\n");
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700302}
303
Johannes Berge1991882012-03-06 13:30:36 -0800304int iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700305{
306 struct iwl_bt_coex_prot_env_cmd env_cmd;
Johannes Bergca7966c2011-04-22 10:15:23 -0700307 int ret;
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700308
309 env_cmd.action = action;
310 env_cmd.type = type;
Johannes Berge1991882012-03-06 13:30:36 -0800311 ret = iwl_trans_send_cmd_pdu(trans(priv),
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700312 REPLY_BT_COEX_PROT_ENV, CMD_SYNC,
Johannes Bergca7966c2011-04-22 10:15:23 -0700313 sizeof(env_cmd), &env_cmd);
314 if (ret)
Johannes Berge1991882012-03-06 13:30:36 -0800315 IWL_ERR(priv, "failed to send BT env command\n");
Johannes Bergca7966c2011-04-22 10:15:23 -0700316 return ret;
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700317}
318
319
Johannes Berge1991882012-03-06 13:30:36 -0800320static int iwl_alive_notify(struct iwl_priv *priv)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700321{
Emmanuel Grumbach7a10e3e42011-09-06 09:31:21 -0700322 struct iwl_rxon_context *ctx;
Wey-Yi Guy74159522011-04-05 09:42:01 -0700323 int ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700324
Emmanuel Grumbachdfa2bdb2011-08-25 23:11:23 -0700325 if (!priv->tx_cmd_pool)
326 priv->tx_cmd_pool =
Don Fry66128b12011-11-28 14:35:14 -0800327 kmem_cache_create("iwl_dev_cmd",
Emmanuel Grumbachdfa2bdb2011-08-25 23:11:23 -0700328 sizeof(struct iwl_device_cmd),
329 sizeof(void *), 0, NULL);
330
331 if (!priv->tx_cmd_pool)
332 return -ENOMEM;
333
Johannes Berge1991882012-03-06 13:30:36 -0800334 iwl_trans_fw_alive(trans(priv));
Emmanuel Grumbach7a10e3e42011-09-06 09:31:21 -0700335 for_each_context(priv, ctx)
336 ctx->last_tx_rejected = false;
Grumbach, Emmanuele7cad692010-11-18 03:47:38 -0800337
Johannes Berge1991882012-03-06 13:30:36 -0800338 ret = iwl_send_wimax_coex(priv);
Wey-Yi Guy74159522011-04-05 09:42:01 -0700339 if (ret)
340 return ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700341
Don Fry38622412011-12-16 07:07:36 -0800342 if (!cfg(priv)->no_xtal_calib) {
Johannes Berge1991882012-03-06 13:30:36 -0800343 ret = iwl_set_Xtal_calib(priv);
Johannes Berg93b64102011-11-17 08:51:53 -0800344 if (ret)
345 return ret;
346 }
Wey-Yi Guy74159522011-04-05 09:42:01 -0700347
Johannes Berge1991882012-03-06 13:30:36 -0800348 return iwl_send_calib_results(priv);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700349}
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700350
351
352/**
353 * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
354 * using sample data 100 bytes apart. If these sample points are good,
355 * it's a pretty good bet that everything between them is good, too.
356 */
Johannes Berge1991882012-03-06 13:30:36 -0800357static int iwl_verify_inst_sparse(struct iwl_priv *priv,
358 struct fw_desc *fw_desc)
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700359{
Johannes Berg35b1d922011-04-05 09:41:56 -0700360 __le32 *image = (__le32 *)fw_desc->v_addr;
361 u32 len = fw_desc->len;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700362 u32 val;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700363 u32 i;
364
Johannes Berge1991882012-03-06 13:30:36 -0800365 IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700366
367 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
368 /* read data comes through single port, auto-incr addr */
369 /* NOTE: Use the debugless read so we don't flood kernel log
370 * if IWL_DL_IO is set */
Johannes Berge1991882012-03-06 13:30:36 -0800371 iwl_write_direct32(trans(priv), HBUS_TARG_MEM_RADDR,
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700372 i + IWLAGN_RTC_INST_LOWER_BOUND);
Johannes Berge1991882012-03-06 13:30:36 -0800373 val = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
Johannes Bergfb662162011-04-05 09:41:55 -0700374 if (val != le32_to_cpu(*image))
375 return -EIO;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700376 }
377
Johannes Bergfb662162011-04-05 09:41:55 -0700378 return 0;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700379}
380
Johannes Berge1991882012-03-06 13:30:36 -0800381static void iwl_print_mismatch_inst(struct iwl_priv *priv,
Johannes Berg35b1d922011-04-05 09:41:56 -0700382 struct fw_desc *fw_desc)
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700383{
Johannes Berg35b1d922011-04-05 09:41:56 -0700384 __le32 *image = (__le32 *)fw_desc->v_addr;
385 u32 len = fw_desc->len;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700386 u32 val;
Johannes Bergfb662162011-04-05 09:41:55 -0700387 u32 offs;
388 int errors = 0;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700389
Johannes Berge1991882012-03-06 13:30:36 -0800390 IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700391
Johannes Berge1991882012-03-06 13:30:36 -0800392 iwl_write_direct32(trans(priv), HBUS_TARG_MEM_RADDR,
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700393 IWLAGN_RTC_INST_LOWER_BOUND);
394
Johannes Bergfb662162011-04-05 09:41:55 -0700395 for (offs = 0;
396 offs < len && errors < 20;
397 offs += sizeof(u32), image++) {
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700398 /* read data comes through single port, auto-incr addr */
Johannes Berge1991882012-03-06 13:30:36 -0800399 val = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700400 if (val != le32_to_cpu(*image)) {
Johannes Berge1991882012-03-06 13:30:36 -0800401 IWL_ERR(priv, "uCode INST section at "
Johannes Bergfb662162011-04-05 09:41:55 -0700402 "offset 0x%x, is 0x%x, s/b 0x%x\n",
403 offs, val, le32_to_cpu(*image));
404 errors++;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700405 }
406 }
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700407}
408
409/**
410 * iwl_verify_ucode - determine which instruction image is in SRAM,
411 * and verify its contents
412 */
Johannes Berge1991882012-03-06 13:30:36 -0800413static int iwl_verify_ucode(struct iwl_priv *priv,
Don Fryde7f5f92011-11-10 06:55:10 -0800414 enum iwl_ucode_type ucode_type)
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700415{
Johannes Berge1991882012-03-06 13:30:36 -0800416 struct fw_img *img = iwl_get_ucode_image(priv, ucode_type);
Don Frybaa00052011-11-10 06:55:09 -0800417
418 if (!img) {
Johannes Berge1991882012-03-06 13:30:36 -0800419 IWL_ERR(priv, "Invalid ucode requested (%d)\n", ucode_type);
Don Frybaa00052011-11-10 06:55:09 -0800420 return -EINVAL;
421 }
422
Johannes Berge1991882012-03-06 13:30:36 -0800423 if (!iwl_verify_inst_sparse(priv, &img->code)) {
424 IWL_DEBUG_FW(priv, "uCode is good in inst SRAM\n");
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700425 return 0;
426 }
427
Johannes Berge1991882012-03-06 13:30:36 -0800428 IWL_ERR(priv, "UCODE IMAGE IN INSTRUCTION SRAM NOT VALID!!\n");
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700429
Johannes Berge1991882012-03-06 13:30:36 -0800430 iwl_print_mismatch_inst(priv, &img->code);
Johannes Bergfb662162011-04-05 09:41:55 -0700431 return -EIO;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700432}
Johannes Bergca7966c2011-04-22 10:15:23 -0700433
Don Fry69a679b2011-12-07 08:50:46 -0800434struct iwl_alive_data {
Johannes Bergca7966c2011-04-22 10:15:23 -0700435 bool valid;
436 u8 subtype;
437};
438
Johannes Berge1991882012-03-06 13:30:36 -0800439static void iwl_alive_fn(struct iwl_priv *priv,
Johannes Bergca7966c2011-04-22 10:15:23 -0700440 struct iwl_rx_packet *pkt,
441 void *data)
442{
Don Fry69a679b2011-12-07 08:50:46 -0800443 struct iwl_alive_data *alive_data = data;
Johannes Bergca7966c2011-04-22 10:15:23 -0700444 struct iwl_alive_resp *palive;
445
446 palive = &pkt->u.alive_frame;
447
Johannes Berge1991882012-03-06 13:30:36 -0800448 IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
Johannes Bergca7966c2011-04-22 10:15:23 -0700449 "0x%01X 0x%01X\n",
450 palive->is_valid, palive->ver_type,
451 palive->ver_subtype);
452
Johannes Berge1991882012-03-06 13:30:36 -0800453 priv->shrd->device_pointers.error_event_table =
Johannes Bergca7966c2011-04-22 10:15:23 -0700454 le32_to_cpu(palive->error_event_table_ptr);
Johannes Berge1991882012-03-06 13:30:36 -0800455 priv->shrd->device_pointers.log_event_table =
Johannes Bergca7966c2011-04-22 10:15:23 -0700456 le32_to_cpu(palive->log_event_table_ptr);
457
458 alive_data->subtype = palive->ver_subtype;
459 alive_data->valid = palive->is_valid == UCODE_VALID_OK;
460}
461
Don Frydd5fe102011-11-28 16:13:19 -0800462/* notification wait support */
463void iwl_init_notification_wait(struct iwl_shared *shrd,
Johannes Berge1991882012-03-06 13:30:36 -0800464 struct iwl_notification_wait *wait_entry,
465 u8 cmd,
466 void (*fn)(struct iwl_priv *priv,
467 struct iwl_rx_packet *pkt,
468 void *data),
469 void *fn_data)
Don Frydd5fe102011-11-28 16:13:19 -0800470{
471 wait_entry->fn = fn;
472 wait_entry->fn_data = fn_data;
473 wait_entry->cmd = cmd;
474 wait_entry->triggered = false;
475 wait_entry->aborted = false;
476
477 spin_lock_bh(&shrd->notif_wait_lock);
478 list_add(&wait_entry->list, &shrd->notif_waits);
479 spin_unlock_bh(&shrd->notif_wait_lock);
480}
481
482int iwl_wait_notification(struct iwl_shared *shrd,
483 struct iwl_notification_wait *wait_entry,
484 unsigned long timeout)
485{
486 int ret;
487
488 ret = wait_event_timeout(shrd->notif_waitq,
489 wait_entry->triggered || wait_entry->aborted,
490 timeout);
491
492 spin_lock_bh(&shrd->notif_wait_lock);
493 list_del(&wait_entry->list);
494 spin_unlock_bh(&shrd->notif_wait_lock);
495
496 if (wait_entry->aborted)
497 return -EIO;
498
499 /* return value is always >= 0 */
500 if (ret <= 0)
501 return -ETIMEDOUT;
502 return 0;
503}
504
505void iwl_remove_notification(struct iwl_shared *shrd,
506 struct iwl_notification_wait *wait_entry)
507{
508 spin_lock_bh(&shrd->notif_wait_lock);
509 list_del(&wait_entry->list);
510 spin_unlock_bh(&shrd->notif_wait_lock);
511}
512
513void iwl_abort_notification_waits(struct iwl_shared *shrd)
514{
515 unsigned long flags;
516 struct iwl_notification_wait *wait_entry;
517
518 spin_lock_irqsave(&shrd->notif_wait_lock, flags);
519 list_for_each_entry(wait_entry, &shrd->notif_waits, list)
520 wait_entry->aborted = true;
521 spin_unlock_irqrestore(&shrd->notif_wait_lock, flags);
522
523 wake_up_all(&shrd->notif_waitq);
524}
525
Johannes Bergca7966c2011-04-22 10:15:23 -0700526#define UCODE_ALIVE_TIMEOUT HZ
527#define UCODE_CALIB_TIMEOUT (2*HZ)
528
Johannes Berge1991882012-03-06 13:30:36 -0800529int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
Don Fryde7f5f92011-11-10 06:55:10 -0800530 enum iwl_ucode_type ucode_type)
Johannes Bergca7966c2011-04-22 10:15:23 -0700531{
532 struct iwl_notification_wait alive_wait;
Don Fry69a679b2011-12-07 08:50:46 -0800533 struct iwl_alive_data alive_data;
Emmanuel Grumbachcf614292012-01-08 16:33:58 +0200534 struct fw_img *fw;
Johannes Bergca7966c2011-04-22 10:15:23 -0700535 int ret;
Don Fryde7f5f92011-11-10 06:55:10 -0800536 enum iwl_ucode_type old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700537
Johannes Berge1991882012-03-06 13:30:36 -0800538 iwl_init_notification_wait(priv->shrd, &alive_wait, REPLY_ALIVE,
Don Fry66128b12011-11-28 14:35:14 -0800539 iwl_alive_fn, &alive_data);
Johannes Bergca7966c2011-04-22 10:15:23 -0700540
Johannes Berge1991882012-03-06 13:30:36 -0800541 old_type = priv->shrd->ucode_type;
542 priv->shrd->ucode_type = ucode_type;
543 fw = iwl_get_ucode_image(priv, ucode_type);
Johannes Bergca7966c2011-04-22 10:15:23 -0700544
Emmanuel Grumbachcf614292012-01-08 16:33:58 +0200545 if (!fw)
546 return -EINVAL;
547
Johannes Berge1991882012-03-06 13:30:36 -0800548 ret = iwl_trans_start_fw(trans(priv), fw);
Johannes Bergca7966c2011-04-22 10:15:23 -0700549 if (ret) {
Johannes Berge1991882012-03-06 13:30:36 -0800550 priv->shrd->ucode_type = old_type;
551 iwl_remove_notification(priv->shrd, &alive_wait);
Johannes Bergca7966c2011-04-22 10:15:23 -0700552 return ret;
553 }
554
Johannes Bergca7966c2011-04-22 10:15:23 -0700555 /*
556 * Some things may run in the background now, but we
557 * just wait for the ALIVE notification here.
558 */
Johannes Berge1991882012-03-06 13:30:36 -0800559 ret = iwl_wait_notification(priv->shrd, &alive_wait,
Don Frydd5fe102011-11-28 16:13:19 -0800560 UCODE_ALIVE_TIMEOUT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700561 if (ret) {
Johannes Berge1991882012-03-06 13:30:36 -0800562 priv->shrd->ucode_type = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700563 return ret;
564 }
565
566 if (!alive_data.valid) {
Johannes Berge1991882012-03-06 13:30:36 -0800567 IWL_ERR(priv, "Loaded ucode is not valid!\n");
568 priv->shrd->ucode_type = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700569 return -EIO;
570 }
571
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700572 /*
573 * This step takes a long time (60-80ms!!) and
574 * WoWLAN image should be loaded quickly, so
575 * skip it for WoWLAN.
576 */
577 if (ucode_type != IWL_UCODE_WOWLAN) {
Johannes Berge1991882012-03-06 13:30:36 -0800578 ret = iwl_verify_ucode(priv, ucode_type);
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700579 if (ret) {
Johannes Berge1991882012-03-06 13:30:36 -0800580 priv->shrd->ucode_type = old_type;
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700581 return ret;
582 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700583
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700584 /* delay a bit to give rfkill time to run */
585 msleep(5);
586 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700587
Johannes Berge1991882012-03-06 13:30:36 -0800588 ret = iwl_alive_notify(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700589 if (ret) {
Johannes Berge1991882012-03-06 13:30:36 -0800590 IWL_WARN(priv,
Johannes Bergca7966c2011-04-22 10:15:23 -0700591 "Could not complete ALIVE transition: %d\n", ret);
Johannes Berge1991882012-03-06 13:30:36 -0800592 priv->shrd->ucode_type = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700593 return ret;
594 }
595
596 return 0;
597}
598
Johannes Berge1991882012-03-06 13:30:36 -0800599int iwl_run_init_ucode(struct iwl_priv *priv)
Johannes Bergca7966c2011-04-22 10:15:23 -0700600{
601 struct iwl_notification_wait calib_wait;
602 int ret;
603
Johannes Berge1991882012-03-06 13:30:36 -0800604 lockdep_assert_held(&priv->shrd->mutex);
Johannes Bergca7966c2011-04-22 10:15:23 -0700605
606 /* No init ucode required? Curious, but maybe ok */
Johannes Berge1991882012-03-06 13:30:36 -0800607 if (!nic(priv)->fw.ucode_init.code.len)
Johannes Bergca7966c2011-04-22 10:15:23 -0700608 return 0;
609
Johannes Berge1991882012-03-06 13:30:36 -0800610 if (priv->shrd->ucode_type != IWL_UCODE_NONE)
Johannes Bergca7966c2011-04-22 10:15:23 -0700611 return 0;
612
Johannes Berge1991882012-03-06 13:30:36 -0800613 iwl_init_notification_wait(priv->shrd, &calib_wait,
Johannes Bergca7966c2011-04-22 10:15:23 -0700614 CALIBRATION_COMPLETE_NOTIFICATION,
615 NULL, NULL);
616
617 /* Will also start the device */
Johannes Berge1991882012-03-06 13:30:36 -0800618 ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700619 if (ret)
620 goto error;
621
Johannes Berge1991882012-03-06 13:30:36 -0800622 ret = iwl_init_alive_start(priv);
Johannes Bergca7966c2011-04-22 10:15:23 -0700623 if (ret)
624 goto error;
625
626 /*
627 * Some things may run in the background now, but we
628 * just wait for the calibration complete notification.
629 */
Johannes Berge1991882012-03-06 13:30:36 -0800630 ret = iwl_wait_notification(priv->shrd, &calib_wait,
Don Frydd5fe102011-11-28 16:13:19 -0800631 UCODE_CALIB_TIMEOUT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700632
633 goto out;
634
635 error:
Johannes Berge1991882012-03-06 13:30:36 -0800636 iwl_remove_notification(priv->shrd, &calib_wait);
Johannes Bergca7966c2011-04-22 10:15:23 -0700637 out:
638 /* Whatever happened, stop the device */
Johannes Berge1991882012-03-06 13:30:36 -0800639 iwl_trans_stop_device(trans(priv));
Johannes Bergca7966c2011-04-22 10:15:23 -0700640 return ret;
641}