iwl-drv.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  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 - 2015 Intel Mobile Communications GmbH
  10. * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of version 2 of the GNU General Public License as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  24. * USA
  25. *
  26. * The full GNU General Public License is included in this distribution
  27. * in the file called COPYING.
  28. *
  29. * Contact Information:
  30. * Intel Linux Wireless <linuxwifi@intel.com>
  31. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  32. *
  33. * BSD LICENSE
  34. *
  35. * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
  36. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  37. * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  38. * All rights reserved.
  39. *
  40. * Redistribution and use in source and binary forms, with or without
  41. * modification, are permitted provided that the following conditions
  42. * are met:
  43. *
  44. * * Redistributions of source code must retain the above copyright
  45. * notice, this list of conditions and the following disclaimer.
  46. * * Redistributions in binary form must reproduce the above copyright
  47. * notice, this list of conditions and the following disclaimer in
  48. * the documentation and/or other materials provided with the
  49. * distribution.
  50. * * Neither the name Intel Corporation nor the names of its
  51. * contributors may be used to endorse or promote products derived
  52. * from this software without specific prior written permission.
  53. *
  54. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  55. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  56. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  57. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  58. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  59. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  60. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  61. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  62. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  63. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  64. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  65. *
  66. *****************************************************************************/
  67. #include <linux/completion.h>
  68. #include <linux/dma-mapping.h>
  69. #include <linux/firmware.h>
  70. #include <linux/module.h>
  71. #include <linux/vmalloc.h>
  72. #include "iwl-drv.h"
  73. #include "iwl-csr.h"
  74. #include "iwl-debug.h"
  75. #include "iwl-trans.h"
  76. #include "iwl-op-mode.h"
  77. #include "iwl-agn-hw.h"
  78. #include "fw/img.h"
  79. #include "iwl-config.h"
  80. #include "iwl-modparams.h"
  81. /******************************************************************************
  82. *
  83. * module boiler plate
  84. *
  85. ******************************************************************************/
  86. #define DRV_DESCRIPTION "Intel(R) Wireless WiFi driver for Linux"
  87. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  88. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  89. MODULE_LICENSE("GPL");
  90. #ifdef CONFIG_IWLWIFI_DEBUGFS
  91. static struct dentry *iwl_dbgfs_root;
  92. #endif
  93. /**
  94. * struct iwl_drv - drv common data
  95. * @list: list of drv structures using this opmode
  96. * @fw: the iwl_fw structure
  97. * @op_mode: the running op_mode
  98. * @trans: transport layer
  99. * @dev: for debug prints only
  100. * @fw_index: firmware revision to try loading
  101. * @firmware_name: composite filename of ucode file to load
  102. * @request_firmware_complete: the firmware has been obtained from user space
  103. */
  104. struct iwl_drv {
  105. struct list_head list;
  106. struct iwl_fw fw;
  107. struct iwl_op_mode *op_mode;
  108. struct iwl_trans *trans;
  109. struct device *dev;
  110. int fw_index; /* firmware we're trying to load */
  111. char firmware_name[64]; /* name of firmware file to load */
  112. struct completion request_firmware_complete;
  113. #ifdef CONFIG_IWLWIFI_DEBUGFS
  114. struct dentry *dbgfs_drv;
  115. struct dentry *dbgfs_trans;
  116. struct dentry *dbgfs_op_mode;
  117. #endif
  118. };
  119. enum {
  120. DVM_OP_MODE,
  121. MVM_OP_MODE,
  122. };
  123. /* Protects the table contents, i.e. the ops pointer & drv list */
  124. static struct mutex iwlwifi_opmode_table_mtx;
  125. static struct iwlwifi_opmode_table {
  126. const char *name; /* name: iwldvm, iwlmvm, etc */
  127. const struct iwl_op_mode_ops *ops; /* pointer to op_mode ops */
  128. struct list_head drv; /* list of devices using this op_mode */
  129. } iwlwifi_opmode_table[] = { /* ops set when driver is initialized */
  130. [DVM_OP_MODE] = { .name = "iwldvm", .ops = NULL },
  131. [MVM_OP_MODE] = { .name = "iwlmvm", .ops = NULL },
  132. };
  133. #define IWL_DEFAULT_SCAN_CHANNELS 40
  134. /*
  135. * struct fw_sec: Just for the image parsing process.
  136. * For the fw storage we are using struct fw_desc.
  137. */
  138. struct fw_sec {
  139. const void *data; /* the sec data */
  140. size_t size; /* section size */
  141. u32 offset; /* offset of writing in the device */
  142. };
  143. static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
  144. {
  145. vfree(desc->data);
  146. desc->data = NULL;
  147. desc->len = 0;
  148. }
  149. static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
  150. {
  151. int i;
  152. for (i = 0; i < img->num_sec; i++)
  153. iwl_free_fw_desc(drv, &img->sec[i]);
  154. kfree(img->sec);
  155. }
  156. static void iwl_dealloc_ucode(struct iwl_drv *drv)
  157. {
  158. int i;
  159. kfree(drv->fw.dbg_dest_tlv);
  160. for (i = 0; i < ARRAY_SIZE(drv->fw.dbg_conf_tlv); i++)
  161. kfree(drv->fw.dbg_conf_tlv[i]);
  162. for (i = 0; i < ARRAY_SIZE(drv->fw.dbg_trigger_tlv); i++)
  163. kfree(drv->fw.dbg_trigger_tlv[i]);
  164. kfree(drv->fw.dbg_mem_tlv);
  165. kfree(drv->fw.iml);
  166. for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
  167. iwl_free_fw_img(drv, drv->fw.img + i);
  168. }
  169. static int iwl_alloc_fw_desc(struct iwl_drv *drv, struct fw_desc *desc,
  170. struct fw_sec *sec)
  171. {
  172. void *data;
  173. desc->data = NULL;
  174. if (!sec || !sec->size)
  175. return -EINVAL;
  176. data = vmalloc(sec->size);
  177. if (!data)
  178. return -ENOMEM;
  179. desc->len = sec->size;
  180. desc->offset = sec->offset;
  181. memcpy(data, sec->data, desc->len);
  182. desc->data = data;
  183. return 0;
  184. }
  185. static void iwl_req_fw_callback(const struct firmware *ucode_raw,
  186. void *context);
  187. static int iwl_request_firmware(struct iwl_drv *drv, bool first)
  188. {
  189. const struct iwl_cfg *cfg = drv->trans->cfg;
  190. char tag[8];
  191. const char *fw_pre_name;
  192. if (drv->trans->cfg->device_family == IWL_DEVICE_FAMILY_9000 &&
  193. (CSR_HW_REV_STEP(drv->trans->hw_rev) == SILICON_B_STEP ||
  194. CSR_HW_REV_STEP(drv->trans->hw_rev) == SILICON_C_STEP))
  195. fw_pre_name = cfg->fw_name_pre_b_or_c_step;
  196. else if (drv->trans->cfg->integrated &&
  197. CSR_HW_RFID_STEP(drv->trans->hw_rf_id) == SILICON_B_STEP &&
  198. cfg->fw_name_pre_rf_next_step)
  199. fw_pre_name = cfg->fw_name_pre_rf_next_step;
  200. else
  201. fw_pre_name = cfg->fw_name_pre;
  202. if (first) {
  203. drv->fw_index = 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 < cfg->ucode_api_min) {
  210. IWL_ERR(drv, "no suitable firmware found!\n");
  211. if (cfg->ucode_api_min == cfg->ucode_api_max) {
  212. IWL_ERR(drv, "%s%d is required\n", fw_pre_name,
  213. cfg->ucode_api_max);
  214. } else {
  215. IWL_ERR(drv, "minimum version required: %s%d\n",
  216. fw_pre_name,
  217. cfg->ucode_api_min);
  218. IWL_ERR(drv, "maximum version supported: %s%d\n",
  219. fw_pre_name,
  220. cfg->ucode_api_max);
  221. }
  222. IWL_ERR(drv,
  223. "check git://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git\n");
  224. return -ENOENT;
  225. }
  226. snprintf(drv->firmware_name, sizeof(drv->firmware_name), "%s%s.ucode",
  227. fw_pre_name, tag);
  228. IWL_DEBUG_INFO(drv, "attempting to load firmware '%s'\n",
  229. drv->firmware_name);
  230. return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
  231. drv->trans->dev,
  232. GFP_KERNEL, drv, iwl_req_fw_callback);
  233. }
  234. struct fw_img_parsing {
  235. struct fw_sec *sec;
  236. int sec_counter;
  237. };
  238. /*
  239. * struct fw_sec_parsing: to extract fw section and it's offset from tlv
  240. */
  241. struct fw_sec_parsing {
  242. __le32 offset;
  243. const u8 data[];
  244. } __packed;
  245. /**
  246. * struct iwl_tlv_calib_data - parse the default calib data from TLV
  247. *
  248. * @ucode_type: the uCode to which the following default calib relates.
  249. * @calib: default calibrations.
  250. */
  251. struct iwl_tlv_calib_data {
  252. __le32 ucode_type;
  253. struct iwl_tlv_calib_ctrl calib;
  254. } __packed;
  255. struct iwl_firmware_pieces {
  256. struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
  257. u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
  258. u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
  259. /* FW debug data parsed for driver usage */
  260. bool dbg_dest_tlv_init;
  261. u8 *dbg_dest_ver;
  262. union {
  263. struct iwl_fw_dbg_dest_tlv *dbg_dest_tlv;
  264. struct iwl_fw_dbg_dest_tlv_v1 *dbg_dest_tlv_v1;
  265. };
  266. struct iwl_fw_dbg_conf_tlv *dbg_conf_tlv[FW_DBG_CONF_MAX];
  267. size_t dbg_conf_tlv_len[FW_DBG_CONF_MAX];
  268. struct iwl_fw_dbg_trigger_tlv *dbg_trigger_tlv[FW_DBG_TRIGGER_MAX];
  269. size_t dbg_trigger_tlv_len[FW_DBG_TRIGGER_MAX];
  270. struct iwl_fw_dbg_mem_seg_tlv *dbg_mem_tlv;
  271. size_t n_dbg_mem_tlv;
  272. };
  273. /*
  274. * These functions are just to extract uCode section data from the pieces
  275. * structure.
  276. */
  277. static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
  278. enum iwl_ucode_type type,
  279. int sec)
  280. {
  281. return &pieces->img[type].sec[sec];
  282. }
  283. static void alloc_sec_data(struct iwl_firmware_pieces *pieces,
  284. enum iwl_ucode_type type,
  285. int sec)
  286. {
  287. struct fw_img_parsing *img = &pieces->img[type];
  288. struct fw_sec *sec_memory;
  289. int size = sec + 1;
  290. size_t alloc_size = sizeof(*img->sec) * size;
  291. if (img->sec && img->sec_counter >= size)
  292. return;
  293. sec_memory = krealloc(img->sec, alloc_size, GFP_KERNEL);
  294. if (!sec_memory)
  295. return;
  296. img->sec = sec_memory;
  297. img->sec_counter = size;
  298. }
  299. static void set_sec_data(struct iwl_firmware_pieces *pieces,
  300. enum iwl_ucode_type type,
  301. int sec,
  302. const void *data)
  303. {
  304. alloc_sec_data(pieces, type, sec);
  305. pieces->img[type].sec[sec].data = data;
  306. }
  307. static void set_sec_size(struct iwl_firmware_pieces *pieces,
  308. enum iwl_ucode_type type,
  309. int sec,
  310. size_t size)
  311. {
  312. alloc_sec_data(pieces, type, sec);
  313. pieces->img[type].sec[sec].size = size;
  314. }
  315. static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
  316. enum iwl_ucode_type type,
  317. int sec)
  318. {
  319. return pieces->img[type].sec[sec].size;
  320. }
  321. static void set_sec_offset(struct iwl_firmware_pieces *pieces,
  322. enum iwl_ucode_type type,
  323. int sec,
  324. u32 offset)
  325. {
  326. alloc_sec_data(pieces, type, sec);
  327. pieces->img[type].sec[sec].offset = offset;
  328. }
  329. static int iwl_store_cscheme(struct iwl_fw *fw, const u8 *data, const u32 len)
  330. {
  331. int i, j;
  332. struct iwl_fw_cscheme_list *l = (struct iwl_fw_cscheme_list *)data;
  333. struct iwl_fw_cipher_scheme *fwcs;
  334. if (len < sizeof(*l) ||
  335. len < sizeof(l->size) + l->size * sizeof(l->cs[0]))
  336. return -EINVAL;
  337. for (i = 0, j = 0; i < IWL_UCODE_MAX_CS && i < l->size; i++) {
  338. fwcs = &l->cs[j];
  339. /* we skip schemes with zero cipher suite selector */
  340. if (!fwcs->cipher)
  341. continue;
  342. fw->cs[j++] = *fwcs;
  343. }
  344. return 0;
  345. }
  346. /*
  347. * Gets uCode section from tlv.
  348. */
  349. static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
  350. const void *data, enum iwl_ucode_type type,
  351. int size)
  352. {
  353. struct fw_img_parsing *img;
  354. struct fw_sec *sec;
  355. struct fw_sec_parsing *sec_parse;
  356. size_t alloc_size;
  357. if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
  358. return -1;
  359. sec_parse = (struct fw_sec_parsing *)data;
  360. img = &pieces->img[type];
  361. alloc_size = sizeof(*img->sec) * (img->sec_counter + 1);
  362. sec = krealloc(img->sec, alloc_size, GFP_KERNEL);
  363. if (!sec)
  364. return -ENOMEM;
  365. img->sec = sec;
  366. sec = &img->sec[img->sec_counter];
  367. sec->offset = le32_to_cpu(sec_parse->offset);
  368. sec->data = sec_parse->data;
  369. sec->size = size - sizeof(sec_parse->offset);
  370. ++img->sec_counter;
  371. return 0;
  372. }
  373. static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
  374. {
  375. struct iwl_tlv_calib_data *def_calib =
  376. (struct iwl_tlv_calib_data *)data;
  377. u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
  378. if (ucode_type >= IWL_UCODE_TYPE_MAX) {
  379. IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
  380. ucode_type);
  381. return -EINVAL;
  382. }
  383. drv->fw.default_calib[ucode_type].flow_trigger =
  384. def_calib->calib.flow_trigger;
  385. drv->fw.default_calib[ucode_type].event_trigger =
  386. def_calib->calib.event_trigger;
  387. return 0;
  388. }
  389. static void iwl_set_ucode_api_flags(struct iwl_drv *drv, const u8 *data,
  390. struct iwl_ucode_capabilities *capa)
  391. {
  392. const struct iwl_ucode_api *ucode_api = (void *)data;
  393. u32 api_index = le32_to_cpu(ucode_api->api_index);
  394. u32 api_flags = le32_to_cpu(ucode_api->api_flags);
  395. int i;
  396. if (api_index >= DIV_ROUND_UP(NUM_IWL_UCODE_TLV_API, 32)) {
  397. IWL_WARN(drv,
  398. "api flags index %d larger than supported by driver\n",
  399. api_index);
  400. return;
  401. }
  402. for (i = 0; i < 32; i++) {
  403. if (api_flags & BIT(i))
  404. __set_bit(i + 32 * api_index, capa->_api);
  405. }
  406. }
  407. static void iwl_set_ucode_capabilities(struct iwl_drv *drv, const u8 *data,
  408. struct iwl_ucode_capabilities *capa)
  409. {
  410. const struct iwl_ucode_capa *ucode_capa = (void *)data;
  411. u32 api_index = le32_to_cpu(ucode_capa->api_index);
  412. u32 api_flags = le32_to_cpu(ucode_capa->api_capa);
  413. int i;
  414. if (api_index >= DIV_ROUND_UP(NUM_IWL_UCODE_TLV_CAPA, 32)) {
  415. IWL_WARN(drv,
  416. "capa flags index %d larger than supported by driver\n",
  417. api_index);
  418. return;
  419. }
  420. for (i = 0; i < 32; i++) {
  421. if (api_flags & BIT(i))
  422. __set_bit(i + 32 * api_index, capa->_capa);
  423. }
  424. }
  425. static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
  426. const struct firmware *ucode_raw,
  427. struct iwl_firmware_pieces *pieces)
  428. {
  429. struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
  430. u32 api_ver, hdr_size, build;
  431. char buildstr[25];
  432. const u8 *src;
  433. drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
  434. api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
  435. switch (api_ver) {
  436. default:
  437. hdr_size = 28;
  438. if (ucode_raw->size < hdr_size) {
  439. IWL_ERR(drv, "File size too small!\n");
  440. return -EINVAL;
  441. }
  442. build = le32_to_cpu(ucode->u.v2.build);
  443. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
  444. le32_to_cpu(ucode->u.v2.inst_size));
  445. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
  446. le32_to_cpu(ucode->u.v2.data_size));
  447. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
  448. le32_to_cpu(ucode->u.v2.init_size));
  449. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
  450. le32_to_cpu(ucode->u.v2.init_data_size));
  451. src = ucode->u.v2.data;
  452. break;
  453. case 0:
  454. case 1:
  455. case 2:
  456. hdr_size = 24;
  457. if (ucode_raw->size < hdr_size) {
  458. IWL_ERR(drv, "File size too small!\n");
  459. return -EINVAL;
  460. }
  461. build = 0;
  462. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
  463. le32_to_cpu(ucode->u.v1.inst_size));
  464. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
  465. le32_to_cpu(ucode->u.v1.data_size));
  466. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
  467. le32_to_cpu(ucode->u.v1.init_size));
  468. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
  469. le32_to_cpu(ucode->u.v1.init_data_size));
  470. src = ucode->u.v1.data;
  471. break;
  472. }
  473. if (build)
  474. sprintf(buildstr, " build %u", build);
  475. else
  476. buildstr[0] = '\0';
  477. snprintf(drv->fw.fw_version,
  478. sizeof(drv->fw.fw_version),
  479. "%u.%u.%u.%u%s",
  480. IWL_UCODE_MAJOR(drv->fw.ucode_ver),
  481. IWL_UCODE_MINOR(drv->fw.ucode_ver),
  482. IWL_UCODE_API(drv->fw.ucode_ver),
  483. IWL_UCODE_SERIAL(drv->fw.ucode_ver),
  484. buildstr);
  485. /* Verify size of file vs. image size info in file's header */
  486. if (ucode_raw->size != hdr_size +
  487. get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
  488. get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
  489. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
  490. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
  491. IWL_ERR(drv,
  492. "uCode file size %d does not match expected size\n",
  493. (int)ucode_raw->size);
  494. return -EINVAL;
  495. }
  496. set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
  497. src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
  498. set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
  499. IWLAGN_RTC_INST_LOWER_BOUND);
  500. set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
  501. src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
  502. set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
  503. IWLAGN_RTC_DATA_LOWER_BOUND);
  504. set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
  505. src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
  506. set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
  507. IWLAGN_RTC_INST_LOWER_BOUND);
  508. set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
  509. src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
  510. set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
  511. IWLAGN_RTC_DATA_LOWER_BOUND);
  512. return 0;
  513. }
  514. static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
  515. const struct firmware *ucode_raw,
  516. struct iwl_firmware_pieces *pieces,
  517. struct iwl_ucode_capabilities *capa,
  518. bool *usniffer_images)
  519. {
  520. struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
  521. struct iwl_ucode_tlv *tlv;
  522. size_t len = ucode_raw->size;
  523. const u8 *data;
  524. u32 tlv_len;
  525. u32 usniffer_img;
  526. enum iwl_ucode_tlv_type tlv_type;
  527. const u8 *tlv_data;
  528. char buildstr[25];
  529. u32 build, paging_mem_size;
  530. int num_of_cpus;
  531. bool usniffer_req = false;
  532. if (len < sizeof(*ucode)) {
  533. IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
  534. return -EINVAL;
  535. }
  536. if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
  537. IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
  538. le32_to_cpu(ucode->magic));
  539. return -EINVAL;
  540. }
  541. drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
  542. memcpy(drv->fw.human_readable, ucode->human_readable,
  543. sizeof(drv->fw.human_readable));
  544. build = le32_to_cpu(ucode->build);
  545. if (build)
  546. sprintf(buildstr, " build %u", build);
  547. else
  548. buildstr[0] = '\0';
  549. snprintf(drv->fw.fw_version,
  550. sizeof(drv->fw.fw_version),
  551. "%u.%u.%u.%u%s",
  552. IWL_UCODE_MAJOR(drv->fw.ucode_ver),
  553. IWL_UCODE_MINOR(drv->fw.ucode_ver),
  554. IWL_UCODE_API(drv->fw.ucode_ver),
  555. IWL_UCODE_SERIAL(drv->fw.ucode_ver),
  556. buildstr);
  557. data = ucode->data;
  558. len -= sizeof(*ucode);
  559. while (len >= sizeof(*tlv)) {
  560. len -= sizeof(*tlv);
  561. tlv = (void *)data;
  562. tlv_len = le32_to_cpu(tlv->length);
  563. tlv_type = le32_to_cpu(tlv->type);
  564. tlv_data = tlv->data;
  565. if (len < tlv_len) {
  566. IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
  567. len, tlv_len);
  568. return -EINVAL;
  569. }
  570. len -= ALIGN(tlv_len, 4);
  571. data += sizeof(*tlv) + ALIGN(tlv_len, 4);
  572. switch (tlv_type) {
  573. case IWL_UCODE_TLV_INST:
  574. set_sec_data(pieces, IWL_UCODE_REGULAR,
  575. IWL_UCODE_SECTION_INST, tlv_data);
  576. set_sec_size(pieces, IWL_UCODE_REGULAR,
  577. IWL_UCODE_SECTION_INST, tlv_len);
  578. set_sec_offset(pieces, IWL_UCODE_REGULAR,
  579. IWL_UCODE_SECTION_INST,
  580. IWLAGN_RTC_INST_LOWER_BOUND);
  581. break;
  582. case IWL_UCODE_TLV_DATA:
  583. set_sec_data(pieces, IWL_UCODE_REGULAR,
  584. IWL_UCODE_SECTION_DATA, tlv_data);
  585. set_sec_size(pieces, IWL_UCODE_REGULAR,
  586. IWL_UCODE_SECTION_DATA, tlv_len);
  587. set_sec_offset(pieces, IWL_UCODE_REGULAR,
  588. IWL_UCODE_SECTION_DATA,
  589. IWLAGN_RTC_DATA_LOWER_BOUND);
  590. break;
  591. case IWL_UCODE_TLV_INIT:
  592. set_sec_data(pieces, IWL_UCODE_INIT,
  593. IWL_UCODE_SECTION_INST, tlv_data);
  594. set_sec_size(pieces, IWL_UCODE_INIT,
  595. IWL_UCODE_SECTION_INST, tlv_len);
  596. set_sec_offset(pieces, IWL_UCODE_INIT,
  597. IWL_UCODE_SECTION_INST,
  598. IWLAGN_RTC_INST_LOWER_BOUND);
  599. break;
  600. case IWL_UCODE_TLV_INIT_DATA:
  601. set_sec_data(pieces, IWL_UCODE_INIT,
  602. IWL_UCODE_SECTION_DATA, tlv_data);
  603. set_sec_size(pieces, IWL_UCODE_INIT,
  604. IWL_UCODE_SECTION_DATA, tlv_len);
  605. set_sec_offset(pieces, IWL_UCODE_INIT,
  606. IWL_UCODE_SECTION_DATA,
  607. IWLAGN_RTC_DATA_LOWER_BOUND);
  608. break;
  609. case IWL_UCODE_TLV_BOOT:
  610. IWL_ERR(drv, "Found unexpected BOOT ucode\n");
  611. break;
  612. case IWL_UCODE_TLV_PROBE_MAX_LEN:
  613. if (tlv_len != sizeof(u32))
  614. goto invalid_tlv_len;
  615. capa->max_probe_length =
  616. le32_to_cpup((__le32 *)tlv_data);
  617. break;
  618. case IWL_UCODE_TLV_PAN:
  619. if (tlv_len)
  620. goto invalid_tlv_len;
  621. capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
  622. break;
  623. case IWL_UCODE_TLV_FLAGS:
  624. /* must be at least one u32 */
  625. if (tlv_len < sizeof(u32))
  626. goto invalid_tlv_len;
  627. /* and a proper number of u32s */
  628. if (tlv_len % sizeof(u32))
  629. goto invalid_tlv_len;
  630. /*
  631. * This driver only reads the first u32 as
  632. * right now no more features are defined,
  633. * if that changes then either the driver
  634. * will not work with the new firmware, or
  635. * it'll not take advantage of new features.
  636. */
  637. capa->flags = le32_to_cpup((__le32 *)tlv_data);
  638. break;
  639. case IWL_UCODE_TLV_API_CHANGES_SET:
  640. if (tlv_len != sizeof(struct iwl_ucode_api))
  641. goto invalid_tlv_len;
  642. iwl_set_ucode_api_flags(drv, tlv_data, capa);
  643. break;
  644. case IWL_UCODE_TLV_ENABLED_CAPABILITIES:
  645. if (tlv_len != sizeof(struct iwl_ucode_capa))
  646. goto invalid_tlv_len;
  647. iwl_set_ucode_capabilities(drv, tlv_data, capa);
  648. break;
  649. case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
  650. if (tlv_len != sizeof(u32))
  651. goto invalid_tlv_len;
  652. pieces->init_evtlog_ptr =
  653. le32_to_cpup((__le32 *)tlv_data);
  654. break;
  655. case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
  656. if (tlv_len != sizeof(u32))
  657. goto invalid_tlv_len;
  658. pieces->init_evtlog_size =
  659. le32_to_cpup((__le32 *)tlv_data);
  660. break;
  661. case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
  662. if (tlv_len != sizeof(u32))
  663. goto invalid_tlv_len;
  664. pieces->init_errlog_ptr =
  665. le32_to_cpup((__le32 *)tlv_data);
  666. break;
  667. case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
  668. if (tlv_len != sizeof(u32))
  669. goto invalid_tlv_len;
  670. pieces->inst_evtlog_ptr =
  671. le32_to_cpup((__le32 *)tlv_data);
  672. break;
  673. case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
  674. if (tlv_len != sizeof(u32))
  675. goto invalid_tlv_len;
  676. pieces->inst_evtlog_size =
  677. le32_to_cpup((__le32 *)tlv_data);
  678. break;
  679. case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
  680. if (tlv_len != sizeof(u32))
  681. goto invalid_tlv_len;
  682. pieces->inst_errlog_ptr =
  683. le32_to_cpup((__le32 *)tlv_data);
  684. break;
  685. case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
  686. if (tlv_len)
  687. goto invalid_tlv_len;
  688. drv->fw.enhance_sensitivity_table = true;
  689. break;
  690. case IWL_UCODE_TLV_WOWLAN_INST:
  691. set_sec_data(pieces, IWL_UCODE_WOWLAN,
  692. IWL_UCODE_SECTION_INST, tlv_data);
  693. set_sec_size(pieces, IWL_UCODE_WOWLAN,
  694. IWL_UCODE_SECTION_INST, tlv_len);
  695. set_sec_offset(pieces, IWL_UCODE_WOWLAN,
  696. IWL_UCODE_SECTION_INST,
  697. IWLAGN_RTC_INST_LOWER_BOUND);
  698. break;
  699. case IWL_UCODE_TLV_WOWLAN_DATA:
  700. set_sec_data(pieces, IWL_UCODE_WOWLAN,
  701. IWL_UCODE_SECTION_DATA, tlv_data);
  702. set_sec_size(pieces, IWL_UCODE_WOWLAN,
  703. IWL_UCODE_SECTION_DATA, tlv_len);
  704. set_sec_offset(pieces, IWL_UCODE_WOWLAN,
  705. IWL_UCODE_SECTION_DATA,
  706. IWLAGN_RTC_DATA_LOWER_BOUND);
  707. break;
  708. case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
  709. if (tlv_len != sizeof(u32))
  710. goto invalid_tlv_len;
  711. capa->standard_phy_calibration_size =
  712. le32_to_cpup((__le32 *)tlv_data);
  713. break;
  714. case IWL_UCODE_TLV_SEC_RT:
  715. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
  716. tlv_len);
  717. drv->fw.type = IWL_FW_MVM;
  718. break;
  719. case IWL_UCODE_TLV_SEC_INIT:
  720. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
  721. tlv_len);
  722. drv->fw.type = IWL_FW_MVM;
  723. break;
  724. case IWL_UCODE_TLV_SEC_WOWLAN:
  725. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
  726. tlv_len);
  727. drv->fw.type = IWL_FW_MVM;
  728. break;
  729. case IWL_UCODE_TLV_DEF_CALIB:
  730. if (tlv_len != sizeof(struct iwl_tlv_calib_data))
  731. goto invalid_tlv_len;
  732. if (iwl_set_default_calib(drv, tlv_data))
  733. goto tlv_error;
  734. break;
  735. case IWL_UCODE_TLV_PHY_SKU:
  736. if (tlv_len != sizeof(u32))
  737. goto invalid_tlv_len;
  738. drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
  739. drv->fw.valid_tx_ant = (drv->fw.phy_config &
  740. FW_PHY_CFG_TX_CHAIN) >>
  741. FW_PHY_CFG_TX_CHAIN_POS;
  742. drv->fw.valid_rx_ant = (drv->fw.phy_config &
  743. FW_PHY_CFG_RX_CHAIN) >>
  744. FW_PHY_CFG_RX_CHAIN_POS;
  745. break;
  746. case IWL_UCODE_TLV_SECURE_SEC_RT:
  747. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
  748. tlv_len);
  749. drv->fw.type = IWL_FW_MVM;
  750. break;
  751. case IWL_UCODE_TLV_SECURE_SEC_INIT:
  752. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
  753. tlv_len);
  754. drv->fw.type = IWL_FW_MVM;
  755. break;
  756. case IWL_UCODE_TLV_SECURE_SEC_WOWLAN:
  757. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
  758. tlv_len);
  759. drv->fw.type = IWL_FW_MVM;
  760. break;
  761. case IWL_UCODE_TLV_NUM_OF_CPU:
  762. if (tlv_len != sizeof(u32))
  763. goto invalid_tlv_len;
  764. num_of_cpus =
  765. le32_to_cpup((__le32 *)tlv_data);
  766. if (num_of_cpus == 2) {
  767. drv->fw.img[IWL_UCODE_REGULAR].is_dual_cpus =
  768. true;
  769. drv->fw.img[IWL_UCODE_INIT].is_dual_cpus =
  770. true;
  771. drv->fw.img[IWL_UCODE_WOWLAN].is_dual_cpus =
  772. true;
  773. } else if ((num_of_cpus > 2) || (num_of_cpus < 1)) {
  774. IWL_ERR(drv, "Driver support upto 2 CPUs\n");
  775. return -EINVAL;
  776. }
  777. break;
  778. case IWL_UCODE_TLV_CSCHEME:
  779. if (iwl_store_cscheme(&drv->fw, tlv_data, tlv_len))
  780. goto invalid_tlv_len;
  781. break;
  782. case IWL_UCODE_TLV_N_SCAN_CHANNELS:
  783. if (tlv_len != sizeof(u32))
  784. goto invalid_tlv_len;
  785. capa->n_scan_channels =
  786. le32_to_cpup((__le32 *)tlv_data);
  787. break;
  788. case IWL_UCODE_TLV_FW_VERSION: {
  789. __le32 *ptr = (void *)tlv_data;
  790. u32 major, minor;
  791. u8 local_comp;
  792. if (tlv_len != sizeof(u32) * 3)
  793. goto invalid_tlv_len;
  794. major = le32_to_cpup(ptr++);
  795. minor = le32_to_cpup(ptr++);
  796. local_comp = le32_to_cpup(ptr);
  797. if (major >= 35)
  798. snprintf(drv->fw.fw_version,
  799. sizeof(drv->fw.fw_version),
  800. "%u.%08x.%u", major, minor, local_comp);
  801. else
  802. snprintf(drv->fw.fw_version,
  803. sizeof(drv->fw.fw_version),
  804. "%u.%u.%u", major, minor, local_comp);
  805. break;
  806. }
  807. case IWL_UCODE_TLV_FW_DBG_DEST: {
  808. struct iwl_fw_dbg_dest_tlv *dest = NULL;
  809. struct iwl_fw_dbg_dest_tlv_v1 *dest_v1 = NULL;
  810. u8 mon_mode;
  811. pieces->dbg_dest_ver = (u8 *)tlv_data;
  812. if (*pieces->dbg_dest_ver == 1) {
  813. dest = (void *)tlv_data;
  814. } else if (*pieces->dbg_dest_ver == 0) {
  815. dest_v1 = (void *)tlv_data;
  816. } else {
  817. IWL_ERR(drv,
  818. "The version is %d, and it is invalid\n",
  819. *pieces->dbg_dest_ver);
  820. break;
  821. }
  822. if (pieces->dbg_dest_tlv_init) {
  823. IWL_ERR(drv,
  824. "dbg destination ignored, already exists\n");
  825. break;
  826. }
  827. pieces->dbg_dest_tlv_init = true;
  828. if (dest_v1) {
  829. pieces->dbg_dest_tlv_v1 = dest_v1;
  830. mon_mode = dest_v1->monitor_mode;
  831. } else {
  832. pieces->dbg_dest_tlv = dest;
  833. mon_mode = dest->monitor_mode;
  834. }
  835. IWL_INFO(drv, "Found debug destination: %s\n",
  836. get_fw_dbg_mode_string(mon_mode));
  837. drv->fw.dbg_dest_reg_num = (dest_v1) ?
  838. tlv_len -
  839. offsetof(struct iwl_fw_dbg_dest_tlv_v1,
  840. reg_ops) :
  841. tlv_len -
  842. offsetof(struct iwl_fw_dbg_dest_tlv,
  843. reg_ops);
  844. drv->fw.dbg_dest_reg_num /=
  845. sizeof(drv->fw.dbg_dest_tlv->reg_ops[0]);
  846. break;
  847. }
  848. case IWL_UCODE_TLV_FW_DBG_CONF: {
  849. struct iwl_fw_dbg_conf_tlv *conf = (void *)tlv_data;
  850. if (!pieces->dbg_dest_tlv_init) {
  851. IWL_ERR(drv,
  852. "Ignore dbg config %d - no destination configured\n",
  853. conf->id);
  854. break;
  855. }
  856. if (conf->id >= ARRAY_SIZE(drv->fw.dbg_conf_tlv)) {
  857. IWL_ERR(drv,
  858. "Skip unknown configuration: %d\n",
  859. conf->id);
  860. break;
  861. }
  862. if (pieces->dbg_conf_tlv[conf->id]) {
  863. IWL_ERR(drv,
  864. "Ignore duplicate dbg config %d\n",
  865. conf->id);
  866. break;
  867. }
  868. if (conf->usniffer)
  869. usniffer_req = true;
  870. IWL_INFO(drv, "Found debug configuration: %d\n",
  871. conf->id);
  872. pieces->dbg_conf_tlv[conf->id] = conf;
  873. pieces->dbg_conf_tlv_len[conf->id] = tlv_len;
  874. break;
  875. }
  876. case IWL_UCODE_TLV_FW_DBG_TRIGGER: {
  877. struct iwl_fw_dbg_trigger_tlv *trigger =
  878. (void *)tlv_data;
  879. u32 trigger_id = le32_to_cpu(trigger->id);
  880. if (trigger_id >= ARRAY_SIZE(drv->fw.dbg_trigger_tlv)) {
  881. IWL_ERR(drv,
  882. "Skip unknown trigger: %u\n",
  883. trigger->id);
  884. break;
  885. }
  886. if (pieces->dbg_trigger_tlv[trigger_id]) {
  887. IWL_ERR(drv,
  888. "Ignore duplicate dbg trigger %u\n",
  889. trigger->id);
  890. break;
  891. }
  892. IWL_INFO(drv, "Found debug trigger: %u\n", trigger->id);
  893. pieces->dbg_trigger_tlv[trigger_id] = trigger;
  894. pieces->dbg_trigger_tlv_len[trigger_id] = tlv_len;
  895. break;
  896. }
  897. case IWL_UCODE_TLV_FW_DBG_DUMP_LST: {
  898. if (tlv_len != sizeof(u32)) {
  899. IWL_ERR(drv,
  900. "dbg lst mask size incorrect, skip\n");
  901. break;
  902. }
  903. drv->fw.dbg_dump_mask =
  904. le32_to_cpup((__le32 *)tlv_data);
  905. break;
  906. }
  907. case IWL_UCODE_TLV_SEC_RT_USNIFFER:
  908. *usniffer_images = true;
  909. iwl_store_ucode_sec(pieces, tlv_data,
  910. IWL_UCODE_REGULAR_USNIFFER,
  911. tlv_len);
  912. break;
  913. case IWL_UCODE_TLV_PAGING:
  914. if (tlv_len != sizeof(u32))
  915. goto invalid_tlv_len;
  916. paging_mem_size = le32_to_cpup((__le32 *)tlv_data);
  917. IWL_DEBUG_FW(drv,
  918. "Paging: paging enabled (size = %u bytes)\n",
  919. paging_mem_size);
  920. if (paging_mem_size > MAX_PAGING_IMAGE_SIZE) {
  921. IWL_ERR(drv,
  922. "Paging: driver supports up to %lu bytes for paging image\n",
  923. MAX_PAGING_IMAGE_SIZE);
  924. return -EINVAL;
  925. }
  926. if (paging_mem_size & (FW_PAGING_SIZE - 1)) {
  927. IWL_ERR(drv,
  928. "Paging: image isn't multiple %lu\n",
  929. FW_PAGING_SIZE);
  930. return -EINVAL;
  931. }
  932. drv->fw.img[IWL_UCODE_REGULAR].paging_mem_size =
  933. paging_mem_size;
  934. usniffer_img = IWL_UCODE_REGULAR_USNIFFER;
  935. drv->fw.img[usniffer_img].paging_mem_size =
  936. paging_mem_size;
  937. break;
  938. case IWL_UCODE_TLV_FW_GSCAN_CAPA:
  939. /* ignored */
  940. break;
  941. case IWL_UCODE_TLV_FW_MEM_SEG: {
  942. struct iwl_fw_dbg_mem_seg_tlv *dbg_mem =
  943. (void *)tlv_data;
  944. u32 type;
  945. size_t size;
  946. struct iwl_fw_dbg_mem_seg_tlv *n;
  947. if (tlv_len != (sizeof(*dbg_mem)))
  948. goto invalid_tlv_len;
  949. type = le32_to_cpu(dbg_mem->data_type);
  950. IWL_DEBUG_INFO(drv, "Found debug memory segment: %u\n",
  951. dbg_mem->data_type);
  952. switch (type & FW_DBG_MEM_TYPE_MASK) {
  953. case FW_DBG_MEM_TYPE_REGULAR:
  954. case FW_DBG_MEM_TYPE_PRPH:
  955. /* we know how to handle these */
  956. break;
  957. default:
  958. IWL_ERR(drv,
  959. "Found debug memory segment with invalid type: 0x%x\n",
  960. type);
  961. return -EINVAL;
  962. }
  963. size = sizeof(*pieces->dbg_mem_tlv) *
  964. (pieces->n_dbg_mem_tlv + 1);
  965. n = krealloc(pieces->dbg_mem_tlv, size, GFP_KERNEL);
  966. if (!n)
  967. return -ENOMEM;
  968. pieces->dbg_mem_tlv = n;
  969. pieces->dbg_mem_tlv[pieces->n_dbg_mem_tlv] = *dbg_mem;
  970. pieces->n_dbg_mem_tlv++;
  971. break;
  972. }
  973. case IWL_UCODE_TLV_IML: {
  974. drv->fw.iml_len = tlv_len;
  975. drv->fw.iml = kmemdup(tlv_data, tlv_len, GFP_KERNEL);
  976. if (!drv->fw.iml)
  977. return -ENOMEM;
  978. break;
  979. }
  980. default:
  981. IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
  982. break;
  983. }
  984. }
  985. if (!fw_has_capa(capa, IWL_UCODE_TLV_CAPA_USNIFFER_UNIFIED) &&
  986. usniffer_req && !*usniffer_images) {
  987. IWL_ERR(drv,
  988. "user selected to work with usniffer but usniffer image isn't available in ucode package\n");
  989. return -EINVAL;
  990. }
  991. if (len) {
  992. IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
  993. iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
  994. return -EINVAL;
  995. }
  996. return 0;
  997. invalid_tlv_len:
  998. IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
  999. tlv_error:
  1000. iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
  1001. return -EINVAL;
  1002. }
  1003. static int iwl_alloc_ucode(struct iwl_drv *drv,
  1004. struct iwl_firmware_pieces *pieces,
  1005. enum iwl_ucode_type type)
  1006. {
  1007. int i;
  1008. struct fw_desc *sec;
  1009. sec = kcalloc(pieces->img[type].sec_counter, sizeof(*sec), GFP_KERNEL);
  1010. if (!sec)
  1011. return -ENOMEM;
  1012. drv->fw.img[type].sec = sec;
  1013. drv->fw.img[type].num_sec = pieces->img[type].sec_counter;
  1014. for (i = 0; i < pieces->img[type].sec_counter; i++)
  1015. if (iwl_alloc_fw_desc(drv, &sec[i], get_sec(pieces, type, i)))
  1016. return -ENOMEM;
  1017. return 0;
  1018. }
  1019. static int validate_sec_sizes(struct iwl_drv *drv,
  1020. struct iwl_firmware_pieces *pieces,
  1021. const struct iwl_cfg *cfg)
  1022. {
  1023. IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %zd\n",
  1024. get_sec_size(pieces, IWL_UCODE_REGULAR,
  1025. IWL_UCODE_SECTION_INST));
  1026. IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %zd\n",
  1027. get_sec_size(pieces, IWL_UCODE_REGULAR,
  1028. IWL_UCODE_SECTION_DATA));
  1029. IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %zd\n",
  1030. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
  1031. IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %zd\n",
  1032. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
  1033. /* Verify that uCode images will fit in card's SRAM. */
  1034. if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
  1035. cfg->max_inst_size) {
  1036. IWL_ERR(drv, "uCode instr len %zd too large to fit in\n",
  1037. get_sec_size(pieces, IWL_UCODE_REGULAR,
  1038. IWL_UCODE_SECTION_INST));
  1039. return -1;
  1040. }
  1041. if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
  1042. cfg->max_data_size) {
  1043. IWL_ERR(drv, "uCode data len %zd too large to fit in\n",
  1044. get_sec_size(pieces, IWL_UCODE_REGULAR,
  1045. IWL_UCODE_SECTION_DATA));
  1046. return -1;
  1047. }
  1048. if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
  1049. cfg->max_inst_size) {
  1050. IWL_ERR(drv, "uCode init instr len %zd too large to fit in\n",
  1051. get_sec_size(pieces, IWL_UCODE_INIT,
  1052. IWL_UCODE_SECTION_INST));
  1053. return -1;
  1054. }
  1055. if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
  1056. cfg->max_data_size) {
  1057. IWL_ERR(drv, "uCode init data len %zd too large to fit in\n",
  1058. get_sec_size(pieces, IWL_UCODE_REGULAR,
  1059. IWL_UCODE_SECTION_DATA));
  1060. return -1;
  1061. }
  1062. return 0;
  1063. }
  1064. static struct iwl_op_mode *
  1065. _iwl_op_mode_start(struct iwl_drv *drv, struct iwlwifi_opmode_table *op)
  1066. {
  1067. const struct iwl_op_mode_ops *ops = op->ops;
  1068. struct dentry *dbgfs_dir = NULL;
  1069. struct iwl_op_mode *op_mode = NULL;
  1070. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1071. drv->dbgfs_op_mode = debugfs_create_dir(op->name,
  1072. drv->dbgfs_drv);
  1073. if (!drv->dbgfs_op_mode) {
  1074. IWL_ERR(drv,
  1075. "failed to create opmode debugfs directory\n");
  1076. return op_mode;
  1077. }
  1078. dbgfs_dir = drv->dbgfs_op_mode;
  1079. #endif
  1080. op_mode = ops->start(drv->trans, drv->trans->cfg, &drv->fw, dbgfs_dir);
  1081. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1082. if (!op_mode) {
  1083. debugfs_remove_recursive(drv->dbgfs_op_mode);
  1084. drv->dbgfs_op_mode = NULL;
  1085. }
  1086. #endif
  1087. return op_mode;
  1088. }
  1089. static void _iwl_op_mode_stop(struct iwl_drv *drv)
  1090. {
  1091. /* op_mode can be NULL if its start failed */
  1092. if (drv->op_mode) {
  1093. iwl_op_mode_stop(drv->op_mode);
  1094. drv->op_mode = NULL;
  1095. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1096. debugfs_remove_recursive(drv->dbgfs_op_mode);
  1097. drv->dbgfs_op_mode = NULL;
  1098. #endif
  1099. }
  1100. }
  1101. /**
  1102. * iwl_req_fw_callback - callback when firmware was loaded
  1103. *
  1104. * If loaded successfully, copies the firmware into buffers
  1105. * for the card to fetch (via DMA).
  1106. */
  1107. static void iwl_req_fw_callback(const struct firmware *ucode_raw, void *context)
  1108. {
  1109. struct iwl_drv *drv = context;
  1110. struct iwl_fw *fw = &drv->fw;
  1111. struct iwl_ucode_header *ucode;
  1112. struct iwlwifi_opmode_table *op;
  1113. int err;
  1114. struct iwl_firmware_pieces *pieces;
  1115. const unsigned int api_max = drv->trans->cfg->ucode_api_max;
  1116. const unsigned int api_min = drv->trans->cfg->ucode_api_min;
  1117. size_t trigger_tlv_sz[FW_DBG_TRIGGER_MAX];
  1118. u32 api_ver;
  1119. int i;
  1120. bool load_module = false;
  1121. bool usniffer_images = false;
  1122. fw->ucode_capa.max_probe_length = IWL_DEFAULT_MAX_PROBE_LENGTH;
  1123. fw->ucode_capa.standard_phy_calibration_size =
  1124. IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  1125. fw->ucode_capa.n_scan_channels = IWL_DEFAULT_SCAN_CHANNELS;
  1126. /* dump all fw memory areas by default */
  1127. fw->dbg_dump_mask = 0xffffffff;
  1128. pieces = kzalloc(sizeof(*pieces), GFP_KERNEL);
  1129. if (!pieces)
  1130. goto out_free_fw;
  1131. if (!ucode_raw)
  1132. goto try_again;
  1133. IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
  1134. drv->firmware_name, ucode_raw->size);
  1135. /* Make sure that we got at least the API version number */
  1136. if (ucode_raw->size < 4) {
  1137. IWL_ERR(drv, "File size way too small!\n");
  1138. goto try_again;
  1139. }
  1140. /* Data from ucode file: header followed by uCode images */
  1141. ucode = (struct iwl_ucode_header *)ucode_raw->data;
  1142. if (ucode->ver)
  1143. err = iwl_parse_v1_v2_firmware(drv, ucode_raw, pieces);
  1144. else
  1145. err = iwl_parse_tlv_firmware(drv, ucode_raw, pieces,
  1146. &fw->ucode_capa, &usniffer_images);
  1147. if (err)
  1148. goto try_again;
  1149. if (fw_has_api(&drv->fw.ucode_capa, IWL_UCODE_TLV_API_NEW_VERSION))
  1150. api_ver = drv->fw.ucode_ver;
  1151. else
  1152. api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
  1153. /*
  1154. * api_ver should match the api version forming part of the
  1155. * firmware filename ... but we don't check for that and only rely
  1156. * on the API version read from firmware header from here on forward
  1157. */
  1158. if (api_ver < api_min || api_ver > api_max) {
  1159. IWL_ERR(drv,
  1160. "Driver unable to support your firmware API. "
  1161. "Driver supports v%u, firmware is v%u.\n",
  1162. api_max, api_ver);
  1163. goto try_again;
  1164. }
  1165. /*
  1166. * In mvm uCode there is no difference between data and instructions
  1167. * sections.
  1168. */
  1169. if (fw->type == IWL_FW_DVM && validate_sec_sizes(drv, pieces,
  1170. drv->trans->cfg))
  1171. goto try_again;
  1172. /* Allocate ucode buffers for card's bus-master loading ... */
  1173. /* Runtime instructions and 2 copies of data:
  1174. * 1) unmodified from disk
  1175. * 2) backup cache for save/restore during power-downs
  1176. */
  1177. for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
  1178. if (iwl_alloc_ucode(drv, pieces, i))
  1179. goto out_free_fw;
  1180. if (pieces->dbg_dest_tlv_init) {
  1181. size_t dbg_dest_size = sizeof(*drv->fw.dbg_dest_tlv) +
  1182. sizeof(drv->fw.dbg_dest_tlv->reg_ops[0]) *
  1183. drv->fw.dbg_dest_reg_num;
  1184. drv->fw.dbg_dest_tlv = kmalloc(dbg_dest_size, GFP_KERNEL);
  1185. if (!drv->fw.dbg_dest_tlv)
  1186. goto out_free_fw;
  1187. if (*pieces->dbg_dest_ver == 0) {
  1188. memcpy(drv->fw.dbg_dest_tlv, pieces->dbg_dest_tlv_v1,
  1189. dbg_dest_size);
  1190. } else {
  1191. struct iwl_fw_dbg_dest_tlv_v1 *dest_tlv =
  1192. drv->fw.dbg_dest_tlv;
  1193. dest_tlv->version = pieces->dbg_dest_tlv->version;
  1194. dest_tlv->monitor_mode =
  1195. pieces->dbg_dest_tlv->monitor_mode;
  1196. dest_tlv->size_power =
  1197. pieces->dbg_dest_tlv->size_power;
  1198. dest_tlv->wrap_count =
  1199. pieces->dbg_dest_tlv->wrap_count;
  1200. dest_tlv->write_ptr_reg =
  1201. pieces->dbg_dest_tlv->write_ptr_reg;
  1202. dest_tlv->base_shift =
  1203. pieces->dbg_dest_tlv->base_shift;
  1204. memcpy(dest_tlv->reg_ops,
  1205. pieces->dbg_dest_tlv->reg_ops,
  1206. sizeof(drv->fw.dbg_dest_tlv->reg_ops[0]) *
  1207. drv->fw.dbg_dest_reg_num);
  1208. /* In version 1 of the destination tlv, which is
  1209. * relevant for internal buffer exclusively,
  1210. * the base address is part of given with the length
  1211. * of the buffer, and the size shift is give instead of
  1212. * end shift. We now store these values in base_reg,
  1213. * and end shift, and when dumping the data we'll
  1214. * manipulate it for extracting both the length and
  1215. * base address */
  1216. dest_tlv->base_reg = pieces->dbg_dest_tlv->cfg_reg;
  1217. dest_tlv->end_shift =
  1218. pieces->dbg_dest_tlv->size_shift;
  1219. }
  1220. }
  1221. for (i = 0; i < ARRAY_SIZE(drv->fw.dbg_conf_tlv); i++) {
  1222. if (pieces->dbg_conf_tlv[i]) {
  1223. drv->fw.dbg_conf_tlv_len[i] =
  1224. pieces->dbg_conf_tlv_len[i];
  1225. drv->fw.dbg_conf_tlv[i] =
  1226. kmemdup(pieces->dbg_conf_tlv[i],
  1227. drv->fw.dbg_conf_tlv_len[i],
  1228. GFP_KERNEL);
  1229. if (!drv->fw.dbg_conf_tlv[i])
  1230. goto out_free_fw;
  1231. }
  1232. }
  1233. memset(&trigger_tlv_sz, 0xff, sizeof(trigger_tlv_sz));
  1234. trigger_tlv_sz[FW_DBG_TRIGGER_MISSED_BEACONS] =
  1235. sizeof(struct iwl_fw_dbg_trigger_missed_bcon);
  1236. trigger_tlv_sz[FW_DBG_TRIGGER_CHANNEL_SWITCH] = 0;
  1237. trigger_tlv_sz[FW_DBG_TRIGGER_FW_NOTIF] =
  1238. sizeof(struct iwl_fw_dbg_trigger_cmd);
  1239. trigger_tlv_sz[FW_DBG_TRIGGER_MLME] =
  1240. sizeof(struct iwl_fw_dbg_trigger_mlme);
  1241. trigger_tlv_sz[FW_DBG_TRIGGER_STATS] =
  1242. sizeof(struct iwl_fw_dbg_trigger_stats);
  1243. trigger_tlv_sz[FW_DBG_TRIGGER_RSSI] =
  1244. sizeof(struct iwl_fw_dbg_trigger_low_rssi);
  1245. trigger_tlv_sz[FW_DBG_TRIGGER_TXQ_TIMERS] =
  1246. sizeof(struct iwl_fw_dbg_trigger_txq_timer);
  1247. trigger_tlv_sz[FW_DBG_TRIGGER_TIME_EVENT] =
  1248. sizeof(struct iwl_fw_dbg_trigger_time_event);
  1249. trigger_tlv_sz[FW_DBG_TRIGGER_BA] =
  1250. sizeof(struct iwl_fw_dbg_trigger_ba);
  1251. trigger_tlv_sz[FW_DBG_TRIGGER_TDLS] =
  1252. sizeof(struct iwl_fw_dbg_trigger_tdls);
  1253. for (i = 0; i < ARRAY_SIZE(drv->fw.dbg_trigger_tlv); i++) {
  1254. if (pieces->dbg_trigger_tlv[i]) {
  1255. /*
  1256. * If the trigger isn't long enough, WARN and exit.
  1257. * Someone is trying to debug something and he won't
  1258. * be able to catch the bug he is trying to chase.
  1259. * We'd better be noisy to be sure he knows what's
  1260. * going on.
  1261. */
  1262. if (WARN_ON(pieces->dbg_trigger_tlv_len[i] <
  1263. (trigger_tlv_sz[i] +
  1264. sizeof(struct iwl_fw_dbg_trigger_tlv))))
  1265. goto out_free_fw;
  1266. drv->fw.dbg_trigger_tlv_len[i] =
  1267. pieces->dbg_trigger_tlv_len[i];
  1268. drv->fw.dbg_trigger_tlv[i] =
  1269. kmemdup(pieces->dbg_trigger_tlv[i],
  1270. drv->fw.dbg_trigger_tlv_len[i],
  1271. GFP_KERNEL);
  1272. if (!drv->fw.dbg_trigger_tlv[i])
  1273. goto out_free_fw;
  1274. }
  1275. }
  1276. /* Now that we can no longer fail, copy information */
  1277. drv->fw.dbg_mem_tlv = pieces->dbg_mem_tlv;
  1278. pieces->dbg_mem_tlv = NULL;
  1279. drv->fw.n_dbg_mem_tlv = pieces->n_dbg_mem_tlv;
  1280. /*
  1281. * The (size - 16) / 12 formula is based on the information recorded
  1282. * for each event, which is of mode 1 (including timestamp) for all
  1283. * new microcodes that include this information.
  1284. */
  1285. fw->init_evtlog_ptr = pieces->init_evtlog_ptr;
  1286. if (pieces->init_evtlog_size)
  1287. fw->init_evtlog_size = (pieces->init_evtlog_size - 16)/12;
  1288. else
  1289. fw->init_evtlog_size =
  1290. drv->trans->cfg->base_params->max_event_log_size;
  1291. fw->init_errlog_ptr = pieces->init_errlog_ptr;
  1292. fw->inst_evtlog_ptr = pieces->inst_evtlog_ptr;
  1293. if (pieces->inst_evtlog_size)
  1294. fw->inst_evtlog_size = (pieces->inst_evtlog_size - 16)/12;
  1295. else
  1296. fw->inst_evtlog_size =
  1297. drv->trans->cfg->base_params->max_event_log_size;
  1298. fw->inst_errlog_ptr = pieces->inst_errlog_ptr;
  1299. /*
  1300. * figure out the offset of chain noise reset and gain commands
  1301. * base on the size of standard phy calibration commands table size
  1302. */
  1303. if (fw->ucode_capa.standard_phy_calibration_size >
  1304. IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
  1305. fw->ucode_capa.standard_phy_calibration_size =
  1306. IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  1307. /* We have our copies now, allow OS release its copies */
  1308. release_firmware(ucode_raw);
  1309. mutex_lock(&iwlwifi_opmode_table_mtx);
  1310. switch (fw->type) {
  1311. case IWL_FW_DVM:
  1312. op = &iwlwifi_opmode_table[DVM_OP_MODE];
  1313. break;
  1314. default:
  1315. WARN(1, "Invalid fw type %d\n", fw->type);
  1316. case IWL_FW_MVM:
  1317. op = &iwlwifi_opmode_table[MVM_OP_MODE];
  1318. break;
  1319. }
  1320. IWL_INFO(drv, "loaded firmware version %s op_mode %s\n",
  1321. drv->fw.fw_version, op->name);
  1322. /* add this device to the list of devices using this op_mode */
  1323. list_add_tail(&drv->list, &op->drv);
  1324. if (op->ops) {
  1325. drv->op_mode = _iwl_op_mode_start(drv, op);
  1326. if (!drv->op_mode) {
  1327. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1328. goto out_unbind;
  1329. }
  1330. } else {
  1331. load_module = true;
  1332. }
  1333. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1334. /*
  1335. * Complete the firmware request last so that
  1336. * a driver unbind (stop) doesn't run while we
  1337. * are doing the start() above.
  1338. */
  1339. complete(&drv->request_firmware_complete);
  1340. /*
  1341. * Load the module last so we don't block anything
  1342. * else from proceeding if the module fails to load
  1343. * or hangs loading.
  1344. */
  1345. if (load_module) {
  1346. request_module("%s", op->name);
  1347. #ifdef CONFIG_IWLWIFI_OPMODE_MODULAR
  1348. if (err)
  1349. IWL_ERR(drv,
  1350. "failed to load module %s (error %d), is dynamic loading enabled?\n",
  1351. op->name, err);
  1352. #endif
  1353. }
  1354. goto free;
  1355. try_again:
  1356. /* try next, if any */
  1357. release_firmware(ucode_raw);
  1358. if (iwl_request_firmware(drv, false))
  1359. goto out_unbind;
  1360. goto free;
  1361. out_free_fw:
  1362. iwl_dealloc_ucode(drv);
  1363. release_firmware(ucode_raw);
  1364. out_unbind:
  1365. complete(&drv->request_firmware_complete);
  1366. device_release_driver(drv->trans->dev);
  1367. free:
  1368. if (pieces) {
  1369. for (i = 0; i < ARRAY_SIZE(pieces->img); i++)
  1370. kfree(pieces->img[i].sec);
  1371. kfree(pieces->dbg_mem_tlv);
  1372. kfree(pieces);
  1373. }
  1374. }
  1375. struct iwl_drv *iwl_drv_start(struct iwl_trans *trans)
  1376. {
  1377. struct iwl_drv *drv;
  1378. int ret;
  1379. drv = kzalloc(sizeof(*drv), GFP_KERNEL);
  1380. if (!drv) {
  1381. ret = -ENOMEM;
  1382. goto err;
  1383. }
  1384. drv->trans = trans;
  1385. drv->dev = trans->dev;
  1386. init_completion(&drv->request_firmware_complete);
  1387. INIT_LIST_HEAD(&drv->list);
  1388. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1389. /* Create the device debugfs entries. */
  1390. drv->dbgfs_drv = debugfs_create_dir(dev_name(trans->dev),
  1391. iwl_dbgfs_root);
  1392. if (!drv->dbgfs_drv) {
  1393. IWL_ERR(drv, "failed to create debugfs directory\n");
  1394. ret = -ENOMEM;
  1395. goto err_free_drv;
  1396. }
  1397. /* Create transport layer debugfs dir */
  1398. drv->trans->dbgfs_dir = debugfs_create_dir("trans", drv->dbgfs_drv);
  1399. if (!drv->trans->dbgfs_dir) {
  1400. IWL_ERR(drv, "failed to create transport debugfs directory\n");
  1401. ret = -ENOMEM;
  1402. goto err_free_dbgfs;
  1403. }
  1404. #endif
  1405. ret = iwl_request_firmware(drv, true);
  1406. if (ret) {
  1407. IWL_ERR(trans, "Couldn't request the fw\n");
  1408. goto err_fw;
  1409. }
  1410. return drv;
  1411. err_fw:
  1412. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1413. err_free_dbgfs:
  1414. debugfs_remove_recursive(drv->dbgfs_drv);
  1415. err_free_drv:
  1416. #endif
  1417. kfree(drv);
  1418. err:
  1419. return ERR_PTR(ret);
  1420. }
  1421. void iwl_drv_stop(struct iwl_drv *drv)
  1422. {
  1423. wait_for_completion(&drv->request_firmware_complete);
  1424. _iwl_op_mode_stop(drv);
  1425. iwl_dealloc_ucode(drv);
  1426. mutex_lock(&iwlwifi_opmode_table_mtx);
  1427. /*
  1428. * List is empty (this item wasn't added)
  1429. * when firmware loading failed -- in that
  1430. * case we can't remove it from any list.
  1431. */
  1432. if (!list_empty(&drv->list))
  1433. list_del(&drv->list);
  1434. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1435. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1436. debugfs_remove_recursive(drv->dbgfs_drv);
  1437. #endif
  1438. kfree(drv);
  1439. }
  1440. /* shared module parameters */
  1441. struct iwl_mod_params iwlwifi_mod_params = {
  1442. .fw_restart = true,
  1443. .bt_coex_active = true,
  1444. .power_level = IWL_POWER_INDEX_1,
  1445. .d0i3_disable = true,
  1446. .d0i3_timeout = 1000,
  1447. .uapsd_disable = IWL_DISABLE_UAPSD_BSS | IWL_DISABLE_UAPSD_P2P_CLIENT,
  1448. /* the rest are 0 by default */
  1449. };
  1450. IWL_EXPORT_SYMBOL(iwlwifi_mod_params);
  1451. int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops)
  1452. {
  1453. int i;
  1454. struct iwl_drv *drv;
  1455. struct iwlwifi_opmode_table *op;
  1456. mutex_lock(&iwlwifi_opmode_table_mtx);
  1457. for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
  1458. op = &iwlwifi_opmode_table[i];
  1459. if (strcmp(op->name, name))
  1460. continue;
  1461. op->ops = ops;
  1462. /* TODO: need to handle exceptional case */
  1463. list_for_each_entry(drv, &op->drv, list)
  1464. drv->op_mode = _iwl_op_mode_start(drv, op);
  1465. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1466. return 0;
  1467. }
  1468. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1469. return -EIO;
  1470. }
  1471. IWL_EXPORT_SYMBOL(iwl_opmode_register);
  1472. void iwl_opmode_deregister(const char *name)
  1473. {
  1474. int i;
  1475. struct iwl_drv *drv;
  1476. mutex_lock(&iwlwifi_opmode_table_mtx);
  1477. for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
  1478. if (strcmp(iwlwifi_opmode_table[i].name, name))
  1479. continue;
  1480. iwlwifi_opmode_table[i].ops = NULL;
  1481. /* call the stop routine for all devices */
  1482. list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list)
  1483. _iwl_op_mode_stop(drv);
  1484. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1485. return;
  1486. }
  1487. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1488. }
  1489. IWL_EXPORT_SYMBOL(iwl_opmode_deregister);
  1490. static int __init iwl_drv_init(void)
  1491. {
  1492. int i;
  1493. mutex_init(&iwlwifi_opmode_table_mtx);
  1494. for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++)
  1495. INIT_LIST_HEAD(&iwlwifi_opmode_table[i].drv);
  1496. pr_info(DRV_DESCRIPTION "\n");
  1497. pr_info(DRV_COPYRIGHT "\n");
  1498. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1499. /* Create the root of iwlwifi debugfs subsystem. */
  1500. iwl_dbgfs_root = debugfs_create_dir(DRV_NAME, NULL);
  1501. if (!iwl_dbgfs_root)
  1502. return -EFAULT;
  1503. #endif
  1504. return iwl_pci_register_driver();
  1505. }
  1506. module_init(iwl_drv_init);
  1507. static void __exit iwl_drv_exit(void)
  1508. {
  1509. iwl_pci_unregister_driver();
  1510. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1511. debugfs_remove_recursive(iwl_dbgfs_root);
  1512. #endif
  1513. }
  1514. module_exit(iwl_drv_exit);
  1515. #ifdef CONFIG_IWLWIFI_DEBUG
  1516. module_param_named(debug, iwlwifi_mod_params.debug_level, uint, 0644);
  1517. MODULE_PARM_DESC(debug, "debug output mask");
  1518. #endif
  1519. module_param_named(swcrypto, iwlwifi_mod_params.swcrypto, int, 0444);
  1520. MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
  1521. module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, 0444);
  1522. MODULE_PARM_DESC(11n_disable,
  1523. "disable 11n functionality, bitmap: 1: full, 2: disable agg TX, 4: disable agg RX, 8 enable agg TX");
  1524. module_param_named(amsdu_size, iwlwifi_mod_params.amsdu_size, int, 0444);
  1525. MODULE_PARM_DESC(amsdu_size,
  1526. "amsdu size 0: 12K for multi Rx queue devices, 2K for 22560 devices, "
  1527. "4K for other devices 1:4K 2:8K 3:12K 4: 2K (default 0)");
  1528. module_param_named(fw_restart, iwlwifi_mod_params.fw_restart, bool, 0444);
  1529. MODULE_PARM_DESC(fw_restart, "restart firmware in case of error (default true)");
  1530. module_param_named(antenna_coupling, iwlwifi_mod_params.antenna_coupling,
  1531. int, 0444);
  1532. MODULE_PARM_DESC(antenna_coupling,
  1533. "specify antenna coupling in dB (default: 0 dB)");
  1534. module_param_named(nvm_file, iwlwifi_mod_params.nvm_file, charp, 0444);
  1535. MODULE_PARM_DESC(nvm_file, "NVM file name");
  1536. module_param_named(d0i3_disable, iwlwifi_mod_params.d0i3_disable, bool, 0444);
  1537. MODULE_PARM_DESC(d0i3_disable, "disable d0i3 functionality (default: Y)");
  1538. module_param_named(lar_disable, iwlwifi_mod_params.lar_disable, bool, 0444);
  1539. MODULE_PARM_DESC(lar_disable, "disable LAR functionality (default: N)");
  1540. module_param_named(uapsd_disable, iwlwifi_mod_params.uapsd_disable, uint, 0644);
  1541. MODULE_PARM_DESC(uapsd_disable,
  1542. "disable U-APSD functionality bitmap 1: BSS 2: P2P Client (default: 3)");
  1543. /*
  1544. * set bt_coex_active to true, uCode will do kill/defer
  1545. * every time the priority line is asserted (BT is sending signals on the
  1546. * priority line in the PCIx).
  1547. * set bt_coex_active to false, uCode will ignore the BT activity and
  1548. * perform the normal operation
  1549. *
  1550. * User might experience transmit issue on some platform due to WiFi/BT
  1551. * co-exist problem. The possible behaviors are:
  1552. * Able to scan and finding all the available AP
  1553. * Not able to associate with any AP
  1554. * On those platforms, WiFi communication can be restored by set
  1555. * "bt_coex_active" module parameter to "false"
  1556. *
  1557. * default: bt_coex_active = true (BT_COEX_ENABLE)
  1558. */
  1559. module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
  1560. bool, 0444);
  1561. MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
  1562. module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, 0444);
  1563. MODULE_PARM_DESC(led_mode, "0=system default, "
  1564. "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
  1565. module_param_named(power_save, iwlwifi_mod_params.power_save, bool, 0444);
  1566. MODULE_PARM_DESC(power_save,
  1567. "enable WiFi power management (default: disable)");
  1568. module_param_named(power_level, iwlwifi_mod_params.power_level, int, 0444);
  1569. MODULE_PARM_DESC(power_level,
  1570. "default power save level (range from 1 - 5, default: 1)");
  1571. module_param_named(fw_monitor, iwlwifi_mod_params.fw_monitor, bool, 0444);
  1572. MODULE_PARM_DESC(fw_monitor,
  1573. "firmware monitor - to debug FW (default: false - needs lots of memory)");
  1574. module_param_named(d0i3_timeout, iwlwifi_mod_params.d0i3_timeout, uint, 0444);
  1575. MODULE_PARM_DESC(d0i3_timeout, "Timeout to D0i3 entry when idle (ms)");
  1576. module_param_named(disable_11ac, iwlwifi_mod_params.disable_11ac, bool, 0444);
  1577. MODULE_PARM_DESC(disable_11ac, "Disable VHT capabilities (default: false)");
  1578. module_param_named(remove_when_gone,
  1579. iwlwifi_mod_params.remove_when_gone, bool,
  1580. 0444);
  1581. MODULE_PARM_DESC(remove_when_gone,
  1582. "Remove dev from PCIe bus if it is deemed inaccessible (default: false)");
  1583. module_param_named(disable_11ax, iwlwifi_mod_params.disable_11ax, bool,
  1584. S_IRUGO);
  1585. MODULE_PARM_DESC(disable_11ax, "Disable HE capabilities (default: false)");