iwl-ucode.c 35 KB

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