iwl-drv.c 51 KB

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