iwl-drv.c 30 KB

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