iwl-drv.c 45 KB

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