iwl-ucode.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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_trans *trans, struct fw_desc *desc)
  81. {
  82. if (desc->v_addr)
  83. dma_free_coherent(trans->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_trans *trans, struct fw_img *img)
  89. {
  90. iwl_free_fw_desc(trans, &img->code);
  91. iwl_free_fw_desc(trans, &img->data);
  92. }
  93. void iwl_dealloc_ucode(struct iwl_trans *trans)
  94. {
  95. iwl_free_fw_img(trans, &trans->ucode_rt);
  96. iwl_free_fw_img(trans, &trans->ucode_init);
  97. iwl_free_fw_img(trans, &trans->ucode_wowlan);
  98. }
  99. static int iwl_alloc_fw_desc(struct iwl_trans *trans, 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->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_trans *trans,
  115. enum iwl_ucode_type ucode_type)
  116. {
  117. switch (ucode_type) {
  118. case IWL_UCODE_INIT:
  119. return &trans->ucode_init;
  120. case IWL_UCODE_WOWLAN:
  121. return &trans->ucode_wowlan;
  122. case IWL_UCODE_REGULAR:
  123. return &trans->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_trans *trans,
  348. struct fw_desc *fw_desc)
  349. {
  350. __le32 *image = (__le32 *)fw_desc->v_addr;
  351. u32 len = fw_desc->len;
  352. u32 val;
  353. u32 i;
  354. IWL_DEBUG_FW(trans, "ucode inst image size is %u\n", len);
  355. for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
  356. /* read data comes through single port, auto-incr addr */
  357. /* NOTE: Use the debugless read so we don't flood kernel log
  358. * if IWL_DL_IO is set */
  359. iwl_write_direct32(trans, HBUS_TARG_MEM_RADDR,
  360. i + IWLAGN_RTC_INST_LOWER_BOUND);
  361. val = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
  362. if (val != le32_to_cpu(*image))
  363. return -EIO;
  364. }
  365. return 0;
  366. }
  367. static void iwl_print_mismatch_inst(struct iwl_trans *trans,
  368. struct fw_desc *fw_desc)
  369. {
  370. __le32 *image = (__le32 *)fw_desc->v_addr;
  371. u32 len = fw_desc->len;
  372. u32 val;
  373. u32 offs;
  374. int errors = 0;
  375. IWL_DEBUG_FW(trans, "ucode inst image size is %u\n", len);
  376. iwl_write_direct32(trans, HBUS_TARG_MEM_RADDR,
  377. IWLAGN_RTC_INST_LOWER_BOUND);
  378. for (offs = 0;
  379. offs < len && errors < 20;
  380. offs += sizeof(u32), image++) {
  381. /* read data comes through single port, auto-incr addr */
  382. val = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
  383. if (val != le32_to_cpu(*image)) {
  384. IWL_ERR(trans, "uCode INST section at "
  385. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  386. offs, val, le32_to_cpu(*image));
  387. errors++;
  388. }
  389. }
  390. }
  391. /**
  392. * iwl_verify_ucode - determine which instruction image is in SRAM,
  393. * and verify its contents
  394. */
  395. static int iwl_verify_ucode(struct iwl_trans *trans,
  396. enum iwl_ucode_type ucode_type)
  397. {
  398. struct fw_img *img = iwl_get_ucode_image(trans, ucode_type);
  399. if (!img) {
  400. IWL_ERR(trans, "Invalid ucode requested (%d)\n", ucode_type);
  401. return -EINVAL;
  402. }
  403. if (!iwl_verify_inst_sparse(trans, &img->code)) {
  404. IWL_DEBUG_FW(trans, "uCode is good in inst SRAM\n");
  405. return 0;
  406. }
  407. IWL_ERR(trans, "UCODE IMAGE IN INSTRUCTION SRAM NOT VALID!!\n");
  408. iwl_print_mismatch_inst(trans, &img->code);
  409. return -EIO;
  410. }
  411. struct iwl_alive_data {
  412. bool valid;
  413. u8 subtype;
  414. };
  415. static void iwl_alive_fn(struct iwl_trans *trans,
  416. struct iwl_rx_packet *pkt,
  417. void *data)
  418. {
  419. struct iwl_alive_data *alive_data = data;
  420. struct iwl_alive_resp *palive;
  421. palive = &pkt->u.alive_frame;
  422. IWL_DEBUG_FW(trans, "Alive ucode status 0x%08X revision "
  423. "0x%01X 0x%01X\n",
  424. palive->is_valid, palive->ver_type,
  425. palive->ver_subtype);
  426. trans->shrd->device_pointers.error_event_table =
  427. le32_to_cpu(palive->error_event_table_ptr);
  428. trans->shrd->device_pointers.log_event_table =
  429. le32_to_cpu(palive->log_event_table_ptr);
  430. alive_data->subtype = palive->ver_subtype;
  431. alive_data->valid = palive->is_valid == UCODE_VALID_OK;
  432. }
  433. /* notification wait support */
  434. void iwl_init_notification_wait(struct iwl_shared *shrd,
  435. struct iwl_notification_wait *wait_entry,
  436. u8 cmd,
  437. void (*fn)(struct iwl_trans *trans,
  438. struct iwl_rx_packet *pkt,
  439. void *data),
  440. void *fn_data)
  441. {
  442. wait_entry->fn = fn;
  443. wait_entry->fn_data = fn_data;
  444. wait_entry->cmd = cmd;
  445. wait_entry->triggered = false;
  446. wait_entry->aborted = false;
  447. spin_lock_bh(&shrd->notif_wait_lock);
  448. list_add(&wait_entry->list, &shrd->notif_waits);
  449. spin_unlock_bh(&shrd->notif_wait_lock);
  450. }
  451. int iwl_wait_notification(struct iwl_shared *shrd,
  452. struct iwl_notification_wait *wait_entry,
  453. unsigned long timeout)
  454. {
  455. int ret;
  456. ret = wait_event_timeout(shrd->notif_waitq,
  457. wait_entry->triggered || wait_entry->aborted,
  458. timeout);
  459. spin_lock_bh(&shrd->notif_wait_lock);
  460. list_del(&wait_entry->list);
  461. spin_unlock_bh(&shrd->notif_wait_lock);
  462. if (wait_entry->aborted)
  463. return -EIO;
  464. /* return value is always >= 0 */
  465. if (ret <= 0)
  466. return -ETIMEDOUT;
  467. return 0;
  468. }
  469. void iwl_remove_notification(struct iwl_shared *shrd,
  470. struct iwl_notification_wait *wait_entry)
  471. {
  472. spin_lock_bh(&shrd->notif_wait_lock);
  473. list_del(&wait_entry->list);
  474. spin_unlock_bh(&shrd->notif_wait_lock);
  475. }
  476. void iwl_abort_notification_waits(struct iwl_shared *shrd)
  477. {
  478. unsigned long flags;
  479. struct iwl_notification_wait *wait_entry;
  480. spin_lock_irqsave(&shrd->notif_wait_lock, flags);
  481. list_for_each_entry(wait_entry, &shrd->notif_waits, list)
  482. wait_entry->aborted = true;
  483. spin_unlock_irqrestore(&shrd->notif_wait_lock, flags);
  484. wake_up_all(&shrd->notif_waitq);
  485. }
  486. #define UCODE_ALIVE_TIMEOUT HZ
  487. #define UCODE_CALIB_TIMEOUT (2*HZ)
  488. int iwl_load_ucode_wait_alive(struct iwl_trans *trans,
  489. enum iwl_ucode_type ucode_type)
  490. {
  491. struct iwl_notification_wait alive_wait;
  492. struct iwl_alive_data alive_data;
  493. struct fw_img *fw;
  494. int ret;
  495. enum iwl_ucode_type old_type;
  496. iwl_init_notification_wait(trans->shrd, &alive_wait, REPLY_ALIVE,
  497. iwl_alive_fn, &alive_data);
  498. old_type = trans->shrd->ucode_type;
  499. trans->shrd->ucode_type = ucode_type;
  500. fw = iwl_get_ucode_image(trans, ucode_type);
  501. if (!fw)
  502. return -EINVAL;
  503. ret = iwl_trans_start_fw(trans, fw);
  504. if (ret) {
  505. trans->shrd->ucode_type = old_type;
  506. iwl_remove_notification(trans->shrd, &alive_wait);
  507. return ret;
  508. }
  509. /*
  510. * Some things may run in the background now, but we
  511. * just wait for the ALIVE notification here.
  512. */
  513. ret = iwl_wait_notification(trans->shrd, &alive_wait,
  514. UCODE_ALIVE_TIMEOUT);
  515. if (ret) {
  516. trans->shrd->ucode_type = old_type;
  517. return ret;
  518. }
  519. if (!alive_data.valid) {
  520. IWL_ERR(trans, "Loaded ucode is not valid!\n");
  521. trans->shrd->ucode_type = old_type;
  522. return -EIO;
  523. }
  524. /*
  525. * This step takes a long time (60-80ms!!) and
  526. * WoWLAN image should be loaded quickly, so
  527. * skip it for WoWLAN.
  528. */
  529. if (ucode_type != IWL_UCODE_WOWLAN) {
  530. ret = iwl_verify_ucode(trans, ucode_type);
  531. if (ret) {
  532. trans->shrd->ucode_type = old_type;
  533. return ret;
  534. }
  535. /* delay a bit to give rfkill time to run */
  536. msleep(5);
  537. }
  538. ret = iwl_alive_notify(trans);
  539. if (ret) {
  540. IWL_WARN(trans,
  541. "Could not complete ALIVE transition: %d\n", ret);
  542. trans->shrd->ucode_type = old_type;
  543. return ret;
  544. }
  545. return 0;
  546. }
  547. int iwl_run_init_ucode(struct iwl_trans *trans)
  548. {
  549. struct iwl_notification_wait calib_wait;
  550. int ret;
  551. lockdep_assert_held(&trans->shrd->mutex);
  552. /* No init ucode required? Curious, but maybe ok */
  553. if (!trans->ucode_init.code.len)
  554. return 0;
  555. if (trans->shrd->ucode_type != IWL_UCODE_NONE)
  556. return 0;
  557. iwl_init_notification_wait(trans->shrd, &calib_wait,
  558. CALIBRATION_COMPLETE_NOTIFICATION,
  559. NULL, NULL);
  560. /* Will also start the device */
  561. ret = iwl_load_ucode_wait_alive(trans, IWL_UCODE_INIT);
  562. if (ret)
  563. goto error;
  564. ret = iwl_init_alive_start(trans);
  565. if (ret)
  566. goto error;
  567. /*
  568. * Some things may run in the background now, but we
  569. * just wait for the calibration complete notification.
  570. */
  571. ret = iwl_wait_notification(trans->shrd, &calib_wait,
  572. UCODE_CALIB_TIMEOUT);
  573. goto out;
  574. error:
  575. iwl_remove_notification(trans->shrd, &calib_wait);
  576. out:
  577. /* Whatever happened, stop the device */
  578. iwl_trans_stop_device(trans);
  579. return ret;
  580. }
  581. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
  582. #define UCODE_EXPERIMENTAL_INDEX 100
  583. #define UCODE_EXPERIMENTAL_TAG "exp"
  584. int __must_check iwl_request_firmware(struct iwl_priv *priv, bool first)
  585. {
  586. const char *name_pre = cfg(priv)->fw_name_pre;
  587. char tag[8];
  588. if (first) {
  589. #ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
  590. priv->fw_index = UCODE_EXPERIMENTAL_INDEX;
  591. strcpy(tag, UCODE_EXPERIMENTAL_TAG);
  592. } else if (priv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
  593. #endif
  594. priv->fw_index = cfg(priv)->ucode_api_max;
  595. sprintf(tag, "%d", priv->fw_index);
  596. } else {
  597. priv->fw_index--;
  598. sprintf(tag, "%d", priv->fw_index);
  599. }
  600. if (priv->fw_index < cfg(priv)->ucode_api_min) {
  601. IWL_ERR(priv, "no suitable firmware found!\n");
  602. return -ENOENT;
  603. }
  604. sprintf(priv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
  605. IWL_DEBUG_INFO(priv, "attempting to load firmware %s'%s'\n",
  606. (priv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  607. ? "EXPERIMENTAL " : "",
  608. priv->firmware_name);
  609. return request_firmware_nowait(THIS_MODULE, 1, priv->firmware_name,
  610. trans(priv)->dev,
  611. GFP_KERNEL, priv, iwl_ucode_callback);
  612. }
  613. struct iwlagn_firmware_pieces {
  614. const void *inst, *data, *init, *init_data, *wowlan_inst, *wowlan_data;
  615. size_t inst_size, data_size, init_size, init_data_size,
  616. wowlan_inst_size, wowlan_data_size;
  617. u32 build;
  618. u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
  619. u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
  620. };
  621. static int iwlagn_load_legacy_firmware(struct iwl_priv *priv,
  622. const struct firmware *ucode_raw,
  623. struct iwlagn_firmware_pieces *pieces)
  624. {
  625. struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
  626. u32 api_ver, hdr_size;
  627. const u8 *src;
  628. priv->ucode_ver = le32_to_cpu(ucode->ver);
  629. api_ver = IWL_UCODE_API(priv->ucode_ver);
  630. switch (api_ver) {
  631. default:
  632. hdr_size = 28;
  633. if (ucode_raw->size < hdr_size) {
  634. IWL_ERR(priv, "File size too small!\n");
  635. return -EINVAL;
  636. }
  637. pieces->build = le32_to_cpu(ucode->u.v2.build);
  638. pieces->inst_size = le32_to_cpu(ucode->u.v2.inst_size);
  639. pieces->data_size = le32_to_cpu(ucode->u.v2.data_size);
  640. pieces->init_size = le32_to_cpu(ucode->u.v2.init_size);
  641. pieces->init_data_size = le32_to_cpu(ucode->u.v2.init_data_size);
  642. src = ucode->u.v2.data;
  643. break;
  644. case 0:
  645. case 1:
  646. case 2:
  647. hdr_size = 24;
  648. if (ucode_raw->size < hdr_size) {
  649. IWL_ERR(priv, "File size too small!\n");
  650. return -EINVAL;
  651. }
  652. pieces->build = 0;
  653. pieces->inst_size = le32_to_cpu(ucode->u.v1.inst_size);
  654. pieces->data_size = le32_to_cpu(ucode->u.v1.data_size);
  655. pieces->init_size = le32_to_cpu(ucode->u.v1.init_size);
  656. pieces->init_data_size = le32_to_cpu(ucode->u.v1.init_data_size);
  657. src = ucode->u.v1.data;
  658. break;
  659. }
  660. /* Verify size of file vs. image size info in file's header */
  661. if (ucode_raw->size != hdr_size + pieces->inst_size +
  662. pieces->data_size + pieces->init_size +
  663. pieces->init_data_size) {
  664. IWL_ERR(priv,
  665. "uCode file size %d does not match expected size\n",
  666. (int)ucode_raw->size);
  667. return -EINVAL;
  668. }
  669. pieces->inst = src;
  670. src += pieces->inst_size;
  671. pieces->data = src;
  672. src += pieces->data_size;
  673. pieces->init = src;
  674. src += pieces->init_size;
  675. pieces->init_data = src;
  676. src += pieces->init_data_size;
  677. return 0;
  678. }
  679. static int iwlagn_load_firmware(struct iwl_priv *priv,
  680. const struct firmware *ucode_raw,
  681. struct iwlagn_firmware_pieces *pieces,
  682. struct iwlagn_ucode_capabilities *capa)
  683. {
  684. struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
  685. struct iwl_ucode_tlv *tlv;
  686. size_t len = ucode_raw->size;
  687. const u8 *data;
  688. int wanted_alternative = iwlagn_mod_params.wanted_ucode_alternative;
  689. int tmp;
  690. u64 alternatives;
  691. u32 tlv_len;
  692. enum iwl_ucode_tlv_type tlv_type;
  693. const u8 *tlv_data;
  694. if (len < sizeof(*ucode)) {
  695. IWL_ERR(priv, "uCode has invalid length: %zd\n", len);
  696. return -EINVAL;
  697. }
  698. if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
  699. IWL_ERR(priv, "invalid uCode magic: 0X%x\n",
  700. le32_to_cpu(ucode->magic));
  701. return -EINVAL;
  702. }
  703. /*
  704. * Check which alternatives are present, and "downgrade"
  705. * when the chosen alternative is not present, warning
  706. * the user when that happens. Some files may not have
  707. * any alternatives, so don't warn in that case.
  708. */
  709. alternatives = le64_to_cpu(ucode->alternatives);
  710. tmp = wanted_alternative;
  711. if (wanted_alternative > 63)
  712. wanted_alternative = 63;
  713. while (wanted_alternative && !(alternatives & BIT(wanted_alternative)))
  714. wanted_alternative--;
  715. if (wanted_alternative && wanted_alternative != tmp)
  716. IWL_WARN(priv,
  717. "uCode alternative %d not available, choosing %d\n",
  718. tmp, wanted_alternative);
  719. priv->ucode_ver = le32_to_cpu(ucode->ver);
  720. pieces->build = le32_to_cpu(ucode->build);
  721. data = ucode->data;
  722. len -= sizeof(*ucode);
  723. while (len >= sizeof(*tlv)) {
  724. u16 tlv_alt;
  725. len -= sizeof(*tlv);
  726. tlv = (void *)data;
  727. tlv_len = le32_to_cpu(tlv->length);
  728. tlv_type = le16_to_cpu(tlv->type);
  729. tlv_alt = le16_to_cpu(tlv->alternative);
  730. tlv_data = tlv->data;
  731. if (len < tlv_len) {
  732. IWL_ERR(priv, "invalid TLV len: %zd/%u\n",
  733. len, tlv_len);
  734. return -EINVAL;
  735. }
  736. len -= ALIGN(tlv_len, 4);
  737. data += sizeof(*tlv) + ALIGN(tlv_len, 4);
  738. /*
  739. * Alternative 0 is always valid.
  740. *
  741. * Skip alternative TLVs that are not selected.
  742. */
  743. if (tlv_alt != 0 && tlv_alt != wanted_alternative)
  744. continue;
  745. switch (tlv_type) {
  746. case IWL_UCODE_TLV_INST:
  747. pieces->inst = tlv_data;
  748. pieces->inst_size = tlv_len;
  749. break;
  750. case IWL_UCODE_TLV_DATA:
  751. pieces->data = tlv_data;
  752. pieces->data_size = tlv_len;
  753. break;
  754. case IWL_UCODE_TLV_INIT:
  755. pieces->init = tlv_data;
  756. pieces->init_size = tlv_len;
  757. break;
  758. case IWL_UCODE_TLV_INIT_DATA:
  759. pieces->init_data = tlv_data;
  760. pieces->init_data_size = tlv_len;
  761. break;
  762. case IWL_UCODE_TLV_BOOT:
  763. IWL_ERR(priv, "Found unexpected BOOT ucode\n");
  764. break;
  765. case IWL_UCODE_TLV_PROBE_MAX_LEN:
  766. if (tlv_len != sizeof(u32))
  767. goto invalid_tlv_len;
  768. capa->max_probe_length =
  769. le32_to_cpup((__le32 *)tlv_data);
  770. break;
  771. case IWL_UCODE_TLV_PAN:
  772. if (tlv_len)
  773. goto invalid_tlv_len;
  774. capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
  775. break;
  776. case IWL_UCODE_TLV_FLAGS:
  777. /* must be at least one u32 */
  778. if (tlv_len < sizeof(u32))
  779. goto invalid_tlv_len;
  780. /* and a proper number of u32s */
  781. if (tlv_len % sizeof(u32))
  782. goto invalid_tlv_len;
  783. /*
  784. * This driver only reads the first u32 as
  785. * right now no more features are defined,
  786. * if that changes then either the driver
  787. * will not work with the new firmware, or
  788. * it'll not take advantage of new features.
  789. */
  790. capa->flags = le32_to_cpup((__le32 *)tlv_data);
  791. break;
  792. case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
  793. if (tlv_len != sizeof(u32))
  794. goto invalid_tlv_len;
  795. pieces->init_evtlog_ptr =
  796. le32_to_cpup((__le32 *)tlv_data);
  797. break;
  798. case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
  799. if (tlv_len != sizeof(u32))
  800. goto invalid_tlv_len;
  801. pieces->init_evtlog_size =
  802. le32_to_cpup((__le32 *)tlv_data);
  803. break;
  804. case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
  805. if (tlv_len != sizeof(u32))
  806. goto invalid_tlv_len;
  807. pieces->init_errlog_ptr =
  808. le32_to_cpup((__le32 *)tlv_data);
  809. break;
  810. case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
  811. if (tlv_len != sizeof(u32))
  812. goto invalid_tlv_len;
  813. pieces->inst_evtlog_ptr =
  814. le32_to_cpup((__le32 *)tlv_data);
  815. break;
  816. case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
  817. if (tlv_len != sizeof(u32))
  818. goto invalid_tlv_len;
  819. pieces->inst_evtlog_size =
  820. le32_to_cpup((__le32 *)tlv_data);
  821. break;
  822. case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
  823. if (tlv_len != sizeof(u32))
  824. goto invalid_tlv_len;
  825. pieces->inst_errlog_ptr =
  826. le32_to_cpup((__le32 *)tlv_data);
  827. break;
  828. case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
  829. if (tlv_len)
  830. goto invalid_tlv_len;
  831. priv->enhance_sensitivity_table = true;
  832. break;
  833. case IWL_UCODE_TLV_WOWLAN_INST:
  834. pieces->wowlan_inst = tlv_data;
  835. pieces->wowlan_inst_size = tlv_len;
  836. break;
  837. case IWL_UCODE_TLV_WOWLAN_DATA:
  838. pieces->wowlan_data = tlv_data;
  839. pieces->wowlan_data_size = tlv_len;
  840. break;
  841. case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
  842. if (tlv_len != sizeof(u32))
  843. goto invalid_tlv_len;
  844. capa->standard_phy_calibration_size =
  845. le32_to_cpup((__le32 *)tlv_data);
  846. break;
  847. default:
  848. IWL_DEBUG_INFO(priv, "unknown TLV: %d\n", tlv_type);
  849. break;
  850. }
  851. }
  852. if (len) {
  853. IWL_ERR(priv, "invalid TLV after parsing: %zd\n", len);
  854. iwl_print_hex_dump(priv, IWL_DL_FW, (u8 *)data, len);
  855. return -EINVAL;
  856. }
  857. return 0;
  858. invalid_tlv_len:
  859. IWL_ERR(priv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
  860. iwl_print_hex_dump(priv, IWL_DL_FW, tlv_data, tlv_len);
  861. return -EINVAL;
  862. }
  863. /**
  864. * iwl_ucode_callback - callback when firmware was loaded
  865. *
  866. * If loaded successfully, copies the firmware into buffers
  867. * for the card to fetch (via DMA).
  868. */
  869. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
  870. {
  871. struct iwl_priv *priv = context;
  872. struct iwl_ucode_header *ucode;
  873. int err;
  874. struct iwlagn_firmware_pieces pieces;
  875. const unsigned int api_max = cfg(priv)->ucode_api_max;
  876. unsigned int api_ok = cfg(priv)->ucode_api_ok;
  877. const unsigned int api_min = cfg(priv)->ucode_api_min;
  878. u32 api_ver;
  879. char buildstr[25];
  880. u32 build;
  881. struct iwlagn_ucode_capabilities ucode_capa = {
  882. .max_probe_length = 200,
  883. .standard_phy_calibration_size =
  884. IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE,
  885. };
  886. if (!api_ok)
  887. api_ok = api_max;
  888. memset(&pieces, 0, sizeof(pieces));
  889. if (!ucode_raw) {
  890. if (priv->fw_index <= api_ok)
  891. IWL_ERR(priv,
  892. "request for firmware file '%s' failed.\n",
  893. priv->firmware_name);
  894. goto try_again;
  895. }
  896. IWL_DEBUG_INFO(priv, "Loaded firmware file '%s' (%zd bytes).\n",
  897. priv->firmware_name, ucode_raw->size);
  898. /* Make sure that we got at least the API version number */
  899. if (ucode_raw->size < 4) {
  900. IWL_ERR(priv, "File size way too small!\n");
  901. goto try_again;
  902. }
  903. /* Data from ucode file: header followed by uCode images */
  904. ucode = (struct iwl_ucode_header *)ucode_raw->data;
  905. if (ucode->ver)
  906. err = iwlagn_load_legacy_firmware(priv, ucode_raw, &pieces);
  907. else
  908. err = iwlagn_load_firmware(priv, ucode_raw, &pieces,
  909. &ucode_capa);
  910. if (err)
  911. goto try_again;
  912. api_ver = IWL_UCODE_API(priv->ucode_ver);
  913. build = pieces.build;
  914. /*
  915. * api_ver should match the api version forming part of the
  916. * firmware filename ... but we don't check for that and only rely
  917. * on the API version read from firmware header from here on forward
  918. */
  919. /* no api version check required for experimental uCode */
  920. if (priv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
  921. if (api_ver < api_min || api_ver > api_max) {
  922. IWL_ERR(priv,
  923. "Driver unable to support your firmware API. "
  924. "Driver supports v%u, firmware is v%u.\n",
  925. api_max, api_ver);
  926. goto try_again;
  927. }
  928. if (api_ver < api_ok) {
  929. if (api_ok != api_max)
  930. IWL_ERR(priv, "Firmware has old API version, "
  931. "expected v%u through v%u, got v%u.\n",
  932. api_ok, api_max, api_ver);
  933. else
  934. IWL_ERR(priv, "Firmware has old API version, "
  935. "expected v%u, got v%u.\n",
  936. api_max, api_ver);
  937. IWL_ERR(priv, "New firmware can be obtained from "
  938. "http://www.intellinuxwireless.org/.\n");
  939. }
  940. }
  941. if (build)
  942. sprintf(buildstr, " build %u%s", build,
  943. (priv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  944. ? " (EXP)" : "");
  945. else
  946. buildstr[0] = '\0';
  947. IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u%s\n",
  948. IWL_UCODE_MAJOR(priv->ucode_ver),
  949. IWL_UCODE_MINOR(priv->ucode_ver),
  950. IWL_UCODE_API(priv->ucode_ver),
  951. IWL_UCODE_SERIAL(priv->ucode_ver),
  952. buildstr);
  953. snprintf(priv->hw->wiphy->fw_version,
  954. sizeof(priv->hw->wiphy->fw_version),
  955. "%u.%u.%u.%u%s",
  956. IWL_UCODE_MAJOR(priv->ucode_ver),
  957. IWL_UCODE_MINOR(priv->ucode_ver),
  958. IWL_UCODE_API(priv->ucode_ver),
  959. IWL_UCODE_SERIAL(priv->ucode_ver),
  960. buildstr);
  961. /*
  962. * For any of the failures below (before allocating pci memory)
  963. * we will try to load a version with a smaller API -- maybe the
  964. * user just got a corrupted version of the latest API.
  965. */
  966. IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
  967. priv->ucode_ver);
  968. IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %Zd\n",
  969. pieces.inst_size);
  970. IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %Zd\n",
  971. pieces.data_size);
  972. IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %Zd\n",
  973. pieces.init_size);
  974. IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %Zd\n",
  975. pieces.init_data_size);
  976. /* Verify that uCode images will fit in card's SRAM */
  977. if (pieces.inst_size > hw_params(priv).max_inst_size) {
  978. IWL_ERR(priv, "uCode instr len %Zd too large to fit in\n",
  979. pieces.inst_size);
  980. goto try_again;
  981. }
  982. if (pieces.data_size > hw_params(priv).max_data_size) {
  983. IWL_ERR(priv, "uCode data len %Zd too large to fit in\n",
  984. pieces.data_size);
  985. goto try_again;
  986. }
  987. if (pieces.init_size > hw_params(priv).max_inst_size) {
  988. IWL_ERR(priv, "uCode init instr len %Zd too large to fit in\n",
  989. pieces.init_size);
  990. goto try_again;
  991. }
  992. if (pieces.init_data_size > hw_params(priv).max_data_size) {
  993. IWL_ERR(priv, "uCode init data len %Zd too large to fit in\n",
  994. pieces.init_data_size);
  995. goto try_again;
  996. }
  997. /* Allocate ucode buffers for card's bus-master loading ... */
  998. /* Runtime instructions and 2 copies of data:
  999. * 1) unmodified from disk
  1000. * 2) backup cache for save/restore during power-downs */
  1001. if (iwl_alloc_fw_desc(trans(priv), &trans(priv)->ucode_rt.code,
  1002. pieces.inst, pieces.inst_size))
  1003. goto err_pci_alloc;
  1004. if (iwl_alloc_fw_desc(trans(priv), &trans(priv)->ucode_rt.data,
  1005. pieces.data, pieces.data_size))
  1006. goto err_pci_alloc;
  1007. /* Initialization instructions and data */
  1008. if (pieces.init_size && pieces.init_data_size) {
  1009. if (iwl_alloc_fw_desc(trans(priv),
  1010. &trans(priv)->ucode_init.code,
  1011. pieces.init, pieces.init_size))
  1012. goto err_pci_alloc;
  1013. if (iwl_alloc_fw_desc(trans(priv),
  1014. &trans(priv)->ucode_init.data,
  1015. pieces.init_data, pieces.init_data_size))
  1016. goto err_pci_alloc;
  1017. }
  1018. /* WoWLAN instructions and data */
  1019. if (pieces.wowlan_inst_size && pieces.wowlan_data_size) {
  1020. if (iwl_alloc_fw_desc(trans(priv),
  1021. &trans(priv)->ucode_wowlan.code,
  1022. pieces.wowlan_inst,
  1023. pieces.wowlan_inst_size))
  1024. goto err_pci_alloc;
  1025. if (iwl_alloc_fw_desc(trans(priv),
  1026. &trans(priv)->ucode_wowlan.data,
  1027. pieces.wowlan_data,
  1028. pieces.wowlan_data_size))
  1029. goto err_pci_alloc;
  1030. }
  1031. /* Now that we can no longer fail, copy information */
  1032. /*
  1033. * The (size - 16) / 12 formula is based on the information recorded
  1034. * for each event, which is of mode 1 (including timestamp) for all
  1035. * new microcodes that include this information.
  1036. */
  1037. priv->init_evtlog_ptr = pieces.init_evtlog_ptr;
  1038. if (pieces.init_evtlog_size)
  1039. priv->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
  1040. else
  1041. priv->init_evtlog_size =
  1042. cfg(priv)->base_params->max_event_log_size;
  1043. priv->init_errlog_ptr = pieces.init_errlog_ptr;
  1044. priv->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
  1045. if (pieces.inst_evtlog_size)
  1046. priv->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
  1047. else
  1048. priv->inst_evtlog_size =
  1049. cfg(priv)->base_params->max_event_log_size;
  1050. priv->inst_errlog_ptr = pieces.inst_errlog_ptr;
  1051. #ifndef CONFIG_IWLWIFI_P2P
  1052. ucode_capa.flags &= ~IWL_UCODE_TLV_FLAGS_PAN;
  1053. #endif
  1054. priv->new_scan_threshold_behaviour =
  1055. !!(ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NEWSCAN);
  1056. if (!(cfg(priv)->sku & EEPROM_SKU_CAP_IPAN_ENABLE))
  1057. ucode_capa.flags &= ~IWL_UCODE_TLV_FLAGS_PAN;
  1058. /*
  1059. * if not PAN, then don't support P2P -- might be a uCode
  1060. * packaging bug or due to the eeprom check above
  1061. */
  1062. if (!(ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN))
  1063. ucode_capa.flags &= ~IWL_UCODE_TLV_FLAGS_P2P;
  1064. if (ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN) {
  1065. priv->sta_key_max_num = STA_KEY_MAX_NUM_PAN;
  1066. priv->shrd->cmd_queue = IWL_IPAN_CMD_QUEUE_NUM;
  1067. } else {
  1068. priv->sta_key_max_num = STA_KEY_MAX_NUM;
  1069. priv->shrd->cmd_queue = IWL_DEFAULT_CMD_QUEUE_NUM;
  1070. }
  1071. /*
  1072. * figure out the offset of chain noise reset and gain commands
  1073. * base on the size of standard phy calibration commands table size
  1074. */
  1075. if (ucode_capa.standard_phy_calibration_size >
  1076. IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
  1077. ucode_capa.standard_phy_calibration_size =
  1078. IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  1079. priv->phy_calib_chain_noise_reset_cmd =
  1080. ucode_capa.standard_phy_calibration_size;
  1081. priv->phy_calib_chain_noise_gain_cmd =
  1082. ucode_capa.standard_phy_calibration_size + 1;
  1083. /* initialize all valid contexts */
  1084. iwl_init_context(priv, ucode_capa.flags);
  1085. /**************************************************
  1086. * This is still part of probe() in a sense...
  1087. *
  1088. * 9. Setup and register with mac80211 and debugfs
  1089. **************************************************/
  1090. err = iwlagn_mac_setup_register(priv, &ucode_capa);
  1091. if (err)
  1092. goto out_unbind;
  1093. err = iwl_dbgfs_register(priv, DRV_NAME);
  1094. if (err)
  1095. IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
  1096. /* We have our copies now, allow OS release its copies */
  1097. release_firmware(ucode_raw);
  1098. complete(&priv->firmware_loading_complete);
  1099. return;
  1100. try_again:
  1101. /* try next, if any */
  1102. if (iwl_request_firmware(priv, false))
  1103. goto out_unbind;
  1104. release_firmware(ucode_raw);
  1105. return;
  1106. err_pci_alloc:
  1107. IWL_ERR(priv, "failed to allocate pci memory\n");
  1108. iwl_dealloc_ucode(trans(priv));
  1109. out_unbind:
  1110. complete(&priv->firmware_loading_complete);
  1111. device_release_driver(trans(priv)->dev);
  1112. release_firmware(ucode_raw);
  1113. }