fw.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  1. /*
  2. * Intel Wireless WiMAX Connection 2400m
  3. * Firmware uploader
  4. *
  5. *
  6. * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * * Neither the name of Intel Corporation nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. *
  35. * Intel Corporation <linux-wimax@intel.com>
  36. * Yanir Lubetkin <yanirx.lubetkin@intel.com>
  37. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  38. * - Initial implementation
  39. *
  40. *
  41. * THE PROCEDURE
  42. *
  43. * The 2400m and derived devices work in two modes: boot-mode or
  44. * normal mode. In boot mode we can execute only a handful of commands
  45. * targeted at uploading the firmware and launching it.
  46. *
  47. * The 2400m enters boot mode when it is first connected to the
  48. * system, when it crashes and when you ask it to reboot. There are
  49. * two submodes of the boot mode: signed and non-signed. Signed takes
  50. * firmwares signed with a certain private key, non-signed takes any
  51. * firmware. Normal hardware takes only signed firmware.
  52. *
  53. * On boot mode, in USB, we write to the device using the bulk out
  54. * endpoint and read from it in the notification endpoint.
  55. *
  56. * Upon entrance to boot mode, the device sends (preceded with a few
  57. * zero length packets (ZLPs) on the notification endpoint in USB) a
  58. * reboot barker (4 le32 words with the same value). We ack it by
  59. * sending the same barker to the device. The device acks with a
  60. * reboot ack barker (4 le32 words with value I2400M_ACK_BARKER) and
  61. * then is fully booted. At this point we can upload the firmware.
  62. *
  63. * Note that different iterations of the device and EEPROM
  64. * configurations will send different [re]boot barkers; these are
  65. * collected in i2400m_barker_db along with the firmware
  66. * characteristics they require.
  67. *
  68. * This process is accomplished by the i2400m_bootrom_init()
  69. * function. All the device interaction happens through the
  70. * i2400m_bm_cmd() [boot mode command]. Special return values will
  71. * indicate if the device did reset during the process.
  72. *
  73. * After this, we read the MAC address and then (if needed)
  74. * reinitialize the device. We need to read it ahead of time because
  75. * in the future, we might not upload the firmware until userspace
  76. * 'ifconfig up's the device.
  77. *
  78. * We can then upload the firmware file. The file is composed of a BCF
  79. * header (basic data, keys and signatures) and a list of write
  80. * commands and payloads. Optionally more BCF headers might follow the
  81. * main payload. We first upload the header [i2400m_dnload_init()] and
  82. * then pass the commands and payloads verbatim to the i2400m_bm_cmd()
  83. * function [i2400m_dnload_bcf()]. Then we tell the device to jump to
  84. * the new firmware [i2400m_dnload_finalize()].
  85. *
  86. * Once firmware is uploaded, we are good to go :)
  87. *
  88. * When we don't know in which mode we are, we first try by sending a
  89. * warm reset request that will take us to boot-mode. If we time out
  90. * waiting for a reboot barker, that means maybe we are already in
  91. * boot mode, so we send a reboot barker.
  92. *
  93. * COMMAND EXECUTION
  94. *
  95. * This code (and process) is single threaded; for executing commands,
  96. * we post a URB to the notification endpoint, post the command, wait
  97. * for data on the notification buffer. We don't need to worry about
  98. * others as we know we are the only ones in there.
  99. *
  100. * BACKEND IMPLEMENTATION
  101. *
  102. * This code is bus-generic; the bus-specific driver provides back end
  103. * implementations to send a boot mode command to the device and to
  104. * read an acknolwedgement from it (or an asynchronous notification)
  105. * from it.
  106. *
  107. * FIRMWARE LOADING
  108. *
  109. * Note that in some cases, we can't just load a firmware file (for
  110. * example, when resuming). For that, we might cache the firmware
  111. * file. Thus, when doing the bootstrap, if there is a cache firmware
  112. * file, it is used; if not, loading from disk is attempted.
  113. *
  114. * ROADMAP
  115. *
  116. * i2400m_barker_db_init Called by i2400m_driver_init()
  117. * i2400m_barker_db_add
  118. *
  119. * i2400m_barker_db_exit Called by i2400m_driver_exit()
  120. *
  121. * i2400m_dev_bootstrap Called by __i2400m_dev_start()
  122. * request_firmware
  123. * i2400m_fw_bootstrap
  124. * i2400m_fw_check
  125. * i2400m_fw_hdr_check
  126. * i2400m_fw_dnload
  127. * release_firmware
  128. *
  129. * i2400m_fw_dnload
  130. * i2400m_bootrom_init
  131. * i2400m_bm_cmd
  132. * i2400m_reset
  133. * i2400m_dnload_init
  134. * i2400m_dnload_init_signed
  135. * i2400m_dnload_init_nonsigned
  136. * i2400m_download_chunk
  137. * i2400m_bm_cmd
  138. * i2400m_dnload_bcf
  139. * i2400m_bm_cmd
  140. * i2400m_dnload_finalize
  141. * i2400m_bm_cmd
  142. *
  143. * i2400m_bm_cmd
  144. * i2400m->bus_bm_cmd_send()
  145. * i2400m->bus_bm_wait_for_ack
  146. * __i2400m_bm_ack_verify
  147. * i2400m_is_boot_barker
  148. *
  149. * i2400m_bm_cmd_prepare Used by bus-drivers to prep
  150. * commands before sending
  151. *
  152. * i2400m_pm_notifier Called on Power Management events
  153. * i2400m_fw_cache
  154. * i2400m_fw_uncache
  155. */
  156. #include <linux/firmware.h>
  157. #include <linux/sched.h>
  158. #include <linux/slab.h>
  159. #include <linux/usb.h>
  160. #include <linux/export.h>
  161. #include "i2400m.h"
  162. #define D_SUBMODULE fw
  163. #include "debug-levels.h"
  164. static const __le32 i2400m_ACK_BARKER[4] = {
  165. cpu_to_le32(I2400M_ACK_BARKER),
  166. cpu_to_le32(I2400M_ACK_BARKER),
  167. cpu_to_le32(I2400M_ACK_BARKER),
  168. cpu_to_le32(I2400M_ACK_BARKER)
  169. };
  170. /**
  171. * Prepare a boot-mode command for delivery
  172. *
  173. * @cmd: pointer to bootrom header to prepare
  174. *
  175. * Computes checksum if so needed. After calling this function, DO NOT
  176. * modify the command or header as the checksum won't work anymore.
  177. *
  178. * We do it from here because some times we cannot do it in the
  179. * original context the command was sent (it is a const), so when we
  180. * copy it to our staging buffer, we add the checksum there.
  181. */
  182. void i2400m_bm_cmd_prepare(struct i2400m_bootrom_header *cmd)
  183. {
  184. if (i2400m_brh_get_use_checksum(cmd)) {
  185. int i;
  186. u32 checksum = 0;
  187. const u32 *checksum_ptr = (void *) cmd->payload;
  188. for (i = 0; i < cmd->data_size / 4; i++)
  189. checksum += cpu_to_le32(*checksum_ptr++);
  190. checksum += cmd->command + cmd->target_addr + cmd->data_size;
  191. cmd->block_checksum = cpu_to_le32(checksum);
  192. }
  193. }
  194. EXPORT_SYMBOL_GPL(i2400m_bm_cmd_prepare);
  195. /*
  196. * Database of known barkers.
  197. *
  198. * A barker is what the device sends indicating he is ready to be
  199. * bootloaded. Different versions of the device will send different
  200. * barkers. Depending on the barker, it might mean the device wants
  201. * some kind of firmware or the other.
  202. */
  203. static struct i2400m_barker_db {
  204. __le32 data[4];
  205. } *i2400m_barker_db;
  206. static size_t i2400m_barker_db_used, i2400m_barker_db_size;
  207. static
  208. int i2400m_zrealloc_2x(void **ptr, size_t *_count, size_t el_size,
  209. gfp_t gfp_flags)
  210. {
  211. size_t old_count = *_count,
  212. new_count = old_count ? 2 * old_count : 2,
  213. old_size = el_size * old_count,
  214. new_size = el_size * new_count;
  215. void *nptr = krealloc(*ptr, new_size, gfp_flags);
  216. if (nptr) {
  217. /* zero the other half or the whole thing if old_count
  218. * was zero */
  219. if (old_size == 0)
  220. memset(nptr, 0, new_size);
  221. else
  222. memset(nptr + old_size, 0, old_size);
  223. *_count = new_count;
  224. *ptr = nptr;
  225. return 0;
  226. } else
  227. return -ENOMEM;
  228. }
  229. /*
  230. * Add a barker to the database
  231. *
  232. * This cannot used outside of this module and only at at module_init
  233. * time. This is to avoid the need to do locking.
  234. */
  235. static
  236. int i2400m_barker_db_add(u32 barker_id)
  237. {
  238. int result;
  239. struct i2400m_barker_db *barker;
  240. if (i2400m_barker_db_used >= i2400m_barker_db_size) {
  241. result = i2400m_zrealloc_2x(
  242. (void **) &i2400m_barker_db, &i2400m_barker_db_size,
  243. sizeof(i2400m_barker_db[0]), GFP_KERNEL);
  244. if (result < 0)
  245. return result;
  246. }
  247. barker = i2400m_barker_db + i2400m_barker_db_used++;
  248. barker->data[0] = le32_to_cpu(barker_id);
  249. barker->data[1] = le32_to_cpu(barker_id);
  250. barker->data[2] = le32_to_cpu(barker_id);
  251. barker->data[3] = le32_to_cpu(barker_id);
  252. return 0;
  253. }
  254. void i2400m_barker_db_exit(void)
  255. {
  256. kfree(i2400m_barker_db);
  257. i2400m_barker_db = NULL;
  258. i2400m_barker_db_size = 0;
  259. i2400m_barker_db_used = 0;
  260. }
  261. /*
  262. * Helper function to add all the known stable barkers to the barker
  263. * database.
  264. */
  265. static
  266. int i2400m_barker_db_known_barkers(void)
  267. {
  268. int result;
  269. result = i2400m_barker_db_add(I2400M_NBOOT_BARKER);
  270. if (result < 0)
  271. goto error_add;
  272. result = i2400m_barker_db_add(I2400M_SBOOT_BARKER);
  273. if (result < 0)
  274. goto error_add;
  275. result = i2400m_barker_db_add(I2400M_SBOOT_BARKER_6050);
  276. if (result < 0)
  277. goto error_add;
  278. error_add:
  279. return result;
  280. }
  281. /*
  282. * Initialize the barker database
  283. *
  284. * This can only be used from the module_init function for this
  285. * module; this is to avoid the need to do locking.
  286. *
  287. * @options: command line argument with extra barkers to
  288. * recognize. This is a comma-separated list of 32-bit hex
  289. * numbers. They are appended to the existing list. Setting 0
  290. * cleans the existing list and starts a new one.
  291. */
  292. int i2400m_barker_db_init(const char *_options)
  293. {
  294. int result;
  295. char *options = NULL, *options_orig, *token;
  296. i2400m_barker_db = NULL;
  297. i2400m_barker_db_size = 0;
  298. i2400m_barker_db_used = 0;
  299. result = i2400m_barker_db_known_barkers();
  300. if (result < 0)
  301. goto error_add;
  302. /* parse command line options from i2400m.barkers */
  303. if (_options != NULL) {
  304. unsigned barker;
  305. options_orig = kstrdup(_options, GFP_KERNEL);
  306. if (options_orig == NULL) {
  307. result = -ENOMEM;
  308. goto error_parse;
  309. }
  310. options = options_orig;
  311. while ((token = strsep(&options, ",")) != NULL) {
  312. if (*token == '\0') /* eat joint commas */
  313. continue;
  314. if (sscanf(token, "%x", &barker) != 1
  315. || barker > 0xffffffff) {
  316. printk(KERN_ERR "%s: can't recognize "
  317. "i2400m.barkers value '%s' as "
  318. "a 32-bit number\n",
  319. __func__, token);
  320. result = -EINVAL;
  321. goto error_parse;
  322. }
  323. if (barker == 0) {
  324. /* clean list and start new */
  325. i2400m_barker_db_exit();
  326. continue;
  327. }
  328. result = i2400m_barker_db_add(barker);
  329. if (result < 0)
  330. goto error_parse_add;
  331. }
  332. kfree(options_orig);
  333. }
  334. return 0;
  335. error_parse_add:
  336. error_parse:
  337. kfree(options_orig);
  338. error_add:
  339. kfree(i2400m_barker_db);
  340. return result;
  341. }
  342. /*
  343. * Recognize a boot barker
  344. *
  345. * @buf: buffer where the boot barker.
  346. * @buf_size: size of the buffer (has to be 16 bytes). It is passed
  347. * here so the function can check it for the caller.
  348. *
  349. * Note that as a side effect, upon identifying the obtained boot
  350. * barker, this function will set i2400m->barker to point to the right
  351. * barker database entry. Subsequent calls to the function will result
  352. * in verifying that the same type of boot barker is returned when the
  353. * device [re]boots (as long as the same device instance is used).
  354. *
  355. * Return: 0 if @buf matches a known boot barker. -ENOENT if the
  356. * buffer in @buf doesn't match any boot barker in the database or
  357. * -EILSEQ if the buffer doesn't have the right size.
  358. */
  359. int i2400m_is_boot_barker(struct i2400m *i2400m,
  360. const void *buf, size_t buf_size)
  361. {
  362. int result;
  363. struct device *dev = i2400m_dev(i2400m);
  364. struct i2400m_barker_db *barker;
  365. int i;
  366. result = -ENOENT;
  367. if (buf_size != sizeof(i2400m_barker_db[i].data))
  368. return result;
  369. /* Short circuit if we have already discovered the barker
  370. * associated with the device. */
  371. if (i2400m->barker &&
  372. !memcmp(buf, i2400m->barker, sizeof(i2400m->barker->data)))
  373. return 0;
  374. for (i = 0; i < i2400m_barker_db_used; i++) {
  375. barker = &i2400m_barker_db[i];
  376. BUILD_BUG_ON(sizeof(barker->data) != 16);
  377. if (memcmp(buf, barker->data, sizeof(barker->data)))
  378. continue;
  379. if (i2400m->barker == NULL) {
  380. i2400m->barker = barker;
  381. d_printf(1, dev, "boot barker set to #%u/%08x\n",
  382. i, le32_to_cpu(barker->data[0]));
  383. if (barker->data[0] == le32_to_cpu(I2400M_NBOOT_BARKER))
  384. i2400m->sboot = 0;
  385. else
  386. i2400m->sboot = 1;
  387. } else if (i2400m->barker != barker) {
  388. dev_err(dev, "HW inconsistency: device "
  389. "reports a different boot barker "
  390. "than set (from %08x to %08x)\n",
  391. le32_to_cpu(i2400m->barker->data[0]),
  392. le32_to_cpu(barker->data[0]));
  393. result = -EIO;
  394. } else
  395. d_printf(2, dev, "boot barker confirmed #%u/%08x\n",
  396. i, le32_to_cpu(barker->data[0]));
  397. result = 0;
  398. break;
  399. }
  400. return result;
  401. }
  402. EXPORT_SYMBOL_GPL(i2400m_is_boot_barker);
  403. /*
  404. * Verify the ack data received
  405. *
  406. * Given a reply to a boot mode command, chew it and verify everything
  407. * is ok.
  408. *
  409. * @opcode: opcode which generated this ack. For error messages.
  410. * @ack: pointer to ack data we received
  411. * @ack_size: size of that data buffer
  412. * @flags: I2400M_BM_CMD_* flags we called the command with.
  413. *
  414. * Way too long function -- maybe it should be further split
  415. */
  416. static
  417. ssize_t __i2400m_bm_ack_verify(struct i2400m *i2400m, int opcode,
  418. struct i2400m_bootrom_header *ack,
  419. size_t ack_size, int flags)
  420. {
  421. ssize_t result = -ENOMEM;
  422. struct device *dev = i2400m_dev(i2400m);
  423. d_fnstart(8, dev, "(i2400m %p opcode %d ack %p size %zu)\n",
  424. i2400m, opcode, ack, ack_size);
  425. if (ack_size < sizeof(*ack)) {
  426. result = -EIO;
  427. dev_err(dev, "boot-mode cmd %d: HW BUG? notification didn't "
  428. "return enough data (%zu bytes vs %zu expected)\n",
  429. opcode, ack_size, sizeof(*ack));
  430. goto error_ack_short;
  431. }
  432. result = i2400m_is_boot_barker(i2400m, ack, ack_size);
  433. if (result >= 0) {
  434. result = -ERESTARTSYS;
  435. d_printf(6, dev, "boot-mode cmd %d: HW boot barker\n", opcode);
  436. goto error_reboot;
  437. }
  438. if (ack_size == sizeof(i2400m_ACK_BARKER)
  439. && memcmp(ack, i2400m_ACK_BARKER, sizeof(*ack)) == 0) {
  440. result = -EISCONN;
  441. d_printf(3, dev, "boot-mode cmd %d: HW reboot ack barker\n",
  442. opcode);
  443. goto error_reboot_ack;
  444. }
  445. result = 0;
  446. if (flags & I2400M_BM_CMD_RAW)
  447. goto out_raw;
  448. ack->data_size = le32_to_cpu(ack->data_size);
  449. ack->target_addr = le32_to_cpu(ack->target_addr);
  450. ack->block_checksum = le32_to_cpu(ack->block_checksum);
  451. d_printf(5, dev, "boot-mode cmd %d: notification for opcode %u "
  452. "response %u csum %u rr %u da %u\n",
  453. opcode, i2400m_brh_get_opcode(ack),
  454. i2400m_brh_get_response(ack),
  455. i2400m_brh_get_use_checksum(ack),
  456. i2400m_brh_get_response_required(ack),
  457. i2400m_brh_get_direct_access(ack));
  458. result = -EIO;
  459. if (i2400m_brh_get_signature(ack) != 0xcbbc) {
  460. dev_err(dev, "boot-mode cmd %d: HW BUG? wrong signature "
  461. "0x%04x\n", opcode, i2400m_brh_get_signature(ack));
  462. goto error_ack_signature;
  463. }
  464. if (opcode != -1 && opcode != i2400m_brh_get_opcode(ack)) {
  465. dev_err(dev, "boot-mode cmd %d: HW BUG? "
  466. "received response for opcode %u, expected %u\n",
  467. opcode, i2400m_brh_get_opcode(ack), opcode);
  468. goto error_ack_opcode;
  469. }
  470. if (i2400m_brh_get_response(ack) != 0) { /* failed? */
  471. dev_err(dev, "boot-mode cmd %d: error; hw response %u\n",
  472. opcode, i2400m_brh_get_response(ack));
  473. goto error_ack_failed;
  474. }
  475. if (ack_size < ack->data_size + sizeof(*ack)) {
  476. dev_err(dev, "boot-mode cmd %d: SW BUG "
  477. "driver provided only %zu bytes for %zu bytes "
  478. "of data\n", opcode, ack_size,
  479. (size_t) le32_to_cpu(ack->data_size) + sizeof(*ack));
  480. goto error_ack_short_buffer;
  481. }
  482. result = ack_size;
  483. /* Don't you love this stack of empty targets? Well, I don't
  484. * either, but it helps track exactly who comes in here and
  485. * why :) */
  486. error_ack_short_buffer:
  487. error_ack_failed:
  488. error_ack_opcode:
  489. error_ack_signature:
  490. out_raw:
  491. error_reboot_ack:
  492. error_reboot:
  493. error_ack_short:
  494. d_fnend(8, dev, "(i2400m %p opcode %d ack %p size %zu) = %d\n",
  495. i2400m, opcode, ack, ack_size, (int) result);
  496. return result;
  497. }
  498. /**
  499. * i2400m_bm_cmd - Execute a boot mode command
  500. *
  501. * @cmd: buffer containing the command data (pointing at the header).
  502. * This data can be ANYWHERE (for USB, we will copy it to an
  503. * specific buffer). Make sure everything is in proper little
  504. * endian.
  505. *
  506. * A raw buffer can be also sent, just cast it and set flags to
  507. * I2400M_BM_CMD_RAW.
  508. *
  509. * This function will generate a checksum for you if the
  510. * checksum bit in the command is set (unless I2400M_BM_CMD_RAW
  511. * is set).
  512. *
  513. * You can use the i2400m->bm_cmd_buf to stage your commands and
  514. * send them.
  515. *
  516. * If NULL, no command is sent (we just wait for an ack).
  517. *
  518. * @cmd_size: size of the command. Will be auto padded to the
  519. * bus-specific drivers padding requirements.
  520. *
  521. * @ack: buffer where to place the acknowledgement. If it is a regular
  522. * command response, all fields will be returned with the right,
  523. * native endianess.
  524. *
  525. * You *cannot* use i2400m->bm_ack_buf for this buffer.
  526. *
  527. * @ack_size: size of @ack, 16 aligned; you need to provide at least
  528. * sizeof(*ack) bytes and then enough to contain the return data
  529. * from the command
  530. *
  531. * @flags: see I2400M_BM_CMD_* above.
  532. *
  533. * @returns: bytes received by the notification; if < 0, an errno code
  534. * denoting an error or:
  535. *
  536. * -ERESTARTSYS The device has rebooted
  537. *
  538. * Executes a boot-mode command and waits for a response, doing basic
  539. * validation on it; if a zero length response is received, it retries
  540. * waiting for a response until a non-zero one is received (timing out
  541. * after %I2400M_BOOT_RETRIES retries).
  542. */
  543. static
  544. ssize_t i2400m_bm_cmd(struct i2400m *i2400m,
  545. const struct i2400m_bootrom_header *cmd, size_t cmd_size,
  546. struct i2400m_bootrom_header *ack, size_t ack_size,
  547. int flags)
  548. {
  549. ssize_t result = -ENOMEM, rx_bytes;
  550. struct device *dev = i2400m_dev(i2400m);
  551. int opcode = cmd == NULL ? -1 : i2400m_brh_get_opcode(cmd);
  552. d_fnstart(6, dev, "(i2400m %p cmd %p size %zu ack %p size %zu)\n",
  553. i2400m, cmd, cmd_size, ack, ack_size);
  554. BUG_ON(ack_size < sizeof(*ack));
  555. BUG_ON(i2400m->boot_mode == 0);
  556. if (cmd != NULL) { /* send the command */
  557. result = i2400m->bus_bm_cmd_send(i2400m, cmd, cmd_size, flags);
  558. if (result < 0)
  559. goto error_cmd_send;
  560. if ((flags & I2400M_BM_CMD_RAW) == 0)
  561. d_printf(5, dev,
  562. "boot-mode cmd %d csum %u rr %u da %u: "
  563. "addr 0x%04x size %u block csum 0x%04x\n",
  564. opcode, i2400m_brh_get_use_checksum(cmd),
  565. i2400m_brh_get_response_required(cmd),
  566. i2400m_brh_get_direct_access(cmd),
  567. cmd->target_addr, cmd->data_size,
  568. cmd->block_checksum);
  569. }
  570. result = i2400m->bus_bm_wait_for_ack(i2400m, ack, ack_size);
  571. if (result < 0) {
  572. dev_err(dev, "boot-mode cmd %d: error waiting for an ack: %d\n",
  573. opcode, (int) result); /* bah, %zd doesn't work */
  574. goto error_wait_for_ack;
  575. }
  576. rx_bytes = result;
  577. /* verify the ack and read more if necessary [result is the
  578. * final amount of bytes we get in the ack] */
  579. result = __i2400m_bm_ack_verify(i2400m, opcode, ack, ack_size, flags);
  580. if (result < 0)
  581. goto error_bad_ack;
  582. /* Don't you love this stack of empty targets? Well, I don't
  583. * either, but it helps track exactly who comes in here and
  584. * why :) */
  585. result = rx_bytes;
  586. error_bad_ack:
  587. error_wait_for_ack:
  588. error_cmd_send:
  589. d_fnend(6, dev, "(i2400m %p cmd %p size %zu ack %p size %zu) = %d\n",
  590. i2400m, cmd, cmd_size, ack, ack_size, (int) result);
  591. return result;
  592. }
  593. /**
  594. * i2400m_download_chunk - write a single chunk of data to the device's memory
  595. *
  596. * @i2400m: device descriptor
  597. * @buf: the buffer to write
  598. * @buf_len: length of the buffer to write
  599. * @addr: address in the device memory space
  600. * @direct: bootrom write mode
  601. * @do_csum: should a checksum validation be performed
  602. */
  603. static int i2400m_download_chunk(struct i2400m *i2400m, const void *chunk,
  604. size_t __chunk_len, unsigned long addr,
  605. unsigned int direct, unsigned int do_csum)
  606. {
  607. int ret;
  608. size_t chunk_len = ALIGN(__chunk_len, I2400M_PL_ALIGN);
  609. struct device *dev = i2400m_dev(i2400m);
  610. struct {
  611. struct i2400m_bootrom_header cmd;
  612. u8 cmd_payload[];
  613. } __packed *buf;
  614. struct i2400m_bootrom_header ack;
  615. d_fnstart(5, dev, "(i2400m %p chunk %p __chunk_len %zu addr 0x%08lx "
  616. "direct %u do_csum %u)\n", i2400m, chunk, __chunk_len,
  617. addr, direct, do_csum);
  618. buf = i2400m->bm_cmd_buf;
  619. memcpy(buf->cmd_payload, chunk, __chunk_len);
  620. memset(buf->cmd_payload + __chunk_len, 0xad, chunk_len - __chunk_len);
  621. buf->cmd.command = i2400m_brh_command(I2400M_BRH_WRITE,
  622. __chunk_len & 0x3 ? 0 : do_csum,
  623. __chunk_len & 0xf ? 0 : direct);
  624. buf->cmd.target_addr = cpu_to_le32(addr);
  625. buf->cmd.data_size = cpu_to_le32(__chunk_len);
  626. ret = i2400m_bm_cmd(i2400m, &buf->cmd, sizeof(buf->cmd) + chunk_len,
  627. &ack, sizeof(ack), 0);
  628. if (ret >= 0)
  629. ret = 0;
  630. d_fnend(5, dev, "(i2400m %p chunk %p __chunk_len %zu addr 0x%08lx "
  631. "direct %u do_csum %u) = %d\n", i2400m, chunk, __chunk_len,
  632. addr, direct, do_csum, ret);
  633. return ret;
  634. }
  635. /*
  636. * Download a BCF file's sections to the device
  637. *
  638. * @i2400m: device descriptor
  639. * @bcf: pointer to firmware data (first header followed by the
  640. * payloads). Assumed verified and consistent.
  641. * @bcf_len: length (in bytes) of the @bcf buffer.
  642. *
  643. * Returns: < 0 errno code on error or the offset to the jump instruction.
  644. *
  645. * Given a BCF file, downloads each section (a command and a payload)
  646. * to the device's address space. Actually, it just executes each
  647. * command i the BCF file.
  648. *
  649. * The section size has to be aligned to 4 bytes AND the padding has
  650. * to be taken from the firmware file, as the signature takes it into
  651. * account.
  652. */
  653. static
  654. ssize_t i2400m_dnload_bcf(struct i2400m *i2400m,
  655. const struct i2400m_bcf_hdr *bcf, size_t bcf_len)
  656. {
  657. ssize_t ret;
  658. struct device *dev = i2400m_dev(i2400m);
  659. size_t offset, /* iterator offset */
  660. data_size, /* Size of the data payload */
  661. section_size, /* Size of the whole section (cmd + payload) */
  662. section = 1;
  663. const struct i2400m_bootrom_header *bh;
  664. struct i2400m_bootrom_header ack;
  665. d_fnstart(3, dev, "(i2400m %p bcf %p bcf_len %zu)\n",
  666. i2400m, bcf, bcf_len);
  667. /* Iterate over the command blocks in the BCF file that start
  668. * after the header */
  669. offset = le32_to_cpu(bcf->header_len) * sizeof(u32);
  670. while (1) { /* start sending the file */
  671. bh = (void *) bcf + offset;
  672. data_size = le32_to_cpu(bh->data_size);
  673. section_size = ALIGN(sizeof(*bh) + data_size, 4);
  674. d_printf(7, dev,
  675. "downloading section #%zu (@%zu %zu B) to 0x%08x\n",
  676. section, offset, sizeof(*bh) + data_size,
  677. le32_to_cpu(bh->target_addr));
  678. /*
  679. * We look for JUMP cmd from the bootmode header,
  680. * either I2400M_BRH_SIGNED_JUMP for secure boot
  681. * or I2400M_BRH_JUMP for unsecure boot, the last chunk
  682. * should be the bootmode header with JUMP cmd.
  683. */
  684. if (i2400m_brh_get_opcode(bh) == I2400M_BRH_SIGNED_JUMP ||
  685. i2400m_brh_get_opcode(bh) == I2400M_BRH_JUMP) {
  686. d_printf(5, dev, "jump found @%zu\n", offset);
  687. break;
  688. }
  689. if (offset + section_size > bcf_len) {
  690. dev_err(dev, "fw %s: bad section #%zu, "
  691. "end (@%zu) beyond EOF (@%zu)\n",
  692. i2400m->fw_name, section,
  693. offset + section_size, bcf_len);
  694. ret = -EINVAL;
  695. goto error_section_beyond_eof;
  696. }
  697. __i2400m_msleep(20);
  698. ret = i2400m_bm_cmd(i2400m, bh, section_size,
  699. &ack, sizeof(ack), I2400M_BM_CMD_RAW);
  700. if (ret < 0) {
  701. dev_err(dev, "fw %s: section #%zu (@%zu %zu B) "
  702. "failed %d\n", i2400m->fw_name, section,
  703. offset, sizeof(*bh) + data_size, (int) ret);
  704. goto error_send;
  705. }
  706. offset += section_size;
  707. section++;
  708. }
  709. ret = offset;
  710. error_section_beyond_eof:
  711. error_send:
  712. d_fnend(3, dev, "(i2400m %p bcf %p bcf_len %zu) = %d\n",
  713. i2400m, bcf, bcf_len, (int) ret);
  714. return ret;
  715. }
  716. /*
  717. * Indicate if the device emitted a reboot barker that indicates
  718. * "signed boot"
  719. */
  720. static
  721. unsigned i2400m_boot_is_signed(struct i2400m *i2400m)
  722. {
  723. return likely(i2400m->sboot);
  724. }
  725. /*
  726. * Do the final steps of uploading firmware
  727. *
  728. * @bcf_hdr: BCF header we are actually using
  729. * @bcf: pointer to the firmware image (which matches the first header
  730. * that is followed by the actual payloads).
  731. * @offset: [byte] offset into @bcf for the command we need to send.
  732. *
  733. * Depending on the boot mode (signed vs non-signed), different
  734. * actions need to be taken.
  735. */
  736. static
  737. int i2400m_dnload_finalize(struct i2400m *i2400m,
  738. const struct i2400m_bcf_hdr *bcf_hdr,
  739. const struct i2400m_bcf_hdr *bcf, size_t offset)
  740. {
  741. int ret = 0;
  742. struct device *dev = i2400m_dev(i2400m);
  743. struct i2400m_bootrom_header *cmd, ack;
  744. struct {
  745. struct i2400m_bootrom_header cmd;
  746. u8 cmd_pl[0];
  747. } __packed *cmd_buf;
  748. size_t signature_block_offset, signature_block_size;
  749. d_fnstart(3, dev, "offset %zu\n", offset);
  750. cmd = (void *) bcf + offset;
  751. if (i2400m_boot_is_signed(i2400m) == 0) {
  752. struct i2400m_bootrom_header jump_ack;
  753. d_printf(1, dev, "unsecure boot, jumping to 0x%08x\n",
  754. le32_to_cpu(cmd->target_addr));
  755. cmd_buf = i2400m->bm_cmd_buf;
  756. memcpy(&cmd_buf->cmd, cmd, sizeof(*cmd));
  757. cmd = &cmd_buf->cmd;
  758. /* now cmd points to the actual bootrom_header in cmd_buf */
  759. i2400m_brh_set_opcode(cmd, I2400M_BRH_JUMP);
  760. cmd->data_size = 0;
  761. ret = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
  762. &jump_ack, sizeof(jump_ack), 0);
  763. } else {
  764. d_printf(1, dev, "secure boot, jumping to 0x%08x\n",
  765. le32_to_cpu(cmd->target_addr));
  766. cmd_buf = i2400m->bm_cmd_buf;
  767. memcpy(&cmd_buf->cmd, cmd, sizeof(*cmd));
  768. signature_block_offset =
  769. sizeof(*bcf_hdr)
  770. + le32_to_cpu(bcf_hdr->key_size) * sizeof(u32)
  771. + le32_to_cpu(bcf_hdr->exponent_size) * sizeof(u32);
  772. signature_block_size =
  773. le32_to_cpu(bcf_hdr->modulus_size) * sizeof(u32);
  774. memcpy(cmd_buf->cmd_pl,
  775. (void *) bcf_hdr + signature_block_offset,
  776. signature_block_size);
  777. ret = i2400m_bm_cmd(i2400m, &cmd_buf->cmd,
  778. sizeof(cmd_buf->cmd) + signature_block_size,
  779. &ack, sizeof(ack), I2400M_BM_CMD_RAW);
  780. }
  781. d_fnend(3, dev, "returning %d\n", ret);
  782. return ret;
  783. }
  784. /**
  785. * i2400m_bootrom_init - Reboots a powered device into boot mode
  786. *
  787. * @i2400m: device descriptor
  788. * @flags:
  789. * I2400M_BRI_SOFT: a reboot barker has been seen
  790. * already, so don't wait for it.
  791. *
  792. * I2400M_BRI_NO_REBOOT: Don't send a reboot command, but wait
  793. * for a reboot barker notification. This is a one shot; if
  794. * the state machine needs to send a reboot command it will.
  795. *
  796. * Returns:
  797. *
  798. * < 0 errno code on error, 0 if ok.
  799. *
  800. * Description:
  801. *
  802. * Tries hard enough to put the device in boot-mode. There are two
  803. * main phases to this:
  804. *
  805. * a. (1) send a reboot command and (2) get a reboot barker
  806. *
  807. * b. (1) echo/ack the reboot sending the reboot barker back and (2)
  808. * getting an ack barker in return
  809. *
  810. * We want to skip (a) in some cases [soft]. The state machine is
  811. * horrible, but it is basically: on each phase, send what has to be
  812. * sent (if any), wait for the answer and act on the answer. We might
  813. * have to backtrack and retry, so we keep a max tries counter for
  814. * that.
  815. *
  816. * It sucks because we don't know ahead of time which is going to be
  817. * the reboot barker (the device might send different ones depending
  818. * on its EEPROM config) and once the device reboots and waits for the
  819. * echo/ack reboot barker being sent back, it doesn't understand
  820. * anything else. So we can be left at the point where we don't know
  821. * what to send to it -- cold reset and bus reset seem to have little
  822. * effect. So the function iterates (in this case) through all the
  823. * known barkers and tries them all until an ACK is
  824. * received. Otherwise, it gives up.
  825. *
  826. * If we get a timeout after sending a warm reset, we do it again.
  827. */
  828. int i2400m_bootrom_init(struct i2400m *i2400m, enum i2400m_bri flags)
  829. {
  830. int result;
  831. struct device *dev = i2400m_dev(i2400m);
  832. struct i2400m_bootrom_header *cmd;
  833. struct i2400m_bootrom_header ack;
  834. int count = i2400m->bus_bm_retries;
  835. int ack_timeout_cnt = 1;
  836. unsigned i;
  837. BUILD_BUG_ON(sizeof(*cmd) != sizeof(i2400m_barker_db[0].data));
  838. BUILD_BUG_ON(sizeof(ack) != sizeof(i2400m_ACK_BARKER));
  839. d_fnstart(4, dev, "(i2400m %p flags 0x%08x)\n", i2400m, flags);
  840. result = -ENOMEM;
  841. cmd = i2400m->bm_cmd_buf;
  842. if (flags & I2400M_BRI_SOFT)
  843. goto do_reboot_ack;
  844. do_reboot:
  845. ack_timeout_cnt = 1;
  846. if (--count < 0)
  847. goto error_timeout;
  848. d_printf(4, dev, "device reboot: reboot command [%d # left]\n",
  849. count);
  850. if ((flags & I2400M_BRI_NO_REBOOT) == 0)
  851. i2400m_reset(i2400m, I2400M_RT_WARM);
  852. result = i2400m_bm_cmd(i2400m, NULL, 0, &ack, sizeof(ack),
  853. I2400M_BM_CMD_RAW);
  854. flags &= ~I2400M_BRI_NO_REBOOT;
  855. switch (result) {
  856. case -ERESTARTSYS:
  857. /*
  858. * at this point, i2400m_bm_cmd(), through
  859. * __i2400m_bm_ack_process(), has updated
  860. * i2400m->barker and we are good to go.
  861. */
  862. d_printf(4, dev, "device reboot: got reboot barker\n");
  863. break;
  864. case -EISCONN: /* we don't know how it got here...but we follow it */
  865. d_printf(4, dev, "device reboot: got ack barker - whatever\n");
  866. goto do_reboot;
  867. case -ETIMEDOUT:
  868. /*
  869. * Device has timed out, we might be in boot mode
  870. * already and expecting an ack; if we don't know what
  871. * the barker is, we just send them all. Cold reset
  872. * and bus reset don't work. Beats me.
  873. */
  874. if (i2400m->barker != NULL) {
  875. dev_err(dev, "device boot: reboot barker timed out, "
  876. "trying (set) %08x echo/ack\n",
  877. le32_to_cpu(i2400m->barker->data[0]));
  878. goto do_reboot_ack;
  879. }
  880. for (i = 0; i < i2400m_barker_db_used; i++) {
  881. struct i2400m_barker_db *barker = &i2400m_barker_db[i];
  882. memcpy(cmd, barker->data, sizeof(barker->data));
  883. result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
  884. &ack, sizeof(ack),
  885. I2400M_BM_CMD_RAW);
  886. if (result == -EISCONN) {
  887. dev_warn(dev, "device boot: got ack barker "
  888. "after sending echo/ack barker "
  889. "#%d/%08x; rebooting j.i.c.\n",
  890. i, le32_to_cpu(barker->data[0]));
  891. flags &= ~I2400M_BRI_NO_REBOOT;
  892. goto do_reboot;
  893. }
  894. }
  895. dev_err(dev, "device boot: tried all the echo/acks, could "
  896. "not get device to respond; giving up");
  897. result = -ESHUTDOWN;
  898. case -EPROTO:
  899. case -ESHUTDOWN: /* dev is gone */
  900. case -EINTR: /* user cancelled */
  901. goto error_dev_gone;
  902. default:
  903. dev_err(dev, "device reboot: error %d while waiting "
  904. "for reboot barker - rebooting\n", result);
  905. d_dump(1, dev, &ack, result);
  906. goto do_reboot;
  907. }
  908. /* At this point we ack back with 4 REBOOT barkers and expect
  909. * 4 ACK barkers. This is ugly, as we send a raw command --
  910. * hence the cast. _bm_cmd() will catch the reboot ack
  911. * notification and report it as -EISCONN. */
  912. do_reboot_ack:
  913. d_printf(4, dev, "device reboot ack: sending ack [%d # left]\n", count);
  914. memcpy(cmd, i2400m->barker->data, sizeof(i2400m->barker->data));
  915. result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
  916. &ack, sizeof(ack), I2400M_BM_CMD_RAW);
  917. switch (result) {
  918. case -ERESTARTSYS:
  919. d_printf(4, dev, "reboot ack: got reboot barker - retrying\n");
  920. if (--count < 0)
  921. goto error_timeout;
  922. goto do_reboot_ack;
  923. case -EISCONN:
  924. d_printf(4, dev, "reboot ack: got ack barker - good\n");
  925. break;
  926. case -ETIMEDOUT: /* no response, maybe it is the other type? */
  927. if (ack_timeout_cnt-- < 0) {
  928. d_printf(4, dev, "reboot ack timedout: retrying\n");
  929. goto do_reboot_ack;
  930. } else {
  931. dev_err(dev, "reboot ack timedout too long: "
  932. "trying reboot\n");
  933. goto do_reboot;
  934. }
  935. break;
  936. case -EPROTO:
  937. case -ESHUTDOWN: /* dev is gone */
  938. goto error_dev_gone;
  939. default:
  940. dev_err(dev, "device reboot ack: error %d while waiting for "
  941. "reboot ack barker - rebooting\n", result);
  942. goto do_reboot;
  943. }
  944. d_printf(2, dev, "device reboot ack: got ack barker - boot done\n");
  945. result = 0;
  946. exit_timeout:
  947. error_dev_gone:
  948. d_fnend(4, dev, "(i2400m %p flags 0x%08x) = %d\n",
  949. i2400m, flags, result);
  950. return result;
  951. error_timeout:
  952. dev_err(dev, "Timed out waiting for reboot ack\n");
  953. result = -ETIMEDOUT;
  954. goto exit_timeout;
  955. }
  956. /*
  957. * Read the MAC addr
  958. *
  959. * The position this function reads is fixed in device memory and
  960. * always available, even without firmware.
  961. *
  962. * Note we specify we want to read only six bytes, but provide space
  963. * for 16, as we always get it rounded up.
  964. */
  965. int i2400m_read_mac_addr(struct i2400m *i2400m)
  966. {
  967. int result;
  968. struct device *dev = i2400m_dev(i2400m);
  969. struct net_device *net_dev = i2400m->wimax_dev.net_dev;
  970. struct i2400m_bootrom_header *cmd;
  971. struct {
  972. struct i2400m_bootrom_header ack;
  973. u8 ack_pl[16];
  974. } __packed ack_buf;
  975. d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
  976. cmd = i2400m->bm_cmd_buf;
  977. cmd->command = i2400m_brh_command(I2400M_BRH_READ, 0, 1);
  978. cmd->target_addr = cpu_to_le32(0x00203fe8);
  979. cmd->data_size = cpu_to_le32(6);
  980. result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
  981. &ack_buf.ack, sizeof(ack_buf), 0);
  982. if (result < 0) {
  983. dev_err(dev, "BM: read mac addr failed: %d\n", result);
  984. goto error_read_mac;
  985. }
  986. d_printf(2, dev, "mac addr is %pM\n", ack_buf.ack_pl);
  987. if (i2400m->bus_bm_mac_addr_impaired == 1) {
  988. ack_buf.ack_pl[0] = 0x00;
  989. ack_buf.ack_pl[1] = 0x16;
  990. ack_buf.ack_pl[2] = 0xd3;
  991. get_random_bytes(&ack_buf.ack_pl[3], 3);
  992. dev_err(dev, "BM is MAC addr impaired, faking MAC addr to "
  993. "mac addr is %pM\n", ack_buf.ack_pl);
  994. result = 0;
  995. }
  996. net_dev->addr_len = ETH_ALEN;
  997. memcpy(net_dev->dev_addr, ack_buf.ack_pl, ETH_ALEN);
  998. error_read_mac:
  999. d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, result);
  1000. return result;
  1001. }
  1002. /*
  1003. * Initialize a non signed boot
  1004. *
  1005. * This implies sending some magic values to the device's memory. Note
  1006. * we convert the values to little endian in the same array
  1007. * declaration.
  1008. */
  1009. static
  1010. int i2400m_dnload_init_nonsigned(struct i2400m *i2400m)
  1011. {
  1012. unsigned i = 0;
  1013. int ret = 0;
  1014. struct device *dev = i2400m_dev(i2400m);
  1015. d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
  1016. if (i2400m->bus_bm_pokes_table) {
  1017. while (i2400m->bus_bm_pokes_table[i].address) {
  1018. ret = i2400m_download_chunk(
  1019. i2400m,
  1020. &i2400m->bus_bm_pokes_table[i].data,
  1021. sizeof(i2400m->bus_bm_pokes_table[i].data),
  1022. i2400m->bus_bm_pokes_table[i].address, 1, 1);
  1023. if (ret < 0)
  1024. break;
  1025. i++;
  1026. }
  1027. }
  1028. d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, ret);
  1029. return ret;
  1030. }
  1031. /*
  1032. * Initialize the signed boot process
  1033. *
  1034. * @i2400m: device descriptor
  1035. *
  1036. * @bcf_hdr: pointer to the firmware header; assumes it is fully in
  1037. * memory (it has gone through basic validation).
  1038. *
  1039. * Returns: 0 if ok, < 0 errno code on error, -ERESTARTSYS if the hw
  1040. * rebooted.
  1041. *
  1042. * This writes the firmware BCF header to the device using the
  1043. * HASH_PAYLOAD_ONLY command.
  1044. */
  1045. static
  1046. int i2400m_dnload_init_signed(struct i2400m *i2400m,
  1047. const struct i2400m_bcf_hdr *bcf_hdr)
  1048. {
  1049. int ret;
  1050. struct device *dev = i2400m_dev(i2400m);
  1051. struct {
  1052. struct i2400m_bootrom_header cmd;
  1053. struct i2400m_bcf_hdr cmd_pl;
  1054. } __packed *cmd_buf;
  1055. struct i2400m_bootrom_header ack;
  1056. d_fnstart(5, dev, "(i2400m %p bcf_hdr %p)\n", i2400m, bcf_hdr);
  1057. cmd_buf = i2400m->bm_cmd_buf;
  1058. cmd_buf->cmd.command =
  1059. i2400m_brh_command(I2400M_BRH_HASH_PAYLOAD_ONLY, 0, 0);
  1060. cmd_buf->cmd.target_addr = 0;
  1061. cmd_buf->cmd.data_size = cpu_to_le32(sizeof(cmd_buf->cmd_pl));
  1062. memcpy(&cmd_buf->cmd_pl, bcf_hdr, sizeof(*bcf_hdr));
  1063. ret = i2400m_bm_cmd(i2400m, &cmd_buf->cmd, sizeof(*cmd_buf),
  1064. &ack, sizeof(ack), 0);
  1065. if (ret >= 0)
  1066. ret = 0;
  1067. d_fnend(5, dev, "(i2400m %p bcf_hdr %p) = %d\n", i2400m, bcf_hdr, ret);
  1068. return ret;
  1069. }
  1070. /*
  1071. * Initialize the firmware download at the device size
  1072. *
  1073. * Multiplex to the one that matters based on the device's mode
  1074. * (signed or non-signed).
  1075. */
  1076. static
  1077. int i2400m_dnload_init(struct i2400m *i2400m,
  1078. const struct i2400m_bcf_hdr *bcf_hdr)
  1079. {
  1080. int result;
  1081. struct device *dev = i2400m_dev(i2400m);
  1082. if (i2400m_boot_is_signed(i2400m)) {
  1083. d_printf(1, dev, "signed boot\n");
  1084. result = i2400m_dnload_init_signed(i2400m, bcf_hdr);
  1085. if (result == -ERESTARTSYS)
  1086. return result;
  1087. if (result < 0)
  1088. dev_err(dev, "firmware %s: signed boot download "
  1089. "initialization failed: %d\n",
  1090. i2400m->fw_name, result);
  1091. } else {
  1092. /* non-signed boot process without pokes */
  1093. d_printf(1, dev, "non-signed boot\n");
  1094. result = i2400m_dnload_init_nonsigned(i2400m);
  1095. if (result == -ERESTARTSYS)
  1096. return result;
  1097. if (result < 0)
  1098. dev_err(dev, "firmware %s: non-signed download "
  1099. "initialization failed: %d\n",
  1100. i2400m->fw_name, result);
  1101. }
  1102. return result;
  1103. }
  1104. /*
  1105. * Run consistency tests on the firmware file and load up headers
  1106. *
  1107. * Check for the firmware being made for the i2400m device,
  1108. * etc...These checks are mostly informative, as the device will make
  1109. * them too; but the driver's response is more informative on what
  1110. * went wrong.
  1111. *
  1112. * This will also look at all the headers present on the firmware
  1113. * file, and update i2400m->fw_bcf_hdr to point to them.
  1114. */
  1115. static
  1116. int i2400m_fw_hdr_check(struct i2400m *i2400m,
  1117. const struct i2400m_bcf_hdr *bcf_hdr,
  1118. size_t index, size_t offset)
  1119. {
  1120. struct device *dev = i2400m_dev(i2400m);
  1121. unsigned module_type, header_len, major_version, minor_version,
  1122. module_id, module_vendor, date, size;
  1123. module_type = le32_to_cpu(bcf_hdr->module_type);
  1124. header_len = sizeof(u32) * le32_to_cpu(bcf_hdr->header_len);
  1125. major_version = (le32_to_cpu(bcf_hdr->header_version) & 0xffff0000)
  1126. >> 16;
  1127. minor_version = le32_to_cpu(bcf_hdr->header_version) & 0x0000ffff;
  1128. module_id = le32_to_cpu(bcf_hdr->module_id);
  1129. module_vendor = le32_to_cpu(bcf_hdr->module_vendor);
  1130. date = le32_to_cpu(bcf_hdr->date);
  1131. size = sizeof(u32) * le32_to_cpu(bcf_hdr->size);
  1132. d_printf(1, dev, "firmware %s #%zd@%08zx: BCF header "
  1133. "type:vendor:id 0x%x:%x:%x v%u.%u (%u/%u B) built %08x\n",
  1134. i2400m->fw_name, index, offset,
  1135. module_type, module_vendor, module_id,
  1136. major_version, minor_version, header_len, size, date);
  1137. /* Hard errors */
  1138. if (major_version != 1) {
  1139. dev_err(dev, "firmware %s #%zd@%08zx: major header version "
  1140. "v%u.%u not supported\n",
  1141. i2400m->fw_name, index, offset,
  1142. major_version, minor_version);
  1143. return -EBADF;
  1144. }
  1145. if (module_type != 6) { /* built for the right hardware? */
  1146. dev_err(dev, "firmware %s #%zd@%08zx: unexpected module "
  1147. "type 0x%x; aborting\n",
  1148. i2400m->fw_name, index, offset,
  1149. module_type);
  1150. return -EBADF;
  1151. }
  1152. if (module_vendor != 0x8086) {
  1153. dev_err(dev, "firmware %s #%zd@%08zx: unexpected module "
  1154. "vendor 0x%x; aborting\n",
  1155. i2400m->fw_name, index, offset, module_vendor);
  1156. return -EBADF;
  1157. }
  1158. if (date < 0x20080300)
  1159. dev_warn(dev, "firmware %s #%zd@%08zx: build date %08x "
  1160. "too old; unsupported\n",
  1161. i2400m->fw_name, index, offset, date);
  1162. return 0;
  1163. }
  1164. /*
  1165. * Run consistency tests on the firmware file and load up headers
  1166. *
  1167. * Check for the firmware being made for the i2400m device,
  1168. * etc...These checks are mostly informative, as the device will make
  1169. * them too; but the driver's response is more informative on what
  1170. * went wrong.
  1171. *
  1172. * This will also look at all the headers present on the firmware
  1173. * file, and update i2400m->fw_hdrs to point to them.
  1174. */
  1175. static
  1176. int i2400m_fw_check(struct i2400m *i2400m, const void *bcf, size_t bcf_size)
  1177. {
  1178. int result;
  1179. struct device *dev = i2400m_dev(i2400m);
  1180. size_t headers = 0;
  1181. const struct i2400m_bcf_hdr *bcf_hdr;
  1182. const void *itr, *next, *top;
  1183. size_t slots = 0, used_slots = 0;
  1184. for (itr = bcf, top = itr + bcf_size;
  1185. itr < top;
  1186. headers++, itr = next) {
  1187. size_t leftover, offset, header_len, size;
  1188. leftover = top - itr;
  1189. offset = itr - bcf;
  1190. if (leftover <= sizeof(*bcf_hdr)) {
  1191. dev_err(dev, "firmware %s: %zu B left at @%zx, "
  1192. "not enough for BCF header\n",
  1193. i2400m->fw_name, leftover, offset);
  1194. break;
  1195. }
  1196. bcf_hdr = itr;
  1197. /* Only the first header is supposed to be followed by
  1198. * payload */
  1199. header_len = sizeof(u32) * le32_to_cpu(bcf_hdr->header_len);
  1200. size = sizeof(u32) * le32_to_cpu(bcf_hdr->size);
  1201. if (headers == 0)
  1202. next = itr + size;
  1203. else
  1204. next = itr + header_len;
  1205. result = i2400m_fw_hdr_check(i2400m, bcf_hdr, headers, offset);
  1206. if (result < 0)
  1207. continue;
  1208. if (used_slots + 1 >= slots) {
  1209. /* +1 -> we need to account for the one we'll
  1210. * occupy and at least an extra one for
  1211. * always being NULL */
  1212. result = i2400m_zrealloc_2x(
  1213. (void **) &i2400m->fw_hdrs, &slots,
  1214. sizeof(i2400m->fw_hdrs[0]),
  1215. GFP_KERNEL);
  1216. if (result < 0)
  1217. goto error_zrealloc;
  1218. }
  1219. i2400m->fw_hdrs[used_slots] = bcf_hdr;
  1220. used_slots++;
  1221. }
  1222. if (headers == 0) {
  1223. dev_err(dev, "firmware %s: no usable headers found\n",
  1224. i2400m->fw_name);
  1225. result = -EBADF;
  1226. } else
  1227. result = 0;
  1228. error_zrealloc:
  1229. return result;
  1230. }
  1231. /*
  1232. * Match a barker to a BCF header module ID
  1233. *
  1234. * The device sends a barker which tells the firmware loader which
  1235. * header in the BCF file has to be used. This does the matching.
  1236. */
  1237. static
  1238. unsigned i2400m_bcf_hdr_match(struct i2400m *i2400m,
  1239. const struct i2400m_bcf_hdr *bcf_hdr)
  1240. {
  1241. u32 barker = le32_to_cpu(i2400m->barker->data[0])
  1242. & 0x7fffffff;
  1243. u32 module_id = le32_to_cpu(bcf_hdr->module_id)
  1244. & 0x7fffffff; /* high bit used for something else */
  1245. /* special case for 5x50 */
  1246. if (barker == I2400M_SBOOT_BARKER && module_id == 0)
  1247. return 1;
  1248. if (module_id == barker)
  1249. return 1;
  1250. return 0;
  1251. }
  1252. static
  1253. const struct i2400m_bcf_hdr *i2400m_bcf_hdr_find(struct i2400m *i2400m)
  1254. {
  1255. struct device *dev = i2400m_dev(i2400m);
  1256. const struct i2400m_bcf_hdr **bcf_itr, *bcf_hdr;
  1257. unsigned i = 0;
  1258. u32 barker = le32_to_cpu(i2400m->barker->data[0]);
  1259. d_printf(2, dev, "finding BCF header for barker %08x\n", barker);
  1260. if (barker == I2400M_NBOOT_BARKER) {
  1261. bcf_hdr = i2400m->fw_hdrs[0];
  1262. d_printf(1, dev, "using BCF header #%u/%08x for non-signed "
  1263. "barker\n", 0, le32_to_cpu(bcf_hdr->module_id));
  1264. return bcf_hdr;
  1265. }
  1266. for (bcf_itr = i2400m->fw_hdrs; *bcf_itr != NULL; bcf_itr++, i++) {
  1267. bcf_hdr = *bcf_itr;
  1268. if (i2400m_bcf_hdr_match(i2400m, bcf_hdr)) {
  1269. d_printf(1, dev, "hit on BCF hdr #%u/%08x\n",
  1270. i, le32_to_cpu(bcf_hdr->module_id));
  1271. return bcf_hdr;
  1272. } else
  1273. d_printf(1, dev, "miss on BCF hdr #%u/%08x\n",
  1274. i, le32_to_cpu(bcf_hdr->module_id));
  1275. }
  1276. dev_err(dev, "cannot find a matching BCF header for barker %08x\n",
  1277. barker);
  1278. return NULL;
  1279. }
  1280. /*
  1281. * Download the firmware to the device
  1282. *
  1283. * @i2400m: device descriptor
  1284. * @bcf: pointer to loaded (and minimally verified for consistency)
  1285. * firmware
  1286. * @bcf_size: size of the @bcf buffer (header plus payloads)
  1287. *
  1288. * The process for doing this is described in this file's header.
  1289. *
  1290. * Note we only reinitialize boot-mode if the flags say so. Some hw
  1291. * iterations need it, some don't. In any case, if we loop, we always
  1292. * need to reinitialize the boot room, hence the flags modification.
  1293. */
  1294. static
  1295. int i2400m_fw_dnload(struct i2400m *i2400m, const struct i2400m_bcf_hdr *bcf,
  1296. size_t fw_size, enum i2400m_bri flags)
  1297. {
  1298. int ret = 0;
  1299. struct device *dev = i2400m_dev(i2400m);
  1300. int count = i2400m->bus_bm_retries;
  1301. const struct i2400m_bcf_hdr *bcf_hdr;
  1302. size_t bcf_size;
  1303. d_fnstart(5, dev, "(i2400m %p bcf %p fw size %zu)\n",
  1304. i2400m, bcf, fw_size);
  1305. i2400m->boot_mode = 1;
  1306. wmb(); /* Make sure other readers see it */
  1307. hw_reboot:
  1308. if (count-- == 0) {
  1309. ret = -ERESTARTSYS;
  1310. dev_err(dev, "device rebooted too many times, aborting\n");
  1311. goto error_too_many_reboots;
  1312. }
  1313. if (flags & I2400M_BRI_MAC_REINIT) {
  1314. ret = i2400m_bootrom_init(i2400m, flags);
  1315. if (ret < 0) {
  1316. dev_err(dev, "bootrom init failed: %d\n", ret);
  1317. goto error_bootrom_init;
  1318. }
  1319. }
  1320. flags |= I2400M_BRI_MAC_REINIT;
  1321. /*
  1322. * Initialize the download, push the bytes to the device and
  1323. * then jump to the new firmware. Note @ret is passed with the
  1324. * offset of the jump instruction to _dnload_finalize()
  1325. *
  1326. * Note we need to use the BCF header in the firmware image
  1327. * that matches the barker that the device sent when it
  1328. * rebooted, so it has to be passed along.
  1329. */
  1330. ret = -EBADF;
  1331. bcf_hdr = i2400m_bcf_hdr_find(i2400m);
  1332. if (bcf_hdr == NULL)
  1333. goto error_bcf_hdr_find;
  1334. ret = i2400m_dnload_init(i2400m, bcf_hdr);
  1335. if (ret == -ERESTARTSYS)
  1336. goto error_dev_rebooted;
  1337. if (ret < 0)
  1338. goto error_dnload_init;
  1339. /*
  1340. * bcf_size refers to one header size plus the fw sections size
  1341. * indicated by the header,ie. if there are other extended headers
  1342. * at the tail, they are not counted
  1343. */
  1344. bcf_size = sizeof(u32) * le32_to_cpu(bcf_hdr->size);
  1345. ret = i2400m_dnload_bcf(i2400m, bcf, bcf_size);
  1346. if (ret == -ERESTARTSYS)
  1347. goto error_dev_rebooted;
  1348. if (ret < 0) {
  1349. dev_err(dev, "fw %s: download failed: %d\n",
  1350. i2400m->fw_name, ret);
  1351. goto error_dnload_bcf;
  1352. }
  1353. ret = i2400m_dnload_finalize(i2400m, bcf_hdr, bcf, ret);
  1354. if (ret == -ERESTARTSYS)
  1355. goto error_dev_rebooted;
  1356. if (ret < 0) {
  1357. dev_err(dev, "fw %s: "
  1358. "download finalization failed: %d\n",
  1359. i2400m->fw_name, ret);
  1360. goto error_dnload_finalize;
  1361. }
  1362. d_printf(2, dev, "fw %s successfully uploaded\n",
  1363. i2400m->fw_name);
  1364. i2400m->boot_mode = 0;
  1365. wmb(); /* Make sure i2400m_msg_to_dev() sees boot_mode */
  1366. error_dnload_finalize:
  1367. error_dnload_bcf:
  1368. error_dnload_init:
  1369. error_bcf_hdr_find:
  1370. error_bootrom_init:
  1371. error_too_many_reboots:
  1372. d_fnend(5, dev, "(i2400m %p bcf %p size %zu) = %d\n",
  1373. i2400m, bcf, fw_size, ret);
  1374. return ret;
  1375. error_dev_rebooted:
  1376. dev_err(dev, "device rebooted, %d tries left\n", count);
  1377. /* we got the notification already, no need to wait for it again */
  1378. flags |= I2400M_BRI_SOFT;
  1379. goto hw_reboot;
  1380. }
  1381. static
  1382. int i2400m_fw_bootstrap(struct i2400m *i2400m, const struct firmware *fw,
  1383. enum i2400m_bri flags)
  1384. {
  1385. int ret;
  1386. struct device *dev = i2400m_dev(i2400m);
  1387. const struct i2400m_bcf_hdr *bcf; /* Firmware data */
  1388. d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
  1389. bcf = (void *) fw->data;
  1390. ret = i2400m_fw_check(i2400m, bcf, fw->size);
  1391. if (ret >= 0)
  1392. ret = i2400m_fw_dnload(i2400m, bcf, fw->size, flags);
  1393. if (ret < 0)
  1394. dev_err(dev, "%s: cannot use: %d, skipping\n",
  1395. i2400m->fw_name, ret);
  1396. kfree(i2400m->fw_hdrs);
  1397. i2400m->fw_hdrs = NULL;
  1398. d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, ret);
  1399. return ret;
  1400. }
  1401. /* Refcounted container for firmware data */
  1402. struct i2400m_fw {
  1403. struct kref kref;
  1404. const struct firmware *fw;
  1405. };
  1406. static
  1407. void i2400m_fw_destroy(struct kref *kref)
  1408. {
  1409. struct i2400m_fw *i2400m_fw =
  1410. container_of(kref, struct i2400m_fw, kref);
  1411. release_firmware(i2400m_fw->fw);
  1412. kfree(i2400m_fw);
  1413. }
  1414. static
  1415. struct i2400m_fw *i2400m_fw_get(struct i2400m_fw *i2400m_fw)
  1416. {
  1417. if (i2400m_fw != NULL && i2400m_fw != (void *) ~0)
  1418. kref_get(&i2400m_fw->kref);
  1419. return i2400m_fw;
  1420. }
  1421. static
  1422. void i2400m_fw_put(struct i2400m_fw *i2400m_fw)
  1423. {
  1424. kref_put(&i2400m_fw->kref, i2400m_fw_destroy);
  1425. }
  1426. /**
  1427. * i2400m_dev_bootstrap - Bring the device to a known state and upload firmware
  1428. *
  1429. * @i2400m: device descriptor
  1430. *
  1431. * Returns: >= 0 if ok, < 0 errno code on error.
  1432. *
  1433. * This sets up the firmware upload environment, loads the firmware
  1434. * file from disk, verifies and then calls the firmware upload process
  1435. * per se.
  1436. *
  1437. * Can be called either from probe, or after a warm reset. Can not be
  1438. * called from within an interrupt. All the flow in this code is
  1439. * single-threade; all I/Os are synchronous.
  1440. */
  1441. int i2400m_dev_bootstrap(struct i2400m *i2400m, enum i2400m_bri flags)
  1442. {
  1443. int ret, itr;
  1444. struct device *dev = i2400m_dev(i2400m);
  1445. struct i2400m_fw *i2400m_fw;
  1446. const struct firmware *fw;
  1447. const char *fw_name;
  1448. d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
  1449. ret = -ENODEV;
  1450. spin_lock(&i2400m->rx_lock);
  1451. i2400m_fw = i2400m_fw_get(i2400m->fw_cached);
  1452. spin_unlock(&i2400m->rx_lock);
  1453. if (i2400m_fw == (void *) ~0) {
  1454. dev_err(dev, "can't load firmware now!");
  1455. goto out;
  1456. } else if (i2400m_fw != NULL) {
  1457. dev_info(dev, "firmware %s: loading from cache\n",
  1458. i2400m->fw_name);
  1459. ret = i2400m_fw_bootstrap(i2400m, i2400m_fw->fw, flags);
  1460. i2400m_fw_put(i2400m_fw);
  1461. goto out;
  1462. }
  1463. /* Load firmware files to memory. */
  1464. for (itr = 0, ret = -ENOENT; ; itr++) {
  1465. fw_name = i2400m->bus_fw_names[itr];
  1466. if (fw_name == NULL) {
  1467. dev_err(dev, "Could not find a usable firmware image\n");
  1468. break;
  1469. }
  1470. d_printf(1, dev, "trying firmware %s (%d)\n", fw_name, itr);
  1471. ret = request_firmware(&fw, fw_name, dev);
  1472. if (ret < 0) {
  1473. dev_err(dev, "fw %s: cannot load file: %d\n",
  1474. fw_name, ret);
  1475. continue;
  1476. }
  1477. i2400m->fw_name = fw_name;
  1478. ret = i2400m_fw_bootstrap(i2400m, fw, flags);
  1479. release_firmware(fw);
  1480. if (ret >= 0) /* firmware loaded successfully */
  1481. break;
  1482. i2400m->fw_name = NULL;
  1483. }
  1484. out:
  1485. d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, ret);
  1486. return ret;
  1487. }
  1488. EXPORT_SYMBOL_GPL(i2400m_dev_bootstrap);
  1489. void i2400m_fw_cache(struct i2400m *i2400m)
  1490. {
  1491. int result;
  1492. struct i2400m_fw *i2400m_fw;
  1493. struct device *dev = i2400m_dev(i2400m);
  1494. /* if there is anything there, free it -- now, this'd be weird */
  1495. spin_lock(&i2400m->rx_lock);
  1496. i2400m_fw = i2400m->fw_cached;
  1497. spin_unlock(&i2400m->rx_lock);
  1498. if (i2400m_fw != NULL && i2400m_fw != (void *) ~0) {
  1499. i2400m_fw_put(i2400m_fw);
  1500. WARN(1, "%s:%u: still cached fw still present?\n",
  1501. __func__, __LINE__);
  1502. }
  1503. if (i2400m->fw_name == NULL) {
  1504. dev_err(dev, "firmware n/a: can't cache\n");
  1505. i2400m_fw = (void *) ~0;
  1506. goto out;
  1507. }
  1508. i2400m_fw = kzalloc(sizeof(*i2400m_fw), GFP_ATOMIC);
  1509. if (i2400m_fw == NULL)
  1510. goto out;
  1511. kref_init(&i2400m_fw->kref);
  1512. result = request_firmware(&i2400m_fw->fw, i2400m->fw_name, dev);
  1513. if (result < 0) {
  1514. dev_err(dev, "firmware %s: failed to cache: %d\n",
  1515. i2400m->fw_name, result);
  1516. kfree(i2400m_fw);
  1517. i2400m_fw = (void *) ~0;
  1518. } else
  1519. dev_info(dev, "firmware %s: cached\n", i2400m->fw_name);
  1520. out:
  1521. spin_lock(&i2400m->rx_lock);
  1522. i2400m->fw_cached = i2400m_fw;
  1523. spin_unlock(&i2400m->rx_lock);
  1524. }
  1525. void i2400m_fw_uncache(struct i2400m *i2400m)
  1526. {
  1527. struct i2400m_fw *i2400m_fw;
  1528. spin_lock(&i2400m->rx_lock);
  1529. i2400m_fw = i2400m->fw_cached;
  1530. i2400m->fw_cached = NULL;
  1531. spin_unlock(&i2400m->rx_lock);
  1532. if (i2400m_fw != NULL && i2400m_fw != (void *) ~0)
  1533. i2400m_fw_put(i2400m_fw);
  1534. }