iwl-drv.c 29 KB

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