iwl-drv.c 26 KB

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