iwl-drv.c 51 KB

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