iwl-drv.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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 - 2014 Intel Corporation. All rights reserved.
  9. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  23. * USA
  24. *
  25. * The full GNU General Public License is included in this distribution
  26. * in the file called COPYING.
  27. *
  28. * Contact Information:
  29. * Intel Linux Wireless <ilw@linux.intel.com>
  30. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  31. *
  32. * BSD LICENSE
  33. *
  34. * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
  35. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  36. * All rights reserved.
  37. *
  38. * Redistribution and use in source and binary forms, with or without
  39. * modification, are permitted provided that the following conditions
  40. * are met:
  41. *
  42. * * Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * * Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in
  46. * the documentation and/or other materials provided with the
  47. * distribution.
  48. * * Neither the name Intel Corporation nor the names of its
  49. * contributors may be used to endorse or promote products derived
  50. * from this software without specific prior written permission.
  51. *
  52. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  53. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  54. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  55. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  56. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  57. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  58. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  59. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  60. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  61. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  62. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. *****************************************************************************/
  65. #include <linux/completion.h>
  66. #include <linux/dma-mapping.h>
  67. #include <linux/firmware.h>
  68. #include <linux/module.h>
  69. #include <linux/vmalloc.h>
  70. #include "iwl-drv.h"
  71. #include "iwl-csr.h"
  72. #include "iwl-debug.h"
  73. #include "iwl-trans.h"
  74. #include "iwl-op-mode.h"
  75. #include "iwl-agn-hw.h"
  76. #include "iwl-fw.h"
  77. #include "iwl-config.h"
  78. #include "iwl-modparams.h"
  79. /* private includes */
  80. #include "iwl-fw-file.h"
  81. /******************************************************************************
  82. *
  83. * module boiler plate
  84. *
  85. ******************************************************************************/
  86. /*
  87. * module name, copyright, version, etc.
  88. */
  89. #define DRV_DESCRIPTION "Intel(R) Wireless WiFi driver for Linux"
  90. #ifdef CONFIG_IWLWIFI_DEBUG
  91. #define VD "d"
  92. #else
  93. #define VD
  94. #endif
  95. #define DRV_VERSION IWLWIFI_VERSION VD
  96. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  97. MODULE_VERSION(DRV_VERSION);
  98. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  99. MODULE_LICENSE("GPL");
  100. #ifdef CONFIG_IWLWIFI_DEBUGFS
  101. static struct dentry *iwl_dbgfs_root;
  102. #endif
  103. /**
  104. * struct iwl_drv - drv common data
  105. * @list: list of drv structures using this opmode
  106. * @fw: the iwl_fw structure
  107. * @op_mode: the running op_mode
  108. * @trans: transport layer
  109. * @dev: for debug prints only
  110. * @cfg: configuration struct
  111. * @fw_index: firmware revision to try loading
  112. * @firmware_name: composite filename of ucode file to load
  113. * @request_firmware_complete: the firmware has been obtained from user space
  114. */
  115. struct iwl_drv {
  116. struct list_head list;
  117. struct iwl_fw fw;
  118. struct iwl_op_mode *op_mode;
  119. struct iwl_trans *trans;
  120. struct device *dev;
  121. const struct iwl_cfg *cfg;
  122. int fw_index; /* firmware we're trying to load */
  123. char firmware_name[32]; /* name of firmware file to load */
  124. struct completion request_firmware_complete;
  125. #ifdef CONFIG_IWLWIFI_DEBUGFS
  126. struct dentry *dbgfs_drv;
  127. struct dentry *dbgfs_trans;
  128. struct dentry *dbgfs_op_mode;
  129. #endif
  130. };
  131. enum {
  132. DVM_OP_MODE = 0,
  133. MVM_OP_MODE = 1,
  134. };
  135. /* Protects the table contents, i.e. the ops pointer & drv list */
  136. static struct mutex iwlwifi_opmode_table_mtx;
  137. static struct iwlwifi_opmode_table {
  138. const char *name; /* name: iwldvm, iwlmvm, etc */
  139. const struct iwl_op_mode_ops *ops; /* pointer to op_mode ops */
  140. struct list_head drv; /* list of devices using this op_mode */
  141. } iwlwifi_opmode_table[] = { /* ops set when driver is initialized */
  142. [DVM_OP_MODE] = { .name = "iwldvm", .ops = NULL },
  143. [MVM_OP_MODE] = { .name = "iwlmvm", .ops = NULL },
  144. };
  145. #define IWL_DEFAULT_SCAN_CHANNELS 40
  146. /*
  147. * struct fw_sec: Just for the image parsing proccess.
  148. * For the fw storage we are using struct fw_desc.
  149. */
  150. struct fw_sec {
  151. const void *data; /* the sec data */
  152. size_t size; /* section size */
  153. u32 offset; /* offset of writing in the device */
  154. };
  155. static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
  156. {
  157. vfree(desc->data);
  158. desc->data = NULL;
  159. desc->len = 0;
  160. }
  161. static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
  162. {
  163. int i;
  164. for (i = 0; i < IWL_UCODE_SECTION_MAX; i++)
  165. iwl_free_fw_desc(drv, &img->sec[i]);
  166. }
  167. static void iwl_dealloc_ucode(struct iwl_drv *drv)
  168. {
  169. int i;
  170. for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
  171. iwl_free_fw_img(drv, drv->fw.img + i);
  172. }
  173. static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
  174. struct fw_sec *sec)
  175. {
  176. void *data;
  177. desc->data = NULL;
  178. if (!sec || !sec->size)
  179. return -EINVAL;
  180. data = vmalloc(sec->size);
  181. if (!data)
  182. return -ENOMEM;
  183. desc->len = sec->size;
  184. desc->offset = sec->offset;
  185. memcpy(data, sec->data, desc->len);
  186. desc->data = data;
  187. return 0;
  188. }
  189. static void iwl_req_fw_callback(const struct firmware *ucode_raw,
  190. void *context);
  191. #define UCODE_EXPERIMENTAL_INDEX 100
  192. #define UCODE_EXPERIMENTAL_TAG "exp"
  193. static int iwl_request_firmware(struct iwl_drv *drv, bool first)
  194. {
  195. const char *name_pre = drv->cfg->fw_name_pre;
  196. char tag[8];
  197. if (first) {
  198. #ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
  199. drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
  200. strcpy(tag, UCODE_EXPERIMENTAL_TAG);
  201. } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
  202. #endif
  203. drv->fw_index = drv->cfg->ucode_api_max;
  204. sprintf(tag, "%d", drv->fw_index);
  205. } else {
  206. drv->fw_index--;
  207. sprintf(tag, "%d", drv->fw_index);
  208. }
  209. if (drv->fw_index < drv->cfg->ucode_api_min) {
  210. IWL_ERR(drv, "no suitable firmware found!\n");
  211. return -ENOENT;
  212. }
  213. snprintf(drv->firmware_name, sizeof(drv->firmware_name), "%s%s.ucode",
  214. name_pre, tag);
  215. /*
  216. * Starting 8000B - FW name format has changed. This overwrites the
  217. * previous name and uses the new format.
  218. */
  219. if (drv->trans->cfg->device_family == IWL_DEVICE_FAMILY_8000) {
  220. char rev_step[2] = {
  221. 'A' + CSR_HW_REV_STEP(drv->trans->hw_rev), 0
  222. };
  223. /* A-step doesn't have an indication */
  224. if (CSR_HW_REV_STEP(drv->trans->hw_rev) == SILICON_A_STEP)
  225. rev_step[0] = 0;
  226. snprintf(drv->firmware_name, sizeof(drv->firmware_name),
  227. "%s%s-%s.ucode", name_pre, rev_step, tag);
  228. }
  229. IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
  230. (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  231. ? "EXPERIMENTAL " : "",
  232. drv->firmware_name);
  233. return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
  234. drv->trans->dev,
  235. GFP_KERNEL, drv, iwl_req_fw_callback);
  236. }
  237. struct fw_img_parsing {
  238. struct fw_sec sec[IWL_UCODE_SECTION_MAX];
  239. int sec_counter;
  240. };
  241. /*
  242. * struct fw_sec_parsing: to extract fw section and it's offset from tlv
  243. */
  244. struct fw_sec_parsing {
  245. __le32 offset;
  246. const u8 data[];
  247. } __packed;
  248. /**
  249. * struct iwl_tlv_calib_data - parse the default calib data from TLV
  250. *
  251. * @ucode_type: the uCode to which the following default calib relates.
  252. * @calib: default calibrations.
  253. */
  254. struct iwl_tlv_calib_data {
  255. __le32 ucode_type;
  256. struct iwl_tlv_calib_ctrl calib;
  257. } __packed;
  258. struct iwl_firmware_pieces {
  259. struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
  260. u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
  261. u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
  262. };
  263. /*
  264. * These functions are just to extract uCode section data from the pieces
  265. * structure.
  266. */
  267. static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
  268. enum iwl_ucode_type type,
  269. int sec)
  270. {
  271. return &pieces->img[type].sec[sec];
  272. }
  273. static void set_sec_data(struct iwl_firmware_pieces *pieces,
  274. enum iwl_ucode_type type,
  275. int sec,
  276. const void *data)
  277. {
  278. pieces->img[type].sec[sec].data = data;
  279. }
  280. static void set_sec_size(struct iwl_firmware_pieces *pieces,
  281. enum iwl_ucode_type type,
  282. int sec,
  283. size_t size)
  284. {
  285. pieces->img[type].sec[sec].size = size;
  286. }
  287. static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
  288. enum iwl_ucode_type type,
  289. int sec)
  290. {
  291. return pieces->img[type].sec[sec].size;
  292. }
  293. static void set_sec_offset(struct iwl_firmware_pieces *pieces,
  294. enum iwl_ucode_type type,
  295. int sec,
  296. u32 offset)
  297. {
  298. pieces->img[type].sec[sec].offset = offset;
  299. }
  300. static int iwl_store_cscheme(struct iwl_fw *fw, const u8 *data, const u32 len)
  301. {
  302. int i, j;
  303. struct iwl_fw_cscheme_list *l = (struct iwl_fw_cscheme_list *)data;
  304. struct iwl_fw_cipher_scheme *fwcs;
  305. struct ieee80211_cipher_scheme *cs;
  306. u32 cipher;
  307. if (len < sizeof(*l) ||
  308. len < sizeof(l->size) + l->size * sizeof(l->cs[0]))
  309. return -EINVAL;
  310. for (i = 0, j = 0; i < IWL_UCODE_MAX_CS && i < l->size; i++) {
  311. fwcs = &l->cs[j];
  312. cipher = le32_to_cpu(fwcs->cipher);
  313. /* we skip schemes with zero cipher suite selector */
  314. if (!cipher)
  315. continue;
  316. cs = &fw->cs[j++];
  317. cs->cipher = cipher;
  318. cs->iftype = BIT(NL80211_IFTYPE_STATION);
  319. cs->hdr_len = fwcs->hdr_len;
  320. cs->pn_len = fwcs->pn_len;
  321. cs->pn_off = fwcs->pn_off;
  322. cs->key_idx_off = fwcs->key_idx_off;
  323. cs->key_idx_mask = fwcs->key_idx_mask;
  324. cs->key_idx_shift = fwcs->key_idx_shift;
  325. cs->mic_len = fwcs->mic_len;
  326. }
  327. return 0;
  328. }
  329. /*
  330. * Gets uCode section from tlv.
  331. */
  332. static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
  333. const void *data, enum iwl_ucode_type type,
  334. int size)
  335. {
  336. struct fw_img_parsing *img;
  337. struct fw_sec *sec;
  338. struct fw_sec_parsing *sec_parse;
  339. if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
  340. return -1;
  341. sec_parse = (struct fw_sec_parsing *)data;
  342. img = &pieces->img[type];
  343. sec = &img->sec[img->sec_counter];
  344. sec->offset = le32_to_cpu(sec_parse->offset);
  345. sec->data = sec_parse->data;
  346. sec->size = size - sizeof(sec_parse->offset);
  347. ++img->sec_counter;
  348. return 0;
  349. }
  350. static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
  351. {
  352. struct iwl_tlv_calib_data *def_calib =
  353. (struct iwl_tlv_calib_data *)data;
  354. u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
  355. if (ucode_type >= IWL_UCODE_TYPE_MAX) {
  356. IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
  357. ucode_type);
  358. return -EINVAL;
  359. }
  360. drv->fw.default_calib[ucode_type].flow_trigger =
  361. def_calib->calib.flow_trigger;
  362. drv->fw.default_calib[ucode_type].event_trigger =
  363. def_calib->calib.event_trigger;
  364. return 0;
  365. }
  366. static int iwl_set_ucode_api_flags(struct iwl_drv *drv, const u8 *data,
  367. struct iwl_ucode_capabilities *capa)
  368. {
  369. const struct iwl_ucode_api *ucode_api = (void *)data;
  370. u32 api_index = le32_to_cpu(ucode_api->api_index);
  371. if (api_index >= IWL_API_ARRAY_SIZE) {
  372. IWL_ERR(drv, "api_index larger than supported by driver\n");
  373. return -EINVAL;
  374. }
  375. capa->api[api_index] = le32_to_cpu(ucode_api->api_flags);
  376. return 0;
  377. }
  378. static int iwl_set_ucode_capabilities(struct iwl_drv *drv, const u8 *data,
  379. struct iwl_ucode_capabilities *capa)
  380. {
  381. const struct iwl_ucode_capa *ucode_capa = (void *)data;
  382. u32 api_index = le32_to_cpu(ucode_capa->api_index);
  383. if (api_index >= IWL_CAPABILITIES_ARRAY_SIZE) {
  384. IWL_ERR(drv, "api_index larger than supported by driver\n");
  385. return -EINVAL;
  386. }
  387. capa->capa[api_index] = le32_to_cpu(ucode_capa->api_capa);
  388. return 0;
  389. }
  390. static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
  391. const struct firmware *ucode_raw,
  392. struct iwl_firmware_pieces *pieces)
  393. {
  394. struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
  395. u32 api_ver, hdr_size, build;
  396. char buildstr[25];
  397. const u8 *src;
  398. drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
  399. api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
  400. switch (api_ver) {
  401. default:
  402. hdr_size = 28;
  403. if (ucode_raw->size < hdr_size) {
  404. IWL_ERR(drv, "File size too small!\n");
  405. return -EINVAL;
  406. }
  407. build = le32_to_cpu(ucode->u.v2.build);
  408. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
  409. le32_to_cpu(ucode->u.v2.inst_size));
  410. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
  411. le32_to_cpu(ucode->u.v2.data_size));
  412. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
  413. le32_to_cpu(ucode->u.v2.init_size));
  414. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
  415. le32_to_cpu(ucode->u.v2.init_data_size));
  416. src = ucode->u.v2.data;
  417. break;
  418. case 0:
  419. case 1:
  420. case 2:
  421. hdr_size = 24;
  422. if (ucode_raw->size < hdr_size) {
  423. IWL_ERR(drv, "File size too small!\n");
  424. return -EINVAL;
  425. }
  426. build = 0;
  427. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
  428. le32_to_cpu(ucode->u.v1.inst_size));
  429. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
  430. le32_to_cpu(ucode->u.v1.data_size));
  431. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
  432. le32_to_cpu(ucode->u.v1.init_size));
  433. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
  434. le32_to_cpu(ucode->u.v1.init_data_size));
  435. src = ucode->u.v1.data;
  436. break;
  437. }
  438. if (build)
  439. sprintf(buildstr, " build %u%s", build,
  440. (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  441. ? " (EXP)" : "");
  442. else
  443. buildstr[0] = '\0';
  444. snprintf(drv->fw.fw_version,
  445. sizeof(drv->fw.fw_version),
  446. "%u.%u.%u.%u%s",
  447. IWL_UCODE_MAJOR(drv->fw.ucode_ver),
  448. IWL_UCODE_MINOR(drv->fw.ucode_ver),
  449. IWL_UCODE_API(drv->fw.ucode_ver),
  450. IWL_UCODE_SERIAL(drv->fw.ucode_ver),
  451. buildstr);
  452. /* Verify size of file vs. image size info in file's header */
  453. if (ucode_raw->size != hdr_size +
  454. get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
  455. get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
  456. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
  457. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
  458. IWL_ERR(drv,
  459. "uCode file size %d does not match expected size\n",
  460. (int)ucode_raw->size);
  461. return -EINVAL;
  462. }
  463. set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
  464. src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
  465. set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
  466. IWLAGN_RTC_INST_LOWER_BOUND);
  467. set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
  468. src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
  469. set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
  470. IWLAGN_RTC_DATA_LOWER_BOUND);
  471. set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
  472. src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
  473. set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
  474. IWLAGN_RTC_INST_LOWER_BOUND);
  475. set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
  476. src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
  477. set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
  478. IWLAGN_RTC_DATA_LOWER_BOUND);
  479. return 0;
  480. }
  481. static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
  482. const struct firmware *ucode_raw,
  483. struct iwl_firmware_pieces *pieces,
  484. struct iwl_ucode_capabilities *capa)
  485. {
  486. struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
  487. struct iwl_ucode_tlv *tlv;
  488. size_t len = ucode_raw->size;
  489. const u8 *data;
  490. u32 tlv_len;
  491. enum iwl_ucode_tlv_type tlv_type;
  492. const u8 *tlv_data;
  493. char buildstr[25];
  494. u32 build;
  495. int num_of_cpus;
  496. if (len < sizeof(*ucode)) {
  497. IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
  498. return -EINVAL;
  499. }
  500. if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
  501. IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
  502. le32_to_cpu(ucode->magic));
  503. return -EINVAL;
  504. }
  505. drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
  506. memcpy(drv->fw.human_readable, ucode->human_readable,
  507. sizeof(drv->fw.human_readable));
  508. build = le32_to_cpu(ucode->build);
  509. if (build)
  510. sprintf(buildstr, " build %u%s", build,
  511. (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  512. ? " (EXP)" : "");
  513. else
  514. buildstr[0] = '\0';
  515. snprintf(drv->fw.fw_version,
  516. sizeof(drv->fw.fw_version),
  517. "%u.%u.%u.%u%s",
  518. IWL_UCODE_MAJOR(drv->fw.ucode_ver),
  519. IWL_UCODE_MINOR(drv->fw.ucode_ver),
  520. IWL_UCODE_API(drv->fw.ucode_ver),
  521. IWL_UCODE_SERIAL(drv->fw.ucode_ver),
  522. buildstr);
  523. data = ucode->data;
  524. len -= sizeof(*ucode);
  525. while (len >= sizeof(*tlv)) {
  526. len -= sizeof(*tlv);
  527. tlv = (void *)data;
  528. tlv_len = le32_to_cpu(tlv->length);
  529. tlv_type = le32_to_cpu(tlv->type);
  530. tlv_data = tlv->data;
  531. if (len < tlv_len) {
  532. IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
  533. len, tlv_len);
  534. return -EINVAL;
  535. }
  536. len -= ALIGN(tlv_len, 4);
  537. data += sizeof(*tlv) + ALIGN(tlv_len, 4);
  538. switch (tlv_type) {
  539. case IWL_UCODE_TLV_INST:
  540. set_sec_data(pieces, IWL_UCODE_REGULAR,
  541. IWL_UCODE_SECTION_INST, tlv_data);
  542. set_sec_size(pieces, IWL_UCODE_REGULAR,
  543. IWL_UCODE_SECTION_INST, tlv_len);
  544. set_sec_offset(pieces, IWL_UCODE_REGULAR,
  545. IWL_UCODE_SECTION_INST,
  546. IWLAGN_RTC_INST_LOWER_BOUND);
  547. break;
  548. case IWL_UCODE_TLV_DATA:
  549. set_sec_data(pieces, IWL_UCODE_REGULAR,
  550. IWL_UCODE_SECTION_DATA, tlv_data);
  551. set_sec_size(pieces, IWL_UCODE_REGULAR,
  552. IWL_UCODE_SECTION_DATA, tlv_len);
  553. set_sec_offset(pieces, IWL_UCODE_REGULAR,
  554. IWL_UCODE_SECTION_DATA,
  555. IWLAGN_RTC_DATA_LOWER_BOUND);
  556. break;
  557. case IWL_UCODE_TLV_INIT:
  558. set_sec_data(pieces, IWL_UCODE_INIT,
  559. IWL_UCODE_SECTION_INST, tlv_data);
  560. set_sec_size(pieces, IWL_UCODE_INIT,
  561. IWL_UCODE_SECTION_INST, tlv_len);
  562. set_sec_offset(pieces, IWL_UCODE_INIT,
  563. IWL_UCODE_SECTION_INST,
  564. IWLAGN_RTC_INST_LOWER_BOUND);
  565. break;
  566. case IWL_UCODE_TLV_INIT_DATA:
  567. set_sec_data(pieces, IWL_UCODE_INIT,
  568. IWL_UCODE_SECTION_DATA, tlv_data);
  569. set_sec_size(pieces, IWL_UCODE_INIT,
  570. IWL_UCODE_SECTION_DATA, tlv_len);
  571. set_sec_offset(pieces, IWL_UCODE_INIT,
  572. IWL_UCODE_SECTION_DATA,
  573. IWLAGN_RTC_DATA_LOWER_BOUND);
  574. break;
  575. case IWL_UCODE_TLV_BOOT:
  576. IWL_ERR(drv, "Found unexpected BOOT ucode\n");
  577. break;
  578. case IWL_UCODE_TLV_PROBE_MAX_LEN:
  579. if (tlv_len != sizeof(u32))
  580. goto invalid_tlv_len;
  581. capa->max_probe_length =
  582. le32_to_cpup((__le32 *)tlv_data);
  583. break;
  584. case IWL_UCODE_TLV_PAN:
  585. if (tlv_len)
  586. goto invalid_tlv_len;
  587. capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
  588. break;
  589. case IWL_UCODE_TLV_FLAGS:
  590. /* must be at least one u32 */
  591. if (tlv_len < sizeof(u32))
  592. goto invalid_tlv_len;
  593. /* and a proper number of u32s */
  594. if (tlv_len % sizeof(u32))
  595. goto invalid_tlv_len;
  596. /*
  597. * This driver only reads the first u32 as
  598. * right now no more features are defined,
  599. * if that changes then either the driver
  600. * will not work with the new firmware, or
  601. * it'll not take advantage of new features.
  602. */
  603. capa->flags = le32_to_cpup((__le32 *)tlv_data);
  604. break;
  605. case IWL_UCODE_TLV_API_CHANGES_SET:
  606. if (tlv_len != sizeof(struct iwl_ucode_api))
  607. goto invalid_tlv_len;
  608. if (iwl_set_ucode_api_flags(drv, tlv_data, capa))
  609. goto tlv_error;
  610. break;
  611. case IWL_UCODE_TLV_ENABLED_CAPABILITIES:
  612. if (tlv_len != sizeof(struct iwl_ucode_capa))
  613. goto invalid_tlv_len;
  614. if (iwl_set_ucode_capabilities(drv, tlv_data, capa))
  615. goto tlv_error;
  616. break;
  617. case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
  618. if (tlv_len != sizeof(u32))
  619. goto invalid_tlv_len;
  620. pieces->init_evtlog_ptr =
  621. le32_to_cpup((__le32 *)tlv_data);
  622. break;
  623. case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
  624. if (tlv_len != sizeof(u32))
  625. goto invalid_tlv_len;
  626. pieces->init_evtlog_size =
  627. le32_to_cpup((__le32 *)tlv_data);
  628. break;
  629. case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
  630. if (tlv_len != sizeof(u32))
  631. goto invalid_tlv_len;
  632. pieces->init_errlog_ptr =
  633. le32_to_cpup((__le32 *)tlv_data);
  634. break;
  635. case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
  636. if (tlv_len != sizeof(u32))
  637. goto invalid_tlv_len;
  638. pieces->inst_evtlog_ptr =
  639. le32_to_cpup((__le32 *)tlv_data);
  640. break;
  641. case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
  642. if (tlv_len != sizeof(u32))
  643. goto invalid_tlv_len;
  644. pieces->inst_evtlog_size =
  645. le32_to_cpup((__le32 *)tlv_data);
  646. break;
  647. case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
  648. if (tlv_len != sizeof(u32))
  649. goto invalid_tlv_len;
  650. pieces->inst_errlog_ptr =
  651. le32_to_cpup((__le32 *)tlv_data);
  652. break;
  653. case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
  654. if (tlv_len)
  655. goto invalid_tlv_len;
  656. drv->fw.enhance_sensitivity_table = true;
  657. break;
  658. case IWL_UCODE_TLV_WOWLAN_INST:
  659. set_sec_data(pieces, IWL_UCODE_WOWLAN,
  660. IWL_UCODE_SECTION_INST, tlv_data);
  661. set_sec_size(pieces, IWL_UCODE_WOWLAN,
  662. IWL_UCODE_SECTION_INST, tlv_len);
  663. set_sec_offset(pieces, IWL_UCODE_WOWLAN,
  664. IWL_UCODE_SECTION_INST,
  665. IWLAGN_RTC_INST_LOWER_BOUND);
  666. break;
  667. case IWL_UCODE_TLV_WOWLAN_DATA:
  668. set_sec_data(pieces, IWL_UCODE_WOWLAN,
  669. IWL_UCODE_SECTION_DATA, tlv_data);
  670. set_sec_size(pieces, IWL_UCODE_WOWLAN,
  671. IWL_UCODE_SECTION_DATA, tlv_len);
  672. set_sec_offset(pieces, IWL_UCODE_WOWLAN,
  673. IWL_UCODE_SECTION_DATA,
  674. IWLAGN_RTC_DATA_LOWER_BOUND);
  675. break;
  676. case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
  677. if (tlv_len != sizeof(u32))
  678. goto invalid_tlv_len;
  679. capa->standard_phy_calibration_size =
  680. le32_to_cpup((__le32 *)tlv_data);
  681. break;
  682. case IWL_UCODE_TLV_SEC_RT:
  683. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
  684. tlv_len);
  685. drv->fw.mvm_fw = true;
  686. break;
  687. case IWL_UCODE_TLV_SEC_INIT:
  688. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
  689. tlv_len);
  690. drv->fw.mvm_fw = true;
  691. break;
  692. case IWL_UCODE_TLV_SEC_WOWLAN:
  693. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
  694. tlv_len);
  695. drv->fw.mvm_fw = true;
  696. break;
  697. case IWL_UCODE_TLV_DEF_CALIB:
  698. if (tlv_len != sizeof(struct iwl_tlv_calib_data))
  699. goto invalid_tlv_len;
  700. if (iwl_set_default_calib(drv, tlv_data))
  701. goto tlv_error;
  702. break;
  703. case IWL_UCODE_TLV_PHY_SKU:
  704. if (tlv_len != sizeof(u32))
  705. goto invalid_tlv_len;
  706. drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
  707. drv->fw.valid_tx_ant = (drv->fw.phy_config &
  708. FW_PHY_CFG_TX_CHAIN) >>
  709. FW_PHY_CFG_TX_CHAIN_POS;
  710. drv->fw.valid_rx_ant = (drv->fw.phy_config &
  711. FW_PHY_CFG_RX_CHAIN) >>
  712. FW_PHY_CFG_RX_CHAIN_POS;
  713. break;
  714. case IWL_UCODE_TLV_SECURE_SEC_RT:
  715. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
  716. tlv_len);
  717. drv->fw.mvm_fw = true;
  718. drv->fw.img[IWL_UCODE_REGULAR].is_secure = true;
  719. break;
  720. case IWL_UCODE_TLV_SECURE_SEC_INIT:
  721. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
  722. tlv_len);
  723. drv->fw.mvm_fw = true;
  724. drv->fw.img[IWL_UCODE_INIT].is_secure = true;
  725. break;
  726. case IWL_UCODE_TLV_SECURE_SEC_WOWLAN:
  727. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
  728. tlv_len);
  729. drv->fw.mvm_fw = true;
  730. drv->fw.img[IWL_UCODE_WOWLAN].is_secure = true;
  731. break;
  732. case IWL_UCODE_TLV_NUM_OF_CPU:
  733. if (tlv_len != sizeof(u32))
  734. goto invalid_tlv_len;
  735. num_of_cpus =
  736. le32_to_cpup((__le32 *)tlv_data);
  737. if (num_of_cpus == 2) {
  738. drv->fw.img[IWL_UCODE_REGULAR].is_dual_cpus =
  739. true;
  740. drv->fw.img[IWL_UCODE_INIT].is_dual_cpus =
  741. true;
  742. drv->fw.img[IWL_UCODE_WOWLAN].is_dual_cpus =
  743. true;
  744. } else if ((num_of_cpus > 2) || (num_of_cpus < 1)) {
  745. IWL_ERR(drv, "Driver support upto 2 CPUs\n");
  746. return -EINVAL;
  747. }
  748. break;
  749. case IWL_UCODE_TLV_CSCHEME:
  750. if (iwl_store_cscheme(&drv->fw, tlv_data, tlv_len))
  751. goto invalid_tlv_len;
  752. break;
  753. case IWL_UCODE_TLV_N_SCAN_CHANNELS:
  754. if (tlv_len != sizeof(u32))
  755. goto invalid_tlv_len;
  756. capa->n_scan_channels =
  757. le32_to_cpup((__le32 *)tlv_data);
  758. break;
  759. default:
  760. IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
  761. break;
  762. }
  763. }
  764. if (len) {
  765. IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
  766. iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
  767. return -EINVAL;
  768. }
  769. return 0;
  770. invalid_tlv_len:
  771. IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
  772. tlv_error:
  773. iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
  774. return -EINVAL;
  775. }
  776. static int iwl_alloc_ucode(struct iwl_drv *drv,
  777. struct iwl_firmware_pieces *pieces,
  778. enum iwl_ucode_type type)
  779. {
  780. int i;
  781. for (i = 0;
  782. i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
  783. i++)
  784. if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
  785. get_sec(pieces, type, i)))
  786. return -ENOMEM;
  787. return 0;
  788. }
  789. static int validate_sec_sizes(struct iwl_drv *drv,
  790. struct iwl_firmware_pieces *pieces,
  791. const struct iwl_cfg *cfg)
  792. {
  793. IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
  794. get_sec_size(pieces, IWL_UCODE_REGULAR,
  795. IWL_UCODE_SECTION_INST));
  796. IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
  797. get_sec_size(pieces, IWL_UCODE_REGULAR,
  798. IWL_UCODE_SECTION_DATA));
  799. IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
  800. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
  801. IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
  802. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
  803. /* Verify that uCode images will fit in card's SRAM. */
  804. if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
  805. cfg->max_inst_size) {
  806. IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
  807. get_sec_size(pieces, IWL_UCODE_REGULAR,
  808. IWL_UCODE_SECTION_INST));
  809. return -1;
  810. }
  811. if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
  812. cfg->max_data_size) {
  813. IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
  814. get_sec_size(pieces, IWL_UCODE_REGULAR,
  815. IWL_UCODE_SECTION_DATA));
  816. return -1;
  817. }
  818. if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
  819. cfg->max_inst_size) {
  820. IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
  821. get_sec_size(pieces, IWL_UCODE_INIT,
  822. IWL_UCODE_SECTION_INST));
  823. return -1;
  824. }
  825. if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
  826. cfg->max_data_size) {
  827. IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
  828. get_sec_size(pieces, IWL_UCODE_REGULAR,
  829. IWL_UCODE_SECTION_DATA));
  830. return -1;
  831. }
  832. return 0;
  833. }
  834. static struct iwl_op_mode *
  835. _iwl_op_mode_start(struct iwl_drv *drv, struct iwlwifi_opmode_table *op)
  836. {
  837. const struct iwl_op_mode_ops *ops = op->ops;
  838. struct dentry *dbgfs_dir = NULL;
  839. struct iwl_op_mode *op_mode = NULL;
  840. #ifdef CONFIG_IWLWIFI_DEBUGFS
  841. drv->dbgfs_op_mode = debugfs_create_dir(op->name,
  842. drv->dbgfs_drv);
  843. if (!drv->dbgfs_op_mode) {
  844. IWL_ERR(drv,
  845. "failed to create opmode debugfs directory\n");
  846. return op_mode;
  847. }
  848. dbgfs_dir = drv->dbgfs_op_mode;
  849. #endif
  850. op_mode = ops->start(drv->trans, drv->cfg, &drv->fw, dbgfs_dir);
  851. #ifdef CONFIG_IWLWIFI_DEBUGFS
  852. if (!op_mode) {
  853. debugfs_remove_recursive(drv->dbgfs_op_mode);
  854. drv->dbgfs_op_mode = NULL;
  855. }
  856. #endif
  857. return op_mode;
  858. }
  859. static void _iwl_op_mode_stop(struct iwl_drv *drv)
  860. {
  861. /* op_mode can be NULL if its start failed */
  862. if (drv->op_mode) {
  863. iwl_op_mode_stop(drv->op_mode);
  864. drv->op_mode = NULL;
  865. #ifdef CONFIG_IWLWIFI_DEBUGFS
  866. debugfs_remove_recursive(drv->dbgfs_op_mode);
  867. drv->dbgfs_op_mode = NULL;
  868. #endif
  869. }
  870. }
  871. /**
  872. * iwl_req_fw_callback - callback when firmware was loaded
  873. *
  874. * If loaded successfully, copies the firmware into buffers
  875. * for the card to fetch (via DMA).
  876. */
  877. static void iwl_req_fw_callback(const struct firmware *ucode_raw, void *context)
  878. {
  879. struct iwl_drv *drv = context;
  880. struct iwl_fw *fw = &drv->fw;
  881. struct iwl_ucode_header *ucode;
  882. struct iwlwifi_opmode_table *op;
  883. int err;
  884. struct iwl_firmware_pieces pieces;
  885. const unsigned int api_max = drv->cfg->ucode_api_max;
  886. unsigned int api_ok = drv->cfg->ucode_api_ok;
  887. const unsigned int api_min = drv->cfg->ucode_api_min;
  888. u32 api_ver;
  889. int i;
  890. bool load_module = false;
  891. fw->ucode_capa.max_probe_length = IWL_DEFAULT_MAX_PROBE_LENGTH;
  892. fw->ucode_capa.standard_phy_calibration_size =
  893. IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  894. fw->ucode_capa.n_scan_channels = IWL_DEFAULT_SCAN_CHANNELS;
  895. if (!api_ok)
  896. api_ok = api_max;
  897. memset(&pieces, 0, sizeof(pieces));
  898. if (!ucode_raw) {
  899. if (drv->fw_index <= api_ok)
  900. IWL_ERR(drv,
  901. "request for firmware file '%s' failed.\n",
  902. drv->firmware_name);
  903. goto try_again;
  904. }
  905. IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
  906. drv->firmware_name, ucode_raw->size);
  907. /* Make sure that we got at least the API version number */
  908. if (ucode_raw->size < 4) {
  909. IWL_ERR(drv, "File size way too small!\n");
  910. goto try_again;
  911. }
  912. /* Data from ucode file: header followed by uCode images */
  913. ucode = (struct iwl_ucode_header *)ucode_raw->data;
  914. if (ucode->ver)
  915. err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
  916. else
  917. err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
  918. &fw->ucode_capa);
  919. if (err)
  920. goto try_again;
  921. api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
  922. /*
  923. * api_ver should match the api version forming part of the
  924. * firmware filename ... but we don't check for that and only rely
  925. * on the API version read from firmware header from here on forward
  926. */
  927. /* no api version check required for experimental uCode */
  928. if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
  929. if (api_ver < api_min || api_ver > api_max) {
  930. IWL_ERR(drv,
  931. "Driver unable to support your firmware API. "
  932. "Driver supports v%u, firmware is v%u.\n",
  933. api_max, api_ver);
  934. goto try_again;
  935. }
  936. if (api_ver < api_ok) {
  937. if (api_ok != api_max)
  938. IWL_ERR(drv, "Firmware has old API version, "
  939. "expected v%u through v%u, got v%u.\n",
  940. api_ok, api_max, api_ver);
  941. else
  942. IWL_ERR(drv, "Firmware has old API version, "
  943. "expected v%u, got v%u.\n",
  944. api_max, api_ver);
  945. IWL_ERR(drv, "New firmware can be obtained from "
  946. "http://www.intellinuxwireless.org/.\n");
  947. }
  948. }
  949. /*
  950. * In mvm uCode there is no difference between data and instructions
  951. * sections.
  952. */
  953. if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, drv->cfg))
  954. goto try_again;
  955. /* Allocate ucode buffers for card's bus-master loading ... */
  956. /* Runtime instructions and 2 copies of data:
  957. * 1) unmodified from disk
  958. * 2) backup cache for save/restore during power-downs */
  959. for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
  960. if (iwl_alloc_ucode(drv, &pieces, i))
  961. goto out_free_fw;
  962. /* Now that we can no longer fail, copy information */
  963. /*
  964. * The (size - 16) / 12 formula is based on the information recorded
  965. * for each event, which is of mode 1 (including timestamp) for all
  966. * new microcodes that include this information.
  967. */
  968. fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
  969. if (pieces.init_evtlog_size)
  970. fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
  971. else
  972. fw->init_evtlog_size =
  973. drv->cfg->base_params->max_event_log_size;
  974. fw->init_errlog_ptr = pieces.init_errlog_ptr;
  975. fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
  976. if (pieces.inst_evtlog_size)
  977. fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
  978. else
  979. fw->inst_evtlog_size =
  980. drv->cfg->base_params->max_event_log_size;
  981. fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
  982. /*
  983. * figure out the offset of chain noise reset and gain commands
  984. * base on the size of standard phy calibration commands table size
  985. */
  986. if (fw->ucode_capa.standard_phy_calibration_size >
  987. IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
  988. fw->ucode_capa.standard_phy_calibration_size =
  989. IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  990. /* We have our copies now, allow OS release its copies */
  991. release_firmware(ucode_raw);
  992. mutex_lock(&iwlwifi_opmode_table_mtx);
  993. if (fw->mvm_fw)
  994. op = &iwlwifi_opmode_table[MVM_OP_MODE];
  995. else
  996. op = &iwlwifi_opmode_table[DVM_OP_MODE];
  997. IWL_INFO(drv, "loaded firmware version %s op_mode %s\n",
  998. drv->fw.fw_version, op->name);
  999. /* add this device to the list of devices using this op_mode */
  1000. list_add_tail(&drv->list, &op->drv);
  1001. if (op->ops) {
  1002. drv->op_mode = _iwl_op_mode_start(drv, op);
  1003. if (!drv->op_mode) {
  1004. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1005. goto out_unbind;
  1006. }
  1007. } else {
  1008. load_module = true;
  1009. }
  1010. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1011. /*
  1012. * Complete the firmware request last so that
  1013. * a driver unbind (stop) doesn't run while we
  1014. * are doing the start() above.
  1015. */
  1016. complete(&drv->request_firmware_complete);
  1017. /*
  1018. * Load the module last so we don't block anything
  1019. * else from proceeding if the module fails to load
  1020. * or hangs loading.
  1021. */
  1022. if (load_module) {
  1023. err = request_module("%s", op->name);
  1024. #ifdef CONFIG_IWLWIFI_OPMODE_MODULAR
  1025. if (err)
  1026. IWL_ERR(drv,
  1027. "failed to load module %s (error %d), is dynamic loading enabled?\n",
  1028. op->name, err);
  1029. #endif
  1030. }
  1031. return;
  1032. try_again:
  1033. /* try next, if any */
  1034. release_firmware(ucode_raw);
  1035. if (iwl_request_firmware(drv, false))
  1036. goto out_unbind;
  1037. return;
  1038. out_free_fw:
  1039. IWL_ERR(drv, "failed to allocate pci memory\n");
  1040. iwl_dealloc_ucode(drv);
  1041. release_firmware(ucode_raw);
  1042. out_unbind:
  1043. complete(&drv->request_firmware_complete);
  1044. device_release_driver(drv->trans->dev);
  1045. }
  1046. struct iwl_drv *iwl_drv_start(struct iwl_trans *trans,
  1047. const struct iwl_cfg *cfg)
  1048. {
  1049. struct iwl_drv *drv;
  1050. int ret;
  1051. drv = kzalloc(sizeof(*drv), GFP_KERNEL);
  1052. if (!drv) {
  1053. ret = -ENOMEM;
  1054. goto err;
  1055. }
  1056. drv->trans = trans;
  1057. drv->dev = trans->dev;
  1058. drv->cfg = cfg;
  1059. init_completion(&drv->request_firmware_complete);
  1060. INIT_LIST_HEAD(&drv->list);
  1061. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1062. /* Create the device debugfs entries. */
  1063. drv->dbgfs_drv = debugfs_create_dir(dev_name(trans->dev),
  1064. iwl_dbgfs_root);
  1065. if (!drv->dbgfs_drv) {
  1066. IWL_ERR(drv, "failed to create debugfs directory\n");
  1067. ret = -ENOMEM;
  1068. goto err_free_drv;
  1069. }
  1070. /* Create transport layer debugfs dir */
  1071. drv->trans->dbgfs_dir = debugfs_create_dir("trans", drv->dbgfs_drv);
  1072. if (!drv->trans->dbgfs_dir) {
  1073. IWL_ERR(drv, "failed to create transport debugfs directory\n");
  1074. ret = -ENOMEM;
  1075. goto err_free_dbgfs;
  1076. }
  1077. #endif
  1078. ret = iwl_request_firmware(drv, true);
  1079. if (ret) {
  1080. IWL_ERR(trans, "Couldn't request the fw\n");
  1081. goto err_fw;
  1082. }
  1083. return drv;
  1084. err_fw:
  1085. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1086. err_free_dbgfs:
  1087. debugfs_remove_recursive(drv->dbgfs_drv);
  1088. err_free_drv:
  1089. #endif
  1090. kfree(drv);
  1091. err:
  1092. return ERR_PTR(ret);
  1093. }
  1094. void iwl_drv_stop(struct iwl_drv *drv)
  1095. {
  1096. wait_for_completion(&drv->request_firmware_complete);
  1097. _iwl_op_mode_stop(drv);
  1098. iwl_dealloc_ucode(drv);
  1099. mutex_lock(&iwlwifi_opmode_table_mtx);
  1100. /*
  1101. * List is empty (this item wasn't added)
  1102. * when firmware loading failed -- in that
  1103. * case we can't remove it from any list.
  1104. */
  1105. if (!list_empty(&drv->list))
  1106. list_del(&drv->list);
  1107. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1108. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1109. debugfs_remove_recursive(drv->dbgfs_drv);
  1110. #endif
  1111. kfree(drv);
  1112. }
  1113. /* shared module parameters */
  1114. struct iwl_mod_params iwlwifi_mod_params = {
  1115. .restart_fw = true,
  1116. .bt_coex_active = true,
  1117. .power_level = IWL_POWER_INDEX_1,
  1118. .wd_disable = true,
  1119. #ifndef CONFIG_IWLWIFI_UAPSD
  1120. .uapsd_disable = true,
  1121. #endif /* CONFIG_IWLWIFI_UAPSD */
  1122. /* the rest are 0 by default */
  1123. };
  1124. IWL_EXPORT_SYMBOL(iwlwifi_mod_params);
  1125. int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops)
  1126. {
  1127. int i;
  1128. struct iwl_drv *drv;
  1129. struct iwlwifi_opmode_table *op;
  1130. mutex_lock(&iwlwifi_opmode_table_mtx);
  1131. for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
  1132. op = &iwlwifi_opmode_table[i];
  1133. if (strcmp(op->name, name))
  1134. continue;
  1135. op->ops = ops;
  1136. /* TODO: need to handle exceptional case */
  1137. list_for_each_entry(drv, &op->drv, list)
  1138. drv->op_mode = _iwl_op_mode_start(drv, op);
  1139. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1140. return 0;
  1141. }
  1142. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1143. return -EIO;
  1144. }
  1145. IWL_EXPORT_SYMBOL(iwl_opmode_register);
  1146. void iwl_opmode_deregister(const char *name)
  1147. {
  1148. int i;
  1149. struct iwl_drv *drv;
  1150. mutex_lock(&iwlwifi_opmode_table_mtx);
  1151. for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
  1152. if (strcmp(iwlwifi_opmode_table[i].name, name))
  1153. continue;
  1154. iwlwifi_opmode_table[i].ops = NULL;
  1155. /* call the stop routine for all devices */
  1156. list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list)
  1157. _iwl_op_mode_stop(drv);
  1158. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1159. return;
  1160. }
  1161. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1162. }
  1163. IWL_EXPORT_SYMBOL(iwl_opmode_deregister);
  1164. static int __init iwl_drv_init(void)
  1165. {
  1166. int i;
  1167. mutex_init(&iwlwifi_opmode_table_mtx);
  1168. for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++)
  1169. INIT_LIST_HEAD(&iwlwifi_opmode_table[i].drv);
  1170. pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
  1171. pr_info(DRV_COPYRIGHT "\n");
  1172. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1173. /* Create the root of iwlwifi debugfs subsystem. */
  1174. iwl_dbgfs_root = debugfs_create_dir(DRV_NAME, NULL);
  1175. if (!iwl_dbgfs_root)
  1176. return -EFAULT;
  1177. #endif
  1178. return iwl_pci_register_driver();
  1179. }
  1180. module_init(iwl_drv_init);
  1181. static void __exit iwl_drv_exit(void)
  1182. {
  1183. iwl_pci_unregister_driver();
  1184. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1185. debugfs_remove_recursive(iwl_dbgfs_root);
  1186. #endif
  1187. }
  1188. module_exit(iwl_drv_exit);
  1189. #ifdef CONFIG_IWLWIFI_DEBUG
  1190. module_param_named(debug, iwlwifi_mod_params.debug_level, uint,
  1191. S_IRUGO | S_IWUSR);
  1192. MODULE_PARM_DESC(debug, "debug output mask");
  1193. #endif
  1194. module_param_named(swcrypto, iwlwifi_mod_params.sw_crypto, int, S_IRUGO);
  1195. MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
  1196. module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, S_IRUGO);
  1197. MODULE_PARM_DESC(11n_disable,
  1198. "disable 11n functionality, bitmap: 1: full, 2: disable agg TX, 4: disable agg RX, 8 enable agg TX");
  1199. module_param_named(amsdu_size_8K, iwlwifi_mod_params.amsdu_size_8K,
  1200. int, S_IRUGO);
  1201. MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size (default 0)");
  1202. module_param_named(fw_restart, iwlwifi_mod_params.restart_fw, bool, S_IRUGO);
  1203. MODULE_PARM_DESC(fw_restart, "restart firmware in case of error (default true)");
  1204. module_param_named(antenna_coupling, iwlwifi_mod_params.ant_coupling,
  1205. int, S_IRUGO);
  1206. MODULE_PARM_DESC(antenna_coupling,
  1207. "specify antenna coupling in dB (default: 0 dB)");
  1208. module_param_named(wd_disable, iwlwifi_mod_params.wd_disable, int, S_IRUGO);
  1209. MODULE_PARM_DESC(wd_disable,
  1210. "Disable stuck queue watchdog timer 0=system default, 1=disable (default: 1)");
  1211. module_param_named(nvm_file, iwlwifi_mod_params.nvm_file, charp, S_IRUGO);
  1212. MODULE_PARM_DESC(nvm_file, "NVM file name");
  1213. module_param_named(uapsd_disable, iwlwifi_mod_params.uapsd_disable,
  1214. bool, S_IRUGO);
  1215. #ifdef CONFIG_IWLWIFI_UAPSD
  1216. MODULE_PARM_DESC(uapsd_disable, "disable U-APSD functionality (default: N)");
  1217. #else
  1218. MODULE_PARM_DESC(uapsd_disable, "disable U-APSD functionality (default: Y)");
  1219. #endif
  1220. /*
  1221. * set bt_coex_active to true, uCode will do kill/defer
  1222. * every time the priority line is asserted (BT is sending signals on the
  1223. * priority line in the PCIx).
  1224. * set bt_coex_active to false, uCode will ignore the BT activity and
  1225. * perform the normal operation
  1226. *
  1227. * User might experience transmit issue on some platform due to WiFi/BT
  1228. * co-exist problem. The possible behaviors are:
  1229. * Able to scan and finding all the available AP
  1230. * Not able to associate with any AP
  1231. * On those platforms, WiFi communication can be restored by set
  1232. * "bt_coex_active" module parameter to "false"
  1233. *
  1234. * default: bt_coex_active = true (BT_COEX_ENABLE)
  1235. */
  1236. module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
  1237. bool, S_IRUGO);
  1238. MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
  1239. module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, S_IRUGO);
  1240. MODULE_PARM_DESC(led_mode, "0=system default, "
  1241. "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
  1242. module_param_named(power_save, iwlwifi_mod_params.power_save,
  1243. bool, S_IRUGO);
  1244. MODULE_PARM_DESC(power_save,
  1245. "enable WiFi power management (default: disable)");
  1246. module_param_named(power_level, iwlwifi_mod_params.power_level,
  1247. int, S_IRUGO);
  1248. MODULE_PARM_DESC(power_level,
  1249. "default power save level (range from 1 - 5, default: 1)");
  1250. module_param_named(fw_monitor, iwlwifi_mod_params.fw_monitor, bool, S_IRUGO);
  1251. MODULE_PARM_DESC(fw_monitor,
  1252. "firmware monitor - to debug FW (default: false - needs lots of memory)");