iwl-ucode.c 36 KB

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