iwl-drv.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2007 - 2012 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called LICENSE.GPL.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2005 - 2012 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <linux/completion.h>
  64. #include <linux/dma-mapping.h>
  65. #include <linux/firmware.h>
  66. #include <linux/module.h>
  67. #include "iwl-drv.h"
  68. #include "iwl-trans.h"
  69. #include "iwl-op-mode.h"
  70. #include "iwl-agn-hw.h"
  71. #include "iwl-fw.h"
  72. #include "iwl-config.h"
  73. #include "iwl-modparams.h"
  74. /* private includes */
  75. #include "iwl-fw-file.h"
  76. /**
  77. * struct iwl_drv - drv common data
  78. * @fw: the iwl_fw structure
  79. * @op_mode: the running op_mode
  80. * @trans: transport layer
  81. * @dev: for debug prints only
  82. * @cfg: configuration struct
  83. * @fw_index: firmware revision to try loading
  84. * @firmware_name: composite filename of ucode file to load
  85. * @request_firmware_complete: the firmware has been obtained from user space
  86. */
  87. struct iwl_drv {
  88. struct iwl_fw fw;
  89. struct iwl_op_mode *op_mode;
  90. struct iwl_trans *trans;
  91. struct device *dev;
  92. const struct iwl_cfg *cfg;
  93. int fw_index; /* firmware we're trying to load */
  94. char firmware_name[25]; /* name of firmware file to load */
  95. struct completion request_firmware_complete;
  96. };
  97. /*
  98. * struct fw_sec: Just for the image parsing proccess.
  99. * For the fw storage we are using struct fw_desc.
  100. */
  101. struct fw_sec {
  102. const void *data; /* the sec data */
  103. size_t size; /* section size */
  104. u32 offset; /* offset of writing in the device */
  105. };
  106. static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
  107. {
  108. if (desc->v_addr)
  109. dma_free_coherent(drv->trans->dev, desc->len,
  110. desc->v_addr, desc->p_addr);
  111. desc->v_addr = NULL;
  112. desc->len = 0;
  113. }
  114. static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
  115. {
  116. int i;
  117. for (i = 0; i < IWL_UCODE_SECTION_MAX; i++)
  118. iwl_free_fw_desc(drv, &img->sec[i]);
  119. }
  120. static void iwl_dealloc_ucode(struct iwl_drv *drv)
  121. {
  122. int i;
  123. for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
  124. iwl_free_fw_img(drv, drv->fw.img + i);
  125. }
  126. static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
  127. struct fw_sec *sec)
  128. {
  129. if (!sec || !sec->size) {
  130. desc->v_addr = NULL;
  131. return -EINVAL;
  132. }
  133. desc->v_addr = dma_alloc_coherent(drv->trans->dev, sec->size,
  134. &desc->p_addr, GFP_KERNEL);
  135. if (!desc->v_addr)
  136. return -ENOMEM;
  137. desc->len = sec->size;
  138. desc->offset = sec->offset;
  139. memcpy(desc->v_addr, sec->data, sec->size);
  140. return 0;
  141. }
  142. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
  143. #define UCODE_EXPERIMENTAL_INDEX 100
  144. #define UCODE_EXPERIMENTAL_TAG "exp"
  145. static int iwl_request_firmware(struct iwl_drv *drv, bool first)
  146. {
  147. const char *name_pre = drv->cfg->fw_name_pre;
  148. char tag[8];
  149. if (first) {
  150. #ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
  151. drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
  152. strcpy(tag, UCODE_EXPERIMENTAL_TAG);
  153. } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
  154. #endif
  155. drv->fw_index = drv->cfg->ucode_api_max;
  156. sprintf(tag, "%d", drv->fw_index);
  157. } else {
  158. drv->fw_index--;
  159. sprintf(tag, "%d", drv->fw_index);
  160. }
  161. if (drv->fw_index < drv->cfg->ucode_api_min) {
  162. IWL_ERR(drv, "no suitable firmware found!\n");
  163. return -ENOENT;
  164. }
  165. sprintf(drv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
  166. IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
  167. (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  168. ? "EXPERIMENTAL " : "",
  169. drv->firmware_name);
  170. return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
  171. drv->trans->dev,
  172. GFP_KERNEL, drv, iwl_ucode_callback);
  173. }
  174. struct fw_img_parsing {
  175. struct fw_sec sec[IWL_UCODE_SECTION_MAX];
  176. int sec_counter;
  177. };
  178. /*
  179. * struct fw_sec_parsing: to extract fw section and it's offset from tlv
  180. */
  181. struct fw_sec_parsing {
  182. __le32 offset;
  183. const u8 data[];
  184. } __packed;
  185. /**
  186. * struct iwl_tlv_calib_data - parse the default calib data from TLV
  187. *
  188. * @ucode_type: the uCode to which the following default calib relates.
  189. * @calib: default calibrations.
  190. */
  191. struct iwl_tlv_calib_data {
  192. __le32 ucode_type;
  193. __le64 calib;
  194. } __packed;
  195. struct iwl_firmware_pieces {
  196. struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
  197. u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
  198. u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
  199. };
  200. /*
  201. * These functions are just to extract uCode section data from the pieces
  202. * structure.
  203. */
  204. static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
  205. enum iwl_ucode_type type,
  206. int sec)
  207. {
  208. return &pieces->img[type].sec[sec];
  209. }
  210. static void set_sec_data(struct iwl_firmware_pieces *pieces,
  211. enum iwl_ucode_type type,
  212. int sec,
  213. const void *data)
  214. {
  215. pieces->img[type].sec[sec].data = data;
  216. }
  217. static void set_sec_size(struct iwl_firmware_pieces *pieces,
  218. enum iwl_ucode_type type,
  219. int sec,
  220. size_t size)
  221. {
  222. pieces->img[type].sec[sec].size = size;
  223. }
  224. static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
  225. enum iwl_ucode_type type,
  226. int sec)
  227. {
  228. return pieces->img[type].sec[sec].size;
  229. }
  230. static void set_sec_offset(struct iwl_firmware_pieces *pieces,
  231. enum iwl_ucode_type type,
  232. int sec,
  233. u32 offset)
  234. {
  235. pieces->img[type].sec[sec].offset = offset;
  236. }
  237. /*
  238. * Gets uCode section from tlv.
  239. */
  240. static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
  241. const void *data, enum iwl_ucode_type type,
  242. int size)
  243. {
  244. struct fw_img_parsing *img;
  245. struct fw_sec *sec;
  246. struct fw_sec_parsing *sec_parse;
  247. if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
  248. return -1;
  249. sec_parse = (struct fw_sec_parsing *)data;
  250. img = &pieces->img[type];
  251. sec = &img->sec[img->sec_counter];
  252. sec->offset = le32_to_cpu(sec_parse->offset);
  253. sec->data = sec_parse->data;
  254. sec->size = size - sizeof(sec_parse->offset);
  255. ++img->sec_counter;
  256. return 0;
  257. }
  258. static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
  259. {
  260. struct iwl_tlv_calib_data *def_calib =
  261. (struct iwl_tlv_calib_data *)data;
  262. u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
  263. if (ucode_type >= IWL_UCODE_TYPE_MAX) {
  264. IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
  265. ucode_type);
  266. return -EINVAL;
  267. }
  268. drv->fw.default_calib[ucode_type] = le64_to_cpu(def_calib->calib);
  269. return 0;
  270. }
  271. static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
  272. const struct firmware *ucode_raw,
  273. struct iwl_firmware_pieces *pieces)
  274. {
  275. struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
  276. u32 api_ver, hdr_size, build;
  277. char buildstr[25];
  278. const u8 *src;
  279. drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
  280. api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
  281. switch (api_ver) {
  282. default:
  283. hdr_size = 28;
  284. if (ucode_raw->size < hdr_size) {
  285. IWL_ERR(drv, "File size too small!\n");
  286. return -EINVAL;
  287. }
  288. build = le32_to_cpu(ucode->u.v2.build);
  289. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
  290. le32_to_cpu(ucode->u.v2.inst_size));
  291. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
  292. le32_to_cpu(ucode->u.v2.data_size));
  293. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
  294. le32_to_cpu(ucode->u.v2.init_size));
  295. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
  296. le32_to_cpu(ucode->u.v2.init_data_size));
  297. src = ucode->u.v2.data;
  298. break;
  299. case 0:
  300. case 1:
  301. case 2:
  302. hdr_size = 24;
  303. if (ucode_raw->size < hdr_size) {
  304. IWL_ERR(drv, "File size too small!\n");
  305. return -EINVAL;
  306. }
  307. build = 0;
  308. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
  309. le32_to_cpu(ucode->u.v1.inst_size));
  310. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
  311. le32_to_cpu(ucode->u.v1.data_size));
  312. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
  313. le32_to_cpu(ucode->u.v1.init_size));
  314. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
  315. le32_to_cpu(ucode->u.v1.init_data_size));
  316. src = ucode->u.v1.data;
  317. break;
  318. }
  319. if (build)
  320. sprintf(buildstr, " build %u%s", build,
  321. (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  322. ? " (EXP)" : "");
  323. else
  324. buildstr[0] = '\0';
  325. snprintf(drv->fw.fw_version,
  326. sizeof(drv->fw.fw_version),
  327. "%u.%u.%u.%u%s",
  328. IWL_UCODE_MAJOR(drv->fw.ucode_ver),
  329. IWL_UCODE_MINOR(drv->fw.ucode_ver),
  330. IWL_UCODE_API(drv->fw.ucode_ver),
  331. IWL_UCODE_SERIAL(drv->fw.ucode_ver),
  332. buildstr);
  333. /* Verify size of file vs. image size info in file's header */
  334. if (ucode_raw->size != hdr_size +
  335. get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
  336. get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
  337. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
  338. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
  339. IWL_ERR(drv,
  340. "uCode file size %d does not match expected size\n",
  341. (int)ucode_raw->size);
  342. return -EINVAL;
  343. }
  344. set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
  345. src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
  346. set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
  347. IWLAGN_RTC_INST_LOWER_BOUND);
  348. set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
  349. src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
  350. set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
  351. IWLAGN_RTC_DATA_LOWER_BOUND);
  352. set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
  353. src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
  354. set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
  355. IWLAGN_RTC_INST_LOWER_BOUND);
  356. set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
  357. src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
  358. set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
  359. IWLAGN_RTC_DATA_LOWER_BOUND);
  360. return 0;
  361. }
  362. static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
  363. const struct firmware *ucode_raw,
  364. struct iwl_firmware_pieces *pieces,
  365. struct iwl_ucode_capabilities *capa)
  366. {
  367. struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
  368. struct iwl_ucode_tlv *tlv;
  369. size_t len = ucode_raw->size;
  370. const u8 *data;
  371. u32 tlv_len;
  372. enum iwl_ucode_tlv_type tlv_type;
  373. const u8 *tlv_data;
  374. char buildstr[25];
  375. u32 build;
  376. if (len < sizeof(*ucode)) {
  377. IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
  378. return -EINVAL;
  379. }
  380. if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
  381. IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
  382. le32_to_cpu(ucode->magic));
  383. return -EINVAL;
  384. }
  385. drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
  386. build = le32_to_cpu(ucode->build);
  387. if (build)
  388. sprintf(buildstr, " build %u%s", build,
  389. (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  390. ? " (EXP)" : "");
  391. else
  392. buildstr[0] = '\0';
  393. snprintf(drv->fw.fw_version,
  394. sizeof(drv->fw.fw_version),
  395. "%u.%u.%u.%u%s",
  396. IWL_UCODE_MAJOR(drv->fw.ucode_ver),
  397. IWL_UCODE_MINOR(drv->fw.ucode_ver),
  398. IWL_UCODE_API(drv->fw.ucode_ver),
  399. IWL_UCODE_SERIAL(drv->fw.ucode_ver),
  400. buildstr);
  401. data = ucode->data;
  402. len -= sizeof(*ucode);
  403. while (len >= sizeof(*tlv)) {
  404. len -= sizeof(*tlv);
  405. tlv = (void *)data;
  406. tlv_len = le32_to_cpu(tlv->length);
  407. tlv_type = le32_to_cpu(tlv->type);
  408. tlv_data = tlv->data;
  409. if (len < tlv_len) {
  410. IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
  411. len, tlv_len);
  412. return -EINVAL;
  413. }
  414. len -= ALIGN(tlv_len, 4);
  415. data += sizeof(*tlv) + ALIGN(tlv_len, 4);
  416. switch (tlv_type) {
  417. case IWL_UCODE_TLV_INST:
  418. set_sec_data(pieces, IWL_UCODE_REGULAR,
  419. IWL_UCODE_SECTION_INST, tlv_data);
  420. set_sec_size(pieces, IWL_UCODE_REGULAR,
  421. IWL_UCODE_SECTION_INST, tlv_len);
  422. set_sec_offset(pieces, IWL_UCODE_REGULAR,
  423. IWL_UCODE_SECTION_INST,
  424. IWLAGN_RTC_INST_LOWER_BOUND);
  425. break;
  426. case IWL_UCODE_TLV_DATA:
  427. set_sec_data(pieces, IWL_UCODE_REGULAR,
  428. IWL_UCODE_SECTION_DATA, tlv_data);
  429. set_sec_size(pieces, IWL_UCODE_REGULAR,
  430. IWL_UCODE_SECTION_DATA, tlv_len);
  431. set_sec_offset(pieces, IWL_UCODE_REGULAR,
  432. IWL_UCODE_SECTION_DATA,
  433. IWLAGN_RTC_DATA_LOWER_BOUND);
  434. break;
  435. case IWL_UCODE_TLV_INIT:
  436. set_sec_data(pieces, IWL_UCODE_INIT,
  437. IWL_UCODE_SECTION_INST, tlv_data);
  438. set_sec_size(pieces, IWL_UCODE_INIT,
  439. IWL_UCODE_SECTION_INST, tlv_len);
  440. set_sec_offset(pieces, IWL_UCODE_INIT,
  441. IWL_UCODE_SECTION_INST,
  442. IWLAGN_RTC_INST_LOWER_BOUND);
  443. break;
  444. case IWL_UCODE_TLV_INIT_DATA:
  445. set_sec_data(pieces, IWL_UCODE_INIT,
  446. IWL_UCODE_SECTION_DATA, tlv_data);
  447. set_sec_size(pieces, IWL_UCODE_INIT,
  448. IWL_UCODE_SECTION_DATA, tlv_len);
  449. set_sec_offset(pieces, IWL_UCODE_INIT,
  450. IWL_UCODE_SECTION_DATA,
  451. IWLAGN_RTC_DATA_LOWER_BOUND);
  452. break;
  453. case IWL_UCODE_TLV_BOOT:
  454. IWL_ERR(drv, "Found unexpected BOOT ucode\n");
  455. break;
  456. case IWL_UCODE_TLV_PROBE_MAX_LEN:
  457. if (tlv_len != sizeof(u32))
  458. goto invalid_tlv_len;
  459. capa->max_probe_length =
  460. le32_to_cpup((__le32 *)tlv_data);
  461. break;
  462. case IWL_UCODE_TLV_PAN:
  463. if (tlv_len)
  464. goto invalid_tlv_len;
  465. capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
  466. break;
  467. case IWL_UCODE_TLV_FLAGS:
  468. /* must be at least one u32 */
  469. if (tlv_len < sizeof(u32))
  470. goto invalid_tlv_len;
  471. /* and a proper number of u32s */
  472. if (tlv_len % sizeof(u32))
  473. goto invalid_tlv_len;
  474. /*
  475. * This driver only reads the first u32 as
  476. * right now no more features are defined,
  477. * if that changes then either the driver
  478. * will not work with the new firmware, or
  479. * it'll not take advantage of new features.
  480. */
  481. capa->flags = le32_to_cpup((__le32 *)tlv_data);
  482. break;
  483. case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
  484. if (tlv_len != sizeof(u32))
  485. goto invalid_tlv_len;
  486. pieces->init_evtlog_ptr =
  487. le32_to_cpup((__le32 *)tlv_data);
  488. break;
  489. case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
  490. if (tlv_len != sizeof(u32))
  491. goto invalid_tlv_len;
  492. pieces->init_evtlog_size =
  493. le32_to_cpup((__le32 *)tlv_data);
  494. break;
  495. case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
  496. if (tlv_len != sizeof(u32))
  497. goto invalid_tlv_len;
  498. pieces->init_errlog_ptr =
  499. le32_to_cpup((__le32 *)tlv_data);
  500. break;
  501. case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
  502. if (tlv_len != sizeof(u32))
  503. goto invalid_tlv_len;
  504. pieces->inst_evtlog_ptr =
  505. le32_to_cpup((__le32 *)tlv_data);
  506. break;
  507. case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
  508. if (tlv_len != sizeof(u32))
  509. goto invalid_tlv_len;
  510. pieces->inst_evtlog_size =
  511. le32_to_cpup((__le32 *)tlv_data);
  512. break;
  513. case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
  514. if (tlv_len != sizeof(u32))
  515. goto invalid_tlv_len;
  516. pieces->inst_errlog_ptr =
  517. le32_to_cpup((__le32 *)tlv_data);
  518. break;
  519. case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
  520. if (tlv_len)
  521. goto invalid_tlv_len;
  522. drv->fw.enhance_sensitivity_table = true;
  523. break;
  524. case IWL_UCODE_TLV_WOWLAN_INST:
  525. set_sec_data(pieces, IWL_UCODE_WOWLAN,
  526. IWL_UCODE_SECTION_INST, tlv_data);
  527. set_sec_size(pieces, IWL_UCODE_WOWLAN,
  528. IWL_UCODE_SECTION_INST, tlv_len);
  529. set_sec_offset(pieces, IWL_UCODE_WOWLAN,
  530. IWL_UCODE_SECTION_INST,
  531. IWLAGN_RTC_INST_LOWER_BOUND);
  532. break;
  533. case IWL_UCODE_TLV_WOWLAN_DATA:
  534. set_sec_data(pieces, IWL_UCODE_WOWLAN,
  535. IWL_UCODE_SECTION_DATA, tlv_data);
  536. set_sec_size(pieces, IWL_UCODE_WOWLAN,
  537. IWL_UCODE_SECTION_DATA, tlv_len);
  538. set_sec_offset(pieces, IWL_UCODE_WOWLAN,
  539. IWL_UCODE_SECTION_DATA,
  540. IWLAGN_RTC_DATA_LOWER_BOUND);
  541. break;
  542. case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
  543. if (tlv_len != sizeof(u32))
  544. goto invalid_tlv_len;
  545. capa->standard_phy_calibration_size =
  546. le32_to_cpup((__le32 *)tlv_data);
  547. break;
  548. case IWL_UCODE_TLV_SEC_RT:
  549. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
  550. tlv_len);
  551. drv->fw.mvm_fw = true;
  552. break;
  553. case IWL_UCODE_TLV_SEC_INIT:
  554. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
  555. tlv_len);
  556. drv->fw.mvm_fw = true;
  557. break;
  558. case IWL_UCODE_TLV_SEC_WOWLAN:
  559. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
  560. tlv_len);
  561. drv->fw.mvm_fw = true;
  562. break;
  563. case IWL_UCODE_TLV_DEF_CALIB:
  564. if (tlv_len != sizeof(struct iwl_tlv_calib_data))
  565. goto invalid_tlv_len;
  566. if (iwl_set_default_calib(drv, tlv_data))
  567. goto tlv_error;
  568. break;
  569. case IWL_UCODE_TLV_PHY_SKU:
  570. if (tlv_len != sizeof(u32))
  571. goto invalid_tlv_len;
  572. drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
  573. break;
  574. default:
  575. IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
  576. break;
  577. }
  578. }
  579. if (len) {
  580. IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
  581. iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
  582. return -EINVAL;
  583. }
  584. return 0;
  585. invalid_tlv_len:
  586. IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
  587. tlv_error:
  588. iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
  589. return -EINVAL;
  590. }
  591. static int alloc_pci_desc(struct iwl_drv *drv,
  592. struct iwl_firmware_pieces *pieces,
  593. enum iwl_ucode_type type)
  594. {
  595. int i;
  596. for (i = 0;
  597. i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
  598. i++)
  599. if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
  600. get_sec(pieces, type, i)))
  601. return -1;
  602. return 0;
  603. }
  604. static int validate_sec_sizes(struct iwl_drv *drv,
  605. struct iwl_firmware_pieces *pieces,
  606. const struct iwl_cfg *cfg)
  607. {
  608. IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
  609. get_sec_size(pieces, IWL_UCODE_REGULAR,
  610. IWL_UCODE_SECTION_INST));
  611. IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
  612. get_sec_size(pieces, IWL_UCODE_REGULAR,
  613. IWL_UCODE_SECTION_DATA));
  614. IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
  615. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
  616. IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
  617. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
  618. /* Verify that uCode images will fit in card's SRAM. */
  619. if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
  620. cfg->max_inst_size) {
  621. IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
  622. get_sec_size(pieces, IWL_UCODE_REGULAR,
  623. IWL_UCODE_SECTION_INST));
  624. return -1;
  625. }
  626. if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
  627. cfg->max_data_size) {
  628. IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
  629. get_sec_size(pieces, IWL_UCODE_REGULAR,
  630. IWL_UCODE_SECTION_DATA));
  631. return -1;
  632. }
  633. if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
  634. cfg->max_inst_size) {
  635. IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
  636. get_sec_size(pieces, IWL_UCODE_INIT,
  637. IWL_UCODE_SECTION_INST));
  638. return -1;
  639. }
  640. if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
  641. cfg->max_data_size) {
  642. IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
  643. get_sec_size(pieces, IWL_UCODE_REGULAR,
  644. IWL_UCODE_SECTION_DATA));
  645. return -1;
  646. }
  647. return 0;
  648. }
  649. /**
  650. * iwl_ucode_callback - callback when firmware was loaded
  651. *
  652. * If loaded successfully, copies the firmware into buffers
  653. * for the card to fetch (via DMA).
  654. */
  655. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
  656. {
  657. struct iwl_drv *drv = context;
  658. struct iwl_fw *fw = &drv->fw;
  659. struct iwl_ucode_header *ucode;
  660. int err;
  661. struct iwl_firmware_pieces pieces;
  662. const unsigned int api_max = drv->cfg->ucode_api_max;
  663. unsigned int api_ok = drv->cfg->ucode_api_ok;
  664. const unsigned int api_min = drv->cfg->ucode_api_min;
  665. u32 api_ver;
  666. int i;
  667. fw->ucode_capa.max_probe_length = 200;
  668. fw->ucode_capa.standard_phy_calibration_size =
  669. IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  670. if (!api_ok)
  671. api_ok = api_max;
  672. memset(&pieces, 0, sizeof(pieces));
  673. if (!ucode_raw) {
  674. if (drv->fw_index <= api_ok)
  675. IWL_ERR(drv,
  676. "request for firmware file '%s' failed.\n",
  677. drv->firmware_name);
  678. goto try_again;
  679. }
  680. IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
  681. drv->firmware_name, ucode_raw->size);
  682. /* Make sure that we got at least the API version number */
  683. if (ucode_raw->size < 4) {
  684. IWL_ERR(drv, "File size way too small!\n");
  685. goto try_again;
  686. }
  687. /* Data from ucode file: header followed by uCode images */
  688. ucode = (struct iwl_ucode_header *)ucode_raw->data;
  689. if (ucode->ver)
  690. err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
  691. else
  692. err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
  693. &fw->ucode_capa);
  694. if (err)
  695. goto try_again;
  696. api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
  697. /*
  698. * api_ver should match the api version forming part of the
  699. * firmware filename ... but we don't check for that and only rely
  700. * on the API version read from firmware header from here on forward
  701. */
  702. /* no api version check required for experimental uCode */
  703. if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
  704. if (api_ver < api_min || api_ver > api_max) {
  705. IWL_ERR(drv,
  706. "Driver unable to support your firmware API. "
  707. "Driver supports v%u, firmware is v%u.\n",
  708. api_max, api_ver);
  709. goto try_again;
  710. }
  711. if (api_ver < api_ok) {
  712. if (api_ok != api_max)
  713. IWL_ERR(drv, "Firmware has old API version, "
  714. "expected v%u through v%u, got v%u.\n",
  715. api_ok, api_max, api_ver);
  716. else
  717. IWL_ERR(drv, "Firmware has old API version, "
  718. "expected v%u, got v%u.\n",
  719. api_max, api_ver);
  720. IWL_ERR(drv, "New firmware can be obtained from "
  721. "http://www.intellinuxwireless.org/.\n");
  722. }
  723. }
  724. IWL_INFO(drv, "loaded firmware version %s", drv->fw.fw_version);
  725. /*
  726. * In mvm uCode there is no difference between data and instructions
  727. * sections.
  728. */
  729. if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, drv->cfg))
  730. goto try_again;
  731. /* Allocate ucode buffers for card's bus-master loading ... */
  732. /* Runtime instructions and 2 copies of data:
  733. * 1) unmodified from disk
  734. * 2) backup cache for save/restore during power-downs */
  735. for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
  736. if (alloc_pci_desc(drv, &pieces, i))
  737. goto err_pci_alloc;
  738. /* Now that we can no longer fail, copy information */
  739. /*
  740. * The (size - 16) / 12 formula is based on the information recorded
  741. * for each event, which is of mode 1 (including timestamp) for all
  742. * new microcodes that include this information.
  743. */
  744. fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
  745. if (pieces.init_evtlog_size)
  746. fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
  747. else
  748. fw->init_evtlog_size =
  749. drv->cfg->base_params->max_event_log_size;
  750. fw->init_errlog_ptr = pieces.init_errlog_ptr;
  751. fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
  752. if (pieces.inst_evtlog_size)
  753. fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
  754. else
  755. fw->inst_evtlog_size =
  756. drv->cfg->base_params->max_event_log_size;
  757. fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
  758. /*
  759. * figure out the offset of chain noise reset and gain commands
  760. * base on the size of standard phy calibration commands table size
  761. */
  762. if (fw->ucode_capa.standard_phy_calibration_size >
  763. IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
  764. fw->ucode_capa.standard_phy_calibration_size =
  765. IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  766. /* We have our copies now, allow OS release its copies */
  767. release_firmware(ucode_raw);
  768. complete(&drv->request_firmware_complete);
  769. drv->op_mode = iwl_dvm_ops.start(drv->trans, drv->cfg, &drv->fw);
  770. if (!drv->op_mode)
  771. goto out_unbind;
  772. return;
  773. try_again:
  774. /* try next, if any */
  775. release_firmware(ucode_raw);
  776. if (iwl_request_firmware(drv, false))
  777. goto out_unbind;
  778. return;
  779. err_pci_alloc:
  780. IWL_ERR(drv, "failed to allocate pci memory\n");
  781. iwl_dealloc_ucode(drv);
  782. release_firmware(ucode_raw);
  783. out_unbind:
  784. complete(&drv->request_firmware_complete);
  785. device_release_driver(drv->trans->dev);
  786. }
  787. struct iwl_drv *iwl_drv_start(struct iwl_trans *trans,
  788. const struct iwl_cfg *cfg)
  789. {
  790. struct iwl_drv *drv;
  791. int ret;
  792. drv = kzalloc(sizeof(*drv), GFP_KERNEL);
  793. if (!drv) {
  794. dev_printk(KERN_ERR, trans->dev, "Couldn't allocate iwl_drv");
  795. return NULL;
  796. }
  797. drv->trans = trans;
  798. drv->dev = trans->dev;
  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. }
  818. /* shared module parameters */
  819. struct iwl_mod_params iwlwifi_mod_params = {
  820. .amsdu_size_8K = 1,
  821. .restart_fw = 1,
  822. .plcp_check = true,
  823. .bt_coex_active = true,
  824. .power_level = IWL_POWER_INDEX_1,
  825. .bt_ch_announce = true,
  826. .auto_agg = true,
  827. /* the rest are 0 by default */
  828. };
  829. #ifdef CONFIG_IWLWIFI_DEBUG
  830. module_param_named(debug, iwlwifi_mod_params.debug_level, uint,
  831. S_IRUGO | S_IWUSR);
  832. MODULE_PARM_DESC(debug, "debug output mask");
  833. #endif
  834. module_param_named(swcrypto, iwlwifi_mod_params.sw_crypto, int, S_IRUGO);
  835. MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
  836. module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, S_IRUGO);
  837. MODULE_PARM_DESC(11n_disable,
  838. "disable 11n functionality, bitmap: 1: full, 2: agg TX, 4: agg RX");
  839. module_param_named(amsdu_size_8K, iwlwifi_mod_params.amsdu_size_8K,
  840. int, S_IRUGO);
  841. MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
  842. module_param_named(fw_restart, iwlwifi_mod_params.restart_fw, int, S_IRUGO);
  843. MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");
  844. module_param_named(antenna_coupling, iwlwifi_mod_params.ant_coupling,
  845. int, S_IRUGO);
  846. MODULE_PARM_DESC(antenna_coupling,
  847. "specify antenna coupling in dB (defualt: 0 dB)");
  848. module_param_named(bt_ch_inhibition, iwlwifi_mod_params.bt_ch_announce,
  849. bool, S_IRUGO);
  850. MODULE_PARM_DESC(bt_ch_inhibition,
  851. "Enable BT channel inhibition (default: enable)");
  852. module_param_named(plcp_check, iwlwifi_mod_params.plcp_check, bool, S_IRUGO);
  853. MODULE_PARM_DESC(plcp_check, "Check plcp health (default: 1 [enabled])");
  854. module_param_named(wd_disable, iwlwifi_mod_params.wd_disable, int, S_IRUGO);
  855. MODULE_PARM_DESC(wd_disable,
  856. "Disable stuck queue watchdog timer 0=system default, "
  857. "1=disable, 2=enable (default: 0)");
  858. /*
  859. * set bt_coex_active to true, uCode will do kill/defer
  860. * every time the priority line is asserted (BT is sending signals on the
  861. * priority line in the PCIx).
  862. * set bt_coex_active to false, uCode will ignore the BT activity and
  863. * perform the normal operation
  864. *
  865. * User might experience transmit issue on some platform due to WiFi/BT
  866. * co-exist problem. The possible behaviors are:
  867. * Able to scan and finding all the available AP
  868. * Not able to associate with any AP
  869. * On those platforms, WiFi communication can be restored by set
  870. * "bt_coex_active" module parameter to "false"
  871. *
  872. * default: bt_coex_active = true (BT_COEX_ENABLE)
  873. */
  874. module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
  875. bool, S_IRUGO);
  876. MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
  877. module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, S_IRUGO);
  878. MODULE_PARM_DESC(led_mode, "0=system default, "
  879. "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
  880. module_param_named(power_save, iwlwifi_mod_params.power_save,
  881. bool, S_IRUGO);
  882. MODULE_PARM_DESC(power_save,
  883. "enable WiFi power management (default: disable)");
  884. module_param_named(power_level, iwlwifi_mod_params.power_level,
  885. int, S_IRUGO);
  886. MODULE_PARM_DESC(power_level,
  887. "default power save level (range from 1 - 5, default: 1)");
  888. module_param_named(auto_agg, iwlwifi_mod_params.auto_agg,
  889. bool, S_IRUGO);
  890. MODULE_PARM_DESC(auto_agg,
  891. "enable agg w/o check traffic load (default: enable)");