iwl-ucode.c 16 KB

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