iwl-drv.c 53 KB

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