blob: 87ce587d7d573f442eb80ae81e430868cd310d12 [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>
31#include <linux/module.h>
32#include <linux/init.h>
Wey-Yi Guy81b81762010-03-16 10:23:30 -070033#include <linux/sched.h>
Don Fryde7f5f92011-11-10 06:55:10 -080034#include <linux/dma-mapping.h>
Wey-Yi Guy792bc3c2010-03-16 10:23:29 -070035
Don Fry69a679b2011-12-07 08:50:46 -080036#include "iwl-wifi.h"
Wey-Yi Guy792bc3c2010-03-16 10:23:29 -070037#include "iwl-dev.h"
38#include "iwl-core.h"
Wey-Yi Guy81b81762010-03-16 10:23:30 -070039#include "iwl-io.h"
Wey-Yi Guy19e6cda2010-03-16 17:41:23 -070040#include "iwl-agn-hw.h"
Wey-Yi Guy741a6262010-03-16 12:37:24 -070041#include "iwl-agn.h"
Johannes Berg0de76732010-09-22 18:02:11 +020042#include "iwl-agn-calib.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070043#include "iwl-trans.h"
Emmanuel Grumbachdda61a42011-08-25 23:11:11 -070044#include "iwl-fh.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
Emmanuel Grumbach8467ab42012-01-03 16:28:12 +020083static void iwl_free_fw_desc(struct iwl_trans *trans, struct fw_desc *desc)
Don Fryde7f5f92011-11-10 06:55:10 -080084{
85 if (desc->v_addr)
Emmanuel Grumbach8467ab42012-01-03 16:28:12 +020086 dma_free_coherent(trans->dev, desc->len,
Don Fryde7f5f92011-11-10 06:55:10 -080087 desc->v_addr, desc->p_addr);
88 desc->v_addr = NULL;
89 desc->len = 0;
90}
91
Emmanuel Grumbach8467ab42012-01-03 16:28:12 +020092static void iwl_free_fw_img(struct iwl_trans *trans, struct fw_img *img)
Don Fryde7f5f92011-11-10 06:55:10 -080093{
Emmanuel Grumbach8467ab42012-01-03 16:28:12 +020094 iwl_free_fw_desc(trans, &img->code);
95 iwl_free_fw_desc(trans, &img->data);
Don Fryde7f5f92011-11-10 06:55:10 -080096}
97
98void iwl_dealloc_ucode(struct iwl_trans *trans)
99{
Emmanuel Grumbach8467ab42012-01-03 16:28:12 +0200100 iwl_free_fw_img(trans, &trans->ucode_rt);
101 iwl_free_fw_img(trans, &trans->ucode_init);
102 iwl_free_fw_img(trans, &trans->ucode_wowlan);
Don Fryde7f5f92011-11-10 06:55:10 -0800103}
104
Emmanuel Grumbach8467ab42012-01-03 16:28:12 +0200105int iwl_alloc_fw_desc(struct iwl_trans *trans, struct fw_desc *desc,
Don Fryde7f5f92011-11-10 06:55:10 -0800106 const void *data, size_t len)
107{
108 if (!len) {
109 desc->v_addr = NULL;
110 return -EINVAL;
111 }
112
Emmanuel Grumbach8467ab42012-01-03 16:28:12 +0200113 desc->v_addr = dma_alloc_coherent(trans->dev, len,
Don Fryde7f5f92011-11-10 06:55:10 -0800114 &desc->p_addr, GFP_KERNEL);
115 if (!desc->v_addr)
116 return -ENOMEM;
117
118 desc->len = len;
119 memcpy(desc->v_addr, data, len);
120 return 0;
121}
122
Don Fryde7f5f92011-11-10 06:55:10 -0800123static inline struct fw_img *iwl_get_ucode_image(struct iwl_trans *trans,
124 enum iwl_ucode_type ucode_type)
Don Fry8929c242011-11-10 06:55:08 -0800125{
126 switch (ucode_type) {
127 case IWL_UCODE_INIT:
Don Fryde7f5f92011-11-10 06:55:10 -0800128 return &trans->ucode_init;
Don Fry8929c242011-11-10 06:55:08 -0800129 case IWL_UCODE_WOWLAN:
Don Fryde7f5f92011-11-10 06:55:10 -0800130 return &trans->ucode_wowlan;
Don Fry8929c242011-11-10 06:55:08 -0800131 case IWL_UCODE_REGULAR:
Don Fryde7f5f92011-11-10 06:55:10 -0800132 return &trans->ucode_rt;
Don Fry8929c242011-11-10 06:55:08 -0800133 case IWL_UCODE_NONE:
134 break;
135 }
136 return NULL;
137}
138
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700139/*
140 * Calibration
141 */
Don Fry69a679b2011-12-07 08:50:46 -0800142static int iwl_set_Xtal_calib(struct iwl_trans *trans)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700143{
144 struct iwl_calib_xtal_freq_cmd cmd;
145 __le16 *xtal_calib =
Don Fry69a679b2011-12-07 08:50:46 -0800146 (__le16 *)iwl_eeprom_query_addr(trans->shrd, EEPROM_XTAL);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700147
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -0700148 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700149 cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
150 cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
Don Fry69a679b2011-12-07 08:50:46 -0800151 return iwl_calib_set(trans, (void *)&cmd, sizeof(cmd));
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700152}
153
Don Fry69a679b2011-12-07 08:50:46 -0800154static int iwl_set_temperature_offset_calib(struct iwl_trans *trans)
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700155{
156 struct iwl_calib_temperature_offset_cmd cmd;
157 __le16 *offset_calib =
Don Fry69a679b2011-12-07 08:50:46 -0800158 (__le16 *)iwl_eeprom_query_addr(trans->shrd,
Don Fryab36eab2011-11-30 15:37:32 -0800159 EEPROM_RAW_TEMPERATURE);
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -0700160
161 memset(&cmd, 0, sizeof(cmd));
162 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
Johannes Berg456fc372011-09-12 21:08:25 +0200163 memcpy(&cmd.radio_sensor_offset, offset_calib, sizeof(*offset_calib));
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700164 if (!(cmd.radio_sensor_offset))
165 cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -0700166
Don Fry69a679b2011-12-07 08:50:46 -0800167 IWL_DEBUG_CALIB(trans, "Radio sensor offset: %d\n",
Wey-Yi Guy2e277992011-07-08 14:29:48 -0700168 le16_to_cpu(cmd.radio_sensor_offset));
Don Fry69a679b2011-12-07 08:50:46 -0800169 return iwl_calib_set(trans, (void *)&cmd, sizeof(cmd));
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700170}
171
Don Fry69a679b2011-12-07 08:50:46 -0800172static int iwl_set_temperature_offset_calib_v2(struct iwl_trans *trans)
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700173{
174 struct iwl_calib_temperature_offset_v2_cmd cmd;
Don Fry69a679b2011-12-07 08:50:46 -0800175 __le16 *offset_calib_high = (__le16 *)iwl_eeprom_query_addr(trans->shrd,
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700176 EEPROM_KELVIN_TEMPERATURE);
177 __le16 *offset_calib_low =
Don Fry69a679b2011-12-07 08:50:46 -0800178 (__le16 *)iwl_eeprom_query_addr(trans->shrd,
Don Fryab36eab2011-11-30 15:37:32 -0800179 EEPROM_RAW_TEMPERATURE);
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -0700180 struct iwl_eeprom_calib_hdr *hdr;
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700181
182 memset(&cmd, 0, sizeof(cmd));
183 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
Don Fry69a679b2011-12-07 08:50:46 -0800184 hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(trans->shrd,
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -0700185 EEPROM_CALIB_ALL);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700186 memcpy(&cmd.radio_sensor_offset_high, offset_calib_high,
Wey-Yi Guy00085002011-09-15 11:46:53 -0700187 sizeof(*offset_calib_high));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700188 memcpy(&cmd.radio_sensor_offset_low, offset_calib_low,
Wey-Yi Guy00085002011-09-15 11:46:53 -0700189 sizeof(*offset_calib_low));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700190 if (!(cmd.radio_sensor_offset_low)) {
Don Fry69a679b2011-12-07 08:50:46 -0800191 IWL_DEBUG_CALIB(trans, "no info in EEPROM, use default\n");
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700192 cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
193 cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
194 }
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -0700195 memcpy(&cmd.burntVoltageRef, &hdr->voltage,
196 sizeof(hdr->voltage));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700197
Don Fry69a679b2011-12-07 08:50:46 -0800198 IWL_DEBUG_CALIB(trans, "Radio sensor offset high: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700199 le16_to_cpu(cmd.radio_sensor_offset_high));
Don Fry69a679b2011-12-07 08:50:46 -0800200 IWL_DEBUG_CALIB(trans, "Radio sensor offset low: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700201 le16_to_cpu(cmd.radio_sensor_offset_low));
Don Fry69a679b2011-12-07 08:50:46 -0800202 IWL_DEBUG_CALIB(trans, "Voltage Ref: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700203 le16_to_cpu(cmd.burntVoltageRef));
204
Don Fry69a679b2011-12-07 08:50:46 -0800205 return iwl_calib_set(trans, (void *)&cmd, sizeof(cmd));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700206}
207
Don Fry66128b12011-11-28 14:35:14 -0800208static int iwl_send_calib_cfg(struct iwl_trans *trans)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700209{
210 struct iwl_calib_cfg_cmd calib_cfg_cmd;
211 struct iwl_host_cmd cmd = {
212 .id = CALIBRATION_CFG_CMD,
Johannes Berg3fa50732011-05-04 07:50:38 -0700213 .len = { sizeof(struct iwl_calib_cfg_cmd), },
214 .data = { &calib_cfg_cmd, },
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700215 };
216
217 memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
218 calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
219 calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
220 calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
Wey-Yi Guydf2a4dc2011-07-08 14:29:45 -0700221 calib_cfg_cmd.ucd_calib_cfg.flags =
222 IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700223
Don Fry66128b12011-11-28 14:35:14 -0800224 return iwl_trans_send_cmd(trans, &cmd);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700225}
226
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700227int iwlagn_rx_calib_result(struct iwl_priv *priv,
228 struct iwl_rx_mem_buffer *rxb,
229 struct iwl_device_cmd *cmd)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700230{
231 struct iwl_rx_packet *pkt = rxb_addr(rxb);
232 struct iwl_calib_hdr *hdr = (struct iwl_calib_hdr *)pkt->u.raw;
233 int len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700234
235 /* reduce the size of the length field itself */
236 len -= 4;
237
Don Fry45c30db2011-11-30 16:58:39 -0800238 if (iwl_calib_set(trans(priv), hdr, len))
Johannes Bergf02c2fd2011-11-17 08:51:57 -0800239 IWL_ERR(priv, "Failed to record calibration data %d\n",
240 hdr->op_code);
241
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700242 return 0;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700243}
244
Don Fry69a679b2011-12-07 08:50:46 -0800245int iwl_init_alive_start(struct iwl_trans *trans)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700246{
Johannes Bergca7966c2011-04-22 10:15:23 -0700247 int ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700248
Don Fry69a679b2011-12-07 08:50:46 -0800249 if (cfg(trans)->bt_params &&
250 cfg(trans)->bt_params->advanced_bt_coexist) {
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700251 /*
252 * Tell uCode we are ready to perform calibration
253 * need to perform this before any calibration
254 * no need to close the envlope since we are going
255 * to load the runtime uCode later.
256 */
Don Fry69a679b2011-12-07 08:50:46 -0800257 ret = iwl_send_bt_env(trans, IWL_BT_COEX_ENV_OPEN,
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700258 BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
Johannes Bergca7966c2011-04-22 10:15:23 -0700259 if (ret)
260 return ret;
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700261
262 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700263
Don Fry69a679b2011-12-07 08:50:46 -0800264 ret = iwl_send_calib_cfg(trans);
Johannes Bergca7966c2011-04-22 10:15:23 -0700265 if (ret)
266 return ret;
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700267
268 /**
269 * temperature offset calibration is only needed for runtime ucode,
270 * so prepare the value now.
271 */
Don Fry69a679b2011-12-07 08:50:46 -0800272 if (cfg(trans)->need_temp_offset_calib) {
273 if (cfg(trans)->temp_offset_v2)
274 return iwl_set_temperature_offset_calib_v2(trans);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700275 else
Don Fry69a679b2011-12-07 08:50:46 -0800276 return iwl_set_temperature_offset_calib(trans);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700277 }
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700278
Johannes Bergca7966c2011-04-22 10:15:23 -0700279 return 0;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700280}
281
Don Fry69a679b2011-12-07 08:50:46 -0800282static int iwl_send_wimax_coex(struct iwl_trans *trans)
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700283{
284 struct iwl_wimax_coex_cmd coex_cmd;
285
Don Fry69a679b2011-12-07 08:50:46 -0800286 if (cfg(trans)->base_params->support_wimax_coexist) {
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700287 /* UnMask wake up src at associated sleep */
288 coex_cmd.flags = COEX_FLAGS_ASSOC_WA_UNMASK_MSK;
289
290 /* UnMask wake up src at unassociated sleep */
291 coex_cmd.flags |= COEX_FLAGS_UNASSOC_WA_UNMASK_MSK;
292 memcpy(coex_cmd.sta_prio, cu_priorities,
293 sizeof(struct iwl_wimax_coex_event_entry) *
294 COEX_NUM_OF_EVENTS);
295
296 /* enabling the coexistence feature */
297 coex_cmd.flags |= COEX_FLAGS_COEX_ENABLE_MSK;
298
299 /* enabling the priorities tables */
300 coex_cmd.flags |= COEX_FLAGS_STA_TABLE_VALID_MSK;
301 } else {
302 /* coexistence is disabled */
303 memset(&coex_cmd, 0, sizeof(coex_cmd));
304 }
Don Fry69a679b2011-12-07 08:50:46 -0800305 return iwl_trans_send_cmd_pdu(trans,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700306 COEX_PRIORITY_TABLE_CMD, CMD_SYNC,
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700307 sizeof(coex_cmd), &coex_cmd);
308}
309
Don Fry66128b12011-11-28 14:35:14 -0800310static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700311 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
312 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
313 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
314 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
315 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
316 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
317 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
318 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
319 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
320 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
321 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
322 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
323 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
324 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
325 ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
326 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
327 ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
328 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
329 0, 0, 0, 0, 0, 0, 0
330};
331
Don Fry66128b12011-11-28 14:35:14 -0800332void iwl_send_prio_tbl(struct iwl_trans *trans)
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700333{
334 struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
335
Don Fry66128b12011-11-28 14:35:14 -0800336 memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
337 sizeof(iwl_bt_prio_tbl));
338 if (iwl_trans_send_cmd_pdu(trans,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700339 REPLY_BT_COEX_PRIO_TABLE, CMD_SYNC,
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700340 sizeof(prio_tbl_cmd), &prio_tbl_cmd))
Don Fry66128b12011-11-28 14:35:14 -0800341 IWL_ERR(trans, "failed to send BT prio tbl command\n");
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700342}
343
Don Fry66128b12011-11-28 14:35:14 -0800344int iwl_send_bt_env(struct iwl_trans *trans, u8 action, u8 type)
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700345{
346 struct iwl_bt_coex_prot_env_cmd env_cmd;
Johannes Bergca7966c2011-04-22 10:15:23 -0700347 int ret;
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700348
349 env_cmd.action = action;
350 env_cmd.type = type;
Don Fry66128b12011-11-28 14:35:14 -0800351 ret = iwl_trans_send_cmd_pdu(trans,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700352 REPLY_BT_COEX_PROT_ENV, CMD_SYNC,
Johannes Bergca7966c2011-04-22 10:15:23 -0700353 sizeof(env_cmd), &env_cmd);
354 if (ret)
Don Fry66128b12011-11-28 14:35:14 -0800355 IWL_ERR(trans, "failed to send BT env command\n");
Johannes Bergca7966c2011-04-22 10:15:23 -0700356 return ret;
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700357}
358
359
Don Fry69a679b2011-12-07 08:50:46 -0800360static int iwl_alive_notify(struct iwl_trans *trans)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700361{
Don Fry69a679b2011-12-07 08:50:46 -0800362 struct iwl_priv *priv = priv(trans);
Emmanuel Grumbach7a10e3e2011-09-06 09:31:21 -0700363 struct iwl_rxon_context *ctx;
Wey-Yi Guy74159522011-04-05 09:42:01 -0700364 int ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700365
Emmanuel Grumbachdfa2bdb2011-08-25 23:11:23 -0700366 if (!priv->tx_cmd_pool)
367 priv->tx_cmd_pool =
Don Fry66128b12011-11-28 14:35:14 -0800368 kmem_cache_create("iwl_dev_cmd",
Emmanuel Grumbachdfa2bdb2011-08-25 23:11:23 -0700369 sizeof(struct iwl_device_cmd),
370 sizeof(void *), 0, NULL);
371
372 if (!priv->tx_cmd_pool)
373 return -ENOMEM;
374
Emmanuel Grumbached6a3802012-01-02 16:10:08 +0200375 iwl_trans_fw_alive(trans);
Emmanuel Grumbach7a10e3e2011-09-06 09:31:21 -0700376 for_each_context(priv, ctx)
377 ctx->last_tx_rejected = false;
Grumbach, Emmanuele7cad692010-11-18 03:47:38 -0800378
Don Fry69a679b2011-12-07 08:50:46 -0800379 ret = iwl_send_wimax_coex(trans);
Wey-Yi Guy74159522011-04-05 09:42:01 -0700380 if (ret)
381 return ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700382
Don Fry38622412011-12-16 07:07:36 -0800383 if (!cfg(priv)->no_xtal_calib) {
Don Fry69a679b2011-12-07 08:50:46 -0800384 ret = iwl_set_Xtal_calib(trans);
Johannes Berg93b64102011-11-17 08:51:53 -0800385 if (ret)
386 return ret;
387 }
Wey-Yi Guy74159522011-04-05 09:42:01 -0700388
Don Fry69a679b2011-12-07 08:50:46 -0800389 return iwl_send_calib_results(trans);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700390}
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700391
392
393/**
394 * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
395 * using sample data 100 bytes apart. If these sample points are good,
396 * it's a pretty good bet that everything between them is good, too.
397 */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200398static int iwl_verify_inst_sparse(struct iwl_trans *trans,
Johannes Berg35b1d922011-04-05 09:41:56 -0700399 struct fw_desc *fw_desc)
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700400{
Johannes Berg35b1d922011-04-05 09:41:56 -0700401 __le32 *image = (__le32 *)fw_desc->v_addr;
402 u32 len = fw_desc->len;
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700403 u32 val;
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700404 u32 i;
405
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200406 IWL_DEBUG_FW(trans, "ucode inst image size is %u\n", len);
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700407
408 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
409 /* read data comes through single port, auto-incr addr */
410 /* NOTE: Use the debugless read so we don't flood kernel log
411 * if IWL_DL_IO is set */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200412 iwl_write_direct32(trans, HBUS_TARG_MEM_RADDR,
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700413 i + IWLAGN_RTC_INST_LOWER_BOUND);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200414 val = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
Johannes Bergfb662162011-04-05 09:41:55 -0700415 if (val != le32_to_cpu(*image))
416 return -EIO;
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700417 }
418
Johannes Bergfb662162011-04-05 09:41:55 -0700419 return 0;
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700420}
421
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200422static void iwl_print_mismatch_inst(struct iwl_trans *trans,
Johannes Berg35b1d922011-04-05 09:41:56 -0700423 struct fw_desc *fw_desc)
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700424{
Johannes Berg35b1d922011-04-05 09:41:56 -0700425 __le32 *image = (__le32 *)fw_desc->v_addr;
426 u32 len = fw_desc->len;
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700427 u32 val;
Johannes Bergfb662162011-04-05 09:41:55 -0700428 u32 offs;
429 int errors = 0;
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700430
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200431 IWL_DEBUG_FW(trans, "ucode inst image size is %u\n", len);
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700432
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200433 iwl_write_direct32(trans, HBUS_TARG_MEM_RADDR,
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700434 IWLAGN_RTC_INST_LOWER_BOUND);
435
Johannes Bergfb662162011-04-05 09:41:55 -0700436 for (offs = 0;
437 offs < len && errors < 20;
438 offs += sizeof(u32), image++) {
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700439 /* read data comes through single port, auto-incr addr */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200440 val = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700441 if (val != le32_to_cpu(*image)) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200442 IWL_ERR(trans, "uCode INST section at "
Johannes Bergfb662162011-04-05 09:41:55 -0700443 "offset 0x%x, is 0x%x, s/b 0x%x\n",
444 offs, val, le32_to_cpu(*image));
445 errors++;
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700446 }
447 }
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700448}
449
450/**
451 * iwl_verify_ucode - determine which instruction image is in SRAM,
452 * and verify its contents
453 */
Don Fryde7f5f92011-11-10 06:55:10 -0800454static int iwl_verify_ucode(struct iwl_trans *trans,
455 enum iwl_ucode_type ucode_type)
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700456{
Don Fryde7f5f92011-11-10 06:55:10 -0800457 struct fw_img *img = iwl_get_ucode_image(trans, ucode_type);
Don Frybaa00052011-11-10 06:55:09 -0800458
459 if (!img) {
Don Fryde7f5f92011-11-10 06:55:10 -0800460 IWL_ERR(trans, "Invalid ucode requested (%d)\n", ucode_type);
Don Frybaa00052011-11-10 06:55:09 -0800461 return -EINVAL;
462 }
463
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200464 if (!iwl_verify_inst_sparse(trans, &img->code)) {
Don Fryde7f5f92011-11-10 06:55:10 -0800465 IWL_DEBUG_FW(trans, "uCode is good in inst SRAM\n");
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700466 return 0;
467 }
468
Don Fryde7f5f92011-11-10 06:55:10 -0800469 IWL_ERR(trans, "UCODE IMAGE IN INSTRUCTION SRAM NOT VALID!!\n");
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700470
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200471 iwl_print_mismatch_inst(trans, &img->code);
Johannes Bergfb662162011-04-05 09:41:55 -0700472 return -EIO;
Wey-Yi Guydb41dd22010-05-10 14:15:25 -0700473}
Johannes Bergca7966c2011-04-22 10:15:23 -0700474
Don Fry69a679b2011-12-07 08:50:46 -0800475struct iwl_alive_data {
Johannes Bergca7966c2011-04-22 10:15:23 -0700476 bool valid;
477 u8 subtype;
478};
479
Don Fryae6130f2011-11-30 16:12:59 -0800480static void iwl_alive_fn(struct iwl_trans *trans,
Johannes Bergca7966c2011-04-22 10:15:23 -0700481 struct iwl_rx_packet *pkt,
482 void *data)
483{
Don Fry69a679b2011-12-07 08:50:46 -0800484 struct iwl_alive_data *alive_data = data;
Johannes Bergca7966c2011-04-22 10:15:23 -0700485 struct iwl_alive_resp *palive;
486
487 palive = &pkt->u.alive_frame;
488
Don Fryae6130f2011-11-30 16:12:59 -0800489 IWL_DEBUG_FW(trans, "Alive ucode status 0x%08X revision "
Johannes Bergca7966c2011-04-22 10:15:23 -0700490 "0x%01X 0x%01X\n",
491 palive->is_valid, palive->ver_type,
492 palive->ver_subtype);
493
Don Fryae6130f2011-11-30 16:12:59 -0800494 trans->shrd->device_pointers.error_event_table =
Johannes Bergca7966c2011-04-22 10:15:23 -0700495 le32_to_cpu(palive->error_event_table_ptr);
Don Fryae6130f2011-11-30 16:12:59 -0800496 trans->shrd->device_pointers.log_event_table =
Johannes Bergca7966c2011-04-22 10:15:23 -0700497 le32_to_cpu(palive->log_event_table_ptr);
498
499 alive_data->subtype = palive->ver_subtype;
500 alive_data->valid = palive->is_valid == UCODE_VALID_OK;
501}
502
Don Frydd5fe102011-11-28 16:13:19 -0800503/* notification wait support */
504void iwl_init_notification_wait(struct iwl_shared *shrd,
505 struct iwl_notification_wait *wait_entry,
506 u8 cmd,
Don Fryae6130f2011-11-30 16:12:59 -0800507 void (*fn)(struct iwl_trans *trans,
Don Frydd5fe102011-11-28 16:13:19 -0800508 struct iwl_rx_packet *pkt,
509 void *data),
510 void *fn_data)
511{
512 wait_entry->fn = fn;
513 wait_entry->fn_data = fn_data;
514 wait_entry->cmd = cmd;
515 wait_entry->triggered = false;
516 wait_entry->aborted = false;
517
518 spin_lock_bh(&shrd->notif_wait_lock);
519 list_add(&wait_entry->list, &shrd->notif_waits);
520 spin_unlock_bh(&shrd->notif_wait_lock);
521}
522
523int iwl_wait_notification(struct iwl_shared *shrd,
524 struct iwl_notification_wait *wait_entry,
525 unsigned long timeout)
526{
527 int ret;
528
529 ret = wait_event_timeout(shrd->notif_waitq,
530 wait_entry->triggered || wait_entry->aborted,
531 timeout);
532
533 spin_lock_bh(&shrd->notif_wait_lock);
534 list_del(&wait_entry->list);
535 spin_unlock_bh(&shrd->notif_wait_lock);
536
537 if (wait_entry->aborted)
538 return -EIO;
539
540 /* return value is always >= 0 */
541 if (ret <= 0)
542 return -ETIMEDOUT;
543 return 0;
544}
545
546void iwl_remove_notification(struct iwl_shared *shrd,
547 struct iwl_notification_wait *wait_entry)
548{
549 spin_lock_bh(&shrd->notif_wait_lock);
550 list_del(&wait_entry->list);
551 spin_unlock_bh(&shrd->notif_wait_lock);
552}
553
554void iwl_abort_notification_waits(struct iwl_shared *shrd)
555{
556 unsigned long flags;
557 struct iwl_notification_wait *wait_entry;
558
559 spin_lock_irqsave(&shrd->notif_wait_lock, flags);
560 list_for_each_entry(wait_entry, &shrd->notif_waits, list)
561 wait_entry->aborted = true;
562 spin_unlock_irqrestore(&shrd->notif_wait_lock, flags);
563
564 wake_up_all(&shrd->notif_waitq);
565}
566
Johannes Bergca7966c2011-04-22 10:15:23 -0700567#define UCODE_ALIVE_TIMEOUT HZ
568#define UCODE_CALIB_TIMEOUT (2*HZ)
569
Don Fry69a679b2011-12-07 08:50:46 -0800570int iwl_load_ucode_wait_alive(struct iwl_trans *trans,
Don Fryde7f5f92011-11-10 06:55:10 -0800571 enum iwl_ucode_type ucode_type)
Johannes Bergca7966c2011-04-22 10:15:23 -0700572{
573 struct iwl_notification_wait alive_wait;
Don Fry69a679b2011-12-07 08:50:46 -0800574 struct iwl_alive_data alive_data;
Emmanuel Grumbachcf614292012-01-08 16:33:58 +0200575 struct fw_img *fw;
Johannes Bergca7966c2011-04-22 10:15:23 -0700576 int ret;
Don Fryde7f5f92011-11-10 06:55:10 -0800577 enum iwl_ucode_type old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700578
Don Frydd5fe102011-11-28 16:13:19 -0800579 iwl_init_notification_wait(trans->shrd, &alive_wait, REPLY_ALIVE,
Don Fry66128b12011-11-28 14:35:14 -0800580 iwl_alive_fn, &alive_data);
Johannes Bergca7966c2011-04-22 10:15:23 -0700581
Don Fry3d6acef2011-11-28 17:05:01 -0800582 old_type = trans->shrd->ucode_type;
583 trans->shrd->ucode_type = ucode_type;
Emmanuel Grumbachcf614292012-01-08 16:33:58 +0200584 fw = iwl_get_ucode_image(trans, ucode_type);
Johannes Bergca7966c2011-04-22 10:15:23 -0700585
Emmanuel Grumbachcf614292012-01-08 16:33:58 +0200586 if (!fw)
587 return -EINVAL;
588
589 ret = iwl_trans_start_fw(trans, fw);
Johannes Bergca7966c2011-04-22 10:15:23 -0700590 if (ret) {
Don Fry3d6acef2011-11-28 17:05:01 -0800591 trans->shrd->ucode_type = old_type;
Don Frydd5fe102011-11-28 16:13:19 -0800592 iwl_remove_notification(trans->shrd, &alive_wait);
Johannes Bergca7966c2011-04-22 10:15:23 -0700593 return ret;
594 }
595
Johannes Bergca7966c2011-04-22 10:15:23 -0700596 /*
597 * Some things may run in the background now, but we
598 * just wait for the ALIVE notification here.
599 */
Don Frydd5fe102011-11-28 16:13:19 -0800600 ret = iwl_wait_notification(trans->shrd, &alive_wait,
601 UCODE_ALIVE_TIMEOUT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700602 if (ret) {
Don Fry3d6acef2011-11-28 17:05:01 -0800603 trans->shrd->ucode_type = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700604 return ret;
605 }
606
607 if (!alive_data.valid) {
Don Fry69a679b2011-12-07 08:50:46 -0800608 IWL_ERR(trans, "Loaded ucode is not valid!\n");
Don Fry3d6acef2011-11-28 17:05:01 -0800609 trans->shrd->ucode_type = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700610 return -EIO;
611 }
612
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700613 /*
614 * This step takes a long time (60-80ms!!) and
615 * WoWLAN image should be loaded quickly, so
616 * skip it for WoWLAN.
617 */
618 if (ucode_type != IWL_UCODE_WOWLAN) {
Don Fry3d6acef2011-11-28 17:05:01 -0800619 ret = iwl_verify_ucode(trans, ucode_type);
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700620 if (ret) {
Don Fry3d6acef2011-11-28 17:05:01 -0800621 trans->shrd->ucode_type = old_type;
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700622 return ret;
623 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700624
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700625 /* delay a bit to give rfkill time to run */
626 msleep(5);
627 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700628
Don Fry69a679b2011-12-07 08:50:46 -0800629 ret = iwl_alive_notify(trans);
Johannes Bergca7966c2011-04-22 10:15:23 -0700630 if (ret) {
Don Fry69a679b2011-12-07 08:50:46 -0800631 IWL_WARN(trans,
Johannes Bergca7966c2011-04-22 10:15:23 -0700632 "Could not complete ALIVE transition: %d\n", ret);
Don Fry3d6acef2011-11-28 17:05:01 -0800633 trans->shrd->ucode_type = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700634 return ret;
635 }
636
637 return 0;
638}
639
Don Fry69a679b2011-12-07 08:50:46 -0800640int iwl_run_init_ucode(struct iwl_trans *trans)
Johannes Bergca7966c2011-04-22 10:15:23 -0700641{
642 struct iwl_notification_wait calib_wait;
643 int ret;
644
Don Fry69a679b2011-12-07 08:50:46 -0800645 lockdep_assert_held(&trans->shrd->mutex);
Johannes Bergca7966c2011-04-22 10:15:23 -0700646
647 /* No init ucode required? Curious, but maybe ok */
Don Fry69a679b2011-12-07 08:50:46 -0800648 if (!trans->ucode_init.code.len)
Johannes Bergca7966c2011-04-22 10:15:23 -0700649 return 0;
650
Don Fry69a679b2011-12-07 08:50:46 -0800651 if (trans->shrd->ucode_type != IWL_UCODE_NONE)
Johannes Bergca7966c2011-04-22 10:15:23 -0700652 return 0;
653
Don Fry69a679b2011-12-07 08:50:46 -0800654 iwl_init_notification_wait(trans->shrd, &calib_wait,
Johannes Bergca7966c2011-04-22 10:15:23 -0700655 CALIBRATION_COMPLETE_NOTIFICATION,
656 NULL, NULL);
657
658 /* Will also start the device */
Don Fry69a679b2011-12-07 08:50:46 -0800659 ret = iwl_load_ucode_wait_alive(trans, IWL_UCODE_INIT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700660 if (ret)
661 goto error;
662
Don Fry69a679b2011-12-07 08:50:46 -0800663 ret = iwl_init_alive_start(trans);
Johannes Bergca7966c2011-04-22 10:15:23 -0700664 if (ret)
665 goto error;
666
667 /*
668 * Some things may run in the background now, but we
669 * just wait for the calibration complete notification.
670 */
Don Fry69a679b2011-12-07 08:50:46 -0800671 ret = iwl_wait_notification(trans->shrd, &calib_wait,
Don Frydd5fe102011-11-28 16:13:19 -0800672 UCODE_CALIB_TIMEOUT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700673
674 goto out;
675
676 error:
Don Fry69a679b2011-12-07 08:50:46 -0800677 iwl_remove_notification(trans->shrd, &calib_wait);
Johannes Bergca7966c2011-04-22 10:15:23 -0700678 out:
679 /* Whatever happened, stop the device */
Don Fry69a679b2011-12-07 08:50:46 -0800680 iwl_trans_stop_device(trans);
Johannes Bergca7966c2011-04-22 10:15:23 -0700681 return ret;
682}