f78b1552ef7c6b9cc8f05e9db53b6e781ccfea43
[pandora-kernel.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 #include <linux/sched.h>
33
34 #include "iwl-wifi.h"
35 #include "iwl-dev.h"
36 #include "iwl-core.h"
37 #include "iwl-io.h"
38 #include "iwl-agn-hw.h"
39 #include "iwl-agn.h"
40 #include "iwl-agn-calib.h"
41 #include "iwl-trans.h"
42 #include "iwl-fh.h"
43 #include "iwl-op-mode.h"
44
45 static struct iwl_wimax_coex_event_entry cu_priorities[COEX_NUM_OF_EVENTS] = {
46         {COEX_CU_UNASSOC_IDLE_RP, COEX_CU_UNASSOC_IDLE_WP,
47          0, COEX_UNASSOC_IDLE_FLAGS},
48         {COEX_CU_UNASSOC_MANUAL_SCAN_RP, COEX_CU_UNASSOC_MANUAL_SCAN_WP,
49          0, COEX_UNASSOC_MANUAL_SCAN_FLAGS},
50         {COEX_CU_UNASSOC_AUTO_SCAN_RP, COEX_CU_UNASSOC_AUTO_SCAN_WP,
51          0, COEX_UNASSOC_AUTO_SCAN_FLAGS},
52         {COEX_CU_CALIBRATION_RP, COEX_CU_CALIBRATION_WP,
53          0, COEX_CALIBRATION_FLAGS},
54         {COEX_CU_PERIODIC_CALIBRATION_RP, COEX_CU_PERIODIC_CALIBRATION_WP,
55          0, COEX_PERIODIC_CALIBRATION_FLAGS},
56         {COEX_CU_CONNECTION_ESTAB_RP, COEX_CU_CONNECTION_ESTAB_WP,
57          0, COEX_CONNECTION_ESTAB_FLAGS},
58         {COEX_CU_ASSOCIATED_IDLE_RP, COEX_CU_ASSOCIATED_IDLE_WP,
59          0, COEX_ASSOCIATED_IDLE_FLAGS},
60         {COEX_CU_ASSOC_MANUAL_SCAN_RP, COEX_CU_ASSOC_MANUAL_SCAN_WP,
61          0, COEX_ASSOC_MANUAL_SCAN_FLAGS},
62         {COEX_CU_ASSOC_AUTO_SCAN_RP, COEX_CU_ASSOC_AUTO_SCAN_WP,
63          0, COEX_ASSOC_AUTO_SCAN_FLAGS},
64         {COEX_CU_ASSOC_ACTIVE_LEVEL_RP, COEX_CU_ASSOC_ACTIVE_LEVEL_WP,
65          0, COEX_ASSOC_ACTIVE_LEVEL_FLAGS},
66         {COEX_CU_RF_ON_RP, COEX_CU_RF_ON_WP, 0, COEX_CU_RF_ON_FLAGS},
67         {COEX_CU_RF_OFF_RP, COEX_CU_RF_OFF_WP, 0, COEX_RF_OFF_FLAGS},
68         {COEX_CU_STAND_ALONE_DEBUG_RP, COEX_CU_STAND_ALONE_DEBUG_WP,
69          0, COEX_STAND_ALONE_DEBUG_FLAGS},
70         {COEX_CU_IPAN_ASSOC_LEVEL_RP, COEX_CU_IPAN_ASSOC_LEVEL_WP,
71          0, COEX_IPAN_ASSOC_LEVEL_FLAGS},
72         {COEX_CU_RSRVD1_RP, COEX_CU_RSRVD1_WP, 0, COEX_RSRVD1_FLAGS},
73         {COEX_CU_RSRVD2_RP, COEX_CU_RSRVD2_WP, 0, COEX_RSRVD2_FLAGS}
74 };
75
76 /******************************************************************************
77  *
78  * uCode download functions
79  *
80  ******************************************************************************/
81
82 static inline const struct fw_img *
83 iwl_get_ucode_image(struct iwl_priv *priv, enum iwl_ucode_type ucode_type)
84 {
85         switch (ucode_type) {
86         case IWL_UCODE_INIT:
87                 return &priv->fw->ucode_init;
88         case IWL_UCODE_WOWLAN:
89                 return &priv->fw->ucode_wowlan;
90         case IWL_UCODE_REGULAR:
91                 return &priv->fw->ucode_rt;
92         case IWL_UCODE_NONE:
93                 break;
94         }
95         return NULL;
96 }
97
98 /*
99  *  Calibration
100  */
101 static int iwl_set_Xtal_calib(struct iwl_priv *priv)
102 {
103         struct iwl_calib_xtal_freq_cmd cmd;
104         __le16 *xtal_calib =
105                 (__le16 *)iwl_eeprom_query_addr(priv->shrd, EEPROM_XTAL);
106
107         iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
108         cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
109         cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
110         return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
111 }
112
113 static int iwl_set_temperature_offset_calib(struct iwl_priv *priv)
114 {
115         struct iwl_calib_temperature_offset_cmd cmd;
116         __le16 *offset_calib =
117                 (__le16 *)iwl_eeprom_query_addr(priv->shrd,
118                                                 EEPROM_RAW_TEMPERATURE);
119
120         memset(&cmd, 0, sizeof(cmd));
121         iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
122         memcpy(&cmd.radio_sensor_offset, offset_calib, sizeof(*offset_calib));
123         if (!(cmd.radio_sensor_offset))
124                 cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
125
126         IWL_DEBUG_CALIB(priv, "Radio sensor offset: %d\n",
127                         le16_to_cpu(cmd.radio_sensor_offset));
128         return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
129 }
130
131 static int iwl_set_temperature_offset_calib_v2(struct iwl_priv *priv)
132 {
133         struct iwl_calib_temperature_offset_v2_cmd cmd;
134         __le16 *offset_calib_high = (__le16 *)iwl_eeprom_query_addr(priv->shrd,
135                                      EEPROM_KELVIN_TEMPERATURE);
136         __le16 *offset_calib_low =
137                 (__le16 *)iwl_eeprom_query_addr(priv->shrd,
138                                                 EEPROM_RAW_TEMPERATURE);
139         struct iwl_eeprom_calib_hdr *hdr;
140
141         memset(&cmd, 0, sizeof(cmd));
142         iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
143         hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv->shrd,
144                                                         EEPROM_CALIB_ALL);
145         memcpy(&cmd.radio_sensor_offset_high, offset_calib_high,
146                 sizeof(*offset_calib_high));
147         memcpy(&cmd.radio_sensor_offset_low, offset_calib_low,
148                 sizeof(*offset_calib_low));
149         if (!(cmd.radio_sensor_offset_low)) {
150                 IWL_DEBUG_CALIB(priv, "no info in EEPROM, use default\n");
151                 cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
152                 cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
153         }
154         memcpy(&cmd.burntVoltageRef, &hdr->voltage,
155                 sizeof(hdr->voltage));
156
157         IWL_DEBUG_CALIB(priv, "Radio sensor offset high: %d\n",
158                         le16_to_cpu(cmd.radio_sensor_offset_high));
159         IWL_DEBUG_CALIB(priv, "Radio sensor offset low: %d\n",
160                         le16_to_cpu(cmd.radio_sensor_offset_low));
161         IWL_DEBUG_CALIB(priv, "Voltage Ref: %d\n",
162                         le16_to_cpu(cmd.burntVoltageRef));
163
164         return iwl_calib_set(priv, (void *)&cmd, sizeof(cmd));
165 }
166
167 static int iwl_send_calib_cfg(struct iwl_priv *priv)
168 {
169         struct iwl_calib_cfg_cmd calib_cfg_cmd;
170         struct iwl_host_cmd cmd = {
171                 .id = CALIBRATION_CFG_CMD,
172                 .len = { sizeof(struct iwl_calib_cfg_cmd), },
173                 .data = { &calib_cfg_cmd, },
174         };
175
176         memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
177         calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
178         calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
179         calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
180         calib_cfg_cmd.ucd_calib_cfg.flags =
181                 IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
182
183         return iwl_trans_send_cmd(trans(priv), &cmd);
184 }
185
186 int iwlagn_rx_calib_result(struct iwl_priv *priv,
187                             struct iwl_rx_cmd_buffer *rxb,
188                             struct iwl_device_cmd *cmd)
189 {
190         struct iwl_rx_packet *pkt = rxb_addr(rxb);
191         struct iwl_calib_hdr *hdr = (struct iwl_calib_hdr *)pkt->u.raw;
192         int len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
193
194         /* reduce the size of the length field itself */
195         len -= 4;
196
197         if (iwl_calib_set(priv, hdr, len))
198                 IWL_ERR(priv, "Failed to record calibration data %d\n",
199                         hdr->op_code);
200
201         return 0;
202 }
203
204 int iwl_init_alive_start(struct iwl_priv *priv)
205 {
206         int ret;
207
208         if (cfg(priv)->bt_params &&
209             cfg(priv)->bt_params->advanced_bt_coexist) {
210                 /*
211                  * Tell uCode we are ready to perform calibration
212                  * need to perform this before any calibration
213                  * no need to close the envlope since we are going
214                  * to load the runtime uCode later.
215                  */
216                 ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
217                         BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
218                 if (ret)
219                         return ret;
220
221         }
222
223         ret = iwl_send_calib_cfg(priv);
224         if (ret)
225                 return ret;
226
227         /**
228          * temperature offset calibration is only needed for runtime ucode,
229          * so prepare the value now.
230          */
231         if (cfg(priv)->need_temp_offset_calib) {
232                 if (cfg(priv)->temp_offset_v2)
233                         return iwl_set_temperature_offset_calib_v2(priv);
234                 else
235                         return iwl_set_temperature_offset_calib(priv);
236         }
237
238         return 0;
239 }
240
241 static int iwl_send_wimax_coex(struct iwl_priv *priv)
242 {
243         struct iwl_wimax_coex_cmd coex_cmd;
244
245         if (cfg(priv)->base_params->support_wimax_coexist) {
246                 /* UnMask wake up src at associated sleep */
247                 coex_cmd.flags = COEX_FLAGS_ASSOC_WA_UNMASK_MSK;
248
249                 /* UnMask wake up src at unassociated sleep */
250                 coex_cmd.flags |= COEX_FLAGS_UNASSOC_WA_UNMASK_MSK;
251                 memcpy(coex_cmd.sta_prio, cu_priorities,
252                         sizeof(struct iwl_wimax_coex_event_entry) *
253                          COEX_NUM_OF_EVENTS);
254
255                 /* enabling the coexistence feature */
256                 coex_cmd.flags |= COEX_FLAGS_COEX_ENABLE_MSK;
257
258                 /* enabling the priorities tables */
259                 coex_cmd.flags |= COEX_FLAGS_STA_TABLE_VALID_MSK;
260         } else {
261                 /* coexistence is disabled */
262                 memset(&coex_cmd, 0, sizeof(coex_cmd));
263         }
264         return iwl_trans_send_cmd_pdu(trans(priv),
265                                 COEX_PRIORITY_TABLE_CMD, CMD_SYNC,
266                                 sizeof(coex_cmd), &coex_cmd);
267 }
268
269 static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
270         ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
271                 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
272         ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
273                 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
274         ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
275                 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
276         ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
277                 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
278         ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
279                 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
280         ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
281                 (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
282         ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
283                 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
284         ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
285                 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
286         ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
287                 (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
288         0, 0, 0, 0, 0, 0, 0
289 };
290
291 void iwl_send_prio_tbl(struct iwl_priv *priv)
292 {
293         struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
294
295         memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
296                 sizeof(iwl_bt_prio_tbl));
297         if (iwl_trans_send_cmd_pdu(trans(priv),
298                                 REPLY_BT_COEX_PRIO_TABLE, CMD_SYNC,
299                                 sizeof(prio_tbl_cmd), &prio_tbl_cmd))
300                 IWL_ERR(priv, "failed to send BT prio tbl command\n");
301 }
302
303 int iwl_send_bt_env(struct iwl_priv *priv, u8 action, u8 type)
304 {
305         struct iwl_bt_coex_prot_env_cmd env_cmd;
306         int ret;
307
308         env_cmd.action = action;
309         env_cmd.type = type;
310         ret = iwl_trans_send_cmd_pdu(trans(priv),
311                                REPLY_BT_COEX_PROT_ENV, CMD_SYNC,
312                                sizeof(env_cmd), &env_cmd);
313         if (ret)
314                 IWL_ERR(priv, "failed to send BT env command\n");
315         return ret;
316 }
317
318
319 static int iwl_alive_notify(struct iwl_priv *priv)
320 {
321         struct iwl_rxon_context *ctx;
322         int ret;
323
324         if (!priv->tx_cmd_pool)
325                 priv->tx_cmd_pool =
326                         kmem_cache_create("iwl_dev_cmd",
327                                           sizeof(struct iwl_device_cmd),
328                                           sizeof(void *), 0, NULL);
329
330         if (!priv->tx_cmd_pool)
331                 return -ENOMEM;
332
333         iwl_trans_fw_alive(trans(priv));
334         for_each_context(priv, ctx)
335                 ctx->last_tx_rejected = false;
336
337         ret = iwl_send_wimax_coex(priv);
338         if (ret)
339                 return ret;
340
341         if (!cfg(priv)->no_xtal_calib) {
342                 ret = iwl_set_Xtal_calib(priv);
343                 if (ret)
344                         return ret;
345         }
346
347         return iwl_send_calib_results(priv);
348 }
349
350
351 /**
352  * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
353  *   using sample data 100 bytes apart.  If these sample points are good,
354  *   it's a pretty good bet that everything between them is good, too.
355  */
356 static int iwl_verify_inst_sparse(struct iwl_priv *priv,
357                                   const struct fw_desc *fw_desc)
358 {
359         __le32 *image = (__le32 *)fw_desc->v_addr;
360         u32 len = fw_desc->len;
361         u32 val;
362         u32 i;
363
364         IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
365
366         for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
367                 /* read data comes through single port, auto-incr addr */
368                 /* NOTE: Use the debugless read so we don't flood kernel log
369                  * if IWL_DL_IO is set */
370                 iwl_write_direct32(trans(priv), HBUS_TARG_MEM_RADDR,
371                         i + IWLAGN_RTC_INST_LOWER_BOUND);
372                 val = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
373                 if (val != le32_to_cpu(*image))
374                         return -EIO;
375         }
376
377         return 0;
378 }
379
380 static void iwl_print_mismatch_inst(struct iwl_priv *priv,
381                                     const struct fw_desc *fw_desc)
382 {
383         __le32 *image = (__le32 *)fw_desc->v_addr;
384         u32 len = fw_desc->len;
385         u32 val;
386         u32 offs;
387         int errors = 0;
388
389         IWL_DEBUG_FW(priv, "ucode inst image size is %u\n", len);
390
391         iwl_write_direct32(trans(priv), HBUS_TARG_MEM_RADDR,
392                            IWLAGN_RTC_INST_LOWER_BOUND);
393
394         for (offs = 0;
395              offs < len && errors < 20;
396              offs += sizeof(u32), image++) {
397                 /* read data comes through single port, auto-incr addr */
398                 val = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
399                 if (val != le32_to_cpu(*image)) {
400                         IWL_ERR(priv, "uCode INST section at "
401                                 "offset 0x%x, is 0x%x, s/b 0x%x\n",
402                                 offs, val, le32_to_cpu(*image));
403                         errors++;
404                 }
405         }
406 }
407
408 /**
409  * iwl_verify_ucode - determine which instruction image is in SRAM,
410  *    and verify its contents
411  */
412 static int iwl_verify_ucode(struct iwl_priv *priv,
413                             enum iwl_ucode_type ucode_type)
414 {
415         const struct fw_img *img = iwl_get_ucode_image(priv, ucode_type);
416
417         if (!img) {
418                 IWL_ERR(priv, "Invalid ucode requested (%d)\n", ucode_type);
419                 return -EINVAL;
420         }
421
422         if (!iwl_verify_inst_sparse(priv, &img->code)) {
423                 IWL_DEBUG_FW(priv, "uCode is good in inst SRAM\n");
424                 return 0;
425         }
426
427         IWL_ERR(priv, "UCODE IMAGE IN INSTRUCTION SRAM NOT VALID!!\n");
428
429         iwl_print_mismatch_inst(priv, &img->code);
430         return -EIO;
431 }
432
433 struct iwl_alive_data {
434         bool valid;
435         u8 subtype;
436 };
437
438 static void iwl_alive_fn(struct iwl_priv *priv,
439                             struct iwl_rx_packet *pkt,
440                             void *data)
441 {
442         struct iwl_alive_data *alive_data = data;
443         struct iwl_alive_resp *palive;
444
445         palive = &pkt->u.alive_frame;
446
447         IWL_DEBUG_FW(priv, "Alive ucode status 0x%08X revision "
448                        "0x%01X 0x%01X\n",
449                        palive->is_valid, palive->ver_type,
450                        palive->ver_subtype);
451
452         priv->shrd->device_pointers.error_event_table =
453                 le32_to_cpu(palive->error_event_table_ptr);
454         priv->shrd->device_pointers.log_event_table =
455                 le32_to_cpu(palive->log_event_table_ptr);
456
457         alive_data->subtype = palive->ver_subtype;
458         alive_data->valid = palive->is_valid == UCODE_VALID_OK;
459 }
460
461 /* notification wait support */
462 void iwl_init_notification_wait(struct iwl_shared *shrd,
463                                 struct iwl_notification_wait *wait_entry,
464                                 u8 cmd,
465                                 void (*fn)(struct iwl_priv *priv,
466                                            struct iwl_rx_packet *pkt,
467                                            void *data),
468                                 void *fn_data)
469 {
470         wait_entry->fn = fn;
471         wait_entry->fn_data = fn_data;
472         wait_entry->cmd = cmd;
473         wait_entry->triggered = false;
474         wait_entry->aborted = false;
475
476         spin_lock_bh(&shrd->notif_wait_lock);
477         list_add(&wait_entry->list, &shrd->notif_waits);
478         spin_unlock_bh(&shrd->notif_wait_lock);
479 }
480
481 int iwl_wait_notification(struct iwl_shared *shrd,
482                              struct iwl_notification_wait *wait_entry,
483                              unsigned long timeout)
484 {
485         int ret;
486
487         ret = wait_event_timeout(shrd->notif_waitq,
488                                  wait_entry->triggered || wait_entry->aborted,
489                                  timeout);
490
491         spin_lock_bh(&shrd->notif_wait_lock);
492         list_del(&wait_entry->list);
493         spin_unlock_bh(&shrd->notif_wait_lock);
494
495         if (wait_entry->aborted)
496                 return -EIO;
497
498         /* return value is always >= 0 */
499         if (ret <= 0)
500                 return -ETIMEDOUT;
501         return 0;
502 }
503
504 void iwl_remove_notification(struct iwl_shared *shrd,
505                                 struct iwl_notification_wait *wait_entry)
506 {
507         spin_lock_bh(&shrd->notif_wait_lock);
508         list_del(&wait_entry->list);
509         spin_unlock_bh(&shrd->notif_wait_lock);
510 }
511
512 void iwl_abort_notification_waits(struct iwl_shared *shrd)
513 {
514         unsigned long flags;
515         struct iwl_notification_wait *wait_entry;
516
517         spin_lock_irqsave(&shrd->notif_wait_lock, flags);
518         list_for_each_entry(wait_entry, &shrd->notif_waits, list)
519                 wait_entry->aborted = true;
520         spin_unlock_irqrestore(&shrd->notif_wait_lock, flags);
521
522         wake_up_all(&shrd->notif_waitq);
523 }
524
525 #define UCODE_ALIVE_TIMEOUT     HZ
526 #define UCODE_CALIB_TIMEOUT     (2*HZ)
527
528 int iwl_load_ucode_wait_alive(struct iwl_priv *priv,
529                                  enum iwl_ucode_type ucode_type)
530 {
531         struct iwl_notification_wait alive_wait;
532         struct iwl_alive_data alive_data;
533         const struct fw_img *fw;
534         int ret;
535         enum iwl_ucode_type old_type;
536
537         iwl_init_notification_wait(priv->shrd, &alive_wait, REPLY_ALIVE,
538                                       iwl_alive_fn, &alive_data);
539
540         old_type = priv->shrd->ucode_type;
541         priv->shrd->ucode_type = ucode_type;
542         fw = iwl_get_ucode_image(priv, ucode_type);
543
544         if (!fw)
545                 return -EINVAL;
546
547         ret = iwl_trans_start_fw(trans(priv), fw);
548         if (ret) {
549                 priv->shrd->ucode_type = old_type;
550                 iwl_remove_notification(priv->shrd, &alive_wait);
551                 return ret;
552         }
553
554         /*
555          * Some things may run in the background now, but we
556          * just wait for the ALIVE notification here.
557          */
558         ret = iwl_wait_notification(priv->shrd, &alive_wait,
559                                         UCODE_ALIVE_TIMEOUT);
560         if (ret) {
561                 priv->shrd->ucode_type = old_type;
562                 return ret;
563         }
564
565         if (!alive_data.valid) {
566                 IWL_ERR(priv, "Loaded ucode is not valid!\n");
567                 priv->shrd->ucode_type = old_type;
568                 return -EIO;
569         }
570
571         /*
572          * This step takes a long time (60-80ms!!) and
573          * WoWLAN image should be loaded quickly, so
574          * skip it for WoWLAN.
575          */
576         if (ucode_type != IWL_UCODE_WOWLAN) {
577                 ret = iwl_verify_ucode(priv, ucode_type);
578                 if (ret) {
579                         priv->shrd->ucode_type = old_type;
580                         return ret;
581                 }
582
583                 /* delay a bit to give rfkill time to run */
584                 msleep(5);
585         }
586
587         ret = iwl_alive_notify(priv);
588         if (ret) {
589                 IWL_WARN(priv,
590                         "Could not complete ALIVE transition: %d\n", ret);
591                 priv->shrd->ucode_type = old_type;
592                 return ret;
593         }
594
595         return 0;
596 }
597
598 int iwl_run_init_ucode(struct iwl_priv *priv)
599 {
600         struct iwl_notification_wait calib_wait;
601         int ret;
602
603         lockdep_assert_held(&priv->shrd->mutex);
604
605         /* No init ucode required? Curious, but maybe ok */
606         if (!priv->fw->ucode_init.code.len)
607                 return 0;
608
609         if (priv->shrd->ucode_type != IWL_UCODE_NONE)
610                 return 0;
611
612         iwl_init_notification_wait(priv->shrd, &calib_wait,
613                                       CALIBRATION_COMPLETE_NOTIFICATION,
614                                       NULL, NULL);
615
616         /* Will also start the device */
617         ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_INIT);
618         if (ret)
619                 goto error;
620
621         ret = iwl_init_alive_start(priv);
622         if (ret)
623                 goto error;
624
625         /*
626          * Some things may run in the background now, but we
627          * just wait for the calibration complete notification.
628          */
629         ret = iwl_wait_notification(priv->shrd, &calib_wait,
630                                         UCODE_CALIB_TIMEOUT);
631
632         goto out;
633
634  error:
635         iwl_remove_notification(priv->shrd, &calib_wait);
636  out:
637         /* Whatever happened, stop the device */
638         iwl_trans_stop_device(trans(priv));
639         return ret;
640 }