iwl-ucode.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  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_INDEX 100
  585. #define UCODE_EXPERIMENTAL_TAG "exp"
  586. int __must_check iwl_request_firmware(struct iwl_nic *nic, bool first)
  587. {
  588. 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 build;
  621. u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
  622. u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
  623. };
  624. static int iwl_parse_v1_v2_firmware(struct iwl_nic *nic,
  625. const struct firmware *ucode_raw,
  626. struct iwlagn_firmware_pieces *pieces)
  627. {
  628. struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
  629. u32 api_ver, hdr_size;
  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. pieces->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. pieces->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. /* Verify size of file vs. image size info in file's header */
  664. if (ucode_raw->size != hdr_size + pieces->inst_size +
  665. pieces->data_size + pieces->init_size +
  666. pieces->init_data_size) {
  667. IWL_ERR(nic,
  668. "uCode file size %d does not match expected size\n",
  669. (int)ucode_raw->size);
  670. return -EINVAL;
  671. }
  672. pieces->inst = src;
  673. src += pieces->inst_size;
  674. pieces->data = src;
  675. src += pieces->data_size;
  676. pieces->init = src;
  677. src += pieces->init_size;
  678. pieces->init_data = src;
  679. src += pieces->init_data_size;
  680. return 0;
  681. }
  682. static int iwl_parse_tlv_firmware(struct iwl_nic *nic,
  683. const struct firmware *ucode_raw,
  684. struct iwlagn_firmware_pieces *pieces,
  685. struct iwl_ucode_capabilities *capa)
  686. {
  687. struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
  688. struct iwl_ucode_tlv *tlv;
  689. size_t len = ucode_raw->size;
  690. const u8 *data;
  691. int wanted_alternative = iwlagn_mod_params.wanted_ucode_alternative;
  692. int tmp;
  693. u64 alternatives;
  694. u32 tlv_len;
  695. enum iwl_ucode_tlv_type tlv_type;
  696. const u8 *tlv_data;
  697. if (len < sizeof(*ucode)) {
  698. IWL_ERR(nic, "uCode has invalid length: %zd\n", len);
  699. return -EINVAL;
  700. }
  701. if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
  702. IWL_ERR(nic, "invalid uCode magic: 0X%x\n",
  703. le32_to_cpu(ucode->magic));
  704. return -EINVAL;
  705. }
  706. /*
  707. * Check which alternatives are present, and "downgrade"
  708. * when the chosen alternative is not present, warning
  709. * the user when that happens. Some files may not have
  710. * any alternatives, so don't warn in that case.
  711. */
  712. alternatives = le64_to_cpu(ucode->alternatives);
  713. tmp = wanted_alternative;
  714. if (wanted_alternative > 63)
  715. wanted_alternative = 63;
  716. while (wanted_alternative && !(alternatives & BIT(wanted_alternative)))
  717. wanted_alternative--;
  718. if (wanted_alternative && wanted_alternative != tmp)
  719. IWL_WARN(nic,
  720. "uCode alternative %d not available, choosing %d\n",
  721. tmp, wanted_alternative);
  722. nic->fw.ucode_ver = le32_to_cpu(ucode->ver);
  723. pieces->build = le32_to_cpu(ucode->build);
  724. data = ucode->data;
  725. len -= sizeof(*ucode);
  726. while (len >= sizeof(*tlv)) {
  727. u16 tlv_alt;
  728. len -= sizeof(*tlv);
  729. tlv = (void *)data;
  730. tlv_len = le32_to_cpu(tlv->length);
  731. tlv_type = le16_to_cpu(tlv->type);
  732. tlv_alt = le16_to_cpu(tlv->alternative);
  733. tlv_data = tlv->data;
  734. if (len < tlv_len) {
  735. IWL_ERR(nic, "invalid TLV len: %zd/%u\n",
  736. len, tlv_len);
  737. return -EINVAL;
  738. }
  739. len -= ALIGN(tlv_len, 4);
  740. data += sizeof(*tlv) + ALIGN(tlv_len, 4);
  741. /*
  742. * Alternative 0 is always valid.
  743. *
  744. * Skip alternative TLVs that are not selected.
  745. */
  746. if (tlv_alt != 0 && tlv_alt != wanted_alternative)
  747. continue;
  748. switch (tlv_type) {
  749. case IWL_UCODE_TLV_INST:
  750. pieces->inst = tlv_data;
  751. pieces->inst_size = tlv_len;
  752. break;
  753. case IWL_UCODE_TLV_DATA:
  754. pieces->data = tlv_data;
  755. pieces->data_size = tlv_len;
  756. break;
  757. case IWL_UCODE_TLV_INIT:
  758. pieces->init = tlv_data;
  759. pieces->init_size = tlv_len;
  760. break;
  761. case IWL_UCODE_TLV_INIT_DATA:
  762. pieces->init_data = tlv_data;
  763. pieces->init_data_size = tlv_len;
  764. break;
  765. case IWL_UCODE_TLV_BOOT:
  766. IWL_ERR(nic, "Found unexpected BOOT ucode\n");
  767. break;
  768. case IWL_UCODE_TLV_PROBE_MAX_LEN:
  769. if (tlv_len != sizeof(u32))
  770. goto invalid_tlv_len;
  771. capa->max_probe_length =
  772. le32_to_cpup((__le32 *)tlv_data);
  773. break;
  774. case IWL_UCODE_TLV_PAN:
  775. if (tlv_len)
  776. goto invalid_tlv_len;
  777. capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
  778. break;
  779. case IWL_UCODE_TLV_FLAGS:
  780. /* must be at least one u32 */
  781. if (tlv_len < sizeof(u32))
  782. goto invalid_tlv_len;
  783. /* and a proper number of u32s */
  784. if (tlv_len % sizeof(u32))
  785. goto invalid_tlv_len;
  786. /*
  787. * This driver only reads the first u32 as
  788. * right now no more features are defined,
  789. * if that changes then either the driver
  790. * will not work with the new firmware, or
  791. * it'll not take advantage of new features.
  792. */
  793. capa->flags = le32_to_cpup((__le32 *)tlv_data);
  794. break;
  795. case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
  796. if (tlv_len != sizeof(u32))
  797. goto invalid_tlv_len;
  798. pieces->init_evtlog_ptr =
  799. le32_to_cpup((__le32 *)tlv_data);
  800. break;
  801. case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
  802. if (tlv_len != sizeof(u32))
  803. goto invalid_tlv_len;
  804. pieces->init_evtlog_size =
  805. le32_to_cpup((__le32 *)tlv_data);
  806. break;
  807. case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
  808. if (tlv_len != sizeof(u32))
  809. goto invalid_tlv_len;
  810. pieces->init_errlog_ptr =
  811. le32_to_cpup((__le32 *)tlv_data);
  812. break;
  813. case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
  814. if (tlv_len != sizeof(u32))
  815. goto invalid_tlv_len;
  816. pieces->inst_evtlog_ptr =
  817. le32_to_cpup((__le32 *)tlv_data);
  818. break;
  819. case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
  820. if (tlv_len != sizeof(u32))
  821. goto invalid_tlv_len;
  822. pieces->inst_evtlog_size =
  823. le32_to_cpup((__le32 *)tlv_data);
  824. break;
  825. case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
  826. if (tlv_len != sizeof(u32))
  827. goto invalid_tlv_len;
  828. pieces->inst_errlog_ptr =
  829. le32_to_cpup((__le32 *)tlv_data);
  830. break;
  831. case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
  832. if (tlv_len)
  833. goto invalid_tlv_len;
  834. nic->fw.enhance_sensitivity_table = true;
  835. break;
  836. case IWL_UCODE_TLV_WOWLAN_INST:
  837. pieces->wowlan_inst = tlv_data;
  838. pieces->wowlan_inst_size = tlv_len;
  839. break;
  840. case IWL_UCODE_TLV_WOWLAN_DATA:
  841. pieces->wowlan_data = tlv_data;
  842. pieces->wowlan_data_size = tlv_len;
  843. break;
  844. case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
  845. if (tlv_len != sizeof(u32))
  846. goto invalid_tlv_len;
  847. capa->standard_phy_calibration_size =
  848. le32_to_cpup((__le32 *)tlv_data);
  849. break;
  850. default:
  851. IWL_DEBUG_INFO(nic, "unknown TLV: %d\n", tlv_type);
  852. break;
  853. }
  854. }
  855. if (len) {
  856. IWL_ERR(nic, "invalid TLV after parsing: %zd\n", len);
  857. iwl_print_hex_dump(nic, IWL_DL_FW, (u8 *)data, len);
  858. return -EINVAL;
  859. }
  860. return 0;
  861. invalid_tlv_len:
  862. IWL_ERR(nic, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
  863. iwl_print_hex_dump(nic, IWL_DL_FW, tlv_data, tlv_len);
  864. return -EINVAL;
  865. }
  866. /**
  867. * iwl_ucode_callback - callback when firmware was loaded
  868. *
  869. * If loaded successfully, copies the firmware into buffers
  870. * for the card to fetch (via DMA).
  871. */
  872. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
  873. {
  874. struct iwl_nic *nic = context;
  875. struct iwl_cfg *cfg = cfg(nic);
  876. struct iwl_fw *fw = &nic->fw;
  877. struct iwl_priv *priv = priv(nic); /* temporary */
  878. struct iwl_ucode_header *ucode;
  879. int err;
  880. struct iwlagn_firmware_pieces pieces;
  881. const unsigned int api_max = cfg->ucode_api_max;
  882. unsigned int api_ok = cfg->ucode_api_ok;
  883. const unsigned int api_min = cfg->ucode_api_min;
  884. u32 api_ver;
  885. char buildstr[25];
  886. u32 build;
  887. fw->ucode_capa.max_probe_length = 200;
  888. fw->ucode_capa.standard_phy_calibration_size =
  889. IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  890. if (!api_ok)
  891. api_ok = api_max;
  892. memset(&pieces, 0, sizeof(pieces));
  893. if (!ucode_raw) {
  894. if (nic->fw_index <= api_ok)
  895. IWL_ERR(nic,
  896. "request for firmware file '%s' failed.\n",
  897. nic->firmware_name);
  898. goto try_again;
  899. }
  900. IWL_DEBUG_INFO(nic, "Loaded firmware file '%s' (%zd bytes).\n",
  901. nic->firmware_name, ucode_raw->size);
  902. /* Make sure that we got at least the API version number */
  903. if (ucode_raw->size < 4) {
  904. IWL_ERR(nic, "File size way too small!\n");
  905. goto try_again;
  906. }
  907. /* Data from ucode file: header followed by uCode images */
  908. ucode = (struct iwl_ucode_header *)ucode_raw->data;
  909. if (ucode->ver)
  910. err = iwl_parse_v1_v2_firmware(nic, ucode_raw, &pieces);
  911. else
  912. err = iwl_parse_tlv_firmware(nic, ucode_raw, &pieces,
  913. &fw->ucode_capa);
  914. if (err)
  915. goto try_again;
  916. api_ver = IWL_UCODE_API(nic->fw.ucode_ver);
  917. build = pieces.build;
  918. /*
  919. * api_ver should match the api version forming part of the
  920. * firmware filename ... but we don't check for that and only rely
  921. * on the API version read from firmware header from here on forward
  922. */
  923. /* no api version check required for experimental uCode */
  924. if (nic->fw_index != UCODE_EXPERIMENTAL_INDEX) {
  925. if (api_ver < api_min || api_ver > api_max) {
  926. IWL_ERR(nic,
  927. "Driver unable to support your firmware API. "
  928. "Driver supports v%u, firmware is v%u.\n",
  929. api_max, api_ver);
  930. goto try_again;
  931. }
  932. if (api_ver < api_ok) {
  933. if (api_ok != api_max)
  934. IWL_ERR(nic, "Firmware has old API version, "
  935. "expected v%u through v%u, got v%u.\n",
  936. api_ok, api_max, api_ver);
  937. else
  938. IWL_ERR(nic, "Firmware has old API version, "
  939. "expected v%u, got v%u.\n",
  940. api_max, api_ver);
  941. IWL_ERR(nic, "New firmware can be obtained from "
  942. "http://www.intellinuxwireless.org/.\n");
  943. }
  944. }
  945. if (build)
  946. sprintf(buildstr, " build %u%s", build,
  947. (nic->fw_index == UCODE_EXPERIMENTAL_INDEX)
  948. ? " (EXP)" : "");
  949. else
  950. buildstr[0] = '\0';
  951. IWL_INFO(nic, "loaded firmware version %u.%u.%u.%u%s\n",
  952. IWL_UCODE_MAJOR(nic->fw.ucode_ver),
  953. IWL_UCODE_MINOR(nic->fw.ucode_ver),
  954. IWL_UCODE_API(nic->fw.ucode_ver),
  955. IWL_UCODE_SERIAL(nic->fw.ucode_ver),
  956. buildstr);
  957. snprintf(priv->hw->wiphy->fw_version,
  958. sizeof(priv->hw->wiphy->fw_version),
  959. "%u.%u.%u.%u%s",
  960. IWL_UCODE_MAJOR(nic->fw.ucode_ver),
  961. IWL_UCODE_MINOR(nic->fw.ucode_ver),
  962. IWL_UCODE_API(nic->fw.ucode_ver),
  963. IWL_UCODE_SERIAL(nic->fw.ucode_ver),
  964. buildstr);
  965. /*
  966. * For any of the failures below (before allocating pci memory)
  967. * we will try to load a version with a smaller API -- maybe the
  968. * user just got a corrupted version of the latest API.
  969. */
  970. IWL_DEBUG_INFO(nic, "f/w package hdr ucode version raw = 0x%x\n",
  971. nic->fw.ucode_ver);
  972. IWL_DEBUG_INFO(nic, "f/w package hdr runtime inst size = %Zd\n",
  973. pieces.inst_size);
  974. IWL_DEBUG_INFO(nic, "f/w package hdr runtime data size = %Zd\n",
  975. pieces.data_size);
  976. IWL_DEBUG_INFO(nic, "f/w package hdr init inst size = %Zd\n",
  977. pieces.init_size);
  978. IWL_DEBUG_INFO(nic, "f/w package hdr init data size = %Zd\n",
  979. pieces.init_data_size);
  980. /* Verify that uCode images will fit in card's SRAM */
  981. if (pieces.inst_size > cfg->max_inst_size) {
  982. IWL_ERR(nic, "uCode instr len %Zd too large to fit in\n",
  983. pieces.inst_size);
  984. goto try_again;
  985. }
  986. if (pieces.data_size > cfg->max_data_size) {
  987. IWL_ERR(nic, "uCode data len %Zd too large to fit in\n",
  988. pieces.data_size);
  989. goto try_again;
  990. }
  991. if (pieces.init_size > cfg->max_inst_size) {
  992. IWL_ERR(nic, "uCode init instr len %Zd too large to fit in\n",
  993. pieces.init_size);
  994. goto try_again;
  995. }
  996. if (pieces.init_data_size > cfg->max_data_size) {
  997. IWL_ERR(nic, "uCode init data len %Zd too large to fit in\n",
  998. pieces.init_data_size);
  999. goto try_again;
  1000. }
  1001. /* Allocate ucode buffers for card's bus-master loading ... */
  1002. /* Runtime instructions and 2 copies of data:
  1003. * 1) unmodified from disk
  1004. * 2) backup cache for save/restore during power-downs */
  1005. if (iwl_alloc_fw_desc(nic, &nic->fw.ucode_rt.code,
  1006. pieces.inst, pieces.inst_size))
  1007. goto err_pci_alloc;
  1008. if (iwl_alloc_fw_desc(nic, &nic->fw.ucode_rt.data,
  1009. pieces.data, pieces.data_size))
  1010. goto err_pci_alloc;
  1011. /* Initialization instructions and data */
  1012. if (pieces.init_size && pieces.init_data_size) {
  1013. if (iwl_alloc_fw_desc(nic,
  1014. &nic->fw.ucode_init.code,
  1015. pieces.init, pieces.init_size))
  1016. goto err_pci_alloc;
  1017. if (iwl_alloc_fw_desc(nic,
  1018. &nic->fw.ucode_init.data,
  1019. pieces.init_data, pieces.init_data_size))
  1020. goto err_pci_alloc;
  1021. }
  1022. /* WoWLAN instructions and data */
  1023. if (pieces.wowlan_inst_size && pieces.wowlan_data_size) {
  1024. if (iwl_alloc_fw_desc(nic,
  1025. &nic->fw.ucode_wowlan.code,
  1026. pieces.wowlan_inst,
  1027. pieces.wowlan_inst_size))
  1028. goto err_pci_alloc;
  1029. if (iwl_alloc_fw_desc(nic,
  1030. &nic->fw.ucode_wowlan.data,
  1031. pieces.wowlan_data,
  1032. pieces.wowlan_data_size))
  1033. goto err_pci_alloc;
  1034. }
  1035. /* Now that we can no longer fail, copy information */
  1036. /*
  1037. * The (size - 16) / 12 formula is based on the information recorded
  1038. * for each event, which is of mode 1 (including timestamp) for all
  1039. * new microcodes that include this information.
  1040. */
  1041. nic->init_evtlog_ptr = pieces.init_evtlog_ptr;
  1042. if (pieces.init_evtlog_size)
  1043. nic->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
  1044. else
  1045. nic->init_evtlog_size =
  1046. cfg->base_params->max_event_log_size;
  1047. nic->init_errlog_ptr = pieces.init_errlog_ptr;
  1048. nic->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
  1049. if (pieces.inst_evtlog_size)
  1050. nic->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
  1051. else
  1052. nic->inst_evtlog_size =
  1053. cfg->base_params->max_event_log_size;
  1054. nic->inst_errlog_ptr = pieces.inst_errlog_ptr;
  1055. #ifndef CONFIG_IWLWIFI_P2P
  1056. fw->ucode_capa.flags &= ~IWL_UCODE_TLV_FLAGS_PAN;
  1057. #endif
  1058. priv->new_scan_threshold_behaviour =
  1059. !!(fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NEWSCAN);
  1060. if (!(cfg->sku & EEPROM_SKU_CAP_IPAN_ENABLE))
  1061. fw->ucode_capa.flags &= ~IWL_UCODE_TLV_FLAGS_PAN;
  1062. /*
  1063. * if not PAN, then don't support P2P -- might be a uCode
  1064. * packaging bug or due to the eeprom check above
  1065. */
  1066. if (!(fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN))
  1067. fw->ucode_capa.flags &= ~IWL_UCODE_TLV_FLAGS_P2P;
  1068. if (fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN) {
  1069. priv->sta_key_max_num = STA_KEY_MAX_NUM_PAN;
  1070. nic->shrd->cmd_queue = IWL_IPAN_CMD_QUEUE_NUM;
  1071. } else {
  1072. priv->sta_key_max_num = STA_KEY_MAX_NUM;
  1073. nic->shrd->cmd_queue = IWL_DEFAULT_CMD_QUEUE_NUM;
  1074. }
  1075. /*
  1076. * figure out the offset of chain noise reset and gain commands
  1077. * base on the size of standard phy calibration commands table size
  1078. */
  1079. if (fw->ucode_capa.standard_phy_calibration_size >
  1080. IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
  1081. fw->ucode_capa.standard_phy_calibration_size =
  1082. IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  1083. priv->phy_calib_chain_noise_reset_cmd =
  1084. fw->ucode_capa.standard_phy_calibration_size;
  1085. priv->phy_calib_chain_noise_gain_cmd =
  1086. fw->ucode_capa.standard_phy_calibration_size + 1;
  1087. /* initialize all valid contexts */
  1088. iwl_init_context(priv, fw->ucode_capa.flags);
  1089. /**************************************************
  1090. * This is still part of probe() in a sense...
  1091. *
  1092. * 9. Setup and register with mac80211 and debugfs
  1093. **************************************************/
  1094. err = iwlagn_mac_setup_register(priv, &fw->ucode_capa);
  1095. if (err)
  1096. goto out_unbind;
  1097. err = iwl_dbgfs_register(priv, DRV_NAME);
  1098. if (err)
  1099. IWL_ERR(nic,
  1100. "failed to create debugfs files. Ignoring error: %d\n",
  1101. err);
  1102. /* We have our copies now, allow OS release its copies */
  1103. release_firmware(ucode_raw);
  1104. complete(&nic->request_firmware_complete);
  1105. return;
  1106. try_again:
  1107. /* try next, if any */
  1108. if (iwl_request_firmware(nic, false))
  1109. goto out_unbind;
  1110. release_firmware(ucode_raw);
  1111. return;
  1112. err_pci_alloc:
  1113. IWL_ERR(nic, "failed to allocate pci memory\n");
  1114. iwl_dealloc_ucode(nic);
  1115. out_unbind:
  1116. complete(&nic->request_firmware_complete);
  1117. device_release_driver(trans(nic)->dev);
  1118. release_firmware(ucode_raw);
  1119. }