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. * @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. sec->size = size - sizeof(sec_parse->offset);
  250. ++img->sec_counter;
  251. return 0;
  252. }
  253. static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
  254. {
  255. struct iwl_tlv_calib_data *def_calib =
  256. (struct iwl_tlv_calib_data *)data;
  257. u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
  258. if (ucode_type >= IWL_UCODE_TYPE_MAX) {
  259. IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
  260. ucode_type);
  261. return -EINVAL;
  262. }
  263. drv->fw.default_calib[ucode_type] = le64_to_cpu(def_calib->calib);
  264. return 0;
  265. }
  266. static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
  267. const struct firmware *ucode_raw,
  268. struct iwl_firmware_pieces *pieces)
  269. {
  270. struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
  271. u32 api_ver, hdr_size, build;
  272. char buildstr[25];
  273. const u8 *src;
  274. drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
  275. api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
  276. switch (api_ver) {
  277. default:
  278. hdr_size = 28;
  279. if (ucode_raw->size < hdr_size) {
  280. IWL_ERR(drv, "File size too small!\n");
  281. return -EINVAL;
  282. }
  283. build = le32_to_cpu(ucode->u.v2.build);
  284. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
  285. le32_to_cpu(ucode->u.v2.inst_size));
  286. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
  287. le32_to_cpu(ucode->u.v2.data_size));
  288. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
  289. le32_to_cpu(ucode->u.v2.init_size));
  290. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
  291. le32_to_cpu(ucode->u.v2.init_data_size));
  292. src = ucode->u.v2.data;
  293. break;
  294. case 0:
  295. case 1:
  296. case 2:
  297. hdr_size = 24;
  298. if (ucode_raw->size < hdr_size) {
  299. IWL_ERR(drv, "File size too small!\n");
  300. return -EINVAL;
  301. }
  302. build = 0;
  303. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
  304. le32_to_cpu(ucode->u.v1.inst_size));
  305. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
  306. le32_to_cpu(ucode->u.v1.data_size));
  307. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
  308. le32_to_cpu(ucode->u.v1.init_size));
  309. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
  310. le32_to_cpu(ucode->u.v1.init_data_size));
  311. src = ucode->u.v1.data;
  312. break;
  313. }
  314. if (build)
  315. sprintf(buildstr, " build %u%s", build,
  316. (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  317. ? " (EXP)" : "");
  318. else
  319. buildstr[0] = '\0';
  320. snprintf(drv->fw.fw_version,
  321. sizeof(drv->fw.fw_version),
  322. "%u.%u.%u.%u%s",
  323. IWL_UCODE_MAJOR(drv->fw.ucode_ver),
  324. IWL_UCODE_MINOR(drv->fw.ucode_ver),
  325. IWL_UCODE_API(drv->fw.ucode_ver),
  326. IWL_UCODE_SERIAL(drv->fw.ucode_ver),
  327. buildstr);
  328. /* Verify size of file vs. image size info in file's header */
  329. if (ucode_raw->size != hdr_size +
  330. get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
  331. get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
  332. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
  333. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
  334. IWL_ERR(drv,
  335. "uCode file size %d does not match expected size\n",
  336. (int)ucode_raw->size);
  337. return -EINVAL;
  338. }
  339. set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
  340. src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
  341. set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
  342. IWLAGN_RTC_INST_LOWER_BOUND);
  343. set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
  344. src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
  345. set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
  346. IWLAGN_RTC_DATA_LOWER_BOUND);
  347. set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
  348. src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
  349. set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
  350. IWLAGN_RTC_INST_LOWER_BOUND);
  351. set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
  352. src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
  353. set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
  354. IWLAGN_RTC_DATA_LOWER_BOUND);
  355. return 0;
  356. }
  357. static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
  358. const struct firmware *ucode_raw,
  359. struct iwl_firmware_pieces *pieces,
  360. struct iwl_ucode_capabilities *capa)
  361. {
  362. struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
  363. struct iwl_ucode_tlv *tlv;
  364. size_t len = ucode_raw->size;
  365. const u8 *data;
  366. u32 tlv_len;
  367. enum iwl_ucode_tlv_type tlv_type;
  368. const u8 *tlv_data;
  369. char buildstr[25];
  370. u32 build;
  371. if (len < sizeof(*ucode)) {
  372. IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
  373. return -EINVAL;
  374. }
  375. if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
  376. IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
  377. le32_to_cpu(ucode->magic));
  378. return -EINVAL;
  379. }
  380. drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
  381. build = le32_to_cpu(ucode->build);
  382. if (build)
  383. sprintf(buildstr, " build %u%s", build,
  384. (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  385. ? " (EXP)" : "");
  386. else
  387. buildstr[0] = '\0';
  388. snprintf(drv->fw.fw_version,
  389. sizeof(drv->fw.fw_version),
  390. "%u.%u.%u.%u%s",
  391. IWL_UCODE_MAJOR(drv->fw.ucode_ver),
  392. IWL_UCODE_MINOR(drv->fw.ucode_ver),
  393. IWL_UCODE_API(drv->fw.ucode_ver),
  394. IWL_UCODE_SERIAL(drv->fw.ucode_ver),
  395. buildstr);
  396. data = ucode->data;
  397. len -= sizeof(*ucode);
  398. while (len >= sizeof(*tlv)) {
  399. len -= sizeof(*tlv);
  400. tlv = (void *)data;
  401. tlv_len = le32_to_cpu(tlv->length);
  402. tlv_type = le32_to_cpu(tlv->type);
  403. tlv_data = tlv->data;
  404. if (len < tlv_len) {
  405. IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
  406. len, tlv_len);
  407. return -EINVAL;
  408. }
  409. len -= ALIGN(tlv_len, 4);
  410. data += sizeof(*tlv) + ALIGN(tlv_len, 4);
  411. switch (tlv_type) {
  412. case IWL_UCODE_TLV_INST:
  413. set_sec_data(pieces, IWL_UCODE_REGULAR,
  414. IWL_UCODE_SECTION_INST, tlv_data);
  415. set_sec_size(pieces, IWL_UCODE_REGULAR,
  416. IWL_UCODE_SECTION_INST, tlv_len);
  417. set_sec_offset(pieces, IWL_UCODE_REGULAR,
  418. IWL_UCODE_SECTION_INST,
  419. IWLAGN_RTC_INST_LOWER_BOUND);
  420. break;
  421. case IWL_UCODE_TLV_DATA:
  422. set_sec_data(pieces, IWL_UCODE_REGULAR,
  423. IWL_UCODE_SECTION_DATA, tlv_data);
  424. set_sec_size(pieces, IWL_UCODE_REGULAR,
  425. IWL_UCODE_SECTION_DATA, tlv_len);
  426. set_sec_offset(pieces, IWL_UCODE_REGULAR,
  427. IWL_UCODE_SECTION_DATA,
  428. IWLAGN_RTC_DATA_LOWER_BOUND);
  429. break;
  430. case IWL_UCODE_TLV_INIT:
  431. set_sec_data(pieces, IWL_UCODE_INIT,
  432. IWL_UCODE_SECTION_INST, tlv_data);
  433. set_sec_size(pieces, IWL_UCODE_INIT,
  434. IWL_UCODE_SECTION_INST, tlv_len);
  435. set_sec_offset(pieces, IWL_UCODE_INIT,
  436. IWL_UCODE_SECTION_INST,
  437. IWLAGN_RTC_INST_LOWER_BOUND);
  438. break;
  439. case IWL_UCODE_TLV_INIT_DATA:
  440. set_sec_data(pieces, IWL_UCODE_INIT,
  441. IWL_UCODE_SECTION_DATA, tlv_data);
  442. set_sec_size(pieces, IWL_UCODE_INIT,
  443. IWL_UCODE_SECTION_DATA, tlv_len);
  444. set_sec_offset(pieces, IWL_UCODE_INIT,
  445. IWL_UCODE_SECTION_DATA,
  446. IWLAGN_RTC_DATA_LOWER_BOUND);
  447. break;
  448. case IWL_UCODE_TLV_BOOT:
  449. IWL_ERR(drv, "Found unexpected BOOT ucode\n");
  450. break;
  451. case IWL_UCODE_TLV_PROBE_MAX_LEN:
  452. if (tlv_len != sizeof(u32))
  453. goto invalid_tlv_len;
  454. capa->max_probe_length =
  455. le32_to_cpup((__le32 *)tlv_data);
  456. break;
  457. case IWL_UCODE_TLV_PAN:
  458. if (tlv_len)
  459. goto invalid_tlv_len;
  460. capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
  461. break;
  462. case IWL_UCODE_TLV_FLAGS:
  463. /* must be at least one u32 */
  464. if (tlv_len < sizeof(u32))
  465. goto invalid_tlv_len;
  466. /* and a proper number of u32s */
  467. if (tlv_len % sizeof(u32))
  468. goto invalid_tlv_len;
  469. /*
  470. * This driver only reads the first u32 as
  471. * right now no more features are defined,
  472. * if that changes then either the driver
  473. * will not work with the new firmware, or
  474. * it'll not take advantage of new features.
  475. */
  476. capa->flags = le32_to_cpup((__le32 *)tlv_data);
  477. break;
  478. case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
  479. if (tlv_len != sizeof(u32))
  480. goto invalid_tlv_len;
  481. pieces->init_evtlog_ptr =
  482. le32_to_cpup((__le32 *)tlv_data);
  483. break;
  484. case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
  485. if (tlv_len != sizeof(u32))
  486. goto invalid_tlv_len;
  487. pieces->init_evtlog_size =
  488. le32_to_cpup((__le32 *)tlv_data);
  489. break;
  490. case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
  491. if (tlv_len != sizeof(u32))
  492. goto invalid_tlv_len;
  493. pieces->init_errlog_ptr =
  494. le32_to_cpup((__le32 *)tlv_data);
  495. break;
  496. case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
  497. if (tlv_len != sizeof(u32))
  498. goto invalid_tlv_len;
  499. pieces->inst_evtlog_ptr =
  500. le32_to_cpup((__le32 *)tlv_data);
  501. break;
  502. case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
  503. if (tlv_len != sizeof(u32))
  504. goto invalid_tlv_len;
  505. pieces->inst_evtlog_size =
  506. le32_to_cpup((__le32 *)tlv_data);
  507. break;
  508. case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
  509. if (tlv_len != sizeof(u32))
  510. goto invalid_tlv_len;
  511. pieces->inst_errlog_ptr =
  512. le32_to_cpup((__le32 *)tlv_data);
  513. break;
  514. case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
  515. if (tlv_len)
  516. goto invalid_tlv_len;
  517. drv->fw.enhance_sensitivity_table = true;
  518. break;
  519. case IWL_UCODE_TLV_WOWLAN_INST:
  520. set_sec_data(pieces, IWL_UCODE_WOWLAN,
  521. IWL_UCODE_SECTION_INST, tlv_data);
  522. set_sec_size(pieces, IWL_UCODE_WOWLAN,
  523. IWL_UCODE_SECTION_INST, tlv_len);
  524. set_sec_offset(pieces, IWL_UCODE_WOWLAN,
  525. IWL_UCODE_SECTION_INST,
  526. IWLAGN_RTC_INST_LOWER_BOUND);
  527. break;
  528. case IWL_UCODE_TLV_WOWLAN_DATA:
  529. set_sec_data(pieces, IWL_UCODE_WOWLAN,
  530. IWL_UCODE_SECTION_DATA, tlv_data);
  531. set_sec_size(pieces, IWL_UCODE_WOWLAN,
  532. IWL_UCODE_SECTION_DATA, tlv_len);
  533. set_sec_offset(pieces, IWL_UCODE_WOWLAN,
  534. IWL_UCODE_SECTION_DATA,
  535. IWLAGN_RTC_DATA_LOWER_BOUND);
  536. break;
  537. case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
  538. if (tlv_len != sizeof(u32))
  539. goto invalid_tlv_len;
  540. capa->standard_phy_calibration_size =
  541. le32_to_cpup((__le32 *)tlv_data);
  542. break;
  543. case IWL_UCODE_TLV_SEC_RT:
  544. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
  545. tlv_len);
  546. drv->fw.mvm_fw = true;
  547. break;
  548. case IWL_UCODE_TLV_SEC_INIT:
  549. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
  550. tlv_len);
  551. drv->fw.mvm_fw = true;
  552. break;
  553. case IWL_UCODE_TLV_SEC_WOWLAN:
  554. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
  555. tlv_len);
  556. drv->fw.mvm_fw = true;
  557. break;
  558. case IWL_UCODE_TLV_DEF_CALIB:
  559. if (tlv_len != sizeof(struct iwl_tlv_calib_data))
  560. goto invalid_tlv_len;
  561. if (iwl_set_default_calib(drv, tlv_data))
  562. goto tlv_error;
  563. break;
  564. case IWL_UCODE_TLV_PHY_SKU:
  565. if (tlv_len != sizeof(u32))
  566. goto invalid_tlv_len;
  567. drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
  568. break;
  569. default:
  570. IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
  571. break;
  572. }
  573. }
  574. if (len) {
  575. IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
  576. iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
  577. return -EINVAL;
  578. }
  579. return 0;
  580. invalid_tlv_len:
  581. IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
  582. tlv_error:
  583. iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
  584. return -EINVAL;
  585. }
  586. static int alloc_pci_desc(struct iwl_drv *drv,
  587. struct iwl_firmware_pieces *pieces,
  588. enum iwl_ucode_type type)
  589. {
  590. int i;
  591. for (i = 0;
  592. i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
  593. i++)
  594. if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
  595. get_sec(pieces, type, i)))
  596. return -1;
  597. return 0;
  598. }
  599. static int validate_sec_sizes(struct iwl_drv *drv,
  600. struct iwl_firmware_pieces *pieces,
  601. const struct iwl_cfg *cfg)
  602. {
  603. IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
  604. get_sec_size(pieces, IWL_UCODE_REGULAR,
  605. IWL_UCODE_SECTION_INST));
  606. IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
  607. get_sec_size(pieces, IWL_UCODE_REGULAR,
  608. IWL_UCODE_SECTION_DATA));
  609. IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
  610. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
  611. IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
  612. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
  613. /* Verify that uCode images will fit in card's SRAM. */
  614. if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
  615. cfg->max_inst_size) {
  616. IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
  617. get_sec_size(pieces, IWL_UCODE_REGULAR,
  618. IWL_UCODE_SECTION_INST));
  619. return -1;
  620. }
  621. if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
  622. cfg->max_data_size) {
  623. IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
  624. get_sec_size(pieces, IWL_UCODE_REGULAR,
  625. IWL_UCODE_SECTION_DATA));
  626. return -1;
  627. }
  628. if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
  629. cfg->max_inst_size) {
  630. IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
  631. get_sec_size(pieces, IWL_UCODE_INIT,
  632. IWL_UCODE_SECTION_INST));
  633. return -1;
  634. }
  635. if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
  636. cfg->max_data_size) {
  637. IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
  638. get_sec_size(pieces, IWL_UCODE_REGULAR,
  639. IWL_UCODE_SECTION_DATA));
  640. return -1;
  641. }
  642. return 0;
  643. }
  644. /**
  645. * iwl_ucode_callback - callback when firmware was loaded
  646. *
  647. * If loaded successfully, copies the firmware into buffers
  648. * for the card to fetch (via DMA).
  649. */
  650. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
  651. {
  652. struct iwl_drv *drv = context;
  653. const struct iwl_cfg *cfg = cfg(drv);
  654. struct iwl_fw *fw = &drv->fw;
  655. struct iwl_ucode_header *ucode;
  656. int err;
  657. struct iwl_firmware_pieces pieces;
  658. const unsigned int api_max = cfg->ucode_api_max;
  659. unsigned int api_ok = cfg->ucode_api_ok;
  660. const unsigned int api_min = cfg->ucode_api_min;
  661. u32 api_ver;
  662. int i;
  663. fw->ucode_capa.max_probe_length = 200;
  664. fw->ucode_capa.standard_phy_calibration_size =
  665. IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  666. if (!api_ok)
  667. api_ok = api_max;
  668. memset(&pieces, 0, sizeof(pieces));
  669. if (!ucode_raw) {
  670. if (drv->fw_index <= api_ok)
  671. IWL_ERR(drv,
  672. "request for firmware file '%s' failed.\n",
  673. drv->firmware_name);
  674. goto try_again;
  675. }
  676. IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
  677. drv->firmware_name, ucode_raw->size);
  678. /* Make sure that we got at least the API version number */
  679. if (ucode_raw->size < 4) {
  680. IWL_ERR(drv, "File size way too small!\n");
  681. goto try_again;
  682. }
  683. /* Data from ucode file: header followed by uCode images */
  684. ucode = (struct iwl_ucode_header *)ucode_raw->data;
  685. if (ucode->ver)
  686. err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
  687. else
  688. err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
  689. &fw->ucode_capa);
  690. if (err)
  691. goto try_again;
  692. api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
  693. /*
  694. * api_ver should match the api version forming part of the
  695. * firmware filename ... but we don't check for that and only rely
  696. * on the API version read from firmware header from here on forward
  697. */
  698. /* no api version check required for experimental uCode */
  699. if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
  700. if (api_ver < api_min || api_ver > api_max) {
  701. IWL_ERR(drv,
  702. "Driver unable to support your firmware API. "
  703. "Driver supports v%u, firmware is v%u.\n",
  704. api_max, api_ver);
  705. goto try_again;
  706. }
  707. if (api_ver < api_ok) {
  708. if (api_ok != api_max)
  709. IWL_ERR(drv, "Firmware has old API version, "
  710. "expected v%u through v%u, got v%u.\n",
  711. api_ok, api_max, api_ver);
  712. else
  713. IWL_ERR(drv, "Firmware has old API version, "
  714. "expected v%u, got v%u.\n",
  715. api_max, api_ver);
  716. IWL_ERR(drv, "New firmware can be obtained from "
  717. "http://www.intellinuxwireless.org/.\n");
  718. }
  719. }
  720. IWL_INFO(drv, "loaded firmware version %s", drv->fw.fw_version);
  721. /*
  722. * In mvm uCode there is no difference between data and instructions
  723. * sections.
  724. */
  725. if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, cfg))
  726. goto try_again;
  727. /* Allocate ucode buffers for card's bus-master loading ... */
  728. /* Runtime instructions and 2 copies of data:
  729. * 1) unmodified from disk
  730. * 2) backup cache for save/restore during power-downs */
  731. for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
  732. if (alloc_pci_desc(drv, &pieces, i))
  733. goto err_pci_alloc;
  734. /* Now that we can no longer fail, copy information */
  735. /*
  736. * The (size - 16) / 12 formula is based on the information recorded
  737. * for each event, which is of mode 1 (including timestamp) for all
  738. * new microcodes that include this information.
  739. */
  740. fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
  741. if (pieces.init_evtlog_size)
  742. fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
  743. else
  744. fw->init_evtlog_size =
  745. cfg->base_params->max_event_log_size;
  746. fw->init_errlog_ptr = pieces.init_errlog_ptr;
  747. fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
  748. if (pieces.inst_evtlog_size)
  749. fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
  750. else
  751. fw->inst_evtlog_size =
  752. cfg->base_params->max_event_log_size;
  753. fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
  754. /*
  755. * figure out the offset of chain noise reset and gain commands
  756. * base on the size of standard phy calibration commands table size
  757. */
  758. if (fw->ucode_capa.standard_phy_calibration_size >
  759. IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
  760. fw->ucode_capa.standard_phy_calibration_size =
  761. IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  762. /* We have our copies now, allow OS release its copies */
  763. release_firmware(ucode_raw);
  764. complete(&drv->request_firmware_complete);
  765. drv->op_mode = iwl_dvm_ops.start(drv->shrd->trans, &drv->fw);
  766. if (!drv->op_mode)
  767. goto out_unbind;
  768. return;
  769. try_again:
  770. /* try next, if any */
  771. release_firmware(ucode_raw);
  772. if (iwl_request_firmware(drv, false))
  773. goto out_unbind;
  774. return;
  775. err_pci_alloc:
  776. IWL_ERR(drv, "failed to allocate pci memory\n");
  777. iwl_dealloc_ucode(drv);
  778. release_firmware(ucode_raw);
  779. out_unbind:
  780. complete(&drv->request_firmware_complete);
  781. device_release_driver(trans(drv)->dev);
  782. }
  783. int iwl_drv_start(struct iwl_shared *shrd,
  784. struct iwl_trans *trans, const struct iwl_cfg *cfg)
  785. {
  786. struct iwl_drv *drv;
  787. int ret;
  788. shrd->cfg = cfg;
  789. drv = kzalloc(sizeof(*drv), GFP_KERNEL);
  790. if (!drv) {
  791. dev_printk(KERN_ERR, trans->dev, "Couldn't allocate iwl_drv");
  792. return -ENOMEM;
  793. }
  794. drv->shrd = shrd;
  795. shrd->drv = drv;
  796. init_completion(&drv->request_firmware_complete);
  797. ret = iwl_request_firmware(drv, true);
  798. if (ret) {
  799. dev_printk(KERN_ERR, trans->dev, "Couldn't request the fw");
  800. kfree(drv);
  801. shrd->drv = NULL;
  802. }
  803. return ret;
  804. }
  805. void iwl_drv_stop(struct iwl_shared *shrd)
  806. {
  807. struct iwl_drv *drv = shrd->drv;
  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. shrd->drv = NULL;
  815. }