iwl-drv.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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-wifi.h"
  70. #include "iwl-shared.h"
  71. #include "iwl-op-mode.h"
  72. static void iwl_free_fw_desc(struct iwl_nic *nic, struct fw_desc *desc)
  73. {
  74. if (desc->v_addr)
  75. dma_free_coherent(trans(nic)->dev, desc->len,
  76. desc->v_addr, desc->p_addr);
  77. desc->v_addr = NULL;
  78. desc->len = 0;
  79. }
  80. static void iwl_free_fw_img(struct iwl_nic *nic, struct fw_img *img)
  81. {
  82. iwl_free_fw_desc(nic, &img->code);
  83. iwl_free_fw_desc(nic, &img->data);
  84. }
  85. static void iwl_dealloc_ucode(struct iwl_nic *nic)
  86. {
  87. iwl_free_fw_img(nic, &nic->fw.ucode_rt);
  88. iwl_free_fw_img(nic, &nic->fw.ucode_init);
  89. iwl_free_fw_img(nic, &nic->fw.ucode_wowlan);
  90. }
  91. static int iwl_alloc_fw_desc(struct iwl_nic *nic, struct fw_desc *desc,
  92. const void *data, size_t len)
  93. {
  94. if (!len) {
  95. desc->v_addr = NULL;
  96. return -EINVAL;
  97. }
  98. desc->v_addr = dma_alloc_coherent(trans(nic)->dev, len,
  99. &desc->p_addr, GFP_KERNEL);
  100. if (!desc->v_addr)
  101. return -ENOMEM;
  102. desc->len = len;
  103. memcpy(desc->v_addr, data, len);
  104. return 0;
  105. }
  106. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
  107. #define UCODE_EXPERIMENTAL_INDEX 100
  108. #define UCODE_EXPERIMENTAL_TAG "exp"
  109. static int iwl_request_firmware(struct iwl_nic *nic, bool first)
  110. {
  111. const struct iwl_cfg *cfg = cfg(nic);
  112. const char *name_pre = cfg->fw_name_pre;
  113. char tag[8];
  114. if (first) {
  115. #ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
  116. nic->fw_index = UCODE_EXPERIMENTAL_INDEX;
  117. strcpy(tag, UCODE_EXPERIMENTAL_TAG);
  118. } else if (nic->fw_index == UCODE_EXPERIMENTAL_INDEX) {
  119. #endif
  120. nic->fw_index = cfg->ucode_api_max;
  121. sprintf(tag, "%d", nic->fw_index);
  122. } else {
  123. nic->fw_index--;
  124. sprintf(tag, "%d", nic->fw_index);
  125. }
  126. if (nic->fw_index < cfg->ucode_api_min) {
  127. IWL_ERR(nic, "no suitable firmware found!\n");
  128. return -ENOENT;
  129. }
  130. sprintf(nic->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
  131. IWL_DEBUG_INFO(nic, "attempting to load firmware %s'%s'\n",
  132. (nic->fw_index == UCODE_EXPERIMENTAL_INDEX)
  133. ? "EXPERIMENTAL " : "",
  134. nic->firmware_name);
  135. return request_firmware_nowait(THIS_MODULE, 1, nic->firmware_name,
  136. trans(nic)->dev,
  137. GFP_KERNEL, nic, iwl_ucode_callback);
  138. }
  139. struct iwlagn_firmware_pieces {
  140. const void *inst, *data, *init, *init_data, *wowlan_inst, *wowlan_data;
  141. size_t inst_size, data_size, init_size, init_data_size,
  142. wowlan_inst_size, wowlan_data_size;
  143. u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
  144. u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
  145. };
  146. static int iwl_parse_v1_v2_firmware(struct iwl_nic *nic,
  147. const struct firmware *ucode_raw,
  148. struct iwlagn_firmware_pieces *pieces)
  149. {
  150. struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
  151. u32 api_ver, hdr_size, build;
  152. char buildstr[25];
  153. const u8 *src;
  154. nic->fw.ucode_ver = le32_to_cpu(ucode->ver);
  155. api_ver = IWL_UCODE_API(nic->fw.ucode_ver);
  156. switch (api_ver) {
  157. default:
  158. hdr_size = 28;
  159. if (ucode_raw->size < hdr_size) {
  160. IWL_ERR(nic, "File size too small!\n");
  161. return -EINVAL;
  162. }
  163. build = le32_to_cpu(ucode->u.v2.build);
  164. pieces->inst_size = le32_to_cpu(ucode->u.v2.inst_size);
  165. pieces->data_size = le32_to_cpu(ucode->u.v2.data_size);
  166. pieces->init_size = le32_to_cpu(ucode->u.v2.init_size);
  167. pieces->init_data_size =
  168. le32_to_cpu(ucode->u.v2.init_data_size);
  169. src = ucode->u.v2.data;
  170. break;
  171. case 0:
  172. case 1:
  173. case 2:
  174. hdr_size = 24;
  175. if (ucode_raw->size < hdr_size) {
  176. IWL_ERR(nic, "File size too small!\n");
  177. return -EINVAL;
  178. }
  179. build = 0;
  180. pieces->inst_size = le32_to_cpu(ucode->u.v1.inst_size);
  181. pieces->data_size = le32_to_cpu(ucode->u.v1.data_size);
  182. pieces->init_size = le32_to_cpu(ucode->u.v1.init_size);
  183. pieces->init_data_size =
  184. le32_to_cpu(ucode->u.v1.init_data_size);
  185. src = ucode->u.v1.data;
  186. break;
  187. }
  188. if (build)
  189. sprintf(buildstr, " build %u%s", build,
  190. (nic->fw_index == UCODE_EXPERIMENTAL_INDEX)
  191. ? " (EXP)" : "");
  192. else
  193. buildstr[0] = '\0';
  194. snprintf(nic->fw.fw_version,
  195. sizeof(nic->fw.fw_version),
  196. "%u.%u.%u.%u%s",
  197. IWL_UCODE_MAJOR(nic->fw.ucode_ver),
  198. IWL_UCODE_MINOR(nic->fw.ucode_ver),
  199. IWL_UCODE_API(nic->fw.ucode_ver),
  200. IWL_UCODE_SERIAL(nic->fw.ucode_ver),
  201. buildstr);
  202. /* Verify size of file vs. image size info in file's header */
  203. if (ucode_raw->size != hdr_size + pieces->inst_size +
  204. pieces->data_size + pieces->init_size +
  205. pieces->init_data_size) {
  206. IWL_ERR(nic,
  207. "uCode file size %d does not match expected size\n",
  208. (int)ucode_raw->size);
  209. return -EINVAL;
  210. }
  211. pieces->inst = src;
  212. src += pieces->inst_size;
  213. pieces->data = src;
  214. src += pieces->data_size;
  215. pieces->init = src;
  216. src += pieces->init_size;
  217. pieces->init_data = src;
  218. src += pieces->init_data_size;
  219. return 0;
  220. }
  221. static int iwl_parse_tlv_firmware(struct iwl_nic *nic,
  222. const struct firmware *ucode_raw,
  223. struct iwlagn_firmware_pieces *pieces,
  224. struct iwl_ucode_capabilities *capa)
  225. {
  226. struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
  227. struct iwl_ucode_tlv *tlv;
  228. size_t len = ucode_raw->size;
  229. const u8 *data;
  230. int wanted_alternative = iwlagn_mod_params.wanted_ucode_alternative;
  231. int tmp;
  232. u64 alternatives;
  233. u32 tlv_len;
  234. enum iwl_ucode_tlv_type tlv_type;
  235. const u8 *tlv_data;
  236. char buildstr[25];
  237. u32 build;
  238. if (len < sizeof(*ucode)) {
  239. IWL_ERR(nic, "uCode has invalid length: %zd\n", len);
  240. return -EINVAL;
  241. }
  242. if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
  243. IWL_ERR(nic, "invalid uCode magic: 0X%x\n",
  244. le32_to_cpu(ucode->magic));
  245. return -EINVAL;
  246. }
  247. /*
  248. * Check which alternatives are present, and "downgrade"
  249. * when the chosen alternative is not present, warning
  250. * the user when that happens. Some files may not have
  251. * any alternatives, so don't warn in that case.
  252. */
  253. alternatives = le64_to_cpu(ucode->alternatives);
  254. tmp = wanted_alternative;
  255. if (wanted_alternative > 63)
  256. wanted_alternative = 63;
  257. while (wanted_alternative && !(alternatives & BIT(wanted_alternative)))
  258. wanted_alternative--;
  259. if (wanted_alternative && wanted_alternative != tmp)
  260. IWL_WARN(nic,
  261. "uCode alternative %d not available, choosing %d\n",
  262. tmp, wanted_alternative);
  263. nic->fw.ucode_ver = le32_to_cpu(ucode->ver);
  264. build = le32_to_cpu(ucode->build);
  265. if (build)
  266. sprintf(buildstr, " build %u%s", build,
  267. (nic->fw_index == UCODE_EXPERIMENTAL_INDEX)
  268. ? " (EXP)" : "");
  269. else
  270. buildstr[0] = '\0';
  271. snprintf(nic->fw.fw_version,
  272. sizeof(nic->fw.fw_version),
  273. "%u.%u.%u.%u%s",
  274. IWL_UCODE_MAJOR(nic->fw.ucode_ver),
  275. IWL_UCODE_MINOR(nic->fw.ucode_ver),
  276. IWL_UCODE_API(nic->fw.ucode_ver),
  277. IWL_UCODE_SERIAL(nic->fw.ucode_ver),
  278. buildstr);
  279. data = ucode->data;
  280. len -= sizeof(*ucode);
  281. while (len >= sizeof(*tlv)) {
  282. u16 tlv_alt;
  283. len -= sizeof(*tlv);
  284. tlv = (void *)data;
  285. tlv_len = le32_to_cpu(tlv->length);
  286. tlv_type = le16_to_cpu(tlv->type);
  287. tlv_alt = le16_to_cpu(tlv->alternative);
  288. tlv_data = tlv->data;
  289. if (len < tlv_len) {
  290. IWL_ERR(nic, "invalid TLV len: %zd/%u\n",
  291. len, tlv_len);
  292. return -EINVAL;
  293. }
  294. len -= ALIGN(tlv_len, 4);
  295. data += sizeof(*tlv) + ALIGN(tlv_len, 4);
  296. /*
  297. * Alternative 0 is always valid.
  298. *
  299. * Skip alternative TLVs that are not selected.
  300. */
  301. if (tlv_alt != 0 && tlv_alt != wanted_alternative)
  302. continue;
  303. switch (tlv_type) {
  304. case IWL_UCODE_TLV_INST:
  305. pieces->inst = tlv_data;
  306. pieces->inst_size = tlv_len;
  307. break;
  308. case IWL_UCODE_TLV_DATA:
  309. pieces->data = tlv_data;
  310. pieces->data_size = tlv_len;
  311. break;
  312. case IWL_UCODE_TLV_INIT:
  313. pieces->init = tlv_data;
  314. pieces->init_size = tlv_len;
  315. break;
  316. case IWL_UCODE_TLV_INIT_DATA:
  317. pieces->init_data = tlv_data;
  318. pieces->init_data_size = tlv_len;
  319. break;
  320. case IWL_UCODE_TLV_BOOT:
  321. IWL_ERR(nic, "Found unexpected BOOT ucode\n");
  322. break;
  323. case IWL_UCODE_TLV_PROBE_MAX_LEN:
  324. if (tlv_len != sizeof(u32))
  325. goto invalid_tlv_len;
  326. capa->max_probe_length =
  327. le32_to_cpup((__le32 *)tlv_data);
  328. break;
  329. case IWL_UCODE_TLV_PAN:
  330. if (tlv_len)
  331. goto invalid_tlv_len;
  332. capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
  333. break;
  334. case IWL_UCODE_TLV_FLAGS:
  335. /* must be at least one u32 */
  336. if (tlv_len < sizeof(u32))
  337. goto invalid_tlv_len;
  338. /* and a proper number of u32s */
  339. if (tlv_len % sizeof(u32))
  340. goto invalid_tlv_len;
  341. /*
  342. * This driver only reads the first u32 as
  343. * right now no more features are defined,
  344. * if that changes then either the driver
  345. * will not work with the new firmware, or
  346. * it'll not take advantage of new features.
  347. */
  348. capa->flags = le32_to_cpup((__le32 *)tlv_data);
  349. break;
  350. case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
  351. if (tlv_len != sizeof(u32))
  352. goto invalid_tlv_len;
  353. pieces->init_evtlog_ptr =
  354. le32_to_cpup((__le32 *)tlv_data);
  355. break;
  356. case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
  357. if (tlv_len != sizeof(u32))
  358. goto invalid_tlv_len;
  359. pieces->init_evtlog_size =
  360. le32_to_cpup((__le32 *)tlv_data);
  361. break;
  362. case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
  363. if (tlv_len != sizeof(u32))
  364. goto invalid_tlv_len;
  365. pieces->init_errlog_ptr =
  366. le32_to_cpup((__le32 *)tlv_data);
  367. break;
  368. case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
  369. if (tlv_len != sizeof(u32))
  370. goto invalid_tlv_len;
  371. pieces->inst_evtlog_ptr =
  372. le32_to_cpup((__le32 *)tlv_data);
  373. break;
  374. case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
  375. if (tlv_len != sizeof(u32))
  376. goto invalid_tlv_len;
  377. pieces->inst_evtlog_size =
  378. le32_to_cpup((__le32 *)tlv_data);
  379. break;
  380. case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
  381. if (tlv_len != sizeof(u32))
  382. goto invalid_tlv_len;
  383. pieces->inst_errlog_ptr =
  384. le32_to_cpup((__le32 *)tlv_data);
  385. break;
  386. case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
  387. if (tlv_len)
  388. goto invalid_tlv_len;
  389. nic->fw.enhance_sensitivity_table = true;
  390. break;
  391. case IWL_UCODE_TLV_WOWLAN_INST:
  392. pieces->wowlan_inst = tlv_data;
  393. pieces->wowlan_inst_size = tlv_len;
  394. break;
  395. case IWL_UCODE_TLV_WOWLAN_DATA:
  396. pieces->wowlan_data = tlv_data;
  397. pieces->wowlan_data_size = tlv_len;
  398. break;
  399. case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
  400. if (tlv_len != sizeof(u32))
  401. goto invalid_tlv_len;
  402. capa->standard_phy_calibration_size =
  403. le32_to_cpup((__le32 *)tlv_data);
  404. break;
  405. default:
  406. IWL_DEBUG_INFO(nic, "unknown TLV: %d\n", tlv_type);
  407. break;
  408. }
  409. }
  410. if (len) {
  411. IWL_ERR(nic, "invalid TLV after parsing: %zd\n", len);
  412. iwl_print_hex_dump(nic, IWL_DL_FW, (u8 *)data, len);
  413. return -EINVAL;
  414. }
  415. return 0;
  416. invalid_tlv_len:
  417. IWL_ERR(nic, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
  418. iwl_print_hex_dump(nic, IWL_DL_FW, tlv_data, tlv_len);
  419. return -EINVAL;
  420. }
  421. /**
  422. * iwl_ucode_callback - callback when firmware was loaded
  423. *
  424. * If loaded successfully, copies the firmware into buffers
  425. * for the card to fetch (via DMA).
  426. */
  427. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
  428. {
  429. struct iwl_nic *nic = context;
  430. const struct iwl_cfg *cfg = cfg(nic);
  431. struct iwl_fw *fw = &nic->fw;
  432. struct iwl_ucode_header *ucode;
  433. int err;
  434. struct iwlagn_firmware_pieces pieces;
  435. const unsigned int api_max = cfg->ucode_api_max;
  436. unsigned int api_ok = cfg->ucode_api_ok;
  437. const unsigned int api_min = cfg->ucode_api_min;
  438. u32 api_ver;
  439. fw->ucode_capa.max_probe_length = 200;
  440. fw->ucode_capa.standard_phy_calibration_size =
  441. IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  442. if (!api_ok)
  443. api_ok = api_max;
  444. memset(&pieces, 0, sizeof(pieces));
  445. if (!ucode_raw) {
  446. if (nic->fw_index <= api_ok)
  447. IWL_ERR(nic,
  448. "request for firmware file '%s' failed.\n",
  449. nic->firmware_name);
  450. goto try_again;
  451. }
  452. IWL_DEBUG_INFO(nic, "Loaded firmware file '%s' (%zd bytes).\n",
  453. nic->firmware_name, ucode_raw->size);
  454. /* Make sure that we got at least the API version number */
  455. if (ucode_raw->size < 4) {
  456. IWL_ERR(nic, "File size way too small!\n");
  457. goto try_again;
  458. }
  459. /* Data from ucode file: header followed by uCode images */
  460. ucode = (struct iwl_ucode_header *)ucode_raw->data;
  461. if (ucode->ver)
  462. err = iwl_parse_v1_v2_firmware(nic, ucode_raw, &pieces);
  463. else
  464. err = iwl_parse_tlv_firmware(nic, ucode_raw, &pieces,
  465. &fw->ucode_capa);
  466. if (err)
  467. goto try_again;
  468. api_ver = IWL_UCODE_API(nic->fw.ucode_ver);
  469. /*
  470. * api_ver should match the api version forming part of the
  471. * firmware filename ... but we don't check for that and only rely
  472. * on the API version read from firmware header from here on forward
  473. */
  474. /* no api version check required for experimental uCode */
  475. if (nic->fw_index != UCODE_EXPERIMENTAL_INDEX) {
  476. if (api_ver < api_min || api_ver > api_max) {
  477. IWL_ERR(nic,
  478. "Driver unable to support your firmware API. "
  479. "Driver supports v%u, firmware is v%u.\n",
  480. api_max, api_ver);
  481. goto try_again;
  482. }
  483. if (api_ver < api_ok) {
  484. if (api_ok != api_max)
  485. IWL_ERR(nic, "Firmware has old API version, "
  486. "expected v%u through v%u, got v%u.\n",
  487. api_ok, api_max, api_ver);
  488. else
  489. IWL_ERR(nic, "Firmware has old API version, "
  490. "expected v%u, got v%u.\n",
  491. api_max, api_ver);
  492. IWL_ERR(nic, "New firmware can be obtained from "
  493. "http://www.intellinuxwireless.org/.\n");
  494. }
  495. }
  496. IWL_INFO(nic, "loaded firmware version %s", nic->fw.fw_version);
  497. /*
  498. * For any of the failures below (before allocating pci memory)
  499. * we will try to load a version with a smaller API -- maybe the
  500. * user just got a corrupted version of the latest API.
  501. */
  502. IWL_DEBUG_INFO(nic, "f/w package hdr ucode version raw = 0x%x\n",
  503. nic->fw.ucode_ver);
  504. IWL_DEBUG_INFO(nic, "f/w package hdr runtime inst size = %Zd\n",
  505. pieces.inst_size);
  506. IWL_DEBUG_INFO(nic, "f/w package hdr runtime data size = %Zd\n",
  507. pieces.data_size);
  508. IWL_DEBUG_INFO(nic, "f/w package hdr init inst size = %Zd\n",
  509. pieces.init_size);
  510. IWL_DEBUG_INFO(nic, "f/w package hdr init data size = %Zd\n",
  511. pieces.init_data_size);
  512. /* Verify that uCode images will fit in card's SRAM */
  513. if (pieces.inst_size > cfg->max_inst_size) {
  514. IWL_ERR(nic, "uCode instr len %Zd too large to fit in\n",
  515. pieces.inst_size);
  516. goto try_again;
  517. }
  518. if (pieces.data_size > cfg->max_data_size) {
  519. IWL_ERR(nic, "uCode data len %Zd too large to fit in\n",
  520. pieces.data_size);
  521. goto try_again;
  522. }
  523. if (pieces.init_size > cfg->max_inst_size) {
  524. IWL_ERR(nic, "uCode init instr len %Zd too large to fit in\n",
  525. pieces.init_size);
  526. goto try_again;
  527. }
  528. if (pieces.init_data_size > cfg->max_data_size) {
  529. IWL_ERR(nic, "uCode init data len %Zd too large to fit in\n",
  530. pieces.init_data_size);
  531. goto try_again;
  532. }
  533. /* Allocate ucode buffers for card's bus-master loading ... */
  534. /* Runtime instructions and 2 copies of data:
  535. * 1) unmodified from disk
  536. * 2) backup cache for save/restore during power-downs */
  537. if (iwl_alloc_fw_desc(nic, &nic->fw.ucode_rt.code,
  538. pieces.inst, pieces.inst_size))
  539. goto err_pci_alloc;
  540. if (iwl_alloc_fw_desc(nic, &nic->fw.ucode_rt.data,
  541. pieces.data, pieces.data_size))
  542. goto err_pci_alloc;
  543. /* Initialization instructions and data */
  544. if (pieces.init_size && pieces.init_data_size) {
  545. if (iwl_alloc_fw_desc(nic,
  546. &nic->fw.ucode_init.code,
  547. pieces.init, pieces.init_size))
  548. goto err_pci_alloc;
  549. if (iwl_alloc_fw_desc(nic,
  550. &nic->fw.ucode_init.data,
  551. pieces.init_data, pieces.init_data_size))
  552. goto err_pci_alloc;
  553. }
  554. /* WoWLAN instructions and data */
  555. if (pieces.wowlan_inst_size && pieces.wowlan_data_size) {
  556. if (iwl_alloc_fw_desc(nic,
  557. &nic->fw.ucode_wowlan.code,
  558. pieces.wowlan_inst,
  559. pieces.wowlan_inst_size))
  560. goto err_pci_alloc;
  561. if (iwl_alloc_fw_desc(nic,
  562. &nic->fw.ucode_wowlan.data,
  563. pieces.wowlan_data,
  564. pieces.wowlan_data_size))
  565. goto err_pci_alloc;
  566. }
  567. /* Now that we can no longer fail, copy information */
  568. /*
  569. * The (size - 16) / 12 formula is based on the information recorded
  570. * for each event, which is of mode 1 (including timestamp) for all
  571. * new microcodes that include this information.
  572. */
  573. nic->init_evtlog_ptr = pieces.init_evtlog_ptr;
  574. if (pieces.init_evtlog_size)
  575. nic->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
  576. else
  577. nic->init_evtlog_size =
  578. cfg->base_params->max_event_log_size;
  579. nic->init_errlog_ptr = pieces.init_errlog_ptr;
  580. nic->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
  581. if (pieces.inst_evtlog_size)
  582. nic->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
  583. else
  584. nic->inst_evtlog_size =
  585. cfg->base_params->max_event_log_size;
  586. nic->inst_errlog_ptr = pieces.inst_errlog_ptr;
  587. /*
  588. * figure out the offset of chain noise reset and gain commands
  589. * base on the size of standard phy calibration commands table size
  590. */
  591. if (fw->ucode_capa.standard_phy_calibration_size >
  592. IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
  593. fw->ucode_capa.standard_phy_calibration_size =
  594. IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  595. /* We have our copies now, allow OS release its copies */
  596. release_firmware(ucode_raw);
  597. complete(&nic->request_firmware_complete);
  598. nic->op_mode = iwl_dvm_ops.start(nic->shrd->trans);
  599. if (!nic->op_mode)
  600. goto out_unbind;
  601. return;
  602. try_again:
  603. /* try next, if any */
  604. release_firmware(ucode_raw);
  605. if (iwl_request_firmware(nic, false))
  606. goto out_unbind;
  607. return;
  608. err_pci_alloc:
  609. IWL_ERR(nic, "failed to allocate pci memory\n");
  610. iwl_dealloc_ucode(nic);
  611. release_firmware(ucode_raw);
  612. out_unbind:
  613. complete(&nic->request_firmware_complete);
  614. device_release_driver(trans(nic)->dev);
  615. }
  616. int iwl_drv_start(struct iwl_shared *shrd,
  617. struct iwl_trans *trans, const struct iwl_cfg *cfg)
  618. {
  619. int ret;
  620. shrd->cfg = cfg;
  621. shrd->nic = kzalloc(sizeof(*shrd->nic), GFP_KERNEL);
  622. if (!shrd->nic) {
  623. dev_printk(KERN_ERR, trans->dev, "Couldn't allocate iwl_nic");
  624. return -ENOMEM;
  625. }
  626. shrd->nic->shrd = shrd;
  627. init_completion(&shrd->nic->request_firmware_complete);
  628. ret = iwl_request_firmware(shrd->nic, true);
  629. if (ret) {
  630. dev_printk(KERN_ERR, trans->dev, "Couldn't request the fw");
  631. kfree(shrd->nic);
  632. }
  633. return ret;
  634. }
  635. void iwl_drv_stop(struct iwl_shared *shrd)
  636. {
  637. /* op_mode can be NULL if its start failed */
  638. if (shrd->nic->op_mode)
  639. iwl_op_mode_stop(shrd->nic->op_mode);
  640. iwl_dealloc_ucode(shrd->nic);
  641. kfree(shrd->nic);
  642. }