iwl-drv.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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. complete(&drv->request_firmware_complete);
  770. drv->op_mode = iwl_dvm_ops.start(drv->trans, drv->cfg, &drv->fw);
  771. if (!drv->op_mode)
  772. goto out_free_fw;
  773. return;
  774. try_again:
  775. /* try next, if any */
  776. release_firmware(ucode_raw);
  777. if (iwl_request_firmware(drv, false))
  778. goto out_unbind;
  779. return;
  780. out_free_fw:
  781. IWL_ERR(drv, "failed to allocate pci memory\n");
  782. iwl_dealloc_ucode(drv);
  783. release_firmware(ucode_raw);
  784. out_unbind:
  785. complete(&drv->request_firmware_complete);
  786. device_release_driver(drv->trans->dev);
  787. }
  788. struct iwl_drv *iwl_drv_start(struct iwl_trans *trans,
  789. const struct iwl_cfg *cfg)
  790. {
  791. struct iwl_drv *drv;
  792. int ret;
  793. drv = kzalloc(sizeof(*drv), GFP_KERNEL);
  794. if (!drv)
  795. return NULL;
  796. drv->trans = trans;
  797. drv->dev = trans->dev;
  798. drv->cfg = cfg;
  799. init_completion(&drv->request_firmware_complete);
  800. ret = iwl_request_firmware(drv, true);
  801. if (ret) {
  802. IWL_ERR(trans, "Couldn't request the fw\n");
  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. }
  817. /* shared module parameters */
  818. struct iwl_mod_params iwlwifi_mod_params = {
  819. .amsdu_size_8K = 1,
  820. .restart_fw = 1,
  821. .plcp_check = true,
  822. .bt_coex_active = true,
  823. .power_level = IWL_POWER_INDEX_1,
  824. .bt_ch_announce = true,
  825. .auto_agg = true,
  826. /* the rest are 0 by default */
  827. };
  828. #ifdef CONFIG_IWLWIFI_DEBUG
  829. module_param_named(debug, iwlwifi_mod_params.debug_level, uint,
  830. S_IRUGO | S_IWUSR);
  831. MODULE_PARM_DESC(debug, "debug output mask");
  832. #endif
  833. module_param_named(swcrypto, iwlwifi_mod_params.sw_crypto, int, S_IRUGO);
  834. MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
  835. module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, S_IRUGO);
  836. MODULE_PARM_DESC(11n_disable,
  837. "disable 11n functionality, bitmap: 1: full, 2: agg TX, 4: agg RX");
  838. module_param_named(amsdu_size_8K, iwlwifi_mod_params.amsdu_size_8K,
  839. int, S_IRUGO);
  840. MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
  841. module_param_named(fw_restart, iwlwifi_mod_params.restart_fw, int, S_IRUGO);
  842. MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");
  843. module_param_named(antenna_coupling, iwlwifi_mod_params.ant_coupling,
  844. int, S_IRUGO);
  845. MODULE_PARM_DESC(antenna_coupling,
  846. "specify antenna coupling in dB (defualt: 0 dB)");
  847. module_param_named(bt_ch_inhibition, iwlwifi_mod_params.bt_ch_announce,
  848. bool, S_IRUGO);
  849. MODULE_PARM_DESC(bt_ch_inhibition,
  850. "Enable BT channel inhibition (default: enable)");
  851. module_param_named(plcp_check, iwlwifi_mod_params.plcp_check, bool, S_IRUGO);
  852. MODULE_PARM_DESC(plcp_check, "Check plcp health (default: 1 [enabled])");
  853. module_param_named(wd_disable, iwlwifi_mod_params.wd_disable, int, S_IRUGO);
  854. MODULE_PARM_DESC(wd_disable,
  855. "Disable stuck queue watchdog timer 0=system default, "
  856. "1=disable, 2=enable (default: 0)");
  857. /*
  858. * set bt_coex_active to true, uCode will do kill/defer
  859. * every time the priority line is asserted (BT is sending signals on the
  860. * priority line in the PCIx).
  861. * set bt_coex_active to false, uCode will ignore the BT activity and
  862. * perform the normal operation
  863. *
  864. * User might experience transmit issue on some platform due to WiFi/BT
  865. * co-exist problem. The possible behaviors are:
  866. * Able to scan and finding all the available AP
  867. * Not able to associate with any AP
  868. * On those platforms, WiFi communication can be restored by set
  869. * "bt_coex_active" module parameter to "false"
  870. *
  871. * default: bt_coex_active = true (BT_COEX_ENABLE)
  872. */
  873. module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
  874. bool, S_IRUGO);
  875. MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
  876. module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, S_IRUGO);
  877. MODULE_PARM_DESC(led_mode, "0=system default, "
  878. "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
  879. module_param_named(power_save, iwlwifi_mod_params.power_save,
  880. bool, S_IRUGO);
  881. MODULE_PARM_DESC(power_save,
  882. "enable WiFi power management (default: disable)");
  883. module_param_named(power_level, iwlwifi_mod_params.power_level,
  884. int, S_IRUGO);
  885. MODULE_PARM_DESC(power_level,
  886. "default power save level (range from 1 - 5, default: 1)");
  887. module_param_named(auto_agg, iwlwifi_mod_params.auto_agg,
  888. bool, S_IRUGO);
  889. MODULE_PARM_DESC(auto_agg,
  890. "enable agg w/o check traffic load (default: enable)");
  891. module_param_named(5ghz_disable, iwlwifi_mod_params.disable_5ghz,
  892. bool, S_IRUGO);
  893. MODULE_PARM_DESC(5ghz_disable, "disable 5GHz band (default: 0 [enabled])");