iwl-ucode.c 18 KB

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