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. struct iwl_trans *trans;
  88. int fw_index; /* firmware we're trying to load */
  89. char firmware_name[25]; /* name of firmware file to load */
  90. struct completion request_firmware_complete;
  91. };
  92. /*
  93. * struct fw_sec: Just for the image parsing proccess.
  94. * For the fw storage we are using struct fw_desc.
  95. */
  96. struct fw_sec {
  97. const void *data; /* the sec data */
  98. size_t size; /* section size */
  99. u32 offset; /* offset of writing in the device */
  100. };
  101. static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
  102. {
  103. if (desc->v_addr)
  104. dma_free_coherent(trans(drv)->dev, desc->len,
  105. desc->v_addr, desc->p_addr);
  106. desc->v_addr = NULL;
  107. desc->len = 0;
  108. }
  109. static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
  110. {
  111. int i;
  112. for (i = 0; i < IWL_UCODE_SECTION_MAX; i++)
  113. iwl_free_fw_desc(drv, &img->sec[i]);
  114. }
  115. static void iwl_dealloc_ucode(struct iwl_drv *drv)
  116. {
  117. int i;
  118. for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
  119. iwl_free_fw_img(drv, drv->fw.img + i);
  120. }
  121. static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
  122. struct fw_sec *sec)
  123. {
  124. if (!sec || !sec->size) {
  125. desc->v_addr = NULL;
  126. return -EINVAL;
  127. }
  128. desc->v_addr = dma_alloc_coherent(trans(drv)->dev, sec->size,
  129. &desc->p_addr, GFP_KERNEL);
  130. if (!desc->v_addr)
  131. return -ENOMEM;
  132. desc->len = sec->size;
  133. desc->offset = sec->offset;
  134. memcpy(desc->v_addr, sec->data, sec->size);
  135. return 0;
  136. }
  137. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
  138. #define UCODE_EXPERIMENTAL_INDEX 100
  139. #define UCODE_EXPERIMENTAL_TAG "exp"
  140. static int iwl_request_firmware(struct iwl_drv *drv, bool first)
  141. {
  142. const struct iwl_cfg *cfg = cfg(drv);
  143. const char *name_pre = cfg->fw_name_pre;
  144. char tag[8];
  145. if (first) {
  146. #ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
  147. drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
  148. strcpy(tag, UCODE_EXPERIMENTAL_TAG);
  149. } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
  150. #endif
  151. drv->fw_index = cfg->ucode_api_max;
  152. sprintf(tag, "%d", drv->fw_index);
  153. } else {
  154. drv->fw_index--;
  155. sprintf(tag, "%d", drv->fw_index);
  156. }
  157. if (drv->fw_index < cfg->ucode_api_min) {
  158. IWL_ERR(drv, "no suitable firmware found!\n");
  159. return -ENOENT;
  160. }
  161. sprintf(drv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
  162. IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
  163. (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  164. ? "EXPERIMENTAL " : "",
  165. drv->firmware_name);
  166. return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
  167. trans(drv)->dev,
  168. GFP_KERNEL, drv, iwl_ucode_callback);
  169. }
  170. struct fw_img_parsing {
  171. struct fw_sec sec[IWL_UCODE_SECTION_MAX];
  172. int sec_counter;
  173. };
  174. /*
  175. * struct fw_sec_parsing: to extract fw section and it's offset from tlv
  176. */
  177. struct fw_sec_parsing {
  178. __le32 offset;
  179. const u8 data[];
  180. } __packed;
  181. /**
  182. * struct iwl_tlv_calib_data - parse the default calib data from TLV
  183. *
  184. * @ucode_type: the uCode to which the following default calib relates.
  185. * @calib: default calibrations.
  186. */
  187. struct iwl_tlv_calib_data {
  188. __le32 ucode_type;
  189. __le64 calib;
  190. } __packed;
  191. struct iwl_firmware_pieces {
  192. struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
  193. u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
  194. u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
  195. };
  196. /*
  197. * These functions are just to extract uCode section data from the pieces
  198. * structure.
  199. */
  200. static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
  201. enum iwl_ucode_type type,
  202. int sec)
  203. {
  204. return &pieces->img[type].sec[sec];
  205. }
  206. static void set_sec_data(struct iwl_firmware_pieces *pieces,
  207. enum iwl_ucode_type type,
  208. int sec,
  209. const void *data)
  210. {
  211. pieces->img[type].sec[sec].data = data;
  212. }
  213. static void set_sec_size(struct iwl_firmware_pieces *pieces,
  214. enum iwl_ucode_type type,
  215. int sec,
  216. size_t size)
  217. {
  218. pieces->img[type].sec[sec].size = size;
  219. }
  220. static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
  221. enum iwl_ucode_type type,
  222. int sec)
  223. {
  224. return pieces->img[type].sec[sec].size;
  225. }
  226. static void set_sec_offset(struct iwl_firmware_pieces *pieces,
  227. enum iwl_ucode_type type,
  228. int sec,
  229. u32 offset)
  230. {
  231. pieces->img[type].sec[sec].offset = offset;
  232. }
  233. /*
  234. * Gets uCode section from tlv.
  235. */
  236. static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
  237. const void *data, enum iwl_ucode_type type,
  238. int size)
  239. {
  240. struct fw_img_parsing *img;
  241. struct fw_sec *sec;
  242. struct fw_sec_parsing *sec_parse;
  243. if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
  244. return -1;
  245. sec_parse = (struct fw_sec_parsing *)data;
  246. img = &pieces->img[type];
  247. sec = &img->sec[img->sec_counter];
  248. sec->offset = le32_to_cpu(sec_parse->offset);
  249. sec->data = sec_parse->data;
  250. sec->size = size - sizeof(sec_parse->offset);
  251. ++img->sec_counter;
  252. return 0;
  253. }
  254. static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
  255. {
  256. struct iwl_tlv_calib_data *def_calib =
  257. (struct iwl_tlv_calib_data *)data;
  258. u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
  259. if (ucode_type >= IWL_UCODE_TYPE_MAX) {
  260. IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
  261. ucode_type);
  262. return -EINVAL;
  263. }
  264. drv->fw.default_calib[ucode_type] = le64_to_cpu(def_calib->calib);
  265. return 0;
  266. }
  267. static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
  268. const struct firmware *ucode_raw,
  269. struct iwl_firmware_pieces *pieces)
  270. {
  271. struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
  272. u32 api_ver, hdr_size, build;
  273. char buildstr[25];
  274. const u8 *src;
  275. drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
  276. api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
  277. switch (api_ver) {
  278. default:
  279. hdr_size = 28;
  280. if (ucode_raw->size < hdr_size) {
  281. IWL_ERR(drv, "File size too small!\n");
  282. return -EINVAL;
  283. }
  284. build = le32_to_cpu(ucode->u.v2.build);
  285. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
  286. le32_to_cpu(ucode->u.v2.inst_size));
  287. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
  288. le32_to_cpu(ucode->u.v2.data_size));
  289. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
  290. le32_to_cpu(ucode->u.v2.init_size));
  291. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
  292. le32_to_cpu(ucode->u.v2.init_data_size));
  293. src = ucode->u.v2.data;
  294. break;
  295. case 0:
  296. case 1:
  297. case 2:
  298. hdr_size = 24;
  299. if (ucode_raw->size < hdr_size) {
  300. IWL_ERR(drv, "File size too small!\n");
  301. return -EINVAL;
  302. }
  303. build = 0;
  304. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
  305. le32_to_cpu(ucode->u.v1.inst_size));
  306. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
  307. le32_to_cpu(ucode->u.v1.data_size));
  308. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
  309. le32_to_cpu(ucode->u.v1.init_size));
  310. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
  311. le32_to_cpu(ucode->u.v1.init_data_size));
  312. src = ucode->u.v1.data;
  313. break;
  314. }
  315. if (build)
  316. sprintf(buildstr, " build %u%s", build,
  317. (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  318. ? " (EXP)" : "");
  319. else
  320. buildstr[0] = '\0';
  321. snprintf(drv->fw.fw_version,
  322. sizeof(drv->fw.fw_version),
  323. "%u.%u.%u.%u%s",
  324. IWL_UCODE_MAJOR(drv->fw.ucode_ver),
  325. IWL_UCODE_MINOR(drv->fw.ucode_ver),
  326. IWL_UCODE_API(drv->fw.ucode_ver),
  327. IWL_UCODE_SERIAL(drv->fw.ucode_ver),
  328. buildstr);
  329. /* Verify size of file vs. image size info in file's header */
  330. if (ucode_raw->size != hdr_size +
  331. get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
  332. get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
  333. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
  334. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
  335. IWL_ERR(drv,
  336. "uCode file size %d does not match expected size\n",
  337. (int)ucode_raw->size);
  338. return -EINVAL;
  339. }
  340. set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
  341. src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
  342. set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
  343. IWLAGN_RTC_INST_LOWER_BOUND);
  344. set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
  345. src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
  346. set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
  347. IWLAGN_RTC_DATA_LOWER_BOUND);
  348. set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
  349. src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
  350. set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
  351. IWLAGN_RTC_INST_LOWER_BOUND);
  352. set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
  353. src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
  354. set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
  355. IWLAGN_RTC_DATA_LOWER_BOUND);
  356. return 0;
  357. }
  358. static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
  359. const struct firmware *ucode_raw,
  360. struct iwl_firmware_pieces *pieces,
  361. struct iwl_ucode_capabilities *capa)
  362. {
  363. struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
  364. struct iwl_ucode_tlv *tlv;
  365. size_t len = ucode_raw->size;
  366. const u8 *data;
  367. u32 tlv_len;
  368. enum iwl_ucode_tlv_type tlv_type;
  369. const u8 *tlv_data;
  370. char buildstr[25];
  371. u32 build;
  372. if (len < sizeof(*ucode)) {
  373. IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
  374. return -EINVAL;
  375. }
  376. if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
  377. IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
  378. le32_to_cpu(ucode->magic));
  379. return -EINVAL;
  380. }
  381. drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
  382. build = le32_to_cpu(ucode->build);
  383. if (build)
  384. sprintf(buildstr, " build %u%s", build,
  385. (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  386. ? " (EXP)" : "");
  387. else
  388. buildstr[0] = '\0';
  389. snprintf(drv->fw.fw_version,
  390. sizeof(drv->fw.fw_version),
  391. "%u.%u.%u.%u%s",
  392. IWL_UCODE_MAJOR(drv->fw.ucode_ver),
  393. IWL_UCODE_MINOR(drv->fw.ucode_ver),
  394. IWL_UCODE_API(drv->fw.ucode_ver),
  395. IWL_UCODE_SERIAL(drv->fw.ucode_ver),
  396. buildstr);
  397. data = ucode->data;
  398. len -= sizeof(*ucode);
  399. while (len >= sizeof(*tlv)) {
  400. len -= sizeof(*tlv);
  401. tlv = (void *)data;
  402. tlv_len = le32_to_cpu(tlv->length);
  403. tlv_type = le32_to_cpu(tlv->type);
  404. tlv_data = tlv->data;
  405. if (len < tlv_len) {
  406. IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
  407. len, tlv_len);
  408. return -EINVAL;
  409. }
  410. len -= ALIGN(tlv_len, 4);
  411. data += sizeof(*tlv) + ALIGN(tlv_len, 4);
  412. switch (tlv_type) {
  413. case IWL_UCODE_TLV_INST:
  414. set_sec_data(pieces, IWL_UCODE_REGULAR,
  415. IWL_UCODE_SECTION_INST, tlv_data);
  416. set_sec_size(pieces, IWL_UCODE_REGULAR,
  417. IWL_UCODE_SECTION_INST, tlv_len);
  418. set_sec_offset(pieces, IWL_UCODE_REGULAR,
  419. IWL_UCODE_SECTION_INST,
  420. IWLAGN_RTC_INST_LOWER_BOUND);
  421. break;
  422. case IWL_UCODE_TLV_DATA:
  423. set_sec_data(pieces, IWL_UCODE_REGULAR,
  424. IWL_UCODE_SECTION_DATA, tlv_data);
  425. set_sec_size(pieces, IWL_UCODE_REGULAR,
  426. IWL_UCODE_SECTION_DATA, tlv_len);
  427. set_sec_offset(pieces, IWL_UCODE_REGULAR,
  428. IWL_UCODE_SECTION_DATA,
  429. IWLAGN_RTC_DATA_LOWER_BOUND);
  430. break;
  431. case IWL_UCODE_TLV_INIT:
  432. set_sec_data(pieces, IWL_UCODE_INIT,
  433. IWL_UCODE_SECTION_INST, tlv_data);
  434. set_sec_size(pieces, IWL_UCODE_INIT,
  435. IWL_UCODE_SECTION_INST, tlv_len);
  436. set_sec_offset(pieces, IWL_UCODE_INIT,
  437. IWL_UCODE_SECTION_INST,
  438. IWLAGN_RTC_INST_LOWER_BOUND);
  439. break;
  440. case IWL_UCODE_TLV_INIT_DATA:
  441. set_sec_data(pieces, IWL_UCODE_INIT,
  442. IWL_UCODE_SECTION_DATA, tlv_data);
  443. set_sec_size(pieces, IWL_UCODE_INIT,
  444. IWL_UCODE_SECTION_DATA, tlv_len);
  445. set_sec_offset(pieces, IWL_UCODE_INIT,
  446. IWL_UCODE_SECTION_DATA,
  447. IWLAGN_RTC_DATA_LOWER_BOUND);
  448. break;
  449. case IWL_UCODE_TLV_BOOT:
  450. IWL_ERR(drv, "Found unexpected BOOT ucode\n");
  451. break;
  452. case IWL_UCODE_TLV_PROBE_MAX_LEN:
  453. if (tlv_len != sizeof(u32))
  454. goto invalid_tlv_len;
  455. capa->max_probe_length =
  456. le32_to_cpup((__le32 *)tlv_data);
  457. break;
  458. case IWL_UCODE_TLV_PAN:
  459. if (tlv_len)
  460. goto invalid_tlv_len;
  461. capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
  462. break;
  463. case IWL_UCODE_TLV_FLAGS:
  464. /* must be at least one u32 */
  465. if (tlv_len < sizeof(u32))
  466. goto invalid_tlv_len;
  467. /* and a proper number of u32s */
  468. if (tlv_len % sizeof(u32))
  469. goto invalid_tlv_len;
  470. /*
  471. * This driver only reads the first u32 as
  472. * right now no more features are defined,
  473. * if that changes then either the driver
  474. * will not work with the new firmware, or
  475. * it'll not take advantage of new features.
  476. */
  477. capa->flags = le32_to_cpup((__le32 *)tlv_data);
  478. break;
  479. case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
  480. if (tlv_len != sizeof(u32))
  481. goto invalid_tlv_len;
  482. pieces->init_evtlog_ptr =
  483. le32_to_cpup((__le32 *)tlv_data);
  484. break;
  485. case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
  486. if (tlv_len != sizeof(u32))
  487. goto invalid_tlv_len;
  488. pieces->init_evtlog_size =
  489. le32_to_cpup((__le32 *)tlv_data);
  490. break;
  491. case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
  492. if (tlv_len != sizeof(u32))
  493. goto invalid_tlv_len;
  494. pieces->init_errlog_ptr =
  495. le32_to_cpup((__le32 *)tlv_data);
  496. break;
  497. case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
  498. if (tlv_len != sizeof(u32))
  499. goto invalid_tlv_len;
  500. pieces->inst_evtlog_ptr =
  501. le32_to_cpup((__le32 *)tlv_data);
  502. break;
  503. case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
  504. if (tlv_len != sizeof(u32))
  505. goto invalid_tlv_len;
  506. pieces->inst_evtlog_size =
  507. le32_to_cpup((__le32 *)tlv_data);
  508. break;
  509. case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
  510. if (tlv_len != sizeof(u32))
  511. goto invalid_tlv_len;
  512. pieces->inst_errlog_ptr =
  513. le32_to_cpup((__le32 *)tlv_data);
  514. break;
  515. case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
  516. if (tlv_len)
  517. goto invalid_tlv_len;
  518. drv->fw.enhance_sensitivity_table = true;
  519. break;
  520. case IWL_UCODE_TLV_WOWLAN_INST:
  521. set_sec_data(pieces, IWL_UCODE_WOWLAN,
  522. IWL_UCODE_SECTION_INST, tlv_data);
  523. set_sec_size(pieces, IWL_UCODE_WOWLAN,
  524. IWL_UCODE_SECTION_INST, tlv_len);
  525. set_sec_offset(pieces, IWL_UCODE_WOWLAN,
  526. IWL_UCODE_SECTION_INST,
  527. IWLAGN_RTC_INST_LOWER_BOUND);
  528. break;
  529. case IWL_UCODE_TLV_WOWLAN_DATA:
  530. set_sec_data(pieces, IWL_UCODE_WOWLAN,
  531. IWL_UCODE_SECTION_DATA, tlv_data);
  532. set_sec_size(pieces, IWL_UCODE_WOWLAN,
  533. IWL_UCODE_SECTION_DATA, tlv_len);
  534. set_sec_offset(pieces, IWL_UCODE_WOWLAN,
  535. IWL_UCODE_SECTION_DATA,
  536. IWLAGN_RTC_DATA_LOWER_BOUND);
  537. break;
  538. case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
  539. if (tlv_len != sizeof(u32))
  540. goto invalid_tlv_len;
  541. capa->standard_phy_calibration_size =
  542. le32_to_cpup((__le32 *)tlv_data);
  543. break;
  544. case IWL_UCODE_TLV_SEC_RT:
  545. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
  546. tlv_len);
  547. drv->fw.mvm_fw = true;
  548. break;
  549. case IWL_UCODE_TLV_SEC_INIT:
  550. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
  551. tlv_len);
  552. drv->fw.mvm_fw = true;
  553. break;
  554. case IWL_UCODE_TLV_SEC_WOWLAN:
  555. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
  556. tlv_len);
  557. drv->fw.mvm_fw = true;
  558. break;
  559. case IWL_UCODE_TLV_DEF_CALIB:
  560. if (tlv_len != sizeof(struct iwl_tlv_calib_data))
  561. goto invalid_tlv_len;
  562. if (iwl_set_default_calib(drv, tlv_data))
  563. goto tlv_error;
  564. break;
  565. case IWL_UCODE_TLV_PHY_SKU:
  566. if (tlv_len != sizeof(u32))
  567. goto invalid_tlv_len;
  568. drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
  569. break;
  570. default:
  571. IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
  572. break;
  573. }
  574. }
  575. if (len) {
  576. IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
  577. iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
  578. return -EINVAL;
  579. }
  580. return 0;
  581. invalid_tlv_len:
  582. IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
  583. tlv_error:
  584. iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
  585. return -EINVAL;
  586. }
  587. static int alloc_pci_desc(struct iwl_drv *drv,
  588. struct iwl_firmware_pieces *pieces,
  589. enum iwl_ucode_type type)
  590. {
  591. int i;
  592. for (i = 0;
  593. i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
  594. i++)
  595. if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
  596. get_sec(pieces, type, i)))
  597. return -1;
  598. return 0;
  599. }
  600. static int validate_sec_sizes(struct iwl_drv *drv,
  601. struct iwl_firmware_pieces *pieces,
  602. const struct iwl_cfg *cfg)
  603. {
  604. IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
  605. get_sec_size(pieces, IWL_UCODE_REGULAR,
  606. IWL_UCODE_SECTION_INST));
  607. IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
  608. get_sec_size(pieces, IWL_UCODE_REGULAR,
  609. IWL_UCODE_SECTION_DATA));
  610. IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
  611. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
  612. IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
  613. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
  614. /* Verify that uCode images will fit in card's SRAM. */
  615. if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
  616. cfg->max_inst_size) {
  617. IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
  618. get_sec_size(pieces, IWL_UCODE_REGULAR,
  619. IWL_UCODE_SECTION_INST));
  620. return -1;
  621. }
  622. if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
  623. cfg->max_data_size) {
  624. IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
  625. get_sec_size(pieces, IWL_UCODE_REGULAR,
  626. IWL_UCODE_SECTION_DATA));
  627. return -1;
  628. }
  629. if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
  630. cfg->max_inst_size) {
  631. IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
  632. get_sec_size(pieces, IWL_UCODE_INIT,
  633. IWL_UCODE_SECTION_INST));
  634. return -1;
  635. }
  636. if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
  637. cfg->max_data_size) {
  638. IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
  639. get_sec_size(pieces, IWL_UCODE_REGULAR,
  640. IWL_UCODE_SECTION_DATA));
  641. return -1;
  642. }
  643. return 0;
  644. }
  645. /**
  646. * iwl_ucode_callback - callback when firmware was loaded
  647. *
  648. * If loaded successfully, copies the firmware into buffers
  649. * for the card to fetch (via DMA).
  650. */
  651. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
  652. {
  653. struct iwl_drv *drv = context;
  654. const struct iwl_cfg *cfg = cfg(drv);
  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 = cfg->ucode_api_max;
  660. unsigned int api_ok = cfg->ucode_api_ok;
  661. const unsigned int api_min = 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, 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. 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. 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, 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(trans(drv)->dev);
  783. }
  784. struct iwl_drv *iwl_drv_start(struct iwl_shared *shrd,
  785. struct iwl_trans *trans,
  786. const struct iwl_cfg *cfg)
  787. {
  788. struct iwl_drv *drv;
  789. int ret;
  790. shrd->cfg = cfg;
  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. init_completion(&drv->request_firmware_complete);
  800. ret = iwl_request_firmware(drv, true);
  801. if (ret) {
  802. dev_printk(KERN_ERR, trans->dev, "Couldn't request the fw");
  803. kfree(drv);
  804. drv = NULL;
  805. }
  806. return drv;
  807. }
  808. void iwl_drv_stop(struct iwl_drv *drv)
  809. {
  810. wait_for_completion(&drv->request_firmware_complete);
  811. /* op_mode can be NULL if its start failed */
  812. if (drv->op_mode)
  813. iwl_op_mode_stop(drv->op_mode);
  814. iwl_dealloc_ucode(drv);
  815. kfree(drv);
  816. }