blob: 61c6c0568c1101d1e69dbfd6717cb80d090b0db9 [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>
Don Fryedf38332012-01-25 16:18:52 -080035#include <linux/firmware.h>
Wey-Yi Guy792bc3c2010-03-16 10:23:29 -070036
Don Fryedf38332012-01-25 16:18:52 -080037#include "iwl-ucode.h"
Don Fry69a679b2011-12-07 08:50:46 -080038#include "iwl-wifi.h"
Wey-Yi Guy792bc3c2010-03-16 10:23:29 -070039#include "iwl-dev.h"
40#include "iwl-core.h"
Wey-Yi Guy81b81762010-03-16 10:23:30 -070041#include "iwl-io.h"
Wey-Yi Guy19e6cda2010-03-16 17:41:23 -070042#include "iwl-agn-hw.h"
Wey-Yi Guy741a6262010-03-16 12:37:24 -070043#include "iwl-agn.h"
Johannes Berg0de76732010-09-22 18:02:11 +020044#include "iwl-agn-calib.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070045#include "iwl-trans.h"
Emmanuel Grumbachdda61a42011-08-25 23:11:11 -070046#include "iwl-fh.h"
Wey-Yi Guy741a6262010-03-16 12:37:24 -070047
Wey-Yi Guyf4012412010-04-27 14:10:00 -070048static struct iwl_wimax_coex_event_entry cu_priorities[COEX_NUM_OF_EVENTS] = {
49 {COEX_CU_UNASSOC_IDLE_RP, COEX_CU_UNASSOC_IDLE_WP,
50 0, COEX_UNASSOC_IDLE_FLAGS},
51 {COEX_CU_UNASSOC_MANUAL_SCAN_RP, COEX_CU_UNASSOC_MANUAL_SCAN_WP,
52 0, COEX_UNASSOC_MANUAL_SCAN_FLAGS},
53 {COEX_CU_UNASSOC_AUTO_SCAN_RP, COEX_CU_UNASSOC_AUTO_SCAN_WP,
54 0, COEX_UNASSOC_AUTO_SCAN_FLAGS},
55 {COEX_CU_CALIBRATION_RP, COEX_CU_CALIBRATION_WP,
56 0, COEX_CALIBRATION_FLAGS},
57 {COEX_CU_PERIODIC_CALIBRATION_RP, COEX_CU_PERIODIC_CALIBRATION_WP,
58 0, COEX_PERIODIC_CALIBRATION_FLAGS},
59 {COEX_CU_CONNECTION_ESTAB_RP, COEX_CU_CONNECTION_ESTAB_WP,
60 0, COEX_CONNECTION_ESTAB_FLAGS},
61 {COEX_CU_ASSOCIATED_IDLE_RP, COEX_CU_ASSOCIATED_IDLE_WP,
62 0, COEX_ASSOCIATED_IDLE_FLAGS},
63 {COEX_CU_ASSOC_MANUAL_SCAN_RP, COEX_CU_ASSOC_MANUAL_SCAN_WP,
64 0, COEX_ASSOC_MANUAL_SCAN_FLAGS},
65 {COEX_CU_ASSOC_AUTO_SCAN_RP, COEX_CU_ASSOC_AUTO_SCAN_WP,
66 0, COEX_ASSOC_AUTO_SCAN_FLAGS},
67 {COEX_CU_ASSOC_ACTIVE_LEVEL_RP, COEX_CU_ASSOC_ACTIVE_LEVEL_WP,
68 0, COEX_ASSOC_ACTIVE_LEVEL_FLAGS},
69 {COEX_CU_RF_ON_RP, COEX_CU_RF_ON_WP, 0, COEX_CU_RF_ON_FLAGS},
70 {COEX_CU_RF_OFF_RP, COEX_CU_RF_OFF_WP, 0, COEX_RF_OFF_FLAGS},
71 {COEX_CU_STAND_ALONE_DEBUG_RP, COEX_CU_STAND_ALONE_DEBUG_WP,
72 0, COEX_STAND_ALONE_DEBUG_FLAGS},
73 {COEX_CU_IPAN_ASSOC_LEVEL_RP, COEX_CU_IPAN_ASSOC_LEVEL_WP,
74 0, COEX_IPAN_ASSOC_LEVEL_FLAGS},
75 {COEX_CU_RSRVD1_RP, COEX_CU_RSRVD1_WP, 0, COEX_RSRVD1_FLAGS},
76 {COEX_CU_RSRVD2_RP, COEX_CU_RSRVD2_WP, 0, COEX_RSRVD2_FLAGS}
77};
78
Don Fryde7f5f92011-11-10 06:55:10 -080079/******************************************************************************
80 *
81 * uCode download functions
82 *
83 ******************************************************************************/
84
Don Fry655ccce2012-02-06 16:37:46 -080085static void iwl_free_fw_desc(struct iwl_nic *nic, struct fw_desc *desc)
Don Fryde7f5f92011-11-10 06:55:10 -080086{
87 if (desc->v_addr)
Don Fry655ccce2012-02-06 16:37:46 -080088 dma_free_coherent(trans(nic)->dev, desc->len,
Don Fryde7f5f92011-11-10 06:55:10 -080089 desc->v_addr, desc->p_addr);
90 desc->v_addr = NULL;
91 desc->len = 0;
92}
93
Don Fry655ccce2012-02-06 16:37:46 -080094static void iwl_free_fw_img(struct iwl_nic *nic, struct fw_img *img)
Don Fryde7f5f92011-11-10 06:55:10 -080095{
Don Fry655ccce2012-02-06 16:37:46 -080096 iwl_free_fw_desc(nic, &img->code);
97 iwl_free_fw_desc(nic, &img->data);
Don Fryde7f5f92011-11-10 06:55:10 -080098}
99
Don Fry655ccce2012-02-06 16:37:46 -0800100void iwl_dealloc_ucode(struct iwl_nic *nic)
Don Fryde7f5f92011-11-10 06:55:10 -0800101{
Don Fry655ccce2012-02-06 16:37:46 -0800102 iwl_free_fw_img(nic, &nic->fw.ucode_rt);
103 iwl_free_fw_img(nic, &nic->fw.ucode_init);
104 iwl_free_fw_img(nic, &nic->fw.ucode_wowlan);
Don Fryde7f5f92011-11-10 06:55:10 -0800105}
106
Don Fry655ccce2012-02-06 16:37:46 -0800107static int iwl_alloc_fw_desc(struct iwl_nic *nic, struct fw_desc *desc,
Don Fryde7f5f92011-11-10 06:55:10 -0800108 const void *data, size_t len)
109{
110 if (!len) {
111 desc->v_addr = NULL;
112 return -EINVAL;
113 }
114
Don Fry655ccce2012-02-06 16:37:46 -0800115 desc->v_addr = dma_alloc_coherent(trans(nic)->dev, len,
Don Fryde7f5f92011-11-10 06:55:10 -0800116 &desc->p_addr, GFP_KERNEL);
117 if (!desc->v_addr)
118 return -ENOMEM;
119
120 desc->len = len;
121 memcpy(desc->v_addr, data, len);
122 return 0;
123}
124
Don Fry65161742012-02-07 15:00:12 -0800125static inline struct fw_img *iwl_get_ucode_image(struct iwl_nic *nic,
Don Fryde7f5f92011-11-10 06:55:10 -0800126 enum iwl_ucode_type ucode_type)
Don Fry8929c242011-11-10 06:55:08 -0800127{
128 switch (ucode_type) {
129 case IWL_UCODE_INIT:
Don Fry65161742012-02-07 15:00:12 -0800130 return &nic->fw.ucode_init;
Don Fry8929c242011-11-10 06:55:08 -0800131 case IWL_UCODE_WOWLAN:
Don Fry65161742012-02-07 15:00:12 -0800132 return &nic->fw.ucode_wowlan;
Don Fry8929c242011-11-10 06:55:08 -0800133 case IWL_UCODE_REGULAR:
Don Fry65161742012-02-07 15:00:12 -0800134 return &nic->fw.ucode_rt;
Don Fry8929c242011-11-10 06:55:08 -0800135 case IWL_UCODE_NONE:
136 break;
137 }
138 return NULL;
139}
140
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700141/*
142 * Calibration
143 */
Don Fry69a679b2011-12-07 08:50:46 -0800144static int iwl_set_Xtal_calib(struct iwl_trans *trans)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700145{
146 struct iwl_calib_xtal_freq_cmd cmd;
147 __le16 *xtal_calib =
Don Fry69a679b2011-12-07 08:50:46 -0800148 (__le16 *)iwl_eeprom_query_addr(trans->shrd, EEPROM_XTAL);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700149
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -0700150 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700151 cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
152 cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
Don Fry69a679b2011-12-07 08:50:46 -0800153 return iwl_calib_set(trans, (void *)&cmd, sizeof(cmd));
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700154}
155
Don Fry69a679b2011-12-07 08:50:46 -0800156static int iwl_set_temperature_offset_calib(struct iwl_trans *trans)
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700157{
158 struct iwl_calib_temperature_offset_cmd cmd;
159 __le16 *offset_calib =
Don Fry69a679b2011-12-07 08:50:46 -0800160 (__le16 *)iwl_eeprom_query_addr(trans->shrd,
Don Fryab36eab2011-11-30 15:37:32 -0800161 EEPROM_RAW_TEMPERATURE);
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -0700162
163 memset(&cmd, 0, sizeof(cmd));
164 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
Johannes Berg456fc372011-09-12 21:08:25 +0200165 memcpy(&cmd.radio_sensor_offset, offset_calib, sizeof(*offset_calib));
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700166 if (!(cmd.radio_sensor_offset))
167 cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
Wey-Yi Guy1f8bf032011-06-06 14:26:43 -0700168
Don Fry69a679b2011-12-07 08:50:46 -0800169 IWL_DEBUG_CALIB(trans, "Radio sensor offset: %d\n",
Wey-Yi Guy2e277992011-07-08 14:29:48 -0700170 le16_to_cpu(cmd.radio_sensor_offset));
Don Fry69a679b2011-12-07 08:50:46 -0800171 return iwl_calib_set(trans, (void *)&cmd, sizeof(cmd));
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700172}
173
Don Fry69a679b2011-12-07 08:50:46 -0800174static int iwl_set_temperature_offset_calib_v2(struct iwl_trans *trans)
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700175{
176 struct iwl_calib_temperature_offset_v2_cmd cmd;
Don Fry69a679b2011-12-07 08:50:46 -0800177 __le16 *offset_calib_high = (__le16 *)iwl_eeprom_query_addr(trans->shrd,
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700178 EEPROM_KELVIN_TEMPERATURE);
179 __le16 *offset_calib_low =
Don Fry69a679b2011-12-07 08:50:46 -0800180 (__le16 *)iwl_eeprom_query_addr(trans->shrd,
Don Fryab36eab2011-11-30 15:37:32 -0800181 EEPROM_RAW_TEMPERATURE);
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -0700182 struct iwl_eeprom_calib_hdr *hdr;
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700183
184 memset(&cmd, 0, sizeof(cmd));
185 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
Don Fry69a679b2011-12-07 08:50:46 -0800186 hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(trans->shrd,
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -0700187 EEPROM_CALIB_ALL);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700188 memcpy(&cmd.radio_sensor_offset_high, offset_calib_high,
Wey-Yi Guy00085002011-09-15 11:46:53 -0700189 sizeof(*offset_calib_high));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700190 memcpy(&cmd.radio_sensor_offset_low, offset_calib_low,
Wey-Yi Guy00085002011-09-15 11:46:53 -0700191 sizeof(*offset_calib_low));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700192 if (!(cmd.radio_sensor_offset_low)) {
Don Fry69a679b2011-12-07 08:50:46 -0800193 IWL_DEBUG_CALIB(trans, "no info in EEPROM, use default\n");
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700194 cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
195 cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
196 }
Wey-Yi Guy7d8f2d52011-09-15 11:46:51 -0700197 memcpy(&cmd.burntVoltageRef, &hdr->voltage,
198 sizeof(hdr->voltage));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700199
Don Fry69a679b2011-12-07 08:50:46 -0800200 IWL_DEBUG_CALIB(trans, "Radio sensor offset high: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700201 le16_to_cpu(cmd.radio_sensor_offset_high));
Don Fry69a679b2011-12-07 08:50:46 -0800202 IWL_DEBUG_CALIB(trans, "Radio sensor offset low: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700203 le16_to_cpu(cmd.radio_sensor_offset_low));
Don Fry69a679b2011-12-07 08:50:46 -0800204 IWL_DEBUG_CALIB(trans, "Voltage Ref: %d\n",
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700205 le16_to_cpu(cmd.burntVoltageRef));
206
Don Fry69a679b2011-12-07 08:50:46 -0800207 return iwl_calib_set(trans, (void *)&cmd, sizeof(cmd));
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700208}
209
Don Fry66128b12011-11-28 14:35:14 -0800210static int iwl_send_calib_cfg(struct iwl_trans *trans)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700211{
212 struct iwl_calib_cfg_cmd calib_cfg_cmd;
213 struct iwl_host_cmd cmd = {
214 .id = CALIBRATION_CFG_CMD,
Johannes Berg3fa50732011-05-04 07:50:38 -0700215 .len = { sizeof(struct iwl_calib_cfg_cmd), },
216 .data = { &calib_cfg_cmd, },
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700217 };
218
219 memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
220 calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
221 calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
222 calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
Wey-Yi Guydf2a4dc2011-07-08 14:29:45 -0700223 calib_cfg_cmd.ucd_calib_cfg.flags =
224 IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700225
Don Fry66128b12011-11-28 14:35:14 -0800226 return iwl_trans_send_cmd(trans, &cmd);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700227}
228
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700229int iwlagn_rx_calib_result(struct iwl_priv *priv,
230 struct iwl_rx_mem_buffer *rxb,
231 struct iwl_device_cmd *cmd)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700232{
233 struct iwl_rx_packet *pkt = rxb_addr(rxb);
234 struct iwl_calib_hdr *hdr = (struct iwl_calib_hdr *)pkt->u.raw;
235 int len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700236
237 /* reduce the size of the length field itself */
238 len -= 4;
239
Don Fry45c30db2011-11-30 16:58:39 -0800240 if (iwl_calib_set(trans(priv), hdr, len))
Johannes Bergf02c2fd2011-11-17 08:51:57 -0800241 IWL_ERR(priv, "Failed to record calibration data %d\n",
242 hdr->op_code);
243
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700244 return 0;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700245}
246
Don Fry69a679b2011-12-07 08:50:46 -0800247int iwl_init_alive_start(struct iwl_trans *trans)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700248{
Johannes Bergca7966c2011-04-22 10:15:23 -0700249 int ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700250
Don Fry69a679b2011-12-07 08:50:46 -0800251 if (cfg(trans)->bt_params &&
252 cfg(trans)->bt_params->advanced_bt_coexist) {
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700253 /*
254 * Tell uCode we are ready to perform calibration
255 * need to perform this before any calibration
256 * no need to close the envlope since we are going
257 * to load the runtime uCode later.
258 */
Don Fry69a679b2011-12-07 08:50:46 -0800259 ret = iwl_send_bt_env(trans, IWL_BT_COEX_ENV_OPEN,
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700260 BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
Johannes Bergca7966c2011-04-22 10:15:23 -0700261 if (ret)
262 return ret;
Wey-Yi Guyf7322f82010-08-23 15:24:49 -0700263
264 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700265
Don Fry69a679b2011-12-07 08:50:46 -0800266 ret = iwl_send_calib_cfg(trans);
Johannes Bergca7966c2011-04-22 10:15:23 -0700267 if (ret)
268 return ret;
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700269
270 /**
271 * temperature offset calibration is only needed for runtime ucode,
272 * so prepare the value now.
273 */
Don Fry69a679b2011-12-07 08:50:46 -0800274 if (cfg(trans)->need_temp_offset_calib) {
275 if (cfg(trans)->temp_offset_v2)
276 return iwl_set_temperature_offset_calib_v2(trans);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700277 else
Don Fry69a679b2011-12-07 08:50:46 -0800278 return iwl_set_temperature_offset_calib(trans);
Wey-Yi Guyc6f30342011-09-15 11:46:50 -0700279 }
Shanyu Zhaobf53f932010-09-21 16:54:01 -0700280
Johannes Bergca7966c2011-04-22 10:15:23 -0700281 return 0;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700282}
283
Don Fry69a679b2011-12-07 08:50:46 -0800284static int iwl_send_wimax_coex(struct iwl_trans *trans)
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700285{
286 struct iwl_wimax_coex_cmd coex_cmd;
287
Don Fry69a679b2011-12-07 08:50:46 -0800288 if (cfg(trans)->base_params->support_wimax_coexist) {
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700289 /* UnMask wake up src at associated sleep */
290 coex_cmd.flags = COEX_FLAGS_ASSOC_WA_UNMASK_MSK;
291
292 /* UnMask wake up src at unassociated sleep */
293 coex_cmd.flags |= COEX_FLAGS_UNASSOC_WA_UNMASK_MSK;
294 memcpy(coex_cmd.sta_prio, cu_priorities,
295 sizeof(struct iwl_wimax_coex_event_entry) *
296 COEX_NUM_OF_EVENTS);
297
298 /* enabling the coexistence feature */
299 coex_cmd.flags |= COEX_FLAGS_COEX_ENABLE_MSK;
300
301 /* enabling the priorities tables */
302 coex_cmd.flags |= COEX_FLAGS_STA_TABLE_VALID_MSK;
303 } else {
304 /* coexistence is disabled */
305 memset(&coex_cmd, 0, sizeof(coex_cmd));
306 }
Don Fry69a679b2011-12-07 08:50:46 -0800307 return iwl_trans_send_cmd_pdu(trans,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700308 COEX_PRIORITY_TABLE_CMD, CMD_SYNC,
Wey-Yi Guyf4012412010-04-27 14:10:00 -0700309 sizeof(coex_cmd), &coex_cmd);
310}
311
Don Fry66128b12011-11-28 14:35:14 -0800312static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700313 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
314 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
315 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
316 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
317 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
318 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
319 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
320 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
321 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
322 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
323 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
324 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
325 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
326 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
327 ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
328 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
329 ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
330 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
331 0, 0, 0, 0, 0, 0, 0
332};
333
Don Fry66128b12011-11-28 14:35:14 -0800334void iwl_send_prio_tbl(struct iwl_trans *trans)
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700335{
336 struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
337
Don Fry66128b12011-11-28 14:35:14 -0800338 memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
339 sizeof(iwl_bt_prio_tbl));
340 if (iwl_trans_send_cmd_pdu(trans,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700341 REPLY_BT_COEX_PRIO_TABLE, CMD_SYNC,
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700342 sizeof(prio_tbl_cmd), &prio_tbl_cmd))
Don Fry66128b12011-11-28 14:35:14 -0800343 IWL_ERR(trans, "failed to send BT prio tbl command\n");
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700344}
345
Don Fry66128b12011-11-28 14:35:14 -0800346int iwl_send_bt_env(struct iwl_trans *trans, u8 action, u8 type)
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700347{
348 struct iwl_bt_coex_prot_env_cmd env_cmd;
Johannes Bergca7966c2011-04-22 10:15:23 -0700349 int ret;
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700350
351 env_cmd.action = action;
352 env_cmd.type = type;
Don Fry66128b12011-11-28 14:35:14 -0800353 ret = iwl_trans_send_cmd_pdu(trans,
Emmanuel Grumbache419d622011-07-08 08:46:14 -0700354 REPLY_BT_COEX_PROT_ENV, CMD_SYNC,
Johannes Bergca7966c2011-04-22 10:15:23 -0700355 sizeof(env_cmd), &env_cmd);
356 if (ret)
Don Fry66128b12011-11-28 14:35:14 -0800357 IWL_ERR(trans, "failed to send BT env command\n");
Johannes Bergca7966c2011-04-22 10:15:23 -0700358 return ret;
Wey-Yi Guyaeb4a2e2010-08-23 07:57:05 -0700359}
360
361
Don Fry69a679b2011-12-07 08:50:46 -0800362static int iwl_alive_notify(struct iwl_trans *trans)
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700363{
Don Fry69a679b2011-12-07 08:50:46 -0800364 struct iwl_priv *priv = priv(trans);
Emmanuel Grumbach7a10e3e42011-09-06 09:31:21 -0700365 struct iwl_rxon_context *ctx;
Wey-Yi Guy74159522011-04-05 09:42:01 -0700366 int ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700367
Emmanuel Grumbachdfa2bdb2011-08-25 23:11:23 -0700368 if (!priv->tx_cmd_pool)
369 priv->tx_cmd_pool =
Don Fry66128b12011-11-28 14:35:14 -0800370 kmem_cache_create("iwl_dev_cmd",
Emmanuel Grumbachdfa2bdb2011-08-25 23:11:23 -0700371 sizeof(struct iwl_device_cmd),
372 sizeof(void *), 0, NULL);
373
374 if (!priv->tx_cmd_pool)
375 return -ENOMEM;
376
Emmanuel Grumbached6a3802012-01-02 16:10:08 +0200377 iwl_trans_fw_alive(trans);
Emmanuel Grumbach7a10e3e42011-09-06 09:31:21 -0700378 for_each_context(priv, ctx)
379 ctx->last_tx_rejected = false;
Grumbach, Emmanuele7cad692010-11-18 03:47:38 -0800380
Don Fry69a679b2011-12-07 08:50:46 -0800381 ret = iwl_send_wimax_coex(trans);
Wey-Yi Guy74159522011-04-05 09:42:01 -0700382 if (ret)
383 return ret;
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700384
Don Fry38622412011-12-16 07:07:36 -0800385 if (!cfg(priv)->no_xtal_calib) {
Don Fry69a679b2011-12-07 08:50:46 -0800386 ret = iwl_set_Xtal_calib(trans);
Johannes Berg93b64102011-11-17 08:51:53 -0800387 if (ret)
388 return ret;
389 }
Wey-Yi Guy74159522011-04-05 09:42:01 -0700390
Don Fry69a679b2011-12-07 08:50:46 -0800391 return iwl_send_calib_results(trans);
Wey-Yi Guy741a6262010-03-16 12:37:24 -0700392}
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700393
394
395/**
396 * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
397 * using sample data 100 bytes apart. If these sample points are good,
398 * it's a pretty good bet that everything between them is good, too.
399 */
Don Fry655ccce2012-02-06 16:37:46 -0800400static int iwl_verify_inst_sparse(struct iwl_nic *nic,
Johannes Berg35b1d922011-04-05 09:41:56 -0700401 struct fw_desc *fw_desc)
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700402{
Don Fry655ccce2012-02-06 16:37:46 -0800403 struct iwl_trans *trans = trans(nic);
Johannes Berg35b1d922011-04-05 09:41:56 -0700404 __le32 *image = (__le32 *)fw_desc->v_addr;
405 u32 len = fw_desc->len;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700406 u32 val;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700407 u32 i;
408
Don Fry655ccce2012-02-06 16:37:46 -0800409 IWL_DEBUG_FW(nic, "ucode inst image size is %u\n", len);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700410
411 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
412 /* read data comes through single port, auto-incr addr */
413 /* NOTE: Use the debugless read so we don't flood kernel log
414 * if IWL_DL_IO is set */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200415 iwl_write_direct32(trans, HBUS_TARG_MEM_RADDR,
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700416 i + IWLAGN_RTC_INST_LOWER_BOUND);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200417 val = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
Johannes Bergfb662162011-04-05 09:41:55 -0700418 if (val != le32_to_cpu(*image))
419 return -EIO;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700420 }
421
Johannes Bergfb662162011-04-05 09:41:55 -0700422 return 0;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700423}
424
Don Fry655ccce2012-02-06 16:37:46 -0800425static void iwl_print_mismatch_inst(struct iwl_nic *nic,
Johannes Berg35b1d922011-04-05 09:41:56 -0700426 struct fw_desc *fw_desc)
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700427{
Don Fry655ccce2012-02-06 16:37:46 -0800428 struct iwl_trans *trans = trans(nic);
Johannes Berg35b1d922011-04-05 09:41:56 -0700429 __le32 *image = (__le32 *)fw_desc->v_addr;
430 u32 len = fw_desc->len;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700431 u32 val;
Johannes Bergfb662162011-04-05 09:41:55 -0700432 u32 offs;
433 int errors = 0;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700434
Don Fry655ccce2012-02-06 16:37:46 -0800435 IWL_DEBUG_FW(nic, "ucode inst image size is %u\n", len);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700436
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200437 iwl_write_direct32(trans, HBUS_TARG_MEM_RADDR,
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700438 IWLAGN_RTC_INST_LOWER_BOUND);
439
Johannes Bergfb662162011-04-05 09:41:55 -0700440 for (offs = 0;
441 offs < len && errors < 20;
442 offs += sizeof(u32), image++) {
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700443 /* read data comes through single port, auto-incr addr */
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200444 val = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700445 if (val != le32_to_cpu(*image)) {
Don Fry655ccce2012-02-06 16:37:46 -0800446 IWL_ERR(nic, "uCode INST section at "
Johannes Bergfb662162011-04-05 09:41:55 -0700447 "offset 0x%x, is 0x%x, s/b 0x%x\n",
448 offs, val, le32_to_cpu(*image));
449 errors++;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700450 }
451 }
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700452}
453
454/**
455 * iwl_verify_ucode - determine which instruction image is in SRAM,
456 * and verify its contents
457 */
Don Fry655ccce2012-02-06 16:37:46 -0800458static int iwl_verify_ucode(struct iwl_nic *nic,
Don Fryde7f5f92011-11-10 06:55:10 -0800459 enum iwl_ucode_type ucode_type)
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700460{
Don Fry655ccce2012-02-06 16:37:46 -0800461 struct fw_img *img = iwl_get_ucode_image(nic, ucode_type);
Don Frybaa00052011-11-10 06:55:09 -0800462
463 if (!img) {
Don Fry655ccce2012-02-06 16:37:46 -0800464 IWL_ERR(nic, "Invalid ucode requested (%d)\n", ucode_type);
Don Frybaa00052011-11-10 06:55:09 -0800465 return -EINVAL;
466 }
467
Don Fry655ccce2012-02-06 16:37:46 -0800468 if (!iwl_verify_inst_sparse(nic, &img->code)) {
469 IWL_DEBUG_FW(nic, "uCode is good in inst SRAM\n");
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700470 return 0;
471 }
472
Don Fry655ccce2012-02-06 16:37:46 -0800473 IWL_ERR(nic, "UCODE IMAGE IN INSTRUCTION SRAM NOT VALID!!\n");
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700474
Don Fry655ccce2012-02-06 16:37:46 -0800475 iwl_print_mismatch_inst(nic, &img->code);
Johannes Bergfb662162011-04-05 09:41:55 -0700476 return -EIO;
Wey-Yi Guydb41dd272010-05-10 14:15:25 -0700477}
Johannes Bergca7966c2011-04-22 10:15:23 -0700478
Don Fry69a679b2011-12-07 08:50:46 -0800479struct iwl_alive_data {
Johannes Bergca7966c2011-04-22 10:15:23 -0700480 bool valid;
481 u8 subtype;
482};
483
Don Fryae6130f2011-11-30 16:12:59 -0800484static void iwl_alive_fn(struct iwl_trans *trans,
Johannes Bergca7966c2011-04-22 10:15:23 -0700485 struct iwl_rx_packet *pkt,
486 void *data)
487{
Don Fry69a679b2011-12-07 08:50:46 -0800488 struct iwl_alive_data *alive_data = data;
Johannes Bergca7966c2011-04-22 10:15:23 -0700489 struct iwl_alive_resp *palive;
490
491 palive = &pkt->u.alive_frame;
492
Don Fryae6130f2011-11-30 16:12:59 -0800493 IWL_DEBUG_FW(trans, "Alive ucode status 0x%08X revision "
Johannes Bergca7966c2011-04-22 10:15:23 -0700494 "0x%01X 0x%01X\n",
495 palive->is_valid, palive->ver_type,
496 palive->ver_subtype);
497
Don Fryae6130f2011-11-30 16:12:59 -0800498 trans->shrd->device_pointers.error_event_table =
Johannes Bergca7966c2011-04-22 10:15:23 -0700499 le32_to_cpu(palive->error_event_table_ptr);
Don Fryae6130f2011-11-30 16:12:59 -0800500 trans->shrd->device_pointers.log_event_table =
Johannes Bergca7966c2011-04-22 10:15:23 -0700501 le32_to_cpu(palive->log_event_table_ptr);
502
503 alive_data->subtype = palive->ver_subtype;
504 alive_data->valid = palive->is_valid == UCODE_VALID_OK;
505}
506
Don Frydd5fe102011-11-28 16:13:19 -0800507/* notification wait support */
508void iwl_init_notification_wait(struct iwl_shared *shrd,
509 struct iwl_notification_wait *wait_entry,
510 u8 cmd,
Don Fryae6130f2011-11-30 16:12:59 -0800511 void (*fn)(struct iwl_trans *trans,
Don Frydd5fe102011-11-28 16:13:19 -0800512 struct iwl_rx_packet *pkt,
513 void *data),
514 void *fn_data)
515{
516 wait_entry->fn = fn;
517 wait_entry->fn_data = fn_data;
518 wait_entry->cmd = cmd;
519 wait_entry->triggered = false;
520 wait_entry->aborted = false;
521
522 spin_lock_bh(&shrd->notif_wait_lock);
523 list_add(&wait_entry->list, &shrd->notif_waits);
524 spin_unlock_bh(&shrd->notif_wait_lock);
525}
526
527int iwl_wait_notification(struct iwl_shared *shrd,
528 struct iwl_notification_wait *wait_entry,
529 unsigned long timeout)
530{
531 int ret;
532
533 ret = wait_event_timeout(shrd->notif_waitq,
534 wait_entry->triggered || wait_entry->aborted,
535 timeout);
536
537 spin_lock_bh(&shrd->notif_wait_lock);
538 list_del(&wait_entry->list);
539 spin_unlock_bh(&shrd->notif_wait_lock);
540
541 if (wait_entry->aborted)
542 return -EIO;
543
544 /* return value is always >= 0 */
545 if (ret <= 0)
546 return -ETIMEDOUT;
547 return 0;
548}
549
550void iwl_remove_notification(struct iwl_shared *shrd,
551 struct iwl_notification_wait *wait_entry)
552{
553 spin_lock_bh(&shrd->notif_wait_lock);
554 list_del(&wait_entry->list);
555 spin_unlock_bh(&shrd->notif_wait_lock);
556}
557
558void iwl_abort_notification_waits(struct iwl_shared *shrd)
559{
560 unsigned long flags;
561 struct iwl_notification_wait *wait_entry;
562
563 spin_lock_irqsave(&shrd->notif_wait_lock, flags);
564 list_for_each_entry(wait_entry, &shrd->notif_waits, list)
565 wait_entry->aborted = true;
566 spin_unlock_irqrestore(&shrd->notif_wait_lock, flags);
567
568 wake_up_all(&shrd->notif_waitq);
569}
570
Johannes Bergca7966c2011-04-22 10:15:23 -0700571#define UCODE_ALIVE_TIMEOUT HZ
572#define UCODE_CALIB_TIMEOUT (2*HZ)
573
Don Fry69a679b2011-12-07 08:50:46 -0800574int iwl_load_ucode_wait_alive(struct iwl_trans *trans,
Don Fryde7f5f92011-11-10 06:55:10 -0800575 enum iwl_ucode_type ucode_type)
Johannes Bergca7966c2011-04-22 10:15:23 -0700576{
577 struct iwl_notification_wait alive_wait;
Don Fry69a679b2011-12-07 08:50:46 -0800578 struct iwl_alive_data alive_data;
Emmanuel Grumbachcf614292012-01-08 16:33:58 +0200579 struct fw_img *fw;
Johannes Bergca7966c2011-04-22 10:15:23 -0700580 int ret;
Don Fryde7f5f92011-11-10 06:55:10 -0800581 enum iwl_ucode_type old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700582
Don Frydd5fe102011-11-28 16:13:19 -0800583 iwl_init_notification_wait(trans->shrd, &alive_wait, REPLY_ALIVE,
Don Fry66128b12011-11-28 14:35:14 -0800584 iwl_alive_fn, &alive_data);
Johannes Bergca7966c2011-04-22 10:15:23 -0700585
Don Fry3d6acef2011-11-28 17:05:01 -0800586 old_type = trans->shrd->ucode_type;
587 trans->shrd->ucode_type = ucode_type;
Don Fry65161742012-02-07 15:00:12 -0800588 fw = iwl_get_ucode_image(nic(trans), ucode_type);
Johannes Bergca7966c2011-04-22 10:15:23 -0700589
Emmanuel Grumbachcf614292012-01-08 16:33:58 +0200590 if (!fw)
591 return -EINVAL;
592
593 ret = iwl_trans_start_fw(trans, fw);
Johannes Bergca7966c2011-04-22 10:15:23 -0700594 if (ret) {
Don Fry3d6acef2011-11-28 17:05:01 -0800595 trans->shrd->ucode_type = old_type;
Don Frydd5fe102011-11-28 16:13:19 -0800596 iwl_remove_notification(trans->shrd, &alive_wait);
Johannes Bergca7966c2011-04-22 10:15:23 -0700597 return ret;
598 }
599
Johannes Bergca7966c2011-04-22 10:15:23 -0700600 /*
601 * Some things may run in the background now, but we
602 * just wait for the ALIVE notification here.
603 */
Don Frydd5fe102011-11-28 16:13:19 -0800604 ret = iwl_wait_notification(trans->shrd, &alive_wait,
605 UCODE_ALIVE_TIMEOUT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700606 if (ret) {
Don Fry3d6acef2011-11-28 17:05:01 -0800607 trans->shrd->ucode_type = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700608 return ret;
609 }
610
611 if (!alive_data.valid) {
Don Fry69a679b2011-12-07 08:50:46 -0800612 IWL_ERR(trans, "Loaded ucode is not valid!\n");
Don Fry3d6acef2011-11-28 17:05:01 -0800613 trans->shrd->ucode_type = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700614 return -EIO;
615 }
616
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700617 /*
618 * This step takes a long time (60-80ms!!) and
619 * WoWLAN image should be loaded quickly, so
620 * skip it for WoWLAN.
621 */
622 if (ucode_type != IWL_UCODE_WOWLAN) {
Don Fry655ccce2012-02-06 16:37:46 -0800623 ret = iwl_verify_ucode(nic(trans), ucode_type);
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700624 if (ret) {
Don Fry3d6acef2011-11-28 17:05:01 -0800625 trans->shrd->ucode_type = old_type;
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700626 return ret;
627 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700628
Johannes Bergc8ac61c2011-07-15 13:23:45 -0700629 /* delay a bit to give rfkill time to run */
630 msleep(5);
631 }
Johannes Bergca7966c2011-04-22 10:15:23 -0700632
Don Fry69a679b2011-12-07 08:50:46 -0800633 ret = iwl_alive_notify(trans);
Johannes Bergca7966c2011-04-22 10:15:23 -0700634 if (ret) {
Don Fry69a679b2011-12-07 08:50:46 -0800635 IWL_WARN(trans,
Johannes Bergca7966c2011-04-22 10:15:23 -0700636 "Could not complete ALIVE transition: %d\n", ret);
Don Fry3d6acef2011-11-28 17:05:01 -0800637 trans->shrd->ucode_type = old_type;
Johannes Bergca7966c2011-04-22 10:15:23 -0700638 return ret;
639 }
640
641 return 0;
642}
643
Don Fry69a679b2011-12-07 08:50:46 -0800644int iwl_run_init_ucode(struct iwl_trans *trans)
Johannes Bergca7966c2011-04-22 10:15:23 -0700645{
646 struct iwl_notification_wait calib_wait;
647 int ret;
648
Don Fry69a679b2011-12-07 08:50:46 -0800649 lockdep_assert_held(&trans->shrd->mutex);
Johannes Bergca7966c2011-04-22 10:15:23 -0700650
651 /* No init ucode required? Curious, but maybe ok */
Don Fry65161742012-02-07 15:00:12 -0800652 if (!nic(trans)->fw.ucode_init.code.len)
Johannes Bergca7966c2011-04-22 10:15:23 -0700653 return 0;
654
Don Fry69a679b2011-12-07 08:50:46 -0800655 if (trans->shrd->ucode_type != IWL_UCODE_NONE)
Johannes Bergca7966c2011-04-22 10:15:23 -0700656 return 0;
657
Don Fry69a679b2011-12-07 08:50:46 -0800658 iwl_init_notification_wait(trans->shrd, &calib_wait,
Johannes Bergca7966c2011-04-22 10:15:23 -0700659 CALIBRATION_COMPLETE_NOTIFICATION,
660 NULL, NULL);
661
662 /* Will also start the device */
Don Fry69a679b2011-12-07 08:50:46 -0800663 ret = iwl_load_ucode_wait_alive(trans, IWL_UCODE_INIT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700664 if (ret)
665 goto error;
666
Don Fry69a679b2011-12-07 08:50:46 -0800667 ret = iwl_init_alive_start(trans);
Johannes Bergca7966c2011-04-22 10:15:23 -0700668 if (ret)
669 goto error;
670
671 /*
672 * Some things may run in the background now, but we
673 * just wait for the calibration complete notification.
674 */
Don Fry69a679b2011-12-07 08:50:46 -0800675 ret = iwl_wait_notification(trans->shrd, &calib_wait,
Don Frydd5fe102011-11-28 16:13:19 -0800676 UCODE_CALIB_TIMEOUT);
Johannes Bergca7966c2011-04-22 10:15:23 -0700677
678 goto out;
679
680 error:
Don Fry69a679b2011-12-07 08:50:46 -0800681 iwl_remove_notification(trans->shrd, &calib_wait);
Johannes Bergca7966c2011-04-22 10:15:23 -0700682 out:
683 /* Whatever happened, stop the device */
Don Fry69a679b2011-12-07 08:50:46 -0800684 iwl_trans_stop_device(trans);
Johannes Bergca7966c2011-04-22 10:15:23 -0700685 return ret;
686}
Don Fryedf38332012-01-25 16:18:52 -0800687
688static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
689
690#define UCODE_EXPERIMENTAL_INDEX 100
691#define UCODE_EXPERIMENTAL_TAG "exp"
692
Don Fry06e03f82012-02-07 14:21:32 -0800693int __must_check iwl_request_firmware(struct iwl_nic *nic, bool first)
Don Fryedf38332012-01-25 16:18:52 -0800694{
Don Fry06e03f82012-02-07 14:21:32 -0800695 struct iwl_cfg *cfg = cfg(nic);
696 const char *name_pre = cfg->fw_name_pre;
Don Fryedf38332012-01-25 16:18:52 -0800697 char tag[8];
698
699 if (first) {
700#ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
Don Fryf6fd51d2012-02-06 15:54:31 -0800701 nic->fw_index = UCODE_EXPERIMENTAL_INDEX;
Don Fryedf38332012-01-25 16:18:52 -0800702 strcpy(tag, UCODE_EXPERIMENTAL_TAG);
Don Fryf6fd51d2012-02-06 15:54:31 -0800703 } else if (nic->fw_index == UCODE_EXPERIMENTAL_INDEX) {
Don Fryedf38332012-01-25 16:18:52 -0800704#endif
Don Fry06e03f82012-02-07 14:21:32 -0800705 nic->fw_index = cfg->ucode_api_max;
Don Fryf6fd51d2012-02-06 15:54:31 -0800706 sprintf(tag, "%d", nic->fw_index);
Don Fryedf38332012-01-25 16:18:52 -0800707 } else {
Don Fryf6fd51d2012-02-06 15:54:31 -0800708 nic->fw_index--;
709 sprintf(tag, "%d", nic->fw_index);
Don Fryedf38332012-01-25 16:18:52 -0800710 }
711
Don Fry06e03f82012-02-07 14:21:32 -0800712 if (nic->fw_index < cfg->ucode_api_min) {
713 IWL_ERR(nic, "no suitable firmware found!\n");
Don Fryedf38332012-01-25 16:18:52 -0800714 return -ENOENT;
715 }
716
Don Fry737805f2012-02-06 15:57:40 -0800717 sprintf(nic->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
Don Fryedf38332012-01-25 16:18:52 -0800718
Don Fry06e03f82012-02-07 14:21:32 -0800719 IWL_DEBUG_INFO(nic, "attempting to load firmware %s'%s'\n",
Don Fryf6fd51d2012-02-06 15:54:31 -0800720 (nic->fw_index == UCODE_EXPERIMENTAL_INDEX)
Don Fryedf38332012-01-25 16:18:52 -0800721 ? "EXPERIMENTAL " : "",
Don Fry737805f2012-02-06 15:57:40 -0800722 nic->firmware_name);
Don Fryedf38332012-01-25 16:18:52 -0800723
Don Fry737805f2012-02-06 15:57:40 -0800724 return request_firmware_nowait(THIS_MODULE, 1, nic->firmware_name,
Don Fry06e03f82012-02-07 14:21:32 -0800725 trans(nic)->dev,
726 GFP_KERNEL, nic, iwl_ucode_callback);
Don Fryedf38332012-01-25 16:18:52 -0800727}
728
729struct iwlagn_firmware_pieces {
730 const void *inst, *data, *init, *init_data, *wowlan_inst, *wowlan_data;
731 size_t inst_size, data_size, init_size, init_data_size,
732 wowlan_inst_size, wowlan_data_size;
733
734 u32 build;
735
736 u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
737 u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
738};
739
Don Fry06e03f82012-02-07 14:21:32 -0800740static int iwl_parse_v1_v2_firmware(struct iwl_nic *nic,
Don Fryedf38332012-01-25 16:18:52 -0800741 const struct firmware *ucode_raw,
742 struct iwlagn_firmware_pieces *pieces)
743{
744 struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
745 u32 api_ver, hdr_size;
746 const u8 *src;
747
Don Fryd3596672012-02-06 15:51:15 -0800748 nic->fw.ucode_ver = le32_to_cpu(ucode->ver);
749 api_ver = IWL_UCODE_API(nic->fw.ucode_ver);
Don Fryedf38332012-01-25 16:18:52 -0800750
751 switch (api_ver) {
752 default:
753 hdr_size = 28;
754 if (ucode_raw->size < hdr_size) {
Don Fry06e03f82012-02-07 14:21:32 -0800755 IWL_ERR(nic, "File size too small!\n");
Don Fryedf38332012-01-25 16:18:52 -0800756 return -EINVAL;
757 }
758 pieces->build = le32_to_cpu(ucode->u.v2.build);
759 pieces->inst_size = le32_to_cpu(ucode->u.v2.inst_size);
760 pieces->data_size = le32_to_cpu(ucode->u.v2.data_size);
761 pieces->init_size = le32_to_cpu(ucode->u.v2.init_size);
762 pieces->init_data_size = le32_to_cpu(ucode->u.v2.init_data_size);
763 src = ucode->u.v2.data;
764 break;
765 case 0:
766 case 1:
767 case 2:
768 hdr_size = 24;
769 if (ucode_raw->size < hdr_size) {
Don Fry06e03f82012-02-07 14:21:32 -0800770 IWL_ERR(nic, "File size too small!\n");
Don Fryedf38332012-01-25 16:18:52 -0800771 return -EINVAL;
772 }
773 pieces->build = 0;
774 pieces->inst_size = le32_to_cpu(ucode->u.v1.inst_size);
775 pieces->data_size = le32_to_cpu(ucode->u.v1.data_size);
776 pieces->init_size = le32_to_cpu(ucode->u.v1.init_size);
777 pieces->init_data_size = le32_to_cpu(ucode->u.v1.init_data_size);
778 src = ucode->u.v1.data;
779 break;
780 }
781
782 /* Verify size of file vs. image size info in file's header */
783 if (ucode_raw->size != hdr_size + pieces->inst_size +
784 pieces->data_size + pieces->init_size +
785 pieces->init_data_size) {
786
Don Fry06e03f82012-02-07 14:21:32 -0800787 IWL_ERR(nic,
Don Fryedf38332012-01-25 16:18:52 -0800788 "uCode file size %d does not match expected size\n",
789 (int)ucode_raw->size);
790 return -EINVAL;
791 }
792
793 pieces->inst = src;
794 src += pieces->inst_size;
795 pieces->data = src;
796 src += pieces->data_size;
797 pieces->init = src;
798 src += pieces->init_size;
799 pieces->init_data = src;
800 src += pieces->init_data_size;
801
802 return 0;
803}
804
Don Fry06e03f82012-02-07 14:21:32 -0800805static int iwl_parse_tlv_firmware(struct iwl_nic *nic,
Don Fryedf38332012-01-25 16:18:52 -0800806 const struct firmware *ucode_raw,
807 struct iwlagn_firmware_pieces *pieces,
Don Fry4a986772012-02-06 17:06:07 -0800808 struct iwl_ucode_capabilities *capa)
Don Fryedf38332012-01-25 16:18:52 -0800809{
810 struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
811 struct iwl_ucode_tlv *tlv;
812 size_t len = ucode_raw->size;
813 const u8 *data;
814 int wanted_alternative = iwlagn_mod_params.wanted_ucode_alternative;
815 int tmp;
816 u64 alternatives;
817 u32 tlv_len;
818 enum iwl_ucode_tlv_type tlv_type;
819 const u8 *tlv_data;
820
821 if (len < sizeof(*ucode)) {
Don Fry06e03f82012-02-07 14:21:32 -0800822 IWL_ERR(nic, "uCode has invalid length: %zd\n", len);
Don Fryedf38332012-01-25 16:18:52 -0800823 return -EINVAL;
824 }
825
826 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
Don Fry06e03f82012-02-07 14:21:32 -0800827 IWL_ERR(nic, "invalid uCode magic: 0X%x\n",
Don Fryedf38332012-01-25 16:18:52 -0800828 le32_to_cpu(ucode->magic));
829 return -EINVAL;
830 }
831
832 /*
833 * Check which alternatives are present, and "downgrade"
834 * when the chosen alternative is not present, warning
835 * the user when that happens. Some files may not have
836 * any alternatives, so don't warn in that case.
837 */
838 alternatives = le64_to_cpu(ucode->alternatives);
839 tmp = wanted_alternative;
840 if (wanted_alternative > 63)
841 wanted_alternative = 63;
842 while (wanted_alternative && !(alternatives & BIT(wanted_alternative)))
843 wanted_alternative--;
844 if (wanted_alternative && wanted_alternative != tmp)
Don Fry06e03f82012-02-07 14:21:32 -0800845 IWL_WARN(nic,
Don Fryedf38332012-01-25 16:18:52 -0800846 "uCode alternative %d not available, choosing %d\n",
847 tmp, wanted_alternative);
848
Don Fryd3596672012-02-06 15:51:15 -0800849 nic->fw.ucode_ver = le32_to_cpu(ucode->ver);
Don Fryedf38332012-01-25 16:18:52 -0800850 pieces->build = le32_to_cpu(ucode->build);
851 data = ucode->data;
852
853 len -= sizeof(*ucode);
854
855 while (len >= sizeof(*tlv)) {
856 u16 tlv_alt;
857
858 len -= sizeof(*tlv);
859 tlv = (void *)data;
860
861 tlv_len = le32_to_cpu(tlv->length);
862 tlv_type = le16_to_cpu(tlv->type);
863 tlv_alt = le16_to_cpu(tlv->alternative);
864 tlv_data = tlv->data;
865
866 if (len < tlv_len) {
Don Fry06e03f82012-02-07 14:21:32 -0800867 IWL_ERR(nic, "invalid TLV len: %zd/%u\n",
Don Fryedf38332012-01-25 16:18:52 -0800868 len, tlv_len);
869 return -EINVAL;
870 }
871 len -= ALIGN(tlv_len, 4);
872 data += sizeof(*tlv) + ALIGN(tlv_len, 4);
873
874 /*
875 * Alternative 0 is always valid.
876 *
877 * Skip alternative TLVs that are not selected.
878 */
879 if (tlv_alt != 0 && tlv_alt != wanted_alternative)
880 continue;
881
882 switch (tlv_type) {
883 case IWL_UCODE_TLV_INST:
884 pieces->inst = tlv_data;
885 pieces->inst_size = tlv_len;
886 break;
887 case IWL_UCODE_TLV_DATA:
888 pieces->data = tlv_data;
889 pieces->data_size = tlv_len;
890 break;
891 case IWL_UCODE_TLV_INIT:
892 pieces->init = tlv_data;
893 pieces->init_size = tlv_len;
894 break;
895 case IWL_UCODE_TLV_INIT_DATA:
896 pieces->init_data = tlv_data;
897 pieces->init_data_size = tlv_len;
898 break;
899 case IWL_UCODE_TLV_BOOT:
Don Fry06e03f82012-02-07 14:21:32 -0800900 IWL_ERR(nic, "Found unexpected BOOT ucode\n");
Don Fryedf38332012-01-25 16:18:52 -0800901 break;
902 case IWL_UCODE_TLV_PROBE_MAX_LEN:
903 if (tlv_len != sizeof(u32))
904 goto invalid_tlv_len;
905 capa->max_probe_length =
906 le32_to_cpup((__le32 *)tlv_data);
907 break;
908 case IWL_UCODE_TLV_PAN:
909 if (tlv_len)
910 goto invalid_tlv_len;
911 capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
912 break;
913 case IWL_UCODE_TLV_FLAGS:
914 /* must be at least one u32 */
915 if (tlv_len < sizeof(u32))
916 goto invalid_tlv_len;
917 /* and a proper number of u32s */
918 if (tlv_len % sizeof(u32))
919 goto invalid_tlv_len;
920 /*
921 * This driver only reads the first u32 as
922 * right now no more features are defined,
923 * if that changes then either the driver
924 * will not work with the new firmware, or
925 * it'll not take advantage of new features.
926 */
927 capa->flags = le32_to_cpup((__le32 *)tlv_data);
928 break;
929 case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
930 if (tlv_len != sizeof(u32))
931 goto invalid_tlv_len;
932 pieces->init_evtlog_ptr =
933 le32_to_cpup((__le32 *)tlv_data);
934 break;
935 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
936 if (tlv_len != sizeof(u32))
937 goto invalid_tlv_len;
938 pieces->init_evtlog_size =
939 le32_to_cpup((__le32 *)tlv_data);
940 break;
941 case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
942 if (tlv_len != sizeof(u32))
943 goto invalid_tlv_len;
944 pieces->init_errlog_ptr =
945 le32_to_cpup((__le32 *)tlv_data);
946 break;
947 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
948 if (tlv_len != sizeof(u32))
949 goto invalid_tlv_len;
950 pieces->inst_evtlog_ptr =
951 le32_to_cpup((__le32 *)tlv_data);
952 break;
953 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
954 if (tlv_len != sizeof(u32))
955 goto invalid_tlv_len;
956 pieces->inst_evtlog_size =
957 le32_to_cpup((__le32 *)tlv_data);
958 break;
959 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
960 if (tlv_len != sizeof(u32))
961 goto invalid_tlv_len;
962 pieces->inst_errlog_ptr =
963 le32_to_cpup((__le32 *)tlv_data);
964 break;
965 case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
966 if (tlv_len)
967 goto invalid_tlv_len;
Emmanuel Grumbach637d79252012-02-07 10:55:46 +0200968 nic->fw.enhance_sensitivity_table = true;
Don Fryedf38332012-01-25 16:18:52 -0800969 break;
970 case IWL_UCODE_TLV_WOWLAN_INST:
971 pieces->wowlan_inst = tlv_data;
972 pieces->wowlan_inst_size = tlv_len;
973 break;
974 case IWL_UCODE_TLV_WOWLAN_DATA:
975 pieces->wowlan_data = tlv_data;
976 pieces->wowlan_data_size = tlv_len;
977 break;
978 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
979 if (tlv_len != sizeof(u32))
980 goto invalid_tlv_len;
981 capa->standard_phy_calibration_size =
982 le32_to_cpup((__le32 *)tlv_data);
983 break;
984 default:
Don Fry06e03f82012-02-07 14:21:32 -0800985 IWL_DEBUG_INFO(nic, "unknown TLV: %d\n", tlv_type);
Don Fryedf38332012-01-25 16:18:52 -0800986 break;
987 }
988 }
989
990 if (len) {
Don Fry06e03f82012-02-07 14:21:32 -0800991 IWL_ERR(nic, "invalid TLV after parsing: %zd\n", len);
992 iwl_print_hex_dump(nic, IWL_DL_FW, (u8 *)data, len);
Don Fryedf38332012-01-25 16:18:52 -0800993 return -EINVAL;
994 }
995
996 return 0;
997
998 invalid_tlv_len:
Don Fry06e03f82012-02-07 14:21:32 -0800999 IWL_ERR(nic, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
1000 iwl_print_hex_dump(nic, IWL_DL_FW, tlv_data, tlv_len);
Don Fryedf38332012-01-25 16:18:52 -08001001
1002 return -EINVAL;
1003}
1004
1005/**
1006 * iwl_ucode_callback - callback when firmware was loaded
1007 *
1008 * If loaded successfully, copies the firmware into buffers
1009 * for the card to fetch (via DMA).
1010 */
1011static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
1012{
Don Fry06e03f82012-02-07 14:21:32 -08001013 struct iwl_nic *nic = context;
1014 struct iwl_cfg *cfg = cfg(nic);
Don Fry4a986772012-02-06 17:06:07 -08001015 struct iwl_fw *fw = &nic->fw;
Don Fry06e03f82012-02-07 14:21:32 -08001016 struct iwl_priv *priv = priv(nic); /* temporary */
Don Fryedf38332012-01-25 16:18:52 -08001017 struct iwl_ucode_header *ucode;
1018 int err;
1019 struct iwlagn_firmware_pieces pieces;
Don Fry06e03f82012-02-07 14:21:32 -08001020 const unsigned int api_max = cfg->ucode_api_max;
1021 unsigned int api_ok = cfg->ucode_api_ok;
1022 const unsigned int api_min = cfg->ucode_api_min;
Don Fryedf38332012-01-25 16:18:52 -08001023 u32 api_ver;
1024 char buildstr[25];
1025 u32 build;
Don Fry4a986772012-02-06 17:06:07 -08001026
1027 fw->ucode_capa.max_probe_length = 200;
1028 fw->ucode_capa.standard_phy_calibration_size =
1029 IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
Don Fryedf38332012-01-25 16:18:52 -08001030
1031 if (!api_ok)
1032 api_ok = api_max;
1033
1034 memset(&pieces, 0, sizeof(pieces));
1035
1036 if (!ucode_raw) {
Don Fryf6fd51d2012-02-06 15:54:31 -08001037 if (nic->fw_index <= api_ok)
Don Fry06e03f82012-02-07 14:21:32 -08001038 IWL_ERR(nic,
Don Fryedf38332012-01-25 16:18:52 -08001039 "request for firmware file '%s' failed.\n",
Don Fry737805f2012-02-06 15:57:40 -08001040 nic->firmware_name);
Don Fryedf38332012-01-25 16:18:52 -08001041 goto try_again;
1042 }
1043
Don Fry06e03f82012-02-07 14:21:32 -08001044 IWL_DEBUG_INFO(nic, "Loaded firmware file '%s' (%zd bytes).\n",
Don Fry737805f2012-02-06 15:57:40 -08001045 nic->firmware_name, ucode_raw->size);
Don Fryedf38332012-01-25 16:18:52 -08001046
1047 /* Make sure that we got at least the API version number */
1048 if (ucode_raw->size < 4) {
Don Fry06e03f82012-02-07 14:21:32 -08001049 IWL_ERR(nic, "File size way too small!\n");
Don Fryedf38332012-01-25 16:18:52 -08001050 goto try_again;
1051 }
1052
1053 /* Data from ucode file: header followed by uCode images */
1054 ucode = (struct iwl_ucode_header *)ucode_raw->data;
1055
1056 if (ucode->ver)
Don Fry06e03f82012-02-07 14:21:32 -08001057 err = iwl_parse_v1_v2_firmware(nic, ucode_raw, &pieces);
Don Fryedf38332012-01-25 16:18:52 -08001058 else
Don Fry06e03f82012-02-07 14:21:32 -08001059 err = iwl_parse_tlv_firmware(nic, ucode_raw, &pieces,
Don Fry4a986772012-02-06 17:06:07 -08001060 &fw->ucode_capa);
Don Fryedf38332012-01-25 16:18:52 -08001061
1062 if (err)
1063 goto try_again;
1064
Don Fryd3596672012-02-06 15:51:15 -08001065 api_ver = IWL_UCODE_API(nic->fw.ucode_ver);
Don Fryedf38332012-01-25 16:18:52 -08001066 build = pieces.build;
1067
1068 /*
1069 * api_ver should match the api version forming part of the
1070 * firmware filename ... but we don't check for that and only rely
1071 * on the API version read from firmware header from here on forward
1072 */
1073 /* no api version check required for experimental uCode */
Don Fryf6fd51d2012-02-06 15:54:31 -08001074 if (nic->fw_index != UCODE_EXPERIMENTAL_INDEX) {
Don Fryedf38332012-01-25 16:18:52 -08001075 if (api_ver < api_min || api_ver > api_max) {
Don Fry06e03f82012-02-07 14:21:32 -08001076 IWL_ERR(nic,
Don Fryedf38332012-01-25 16:18:52 -08001077 "Driver unable to support your firmware API. "
1078 "Driver supports v%u, firmware is v%u.\n",
1079 api_max, api_ver);
1080 goto try_again;
1081 }
1082
1083 if (api_ver < api_ok) {
1084 if (api_ok != api_max)
Don Fry06e03f82012-02-07 14:21:32 -08001085 IWL_ERR(nic, "Firmware has old API version, "
Don Fryedf38332012-01-25 16:18:52 -08001086 "expected v%u through v%u, got v%u.\n",
1087 api_ok, api_max, api_ver);
1088 else
Don Fry06e03f82012-02-07 14:21:32 -08001089 IWL_ERR(nic, "Firmware has old API version, "
Don Fryedf38332012-01-25 16:18:52 -08001090 "expected v%u, got v%u.\n",
1091 api_max, api_ver);
Don Fry06e03f82012-02-07 14:21:32 -08001092 IWL_ERR(nic, "New firmware can be obtained from "
Don Fryedf38332012-01-25 16:18:52 -08001093 "http://www.intellinuxwireless.org/.\n");
1094 }
1095 }
1096
1097 if (build)
1098 sprintf(buildstr, " build %u%s", build,
Don Fryf6fd51d2012-02-06 15:54:31 -08001099 (nic->fw_index == UCODE_EXPERIMENTAL_INDEX)
Don Fryedf38332012-01-25 16:18:52 -08001100 ? " (EXP)" : "");
1101 else
1102 buildstr[0] = '\0';
1103
Don Fry06e03f82012-02-07 14:21:32 -08001104 IWL_INFO(nic, "loaded firmware version %u.%u.%u.%u%s\n",
Don Fryd3596672012-02-06 15:51:15 -08001105 IWL_UCODE_MAJOR(nic->fw.ucode_ver),
1106 IWL_UCODE_MINOR(nic->fw.ucode_ver),
1107 IWL_UCODE_API(nic->fw.ucode_ver),
1108 IWL_UCODE_SERIAL(nic->fw.ucode_ver),
Don Fryedf38332012-01-25 16:18:52 -08001109 buildstr);
1110
1111 snprintf(priv->hw->wiphy->fw_version,
1112 sizeof(priv->hw->wiphy->fw_version),
1113 "%u.%u.%u.%u%s",
Don Fryd3596672012-02-06 15:51:15 -08001114 IWL_UCODE_MAJOR(nic->fw.ucode_ver),
1115 IWL_UCODE_MINOR(nic->fw.ucode_ver),
1116 IWL_UCODE_API(nic->fw.ucode_ver),
1117 IWL_UCODE_SERIAL(nic->fw.ucode_ver),
Don Fryedf38332012-01-25 16:18:52 -08001118 buildstr);
1119
1120 /*
1121 * For any of the failures below (before allocating pci memory)
1122 * we will try to load a version with a smaller API -- maybe the
1123 * user just got a corrupted version of the latest API.
1124 */
1125
Don Fry06e03f82012-02-07 14:21:32 -08001126 IWL_DEBUG_INFO(nic, "f/w package hdr ucode version raw = 0x%x\n",
Don Fryd3596672012-02-06 15:51:15 -08001127 nic->fw.ucode_ver);
Don Fry06e03f82012-02-07 14:21:32 -08001128 IWL_DEBUG_INFO(nic, "f/w package hdr runtime inst size = %Zd\n",
Don Fryedf38332012-01-25 16:18:52 -08001129 pieces.inst_size);
Don Fry06e03f82012-02-07 14:21:32 -08001130 IWL_DEBUG_INFO(nic, "f/w package hdr runtime data size = %Zd\n",
Don Fryedf38332012-01-25 16:18:52 -08001131 pieces.data_size);
Don Fry06e03f82012-02-07 14:21:32 -08001132 IWL_DEBUG_INFO(nic, "f/w package hdr init inst size = %Zd\n",
Don Fryedf38332012-01-25 16:18:52 -08001133 pieces.init_size);
Don Fry06e03f82012-02-07 14:21:32 -08001134 IWL_DEBUG_INFO(nic, "f/w package hdr init data size = %Zd\n",
Don Fryedf38332012-01-25 16:18:52 -08001135 pieces.init_data_size);
1136
1137 /* Verify that uCode images will fit in card's SRAM */
Emmanuel Grumbachdae66d02012-02-07 12:56:26 +02001138 if (pieces.inst_size > cfg->max_inst_size) {
Don Fry06e03f82012-02-07 14:21:32 -08001139 IWL_ERR(nic, "uCode instr len %Zd too large to fit in\n",
Don Fryedf38332012-01-25 16:18:52 -08001140 pieces.inst_size);
1141 goto try_again;
1142 }
1143
Emmanuel Grumbachdae66d02012-02-07 12:56:26 +02001144 if (pieces.data_size > cfg->max_data_size) {
Don Fry06e03f82012-02-07 14:21:32 -08001145 IWL_ERR(nic, "uCode data len %Zd too large to fit in\n",
Don Fryedf38332012-01-25 16:18:52 -08001146 pieces.data_size);
1147 goto try_again;
1148 }
1149
Emmanuel Grumbachdae66d02012-02-07 12:56:26 +02001150 if (pieces.init_size > cfg->max_inst_size) {
Don Fry06e03f82012-02-07 14:21:32 -08001151 IWL_ERR(nic, "uCode init instr len %Zd too large to fit in\n",
Don Fryedf38332012-01-25 16:18:52 -08001152 pieces.init_size);
1153 goto try_again;
1154 }
1155
Emmanuel Grumbachdae66d02012-02-07 12:56:26 +02001156 if (pieces.init_data_size > cfg->max_data_size) {
Don Fry06e03f82012-02-07 14:21:32 -08001157 IWL_ERR(nic, "uCode init data len %Zd too large to fit in\n",
Don Fryedf38332012-01-25 16:18:52 -08001158 pieces.init_data_size);
1159 goto try_again;
1160 }
1161
1162 /* Allocate ucode buffers for card's bus-master loading ... */
1163
1164 /* Runtime instructions and 2 copies of data:
1165 * 1) unmodified from disk
1166 * 2) backup cache for save/restore during power-downs */
Don Fry06e03f82012-02-07 14:21:32 -08001167 if (iwl_alloc_fw_desc(nic, &nic->fw.ucode_rt.code,
Don Fryedf38332012-01-25 16:18:52 -08001168 pieces.inst, pieces.inst_size))
1169 goto err_pci_alloc;
Don Fry06e03f82012-02-07 14:21:32 -08001170 if (iwl_alloc_fw_desc(nic, &nic->fw.ucode_rt.data,
Don Fryedf38332012-01-25 16:18:52 -08001171 pieces.data, pieces.data_size))
1172 goto err_pci_alloc;
1173
1174 /* Initialization instructions and data */
1175 if (pieces.init_size && pieces.init_data_size) {
Don Fry06e03f82012-02-07 14:21:32 -08001176 if (iwl_alloc_fw_desc(nic,
1177 &nic->fw.ucode_init.code,
Don Fryedf38332012-01-25 16:18:52 -08001178 pieces.init, pieces.init_size))
1179 goto err_pci_alloc;
Don Fry06e03f82012-02-07 14:21:32 -08001180 if (iwl_alloc_fw_desc(nic,
1181 &nic->fw.ucode_init.data,
Don Fryedf38332012-01-25 16:18:52 -08001182 pieces.init_data, pieces.init_data_size))
1183 goto err_pci_alloc;
1184 }
1185
1186 /* WoWLAN instructions and data */
1187 if (pieces.wowlan_inst_size && pieces.wowlan_data_size) {
Don Fry06e03f82012-02-07 14:21:32 -08001188 if (iwl_alloc_fw_desc(nic,
1189 &nic->fw.ucode_wowlan.code,
Don Fryedf38332012-01-25 16:18:52 -08001190 pieces.wowlan_inst,
1191 pieces.wowlan_inst_size))
1192 goto err_pci_alloc;
Don Fry06e03f82012-02-07 14:21:32 -08001193 if (iwl_alloc_fw_desc(nic,
1194 &nic->fw.ucode_wowlan.data,
Don Fryedf38332012-01-25 16:18:52 -08001195 pieces.wowlan_data,
1196 pieces.wowlan_data_size))
1197 goto err_pci_alloc;
1198 }
1199
1200 /* Now that we can no longer fail, copy information */
1201
1202 /*
1203 * The (size - 16) / 12 formula is based on the information recorded
1204 * for each event, which is of mode 1 (including timestamp) for all
1205 * new microcodes that include this information.
1206 */
Don Fry86551122012-02-07 14:03:55 -08001207 nic->init_evtlog_ptr = pieces.init_evtlog_ptr;
Don Fryedf38332012-01-25 16:18:52 -08001208 if (pieces.init_evtlog_size)
Don Fry86551122012-02-07 14:03:55 -08001209 nic->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
Don Fryedf38332012-01-25 16:18:52 -08001210 else
Don Fry86551122012-02-07 14:03:55 -08001211 nic->init_evtlog_size =
Don Fry06e03f82012-02-07 14:21:32 -08001212 cfg->base_params->max_event_log_size;
Don Fry86551122012-02-07 14:03:55 -08001213 nic->init_errlog_ptr = pieces.init_errlog_ptr;
1214 nic->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
Don Fryedf38332012-01-25 16:18:52 -08001215 if (pieces.inst_evtlog_size)
Don Fry86551122012-02-07 14:03:55 -08001216 nic->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
Don Fryedf38332012-01-25 16:18:52 -08001217 else
Don Fry86551122012-02-07 14:03:55 -08001218 nic->inst_evtlog_size =
Don Fry06e03f82012-02-07 14:21:32 -08001219 cfg->base_params->max_event_log_size;
Don Fry86551122012-02-07 14:03:55 -08001220 nic->inst_errlog_ptr = pieces.inst_errlog_ptr;
Don Fryedf38332012-01-25 16:18:52 -08001221#ifndef CONFIG_IWLWIFI_P2P
Don Fry4a986772012-02-06 17:06:07 -08001222 fw->ucode_capa.flags &= ~IWL_UCODE_TLV_FLAGS_PAN;
Don Fryedf38332012-01-25 16:18:52 -08001223#endif
1224
1225 priv->new_scan_threshold_behaviour =
Don Fry4a986772012-02-06 17:06:07 -08001226 !!(fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NEWSCAN);
Don Fryedf38332012-01-25 16:18:52 -08001227
Don Fry06e03f82012-02-07 14:21:32 -08001228 if (!(cfg->sku & EEPROM_SKU_CAP_IPAN_ENABLE))
Don Fry4a986772012-02-06 17:06:07 -08001229 fw->ucode_capa.flags &= ~IWL_UCODE_TLV_FLAGS_PAN;
Don Fryedf38332012-01-25 16:18:52 -08001230
1231 /*
1232 * if not PAN, then don't support P2P -- might be a uCode
1233 * packaging bug or due to the eeprom check above
1234 */
Don Fry4a986772012-02-06 17:06:07 -08001235 if (!(fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN))
1236 fw->ucode_capa.flags &= ~IWL_UCODE_TLV_FLAGS_P2P;
Don Fryedf38332012-01-25 16:18:52 -08001237
Don Fry4a986772012-02-06 17:06:07 -08001238 if (fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN) {
Don Fryedf38332012-01-25 16:18:52 -08001239 priv->sta_key_max_num = STA_KEY_MAX_NUM_PAN;
Don Fry06e03f82012-02-07 14:21:32 -08001240 nic->shrd->cmd_queue = IWL_IPAN_CMD_QUEUE_NUM;
Don Fryedf38332012-01-25 16:18:52 -08001241 } else {
1242 priv->sta_key_max_num = STA_KEY_MAX_NUM;
Don Fry06e03f82012-02-07 14:21:32 -08001243 nic->shrd->cmd_queue = IWL_DEFAULT_CMD_QUEUE_NUM;
Don Fryedf38332012-01-25 16:18:52 -08001244 }
1245 /*
1246 * figure out the offset of chain noise reset and gain commands
1247 * base on the size of standard phy calibration commands table size
1248 */
Don Fry4a986772012-02-06 17:06:07 -08001249 if (fw->ucode_capa.standard_phy_calibration_size >
Don Fryedf38332012-01-25 16:18:52 -08001250 IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
Don Fry4a986772012-02-06 17:06:07 -08001251 fw->ucode_capa.standard_phy_calibration_size =
Don Fryedf38332012-01-25 16:18:52 -08001252 IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
1253
1254 priv->phy_calib_chain_noise_reset_cmd =
Don Fry4a986772012-02-06 17:06:07 -08001255 fw->ucode_capa.standard_phy_calibration_size;
Don Fryedf38332012-01-25 16:18:52 -08001256 priv->phy_calib_chain_noise_gain_cmd =
Don Fry4a986772012-02-06 17:06:07 -08001257 fw->ucode_capa.standard_phy_calibration_size + 1;
Don Fryedf38332012-01-25 16:18:52 -08001258
1259 /* initialize all valid contexts */
Don Fry4a986772012-02-06 17:06:07 -08001260 iwl_init_context(priv, fw->ucode_capa.flags);
Don Fryedf38332012-01-25 16:18:52 -08001261
1262 /**************************************************
1263 * This is still part of probe() in a sense...
1264 *
1265 * 9. Setup and register with mac80211 and debugfs
1266 **************************************************/
Don Fry4a986772012-02-06 17:06:07 -08001267 err = iwlagn_mac_setup_register(priv, &fw->ucode_capa);
Don Fryedf38332012-01-25 16:18:52 -08001268 if (err)
1269 goto out_unbind;
1270
1271 err = iwl_dbgfs_register(priv, DRV_NAME);
1272 if (err)
Don Fry06e03f82012-02-07 14:21:32 -08001273 IWL_ERR(nic,
1274 "failed to create debugfs files. Ignoring error: %d\n",
1275 err);
Don Fryedf38332012-01-25 16:18:52 -08001276
1277 /* We have our copies now, allow OS release its copies */
1278 release_firmware(ucode_raw);
Don Fry96502ce2012-02-06 16:00:14 -08001279 complete(&nic->request_firmware_complete);
Don Fryedf38332012-01-25 16:18:52 -08001280 return;
1281
1282 try_again:
1283 /* try next, if any */
Don Fry06e03f82012-02-07 14:21:32 -08001284 if (iwl_request_firmware(nic, false))
Don Fryedf38332012-01-25 16:18:52 -08001285 goto out_unbind;
1286 release_firmware(ucode_raw);
1287 return;
1288
1289 err_pci_alloc:
Don Fry06e03f82012-02-07 14:21:32 -08001290 IWL_ERR(nic, "failed to allocate pci memory\n");
1291 iwl_dealloc_ucode(nic);
Don Fryedf38332012-01-25 16:18:52 -08001292 out_unbind:
Don Fry96502ce2012-02-06 16:00:14 -08001293 complete(&nic->request_firmware_complete);
Don Fry06e03f82012-02-07 14:21:32 -08001294 device_release_driver(trans(nic)->dev);
Don Fryedf38332012-01-25 16:18:52 -08001295 release_firmware(ucode_raw);
1296}
1297