iwl-ucode.c 16 KB

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