iwl-drv.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  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. break;
  719. case IWL_UCODE_TLV_SECURE_SEC_INIT:
  720. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
  721. tlv_len);
  722. drv->fw.mvm_fw = true;
  723. break;
  724. case IWL_UCODE_TLV_SECURE_SEC_WOWLAN:
  725. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
  726. tlv_len);
  727. drv->fw.mvm_fw = true;
  728. break;
  729. case IWL_UCODE_TLV_NUM_OF_CPU:
  730. if (tlv_len != sizeof(u32))
  731. goto invalid_tlv_len;
  732. num_of_cpus =
  733. le32_to_cpup((__le32 *)tlv_data);
  734. if (num_of_cpus == 2) {
  735. drv->fw.img[IWL_UCODE_REGULAR].is_dual_cpus =
  736. true;
  737. drv->fw.img[IWL_UCODE_INIT].is_dual_cpus =
  738. true;
  739. drv->fw.img[IWL_UCODE_WOWLAN].is_dual_cpus =
  740. true;
  741. } else if ((num_of_cpus > 2) || (num_of_cpus < 1)) {
  742. IWL_ERR(drv, "Driver support upto 2 CPUs\n");
  743. return -EINVAL;
  744. }
  745. break;
  746. case IWL_UCODE_TLV_CSCHEME:
  747. if (iwl_store_cscheme(&drv->fw, tlv_data, tlv_len))
  748. goto invalid_tlv_len;
  749. break;
  750. case IWL_UCODE_TLV_N_SCAN_CHANNELS:
  751. if (tlv_len != sizeof(u32))
  752. goto invalid_tlv_len;
  753. capa->n_scan_channels =
  754. le32_to_cpup((__le32 *)tlv_data);
  755. break;
  756. default:
  757. IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
  758. break;
  759. }
  760. }
  761. if (len) {
  762. IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
  763. iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
  764. return -EINVAL;
  765. }
  766. return 0;
  767. invalid_tlv_len:
  768. IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
  769. tlv_error:
  770. iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
  771. return -EINVAL;
  772. }
  773. static int iwl_alloc_ucode(struct iwl_drv *drv,
  774. struct iwl_firmware_pieces *pieces,
  775. enum iwl_ucode_type type)
  776. {
  777. int i;
  778. for (i = 0;
  779. i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
  780. i++)
  781. if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
  782. get_sec(pieces, type, i)))
  783. return -ENOMEM;
  784. return 0;
  785. }
  786. static int validate_sec_sizes(struct iwl_drv *drv,
  787. struct iwl_firmware_pieces *pieces,
  788. const struct iwl_cfg *cfg)
  789. {
  790. IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
  791. get_sec_size(pieces, IWL_UCODE_REGULAR,
  792. IWL_UCODE_SECTION_INST));
  793. IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
  794. get_sec_size(pieces, IWL_UCODE_REGULAR,
  795. IWL_UCODE_SECTION_DATA));
  796. IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
  797. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
  798. IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
  799. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
  800. /* Verify that uCode images will fit in card's SRAM. */
  801. if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
  802. cfg->max_inst_size) {
  803. IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
  804. get_sec_size(pieces, IWL_UCODE_REGULAR,
  805. IWL_UCODE_SECTION_INST));
  806. return -1;
  807. }
  808. if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
  809. cfg->max_data_size) {
  810. IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
  811. get_sec_size(pieces, IWL_UCODE_REGULAR,
  812. IWL_UCODE_SECTION_DATA));
  813. return -1;
  814. }
  815. if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
  816. cfg->max_inst_size) {
  817. IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
  818. get_sec_size(pieces, IWL_UCODE_INIT,
  819. IWL_UCODE_SECTION_INST));
  820. return -1;
  821. }
  822. if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
  823. cfg->max_data_size) {
  824. IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
  825. get_sec_size(pieces, IWL_UCODE_REGULAR,
  826. IWL_UCODE_SECTION_DATA));
  827. return -1;
  828. }
  829. return 0;
  830. }
  831. static struct iwl_op_mode *
  832. _iwl_op_mode_start(struct iwl_drv *drv, struct iwlwifi_opmode_table *op)
  833. {
  834. const struct iwl_op_mode_ops *ops = op->ops;
  835. struct dentry *dbgfs_dir = NULL;
  836. struct iwl_op_mode *op_mode = NULL;
  837. #ifdef CONFIG_IWLWIFI_DEBUGFS
  838. drv->dbgfs_op_mode = debugfs_create_dir(op->name,
  839. drv->dbgfs_drv);
  840. if (!drv->dbgfs_op_mode) {
  841. IWL_ERR(drv,
  842. "failed to create opmode debugfs directory\n");
  843. return op_mode;
  844. }
  845. dbgfs_dir = drv->dbgfs_op_mode;
  846. #endif
  847. op_mode = ops->start(drv->trans, drv->cfg, &drv->fw, dbgfs_dir);
  848. #ifdef CONFIG_IWLWIFI_DEBUGFS
  849. if (!op_mode) {
  850. debugfs_remove_recursive(drv->dbgfs_op_mode);
  851. drv->dbgfs_op_mode = NULL;
  852. }
  853. #endif
  854. return op_mode;
  855. }
  856. static void _iwl_op_mode_stop(struct iwl_drv *drv)
  857. {
  858. /* op_mode can be NULL if its start failed */
  859. if (drv->op_mode) {
  860. iwl_op_mode_stop(drv->op_mode);
  861. drv->op_mode = NULL;
  862. #ifdef CONFIG_IWLWIFI_DEBUGFS
  863. debugfs_remove_recursive(drv->dbgfs_op_mode);
  864. drv->dbgfs_op_mode = NULL;
  865. #endif
  866. }
  867. }
  868. /**
  869. * iwl_req_fw_callback - callback when firmware was loaded
  870. *
  871. * If loaded successfully, copies the firmware into buffers
  872. * for the card to fetch (via DMA).
  873. */
  874. static void iwl_req_fw_callback(const struct firmware *ucode_raw, void *context)
  875. {
  876. struct iwl_drv *drv = context;
  877. struct iwl_fw *fw = &drv->fw;
  878. struct iwl_ucode_header *ucode;
  879. struct iwlwifi_opmode_table *op;
  880. int err;
  881. struct iwl_firmware_pieces pieces;
  882. const unsigned int api_max = drv->cfg->ucode_api_max;
  883. unsigned int api_ok = drv->cfg->ucode_api_ok;
  884. const unsigned int api_min = drv->cfg->ucode_api_min;
  885. u32 api_ver;
  886. int i;
  887. bool load_module = false;
  888. fw->ucode_capa.max_probe_length = IWL_DEFAULT_MAX_PROBE_LENGTH;
  889. fw->ucode_capa.standard_phy_calibration_size =
  890. IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  891. fw->ucode_capa.n_scan_channels = IWL_DEFAULT_SCAN_CHANNELS;
  892. if (!api_ok)
  893. api_ok = api_max;
  894. memset(&pieces, 0, sizeof(pieces));
  895. if (!ucode_raw) {
  896. if (drv->fw_index <= api_ok)
  897. IWL_ERR(drv,
  898. "request for firmware file '%s' failed.\n",
  899. drv->firmware_name);
  900. goto try_again;
  901. }
  902. IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
  903. drv->firmware_name, ucode_raw->size);
  904. /* Make sure that we got at least the API version number */
  905. if (ucode_raw->size < 4) {
  906. IWL_ERR(drv, "File size way too small!\n");
  907. goto try_again;
  908. }
  909. /* Data from ucode file: header followed by uCode images */
  910. ucode = (struct iwl_ucode_header *)ucode_raw->data;
  911. if (ucode->ver)
  912. err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
  913. else
  914. err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
  915. &fw->ucode_capa);
  916. if (err)
  917. goto try_again;
  918. api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
  919. /*
  920. * api_ver should match the api version forming part of the
  921. * firmware filename ... but we don't check for that and only rely
  922. * on the API version read from firmware header from here on forward
  923. */
  924. /* no api version check required for experimental uCode */
  925. if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
  926. if (api_ver < api_min || api_ver > api_max) {
  927. IWL_ERR(drv,
  928. "Driver unable to support your firmware API. "
  929. "Driver supports v%u, firmware is v%u.\n",
  930. api_max, api_ver);
  931. goto try_again;
  932. }
  933. if (api_ver < api_ok) {
  934. if (api_ok != api_max)
  935. IWL_ERR(drv, "Firmware has old API version, "
  936. "expected v%u through v%u, got v%u.\n",
  937. api_ok, api_max, api_ver);
  938. else
  939. IWL_ERR(drv, "Firmware has old API version, "
  940. "expected v%u, got v%u.\n",
  941. api_max, api_ver);
  942. IWL_ERR(drv, "New firmware can be obtained from "
  943. "http://www.intellinuxwireless.org/.\n");
  944. }
  945. }
  946. /*
  947. * In mvm uCode there is no difference between data and instructions
  948. * sections.
  949. */
  950. if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, drv->cfg))
  951. goto try_again;
  952. /* Allocate ucode buffers for card's bus-master loading ... */
  953. /* Runtime instructions and 2 copies of data:
  954. * 1) unmodified from disk
  955. * 2) backup cache for save/restore during power-downs */
  956. for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
  957. if (iwl_alloc_ucode(drv, &pieces, i))
  958. goto out_free_fw;
  959. /* Now that we can no longer fail, copy information */
  960. /*
  961. * The (size - 16) / 12 formula is based on the information recorded
  962. * for each event, which is of mode 1 (including timestamp) for all
  963. * new microcodes that include this information.
  964. */
  965. fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
  966. if (pieces.init_evtlog_size)
  967. fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
  968. else
  969. fw->init_evtlog_size =
  970. drv->cfg->base_params->max_event_log_size;
  971. fw->init_errlog_ptr = pieces.init_errlog_ptr;
  972. fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
  973. if (pieces.inst_evtlog_size)
  974. fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
  975. else
  976. fw->inst_evtlog_size =
  977. drv->cfg->base_params->max_event_log_size;
  978. fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
  979. /*
  980. * figure out the offset of chain noise reset and gain commands
  981. * base on the size of standard phy calibration commands table size
  982. */
  983. if (fw->ucode_capa.standard_phy_calibration_size >
  984. IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
  985. fw->ucode_capa.standard_phy_calibration_size =
  986. IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  987. /* We have our copies now, allow OS release its copies */
  988. release_firmware(ucode_raw);
  989. mutex_lock(&iwlwifi_opmode_table_mtx);
  990. if (fw->mvm_fw)
  991. op = &iwlwifi_opmode_table[MVM_OP_MODE];
  992. else
  993. op = &iwlwifi_opmode_table[DVM_OP_MODE];
  994. IWL_INFO(drv, "loaded firmware version %s op_mode %s\n",
  995. drv->fw.fw_version, op->name);
  996. /* add this device to the list of devices using this op_mode */
  997. list_add_tail(&drv->list, &op->drv);
  998. if (op->ops) {
  999. drv->op_mode = _iwl_op_mode_start(drv, op);
  1000. if (!drv->op_mode) {
  1001. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1002. goto out_unbind;
  1003. }
  1004. } else {
  1005. load_module = true;
  1006. }
  1007. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1008. /*
  1009. * Complete the firmware request last so that
  1010. * a driver unbind (stop) doesn't run while we
  1011. * are doing the start() above.
  1012. */
  1013. complete(&drv->request_firmware_complete);
  1014. /*
  1015. * Load the module last so we don't block anything
  1016. * else from proceeding if the module fails to load
  1017. * or hangs loading.
  1018. */
  1019. if (load_module) {
  1020. err = request_module("%s", op->name);
  1021. #ifdef CONFIG_IWLWIFI_OPMODE_MODULAR
  1022. if (err)
  1023. IWL_ERR(drv,
  1024. "failed to load module %s (error %d), is dynamic loading enabled?\n",
  1025. op->name, err);
  1026. #endif
  1027. }
  1028. return;
  1029. try_again:
  1030. /* try next, if any */
  1031. release_firmware(ucode_raw);
  1032. if (iwl_request_firmware(drv, false))
  1033. goto out_unbind;
  1034. return;
  1035. out_free_fw:
  1036. IWL_ERR(drv, "failed to allocate pci memory\n");
  1037. iwl_dealloc_ucode(drv);
  1038. release_firmware(ucode_raw);
  1039. out_unbind:
  1040. complete(&drv->request_firmware_complete);
  1041. device_release_driver(drv->trans->dev);
  1042. }
  1043. struct iwl_drv *iwl_drv_start(struct iwl_trans *trans,
  1044. const struct iwl_cfg *cfg)
  1045. {
  1046. struct iwl_drv *drv;
  1047. int ret;
  1048. drv = kzalloc(sizeof(*drv), GFP_KERNEL);
  1049. if (!drv) {
  1050. ret = -ENOMEM;
  1051. goto err;
  1052. }
  1053. drv->trans = trans;
  1054. drv->dev = trans->dev;
  1055. drv->cfg = cfg;
  1056. init_completion(&drv->request_firmware_complete);
  1057. INIT_LIST_HEAD(&drv->list);
  1058. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1059. /* Create the device debugfs entries. */
  1060. drv->dbgfs_drv = debugfs_create_dir(dev_name(trans->dev),
  1061. iwl_dbgfs_root);
  1062. if (!drv->dbgfs_drv) {
  1063. IWL_ERR(drv, "failed to create debugfs directory\n");
  1064. ret = -ENOMEM;
  1065. goto err_free_drv;
  1066. }
  1067. /* Create transport layer debugfs dir */
  1068. drv->trans->dbgfs_dir = debugfs_create_dir("trans", drv->dbgfs_drv);
  1069. if (!drv->trans->dbgfs_dir) {
  1070. IWL_ERR(drv, "failed to create transport debugfs directory\n");
  1071. ret = -ENOMEM;
  1072. goto err_free_dbgfs;
  1073. }
  1074. #endif
  1075. ret = iwl_request_firmware(drv, true);
  1076. if (ret) {
  1077. IWL_ERR(trans, "Couldn't request the fw\n");
  1078. goto err_fw;
  1079. }
  1080. return drv;
  1081. err_fw:
  1082. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1083. err_free_dbgfs:
  1084. debugfs_remove_recursive(drv->dbgfs_drv);
  1085. err_free_drv:
  1086. #endif
  1087. kfree(drv);
  1088. err:
  1089. return ERR_PTR(ret);
  1090. }
  1091. void iwl_drv_stop(struct iwl_drv *drv)
  1092. {
  1093. wait_for_completion(&drv->request_firmware_complete);
  1094. _iwl_op_mode_stop(drv);
  1095. iwl_dealloc_ucode(drv);
  1096. mutex_lock(&iwlwifi_opmode_table_mtx);
  1097. /*
  1098. * List is empty (this item wasn't added)
  1099. * when firmware loading failed -- in that
  1100. * case we can't remove it from any list.
  1101. */
  1102. if (!list_empty(&drv->list))
  1103. list_del(&drv->list);
  1104. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1105. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1106. debugfs_remove_recursive(drv->dbgfs_drv);
  1107. #endif
  1108. kfree(drv);
  1109. }
  1110. /* shared module parameters */
  1111. struct iwl_mod_params iwlwifi_mod_params = {
  1112. .restart_fw = true,
  1113. .bt_coex_active = true,
  1114. .power_level = IWL_POWER_INDEX_1,
  1115. .wd_disable = true,
  1116. #ifndef CONFIG_IWLWIFI_UAPSD
  1117. .uapsd_disable = true,
  1118. #endif /* CONFIG_IWLWIFI_UAPSD */
  1119. /* the rest are 0 by default */
  1120. };
  1121. IWL_EXPORT_SYMBOL(iwlwifi_mod_params);
  1122. int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops)
  1123. {
  1124. int i;
  1125. struct iwl_drv *drv;
  1126. struct iwlwifi_opmode_table *op;
  1127. mutex_lock(&iwlwifi_opmode_table_mtx);
  1128. for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
  1129. op = &iwlwifi_opmode_table[i];
  1130. if (strcmp(op->name, name))
  1131. continue;
  1132. op->ops = ops;
  1133. /* TODO: need to handle exceptional case */
  1134. list_for_each_entry(drv, &op->drv, list)
  1135. drv->op_mode = _iwl_op_mode_start(drv, op);
  1136. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1137. return 0;
  1138. }
  1139. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1140. return -EIO;
  1141. }
  1142. IWL_EXPORT_SYMBOL(iwl_opmode_register);
  1143. void iwl_opmode_deregister(const char *name)
  1144. {
  1145. int i;
  1146. struct iwl_drv *drv;
  1147. mutex_lock(&iwlwifi_opmode_table_mtx);
  1148. for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
  1149. if (strcmp(iwlwifi_opmode_table[i].name, name))
  1150. continue;
  1151. iwlwifi_opmode_table[i].ops = NULL;
  1152. /* call the stop routine for all devices */
  1153. list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list)
  1154. _iwl_op_mode_stop(drv);
  1155. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1156. return;
  1157. }
  1158. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1159. }
  1160. IWL_EXPORT_SYMBOL(iwl_opmode_deregister);
  1161. static int __init iwl_drv_init(void)
  1162. {
  1163. int i;
  1164. mutex_init(&iwlwifi_opmode_table_mtx);
  1165. for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++)
  1166. INIT_LIST_HEAD(&iwlwifi_opmode_table[i].drv);
  1167. pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
  1168. pr_info(DRV_COPYRIGHT "\n");
  1169. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1170. /* Create the root of iwlwifi debugfs subsystem. */
  1171. iwl_dbgfs_root = debugfs_create_dir(DRV_NAME, NULL);
  1172. if (!iwl_dbgfs_root)
  1173. return -EFAULT;
  1174. #endif
  1175. return iwl_pci_register_driver();
  1176. }
  1177. module_init(iwl_drv_init);
  1178. static void __exit iwl_drv_exit(void)
  1179. {
  1180. iwl_pci_unregister_driver();
  1181. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1182. debugfs_remove_recursive(iwl_dbgfs_root);
  1183. #endif
  1184. }
  1185. module_exit(iwl_drv_exit);
  1186. #ifdef CONFIG_IWLWIFI_DEBUG
  1187. module_param_named(debug, iwlwifi_mod_params.debug_level, uint,
  1188. S_IRUGO | S_IWUSR);
  1189. MODULE_PARM_DESC(debug, "debug output mask");
  1190. #endif
  1191. module_param_named(swcrypto, iwlwifi_mod_params.sw_crypto, int, S_IRUGO);
  1192. MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
  1193. module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, S_IRUGO);
  1194. MODULE_PARM_DESC(11n_disable,
  1195. "disable 11n functionality, bitmap: 1: full, 2: disable agg TX, 4: disable agg RX, 8 enable agg TX");
  1196. module_param_named(amsdu_size_8K, iwlwifi_mod_params.amsdu_size_8K,
  1197. int, S_IRUGO);
  1198. MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size (default 0)");
  1199. module_param_named(fw_restart, iwlwifi_mod_params.restart_fw, bool, S_IRUGO);
  1200. MODULE_PARM_DESC(fw_restart, "restart firmware in case of error (default true)");
  1201. module_param_named(antenna_coupling, iwlwifi_mod_params.ant_coupling,
  1202. int, S_IRUGO);
  1203. MODULE_PARM_DESC(antenna_coupling,
  1204. "specify antenna coupling in dB (default: 0 dB)");
  1205. module_param_named(wd_disable, iwlwifi_mod_params.wd_disable, int, S_IRUGO);
  1206. MODULE_PARM_DESC(wd_disable,
  1207. "Disable stuck queue watchdog timer 0=system default, 1=disable (default: 1)");
  1208. module_param_named(nvm_file, iwlwifi_mod_params.nvm_file, charp, S_IRUGO);
  1209. MODULE_PARM_DESC(nvm_file, "NVM file name");
  1210. module_param_named(uapsd_disable, iwlwifi_mod_params.uapsd_disable,
  1211. bool, S_IRUGO);
  1212. #ifdef CONFIG_IWLWIFI_UAPSD
  1213. MODULE_PARM_DESC(uapsd_disable, "disable U-APSD functionality (default: N)");
  1214. #else
  1215. MODULE_PARM_DESC(uapsd_disable, "disable U-APSD functionality (default: Y)");
  1216. #endif
  1217. /*
  1218. * set bt_coex_active to true, uCode will do kill/defer
  1219. * every time the priority line is asserted (BT is sending signals on the
  1220. * priority line in the PCIx).
  1221. * set bt_coex_active to false, uCode will ignore the BT activity and
  1222. * perform the normal operation
  1223. *
  1224. * User might experience transmit issue on some platform due to WiFi/BT
  1225. * co-exist problem. The possible behaviors are:
  1226. * Able to scan and finding all the available AP
  1227. * Not able to associate with any AP
  1228. * On those platforms, WiFi communication can be restored by set
  1229. * "bt_coex_active" module parameter to "false"
  1230. *
  1231. * default: bt_coex_active = true (BT_COEX_ENABLE)
  1232. */
  1233. module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
  1234. bool, S_IRUGO);
  1235. MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
  1236. module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, S_IRUGO);
  1237. MODULE_PARM_DESC(led_mode, "0=system default, "
  1238. "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
  1239. module_param_named(power_save, iwlwifi_mod_params.power_save,
  1240. bool, S_IRUGO);
  1241. MODULE_PARM_DESC(power_save,
  1242. "enable WiFi power management (default: disable)");
  1243. module_param_named(power_level, iwlwifi_mod_params.power_level,
  1244. int, S_IRUGO);
  1245. MODULE_PARM_DESC(power_level,
  1246. "default power save level (range from 1 - 5, default: 1)");
  1247. module_param_named(fw_monitor, iwlwifi_mod_params.fw_monitor, bool, S_IRUGO);
  1248. MODULE_PARM_DESC(fw_monitor,
  1249. "firmware monitor - to debug FW (default: false - needs lots of memory)");