iwlwifi: move ucode_type from shared to op_mode
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / iwl-ucode.c
1 /******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
5 * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
6 *
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/init.h>
32
33 #include "iwl-dev.h"
34 #include "iwl-core.h"
35 #include "iwl-io.h"
36 #include "iwl-agn-hw.h"
37 #include "iwl-agn.h"
38 #include "iwl-agn-calib.h"
39 #include "iwl-trans.h"
40 #include "iwl-fh.h"
41 #include "iwl-op-mode.h"
42
43 /******************************************************************************
44 *
45 * uCode download functions
46 *
47 ******************************************************************************/
48
49 static inline const struct fw_img *
50 iwl_get_ucode_image(struct iwl_priv *priv, enum iwl_ucode_type ucode_type)
51 {
52 if (ucode_type >= IWL_UCODE_TYPE_MAX)
53 return NULL;
54
55 return &priv->fw->img[ucode_type];
56 }
57
58 /*
59 * Calibration
60 */
61 static int iwl_set_Xtal_calib(struct iwl_priv *priv)
62 {
63 struct iwl_calib_xtal_freq_cmd cmd;
64 __le16 *xtal_calib =
65 (__le16 *)iwl_eeprom_query_addr(priv->shrd, EEPROM_XTAL);
66
67 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
68 cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
69 cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
70 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
71 }
72
73 static int iwl_set_temperature_offset_calib(struct iwl_priv *priv)
74 {
75 struct iwl_calib_temperature_offset_cmd cmd;
76 __le16 *offset_calib =
77 (__le16 *)iwl_eeprom_query_addr(priv->shrd,
78 EEPROM_RAW_TEMPERATURE);
79
80 memset(&cmd, 0, sizeof(cmd));
81 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
82 memcpy(&cmd.radio_sensor_offset, offset_calib, sizeof(*offset_calib));
83 if (!(cmd.radio_sensor_offset))
84 cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
85
86 IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
87 le16_to_cpu(cmd.radio_sensor_offset));
88 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
89 }
90
91 static int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
92 {
93 struct iwl_calib_temperature_offset_v2_cmd cmd;
94 __le16 *offset_calib_high = (__le16 *)iwl_eeprom_query_addr(priv->shrd,
95 EEPROM_KELVIN_TEMPERATURE);
96 __le16 *offset_calib_low =
97 (__le16 *)iwl_eeprom_query_addr(priv->shrd,
98 EEPROM_RAW_TEMPERATURE);
99 struct iwl_eeprom_calib_hdr *hdr;
100
101 memset(&cmd, 0, sizeof(cmd));
102 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
103 hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv->shrd,
104 EEPROM_CALIB_ALL);
105 memcpy(&cmd.radio_sensor_offset_high, offset_calib_high,
106 sizeof(*offset_calib_high));
107 memcpy(&cmd.radio_sensor_offset_low, offset_calib_low,
108 sizeof(*offset_calib_low));
109 if (!(cmd.radio_sensor_offset_low)) {
110 IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
111 cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
112 cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
113 }
114 memcpy(&cmd.burntVoltageRef, &hdr->voltage,
115 sizeof(hdr->voltage));
116
117 IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
118 le16_to_cpu(cmd.radio_sensor_offset_high));
119 IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
120 le16_to_cpu(cmd.radio_sensor_offset_low));
121 IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
122 le16_to_cpu(cmd.burntVoltageRef));
123
124 return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
125 }
126
127 static int iwl_send_calib_cfg(struct iwl_priv *priv)
128 {
129 struct iwl_calib_cfg_cmd calib_cfg_cmd;
130 struct iwl_host_cmd cmd = {
131 .id = CALIBRATION_CFG_CMD,
132 .len = { sizeof(struct iwl_calib_cfg_cmd), },
133 .data = { &calib_cfg_cmd, },
134 };
135
136 memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
137 calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
138 calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
139 calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
140 calib_cfg_cmd.ucd_calib_cfg.flags =
141 IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
142
143 return iwl_dvm_send_cmd(priv, &cmd);
144 }
145
146 int iwl_init_alive_start(struct iwl_priv *priv)
147 {
148 int ret;
149
150 if (cfg(priv)->bt_params &&
151 cfg(priv)->bt_params->advanced_bt_coexist) {
152 /*
153 * Tell uCode we are ready to perform calibration
154 * need to perform this before any calibration
155 * no need to close the envlope since we are going
156 * to load the runtime uCode later.
157 */
158 ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
159 BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
160 if (ret)
161 return ret;
162
163 }
164
165 ret = iwl_send_calib_cfg(priv);
166 if (ret)
167 return ret;
168
169 /**
170 * temperature offset calibration is only needed for runtime ucode,
171 * so prepare the value now.
172 */
173 if (cfg(priv)->need_temp_offset_calib) {
174 if (cfg(priv)->temp_offset_v2)
175 return iwl_set_temperature_offset_calib_v2(priv);
176 else
177 return iwl_set_temperature_offset_calib(priv);
178 }
179
180 return 0;
181 }
182
183 static int iwl_send_wimax_coex(struct iwl_priv *priv)
184 {
185 struct iwl_wimax_coex_cmd coex_cmd;
186
187 /* coexistence is disabled */
188 memset(&coex_cmd, 0, sizeof(coex_cmd));
189
190 return iwl_dvm_send_cmd_pdu(priv,
191 COEX_PRIORITY_TABLE_CMD, CMD_SYNC,
192 sizeof(coex_cmd), &coex_cmd);
193 }
194
195 static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
196 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
197 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
198 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
199 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
200 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
201 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
202 ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
203 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
204 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
205 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
206 ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
207 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
208 ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
209 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
210 ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
211 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
212 ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
213 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
214 0, 0, 0, 0, 0, 0, 0
215 };
216
217 void iwl_send_prio_tbl(struct iwl_priv *priv)
218 {
219 struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
220
221 memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
222 sizeof(iwl_bt_prio_tbl));
223 if (iwl_dvm_send_cmd_pdu(priv,
224 REPLY_BT_COEX_PRIO_TABLE, CMD_SYNC,
225 sizeof(prio_tbl_cmd), &prio_tbl_cmd))
226 IWL_ERR(priv, "failed to send BT prio tbl command\n");
227 }
228
229 int iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
230 {
231 struct iwl_bt_coex_prot_env_cmd env_cmd;
232 int ret;
233
234 env_cmd.action = action;
235 env_cmd.type = type;
236 ret = iwl_dvm_send_cmd_pdu(priv,
237 REPLY_BT_COEX_PROT_ENV, CMD_SYNC,
238 sizeof(env_cmd), &env_cmd);
239 if (ret)
240 IWL_ERR(priv, "failed to send BT env command\n");
241 return ret;
242 }
243
244
245 static int iwl_alive_notify(struct iwl_priv *priv)
246 {
247 int ret;
248
249 iwl_trans_fw_alive(trans(priv));
250
251 priv->passive_no_rx = false;
252 priv->transport_queue_stop = 0;
253
254 ret = iwl_send_wimax_coex(priv);
255 if (ret)
256 return ret;
257
258 if (!cfg(priv)->no_xtal_calib) {
259 ret = iwl_set_Xtal_calib(priv);
260 if (ret)
261 return ret;
262 }
263
264 return iwl_send_calib_results(priv);
265 }
266
267
268 /**
269 * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
270 * using sample data 100 bytes apart. If these sample points are good,
271 * it's a pretty good bet that everything between them is good, too.
272 */
273 static int iwl_verify_sec_sparse(struct iwl_priv *priv,
274 const struct fw_desc *fw_desc)
275 {
276 __le32 *image = (__le32 *)fw_desc->v_addr;
277 u32 len = fw_desc->len;
278 u32 val;
279 u32 i;
280
281 IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
282
283 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
284 /* read data comes through single port, auto-incr addr */
285 /* NOTE: Use the debugless read so we don't flood kernel log
286 * if IWL_DL_IO is set */
287 iwl_write_direct32(trans(priv), HBUS_TARG_MEM_RADDR,
288 i + fw_desc->offset);
289 val = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
290 if (val != le32_to_cpu(*image))
291 return -EIO;
292 }
293
294 return 0;
295 }
296
297 static void iwl_print_mismatch_sec(struct iwl_priv *priv,
298 const struct fw_desc *fw_desc)
299 {
300 __le32 *image = (__le32 *)fw_desc->v_addr;
301 u32 len = fw_desc->len;
302 u32 val;
303 u32 offs;
304 int errors = 0;
305
306 IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
307
308 iwl_write_direct32(trans(priv), HBUS_TARG_MEM_RADDR,
309 fw_desc->offset);
310
311 for (offs = 0;
312 offs < len && errors < 20;
313 offs += sizeof(u32), image++) {
314 /* read data comes through single port, auto-incr addr */
315 val = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
316 if (val != le32_to_cpu(*image)) {
317 IWL_ERR(priv, "uCode INST section at "
318 "offset 0x%x, is 0x%x, s/b 0x%x\n",
319 offs, val, le32_to_cpu(*image));
320 errors++;
321 }
322 }
323 }
324
325 /**
326 * iwl_verify_ucode - determine which instruction image is in SRAM,
327 * and verify its contents
328 */
329 static int iwl_verify_ucode(struct iwl_priv *priv,
330 enum iwl_ucode_type ucode_type)
331 {
332 const struct fw_img *img = iwl_get_ucode_image(priv, ucode_type);
333
334 if (!img) {
335 IWL_ERR(priv, "Invalid ucode requested (%d)\n", ucode_type);
336 return -EINVAL;
337 }
338
339 if (!iwl_verify_sec_sparse(priv, &img->sec[IWL_UCODE_SECTION_INST])) {
340 IWL_DEBUG_FW(priv, "uCode is good in inst SRAM\n");
341 return 0;
342 }
343
344 IWL_ERR(priv, "UCODE IMAGE IN INSTRUCTION SRAM NOT VALID!!\n");
345
346 iwl_print_mismatch_sec(priv, &img->sec[IWL_UCODE_SECTION_INST]);
347 return -EIO;
348 }
349
350 struct iwl_alive_data {
351 bool valid;
352 u8 subtype;
353 };
354
355 static bool iwl_alive_fn(struct iwl_notif_wait_data *notif_wait,
356 struct iwl_rx_packet *pkt, void *data)
357 {
358 struct iwl_priv *priv =
359 container_of(notif_wait, struct iwl_priv, notif_wait);
360 struct iwl_alive_data *alive_data = data;
361 struct iwl_alive_resp *palive;
362
363 palive = (void *)pkt->data;
364
365 IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
366 "0x%01X 0x%01X\n",
367 palive->is_valid, palive->ver_type,
368 palive->ver_subtype);
369
370 priv->device_pointers.error_event_table =
371 le32_to_cpu(palive->error_event_table_ptr);
372 priv->device_pointers.log_event_table =
373 le32_to_cpu(palive->log_event_table_ptr);
374
375 alive_data->subtype = palive->ver_subtype;
376 alive_data->valid = palive->is_valid == UCODE_VALID_OK;
377
378 return true;
379 }
380
381 #define UCODE_ALIVE_TIMEOUT HZ
382 #define UCODE_CALIB_TIMEOUT (2*HZ)
383
384 int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
385 enum iwl_ucode_type ucode_type)
386 {
387 struct iwl_notification_wait alive_wait;
388 struct iwl_alive_data alive_data;
389 const struct fw_img *fw;
390 int ret;
391 enum iwl_ucode_type old_type;
392 static const u8 alive_cmd[] = { REPLY_ALIVE };
393
394 old_type = priv->cur_ucode;
395 priv->cur_ucode = ucode_type;
396 fw = iwl_get_ucode_image(priv, ucode_type);
397
398 priv->ucode_loaded = false;
399
400 if (!fw)
401 return -EINVAL;
402
403 iwl_init_notification_wait(&priv->notif_wait, &alive_wait,
404 alive_cmd, ARRAY_SIZE(alive_cmd),
405 iwl_alive_fn, &alive_data);
406
407 ret = iwl_trans_start_fw(trans(priv), fw);
408 if (ret) {
409 priv->cur_ucode = old_type;
410 iwl_remove_notification(&priv->notif_wait, &alive_wait);
411 return ret;
412 }
413
414 /*
415 * Some things may run in the background now, but we
416 * just wait for the ALIVE notification here.
417 */
418 ret = iwl_wait_notification(&priv->notif_wait, &alive_wait,
419 UCODE_ALIVE_TIMEOUT);
420 if (ret) {
421 priv->cur_ucode = old_type;
422 return ret;
423 }
424
425 if (!alive_data.valid) {
426 IWL_ERR(priv, "Loaded ucode is not valid!\n");
427 priv->cur_ucode = old_type;
428 return -EIO;
429 }
430
431 /*
432 * This step takes a long time (60-80ms!!) and
433 * WoWLAN image should be loaded quickly, so
434 * skip it for WoWLAN.
435 */
436 if (ucode_type != IWL_UCODE_WOWLAN) {
437 ret = iwl_verify_ucode(priv, ucode_type);
438 if (ret) {
439 priv->cur_ucode = old_type;
440 return ret;
441 }
442
443 /* delay a bit to give rfkill time to run */
444 msleep(5);
445 }
446
447 ret = iwl_alive_notify(priv);
448 if (ret) {
449 IWL_WARN(priv,
450 "Could not complete ALIVE transition: %d\n", ret);
451 priv->cur_ucode = old_type;
452 return ret;
453 }
454
455 priv->ucode_loaded = true;
456
457 return 0;
458 }
459
460 static bool iwlagn_wait_calib(struct iwl_notif_wait_data *notif_wait,
461 struct iwl_rx_packet *pkt, void *data)
462 {
463 struct iwl_priv *priv = data;
464 struct iwl_calib_hdr *hdr;
465 int len;
466
467 if (pkt->hdr.cmd != CALIBRATION_RES_NOTIFICATION) {
468 WARN_ON(pkt->hdr.cmd != CALIBRATION_COMPLETE_NOTIFICATION);
469 return true;
470 }
471
472 hdr = (struct iwl_calib_hdr *)pkt->data;
473 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
474
475 /* reduce the size by the length field itself */
476 len -= sizeof(__le32);
477
478 if (iwl_calib_set(priv, hdr, len))
479 IWL_ERR(priv, "Failed to record calibration data %d\n",
480 hdr->op_code);
481
482 return false;
483 }
484
485 int iwl_run_init_ucode(struct iwl_priv *priv)
486 {
487 struct iwl_notification_wait calib_wait;
488 static const u8 calib_complete[] = {
489 CALIBRATION_RES_NOTIFICATION,
490 CALIBRATION_COMPLETE_NOTIFICATION
491 };
492 int ret;
493
494 lockdep_assert_held(&priv->mutex);
495
496 /* No init ucode required? Curious, but maybe ok */
497 if (!priv->fw->img[IWL_UCODE_INIT].sec[0].len)
498 return 0;
499
500 if (priv->init_ucode_run)
501 return 0;
502
503 iwl_init_notification_wait(&priv->notif_wait, &calib_wait,
504 calib_complete, ARRAY_SIZE(calib_complete),
505 iwlagn_wait_calib, priv);
506
507 /* Will also start the device */
508 ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
509 if (ret)
510 goto error;
511
512 ret = iwl_init_alive_start(priv);
513 if (ret)
514 goto error;
515
516 /*
517 * Some things may run in the background now, but we
518 * just wait for the calibration complete notification.
519 */
520 ret = iwl_wait_notification(&priv->notif_wait, &calib_wait,
521 UCODE_CALIB_TIMEOUT);
522 if (!ret)
523 priv->init_ucode_run = true;
524
525 goto out;
526
527 error:
528 iwl_remove_notification(&priv->notif_wait, &calib_wait);
529 out:
530 /* Whatever happened, stop the device */
531 iwl_trans_stop_device(trans(priv));
532 priv->ucode_loaded = false;
533
534 return ret;
535 }
This page took 0.046054 seconds and 5 git commands to generate.