iwl-drv.c 27 KB

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