iwl-ucode.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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/module.h>
  31. #include <linux/init.h>
  32. #include <linux/sched.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/firmware.h>
  35. #include "iwl-ucode.h"
  36. #include "iwl-wifi.h"
  37. #include "iwl-dev.h"
  38. #include "iwl-core.h"
  39. #include "iwl-io.h"
  40. #include "iwl-agn-hw.h"
  41. #include "iwl-agn.h"
  42. #include "iwl-agn-calib.h"
  43. #include "iwl-trans.h"
  44. #include "iwl-fh.h"
  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. * uCode download functions
  78. *
  79. ******************************************************************************/
  80. static void iwl_free_fw_desc(struct iwl_nic *nic, struct fw_desc *desc)
  81. {
  82. if (desc->v_addr)
  83. dma_free_coherent(trans(nic)->dev, desc->len,
  84. desc->v_addr, desc->p_addr);
  85. desc->v_addr = NULL;
  86. desc->len = 0;
  87. }
  88. static void iwl_free_fw_img(struct iwl_nic *nic, struct fw_img *img)
  89. {
  90. iwl_free_fw_desc(nic, &img->code);
  91. iwl_free_fw_desc(nic, &img->data);
  92. }
  93. void iwl_dealloc_ucode(struct iwl_nic *nic)
  94. {
  95. iwl_free_fw_img(nic, &nic->fw.ucode_rt);
  96. iwl_free_fw_img(nic, &nic->fw.ucode_init);
  97. iwl_free_fw_img(nic, &nic->fw.ucode_wowlan);
  98. }
  99. static int iwl_alloc_fw_desc(struct iwl_nic *nic, struct fw_desc *desc,
  100. const void *data, size_t len)
  101. {
  102. if (!len) {
  103. desc->v_addr = NULL;
  104. return -EINVAL;
  105. }
  106. desc->v_addr = dma_alloc_coherent(trans(nic)->dev, len,
  107. &desc->p_addr, GFP_KERNEL);
  108. if (!desc->v_addr)
  109. return -ENOMEM;
  110. desc->len = len;
  111. memcpy(desc->v_addr, data, len);
  112. return 0;
  113. }
  114. static inline struct fw_img *iwl_get_ucode_image(struct iwl_nic *nic,
  115. enum iwl_ucode_type ucode_type)
  116. {
  117. switch (ucode_type) {
  118. case IWL_UCODE_INIT:
  119. return &nic->fw.ucode_init;
  120. case IWL_UCODE_WOWLAN:
  121. return &nic->fw.ucode_wowlan;
  122. case IWL_UCODE_REGULAR:
  123. return &nic->fw.ucode_rt;
  124. case IWL_UCODE_NONE:
  125. break;
  126. }
  127. return NULL;
  128. }
  129. /*
  130. * Calibration
  131. */
  132. static int iwl_set_Xtal_calib(struct iwl_trans *trans)
  133. {
  134. struct iwl_calib_xtal_freq_cmd cmd;
  135. __le16 *xtal_calib =
  136. (__le16 *)iwl_eeprom_query_addr(trans->shrd, EEPROM_XTAL);
  137. iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD);
  138. cmd.cap_pin1 = le16_to_cpu(xtal_calib[0]);
  139. cmd.cap_pin2 = le16_to_cpu(xtal_calib[1]);
  140. return iwl_calib_set(trans, (void *)&cmd, sizeof(cmd));
  141. }
  142. static int iwl_set_temperature_offset_calib(struct iwl_trans *trans)
  143. {
  144. struct iwl_calib_temperature_offset_cmd cmd;
  145. __le16 *offset_calib =
  146. (__le16 *)iwl_eeprom_query_addr(trans->shrd,
  147. EEPROM_RAW_TEMPERATURE);
  148. memset(&cmd, 0, sizeof(cmd));
  149. iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
  150. memcpy(&cmd.radio_sensor_offset, offset_calib, sizeof(*offset_calib));
  151. if (!(cmd.radio_sensor_offset))
  152. cmd.radio_sensor_offset = DEFAULT_RADIO_SENSOR_OFFSET;
  153. IWL_DEBUG_CALIB(trans, "Radio sensor offset: %d\n",
  154. le16_to_cpu(cmd.radio_sensor_offset));
  155. return iwl_calib_set(trans, (void *)&cmd, sizeof(cmd));
  156. }
  157. static int iwl_set_temperature_offset_calib_v2(struct iwl_trans *trans)
  158. {
  159. struct iwl_calib_temperature_offset_v2_cmd cmd;
  160. __le16 *offset_calib_high = (__le16 *)iwl_eeprom_query_addr(trans->shrd,
  161. EEPROM_KELVIN_TEMPERATURE);
  162. __le16 *offset_calib_low =
  163. (__le16 *)iwl_eeprom_query_addr(trans->shrd,
  164. EEPROM_RAW_TEMPERATURE);
  165. struct iwl_eeprom_calib_hdr *hdr;
  166. memset(&cmd, 0, sizeof(cmd));
  167. iwl_set_calib_hdr(&cmd.hdr, IWL_PHY_CALIBRATE_TEMP_OFFSET_CMD);
  168. hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(trans->shrd,
  169. EEPROM_CALIB_ALL);
  170. memcpy(&cmd.radio_sensor_offset_high, offset_calib_high,
  171. sizeof(*offset_calib_high));
  172. memcpy(&cmd.radio_sensor_offset_low, offset_calib_low,
  173. sizeof(*offset_calib_low));
  174. if (!(cmd.radio_sensor_offset_low)) {
  175. IWL_DEBUG_CALIB(trans, "no info in EEPROM, use default\n");
  176. cmd.radio_sensor_offset_low = DEFAULT_RADIO_SENSOR_OFFSET;
  177. cmd.radio_sensor_offset_high = DEFAULT_RADIO_SENSOR_OFFSET;
  178. }
  179. memcpy(&cmd.burntVoltageRef, &hdr->voltage,
  180. sizeof(hdr->voltage));
  181. IWL_DEBUG_CALIB(trans, "Radio sensor offset high: %d\n",
  182. le16_to_cpu(cmd.radio_sensor_offset_high));
  183. IWL_DEBUG_CALIB(trans, "Radio sensor offset low: %d\n",
  184. le16_to_cpu(cmd.radio_sensor_offset_low));
  185. IWL_DEBUG_CALIB(trans, "Voltage Ref: %d\n",
  186. le16_to_cpu(cmd.burntVoltageRef));
  187. return iwl_calib_set(trans, (void *)&cmd, sizeof(cmd));
  188. }
  189. static int iwl_send_calib_cfg(struct iwl_trans *trans)
  190. {
  191. struct iwl_calib_cfg_cmd calib_cfg_cmd;
  192. struct iwl_host_cmd cmd = {
  193. .id = CALIBRATION_CFG_CMD,
  194. .len = { sizeof(struct iwl_calib_cfg_cmd), },
  195. .data = { &calib_cfg_cmd, },
  196. };
  197. memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
  198. calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
  199. calib_cfg_cmd.ucd_calib_cfg.once.start = IWL_CALIB_INIT_CFG_ALL;
  200. calib_cfg_cmd.ucd_calib_cfg.once.send_res = IWL_CALIB_INIT_CFG_ALL;
  201. calib_cfg_cmd.ucd_calib_cfg.flags =
  202. IWL_CALIB_CFG_FLAG_SEND_COMPLETE_NTFY_MSK;
  203. return iwl_trans_send_cmd(trans, &cmd);
  204. }
  205. int iwlagn_rx_calib_result(struct iwl_priv *priv,
  206. struct iwl_rx_mem_buffer *rxb,
  207. struct iwl_device_cmd *cmd)
  208. {
  209. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  210. struct iwl_calib_hdr *hdr = (struct iwl_calib_hdr *)pkt->u.raw;
  211. int len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  212. /* reduce the size of the length field itself */
  213. len -= 4;
  214. if (iwl_calib_set(trans(priv), hdr, len))
  215. IWL_ERR(priv, "Failed to record calibration data %d\n",
  216. hdr->op_code);
  217. return 0;
  218. }
  219. int iwl_init_alive_start(struct iwl_trans *trans)
  220. {
  221. int ret;
  222. if (cfg(trans)->bt_params &&
  223. cfg(trans)->bt_params->advanced_bt_coexist) {
  224. /*
  225. * Tell uCode we are ready to perform calibration
  226. * need to perform this before any calibration
  227. * no need to close the envlope since we are going
  228. * to load the runtime uCode later.
  229. */
  230. ret = iwl_send_bt_env(trans, IWL_BT_COEX_ENV_OPEN,
  231. BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
  232. if (ret)
  233. return ret;
  234. }
  235. ret = iwl_send_calib_cfg(trans);
  236. if (ret)
  237. return ret;
  238. /**
  239. * temperature offset calibration is only needed for runtime ucode,
  240. * so prepare the value now.
  241. */
  242. if (cfg(trans)->need_temp_offset_calib) {
  243. if (cfg(trans)->temp_offset_v2)
  244. return iwl_set_temperature_offset_calib_v2(trans);
  245. else
  246. return iwl_set_temperature_offset_calib(trans);
  247. }
  248. return 0;
  249. }
  250. static int iwl_send_wimax_coex(struct iwl_trans *trans)
  251. {
  252. struct iwl_wimax_coex_cmd coex_cmd;
  253. if (cfg(trans)->base_params->support_wimax_coexist) {
  254. /* UnMask wake up src at associated sleep */
  255. coex_cmd.flags = COEX_FLAGS_ASSOC_WA_UNMASK_MSK;
  256. /* UnMask wake up src at unassociated sleep */
  257. coex_cmd.flags |= COEX_FLAGS_UNASSOC_WA_UNMASK_MSK;
  258. memcpy(coex_cmd.sta_prio, cu_priorities,
  259. sizeof(struct iwl_wimax_coex_event_entry) *
  260. COEX_NUM_OF_EVENTS);
  261. /* enabling the coexistence feature */
  262. coex_cmd.flags |= COEX_FLAGS_COEX_ENABLE_MSK;
  263. /* enabling the priorities tables */
  264. coex_cmd.flags |= COEX_FLAGS_STA_TABLE_VALID_MSK;
  265. } else {
  266. /* coexistence is disabled */
  267. memset(&coex_cmd, 0, sizeof(coex_cmd));
  268. }
  269. return iwl_trans_send_cmd_pdu(trans,
  270. COEX_PRIORITY_TABLE_CMD, CMD_SYNC,
  271. sizeof(coex_cmd), &coex_cmd);
  272. }
  273. static const u8 iwl_bt_prio_tbl[BT_COEX_PRIO_TBL_EVT_MAX] = {
  274. ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  275. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  276. ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  277. (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  278. ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  279. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  280. ((BT_COEX_PRIO_TBL_PRIO_LOW << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  281. (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  282. ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  283. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  284. ((BT_COEX_PRIO_TBL_PRIO_HIGH << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  285. (1 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  286. ((BT_COEX_PRIO_TBL_PRIO_BYPASS << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  287. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  288. ((BT_COEX_PRIO_TBL_PRIO_COEX_OFF << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  289. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  290. ((BT_COEX_PRIO_TBL_PRIO_COEX_ON << IWL_BT_COEX_PRIO_TBL_PRIO_POS) |
  291. (0 << IWL_BT_COEX_PRIO_TBL_SHARED_ANTENNA_POS)),
  292. 0, 0, 0, 0, 0, 0, 0
  293. };
  294. void iwl_send_prio_tbl(struct iwl_trans *trans)
  295. {
  296. struct iwl_bt_coex_prio_table_cmd prio_tbl_cmd;
  297. memcpy(prio_tbl_cmd.prio_tbl, iwl_bt_prio_tbl,
  298. sizeof(iwl_bt_prio_tbl));
  299. if (iwl_trans_send_cmd_pdu(trans,
  300. REPLY_BT_COEX_PRIO_TABLE, CMD_SYNC,
  301. sizeof(prio_tbl_cmd), &prio_tbl_cmd))
  302. IWL_ERR(trans, "failed to send BT prio tbl command\n");
  303. }
  304. int iwl_send_bt_env(struct iwl_trans *trans, u8 action, u8 type)
  305. {
  306. struct iwl_bt_coex_prot_env_cmd env_cmd;
  307. int ret;
  308. env_cmd.action = action;
  309. env_cmd.type = type;
  310. ret = iwl_trans_send_cmd_pdu(trans,
  311. REPLY_BT_COEX_PROT_ENV, CMD_SYNC,
  312. sizeof(env_cmd), &env_cmd);
  313. if (ret)
  314. IWL_ERR(trans, "failed to send BT env command\n");
  315. return ret;
  316. }
  317. static int iwl_alive_notify(struct iwl_trans *trans)
  318. {
  319. struct iwl_priv *priv = priv(trans);
  320. struct iwl_rxon_context *ctx;
  321. int ret;
  322. if (!priv->tx_cmd_pool)
  323. priv->tx_cmd_pool =
  324. kmem_cache_create("iwl_dev_cmd",
  325. sizeof(struct iwl_device_cmd),
  326. sizeof(void *), 0, NULL);
  327. if (!priv->tx_cmd_pool)
  328. return -ENOMEM;
  329. iwl_trans_fw_alive(trans);
  330. for_each_context(priv, ctx)
  331. ctx->last_tx_rejected = false;
  332. ret = iwl_send_wimax_coex(trans);
  333. if (ret)
  334. return ret;
  335. if (!cfg(priv)->no_xtal_calib) {
  336. ret = iwl_set_Xtal_calib(trans);
  337. if (ret)
  338. return ret;
  339. }
  340. return iwl_send_calib_results(trans);
  341. }
  342. /**
  343. * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
  344. * using sample data 100 bytes apart. If these sample points are good,
  345. * it's a pretty good bet that everything between them is good, too.
  346. */
  347. static int iwl_verify_inst_sparse(struct iwl_nic *nic,
  348. struct fw_desc *fw_desc)
  349. {
  350. struct iwl_trans *trans = trans(nic);
  351. __le32 *image = (__le32 *)fw_desc->v_addr;
  352. u32 len = fw_desc->len;
  353. u32 val;
  354. u32 i;
  355. IWL_DEBUG_FW(nic, "ucode inst image size is %u\n", len);
  356. for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
  357. /* read data comes through single port, auto-incr addr */
  358. /* NOTE: Use the debugless read so we don't flood kernel log
  359. * if IWL_DL_IO is set */
  360. iwl_write_direct32(trans, HBUS_TARG_MEM_RADDR,
  361. i + IWLAGN_RTC_INST_LOWER_BOUND);
  362. val = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
  363. if (val != le32_to_cpu(*image))
  364. return -EIO;
  365. }
  366. return 0;
  367. }
  368. static void iwl_print_mismatch_inst(struct iwl_nic *nic,
  369. struct fw_desc *fw_desc)
  370. {
  371. struct iwl_trans *trans = trans(nic);
  372. __le32 *image = (__le32 *)fw_desc->v_addr;
  373. u32 len = fw_desc->len;
  374. u32 val;
  375. u32 offs;
  376. int errors = 0;
  377. IWL_DEBUG_FW(nic, "ucode inst image size is %u\n", len);
  378. iwl_write_direct32(trans, HBUS_TARG_MEM_RADDR,
  379. IWLAGN_RTC_INST_LOWER_BOUND);
  380. for (offs = 0;
  381. offs < len && errors < 20;
  382. offs += sizeof(u32), image++) {
  383. /* read data comes through single port, auto-incr addr */
  384. val = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
  385. if (val != le32_to_cpu(*image)) {
  386. IWL_ERR(nic, "uCode INST section at "
  387. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  388. offs, val, le32_to_cpu(*image));
  389. errors++;
  390. }
  391. }
  392. }
  393. /**
  394. * iwl_verify_ucode - determine which instruction image is in SRAM,
  395. * and verify its contents
  396. */
  397. static int iwl_verify_ucode(struct iwl_nic *nic,
  398. enum iwl_ucode_type ucode_type)
  399. {
  400. struct fw_img *img = iwl_get_ucode_image(nic, ucode_type);
  401. if (!img) {
  402. IWL_ERR(nic, "Invalid ucode requested (%d)\n", ucode_type);
  403. return -EINVAL;
  404. }
  405. if (!iwl_verify_inst_sparse(nic, &img->code)) {
  406. IWL_DEBUG_FW(nic, "uCode is good in inst SRAM\n");
  407. return 0;
  408. }
  409. IWL_ERR(nic, "UCODE IMAGE IN INSTRUCTION SRAM NOT VALID!!\n");
  410. iwl_print_mismatch_inst(nic, &img->code);
  411. return -EIO;
  412. }
  413. struct iwl_alive_data {
  414. bool valid;
  415. u8 subtype;
  416. };
  417. static void iwl_alive_fn(struct iwl_trans *trans,
  418. struct iwl_rx_packet *pkt,
  419. void *data)
  420. {
  421. struct iwl_alive_data *alive_data = data;
  422. struct iwl_alive_resp *palive;
  423. palive = &pkt->u.alive_frame;
  424. IWL_DEBUG_FW(trans, "Alive ucode status 0x%08X revision "
  425. "0x%01X 0x%01X\n",
  426. palive->is_valid, palive->ver_type,
  427. palive->ver_subtype);
  428. trans->shrd->device_pointers.error_event_table =
  429. le32_to_cpu(palive->error_event_table_ptr);
  430. trans->shrd->device_pointers.log_event_table =
  431. le32_to_cpu(palive->log_event_table_ptr);
  432. alive_data->subtype = palive->ver_subtype;
  433. alive_data->valid = palive->is_valid == UCODE_VALID_OK;
  434. }
  435. /* notification wait support */
  436. void iwl_init_notification_wait(struct iwl_shared *shrd,
  437. struct iwl_notification_wait *wait_entry,
  438. u8 cmd,
  439. void (*fn)(struct iwl_trans *trans,
  440. struct iwl_rx_packet *pkt,
  441. void *data),
  442. void *fn_data)
  443. {
  444. wait_entry->fn = fn;
  445. wait_entry->fn_data = fn_data;
  446. wait_entry->cmd = cmd;
  447. wait_entry->triggered = false;
  448. wait_entry->aborted = false;
  449. spin_lock_bh(&shrd->notif_wait_lock);
  450. list_add(&wait_entry->list, &shrd->notif_waits);
  451. spin_unlock_bh(&shrd->notif_wait_lock);
  452. }
  453. int iwl_wait_notification(struct iwl_shared *shrd,
  454. struct iwl_notification_wait *wait_entry,
  455. unsigned long timeout)
  456. {
  457. int ret;
  458. ret = wait_event_timeout(shrd->notif_waitq,
  459. wait_entry->triggered || wait_entry->aborted,
  460. timeout);
  461. spin_lock_bh(&shrd->notif_wait_lock);
  462. list_del(&wait_entry->list);
  463. spin_unlock_bh(&shrd->notif_wait_lock);
  464. if (wait_entry->aborted)
  465. return -EIO;
  466. /* return value is always >= 0 */
  467. if (ret <= 0)
  468. return -ETIMEDOUT;
  469. return 0;
  470. }
  471. void iwl_remove_notification(struct iwl_shared *shrd,
  472. struct iwl_notification_wait *wait_entry)
  473. {
  474. spin_lock_bh(&shrd->notif_wait_lock);
  475. list_del(&wait_entry->list);
  476. spin_unlock_bh(&shrd->notif_wait_lock);
  477. }
  478. void iwl_abort_notification_waits(struct iwl_shared *shrd)
  479. {
  480. unsigned long flags;
  481. struct iwl_notification_wait *wait_entry;
  482. spin_lock_irqsave(&shrd->notif_wait_lock, flags);
  483. list_for_each_entry(wait_entry, &shrd->notif_waits, list)
  484. wait_entry->aborted = true;
  485. spin_unlock_irqrestore(&shrd->notif_wait_lock, flags);
  486. wake_up_all(&shrd->notif_waitq);
  487. }
  488. #define UCODE_ALIVE_TIMEOUT HZ
  489. #define UCODE_CALIB_TIMEOUT (2*HZ)
  490. int iwl_load_ucode_wait_alive(struct iwl_trans *trans,
  491. enum iwl_ucode_type ucode_type)
  492. {
  493. struct iwl_notification_wait alive_wait;
  494. struct iwl_alive_data alive_data;
  495. struct fw_img *fw;
  496. int ret;
  497. enum iwl_ucode_type old_type;
  498. iwl_init_notification_wait(trans->shrd, &alive_wait, REPLY_ALIVE,
  499. iwl_alive_fn, &alive_data);
  500. old_type = trans->shrd->ucode_type;
  501. trans->shrd->ucode_type = ucode_type;
  502. fw = iwl_get_ucode_image(nic(trans), ucode_type);
  503. if (!fw)
  504. return -EINVAL;
  505. ret = iwl_trans_start_fw(trans, fw);
  506. if (ret) {
  507. trans->shrd->ucode_type = old_type;
  508. iwl_remove_notification(trans->shrd, &alive_wait);
  509. return ret;
  510. }
  511. /*
  512. * Some things may run in the background now, but we
  513. * just wait for the ALIVE notification here.
  514. */
  515. ret = iwl_wait_notification(trans->shrd, &alive_wait,
  516. UCODE_ALIVE_TIMEOUT);
  517. if (ret) {
  518. trans->shrd->ucode_type = old_type;
  519. return ret;
  520. }
  521. if (!alive_data.valid) {
  522. IWL_ERR(trans, "Loaded ucode is not valid!\n");
  523. trans->shrd->ucode_type = old_type;
  524. return -EIO;
  525. }
  526. /*
  527. * This step takes a long time (60-80ms!!) and
  528. * WoWLAN image should be loaded quickly, so
  529. * skip it for WoWLAN.
  530. */
  531. if (ucode_type != IWL_UCODE_WOWLAN) {
  532. ret = iwl_verify_ucode(nic(trans), ucode_type);
  533. if (ret) {
  534. trans->shrd->ucode_type = old_type;
  535. return ret;
  536. }
  537. /* delay a bit to give rfkill time to run */
  538. msleep(5);
  539. }
  540. ret = iwl_alive_notify(trans);
  541. if (ret) {
  542. IWL_WARN(trans,
  543. "Could not complete ALIVE transition: %d\n", ret);
  544. trans->shrd->ucode_type = old_type;
  545. return ret;
  546. }
  547. return 0;
  548. }
  549. int iwl_run_init_ucode(struct iwl_trans *trans)
  550. {
  551. struct iwl_notification_wait calib_wait;
  552. int ret;
  553. lockdep_assert_held(&trans->shrd->mutex);
  554. /* No init ucode required? Curious, but maybe ok */
  555. if (!nic(trans)->fw.ucode_init.code.len)
  556. return 0;
  557. if (trans->shrd->ucode_type != IWL_UCODE_NONE)
  558. return 0;
  559. iwl_init_notification_wait(trans->shrd, &calib_wait,
  560. CALIBRATION_COMPLETE_NOTIFICATION,
  561. NULL, NULL);
  562. /* Will also start the device */
  563. ret = iwl_load_ucode_wait_alive(trans, IWL_UCODE_INIT);
  564. if (ret)
  565. goto error;
  566. ret = iwl_init_alive_start(trans);
  567. if (ret)
  568. goto error;
  569. /*
  570. * Some things may run in the background now, but we
  571. * just wait for the calibration complete notification.
  572. */
  573. ret = iwl_wait_notification(trans->shrd, &calib_wait,
  574. UCODE_CALIB_TIMEOUT);
  575. goto out;
  576. error:
  577. iwl_remove_notification(trans->shrd, &calib_wait);
  578. out:
  579. /* Whatever happened, stop the device */
  580. iwl_trans_stop_device(trans);
  581. return ret;
  582. }
  583. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
  584. #define UCODE_EXPERIMENTAL_TAG "exp"
  585. int __must_check iwl_request_firmware(struct iwl_nic *nic, bool first)
  586. {
  587. struct iwl_cfg *cfg = cfg(nic);
  588. const char *name_pre = cfg->fw_name_pre;
  589. char tag[8];
  590. if (first) {
  591. #ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
  592. nic->fw_index = UCODE_EXPERIMENTAL_INDEX;
  593. strcpy(tag, UCODE_EXPERIMENTAL_TAG);
  594. } else if (nic->fw_index == UCODE_EXPERIMENTAL_INDEX) {
  595. #endif
  596. nic->fw_index = cfg->ucode_api_max;
  597. sprintf(tag, "%d", nic->fw_index);
  598. } else {
  599. nic->fw_index--;
  600. sprintf(tag, "%d", nic->fw_index);
  601. }
  602. if (nic->fw_index < cfg->ucode_api_min) {
  603. IWL_ERR(nic, "no suitable firmware found!\n");
  604. return -ENOENT;
  605. }
  606. sprintf(nic->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
  607. IWL_DEBUG_INFO(nic, "attempting to load firmware %s'%s'\n",
  608. (nic->fw_index == UCODE_EXPERIMENTAL_INDEX)
  609. ? "EXPERIMENTAL " : "",
  610. nic->firmware_name);
  611. return request_firmware_nowait(THIS_MODULE, 1, nic->firmware_name,
  612. trans(nic)->dev,
  613. GFP_KERNEL, nic, iwl_ucode_callback);
  614. }
  615. struct iwlagn_firmware_pieces {
  616. const void *inst, *data, *init, *init_data, *wowlan_inst, *wowlan_data;
  617. size_t inst_size, data_size, init_size, init_data_size,
  618. wowlan_inst_size, wowlan_data_size;
  619. u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
  620. u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
  621. };
  622. static int iwl_parse_v1_v2_firmware(struct iwl_nic *nic,
  623. const struct firmware *ucode_raw,
  624. struct iwlagn_firmware_pieces *pieces)
  625. {
  626. struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
  627. u32 api_ver, hdr_size, build;
  628. char buildstr[25];
  629. const u8 *src;
  630. nic->fw.ucode_ver = le32_to_cpu(ucode->ver);
  631. api_ver = IWL_UCODE_API(nic->fw.ucode_ver);
  632. switch (api_ver) {
  633. default:
  634. hdr_size = 28;
  635. if (ucode_raw->size < hdr_size) {
  636. IWL_ERR(nic, "File size too small!\n");
  637. return -EINVAL;
  638. }
  639. build = le32_to_cpu(ucode->u.v2.build);
  640. pieces->inst_size = le32_to_cpu(ucode->u.v2.inst_size);
  641. pieces->data_size = le32_to_cpu(ucode->u.v2.data_size);
  642. pieces->init_size = le32_to_cpu(ucode->u.v2.init_size);
  643. pieces->init_data_size = le32_to_cpu(ucode->u.v2.init_data_size);
  644. src = ucode->u.v2.data;
  645. break;
  646. case 0:
  647. case 1:
  648. case 2:
  649. hdr_size = 24;
  650. if (ucode_raw->size < hdr_size) {
  651. IWL_ERR(nic, "File size too small!\n");
  652. return -EINVAL;
  653. }
  654. build = 0;
  655. pieces->inst_size = le32_to_cpu(ucode->u.v1.inst_size);
  656. pieces->data_size = le32_to_cpu(ucode->u.v1.data_size);
  657. pieces->init_size = le32_to_cpu(ucode->u.v1.init_size);
  658. pieces->init_data_size = le32_to_cpu(ucode->u.v1.init_data_size);
  659. src = ucode->u.v1.data;
  660. break;
  661. }
  662. if (build)
  663. sprintf(buildstr, " build %u%s", build,
  664. (nic->fw_index == UCODE_EXPERIMENTAL_INDEX)
  665. ? " (EXP)" : "");
  666. else
  667. buildstr[0] = '\0';
  668. snprintf(nic->fw.fw_version,
  669. sizeof(nic->fw.fw_version),
  670. "%u.%u.%u.%u%s",
  671. IWL_UCODE_MAJOR(nic->fw.ucode_ver),
  672. IWL_UCODE_MINOR(nic->fw.ucode_ver),
  673. IWL_UCODE_API(nic->fw.ucode_ver),
  674. IWL_UCODE_SERIAL(nic->fw.ucode_ver),
  675. buildstr);
  676. /* Verify size of file vs. image size info in file's header */
  677. if (ucode_raw->size != hdr_size + pieces->inst_size +
  678. pieces->data_size + pieces->init_size +
  679. pieces->init_data_size) {
  680. IWL_ERR(nic,
  681. "uCode file size %d does not match expected size\n",
  682. (int)ucode_raw->size);
  683. return -EINVAL;
  684. }
  685. pieces->inst = src;
  686. src += pieces->inst_size;
  687. pieces->data = src;
  688. src += pieces->data_size;
  689. pieces->init = src;
  690. src += pieces->init_size;
  691. pieces->init_data = src;
  692. src += pieces->init_data_size;
  693. return 0;
  694. }
  695. static int iwl_parse_tlv_firmware(struct iwl_nic *nic,
  696. const struct firmware *ucode_raw,
  697. struct iwlagn_firmware_pieces *pieces,
  698. struct iwl_ucode_capabilities *capa)
  699. {
  700. struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
  701. struct iwl_ucode_tlv *tlv;
  702. size_t len = ucode_raw->size;
  703. const u8 *data;
  704. int wanted_alternative = iwlagn_mod_params.wanted_ucode_alternative;
  705. int tmp;
  706. u64 alternatives;
  707. u32 tlv_len;
  708. enum iwl_ucode_tlv_type tlv_type;
  709. const u8 *tlv_data;
  710. char buildstr[25];
  711. u32 build;
  712. if (len < sizeof(*ucode)) {
  713. IWL_ERR(nic, "uCode has invalid length: %zd\n", len);
  714. return -EINVAL;
  715. }
  716. if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
  717. IWL_ERR(nic, "invalid uCode magic: 0X%x\n",
  718. le32_to_cpu(ucode->magic));
  719. return -EINVAL;
  720. }
  721. /*
  722. * Check which alternatives are present, and "downgrade"
  723. * when the chosen alternative is not present, warning
  724. * the user when that happens. Some files may not have
  725. * any alternatives, so don't warn in that case.
  726. */
  727. alternatives = le64_to_cpu(ucode->alternatives);
  728. tmp = wanted_alternative;
  729. if (wanted_alternative > 63)
  730. wanted_alternative = 63;
  731. while (wanted_alternative && !(alternatives & BIT(wanted_alternative)))
  732. wanted_alternative--;
  733. if (wanted_alternative && wanted_alternative != tmp)
  734. IWL_WARN(nic,
  735. "uCode alternative %d not available, choosing %d\n",
  736. tmp, wanted_alternative);
  737. nic->fw.ucode_ver = le32_to_cpu(ucode->ver);
  738. build = le32_to_cpu(ucode->build);
  739. if (build)
  740. sprintf(buildstr, " build %u%s", build,
  741. (nic->fw_index == UCODE_EXPERIMENTAL_INDEX)
  742. ? " (EXP)" : "");
  743. else
  744. buildstr[0] = '\0';
  745. snprintf(nic->fw.fw_version,
  746. sizeof(nic->fw.fw_version),
  747. "%u.%u.%u.%u%s",
  748. IWL_UCODE_MAJOR(nic->fw.ucode_ver),
  749. IWL_UCODE_MINOR(nic->fw.ucode_ver),
  750. IWL_UCODE_API(nic->fw.ucode_ver),
  751. IWL_UCODE_SERIAL(nic->fw.ucode_ver),
  752. buildstr);
  753. data = ucode->data;
  754. len -= sizeof(*ucode);
  755. while (len >= sizeof(*tlv)) {
  756. u16 tlv_alt;
  757. len -= sizeof(*tlv);
  758. tlv = (void *)data;
  759. tlv_len = le32_to_cpu(tlv->length);
  760. tlv_type = le16_to_cpu(tlv->type);
  761. tlv_alt = le16_to_cpu(tlv->alternative);
  762. tlv_data = tlv->data;
  763. if (len < tlv_len) {
  764. IWL_ERR(nic, "invalid TLV len: %zd/%u\n",
  765. len, tlv_len);
  766. return -EINVAL;
  767. }
  768. len -= ALIGN(tlv_len, 4);
  769. data += sizeof(*tlv) + ALIGN(tlv_len, 4);
  770. /*
  771. * Alternative 0 is always valid.
  772. *
  773. * Skip alternative TLVs that are not selected.
  774. */
  775. if (tlv_alt != 0 && tlv_alt != wanted_alternative)
  776. continue;
  777. switch (tlv_type) {
  778. case IWL_UCODE_TLV_INST:
  779. pieces->inst = tlv_data;
  780. pieces->inst_size = tlv_len;
  781. break;
  782. case IWL_UCODE_TLV_DATA:
  783. pieces->data = tlv_data;
  784. pieces->data_size = tlv_len;
  785. break;
  786. case IWL_UCODE_TLV_INIT:
  787. pieces->init = tlv_data;
  788. pieces->init_size = tlv_len;
  789. break;
  790. case IWL_UCODE_TLV_INIT_DATA:
  791. pieces->init_data = tlv_data;
  792. pieces->init_data_size = tlv_len;
  793. break;
  794. case IWL_UCODE_TLV_BOOT:
  795. IWL_ERR(nic, "Found unexpected BOOT ucode\n");
  796. break;
  797. case IWL_UCODE_TLV_PROBE_MAX_LEN:
  798. if (tlv_len != sizeof(u32))
  799. goto invalid_tlv_len;
  800. capa->max_probe_length =
  801. le32_to_cpup((__le32 *)tlv_data);
  802. break;
  803. case IWL_UCODE_TLV_PAN:
  804. if (tlv_len)
  805. goto invalid_tlv_len;
  806. capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
  807. break;
  808. case IWL_UCODE_TLV_FLAGS:
  809. /* must be at least one u32 */
  810. if (tlv_len < sizeof(u32))
  811. goto invalid_tlv_len;
  812. /* and a proper number of u32s */
  813. if (tlv_len % sizeof(u32))
  814. goto invalid_tlv_len;
  815. /*
  816. * This driver only reads the first u32 as
  817. * right now no more features are defined,
  818. * if that changes then either the driver
  819. * will not work with the new firmware, or
  820. * it'll not take advantage of new features.
  821. */
  822. capa->flags = le32_to_cpup((__le32 *)tlv_data);
  823. break;
  824. case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
  825. if (tlv_len != sizeof(u32))
  826. goto invalid_tlv_len;
  827. pieces->init_evtlog_ptr =
  828. le32_to_cpup((__le32 *)tlv_data);
  829. break;
  830. case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
  831. if (tlv_len != sizeof(u32))
  832. goto invalid_tlv_len;
  833. pieces->init_evtlog_size =
  834. le32_to_cpup((__le32 *)tlv_data);
  835. break;
  836. case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
  837. if (tlv_len != sizeof(u32))
  838. goto invalid_tlv_len;
  839. pieces->init_errlog_ptr =
  840. le32_to_cpup((__le32 *)tlv_data);
  841. break;
  842. case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
  843. if (tlv_len != sizeof(u32))
  844. goto invalid_tlv_len;
  845. pieces->inst_evtlog_ptr =
  846. le32_to_cpup((__le32 *)tlv_data);
  847. break;
  848. case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
  849. if (tlv_len != sizeof(u32))
  850. goto invalid_tlv_len;
  851. pieces->inst_evtlog_size =
  852. le32_to_cpup((__le32 *)tlv_data);
  853. break;
  854. case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
  855. if (tlv_len != sizeof(u32))
  856. goto invalid_tlv_len;
  857. pieces->inst_errlog_ptr =
  858. le32_to_cpup((__le32 *)tlv_data);
  859. break;
  860. case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
  861. if (tlv_len)
  862. goto invalid_tlv_len;
  863. nic->fw.enhance_sensitivity_table = true;
  864. break;
  865. case IWL_UCODE_TLV_WOWLAN_INST:
  866. pieces->wowlan_inst = tlv_data;
  867. pieces->wowlan_inst_size = tlv_len;
  868. break;
  869. case IWL_UCODE_TLV_WOWLAN_DATA:
  870. pieces->wowlan_data = tlv_data;
  871. pieces->wowlan_data_size = tlv_len;
  872. break;
  873. case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
  874. if (tlv_len != sizeof(u32))
  875. goto invalid_tlv_len;
  876. capa->standard_phy_calibration_size =
  877. le32_to_cpup((__le32 *)tlv_data);
  878. break;
  879. default:
  880. IWL_DEBUG_INFO(nic, "unknown TLV: %d\n", tlv_type);
  881. break;
  882. }
  883. }
  884. if (len) {
  885. IWL_ERR(nic, "invalid TLV after parsing: %zd\n", len);
  886. iwl_print_hex_dump(nic, IWL_DL_FW, (u8 *)data, len);
  887. return -EINVAL;
  888. }
  889. return 0;
  890. invalid_tlv_len:
  891. IWL_ERR(nic, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
  892. iwl_print_hex_dump(nic, IWL_DL_FW, tlv_data, tlv_len);
  893. return -EINVAL;
  894. }
  895. /**
  896. * iwl_ucode_callback - callback when firmware was loaded
  897. *
  898. * If loaded successfully, copies the firmware into buffers
  899. * for the card to fetch (via DMA).
  900. */
  901. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
  902. {
  903. struct iwl_nic *nic = context;
  904. struct iwl_cfg *cfg = cfg(nic);
  905. struct iwl_fw *fw = &nic->fw;
  906. struct iwl_ucode_header *ucode;
  907. int err;
  908. struct iwlagn_firmware_pieces pieces;
  909. const unsigned int api_max = cfg->ucode_api_max;
  910. unsigned int api_ok = cfg->ucode_api_ok;
  911. const unsigned int api_min = cfg->ucode_api_min;
  912. u32 api_ver;
  913. fw->ucode_capa.max_probe_length = 200;
  914. fw->ucode_capa.standard_phy_calibration_size =
  915. IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  916. if (!api_ok)
  917. api_ok = api_max;
  918. memset(&pieces, 0, sizeof(pieces));
  919. if (!ucode_raw) {
  920. if (nic->fw_index <= api_ok)
  921. IWL_ERR(nic,
  922. "request for firmware file '%s' failed.\n",
  923. nic->firmware_name);
  924. goto try_again;
  925. }
  926. IWL_DEBUG_INFO(nic, "Loaded firmware file '%s' (%zd bytes).\n",
  927. nic->firmware_name, ucode_raw->size);
  928. /* Make sure that we got at least the API version number */
  929. if (ucode_raw->size < 4) {
  930. IWL_ERR(nic, "File size way too small!\n");
  931. goto try_again;
  932. }
  933. /* Data from ucode file: header followed by uCode images */
  934. ucode = (struct iwl_ucode_header *)ucode_raw->data;
  935. if (ucode->ver)
  936. err = iwl_parse_v1_v2_firmware(nic, ucode_raw, &pieces);
  937. else
  938. err = iwl_parse_tlv_firmware(nic, ucode_raw, &pieces,
  939. &fw->ucode_capa);
  940. if (err)
  941. goto try_again;
  942. api_ver = IWL_UCODE_API(nic->fw.ucode_ver);
  943. /*
  944. * api_ver should match the api version forming part of the
  945. * firmware filename ... but we don't check for that and only rely
  946. * on the API version read from firmware header from here on forward
  947. */
  948. /* no api version check required for experimental uCode */
  949. if (nic->fw_index != UCODE_EXPERIMENTAL_INDEX) {
  950. if (api_ver < api_min || api_ver > api_max) {
  951. IWL_ERR(nic,
  952. "Driver unable to support your firmware API. "
  953. "Driver supports v%u, firmware is v%u.\n",
  954. api_max, api_ver);
  955. goto try_again;
  956. }
  957. if (api_ver < api_ok) {
  958. if (api_ok != api_max)
  959. IWL_ERR(nic, "Firmware has old API version, "
  960. "expected v%u through v%u, got v%u.\n",
  961. api_ok, api_max, api_ver);
  962. else
  963. IWL_ERR(nic, "Firmware has old API version, "
  964. "expected v%u, got v%u.\n",
  965. api_max, api_ver);
  966. IWL_ERR(nic, "New firmware can be obtained from "
  967. "http://www.intellinuxwireless.org/.\n");
  968. }
  969. }
  970. IWL_INFO(nic, "loaded firmware version %s", nic->fw.fw_version);
  971. /*
  972. * For any of the failures below (before allocating pci memory)
  973. * we will try to load a version with a smaller API -- maybe the
  974. * user just got a corrupted version of the latest API.
  975. */
  976. IWL_DEBUG_INFO(nic, "f/w package hdr ucode version raw = 0x%x\n",
  977. nic->fw.ucode_ver);
  978. IWL_DEBUG_INFO(nic, "f/w package hdr runtime inst size = %Zd\n",
  979. pieces.inst_size);
  980. IWL_DEBUG_INFO(nic, "f/w package hdr runtime data size = %Zd\n",
  981. pieces.data_size);
  982. IWL_DEBUG_INFO(nic, "f/w package hdr init inst size = %Zd\n",
  983. pieces.init_size);
  984. IWL_DEBUG_INFO(nic, "f/w package hdr init data size = %Zd\n",
  985. pieces.init_data_size);
  986. /* Verify that uCode images will fit in card's SRAM */
  987. if (pieces.inst_size > cfg->max_inst_size) {
  988. IWL_ERR(nic, "uCode instr len %Zd too large to fit in\n",
  989. pieces.inst_size);
  990. goto try_again;
  991. }
  992. if (pieces.data_size > cfg->max_data_size) {
  993. IWL_ERR(nic, "uCode data len %Zd too large to fit in\n",
  994. pieces.data_size);
  995. goto try_again;
  996. }
  997. if (pieces.init_size > cfg->max_inst_size) {
  998. IWL_ERR(nic, "uCode init instr len %Zd too large to fit in\n",
  999. pieces.init_size);
  1000. goto try_again;
  1001. }
  1002. if (pieces.init_data_size > cfg->max_data_size) {
  1003. IWL_ERR(nic, "uCode init data len %Zd too large to fit in\n",
  1004. pieces.init_data_size);
  1005. goto try_again;
  1006. }
  1007. /* Allocate ucode buffers for card's bus-master loading ... */
  1008. /* Runtime instructions and 2 copies of data:
  1009. * 1) unmodified from disk
  1010. * 2) backup cache for save/restore during power-downs */
  1011. if (iwl_alloc_fw_desc(nic, &nic->fw.ucode_rt.code,
  1012. pieces.inst, pieces.inst_size))
  1013. goto err_pci_alloc;
  1014. if (iwl_alloc_fw_desc(nic, &nic->fw.ucode_rt.data,
  1015. pieces.data, pieces.data_size))
  1016. goto err_pci_alloc;
  1017. /* Initialization instructions and data */
  1018. if (pieces.init_size && pieces.init_data_size) {
  1019. if (iwl_alloc_fw_desc(nic,
  1020. &nic->fw.ucode_init.code,
  1021. pieces.init, pieces.init_size))
  1022. goto err_pci_alloc;
  1023. if (iwl_alloc_fw_desc(nic,
  1024. &nic->fw.ucode_init.data,
  1025. pieces.init_data, pieces.init_data_size))
  1026. goto err_pci_alloc;
  1027. }
  1028. /* WoWLAN instructions and data */
  1029. if (pieces.wowlan_inst_size && pieces.wowlan_data_size) {
  1030. if (iwl_alloc_fw_desc(nic,
  1031. &nic->fw.ucode_wowlan.code,
  1032. pieces.wowlan_inst,
  1033. pieces.wowlan_inst_size))
  1034. goto err_pci_alloc;
  1035. if (iwl_alloc_fw_desc(nic,
  1036. &nic->fw.ucode_wowlan.data,
  1037. pieces.wowlan_data,
  1038. pieces.wowlan_data_size))
  1039. goto err_pci_alloc;
  1040. }
  1041. /* Now that we can no longer fail, copy information */
  1042. /*
  1043. * The (size - 16) / 12 formula is based on the information recorded
  1044. * for each event, which is of mode 1 (including timestamp) for all
  1045. * new microcodes that include this information.
  1046. */
  1047. nic->init_evtlog_ptr = pieces.init_evtlog_ptr;
  1048. if (pieces.init_evtlog_size)
  1049. nic->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
  1050. else
  1051. nic->init_evtlog_size =
  1052. cfg->base_params->max_event_log_size;
  1053. nic->init_errlog_ptr = pieces.init_errlog_ptr;
  1054. nic->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
  1055. if (pieces.inst_evtlog_size)
  1056. nic->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
  1057. else
  1058. nic->inst_evtlog_size =
  1059. cfg->base_params->max_event_log_size;
  1060. nic->inst_errlog_ptr = pieces.inst_errlog_ptr;
  1061. /*
  1062. * figure out the offset of chain noise reset and gain commands
  1063. * base on the size of standard phy calibration commands table size
  1064. */
  1065. if (fw->ucode_capa.standard_phy_calibration_size >
  1066. IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
  1067. fw->ucode_capa.standard_phy_calibration_size =
  1068. IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  1069. /* We have our copies now, allow OS release its copies */
  1070. release_firmware(ucode_raw);
  1071. complete(&nic->request_firmware_complete);
  1072. if (iwl_op_mode_dvm_start(bus(nic), trans(nic)->ops, cfg))
  1073. goto out_unbind;
  1074. return;
  1075. try_again:
  1076. /* try next, if any */
  1077. release_firmware(ucode_raw);
  1078. if (iwl_request_firmware(nic, false))
  1079. goto out_unbind;
  1080. return;
  1081. err_pci_alloc:
  1082. IWL_ERR(nic, "failed to allocate pci memory\n");
  1083. iwl_dealloc_ucode(nic);
  1084. release_firmware(ucode_raw);
  1085. out_unbind:
  1086. complete(&nic->request_firmware_complete);
  1087. device_release_driver(trans(nic)->dev);
  1088. }