iwl-drv.c 27 KB

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