iwl-ucode.c 18 KB

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