iwl-drv.c 51 KB

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