iwlwifi: consolidate the start_device flow
[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/module.h>
32 #include <linux/init.h>
33 #include <linux/sched.h>
34 #include <linux/dma-mapping.h>
35
36 #include "iwl-wifi.h"
37 #include "iwl-dev.h"
38 #include "iwl-core.h"
39 #include "iwl-io.h"
40 #include "iwl-agn-hw.h"
41 #include "iwl-agn.h"
42 #include "iwl-agn-calib.h"
43 #include "iwl-trans.h"
44 #include "iwl-fh.h"
45
46 static 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
77 /******************************************************************************
78 *
79 * uCode download functions
80 *
81 ******************************************************************************/
82
83 static void iwl_free_fw_desc(struct iwl_trans *trans, struct fw_desc *desc)
84 {
85 if (desc->v_addr)
86 dma_free_coherent(trans->dev, desc->len,
87 desc->v_addr, desc->p_addr);
88 desc->v_addr = NULL;
89 desc->len = 0;
90 }
91
92 static void iwl_free_fw_img(struct iwl_trans *trans, struct fw_img *img)
93 {
94 iwl_free_fw_desc(trans, &img->code);
95 iwl_free_fw_desc(trans, &img->data);
96 }
97
98 void iwl_dealloc_ucode(struct iwl_trans *trans)
99 {
100 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);
103 }
104
105 int iwl_alloc_fw_desc(struct iwl_trans *trans, struct fw_desc *desc,
106 const void *data, size_t len)
107 {
108 if (!len) {
109 desc->v_addr = NULL;
110 return -EINVAL;
111 }
112
113 desc->v_addr = dma_alloc_coherent(trans->dev, len,
114 &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
123 static inline struct fw_img *iwl_get_ucode_image(struct iwl_trans *trans,
124 enum iwl_ucode_type ucode_type)
125 {
126 switch (ucode_type) {
127 case IWL_UCODE_INIT:
128 return &trans->ucode_init;
129 case IWL_UCODE_WOWLAN:
130 return &trans->ucode_wowlan;
131 case IWL_UCODE_REGULAR:
132 return &trans->ucode_rt;
133 case IWL_UCODE_NONE:
134 break;
135 }
136 return NULL;
137 }
138
139 /*
140 * Calibration
141 */
142 static int iwl_set_Xtal_calib(struct iwl_trans *trans)
143 {
144 struct iwl_calib_xtal_freq_cmd cmd;
145 __le16 *xtal_calib =
146 (__le16 *)iwl_eeprom_query_addr(trans->shrd, EEPROM_XTAL);
147
148 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
149 cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
150 cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
151 return iwl_calib_set(trans, (void *)&cmd, sizeof(cmd));
152 }
153
154 static int iwl_set_temperature_offset_calib(struct iwl_trans *trans)
155 {
156 struct iwl_calib_temperature_offset_cmd cmd;
157 __le16 *offset_calib =
158 (__le16 *)iwl_eeprom_query_addr(trans->shrd,
159 EEPROM_RAW_TEMPERATURE);
160
161 memset(&cmd, 0, sizeof(cmd));
162 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
163 memcpy(&cmd.radio_sensor_offset, offset_calib, sizeof(*offset_calib));
164 if (!(cmd.radio_sensor_offset))
165 cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
166
167 IWL_DEBUG_CALIB(trans, "Radio sensor offset: %d\n",
168 le16_to_cpu(cmd.radio_sensor_offset));
169 return iwl_calib_set(trans, (void *)&cmd, sizeof(cmd));
170 }
171
172 static int iwl_set_temperature_offset_calib_v2(struct iwl_trans *trans)
173 {
174 struct iwl_calib_temperature_offset_v2_cmd cmd;
175 __le16 *offset_calib_high = (__le16 *)iwl_eeprom_query_addr(trans->shrd,
176 EEPROM_KELVIN_TEMPERATURE);
177 __le16 *offset_calib_low =
178 (__le16 *)iwl_eeprom_query_addr(trans->shrd,
179 EEPROM_RAW_TEMPERATURE);
180 struct iwl_eeprom_calib_hdr *hdr;
181
182 memset(&cmd, 0, sizeof(cmd));
183 iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
184 hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(trans->shrd,
185 EEPROM_CALIB_ALL);
186 memcpy(&cmd.radio_sensor_offset_high, offset_calib_high,
187 sizeof(*offset_calib_high));
188 memcpy(&cmd.radio_sensor_offset_low, offset_calib_low,
189 sizeof(*offset_calib_low));
190 if (!(cmd.radio_sensor_offset_low)) {
191 IWL_DEBUG_CALIB(trans, "no info in EEPROM, use default\n");
192 cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
193 cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
194 }
195 memcpy(&cmd.burntVoltageRef, &hdr->voltage,
196 sizeof(hdr->voltage));
197
198 IWL_DEBUG_CALIB(trans, "Radio sensor offset high: %d\n",
199 le16_to_cpu(cmd.radio_sensor_offset_high));
200 IWL_DEBUG_CALIB(trans, "Radio sensor offset low: %d\n",
201 le16_to_cpu(cmd.radio_sensor_offset_low));
202 IWL_DEBUG_CALIB(trans, "Voltage Ref: %d\n",
203 le16_to_cpu(cmd.burntVoltageRef));
204
205 return iwl_calib_set(trans, (void *)&cmd, sizeof(cmd));
206 }
207
208 static int iwl_send_calib_cfg(struct iwl_trans *trans)
209 {
210 struct iwl_calib_cfg_cmd calib_cfg_cmd;
211 struct iwl_host_cmd cmd = {
212 .id = CALIBRATION_CFG_CMD,
213 .len = { sizeof(struct iwl_calib_cfg_cmd), },
214 .data = { &calib_cfg_cmd, },
215 };
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;
221 calib_cfg_cmd.ucd_calib_cfg.flags =
222 IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
223
224 return iwl_trans_send_cmd(trans, &cmd);
225 }
226
227 int iwlagn_rx_calib_result(struct iwl_priv *priv,
228 struct iwl_rx_mem_buffer *rxb,
229 struct iwl_device_cmd *cmd)
230 {
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;
234
235 /* reduce the size of the length field itself */
236 len -= 4;
237
238 if (iwl_calib_set(trans(priv), hdr, len))
239 IWL_ERR(priv, "Failed to record calibration data %d\n",
240 hdr->op_code);
241
242 return 0;
243 }
244
245 int iwl_init_alive_start(struct iwl_trans *trans)
246 {
247 int ret;
248
249 if (cfg(trans)->bt_params &&
250 cfg(trans)->bt_params->advanced_bt_coexist) {
251 /*
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 */
257 ret = iwl_send_bt_env(trans, IWL_BT_COEX_ENV_OPEN,
258 BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
259 if (ret)
260 return ret;
261
262 }
263
264 ret = iwl_send_calib_cfg(trans);
265 if (ret)
266 return ret;
267
268 /**
269 * temperature offset calibration is only needed for runtime ucode,
270 * so prepare the value now.
271 */
272 if (cfg(trans)->need_temp_offset_calib) {
273 if (cfg(trans)->temp_offset_v2)
274 return iwl_set_temperature_offset_calib_v2(trans);
275 else
276 return iwl_set_temperature_offset_calib(trans);
277 }
278
279 return 0;
280 }
281
282 static int iwl_send_wimax_coex(struct iwl_trans *trans)
283 {
284 struct iwl_wimax_coex_cmd coex_cmd;
285
286 if (cfg(trans)->base_params->support_wimax_coexist) {
287 /* 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 }
305 return iwl_trans_send_cmd_pdu(trans,
306 COEX_PRIORITY_TABLE_CMD, CMD_SYNC,
307 sizeof(coex_cmd), &coex_cmd);
308 }
309
310 static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
311 ((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
332 void iwl_send_prio_tbl(struct iwl_trans *trans)
333 {
334 struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
335
336 memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
337 sizeof(iwl_bt_prio_tbl));
338 if (iwl_trans_send_cmd_pdu(trans,
339 REPLY_BT_COEX_PRIO_TABLE, CMD_SYNC,
340 sizeof(prio_tbl_cmd), &prio_tbl_cmd))
341 IWL_ERR(trans, "failed to send BT prio tbl command\n");
342 }
343
344 int iwl_send_bt_env(struct iwl_trans *trans, u8 action, u8 type)
345 {
346 struct iwl_bt_coex_prot_env_cmd env_cmd;
347 int ret;
348
349 env_cmd.action = action;
350 env_cmd.type = type;
351 ret = iwl_trans_send_cmd_pdu(trans,
352 REPLY_BT_COEX_PROT_ENV, CMD_SYNC,
353 sizeof(env_cmd), &env_cmd);
354 if (ret)
355 IWL_ERR(trans, "failed to send BT env command\n");
356 return ret;
357 }
358
359
360 static int iwl_alive_notify(struct iwl_trans *trans)
361 {
362 struct iwl_priv *priv = priv(trans);
363 struct iwl_rxon_context *ctx;
364 int ret;
365
366 if (!priv->tx_cmd_pool)
367 priv->tx_cmd_pool =
368 kmem_cache_create("iwl_dev_cmd",
369 sizeof(struct iwl_device_cmd),
370 sizeof(void *), 0, NULL);
371
372 if (!priv->tx_cmd_pool)
373 return -ENOMEM;
374
375 iwl_trans_fw_alive(trans);
376 for_each_context(priv, ctx)
377 ctx->last_tx_rejected = false;
378
379 ret = iwl_send_wimax_coex(trans);
380 if (ret)
381 return ret;
382
383 if (!cfg(priv)->no_xtal_calib) {
384 ret = iwl_set_Xtal_calib(trans);
385 if (ret)
386 return ret;
387 }
388
389 return iwl_send_calib_results(trans);
390 }
391
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 */
398 static int iwl_verify_inst_sparse(struct iwl_trans *trans,
399 struct fw_desc *fw_desc)
400 {
401 __le32 *image = (__le32 *)fw_desc->v_addr;
402 u32 len = fw_desc->len;
403 u32 val;
404 u32 i;
405
406 IWL_DEBUG_FW(trans, "ucode inst image size is %u\n", len);
407
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 */
412 iwl_write_direct32(trans, HBUS_TARG_MEM_RADDR,
413 i + IWLAGN_RTC_INST_LOWER_BOUND);
414 val = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
415 if (val != le32_to_cpu(*image))
416 return -EIO;
417 }
418
419 return 0;
420 }
421
422 static void iwl_print_mismatch_inst(struct iwl_trans *trans,
423 struct fw_desc *fw_desc)
424 {
425 __le32 *image = (__le32 *)fw_desc->v_addr;
426 u32 len = fw_desc->len;
427 u32 val;
428 u32 offs;
429 int errors = 0;
430
431 IWL_DEBUG_FW(trans, "ucode inst image size is %u\n", len);
432
433 iwl_write_direct32(trans, HBUS_TARG_MEM_RADDR,
434 IWLAGN_RTC_INST_LOWER_BOUND);
435
436 for (offs = 0;
437 offs < len && errors < 20;
438 offs += sizeof(u32), image++) {
439 /* read data comes through single port, auto-incr addr */
440 val = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
441 if (val != le32_to_cpu(*image)) {
442 IWL_ERR(trans, "uCode INST section at "
443 "offset 0x%x, is 0x%x, s/b 0x%x\n",
444 offs, val, le32_to_cpu(*image));
445 errors++;
446 }
447 }
448 }
449
450 /**
451 * iwl_verify_ucode - determine which instruction image is in SRAM,
452 * and verify its contents
453 */
454 static int iwl_verify_ucode(struct iwl_trans *trans,
455 enum iwl_ucode_type ucode_type)
456 {
457 struct fw_img *img = iwl_get_ucode_image(trans, ucode_type);
458
459 if (!img) {
460 IWL_ERR(trans, "Invalid ucode requested (%d)\n", ucode_type);
461 return -EINVAL;
462 }
463
464 if (!iwl_verify_inst_sparse(trans, &img->code)) {
465 IWL_DEBUG_FW(trans, "uCode is good in inst SRAM\n");
466 return 0;
467 }
468
469 IWL_ERR(trans, "UCODE IMAGE IN INSTRUCTION SRAM NOT VALID!!\n");
470
471 iwl_print_mismatch_inst(trans, &img->code);
472 return -EIO;
473 }
474
475 struct iwl_alive_data {
476 bool valid;
477 u8 subtype;
478 };
479
480 static void iwl_alive_fn(struct iwl_trans *trans,
481 struct iwl_rx_packet *pkt,
482 void *data)
483 {
484 struct iwl_alive_data *alive_data = data;
485 struct iwl_alive_resp *palive;
486
487 palive = &pkt->u.alive_frame;
488
489 IWL_DEBUG_FW(trans, "Alive ucode status 0x%08X revision "
490 "0x%01X 0x%01X\n",
491 palive->is_valid, palive->ver_type,
492 palive->ver_subtype);
493
494 trans->shrd->device_pointers.error_event_table =
495 le32_to_cpu(palive->error_event_table_ptr);
496 trans->shrd->device_pointers.log_event_table =
497 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
503 /* notification wait support */
504 void iwl_init_notification_wait(struct iwl_shared *shrd,
505 struct iwl_notification_wait *wait_entry,
506 u8 cmd,
507 void (*fn)(struct iwl_trans *trans,
508 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
523 int 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
546 void 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
554 void 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
567 #define UCODE_ALIVE_TIMEOUT HZ
568 #define UCODE_CALIB_TIMEOUT (2*HZ)
569
570 int iwl_load_ucode_wait_alive(struct iwl_trans *trans,
571 enum iwl_ucode_type ucode_type)
572 {
573 struct iwl_notification_wait alive_wait;
574 struct iwl_alive_data alive_data;
575 struct fw_img *fw;
576 int ret;
577 enum iwl_ucode_type old_type;
578
579 iwl_init_notification_wait(trans->shrd, &alive_wait, REPLY_ALIVE,
580 iwl_alive_fn, &alive_data);
581
582 old_type = trans->shrd->ucode_type;
583 trans->shrd->ucode_type = ucode_type;
584 fw = iwl_get_ucode_image(trans, ucode_type);
585
586 if (!fw)
587 return -EINVAL;
588
589 ret = iwl_trans_start_fw(trans, fw);
590 if (ret) {
591 trans->shrd->ucode_type = old_type;
592 iwl_remove_notification(trans->shrd, &alive_wait);
593 return ret;
594 }
595
596 /*
597 * Some things may run in the background now, but we
598 * just wait for the ALIVE notification here.
599 */
600 ret = iwl_wait_notification(trans->shrd, &alive_wait,
601 UCODE_ALIVE_TIMEOUT);
602 if (ret) {
603 trans->shrd->ucode_type = old_type;
604 return ret;
605 }
606
607 if (!alive_data.valid) {
608 IWL_ERR(trans, "Loaded ucode is not valid!\n");
609 trans->shrd->ucode_type = old_type;
610 return -EIO;
611 }
612
613 /*
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) {
619 ret = iwl_verify_ucode(trans, ucode_type);
620 if (ret) {
621 trans->shrd->ucode_type = old_type;
622 return ret;
623 }
624
625 /* delay a bit to give rfkill time to run */
626 msleep(5);
627 }
628
629 ret = iwl_alive_notify(trans);
630 if (ret) {
631 IWL_WARN(trans,
632 "Could not complete ALIVE transition: %d\n", ret);
633 trans->shrd->ucode_type = old_type;
634 return ret;
635 }
636
637 return 0;
638 }
639
640 int iwl_run_init_ucode(struct iwl_trans *trans)
641 {
642 struct iwl_notification_wait calib_wait;
643 int ret;
644
645 lockdep_assert_held(&trans->shrd->mutex);
646
647 /* No init ucode required? Curious, but maybe ok */
648 if (!trans->ucode_init.code.len)
649 return 0;
650
651 if (trans->shrd->ucode_type != IWL_UCODE_NONE)
652 return 0;
653
654 iwl_init_notification_wait(trans->shrd, &calib_wait,
655 CALIBRATION_COMPLETE_NOTIFICATION,
656 NULL, NULL);
657
658 /* Will also start the device */
659 ret = iwl_load_ucode_wait_alive(trans, IWL_UCODE_INIT);
660 if (ret)
661 goto error;
662
663 ret = iwl_init_alive_start(trans);
664 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 */
671 ret = iwl_wait_notification(trans->shrd, &calib_wait,
672 UCODE_CALIB_TIMEOUT);
673
674 goto out;
675
676 error:
677 iwl_remove_notification(trans->shrd, &calib_wait);
678 out:
679 /* Whatever happened, stop the device */
680 iwl_trans_stop_device(trans);
681 return ret;
682 }
This page took 0.055155 seconds and 6 git commands to generate.