iwl-drv.c 53 KB

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