iwl-drv.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2007 - 2012 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called LICENSE.GPL.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <linux/completion.h>
  64. #include <linux/dma-mapping.h>
  65. #include <linux/firmware.h>
  66. #include <linux/module.h>
  67. #include "iwl-drv.h"
  68. #include "iwl-trans.h"
  69. #include "iwl-shared.h"
  70. #include "iwl-op-mode.h"
  71. /* private includes */
  72. #include "iwl-fw-file.h"
  73. /**
  74. * struct iwl_drv - drv common data
  75. * @fw: the iwl_fw structure
  76. * @shrd: pointer to common shared structure
  77. * @op_mode: the running op_mode
  78. * @fw_index: firmware revision to try loading
  79. * @firmware_name: composite filename of ucode file to load
  80. * @request_firmware_complete: the firmware has been obtained from user space
  81. */
  82. struct iwl_drv {
  83. struct iwl_fw fw;
  84. struct iwl_shared *shrd;
  85. struct iwl_op_mode *op_mode;
  86. int fw_index; /* firmware we're trying to load */
  87. char firmware_name[25]; /* name of firmware file to load */
  88. struct completion request_firmware_complete;
  89. };
  90. static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
  91. {
  92. if (desc->v_addr)
  93. dma_free_coherent(trans(drv)->dev, desc->len,
  94. desc->v_addr, desc->p_addr);
  95. desc->v_addr = NULL;
  96. desc->len = 0;
  97. }
  98. static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
  99. {
  100. iwl_free_fw_desc(drv, &img->code);
  101. iwl_free_fw_desc(drv, &img->data);
  102. }
  103. static void iwl_dealloc_ucode(struct iwl_drv *drv)
  104. {
  105. iwl_free_fw_img(drv, &drv->fw.ucode_rt);
  106. iwl_free_fw_img(drv, &drv->fw.ucode_init);
  107. iwl_free_fw_img(drv, &drv->fw.ucode_wowlan);
  108. }
  109. static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
  110. const void *data, size_t len)
  111. {
  112. if (!len) {
  113. desc->v_addr = NULL;
  114. return -EINVAL;
  115. }
  116. desc->v_addr = dma_alloc_coherent(trans(drv)->dev, len,
  117. &desc->p_addr, GFP_KERNEL);
  118. if (!desc->v_addr)
  119. return -ENOMEM;
  120. desc->len = len;
  121. memcpy(desc->v_addr, data, len);
  122. return 0;
  123. }
  124. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
  125. #define UCODE_EXPERIMENTAL_INDEX 100
  126. #define UCODE_EXPERIMENTAL_TAG "exp"
  127. static int iwl_request_firmware(struct iwl_drv *drv, bool first)
  128. {
  129. const struct iwl_cfg *cfg = cfg(drv);
  130. const char *name_pre = cfg->fw_name_pre;
  131. char tag[8];
  132. if (first) {
  133. #ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
  134. drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
  135. strcpy(tag, UCODE_EXPERIMENTAL_TAG);
  136. } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
  137. #endif
  138. drv->fw_index = cfg->ucode_api_max;
  139. sprintf(tag, "%d", drv->fw_index);
  140. } else {
  141. drv->fw_index--;
  142. sprintf(tag, "%d", drv->fw_index);
  143. }
  144. if (drv->fw_index < cfg->ucode_api_min) {
  145. IWL_ERR(drv, "no suitable firmware found!\n");
  146. return -ENOENT;
  147. }
  148. sprintf(drv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
  149. IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
  150. (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  151. ? "EXPERIMENTAL " : "",
  152. drv->firmware_name);
  153. return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
  154. trans(drv)->dev,
  155. GFP_KERNEL, drv, iwl_ucode_callback);
  156. }
  157. struct iwlagn_firmware_pieces {
  158. const void *inst, *data, *init, *init_data, *wowlan_inst, *wowlan_data;
  159. size_t inst_size, data_size, init_size, init_data_size,
  160. wowlan_inst_size, wowlan_data_size;
  161. u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
  162. u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
  163. };
  164. static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
  165. const struct firmware *ucode_raw,
  166. struct iwlagn_firmware_pieces *pieces)
  167. {
  168. struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
  169. u32 api_ver, hdr_size, build;
  170. char buildstr[25];
  171. const u8 *src;
  172. drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
  173. api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
  174. switch (api_ver) {
  175. default:
  176. hdr_size = 28;
  177. if (ucode_raw->size < hdr_size) {
  178. IWL_ERR(drv, "File size too small!\n");
  179. return -EINVAL;
  180. }
  181. build = le32_to_cpu(ucode->u.v2.build);
  182. pieces->inst_size = le32_to_cpu(ucode->u.v2.inst_size);
  183. pieces->data_size = le32_to_cpu(ucode->u.v2.data_size);
  184. pieces->init_size = le32_to_cpu(ucode->u.v2.init_size);
  185. pieces->init_data_size =
  186. le32_to_cpu(ucode->u.v2.init_data_size);
  187. src = ucode->u.v2.data;
  188. break;
  189. case 0:
  190. case 1:
  191. case 2:
  192. hdr_size = 24;
  193. if (ucode_raw->size < hdr_size) {
  194. IWL_ERR(drv, "File size too small!\n");
  195. return -EINVAL;
  196. }
  197. build = 0;
  198. pieces->inst_size = le32_to_cpu(ucode->u.v1.inst_size);
  199. pieces->data_size = le32_to_cpu(ucode->u.v1.data_size);
  200. pieces->init_size = le32_to_cpu(ucode->u.v1.init_size);
  201. pieces->init_data_size =
  202. le32_to_cpu(ucode->u.v1.init_data_size);
  203. src = ucode->u.v1.data;
  204. break;
  205. }
  206. if (build)
  207. sprintf(buildstr, " build %u%s", build,
  208. (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  209. ? " (EXP)" : "");
  210. else
  211. buildstr[0] = '\0';
  212. snprintf(drv->fw.fw_version,
  213. sizeof(drv->fw.fw_version),
  214. "%u.%u.%u.%u%s",
  215. IWL_UCODE_MAJOR(drv->fw.ucode_ver),
  216. IWL_UCODE_MINOR(drv->fw.ucode_ver),
  217. IWL_UCODE_API(drv->fw.ucode_ver),
  218. IWL_UCODE_SERIAL(drv->fw.ucode_ver),
  219. buildstr);
  220. /* Verify size of file vs. image size info in file's header */
  221. if (ucode_raw->size != hdr_size + pieces->inst_size +
  222. pieces->data_size + pieces->init_size +
  223. pieces->init_data_size) {
  224. IWL_ERR(drv,
  225. "uCode file size %d does not match expected size\n",
  226. (int)ucode_raw->size);
  227. return -EINVAL;
  228. }
  229. pieces->inst = src;
  230. src += pieces->inst_size;
  231. pieces->data = src;
  232. src += pieces->data_size;
  233. pieces->init = src;
  234. src += pieces->init_size;
  235. pieces->init_data = src;
  236. src += pieces->init_data_size;
  237. return 0;
  238. }
  239. static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
  240. const struct firmware *ucode_raw,
  241. struct iwlagn_firmware_pieces *pieces,
  242. struct iwl_ucode_capabilities *capa)
  243. {
  244. struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
  245. struct iwl_ucode_tlv *tlv;
  246. size_t len = ucode_raw->size;
  247. const u8 *data;
  248. int wanted_alternative = iwlagn_mod_params.wanted_ucode_alternative;
  249. int tmp;
  250. u64 alternatives;
  251. u32 tlv_len;
  252. enum iwl_ucode_tlv_type tlv_type;
  253. const u8 *tlv_data;
  254. char buildstr[25];
  255. u32 build;
  256. if (len < sizeof(*ucode)) {
  257. IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
  258. return -EINVAL;
  259. }
  260. if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
  261. IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
  262. le32_to_cpu(ucode->magic));
  263. return -EINVAL;
  264. }
  265. /*
  266. * Check which alternatives are present, and "downgrade"
  267. * when the chosen alternative is not present, warning
  268. * the user when that happens. Some files may not have
  269. * any alternatives, so don't warn in that case.
  270. */
  271. alternatives = le64_to_cpu(ucode->alternatives);
  272. tmp = wanted_alternative;
  273. if (wanted_alternative > 63)
  274. wanted_alternative = 63;
  275. while (wanted_alternative && !(alternatives & BIT(wanted_alternative)))
  276. wanted_alternative--;
  277. if (wanted_alternative && wanted_alternative != tmp)
  278. IWL_WARN(drv,
  279. "uCode alternative %d not available, choosing %d\n",
  280. tmp, wanted_alternative);
  281. drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
  282. build = le32_to_cpu(ucode->build);
  283. if (build)
  284. sprintf(buildstr, " build %u%s", build,
  285. (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  286. ? " (EXP)" : "");
  287. else
  288. buildstr[0] = '\0';
  289. snprintf(drv->fw.fw_version,
  290. sizeof(drv->fw.fw_version),
  291. "%u.%u.%u.%u%s",
  292. IWL_UCODE_MAJOR(drv->fw.ucode_ver),
  293. IWL_UCODE_MINOR(drv->fw.ucode_ver),
  294. IWL_UCODE_API(drv->fw.ucode_ver),
  295. IWL_UCODE_SERIAL(drv->fw.ucode_ver),
  296. buildstr);
  297. data = ucode->data;
  298. len -= sizeof(*ucode);
  299. while (len >= sizeof(*tlv)) {
  300. u16 tlv_alt;
  301. len -= sizeof(*tlv);
  302. tlv = (void *)data;
  303. tlv_len = le32_to_cpu(tlv->length);
  304. tlv_type = le16_to_cpu(tlv->type);
  305. tlv_alt = le16_to_cpu(tlv->alternative);
  306. tlv_data = tlv->data;
  307. if (len < tlv_len) {
  308. IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
  309. len, tlv_len);
  310. return -EINVAL;
  311. }
  312. len -= ALIGN(tlv_len, 4);
  313. data += sizeof(*tlv) + ALIGN(tlv_len, 4);
  314. /*
  315. * Alternative 0 is always valid.
  316. *
  317. * Skip alternative TLVs that are not selected.
  318. */
  319. if (tlv_alt != 0 && tlv_alt != wanted_alternative)
  320. continue;
  321. switch (tlv_type) {
  322. case IWL_UCODE_TLV_INST:
  323. pieces->inst = tlv_data;
  324. pieces->inst_size = tlv_len;
  325. break;
  326. case IWL_UCODE_TLV_DATA:
  327. pieces->data = tlv_data;
  328. pieces->data_size = tlv_len;
  329. break;
  330. case IWL_UCODE_TLV_INIT:
  331. pieces->init = tlv_data;
  332. pieces->init_size = tlv_len;
  333. break;
  334. case IWL_UCODE_TLV_INIT_DATA:
  335. pieces->init_data = tlv_data;
  336. pieces->init_data_size = tlv_len;
  337. break;
  338. case IWL_UCODE_TLV_BOOT:
  339. IWL_ERR(drv, "Found unexpected BOOT ucode\n");
  340. break;
  341. case IWL_UCODE_TLV_PROBE_MAX_LEN:
  342. if (tlv_len != sizeof(u32))
  343. goto invalid_tlv_len;
  344. capa->max_probe_length =
  345. le32_to_cpup((__le32 *)tlv_data);
  346. break;
  347. case IWL_UCODE_TLV_PAN:
  348. if (tlv_len)
  349. goto invalid_tlv_len;
  350. capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
  351. break;
  352. case IWL_UCODE_TLV_FLAGS:
  353. /* must be at least one u32 */
  354. if (tlv_len < sizeof(u32))
  355. goto invalid_tlv_len;
  356. /* and a proper number of u32s */
  357. if (tlv_len % sizeof(u32))
  358. goto invalid_tlv_len;
  359. /*
  360. * This driver only reads the first u32 as
  361. * right now no more features are defined,
  362. * if that changes then either the driver
  363. * will not work with the new firmware, or
  364. * it'll not take advantage of new features.
  365. */
  366. capa->flags = le32_to_cpup((__le32 *)tlv_data);
  367. break;
  368. case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
  369. if (tlv_len != sizeof(u32))
  370. goto invalid_tlv_len;
  371. pieces->init_evtlog_ptr =
  372. le32_to_cpup((__le32 *)tlv_data);
  373. break;
  374. case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
  375. if (tlv_len != sizeof(u32))
  376. goto invalid_tlv_len;
  377. pieces->init_evtlog_size =
  378. le32_to_cpup((__le32 *)tlv_data);
  379. break;
  380. case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
  381. if (tlv_len != sizeof(u32))
  382. goto invalid_tlv_len;
  383. pieces->init_errlog_ptr =
  384. le32_to_cpup((__le32 *)tlv_data);
  385. break;
  386. case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
  387. if (tlv_len != sizeof(u32))
  388. goto invalid_tlv_len;
  389. pieces->inst_evtlog_ptr =
  390. le32_to_cpup((__le32 *)tlv_data);
  391. break;
  392. case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
  393. if (tlv_len != sizeof(u32))
  394. goto invalid_tlv_len;
  395. pieces->inst_evtlog_size =
  396. le32_to_cpup((__le32 *)tlv_data);
  397. break;
  398. case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
  399. if (tlv_len != sizeof(u32))
  400. goto invalid_tlv_len;
  401. pieces->inst_errlog_ptr =
  402. le32_to_cpup((__le32 *)tlv_data);
  403. break;
  404. case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
  405. if (tlv_len)
  406. goto invalid_tlv_len;
  407. drv->fw.enhance_sensitivity_table = true;
  408. break;
  409. case IWL_UCODE_TLV_WOWLAN_INST:
  410. pieces->wowlan_inst = tlv_data;
  411. pieces->wowlan_inst_size = tlv_len;
  412. break;
  413. case IWL_UCODE_TLV_WOWLAN_DATA:
  414. pieces->wowlan_data = tlv_data;
  415. pieces->wowlan_data_size = tlv_len;
  416. break;
  417. case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
  418. if (tlv_len != sizeof(u32))
  419. goto invalid_tlv_len;
  420. capa->standard_phy_calibration_size =
  421. le32_to_cpup((__le32 *)tlv_data);
  422. break;
  423. default:
  424. IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
  425. break;
  426. }
  427. }
  428. if (len) {
  429. IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
  430. iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
  431. return -EINVAL;
  432. }
  433. return 0;
  434. invalid_tlv_len:
  435. IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
  436. iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
  437. return -EINVAL;
  438. }
  439. /**
  440. * iwl_ucode_callback - callback when firmware was loaded
  441. *
  442. * If loaded successfully, copies the firmware into buffers
  443. * for the card to fetch (via DMA).
  444. */
  445. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
  446. {
  447. struct iwl_drv *drv = context;
  448. const struct iwl_cfg *cfg = cfg(drv);
  449. struct iwl_fw *fw = &drv->fw;
  450. struct iwl_ucode_header *ucode;
  451. int err;
  452. struct iwlagn_firmware_pieces pieces;
  453. const unsigned int api_max = cfg->ucode_api_max;
  454. unsigned int api_ok = cfg->ucode_api_ok;
  455. const unsigned int api_min = cfg->ucode_api_min;
  456. u32 api_ver;
  457. fw->ucode_capa.max_probe_length = 200;
  458. fw->ucode_capa.standard_phy_calibration_size =
  459. IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  460. if (!api_ok)
  461. api_ok = api_max;
  462. memset(&pieces, 0, sizeof(pieces));
  463. if (!ucode_raw) {
  464. if (drv->fw_index <= api_ok)
  465. IWL_ERR(drv,
  466. "request for firmware file '%s' failed.\n",
  467. drv->firmware_name);
  468. goto try_again;
  469. }
  470. IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
  471. drv->firmware_name, ucode_raw->size);
  472. /* Make sure that we got at least the API version number */
  473. if (ucode_raw->size < 4) {
  474. IWL_ERR(drv, "File size way too small!\n");
  475. goto try_again;
  476. }
  477. /* Data from ucode file: header followed by uCode images */
  478. ucode = (struct iwl_ucode_header *)ucode_raw->data;
  479. if (ucode->ver)
  480. err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
  481. else
  482. err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
  483. &fw->ucode_capa);
  484. if (err)
  485. goto try_again;
  486. api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
  487. /*
  488. * api_ver should match the api version forming part of the
  489. * firmware filename ... but we don't check for that and only rely
  490. * on the API version read from firmware header from here on forward
  491. */
  492. /* no api version check required for experimental uCode */
  493. if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
  494. if (api_ver < api_min || api_ver > api_max) {
  495. IWL_ERR(drv,
  496. "Driver unable to support your firmware API. "
  497. "Driver supports v%u, firmware is v%u.\n",
  498. api_max, api_ver);
  499. goto try_again;
  500. }
  501. if (api_ver < api_ok) {
  502. if (api_ok != api_max)
  503. IWL_ERR(drv, "Firmware has old API version, "
  504. "expected v%u through v%u, got v%u.\n",
  505. api_ok, api_max, api_ver);
  506. else
  507. IWL_ERR(drv, "Firmware has old API version, "
  508. "expected v%u, got v%u.\n",
  509. api_max, api_ver);
  510. IWL_ERR(drv, "New firmware can be obtained from "
  511. "http://www.intellinuxwireless.org/.\n");
  512. }
  513. }
  514. IWL_INFO(drv, "loaded firmware version %s", drv->fw.fw_version);
  515. /*
  516. * For any of the failures below (before allocating pci memory)
  517. * we will try to load a version with a smaller API -- maybe the
  518. * user just got a corrupted version of the latest API.
  519. */
  520. IWL_DEBUG_INFO(drv, "f/w package hdr ucode version raw = 0x%x\n",
  521. drv->fw.ucode_ver);
  522. IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
  523. pieces.inst_size);
  524. IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
  525. pieces.data_size);
  526. IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
  527. pieces.init_size);
  528. IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
  529. pieces.init_data_size);
  530. /* Verify that uCode images will fit in card's SRAM */
  531. if (pieces.inst_size > cfg->max_inst_size) {
  532. IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
  533. pieces.inst_size);
  534. goto try_again;
  535. }
  536. if (pieces.data_size > cfg->max_data_size) {
  537. IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
  538. pieces.data_size);
  539. goto try_again;
  540. }
  541. if (pieces.init_size > cfg->max_inst_size) {
  542. IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
  543. pieces.init_size);
  544. goto try_again;
  545. }
  546. if (pieces.init_data_size > cfg->max_data_size) {
  547. IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
  548. pieces.init_data_size);
  549. goto try_again;
  550. }
  551. /* Allocate ucode buffers for card's bus-master loading ... */
  552. /* Runtime instructions and 2 copies of data:
  553. * 1) unmodified from disk
  554. * 2) backup cache for save/restore during power-downs */
  555. if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_rt.code,
  556. pieces.inst, pieces.inst_size))
  557. goto err_pci_alloc;
  558. if (iwl_alloc_fw_desc(drv, &drv->fw.ucode_rt.data,
  559. pieces.data, pieces.data_size))
  560. goto err_pci_alloc;
  561. /* Initialization instructions and data */
  562. if (pieces.init_size && pieces.init_data_size) {
  563. if (iwl_alloc_fw_desc(drv,
  564. &drv->fw.ucode_init.code,
  565. pieces.init, pieces.init_size))
  566. goto err_pci_alloc;
  567. if (iwl_alloc_fw_desc(drv,
  568. &drv->fw.ucode_init.data,
  569. pieces.init_data, pieces.init_data_size))
  570. goto err_pci_alloc;
  571. }
  572. /* WoWLAN instructions and data */
  573. if (pieces.wowlan_inst_size && pieces.wowlan_data_size) {
  574. if (iwl_alloc_fw_desc(drv,
  575. &drv->fw.ucode_wowlan.code,
  576. pieces.wowlan_inst,
  577. pieces.wowlan_inst_size))
  578. goto err_pci_alloc;
  579. if (iwl_alloc_fw_desc(drv,
  580. &drv->fw.ucode_wowlan.data,
  581. pieces.wowlan_data,
  582. pieces.wowlan_data_size))
  583. goto err_pci_alloc;
  584. }
  585. /* Now that we can no longer fail, copy information */
  586. /*
  587. * The (size - 16) / 12 formula is based on the information recorded
  588. * for each event, which is of mode 1 (including timestamp) for all
  589. * new microcodes that include this information.
  590. */
  591. fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
  592. if (pieces.init_evtlog_size)
  593. fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
  594. else
  595. fw->init_evtlog_size =
  596. cfg->base_params->max_event_log_size;
  597. fw->init_errlog_ptr = pieces.init_errlog_ptr;
  598. fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
  599. if (pieces.inst_evtlog_size)
  600. fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
  601. else
  602. fw->inst_evtlog_size =
  603. cfg->base_params->max_event_log_size;
  604. fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
  605. /*
  606. * figure out the offset of chain noise reset and gain commands
  607. * base on the size of standard phy calibration commands table size
  608. */
  609. if (fw->ucode_capa.standard_phy_calibration_size >
  610. IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
  611. fw->ucode_capa.standard_phy_calibration_size =
  612. IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  613. /* We have our copies now, allow OS release its copies */
  614. release_firmware(ucode_raw);
  615. complete(&drv->request_firmware_complete);
  616. drv->op_mode = iwl_dvm_ops.start(drv->shrd->trans, &drv->fw);
  617. if (!drv->op_mode)
  618. goto out_unbind;
  619. return;
  620. try_again:
  621. /* try next, if any */
  622. release_firmware(ucode_raw);
  623. if (iwl_request_firmware(drv, false))
  624. goto out_unbind;
  625. return;
  626. err_pci_alloc:
  627. IWL_ERR(drv, "failed to allocate pci memory\n");
  628. iwl_dealloc_ucode(drv);
  629. release_firmware(ucode_raw);
  630. out_unbind:
  631. complete(&drv->request_firmware_complete);
  632. device_release_driver(trans(drv)->dev);
  633. }
  634. int iwl_drv_start(struct iwl_shared *shrd,
  635. struct iwl_trans *trans, const struct iwl_cfg *cfg)
  636. {
  637. struct iwl_drv *drv;
  638. int ret;
  639. shrd->cfg = cfg;
  640. drv = kzalloc(sizeof(*drv), GFP_KERNEL);
  641. if (!drv) {
  642. dev_printk(KERN_ERR, trans->dev, "Couldn't allocate iwl_drv");
  643. return -ENOMEM;
  644. }
  645. drv->shrd = shrd;
  646. shrd->drv = drv;
  647. init_completion(&drv->request_firmware_complete);
  648. ret = iwl_request_firmware(drv, true);
  649. if (ret) {
  650. dev_printk(KERN_ERR, trans->dev, "Couldn't request the fw");
  651. kfree(drv);
  652. shrd->drv = NULL;
  653. }
  654. return ret;
  655. }
  656. void iwl_drv_stop(struct iwl_shared *shrd)
  657. {
  658. struct iwl_drv *drv = shrd->drv;
  659. wait_for_completion(&drv->request_firmware_complete);
  660. /* op_mode can be NULL if its start failed */
  661. if (drv->op_mode)
  662. iwl_op_mode_stop(drv->op_mode);
  663. iwl_dealloc_ucode(drv);
  664. kfree(drv);
  665. shrd->drv = NULL;
  666. }