iwl-drv.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  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. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called COPYING.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <linux/completion.h>
  64. #include <linux/dma-mapping.h>
  65. #include <linux/firmware.h>
  66. #include <linux/module.h>
  67. #include <linux/vmalloc.h>
  68. #include "iwl-drv.h"
  69. #include "iwl-debug.h"
  70. #include "iwl-trans.h"
  71. #include "iwl-op-mode.h"
  72. #include "iwl-agn-hw.h"
  73. #include "iwl-fw.h"
  74. #include "iwl-config.h"
  75. #include "iwl-modparams.h"
  76. /* private includes */
  77. #include "iwl-fw-file.h"
  78. /******************************************************************************
  79. *
  80. * module boiler plate
  81. *
  82. ******************************************************************************/
  83. /*
  84. * module name, copyright, version, etc.
  85. */
  86. #define DRV_DESCRIPTION "Intel(R) Wireless WiFi driver for Linux"
  87. #ifdef CONFIG_IWLWIFI_DEBUG
  88. #define VD "d"
  89. #else
  90. #define VD
  91. #endif
  92. #define DRV_VERSION IWLWIFI_VERSION VD
  93. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  94. MODULE_VERSION(DRV_VERSION);
  95. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  96. MODULE_LICENSE("GPL");
  97. #ifdef CONFIG_IWLWIFI_DEBUGFS
  98. static struct dentry *iwl_dbgfs_root;
  99. #endif
  100. /**
  101. * struct iwl_drv - drv common data
  102. * @list: list of drv structures using this opmode
  103. * @fw: the iwl_fw structure
  104. * @op_mode: the running op_mode
  105. * @trans: transport layer
  106. * @dev: for debug prints only
  107. * @cfg: configuration struct
  108. * @fw_index: firmware revision to try loading
  109. * @firmware_name: composite filename of ucode file to load
  110. * @request_firmware_complete: the firmware has been obtained from user space
  111. */
  112. struct iwl_drv {
  113. struct list_head list;
  114. struct iwl_fw fw;
  115. struct iwl_op_mode *op_mode;
  116. struct iwl_trans *trans;
  117. struct device *dev;
  118. const struct iwl_cfg *cfg;
  119. int fw_index; /* firmware we're trying to load */
  120. char firmware_name[32]; /* name of firmware file to load */
  121. struct completion request_firmware_complete;
  122. #ifdef CONFIG_IWLWIFI_DEBUGFS
  123. struct dentry *dbgfs_drv;
  124. struct dentry *dbgfs_trans;
  125. struct dentry *dbgfs_op_mode;
  126. #endif
  127. };
  128. enum {
  129. DVM_OP_MODE = 0,
  130. MVM_OP_MODE = 1,
  131. };
  132. /* Protects the table contents, i.e. the ops pointer & drv list */
  133. static struct mutex iwlwifi_opmode_table_mtx;
  134. static struct iwlwifi_opmode_table {
  135. const char *name; /* name: iwldvm, iwlmvm, etc */
  136. const struct iwl_op_mode_ops *ops; /* pointer to op_mode ops */
  137. struct list_head drv; /* list of devices using this op_mode */
  138. } iwlwifi_opmode_table[] = { /* ops set when driver is initialized */
  139. [DVM_OP_MODE] = { .name = "iwldvm", .ops = NULL },
  140. [MVM_OP_MODE] = { .name = "iwlmvm", .ops = NULL },
  141. };
  142. /*
  143. * struct fw_sec: Just for the image parsing proccess.
  144. * For the fw storage we are using struct fw_desc.
  145. */
  146. struct fw_sec {
  147. const void *data; /* the sec data */
  148. size_t size; /* section size */
  149. u32 offset; /* offset of writing in the device */
  150. };
  151. static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc)
  152. {
  153. vfree(desc->data);
  154. desc->data = NULL;
  155. desc->len = 0;
  156. }
  157. static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img)
  158. {
  159. int i;
  160. for (i = 0; i < IWL_UCODE_SECTION_MAX; i++)
  161. iwl_free_fw_desc(drv, &img->sec[i]);
  162. }
  163. static void iwl_dealloc_ucode(struct iwl_drv *drv)
  164. {
  165. int i;
  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. #define UCODE_EXPERIMENTAL_INDEX 100
  188. #define UCODE_EXPERIMENTAL_TAG "exp"
  189. static int iwl_request_firmware(struct iwl_drv *drv, bool first)
  190. {
  191. const char *name_pre = drv->cfg->fw_name_pre;
  192. char tag[8];
  193. if (first) {
  194. #ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
  195. drv->fw_index = UCODE_EXPERIMENTAL_INDEX;
  196. strcpy(tag, UCODE_EXPERIMENTAL_TAG);
  197. } else if (drv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
  198. #endif
  199. drv->fw_index = drv->cfg->ucode_api_max;
  200. sprintf(tag, "%d", drv->fw_index);
  201. } else {
  202. drv->fw_index--;
  203. sprintf(tag, "%d", drv->fw_index);
  204. }
  205. if (drv->fw_index < drv->cfg->ucode_api_min) {
  206. IWL_ERR(drv, "no suitable firmware found!\n");
  207. return -ENOENT;
  208. }
  209. snprintf(drv->firmware_name, sizeof(drv->firmware_name), "%s%s.ucode",
  210. name_pre, tag);
  211. IWL_DEBUG_INFO(drv, "attempting to load firmware %s'%s'\n",
  212. (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  213. ? "EXPERIMENTAL " : "",
  214. drv->firmware_name);
  215. return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name,
  216. drv->trans->dev,
  217. GFP_KERNEL, drv, iwl_req_fw_callback);
  218. }
  219. struct fw_img_parsing {
  220. struct fw_sec sec[IWL_UCODE_SECTION_MAX];
  221. int sec_counter;
  222. };
  223. /*
  224. * struct fw_sec_parsing: to extract fw section and it's offset from tlv
  225. */
  226. struct fw_sec_parsing {
  227. __le32 offset;
  228. const u8 data[];
  229. } __packed;
  230. /**
  231. * struct iwl_tlv_calib_data - parse the default calib data from TLV
  232. *
  233. * @ucode_type: the uCode to which the following default calib relates.
  234. * @calib: default calibrations.
  235. */
  236. struct iwl_tlv_calib_data {
  237. __le32 ucode_type;
  238. struct iwl_tlv_calib_ctrl calib;
  239. } __packed;
  240. struct iwl_firmware_pieces {
  241. struct fw_img_parsing img[IWL_UCODE_TYPE_MAX];
  242. u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
  243. u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
  244. };
  245. /*
  246. * These functions are just to extract uCode section data from the pieces
  247. * structure.
  248. */
  249. static struct fw_sec *get_sec(struct iwl_firmware_pieces *pieces,
  250. enum iwl_ucode_type type,
  251. int sec)
  252. {
  253. return &pieces->img[type].sec[sec];
  254. }
  255. static void set_sec_data(struct iwl_firmware_pieces *pieces,
  256. enum iwl_ucode_type type,
  257. int sec,
  258. const void *data)
  259. {
  260. pieces->img[type].sec[sec].data = data;
  261. }
  262. static void set_sec_size(struct iwl_firmware_pieces *pieces,
  263. enum iwl_ucode_type type,
  264. int sec,
  265. size_t size)
  266. {
  267. pieces->img[type].sec[sec].size = size;
  268. }
  269. static size_t get_sec_size(struct iwl_firmware_pieces *pieces,
  270. enum iwl_ucode_type type,
  271. int sec)
  272. {
  273. return pieces->img[type].sec[sec].size;
  274. }
  275. static void set_sec_offset(struct iwl_firmware_pieces *pieces,
  276. enum iwl_ucode_type type,
  277. int sec,
  278. u32 offset)
  279. {
  280. pieces->img[type].sec[sec].offset = offset;
  281. }
  282. static int iwl_store_cscheme(struct iwl_fw *fw, const u8 *data, const u32 len)
  283. {
  284. int i, j;
  285. struct iwl_fw_cscheme_list *l = (struct iwl_fw_cscheme_list *)data;
  286. struct iwl_fw_cipher_scheme *fwcs;
  287. struct ieee80211_cipher_scheme *cs;
  288. u32 cipher;
  289. if (len < sizeof(*l) ||
  290. len < sizeof(l->size) + l->size * sizeof(l->cs[0]))
  291. return -EINVAL;
  292. for (i = 0, j = 0; i < IWL_UCODE_MAX_CS && i < l->size; i++) {
  293. fwcs = &l->cs[j];
  294. cipher = le32_to_cpu(fwcs->cipher);
  295. /* we skip schemes with zero cipher suite selector */
  296. if (!cipher)
  297. continue;
  298. cs = &fw->cs[j++];
  299. cs->cipher = cipher;
  300. cs->iftype = BIT(NL80211_IFTYPE_STATION);
  301. cs->hdr_len = fwcs->hdr_len;
  302. cs->pn_len = fwcs->pn_len;
  303. cs->pn_off = fwcs->pn_off;
  304. cs->key_idx_off = fwcs->key_idx_off;
  305. cs->key_idx_mask = fwcs->key_idx_mask;
  306. cs->key_idx_shift = fwcs->key_idx_shift;
  307. cs->mic_len = fwcs->mic_len;
  308. }
  309. return 0;
  310. }
  311. /*
  312. * Gets uCode section from tlv.
  313. */
  314. static int iwl_store_ucode_sec(struct iwl_firmware_pieces *pieces,
  315. const void *data, enum iwl_ucode_type type,
  316. int size)
  317. {
  318. struct fw_img_parsing *img;
  319. struct fw_sec *sec;
  320. struct fw_sec_parsing *sec_parse;
  321. if (WARN_ON(!pieces || !data || type >= IWL_UCODE_TYPE_MAX))
  322. return -1;
  323. sec_parse = (struct fw_sec_parsing *)data;
  324. img = &pieces->img[type];
  325. sec = &img->sec[img->sec_counter];
  326. sec->offset = le32_to_cpu(sec_parse->offset);
  327. sec->data = sec_parse->data;
  328. sec->size = size - sizeof(sec_parse->offset);
  329. ++img->sec_counter;
  330. return 0;
  331. }
  332. static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data)
  333. {
  334. struct iwl_tlv_calib_data *def_calib =
  335. (struct iwl_tlv_calib_data *)data;
  336. u32 ucode_type = le32_to_cpu(def_calib->ucode_type);
  337. if (ucode_type >= IWL_UCODE_TYPE_MAX) {
  338. IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n",
  339. ucode_type);
  340. return -EINVAL;
  341. }
  342. drv->fw.default_calib[ucode_type].flow_trigger =
  343. def_calib->calib.flow_trigger;
  344. drv->fw.default_calib[ucode_type].event_trigger =
  345. def_calib->calib.event_trigger;
  346. return 0;
  347. }
  348. static int iwl_set_ucode_api_flags(struct iwl_drv *drv, const u8 *data,
  349. struct iwl_ucode_capabilities *capa)
  350. {
  351. const struct iwl_ucode_api *ucode_api = (void *)data;
  352. u32 api_index = le32_to_cpu(ucode_api->api_index);
  353. if (api_index >= IWL_API_ARRAY_SIZE) {
  354. IWL_ERR(drv, "api_index larger than supported by driver\n");
  355. return -EINVAL;
  356. }
  357. capa->api[api_index] = le32_to_cpu(ucode_api->api_flags);
  358. return 0;
  359. }
  360. static int iwl_set_ucode_capabilities(struct iwl_drv *drv, const u8 *data,
  361. struct iwl_ucode_capabilities *capa)
  362. {
  363. const struct iwl_ucode_capa *ucode_capa = (void *)data;
  364. u32 api_index = le32_to_cpu(ucode_capa->api_index);
  365. if (api_index >= IWL_CAPABILITIES_ARRAY_SIZE) {
  366. IWL_ERR(drv, "api_index larger than supported by driver\n");
  367. return -EINVAL;
  368. }
  369. capa->capa[api_index] = le32_to_cpu(ucode_capa->api_capa);
  370. return 0;
  371. }
  372. static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv,
  373. const struct firmware *ucode_raw,
  374. struct iwl_firmware_pieces *pieces)
  375. {
  376. struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
  377. u32 api_ver, hdr_size, build;
  378. char buildstr[25];
  379. const u8 *src;
  380. drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
  381. api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
  382. switch (api_ver) {
  383. default:
  384. hdr_size = 28;
  385. if (ucode_raw->size < hdr_size) {
  386. IWL_ERR(drv, "File size too small!\n");
  387. return -EINVAL;
  388. }
  389. build = le32_to_cpu(ucode->u.v2.build);
  390. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
  391. le32_to_cpu(ucode->u.v2.inst_size));
  392. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
  393. le32_to_cpu(ucode->u.v2.data_size));
  394. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
  395. le32_to_cpu(ucode->u.v2.init_size));
  396. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
  397. le32_to_cpu(ucode->u.v2.init_data_size));
  398. src = ucode->u.v2.data;
  399. break;
  400. case 0:
  401. case 1:
  402. case 2:
  403. hdr_size = 24;
  404. if (ucode_raw->size < hdr_size) {
  405. IWL_ERR(drv, "File size too small!\n");
  406. return -EINVAL;
  407. }
  408. build = 0;
  409. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
  410. le32_to_cpu(ucode->u.v1.inst_size));
  411. set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
  412. le32_to_cpu(ucode->u.v1.data_size));
  413. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
  414. le32_to_cpu(ucode->u.v1.init_size));
  415. set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
  416. le32_to_cpu(ucode->u.v1.init_data_size));
  417. src = ucode->u.v1.data;
  418. break;
  419. }
  420. if (build)
  421. sprintf(buildstr, " build %u%s", build,
  422. (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  423. ? " (EXP)" : "");
  424. else
  425. buildstr[0] = '\0';
  426. snprintf(drv->fw.fw_version,
  427. sizeof(drv->fw.fw_version),
  428. "%u.%u.%u.%u%s",
  429. IWL_UCODE_MAJOR(drv->fw.ucode_ver),
  430. IWL_UCODE_MINOR(drv->fw.ucode_ver),
  431. IWL_UCODE_API(drv->fw.ucode_ver),
  432. IWL_UCODE_SERIAL(drv->fw.ucode_ver),
  433. buildstr);
  434. /* Verify size of file vs. image size info in file's header */
  435. if (ucode_raw->size != hdr_size +
  436. get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) +
  437. get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) +
  438. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) +
  439. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) {
  440. IWL_ERR(drv,
  441. "uCode file size %d does not match expected size\n",
  442. (int)ucode_raw->size);
  443. return -EINVAL;
  444. }
  445. set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src);
  446. src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST);
  447. set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST,
  448. IWLAGN_RTC_INST_LOWER_BOUND);
  449. set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src);
  450. src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA);
  451. set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA,
  452. IWLAGN_RTC_DATA_LOWER_BOUND);
  453. set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src);
  454. src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST);
  455. set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST,
  456. IWLAGN_RTC_INST_LOWER_BOUND);
  457. set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src);
  458. src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA);
  459. set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA,
  460. IWLAGN_RTC_DATA_LOWER_BOUND);
  461. return 0;
  462. }
  463. static int iwl_parse_tlv_firmware(struct iwl_drv *drv,
  464. const struct firmware *ucode_raw,
  465. struct iwl_firmware_pieces *pieces,
  466. struct iwl_ucode_capabilities *capa)
  467. {
  468. struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
  469. struct iwl_ucode_tlv *tlv;
  470. size_t len = ucode_raw->size;
  471. const u8 *data;
  472. u32 tlv_len;
  473. enum iwl_ucode_tlv_type tlv_type;
  474. const u8 *tlv_data;
  475. char buildstr[25];
  476. u32 build;
  477. int num_of_cpus;
  478. if (len < sizeof(*ucode)) {
  479. IWL_ERR(drv, "uCode has invalid length: %zd\n", len);
  480. return -EINVAL;
  481. }
  482. if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
  483. IWL_ERR(drv, "invalid uCode magic: 0X%x\n",
  484. le32_to_cpu(ucode->magic));
  485. return -EINVAL;
  486. }
  487. drv->fw.ucode_ver = le32_to_cpu(ucode->ver);
  488. build = le32_to_cpu(ucode->build);
  489. if (build)
  490. sprintf(buildstr, " build %u%s", build,
  491. (drv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  492. ? " (EXP)" : "");
  493. else
  494. buildstr[0] = '\0';
  495. snprintf(drv->fw.fw_version,
  496. sizeof(drv->fw.fw_version),
  497. "%u.%u.%u.%u%s",
  498. IWL_UCODE_MAJOR(drv->fw.ucode_ver),
  499. IWL_UCODE_MINOR(drv->fw.ucode_ver),
  500. IWL_UCODE_API(drv->fw.ucode_ver),
  501. IWL_UCODE_SERIAL(drv->fw.ucode_ver),
  502. buildstr);
  503. data = ucode->data;
  504. len -= sizeof(*ucode);
  505. while (len >= sizeof(*tlv)) {
  506. len -= sizeof(*tlv);
  507. tlv = (void *)data;
  508. tlv_len = le32_to_cpu(tlv->length);
  509. tlv_type = le32_to_cpu(tlv->type);
  510. tlv_data = tlv->data;
  511. if (len < tlv_len) {
  512. IWL_ERR(drv, "invalid TLV len: %zd/%u\n",
  513. len, tlv_len);
  514. return -EINVAL;
  515. }
  516. len -= ALIGN(tlv_len, 4);
  517. data += sizeof(*tlv) + ALIGN(tlv_len, 4);
  518. switch (tlv_type) {
  519. case IWL_UCODE_TLV_INST:
  520. set_sec_data(pieces, IWL_UCODE_REGULAR,
  521. IWL_UCODE_SECTION_INST, tlv_data);
  522. set_sec_size(pieces, IWL_UCODE_REGULAR,
  523. IWL_UCODE_SECTION_INST, tlv_len);
  524. set_sec_offset(pieces, IWL_UCODE_REGULAR,
  525. IWL_UCODE_SECTION_INST,
  526. IWLAGN_RTC_INST_LOWER_BOUND);
  527. break;
  528. case IWL_UCODE_TLV_DATA:
  529. set_sec_data(pieces, IWL_UCODE_REGULAR,
  530. IWL_UCODE_SECTION_DATA, tlv_data);
  531. set_sec_size(pieces, IWL_UCODE_REGULAR,
  532. IWL_UCODE_SECTION_DATA, tlv_len);
  533. set_sec_offset(pieces, IWL_UCODE_REGULAR,
  534. IWL_UCODE_SECTION_DATA,
  535. IWLAGN_RTC_DATA_LOWER_BOUND);
  536. break;
  537. case IWL_UCODE_TLV_INIT:
  538. set_sec_data(pieces, IWL_UCODE_INIT,
  539. IWL_UCODE_SECTION_INST, tlv_data);
  540. set_sec_size(pieces, IWL_UCODE_INIT,
  541. IWL_UCODE_SECTION_INST, tlv_len);
  542. set_sec_offset(pieces, IWL_UCODE_INIT,
  543. IWL_UCODE_SECTION_INST,
  544. IWLAGN_RTC_INST_LOWER_BOUND);
  545. break;
  546. case IWL_UCODE_TLV_INIT_DATA:
  547. set_sec_data(pieces, IWL_UCODE_INIT,
  548. IWL_UCODE_SECTION_DATA, tlv_data);
  549. set_sec_size(pieces, IWL_UCODE_INIT,
  550. IWL_UCODE_SECTION_DATA, tlv_len);
  551. set_sec_offset(pieces, IWL_UCODE_INIT,
  552. IWL_UCODE_SECTION_DATA,
  553. IWLAGN_RTC_DATA_LOWER_BOUND);
  554. break;
  555. case IWL_UCODE_TLV_BOOT:
  556. IWL_ERR(drv, "Found unexpected BOOT ucode\n");
  557. break;
  558. case IWL_UCODE_TLV_PROBE_MAX_LEN:
  559. if (tlv_len != sizeof(u32))
  560. goto invalid_tlv_len;
  561. capa->max_probe_length =
  562. le32_to_cpup((__le32 *)tlv_data);
  563. break;
  564. case IWL_UCODE_TLV_PAN:
  565. if (tlv_len)
  566. goto invalid_tlv_len;
  567. capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
  568. break;
  569. case IWL_UCODE_TLV_FLAGS:
  570. /* must be at least one u32 */
  571. if (tlv_len < sizeof(u32))
  572. goto invalid_tlv_len;
  573. /* and a proper number of u32s */
  574. if (tlv_len % sizeof(u32))
  575. goto invalid_tlv_len;
  576. /*
  577. * This driver only reads the first u32 as
  578. * right now no more features are defined,
  579. * if that changes then either the driver
  580. * will not work with the new firmware, or
  581. * it'll not take advantage of new features.
  582. */
  583. capa->flags = le32_to_cpup((__le32 *)tlv_data);
  584. break;
  585. case IWL_UCODE_TLV_API_CHANGES_SET:
  586. if (tlv_len != sizeof(struct iwl_ucode_api))
  587. goto invalid_tlv_len;
  588. if (iwl_set_ucode_api_flags(drv, tlv_data, capa))
  589. goto tlv_error;
  590. break;
  591. case IWL_UCODE_TLV_ENABLED_CAPABILITIES:
  592. if (tlv_len != sizeof(struct iwl_ucode_capa))
  593. goto invalid_tlv_len;
  594. if (iwl_set_ucode_capabilities(drv, tlv_data, capa))
  595. goto tlv_error;
  596. break;
  597. case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
  598. if (tlv_len != sizeof(u32))
  599. goto invalid_tlv_len;
  600. pieces->init_evtlog_ptr =
  601. le32_to_cpup((__le32 *)tlv_data);
  602. break;
  603. case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
  604. if (tlv_len != sizeof(u32))
  605. goto invalid_tlv_len;
  606. pieces->init_evtlog_size =
  607. le32_to_cpup((__le32 *)tlv_data);
  608. break;
  609. case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
  610. if (tlv_len != sizeof(u32))
  611. goto invalid_tlv_len;
  612. pieces->init_errlog_ptr =
  613. le32_to_cpup((__le32 *)tlv_data);
  614. break;
  615. case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
  616. if (tlv_len != sizeof(u32))
  617. goto invalid_tlv_len;
  618. pieces->inst_evtlog_ptr =
  619. le32_to_cpup((__le32 *)tlv_data);
  620. break;
  621. case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
  622. if (tlv_len != sizeof(u32))
  623. goto invalid_tlv_len;
  624. pieces->inst_evtlog_size =
  625. le32_to_cpup((__le32 *)tlv_data);
  626. break;
  627. case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
  628. if (tlv_len != sizeof(u32))
  629. goto invalid_tlv_len;
  630. pieces->inst_errlog_ptr =
  631. le32_to_cpup((__le32 *)tlv_data);
  632. break;
  633. case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
  634. if (tlv_len)
  635. goto invalid_tlv_len;
  636. drv->fw.enhance_sensitivity_table = true;
  637. break;
  638. case IWL_UCODE_TLV_WOWLAN_INST:
  639. set_sec_data(pieces, IWL_UCODE_WOWLAN,
  640. IWL_UCODE_SECTION_INST, tlv_data);
  641. set_sec_size(pieces, IWL_UCODE_WOWLAN,
  642. IWL_UCODE_SECTION_INST, tlv_len);
  643. set_sec_offset(pieces, IWL_UCODE_WOWLAN,
  644. IWL_UCODE_SECTION_INST,
  645. IWLAGN_RTC_INST_LOWER_BOUND);
  646. break;
  647. case IWL_UCODE_TLV_WOWLAN_DATA:
  648. set_sec_data(pieces, IWL_UCODE_WOWLAN,
  649. IWL_UCODE_SECTION_DATA, tlv_data);
  650. set_sec_size(pieces, IWL_UCODE_WOWLAN,
  651. IWL_UCODE_SECTION_DATA, tlv_len);
  652. set_sec_offset(pieces, IWL_UCODE_WOWLAN,
  653. IWL_UCODE_SECTION_DATA,
  654. IWLAGN_RTC_DATA_LOWER_BOUND);
  655. break;
  656. case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
  657. if (tlv_len != sizeof(u32))
  658. goto invalid_tlv_len;
  659. capa->standard_phy_calibration_size =
  660. le32_to_cpup((__le32 *)tlv_data);
  661. break;
  662. case IWL_UCODE_TLV_SEC_RT:
  663. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
  664. tlv_len);
  665. drv->fw.mvm_fw = true;
  666. break;
  667. case IWL_UCODE_TLV_SEC_INIT:
  668. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
  669. tlv_len);
  670. drv->fw.mvm_fw = true;
  671. break;
  672. case IWL_UCODE_TLV_SEC_WOWLAN:
  673. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
  674. tlv_len);
  675. drv->fw.mvm_fw = true;
  676. break;
  677. case IWL_UCODE_TLV_DEF_CALIB:
  678. if (tlv_len != sizeof(struct iwl_tlv_calib_data))
  679. goto invalid_tlv_len;
  680. if (iwl_set_default_calib(drv, tlv_data))
  681. goto tlv_error;
  682. break;
  683. case IWL_UCODE_TLV_PHY_SKU:
  684. if (tlv_len != sizeof(u32))
  685. goto invalid_tlv_len;
  686. drv->fw.phy_config = le32_to_cpup((__le32 *)tlv_data);
  687. drv->fw.valid_tx_ant = (drv->fw.phy_config &
  688. FW_PHY_CFG_TX_CHAIN) >>
  689. FW_PHY_CFG_TX_CHAIN_POS;
  690. drv->fw.valid_rx_ant = (drv->fw.phy_config &
  691. FW_PHY_CFG_RX_CHAIN) >>
  692. FW_PHY_CFG_RX_CHAIN_POS;
  693. break;
  694. case IWL_UCODE_TLV_SECURE_SEC_RT:
  695. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_REGULAR,
  696. tlv_len);
  697. drv->fw.mvm_fw = true;
  698. drv->fw.img[IWL_UCODE_REGULAR].is_secure = true;
  699. break;
  700. case IWL_UCODE_TLV_SECURE_SEC_INIT:
  701. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_INIT,
  702. tlv_len);
  703. drv->fw.mvm_fw = true;
  704. drv->fw.img[IWL_UCODE_INIT].is_secure = true;
  705. break;
  706. case IWL_UCODE_TLV_SECURE_SEC_WOWLAN:
  707. iwl_store_ucode_sec(pieces, tlv_data, IWL_UCODE_WOWLAN,
  708. tlv_len);
  709. drv->fw.mvm_fw = true;
  710. drv->fw.img[IWL_UCODE_WOWLAN].is_secure = true;
  711. break;
  712. case IWL_UCODE_TLV_NUM_OF_CPU:
  713. if (tlv_len != sizeof(u32))
  714. goto invalid_tlv_len;
  715. num_of_cpus =
  716. le32_to_cpup((__le32 *)tlv_data);
  717. if (num_of_cpus == 2) {
  718. drv->fw.img[IWL_UCODE_REGULAR].is_dual_cpus =
  719. true;
  720. drv->fw.img[IWL_UCODE_INIT].is_dual_cpus =
  721. true;
  722. drv->fw.img[IWL_UCODE_WOWLAN].is_dual_cpus =
  723. true;
  724. } else if ((num_of_cpus > 2) || (num_of_cpus < 1)) {
  725. IWL_ERR(drv, "Driver support upto 2 CPUs\n");
  726. return -EINVAL;
  727. }
  728. break;
  729. case IWL_UCODE_TLV_CSCHEME:
  730. if (iwl_store_cscheme(&drv->fw, tlv_data, tlv_len))
  731. goto invalid_tlv_len;
  732. break;
  733. default:
  734. IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type);
  735. break;
  736. }
  737. }
  738. if (len) {
  739. IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len);
  740. iwl_print_hex_dump(drv, IWL_DL_FW, (u8 *)data, len);
  741. return -EINVAL;
  742. }
  743. return 0;
  744. invalid_tlv_len:
  745. IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
  746. tlv_error:
  747. iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len);
  748. return -EINVAL;
  749. }
  750. static int iwl_alloc_ucode(struct iwl_drv *drv,
  751. struct iwl_firmware_pieces *pieces,
  752. enum iwl_ucode_type type)
  753. {
  754. int i;
  755. for (i = 0;
  756. i < IWL_UCODE_SECTION_MAX && get_sec_size(pieces, type, i);
  757. i++)
  758. if (iwl_alloc_fw_desc(drv, &(drv->fw.img[type].sec[i]),
  759. get_sec(pieces, type, i)))
  760. return -ENOMEM;
  761. return 0;
  762. }
  763. static int validate_sec_sizes(struct iwl_drv *drv,
  764. struct iwl_firmware_pieces *pieces,
  765. const struct iwl_cfg *cfg)
  766. {
  767. IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %Zd\n",
  768. get_sec_size(pieces, IWL_UCODE_REGULAR,
  769. IWL_UCODE_SECTION_INST));
  770. IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %Zd\n",
  771. get_sec_size(pieces, IWL_UCODE_REGULAR,
  772. IWL_UCODE_SECTION_DATA));
  773. IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %Zd\n",
  774. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST));
  775. IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %Zd\n",
  776. get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA));
  777. /* Verify that uCode images will fit in card's SRAM. */
  778. if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) >
  779. cfg->max_inst_size) {
  780. IWL_ERR(drv, "uCode instr len %Zd too large to fit in\n",
  781. get_sec_size(pieces, IWL_UCODE_REGULAR,
  782. IWL_UCODE_SECTION_INST));
  783. return -1;
  784. }
  785. if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) >
  786. cfg->max_data_size) {
  787. IWL_ERR(drv, "uCode data len %Zd too large to fit in\n",
  788. get_sec_size(pieces, IWL_UCODE_REGULAR,
  789. IWL_UCODE_SECTION_DATA));
  790. return -1;
  791. }
  792. if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) >
  793. cfg->max_inst_size) {
  794. IWL_ERR(drv, "uCode init instr len %Zd too large to fit in\n",
  795. get_sec_size(pieces, IWL_UCODE_INIT,
  796. IWL_UCODE_SECTION_INST));
  797. return -1;
  798. }
  799. if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) >
  800. cfg->max_data_size) {
  801. IWL_ERR(drv, "uCode init data len %Zd too large to fit in\n",
  802. get_sec_size(pieces, IWL_UCODE_REGULAR,
  803. IWL_UCODE_SECTION_DATA));
  804. return -1;
  805. }
  806. return 0;
  807. }
  808. static struct iwl_op_mode *
  809. _iwl_op_mode_start(struct iwl_drv *drv, struct iwlwifi_opmode_table *op)
  810. {
  811. const struct iwl_op_mode_ops *ops = op->ops;
  812. struct dentry *dbgfs_dir = NULL;
  813. struct iwl_op_mode *op_mode = NULL;
  814. #ifdef CONFIG_IWLWIFI_DEBUGFS
  815. drv->dbgfs_op_mode = debugfs_create_dir(op->name,
  816. drv->dbgfs_drv);
  817. if (!drv->dbgfs_op_mode) {
  818. IWL_ERR(drv,
  819. "failed to create opmode debugfs directory\n");
  820. return op_mode;
  821. }
  822. dbgfs_dir = drv->dbgfs_op_mode;
  823. #endif
  824. op_mode = ops->start(drv->trans, drv->cfg, &drv->fw, dbgfs_dir);
  825. #ifdef CONFIG_IWLWIFI_DEBUGFS
  826. if (!op_mode) {
  827. debugfs_remove_recursive(drv->dbgfs_op_mode);
  828. drv->dbgfs_op_mode = NULL;
  829. }
  830. #endif
  831. return op_mode;
  832. }
  833. static void _iwl_op_mode_stop(struct iwl_drv *drv)
  834. {
  835. /* op_mode can be NULL if its start failed */
  836. if (drv->op_mode) {
  837. iwl_op_mode_stop(drv->op_mode);
  838. drv->op_mode = NULL;
  839. #ifdef CONFIG_IWLWIFI_DEBUGFS
  840. debugfs_remove_recursive(drv->dbgfs_op_mode);
  841. drv->dbgfs_op_mode = NULL;
  842. #endif
  843. }
  844. }
  845. /**
  846. * iwl_req_fw_callback - callback when firmware was loaded
  847. *
  848. * If loaded successfully, copies the firmware into buffers
  849. * for the card to fetch (via DMA).
  850. */
  851. static void iwl_req_fw_callback(const struct firmware *ucode_raw, void *context)
  852. {
  853. struct iwl_drv *drv = context;
  854. struct iwl_fw *fw = &drv->fw;
  855. struct iwl_ucode_header *ucode;
  856. struct iwlwifi_opmode_table *op;
  857. int err;
  858. struct iwl_firmware_pieces pieces;
  859. const unsigned int api_max = drv->cfg->ucode_api_max;
  860. unsigned int api_ok = drv->cfg->ucode_api_ok;
  861. const unsigned int api_min = drv->cfg->ucode_api_min;
  862. u32 api_ver;
  863. int i;
  864. bool load_module = false;
  865. fw->ucode_capa.max_probe_length = IWL_DEFAULT_MAX_PROBE_LENGTH;
  866. fw->ucode_capa.standard_phy_calibration_size =
  867. IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  868. if (!api_ok)
  869. api_ok = api_max;
  870. memset(&pieces, 0, sizeof(pieces));
  871. if (!ucode_raw) {
  872. if (drv->fw_index <= api_ok)
  873. IWL_ERR(drv,
  874. "request for firmware file '%s' failed.\n",
  875. drv->firmware_name);
  876. goto try_again;
  877. }
  878. IWL_DEBUG_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n",
  879. drv->firmware_name, ucode_raw->size);
  880. /* Make sure that we got at least the API version number */
  881. if (ucode_raw->size < 4) {
  882. IWL_ERR(drv, "File size way too small!\n");
  883. goto try_again;
  884. }
  885. /* Data from ucode file: header followed by uCode images */
  886. ucode = (struct iwl_ucode_header *)ucode_raw->data;
  887. if (ucode->ver)
  888. err = iwl_parse_v1_v2_firmware(drv, ucode_raw, &pieces);
  889. else
  890. err = iwl_parse_tlv_firmware(drv, ucode_raw, &pieces,
  891. &fw->ucode_capa);
  892. if (err)
  893. goto try_again;
  894. api_ver = IWL_UCODE_API(drv->fw.ucode_ver);
  895. /*
  896. * api_ver should match the api version forming part of the
  897. * firmware filename ... but we don't check for that and only rely
  898. * on the API version read from firmware header from here on forward
  899. */
  900. /* no api version check required for experimental uCode */
  901. if (drv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
  902. if (api_ver < api_min || api_ver > api_max) {
  903. IWL_ERR(drv,
  904. "Driver unable to support your firmware API. "
  905. "Driver supports v%u, firmware is v%u.\n",
  906. api_max, api_ver);
  907. goto try_again;
  908. }
  909. if (api_ver < api_ok) {
  910. if (api_ok != api_max)
  911. IWL_ERR(drv, "Firmware has old API version, "
  912. "expected v%u through v%u, got v%u.\n",
  913. api_ok, api_max, api_ver);
  914. else
  915. IWL_ERR(drv, "Firmware has old API version, "
  916. "expected v%u, got v%u.\n",
  917. api_max, api_ver);
  918. IWL_ERR(drv, "New firmware can be obtained from "
  919. "http://www.intellinuxwireless.org/.\n");
  920. }
  921. }
  922. /*
  923. * In mvm uCode there is no difference between data and instructions
  924. * sections.
  925. */
  926. if (!fw->mvm_fw && validate_sec_sizes(drv, &pieces, drv->cfg))
  927. goto try_again;
  928. /* Allocate ucode buffers for card's bus-master loading ... */
  929. /* Runtime instructions and 2 copies of data:
  930. * 1) unmodified from disk
  931. * 2) backup cache for save/restore during power-downs */
  932. for (i = 0; i < IWL_UCODE_TYPE_MAX; i++)
  933. if (iwl_alloc_ucode(drv, &pieces, i))
  934. goto out_free_fw;
  935. /* Now that we can no longer fail, copy information */
  936. /*
  937. * The (size - 16) / 12 formula is based on the information recorded
  938. * for each event, which is of mode 1 (including timestamp) for all
  939. * new microcodes that include this information.
  940. */
  941. fw->init_evtlog_ptr = pieces.init_evtlog_ptr;
  942. if (pieces.init_evtlog_size)
  943. fw->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
  944. else
  945. fw->init_evtlog_size =
  946. drv->cfg->base_params->max_event_log_size;
  947. fw->init_errlog_ptr = pieces.init_errlog_ptr;
  948. fw->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
  949. if (pieces.inst_evtlog_size)
  950. fw->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
  951. else
  952. fw->inst_evtlog_size =
  953. drv->cfg->base_params->max_event_log_size;
  954. fw->inst_errlog_ptr = pieces.inst_errlog_ptr;
  955. /*
  956. * figure out the offset of chain noise reset and gain commands
  957. * base on the size of standard phy calibration commands table size
  958. */
  959. if (fw->ucode_capa.standard_phy_calibration_size >
  960. IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
  961. fw->ucode_capa.standard_phy_calibration_size =
  962. IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  963. /* We have our copies now, allow OS release its copies */
  964. release_firmware(ucode_raw);
  965. mutex_lock(&iwlwifi_opmode_table_mtx);
  966. if (fw->mvm_fw)
  967. op = &iwlwifi_opmode_table[MVM_OP_MODE];
  968. else
  969. op = &iwlwifi_opmode_table[DVM_OP_MODE];
  970. IWL_INFO(drv, "loaded firmware version %s op_mode %s\n",
  971. drv->fw.fw_version, op->name);
  972. /* add this device to the list of devices using this op_mode */
  973. list_add_tail(&drv->list, &op->drv);
  974. if (op->ops) {
  975. drv->op_mode = _iwl_op_mode_start(drv, op);
  976. if (!drv->op_mode) {
  977. mutex_unlock(&iwlwifi_opmode_table_mtx);
  978. goto out_unbind;
  979. }
  980. } else {
  981. load_module = true;
  982. }
  983. mutex_unlock(&iwlwifi_opmode_table_mtx);
  984. /*
  985. * Complete the firmware request last so that
  986. * a driver unbind (stop) doesn't run while we
  987. * are doing the start() above.
  988. */
  989. complete(&drv->request_firmware_complete);
  990. /*
  991. * Load the module last so we don't block anything
  992. * else from proceeding if the module fails to load
  993. * or hangs loading.
  994. */
  995. if (load_module) {
  996. err = request_module("%s", op->name);
  997. #ifdef CONFIG_IWLWIFI_OPMODE_MODULAR
  998. if (err)
  999. IWL_ERR(drv,
  1000. "failed to load module %s (error %d), is dynamic loading enabled?\n",
  1001. op->name, err);
  1002. #endif
  1003. }
  1004. return;
  1005. try_again:
  1006. /* try next, if any */
  1007. release_firmware(ucode_raw);
  1008. if (iwl_request_firmware(drv, false))
  1009. goto out_unbind;
  1010. return;
  1011. out_free_fw:
  1012. IWL_ERR(drv, "failed to allocate pci memory\n");
  1013. iwl_dealloc_ucode(drv);
  1014. release_firmware(ucode_raw);
  1015. out_unbind:
  1016. complete(&drv->request_firmware_complete);
  1017. device_release_driver(drv->trans->dev);
  1018. }
  1019. struct iwl_drv *iwl_drv_start(struct iwl_trans *trans,
  1020. const struct iwl_cfg *cfg)
  1021. {
  1022. struct iwl_drv *drv;
  1023. int ret;
  1024. drv = kzalloc(sizeof(*drv), GFP_KERNEL);
  1025. if (!drv) {
  1026. ret = -ENOMEM;
  1027. goto err;
  1028. }
  1029. drv->trans = trans;
  1030. drv->dev = trans->dev;
  1031. drv->cfg = cfg;
  1032. init_completion(&drv->request_firmware_complete);
  1033. INIT_LIST_HEAD(&drv->list);
  1034. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1035. /* Create the device debugfs entries. */
  1036. drv->dbgfs_drv = debugfs_create_dir(dev_name(trans->dev),
  1037. iwl_dbgfs_root);
  1038. if (!drv->dbgfs_drv) {
  1039. IWL_ERR(drv, "failed to create debugfs directory\n");
  1040. ret = -ENOMEM;
  1041. goto err_free_drv;
  1042. }
  1043. /* Create transport layer debugfs dir */
  1044. drv->trans->dbgfs_dir = debugfs_create_dir("trans", drv->dbgfs_drv);
  1045. if (!drv->trans->dbgfs_dir) {
  1046. IWL_ERR(drv, "failed to create transport debugfs directory\n");
  1047. ret = -ENOMEM;
  1048. goto err_free_dbgfs;
  1049. }
  1050. #endif
  1051. ret = iwl_request_firmware(drv, true);
  1052. if (ret) {
  1053. IWL_ERR(trans, "Couldn't request the fw\n");
  1054. goto err_fw;
  1055. }
  1056. return drv;
  1057. err_fw:
  1058. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1059. err_free_dbgfs:
  1060. debugfs_remove_recursive(drv->dbgfs_drv);
  1061. err_free_drv:
  1062. #endif
  1063. kfree(drv);
  1064. err:
  1065. return ERR_PTR(ret);
  1066. }
  1067. void iwl_drv_stop(struct iwl_drv *drv)
  1068. {
  1069. wait_for_completion(&drv->request_firmware_complete);
  1070. _iwl_op_mode_stop(drv);
  1071. iwl_dealloc_ucode(drv);
  1072. mutex_lock(&iwlwifi_opmode_table_mtx);
  1073. /*
  1074. * List is empty (this item wasn't added)
  1075. * when firmware loading failed -- in that
  1076. * case we can't remove it from any list.
  1077. */
  1078. if (!list_empty(&drv->list))
  1079. list_del(&drv->list);
  1080. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1081. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1082. debugfs_remove_recursive(drv->dbgfs_drv);
  1083. #endif
  1084. kfree(drv);
  1085. }
  1086. /* shared module parameters */
  1087. struct iwl_mod_params iwlwifi_mod_params = {
  1088. .restart_fw = true,
  1089. .bt_coex_active = true,
  1090. .power_level = IWL_POWER_INDEX_1,
  1091. .wd_disable = true,
  1092. .uapsd_disable = false,
  1093. /* the rest are 0 by default */
  1094. };
  1095. IWL_EXPORT_SYMBOL(iwlwifi_mod_params);
  1096. int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops)
  1097. {
  1098. int i;
  1099. struct iwl_drv *drv;
  1100. struct iwlwifi_opmode_table *op;
  1101. mutex_lock(&iwlwifi_opmode_table_mtx);
  1102. for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
  1103. op = &iwlwifi_opmode_table[i];
  1104. if (strcmp(op->name, name))
  1105. continue;
  1106. op->ops = ops;
  1107. /* TODO: need to handle exceptional case */
  1108. list_for_each_entry(drv, &op->drv, list)
  1109. drv->op_mode = _iwl_op_mode_start(drv, op);
  1110. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1111. return 0;
  1112. }
  1113. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1114. return -EIO;
  1115. }
  1116. IWL_EXPORT_SYMBOL(iwl_opmode_register);
  1117. void iwl_opmode_deregister(const char *name)
  1118. {
  1119. int i;
  1120. struct iwl_drv *drv;
  1121. mutex_lock(&iwlwifi_opmode_table_mtx);
  1122. for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) {
  1123. if (strcmp(iwlwifi_opmode_table[i].name, name))
  1124. continue;
  1125. iwlwifi_opmode_table[i].ops = NULL;
  1126. /* call the stop routine for all devices */
  1127. list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list)
  1128. _iwl_op_mode_stop(drv);
  1129. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1130. return;
  1131. }
  1132. mutex_unlock(&iwlwifi_opmode_table_mtx);
  1133. }
  1134. IWL_EXPORT_SYMBOL(iwl_opmode_deregister);
  1135. static int __init iwl_drv_init(void)
  1136. {
  1137. int i;
  1138. mutex_init(&iwlwifi_opmode_table_mtx);
  1139. for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++)
  1140. INIT_LIST_HEAD(&iwlwifi_opmode_table[i].drv);
  1141. pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
  1142. pr_info(DRV_COPYRIGHT "\n");
  1143. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1144. /* Create the root of iwlwifi debugfs subsystem. */
  1145. iwl_dbgfs_root = debugfs_create_dir(DRV_NAME, NULL);
  1146. if (!iwl_dbgfs_root)
  1147. return -EFAULT;
  1148. #endif
  1149. return iwl_pci_register_driver();
  1150. }
  1151. module_init(iwl_drv_init);
  1152. static void __exit iwl_drv_exit(void)
  1153. {
  1154. iwl_pci_unregister_driver();
  1155. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1156. debugfs_remove_recursive(iwl_dbgfs_root);
  1157. #endif
  1158. }
  1159. module_exit(iwl_drv_exit);
  1160. #ifdef CONFIG_IWLWIFI_DEBUG
  1161. module_param_named(debug, iwlwifi_mod_params.debug_level, uint,
  1162. S_IRUGO | S_IWUSR);
  1163. MODULE_PARM_DESC(debug, "debug output mask");
  1164. #endif
  1165. module_param_named(swcrypto, iwlwifi_mod_params.sw_crypto, int, S_IRUGO);
  1166. MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
  1167. module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, S_IRUGO);
  1168. MODULE_PARM_DESC(11n_disable,
  1169. "disable 11n functionality, bitmap: 1: full, 2: disable agg TX, 4: disable agg RX, 8 enable agg TX");
  1170. module_param_named(amsdu_size_8K, iwlwifi_mod_params.amsdu_size_8K,
  1171. int, S_IRUGO);
  1172. MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size (default 0)");
  1173. module_param_named(fw_restart, iwlwifi_mod_params.restart_fw, bool, S_IRUGO);
  1174. MODULE_PARM_DESC(fw_restart, "restart firmware in case of error (default true)");
  1175. module_param_named(antenna_coupling, iwlwifi_mod_params.ant_coupling,
  1176. int, S_IRUGO);
  1177. MODULE_PARM_DESC(antenna_coupling,
  1178. "specify antenna coupling in dB (defualt: 0 dB)");
  1179. module_param_named(wd_disable, iwlwifi_mod_params.wd_disable, int, S_IRUGO);
  1180. MODULE_PARM_DESC(wd_disable,
  1181. "Disable stuck queue watchdog timer 0=system default, 1=disable (default: 1)");
  1182. module_param_named(nvm_file, iwlwifi_mod_params.nvm_file, charp, S_IRUGO);
  1183. MODULE_PARM_DESC(nvm_file, "NVM file name");
  1184. module_param_named(uapsd_disable, iwlwifi_mod_params.uapsd_disable,
  1185. bool, S_IRUGO);
  1186. MODULE_PARM_DESC(uapsd_disable, "disable U-APSD functionality (default: N)");
  1187. /*
  1188. * set bt_coex_active to true, uCode will do kill/defer
  1189. * every time the priority line is asserted (BT is sending signals on the
  1190. * priority line in the PCIx).
  1191. * set bt_coex_active to false, uCode will ignore the BT activity and
  1192. * perform the normal operation
  1193. *
  1194. * User might experience transmit issue on some platform due to WiFi/BT
  1195. * co-exist problem. The possible behaviors are:
  1196. * Able to scan and finding all the available AP
  1197. * Not able to associate with any AP
  1198. * On those platforms, WiFi communication can be restored by set
  1199. * "bt_coex_active" module parameter to "false"
  1200. *
  1201. * default: bt_coex_active = true (BT_COEX_ENABLE)
  1202. */
  1203. module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active,
  1204. bool, S_IRUGO);
  1205. MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
  1206. module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, S_IRUGO);
  1207. MODULE_PARM_DESC(led_mode, "0=system default, "
  1208. "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
  1209. module_param_named(power_save, iwlwifi_mod_params.power_save,
  1210. bool, S_IRUGO);
  1211. MODULE_PARM_DESC(power_save,
  1212. "enable WiFi power management (default: disable)");
  1213. module_param_named(power_level, iwlwifi_mod_params.power_level,
  1214. int, S_IRUGO);
  1215. MODULE_PARM_DESC(power_level,
  1216. "default power save level (range from 1 - 5, default: 1)");