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. unsigned index = (i2400m->barker - i2400m_barker_db)
  374. / sizeof(*i2400m->barker);
  375. d_printf(2, dev, "boot barker cache-confirmed #%u/%08x\n",
  376. index, le32_to_cpu(i2400m->barker->data[0]));
  377. return 0;
  378. }
  379. for (i = 0; i < i2400m_barker_db_used; i++) {
  380. barker = &i2400m_barker_db[i];
  381. BUILD_BUG_ON(sizeof(barker->data) != 16);
  382. if (memcmp(buf, barker->data, sizeof(barker->data)))
  383. continue;
  384. if (i2400m->barker == NULL) {
  385. i2400m->barker = barker;
  386. d_printf(1, dev, "boot barker set to #%u/%08x\n",
  387. i, le32_to_cpu(barker->data[0]));
  388. if (barker->data[0] == le32_to_cpu(I2400M_NBOOT_BARKER))
  389. i2400m->sboot = 0;
  390. else
  391. i2400m->sboot = 1;
  392. } else if (i2400m->barker != barker) {
  393. dev_err(dev, "HW inconsistency: device "
  394. "reports a different boot barker "
  395. "than set (from %08x to %08x)\n",
  396. le32_to_cpu(i2400m->barker->data[0]),
  397. le32_to_cpu(barker->data[0]));
  398. result = -EIO;
  399. } else
  400. d_printf(2, dev, "boot barker confirmed #%u/%08x\n",
  401. i, le32_to_cpu(barker->data[0]));
  402. result = 0;
  403. break;
  404. }
  405. return result;
  406. }
  407. EXPORT_SYMBOL_GPL(i2400m_is_boot_barker);
  408. /*
  409. * Verify the ack data received
  410. *
  411. * Given a reply to a boot mode command, chew it and verify everything
  412. * is ok.
  413. *
  414. * @opcode: opcode which generated this ack. For error messages.
  415. * @ack: pointer to ack data we received
  416. * @ack_size: size of that data buffer
  417. * @flags: I2400M_BM_CMD_* flags we called the command with.
  418. *
  419. * Way too long function -- maybe it should be further split
  420. */
  421. static
  422. ssize_t __i2400m_bm_ack_verify(struct i2400m *i2400m, int opcode,
  423. struct i2400m_bootrom_header *ack,
  424. size_t ack_size, int flags)
  425. {
  426. ssize_t result = -ENOMEM;
  427. struct device *dev = i2400m_dev(i2400m);
  428. d_fnstart(8, dev, "(i2400m %p opcode %d ack %p size %zu)\n",
  429. i2400m, opcode, ack, ack_size);
  430. if (ack_size < sizeof(*ack)) {
  431. result = -EIO;
  432. dev_err(dev, "boot-mode cmd %d: HW BUG? notification didn't "
  433. "return enough data (%zu bytes vs %zu expected)\n",
  434. opcode, ack_size, sizeof(*ack));
  435. goto error_ack_short;
  436. }
  437. result = i2400m_is_boot_barker(i2400m, ack, ack_size);
  438. if (result >= 0) {
  439. result = -ERESTARTSYS;
  440. d_printf(6, dev, "boot-mode cmd %d: HW boot barker\n", opcode);
  441. goto error_reboot;
  442. }
  443. if (ack_size == sizeof(i2400m_ACK_BARKER)
  444. && memcmp(ack, i2400m_ACK_BARKER, sizeof(*ack)) == 0) {
  445. result = -EISCONN;
  446. d_printf(3, dev, "boot-mode cmd %d: HW reboot ack barker\n",
  447. opcode);
  448. goto error_reboot_ack;
  449. }
  450. result = 0;
  451. if (flags & I2400M_BM_CMD_RAW)
  452. goto out_raw;
  453. ack->data_size = le32_to_cpu(ack->data_size);
  454. ack->target_addr = le32_to_cpu(ack->target_addr);
  455. ack->block_checksum = le32_to_cpu(ack->block_checksum);
  456. d_printf(5, dev, "boot-mode cmd %d: notification for opcode %u "
  457. "response %u csum %u rr %u da %u\n",
  458. opcode, i2400m_brh_get_opcode(ack),
  459. i2400m_brh_get_response(ack),
  460. i2400m_brh_get_use_checksum(ack),
  461. i2400m_brh_get_response_required(ack),
  462. i2400m_brh_get_direct_access(ack));
  463. result = -EIO;
  464. if (i2400m_brh_get_signature(ack) != 0xcbbc) {
  465. dev_err(dev, "boot-mode cmd %d: HW BUG? wrong signature "
  466. "0x%04x\n", opcode, i2400m_brh_get_signature(ack));
  467. goto error_ack_signature;
  468. }
  469. if (opcode != -1 && opcode != i2400m_brh_get_opcode(ack)) {
  470. dev_err(dev, "boot-mode cmd %d: HW BUG? "
  471. "received response for opcode %u, expected %u\n",
  472. opcode, i2400m_brh_get_opcode(ack), opcode);
  473. goto error_ack_opcode;
  474. }
  475. if (i2400m_brh_get_response(ack) != 0) { /* failed? */
  476. dev_err(dev, "boot-mode cmd %d: error; hw response %u\n",
  477. opcode, i2400m_brh_get_response(ack));
  478. goto error_ack_failed;
  479. }
  480. if (ack_size < ack->data_size + sizeof(*ack)) {
  481. dev_err(dev, "boot-mode cmd %d: SW BUG "
  482. "driver provided only %zu bytes for %zu bytes "
  483. "of data\n", opcode, ack_size,
  484. (size_t) le32_to_cpu(ack->data_size) + sizeof(*ack));
  485. goto error_ack_short_buffer;
  486. }
  487. result = ack_size;
  488. /* Don't you love this stack of empty targets? Well, I don't
  489. * either, but it helps track exactly who comes in here and
  490. * why :) */
  491. error_ack_short_buffer:
  492. error_ack_failed:
  493. error_ack_opcode:
  494. error_ack_signature:
  495. out_raw:
  496. error_reboot_ack:
  497. error_reboot:
  498. error_ack_short:
  499. d_fnend(8, dev, "(i2400m %p opcode %d ack %p size %zu) = %d\n",
  500. i2400m, opcode, ack, ack_size, (int) result);
  501. return result;
  502. }
  503. /**
  504. * i2400m_bm_cmd - Execute a boot mode command
  505. *
  506. * @cmd: buffer containing the command data (pointing at the header).
  507. * This data can be ANYWHERE (for USB, we will copy it to an
  508. * specific buffer). Make sure everything is in proper little
  509. * endian.
  510. *
  511. * A raw buffer can be also sent, just cast it and set flags to
  512. * I2400M_BM_CMD_RAW.
  513. *
  514. * This function will generate a checksum for you if the
  515. * checksum bit in the command is set (unless I2400M_BM_CMD_RAW
  516. * is set).
  517. *
  518. * You can use the i2400m->bm_cmd_buf to stage your commands and
  519. * send them.
  520. *
  521. * If NULL, no command is sent (we just wait for an ack).
  522. *
  523. * @cmd_size: size of the command. Will be auto padded to the
  524. * bus-specific drivers padding requirements.
  525. *
  526. * @ack: buffer where to place the acknowledgement. If it is a regular
  527. * command response, all fields will be returned with the right,
  528. * native endianess.
  529. *
  530. * You *cannot* use i2400m->bm_ack_buf for this buffer.
  531. *
  532. * @ack_size: size of @ack, 16 aligned; you need to provide at least
  533. * sizeof(*ack) bytes and then enough to contain the return data
  534. * from the command
  535. *
  536. * @flags: see I2400M_BM_CMD_* above.
  537. *
  538. * @returns: bytes received by the notification; if < 0, an errno code
  539. * denoting an error or:
  540. *
  541. * -ERESTARTSYS The device has rebooted
  542. *
  543. * Executes a boot-mode command and waits for a response, doing basic
  544. * validation on it; if a zero length response is received, it retries
  545. * waiting for a response until a non-zero one is received (timing out
  546. * after %I2400M_BOOT_RETRIES retries).
  547. */
  548. static
  549. ssize_t i2400m_bm_cmd(struct i2400m *i2400m,
  550. const struct i2400m_bootrom_header *cmd, size_t cmd_size,
  551. struct i2400m_bootrom_header *ack, size_t ack_size,
  552. int flags)
  553. {
  554. ssize_t result = -ENOMEM, rx_bytes;
  555. struct device *dev = i2400m_dev(i2400m);
  556. int opcode = cmd == NULL ? -1 : i2400m_brh_get_opcode(cmd);
  557. d_fnstart(6, dev, "(i2400m %p cmd %p size %zu ack %p size %zu)\n",
  558. i2400m, cmd, cmd_size, ack, ack_size);
  559. BUG_ON(ack_size < sizeof(*ack));
  560. BUG_ON(i2400m->boot_mode == 0);
  561. if (cmd != NULL) { /* send the command */
  562. result = i2400m->bus_bm_cmd_send(i2400m, cmd, cmd_size, flags);
  563. if (result < 0)
  564. goto error_cmd_send;
  565. if ((flags & I2400M_BM_CMD_RAW) == 0)
  566. d_printf(5, dev,
  567. "boot-mode cmd %d csum %u rr %u da %u: "
  568. "addr 0x%04x size %u block csum 0x%04x\n",
  569. opcode, i2400m_brh_get_use_checksum(cmd),
  570. i2400m_brh_get_response_required(cmd),
  571. i2400m_brh_get_direct_access(cmd),
  572. cmd->target_addr, cmd->data_size,
  573. cmd->block_checksum);
  574. }
  575. result = i2400m->bus_bm_wait_for_ack(i2400m, ack, ack_size);
  576. if (result < 0) {
  577. dev_err(dev, "boot-mode cmd %d: error waiting for an ack: %d\n",
  578. opcode, (int) result); /* bah, %zd doesn't work */
  579. goto error_wait_for_ack;
  580. }
  581. rx_bytes = result;
  582. /* verify the ack and read more if necessary [result is the
  583. * final amount of bytes we get in the ack] */
  584. result = __i2400m_bm_ack_verify(i2400m, opcode, ack, ack_size, flags);
  585. if (result < 0)
  586. goto error_bad_ack;
  587. /* Don't you love this stack of empty targets? Well, I don't
  588. * either, but it helps track exactly who comes in here and
  589. * why :) */
  590. result = rx_bytes;
  591. error_bad_ack:
  592. error_wait_for_ack:
  593. error_cmd_send:
  594. d_fnend(6, dev, "(i2400m %p cmd %p size %zu ack %p size %zu) = %d\n",
  595. i2400m, cmd, cmd_size, ack, ack_size, (int) result);
  596. return result;
  597. }
  598. /**
  599. * i2400m_download_chunk - write a single chunk of data to the device's memory
  600. *
  601. * @i2400m: device descriptor
  602. * @buf: the buffer to write
  603. * @buf_len: length of the buffer to write
  604. * @addr: address in the device memory space
  605. * @direct: bootrom write mode
  606. * @do_csum: should a checksum validation be performed
  607. */
  608. static int i2400m_download_chunk(struct i2400m *i2400m, const void *chunk,
  609. size_t __chunk_len, unsigned long addr,
  610. unsigned int direct, unsigned int do_csum)
  611. {
  612. int ret;
  613. size_t chunk_len = ALIGN(__chunk_len, I2400M_PL_ALIGN);
  614. struct device *dev = i2400m_dev(i2400m);
  615. struct {
  616. struct i2400m_bootrom_header cmd;
  617. u8 cmd_payload[];
  618. } __packed *buf;
  619. struct i2400m_bootrom_header ack;
  620. d_fnstart(5, dev, "(i2400m %p chunk %p __chunk_len %zu addr 0x%08lx "
  621. "direct %u do_csum %u)\n", i2400m, chunk, __chunk_len,
  622. addr, direct, do_csum);
  623. buf = i2400m->bm_cmd_buf;
  624. memcpy(buf->cmd_payload, chunk, __chunk_len);
  625. memset(buf->cmd_payload + __chunk_len, 0xad, chunk_len - __chunk_len);
  626. buf->cmd.command = i2400m_brh_command(I2400M_BRH_WRITE,
  627. __chunk_len & 0x3 ? 0 : do_csum,
  628. __chunk_len & 0xf ? 0 : direct);
  629. buf->cmd.target_addr = cpu_to_le32(addr);
  630. buf->cmd.data_size = cpu_to_le32(__chunk_len);
  631. ret = i2400m_bm_cmd(i2400m, &buf->cmd, sizeof(buf->cmd) + chunk_len,
  632. &ack, sizeof(ack), 0);
  633. if (ret >= 0)
  634. ret = 0;
  635. d_fnend(5, dev, "(i2400m %p chunk %p __chunk_len %zu addr 0x%08lx "
  636. "direct %u do_csum %u) = %d\n", i2400m, chunk, __chunk_len,
  637. addr, direct, do_csum, ret);
  638. return ret;
  639. }
  640. /*
  641. * Download a BCF file's sections to the device
  642. *
  643. * @i2400m: device descriptor
  644. * @bcf: pointer to firmware data (first header followed by the
  645. * payloads). Assumed verified and consistent.
  646. * @bcf_len: length (in bytes) of the @bcf buffer.
  647. *
  648. * Returns: < 0 errno code on error or the offset to the jump instruction.
  649. *
  650. * Given a BCF file, downloads each section (a command and a payload)
  651. * to the device's address space. Actually, it just executes each
  652. * command i the BCF file.
  653. *
  654. * The section size has to be aligned to 4 bytes AND the padding has
  655. * to be taken from the firmware file, as the signature takes it into
  656. * account.
  657. */
  658. static
  659. ssize_t i2400m_dnload_bcf(struct i2400m *i2400m,
  660. const struct i2400m_bcf_hdr *bcf, size_t bcf_len)
  661. {
  662. ssize_t ret;
  663. struct device *dev = i2400m_dev(i2400m);
  664. size_t offset, /* iterator offset */
  665. data_size, /* Size of the data payload */
  666. section_size, /* Size of the whole section (cmd + payload) */
  667. section = 1;
  668. const struct i2400m_bootrom_header *bh;
  669. struct i2400m_bootrom_header ack;
  670. d_fnstart(3, dev, "(i2400m %p bcf %p bcf_len %zu)\n",
  671. i2400m, bcf, bcf_len);
  672. /* Iterate over the command blocks in the BCF file that start
  673. * after the header */
  674. offset = le32_to_cpu(bcf->header_len) * sizeof(u32);
  675. while (1) { /* start sending the file */
  676. bh = (void *) bcf + offset;
  677. data_size = le32_to_cpu(bh->data_size);
  678. section_size = ALIGN(sizeof(*bh) + data_size, 4);
  679. d_printf(7, dev,
  680. "downloading section #%zu (@%zu %zu B) to 0x%08x\n",
  681. section, offset, sizeof(*bh) + data_size,
  682. le32_to_cpu(bh->target_addr));
  683. /*
  684. * We look for JUMP cmd from the bootmode header,
  685. * either I2400M_BRH_SIGNED_JUMP for secure boot
  686. * or I2400M_BRH_JUMP for unsecure boot, the last chunk
  687. * should be the bootmode header with JUMP cmd.
  688. */
  689. if (i2400m_brh_get_opcode(bh) == I2400M_BRH_SIGNED_JUMP ||
  690. i2400m_brh_get_opcode(bh) == I2400M_BRH_JUMP) {
  691. d_printf(5, dev, "jump found @%zu\n", offset);
  692. break;
  693. }
  694. if (offset + section_size > bcf_len) {
  695. dev_err(dev, "fw %s: bad section #%zu, "
  696. "end (@%zu) beyond EOF (@%zu)\n",
  697. i2400m->fw_name, section,
  698. offset + section_size, bcf_len);
  699. ret = -EINVAL;
  700. goto error_section_beyond_eof;
  701. }
  702. __i2400m_msleep(20);
  703. ret = i2400m_bm_cmd(i2400m, bh, section_size,
  704. &ack, sizeof(ack), I2400M_BM_CMD_RAW);
  705. if (ret < 0) {
  706. dev_err(dev, "fw %s: section #%zu (@%zu %zu B) "
  707. "failed %d\n", i2400m->fw_name, section,
  708. offset, sizeof(*bh) + data_size, (int) ret);
  709. goto error_send;
  710. }
  711. offset += section_size;
  712. section++;
  713. }
  714. ret = offset;
  715. error_section_beyond_eof:
  716. error_send:
  717. d_fnend(3, dev, "(i2400m %p bcf %p bcf_len %zu) = %d\n",
  718. i2400m, bcf, bcf_len, (int) ret);
  719. return ret;
  720. }
  721. /*
  722. * Indicate if the device emitted a reboot barker that indicates
  723. * "signed boot"
  724. */
  725. static
  726. unsigned i2400m_boot_is_signed(struct i2400m *i2400m)
  727. {
  728. return likely(i2400m->sboot);
  729. }
  730. /*
  731. * Do the final steps of uploading firmware
  732. *
  733. * @bcf_hdr: BCF header we are actually using
  734. * @bcf: pointer to the firmware image (which matches the first header
  735. * that is followed by the actual payloads).
  736. * @offset: [byte] offset into @bcf for the command we need to send.
  737. *
  738. * Depending on the boot mode (signed vs non-signed), different
  739. * actions need to be taken.
  740. */
  741. static
  742. int i2400m_dnload_finalize(struct i2400m *i2400m,
  743. const struct i2400m_bcf_hdr *bcf_hdr,
  744. const struct i2400m_bcf_hdr *bcf, size_t offset)
  745. {
  746. int ret = 0;
  747. struct device *dev = i2400m_dev(i2400m);
  748. struct i2400m_bootrom_header *cmd, ack;
  749. struct {
  750. struct i2400m_bootrom_header cmd;
  751. u8 cmd_pl[0];
  752. } __packed *cmd_buf;
  753. size_t signature_block_offset, signature_block_size;
  754. d_fnstart(3, dev, "offset %zu\n", offset);
  755. cmd = (void *) bcf + offset;
  756. if (i2400m_boot_is_signed(i2400m) == 0) {
  757. struct i2400m_bootrom_header jump_ack;
  758. d_printf(1, dev, "unsecure boot, jumping to 0x%08x\n",
  759. le32_to_cpu(cmd->target_addr));
  760. cmd_buf = i2400m->bm_cmd_buf;
  761. memcpy(&cmd_buf->cmd, cmd, sizeof(*cmd));
  762. cmd = &cmd_buf->cmd;
  763. /* now cmd points to the actual bootrom_header in cmd_buf */
  764. i2400m_brh_set_opcode(cmd, I2400M_BRH_JUMP);
  765. cmd->data_size = 0;
  766. ret = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
  767. &jump_ack, sizeof(jump_ack), 0);
  768. } else {
  769. d_printf(1, dev, "secure boot, jumping to 0x%08x\n",
  770. le32_to_cpu(cmd->target_addr));
  771. cmd_buf = i2400m->bm_cmd_buf;
  772. memcpy(&cmd_buf->cmd, cmd, sizeof(*cmd));
  773. signature_block_offset =
  774. sizeof(*bcf_hdr)
  775. + le32_to_cpu(bcf_hdr->key_size) * sizeof(u32)
  776. + le32_to_cpu(bcf_hdr->exponent_size) * sizeof(u32);
  777. signature_block_size =
  778. le32_to_cpu(bcf_hdr->modulus_size) * sizeof(u32);
  779. memcpy(cmd_buf->cmd_pl,
  780. (void *) bcf_hdr + signature_block_offset,
  781. signature_block_size);
  782. ret = i2400m_bm_cmd(i2400m, &cmd_buf->cmd,
  783. sizeof(cmd_buf->cmd) + signature_block_size,
  784. &ack, sizeof(ack), I2400M_BM_CMD_RAW);
  785. }
  786. d_fnend(3, dev, "returning %d\n", ret);
  787. return ret;
  788. }
  789. /**
  790. * i2400m_bootrom_init - Reboots a powered device into boot mode
  791. *
  792. * @i2400m: device descriptor
  793. * @flags:
  794. * I2400M_BRI_SOFT: a reboot barker has been seen
  795. * already, so don't wait for it.
  796. *
  797. * I2400M_BRI_NO_REBOOT: Don't send a reboot command, but wait
  798. * for a reboot barker notification. This is a one shot; if
  799. * the state machine needs to send a reboot command it will.
  800. *
  801. * Returns:
  802. *
  803. * < 0 errno code on error, 0 if ok.
  804. *
  805. * Description:
  806. *
  807. * Tries hard enough to put the device in boot-mode. There are two
  808. * main phases to this:
  809. *
  810. * a. (1) send a reboot command and (2) get a reboot barker
  811. *
  812. * b. (1) echo/ack the reboot sending the reboot barker back and (2)
  813. * getting an ack barker in return
  814. *
  815. * We want to skip (a) in some cases [soft]. The state machine is
  816. * horrible, but it is basically: on each phase, send what has to be
  817. * sent (if any), wait for the answer and act on the answer. We might
  818. * have to backtrack and retry, so we keep a max tries counter for
  819. * that.
  820. *
  821. * It sucks because we don't know ahead of time which is going to be
  822. * the reboot barker (the device might send different ones depending
  823. * on its EEPROM config) and once the device reboots and waits for the
  824. * echo/ack reboot barker being sent back, it doesn't understand
  825. * anything else. So we can be left at the point where we don't know
  826. * what to send to it -- cold reset and bus reset seem to have little
  827. * effect. So the function iterates (in this case) through all the
  828. * known barkers and tries them all until an ACK is
  829. * received. Otherwise, it gives up.
  830. *
  831. * If we get a timeout after sending a warm reset, we do it again.
  832. */
  833. int i2400m_bootrom_init(struct i2400m *i2400m, enum i2400m_bri flags)
  834. {
  835. int result;
  836. struct device *dev = i2400m_dev(i2400m);
  837. struct i2400m_bootrom_header *cmd;
  838. struct i2400m_bootrom_header ack;
  839. int count = i2400m->bus_bm_retries;
  840. int ack_timeout_cnt = 1;
  841. unsigned i;
  842. BUILD_BUG_ON(sizeof(*cmd) != sizeof(i2400m_barker_db[0].data));
  843. BUILD_BUG_ON(sizeof(ack) != sizeof(i2400m_ACK_BARKER));
  844. d_fnstart(4, dev, "(i2400m %p flags 0x%08x)\n", i2400m, flags);
  845. result = -ENOMEM;
  846. cmd = i2400m->bm_cmd_buf;
  847. if (flags & I2400M_BRI_SOFT)
  848. goto do_reboot_ack;
  849. do_reboot:
  850. ack_timeout_cnt = 1;
  851. if (--count < 0)
  852. goto error_timeout;
  853. d_printf(4, dev, "device reboot: reboot command [%d # left]\n",
  854. count);
  855. if ((flags & I2400M_BRI_NO_REBOOT) == 0)
  856. i2400m_reset(i2400m, I2400M_RT_WARM);
  857. result = i2400m_bm_cmd(i2400m, NULL, 0, &ack, sizeof(ack),
  858. I2400M_BM_CMD_RAW);
  859. flags &= ~I2400M_BRI_NO_REBOOT;
  860. switch (result) {
  861. case -ERESTARTSYS:
  862. /*
  863. * at this point, i2400m_bm_cmd(), through
  864. * __i2400m_bm_ack_process(), has updated
  865. * i2400m->barker and we are good to go.
  866. */
  867. d_printf(4, dev, "device reboot: got reboot barker\n");
  868. break;
  869. case -EISCONN: /* we don't know how it got here...but we follow it */
  870. d_printf(4, dev, "device reboot: got ack barker - whatever\n");
  871. goto do_reboot;
  872. case -ETIMEDOUT:
  873. /*
  874. * Device has timed out, we might be in boot mode
  875. * already and expecting an ack; if we don't know what
  876. * the barker is, we just send them all. Cold reset
  877. * and bus reset don't work. Beats me.
  878. */
  879. if (i2400m->barker != NULL) {
  880. dev_err(dev, "device boot: reboot barker timed out, "
  881. "trying (set) %08x echo/ack\n",
  882. le32_to_cpu(i2400m->barker->data[0]));
  883. goto do_reboot_ack;
  884. }
  885. for (i = 0; i < i2400m_barker_db_used; i++) {
  886. struct i2400m_barker_db *barker = &i2400m_barker_db[i];
  887. memcpy(cmd, barker->data, sizeof(barker->data));
  888. result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
  889. &ack, sizeof(ack),
  890. I2400M_BM_CMD_RAW);
  891. if (result == -EISCONN) {
  892. dev_warn(dev, "device boot: got ack barker "
  893. "after sending echo/ack barker "
  894. "#%d/%08x; rebooting j.i.c.\n",
  895. i, le32_to_cpu(barker->data[0]));
  896. flags &= ~I2400M_BRI_NO_REBOOT;
  897. goto do_reboot;
  898. }
  899. }
  900. dev_err(dev, "device boot: tried all the echo/acks, could "
  901. "not get device to respond; giving up");
  902. result = -ESHUTDOWN;
  903. case -EPROTO:
  904. case -ESHUTDOWN: /* dev is gone */
  905. case -EINTR: /* user cancelled */
  906. goto error_dev_gone;
  907. default:
  908. dev_err(dev, "device reboot: error %d while waiting "
  909. "for reboot barker - rebooting\n", result);
  910. d_dump(1, dev, &ack, result);
  911. goto do_reboot;
  912. }
  913. /* At this point we ack back with 4 REBOOT barkers and expect
  914. * 4 ACK barkers. This is ugly, as we send a raw command --
  915. * hence the cast. _bm_cmd() will catch the reboot ack
  916. * notification and report it as -EISCONN. */
  917. do_reboot_ack:
  918. d_printf(4, dev, "device reboot ack: sending ack [%d # left]\n", count);
  919. memcpy(cmd, i2400m->barker->data, sizeof(i2400m->barker->data));
  920. result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
  921. &ack, sizeof(ack), I2400M_BM_CMD_RAW);
  922. switch (result) {
  923. case -ERESTARTSYS:
  924. d_printf(4, dev, "reboot ack: got reboot barker - retrying\n");
  925. if (--count < 0)
  926. goto error_timeout;
  927. goto do_reboot_ack;
  928. case -EISCONN:
  929. d_printf(4, dev, "reboot ack: got ack barker - good\n");
  930. break;
  931. case -ETIMEDOUT: /* no response, maybe it is the other type? */
  932. if (ack_timeout_cnt-- < 0) {
  933. d_printf(4, dev, "reboot ack timedout: retrying\n");
  934. goto do_reboot_ack;
  935. } else {
  936. dev_err(dev, "reboot ack timedout too long: "
  937. "trying reboot\n");
  938. goto do_reboot;
  939. }
  940. break;
  941. case -EPROTO:
  942. case -ESHUTDOWN: /* dev is gone */
  943. goto error_dev_gone;
  944. default:
  945. dev_err(dev, "device reboot ack: error %d while waiting for "
  946. "reboot ack barker - rebooting\n", result);
  947. goto do_reboot;
  948. }
  949. d_printf(2, dev, "device reboot ack: got ack barker - boot done\n");
  950. result = 0;
  951. exit_timeout:
  952. error_dev_gone:
  953. d_fnend(4, dev, "(i2400m %p flags 0x%08x) = %d\n",
  954. i2400m, flags, result);
  955. return result;
  956. error_timeout:
  957. dev_err(dev, "Timed out waiting for reboot ack\n");
  958. result = -ETIMEDOUT;
  959. goto exit_timeout;
  960. }
  961. /*
  962. * Read the MAC addr
  963. *
  964. * The position this function reads is fixed in device memory and
  965. * always available, even without firmware.
  966. *
  967. * Note we specify we want to read only six bytes, but provide space
  968. * for 16, as we always get it rounded up.
  969. */
  970. int i2400m_read_mac_addr(struct i2400m *i2400m)
  971. {
  972. int result;
  973. struct device *dev = i2400m_dev(i2400m);
  974. struct net_device *net_dev = i2400m->wimax_dev.net_dev;
  975. struct i2400m_bootrom_header *cmd;
  976. struct {
  977. struct i2400m_bootrom_header ack;
  978. u8 ack_pl[16];
  979. } __packed ack_buf;
  980. d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
  981. cmd = i2400m->bm_cmd_buf;
  982. cmd->command = i2400m_brh_command(I2400M_BRH_READ, 0, 1);
  983. cmd->target_addr = cpu_to_le32(0x00203fe8);
  984. cmd->data_size = cpu_to_le32(6);
  985. result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
  986. &ack_buf.ack, sizeof(ack_buf), 0);
  987. if (result < 0) {
  988. dev_err(dev, "BM: read mac addr failed: %d\n", result);
  989. goto error_read_mac;
  990. }
  991. d_printf(2, dev, "mac addr is %pM\n", ack_buf.ack_pl);
  992. if (i2400m->bus_bm_mac_addr_impaired == 1) {
  993. ack_buf.ack_pl[0] = 0x00;
  994. ack_buf.ack_pl[1] = 0x16;
  995. ack_buf.ack_pl[2] = 0xd3;
  996. get_random_bytes(&ack_buf.ack_pl[3], 3);
  997. dev_err(dev, "BM is MAC addr impaired, faking MAC addr to "
  998. "mac addr is %pM\n", ack_buf.ack_pl);
  999. result = 0;
  1000. }
  1001. net_dev->addr_len = ETH_ALEN;
  1002. memcpy(net_dev->dev_addr, ack_buf.ack_pl, ETH_ALEN);
  1003. error_read_mac:
  1004. d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, result);
  1005. return result;
  1006. }
  1007. /*
  1008. * Initialize a non signed boot
  1009. *
  1010. * This implies sending some magic values to the device's memory. Note
  1011. * we convert the values to little endian in the same array
  1012. * declaration.
  1013. */
  1014. static
  1015. int i2400m_dnload_init_nonsigned(struct i2400m *i2400m)
  1016. {
  1017. unsigned i = 0;
  1018. int ret = 0;
  1019. struct device *dev = i2400m_dev(i2400m);
  1020. d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
  1021. if (i2400m->bus_bm_pokes_table) {
  1022. while (i2400m->bus_bm_pokes_table[i].address) {
  1023. ret = i2400m_download_chunk(
  1024. i2400m,
  1025. &i2400m->bus_bm_pokes_table[i].data,
  1026. sizeof(i2400m->bus_bm_pokes_table[i].data),
  1027. i2400m->bus_bm_pokes_table[i].address, 1, 1);
  1028. if (ret < 0)
  1029. break;
  1030. i++;
  1031. }
  1032. }
  1033. d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, ret);
  1034. return ret;
  1035. }
  1036. /*
  1037. * Initialize the signed boot process
  1038. *
  1039. * @i2400m: device descriptor
  1040. *
  1041. * @bcf_hdr: pointer to the firmware header; assumes it is fully in
  1042. * memory (it has gone through basic validation).
  1043. *
  1044. * Returns: 0 if ok, < 0 errno code on error, -ERESTARTSYS if the hw
  1045. * rebooted.
  1046. *
  1047. * This writes the firmware BCF header to the device using the
  1048. * HASH_PAYLOAD_ONLY command.
  1049. */
  1050. static
  1051. int i2400m_dnload_init_signed(struct i2400m *i2400m,
  1052. const struct i2400m_bcf_hdr *bcf_hdr)
  1053. {
  1054. int ret;
  1055. struct device *dev = i2400m_dev(i2400m);
  1056. struct {
  1057. struct i2400m_bootrom_header cmd;
  1058. struct i2400m_bcf_hdr cmd_pl;
  1059. } __packed *cmd_buf;
  1060. struct i2400m_bootrom_header ack;
  1061. d_fnstart(5, dev, "(i2400m %p bcf_hdr %p)\n", i2400m, bcf_hdr);
  1062. cmd_buf = i2400m->bm_cmd_buf;
  1063. cmd_buf->cmd.command =
  1064. i2400m_brh_command(I2400M_BRH_HASH_PAYLOAD_ONLY, 0, 0);
  1065. cmd_buf->cmd.target_addr = 0;
  1066. cmd_buf->cmd.data_size = cpu_to_le32(sizeof(cmd_buf->cmd_pl));
  1067. memcpy(&cmd_buf->cmd_pl, bcf_hdr, sizeof(*bcf_hdr));
  1068. ret = i2400m_bm_cmd(i2400m, &cmd_buf->cmd, sizeof(*cmd_buf),
  1069. &ack, sizeof(ack), 0);
  1070. if (ret >= 0)
  1071. ret = 0;
  1072. d_fnend(5, dev, "(i2400m %p bcf_hdr %p) = %d\n", i2400m, bcf_hdr, ret);
  1073. return ret;
  1074. }
  1075. /*
  1076. * Initialize the firmware download at the device size
  1077. *
  1078. * Multiplex to the one that matters based on the device's mode
  1079. * (signed or non-signed).
  1080. */
  1081. static
  1082. int i2400m_dnload_init(struct i2400m *i2400m,
  1083. const struct i2400m_bcf_hdr *bcf_hdr)
  1084. {
  1085. int result;
  1086. struct device *dev = i2400m_dev(i2400m);
  1087. if (i2400m_boot_is_signed(i2400m)) {
  1088. d_printf(1, dev, "signed boot\n");
  1089. result = i2400m_dnload_init_signed(i2400m, bcf_hdr);
  1090. if (result == -ERESTARTSYS)
  1091. return result;
  1092. if (result < 0)
  1093. dev_err(dev, "firmware %s: signed boot download "
  1094. "initialization failed: %d\n",
  1095. i2400m->fw_name, result);
  1096. } else {
  1097. /* non-signed boot process without pokes */
  1098. d_printf(1, dev, "non-signed boot\n");
  1099. result = i2400m_dnload_init_nonsigned(i2400m);
  1100. if (result == -ERESTARTSYS)
  1101. return result;
  1102. if (result < 0)
  1103. dev_err(dev, "firmware %s: non-signed download "
  1104. "initialization failed: %d\n",
  1105. i2400m->fw_name, result);
  1106. }
  1107. return result;
  1108. }
  1109. /*
  1110. * Run consistency tests on the firmware file and load up headers
  1111. *
  1112. * Check for the firmware being made for the i2400m device,
  1113. * etc...These checks are mostly informative, as the device will make
  1114. * them too; but the driver's response is more informative on what
  1115. * went wrong.
  1116. *
  1117. * This will also look at all the headers present on the firmware
  1118. * file, and update i2400m->fw_bcf_hdr to point to them.
  1119. */
  1120. static
  1121. int i2400m_fw_hdr_check(struct i2400m *i2400m,
  1122. const struct i2400m_bcf_hdr *bcf_hdr,
  1123. size_t index, size_t offset)
  1124. {
  1125. struct device *dev = i2400m_dev(i2400m);
  1126. unsigned module_type, header_len, major_version, minor_version,
  1127. module_id, module_vendor, date, size;
  1128. module_type = le32_to_cpu(bcf_hdr->module_type);
  1129. header_len = sizeof(u32) * le32_to_cpu(bcf_hdr->header_len);
  1130. major_version = (le32_to_cpu(bcf_hdr->header_version) & 0xffff0000)
  1131. >> 16;
  1132. minor_version = le32_to_cpu(bcf_hdr->header_version) & 0x0000ffff;
  1133. module_id = le32_to_cpu(bcf_hdr->module_id);
  1134. module_vendor = le32_to_cpu(bcf_hdr->module_vendor);
  1135. date = le32_to_cpu(bcf_hdr->date);
  1136. size = sizeof(u32) * le32_to_cpu(bcf_hdr->size);
  1137. d_printf(1, dev, "firmware %s #%zd@%08zx: BCF header "
  1138. "type:vendor:id 0x%x:%x:%x v%u.%u (%u/%u B) built %08x\n",
  1139. i2400m->fw_name, index, offset,
  1140. module_type, module_vendor, module_id,
  1141. major_version, minor_version, header_len, size, date);
  1142. /* Hard errors */
  1143. if (major_version != 1) {
  1144. dev_err(dev, "firmware %s #%zd@%08zx: major header version "
  1145. "v%u.%u not supported\n",
  1146. i2400m->fw_name, index, offset,
  1147. major_version, minor_version);
  1148. return -EBADF;
  1149. }
  1150. if (module_type != 6) { /* built for the right hardware? */
  1151. dev_err(dev, "firmware %s #%zd@%08zx: unexpected module "
  1152. "type 0x%x; aborting\n",
  1153. i2400m->fw_name, index, offset,
  1154. module_type);
  1155. return -EBADF;
  1156. }
  1157. if (module_vendor != 0x8086) {
  1158. dev_err(dev, "firmware %s #%zd@%08zx: unexpected module "
  1159. "vendor 0x%x; aborting\n",
  1160. i2400m->fw_name, index, offset, module_vendor);
  1161. return -EBADF;
  1162. }
  1163. if (date < 0x20080300)
  1164. dev_warn(dev, "firmware %s #%zd@%08zx: build date %08x "
  1165. "too old; unsupported\n",
  1166. i2400m->fw_name, index, offset, date);
  1167. return 0;
  1168. }
  1169. /*
  1170. * Run consistency tests on the firmware file and load up headers
  1171. *
  1172. * Check for the firmware being made for the i2400m device,
  1173. * etc...These checks are mostly informative, as the device will make
  1174. * them too; but the driver's response is more informative on what
  1175. * went wrong.
  1176. *
  1177. * This will also look at all the headers present on the firmware
  1178. * file, and update i2400m->fw_hdrs to point to them.
  1179. */
  1180. static
  1181. int i2400m_fw_check(struct i2400m *i2400m, const void *bcf, size_t bcf_size)
  1182. {
  1183. int result;
  1184. struct device *dev = i2400m_dev(i2400m);
  1185. size_t headers = 0;
  1186. const struct i2400m_bcf_hdr *bcf_hdr;
  1187. const void *itr, *next, *top;
  1188. size_t slots = 0, used_slots = 0;
  1189. for (itr = bcf, top = itr + bcf_size;
  1190. itr < top;
  1191. headers++, itr = next) {
  1192. size_t leftover, offset, header_len, size;
  1193. leftover = top - itr;
  1194. offset = itr - bcf;
  1195. if (leftover <= sizeof(*bcf_hdr)) {
  1196. dev_err(dev, "firmware %s: %zu B left at @%zx, "
  1197. "not enough for BCF header\n",
  1198. i2400m->fw_name, leftover, offset);
  1199. break;
  1200. }
  1201. bcf_hdr = itr;
  1202. /* Only the first header is supposed to be followed by
  1203. * payload */
  1204. header_len = sizeof(u32) * le32_to_cpu(bcf_hdr->header_len);
  1205. size = sizeof(u32) * le32_to_cpu(bcf_hdr->size);
  1206. if (headers == 0)
  1207. next = itr + size;
  1208. else
  1209. next = itr + header_len;
  1210. result = i2400m_fw_hdr_check(i2400m, bcf_hdr, headers, offset);
  1211. if (result < 0)
  1212. continue;
  1213. if (used_slots + 1 >= slots) {
  1214. /* +1 -> we need to account for the one we'll
  1215. * occupy and at least an extra one for
  1216. * always being NULL */
  1217. result = i2400m_zrealloc_2x(
  1218. (void **) &i2400m->fw_hdrs, &slots,
  1219. sizeof(i2400m->fw_hdrs[0]),
  1220. GFP_KERNEL);
  1221. if (result < 0)
  1222. goto error_zrealloc;
  1223. }
  1224. i2400m->fw_hdrs[used_slots] = bcf_hdr;
  1225. used_slots++;
  1226. }
  1227. if (headers == 0) {
  1228. dev_err(dev, "firmware %s: no usable headers found\n",
  1229. i2400m->fw_name);
  1230. result = -EBADF;
  1231. } else
  1232. result = 0;
  1233. error_zrealloc:
  1234. return result;
  1235. }
  1236. /*
  1237. * Match a barker to a BCF header module ID
  1238. *
  1239. * The device sends a barker which tells the firmware loader which
  1240. * header in the BCF file has to be used. This does the matching.
  1241. */
  1242. static
  1243. unsigned i2400m_bcf_hdr_match(struct i2400m *i2400m,
  1244. const struct i2400m_bcf_hdr *bcf_hdr)
  1245. {
  1246. u32 barker = le32_to_cpu(i2400m->barker->data[0])
  1247. & 0x7fffffff;
  1248. u32 module_id = le32_to_cpu(bcf_hdr->module_id)
  1249. & 0x7fffffff; /* high bit used for something else */
  1250. /* special case for 5x50 */
  1251. if (barker == I2400M_SBOOT_BARKER && module_id == 0)
  1252. return 1;
  1253. if (module_id == barker)
  1254. return 1;
  1255. return 0;
  1256. }
  1257. static
  1258. const struct i2400m_bcf_hdr *i2400m_bcf_hdr_find(struct i2400m *i2400m)
  1259. {
  1260. struct device *dev = i2400m_dev(i2400m);
  1261. const struct i2400m_bcf_hdr **bcf_itr, *bcf_hdr;
  1262. unsigned i = 0;
  1263. u32 barker = le32_to_cpu(i2400m->barker->data[0]);
  1264. d_printf(2, dev, "finding BCF header for barker %08x\n", barker);
  1265. if (barker == I2400M_NBOOT_BARKER) {
  1266. bcf_hdr = i2400m->fw_hdrs[0];
  1267. d_printf(1, dev, "using BCF header #%u/%08x for non-signed "
  1268. "barker\n", 0, le32_to_cpu(bcf_hdr->module_id));
  1269. return bcf_hdr;
  1270. }
  1271. for (bcf_itr = i2400m->fw_hdrs; *bcf_itr != NULL; bcf_itr++, i++) {
  1272. bcf_hdr = *bcf_itr;
  1273. if (i2400m_bcf_hdr_match(i2400m, bcf_hdr)) {
  1274. d_printf(1, dev, "hit on BCF hdr #%u/%08x\n",
  1275. i, le32_to_cpu(bcf_hdr->module_id));
  1276. return bcf_hdr;
  1277. } else
  1278. d_printf(1, dev, "miss on BCF hdr #%u/%08x\n",
  1279. i, le32_to_cpu(bcf_hdr->module_id));
  1280. }
  1281. dev_err(dev, "cannot find a matching BCF header for barker %08x\n",
  1282. barker);
  1283. return NULL;
  1284. }
  1285. /*
  1286. * Download the firmware to the device
  1287. *
  1288. * @i2400m: device descriptor
  1289. * @bcf: pointer to loaded (and minimally verified for consistency)
  1290. * firmware
  1291. * @bcf_size: size of the @bcf buffer (header plus payloads)
  1292. *
  1293. * The process for doing this is described in this file's header.
  1294. *
  1295. * Note we only reinitialize boot-mode if the flags say so. Some hw
  1296. * iterations need it, some don't. In any case, if we loop, we always
  1297. * need to reinitialize the boot room, hence the flags modification.
  1298. */
  1299. static
  1300. int i2400m_fw_dnload(struct i2400m *i2400m, const struct i2400m_bcf_hdr *bcf,
  1301. size_t fw_size, enum i2400m_bri flags)
  1302. {
  1303. int ret = 0;
  1304. struct device *dev = i2400m_dev(i2400m);
  1305. int count = i2400m->bus_bm_retries;
  1306. const struct i2400m_bcf_hdr *bcf_hdr;
  1307. size_t bcf_size;
  1308. d_fnstart(5, dev, "(i2400m %p bcf %p fw size %zu)\n",
  1309. i2400m, bcf, fw_size);
  1310. i2400m->boot_mode = 1;
  1311. wmb(); /* Make sure other readers see it */
  1312. hw_reboot:
  1313. if (count-- == 0) {
  1314. ret = -ERESTARTSYS;
  1315. dev_err(dev, "device rebooted too many times, aborting\n");
  1316. goto error_too_many_reboots;
  1317. }
  1318. if (flags & I2400M_BRI_MAC_REINIT) {
  1319. ret = i2400m_bootrom_init(i2400m, flags);
  1320. if (ret < 0) {
  1321. dev_err(dev, "bootrom init failed: %d\n", ret);
  1322. goto error_bootrom_init;
  1323. }
  1324. }
  1325. flags |= I2400M_BRI_MAC_REINIT;
  1326. /*
  1327. * Initialize the download, push the bytes to the device and
  1328. * then jump to the new firmware. Note @ret is passed with the
  1329. * offset of the jump instruction to _dnload_finalize()
  1330. *
  1331. * Note we need to use the BCF header in the firmware image
  1332. * that matches the barker that the device sent when it
  1333. * rebooted, so it has to be passed along.
  1334. */
  1335. ret = -EBADF;
  1336. bcf_hdr = i2400m_bcf_hdr_find(i2400m);
  1337. if (bcf_hdr == NULL)
  1338. goto error_bcf_hdr_find;
  1339. ret = i2400m_dnload_init(i2400m, bcf_hdr);
  1340. if (ret == -ERESTARTSYS)
  1341. goto error_dev_rebooted;
  1342. if (ret < 0)
  1343. goto error_dnload_init;
  1344. /*
  1345. * bcf_size refers to one header size plus the fw sections size
  1346. * indicated by the header,ie. if there are other extended headers
  1347. * at the tail, they are not counted
  1348. */
  1349. bcf_size = sizeof(u32) * le32_to_cpu(bcf_hdr->size);
  1350. ret = i2400m_dnload_bcf(i2400m, bcf, bcf_size);
  1351. if (ret == -ERESTARTSYS)
  1352. goto error_dev_rebooted;
  1353. if (ret < 0) {
  1354. dev_err(dev, "fw %s: download failed: %d\n",
  1355. i2400m->fw_name, ret);
  1356. goto error_dnload_bcf;
  1357. }
  1358. ret = i2400m_dnload_finalize(i2400m, bcf_hdr, bcf, ret);
  1359. if (ret == -ERESTARTSYS)
  1360. goto error_dev_rebooted;
  1361. if (ret < 0) {
  1362. dev_err(dev, "fw %s: "
  1363. "download finalization failed: %d\n",
  1364. i2400m->fw_name, ret);
  1365. goto error_dnload_finalize;
  1366. }
  1367. d_printf(2, dev, "fw %s successfully uploaded\n",
  1368. i2400m->fw_name);
  1369. i2400m->boot_mode = 0;
  1370. wmb(); /* Make sure i2400m_msg_to_dev() sees boot_mode */
  1371. error_dnload_finalize:
  1372. error_dnload_bcf:
  1373. error_dnload_init:
  1374. error_bcf_hdr_find:
  1375. error_bootrom_init:
  1376. error_too_many_reboots:
  1377. d_fnend(5, dev, "(i2400m %p bcf %p size %zu) = %d\n",
  1378. i2400m, bcf, fw_size, ret);
  1379. return ret;
  1380. error_dev_rebooted:
  1381. dev_err(dev, "device rebooted, %d tries left\n", count);
  1382. /* we got the notification already, no need to wait for it again */
  1383. flags |= I2400M_BRI_SOFT;
  1384. goto hw_reboot;
  1385. }
  1386. static
  1387. int i2400m_fw_bootstrap(struct i2400m *i2400m, const struct firmware *fw,
  1388. enum i2400m_bri flags)
  1389. {
  1390. int ret;
  1391. struct device *dev = i2400m_dev(i2400m);
  1392. const struct i2400m_bcf_hdr *bcf; /* Firmware data */
  1393. d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
  1394. bcf = (void *) fw->data;
  1395. ret = i2400m_fw_check(i2400m, bcf, fw->size);
  1396. if (ret >= 0)
  1397. ret = i2400m_fw_dnload(i2400m, bcf, fw->size, flags);
  1398. if (ret < 0)
  1399. dev_err(dev, "%s: cannot use: %d, skipping\n",
  1400. i2400m->fw_name, ret);
  1401. kfree(i2400m->fw_hdrs);
  1402. i2400m->fw_hdrs = NULL;
  1403. d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, ret);
  1404. return ret;
  1405. }
  1406. /* Refcounted container for firmware data */
  1407. struct i2400m_fw {
  1408. struct kref kref;
  1409. const struct firmware *fw;
  1410. };
  1411. static
  1412. void i2400m_fw_destroy(struct kref *kref)
  1413. {
  1414. struct i2400m_fw *i2400m_fw =
  1415. container_of(kref, struct i2400m_fw, kref);
  1416. release_firmware(i2400m_fw->fw);
  1417. kfree(i2400m_fw);
  1418. }
  1419. static
  1420. struct i2400m_fw *i2400m_fw_get(struct i2400m_fw *i2400m_fw)
  1421. {
  1422. if (i2400m_fw != NULL && i2400m_fw != (void *) ~0)
  1423. kref_get(&i2400m_fw->kref);
  1424. return i2400m_fw;
  1425. }
  1426. static
  1427. void i2400m_fw_put(struct i2400m_fw *i2400m_fw)
  1428. {
  1429. kref_put(&i2400m_fw->kref, i2400m_fw_destroy);
  1430. }
  1431. /**
  1432. * i2400m_dev_bootstrap - Bring the device to a known state and upload firmware
  1433. *
  1434. * @i2400m: device descriptor
  1435. *
  1436. * Returns: >= 0 if ok, < 0 errno code on error.
  1437. *
  1438. * This sets up the firmware upload environment, loads the firmware
  1439. * file from disk, verifies and then calls the firmware upload process
  1440. * per se.
  1441. *
  1442. * Can be called either from probe, or after a warm reset. Can not be
  1443. * called from within an interrupt. All the flow in this code is
  1444. * single-threade; all I/Os are synchronous.
  1445. */
  1446. int i2400m_dev_bootstrap(struct i2400m *i2400m, enum i2400m_bri flags)
  1447. {
  1448. int ret, itr;
  1449. struct device *dev = i2400m_dev(i2400m);
  1450. struct i2400m_fw *i2400m_fw;
  1451. const struct firmware *fw;
  1452. const char *fw_name;
  1453. d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
  1454. ret = -ENODEV;
  1455. spin_lock(&i2400m->rx_lock);
  1456. i2400m_fw = i2400m_fw_get(i2400m->fw_cached);
  1457. spin_unlock(&i2400m->rx_lock);
  1458. if (i2400m_fw == (void *) ~0) {
  1459. dev_err(dev, "can't load firmware now!");
  1460. goto out;
  1461. } else if (i2400m_fw != NULL) {
  1462. dev_info(dev, "firmware %s: loading from cache\n",
  1463. i2400m->fw_name);
  1464. ret = i2400m_fw_bootstrap(i2400m, i2400m_fw->fw, flags);
  1465. i2400m_fw_put(i2400m_fw);
  1466. goto out;
  1467. }
  1468. /* Load firmware files to memory. */
  1469. for (itr = 0, ret = -ENOENT; ; itr++) {
  1470. fw_name = i2400m->bus_fw_names[itr];
  1471. if (fw_name == NULL) {
  1472. dev_err(dev, "Could not find a usable firmware image\n");
  1473. break;
  1474. }
  1475. d_printf(1, dev, "trying firmware %s (%d)\n", fw_name, itr);
  1476. ret = request_firmware(&fw, fw_name, dev);
  1477. if (ret)
  1478. continue;
  1479. i2400m->fw_name = fw_name;
  1480. ret = i2400m_fw_bootstrap(i2400m, fw, flags);
  1481. release_firmware(fw);
  1482. if (ret >= 0) /* firmware loaded successfully */
  1483. break;
  1484. i2400m->fw_name = NULL;
  1485. }
  1486. out:
  1487. d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, ret);
  1488. return ret;
  1489. }
  1490. EXPORT_SYMBOL_GPL(i2400m_dev_bootstrap);
  1491. void i2400m_fw_cache(struct i2400m *i2400m)
  1492. {
  1493. int result;
  1494. struct i2400m_fw *i2400m_fw;
  1495. struct device *dev = i2400m_dev(i2400m);
  1496. /* if there is anything there, free it -- now, this'd be weird */
  1497. spin_lock(&i2400m->rx_lock);
  1498. i2400m_fw = i2400m->fw_cached;
  1499. spin_unlock(&i2400m->rx_lock);
  1500. if (i2400m_fw != NULL && i2400m_fw != (void *) ~0) {
  1501. i2400m_fw_put(i2400m_fw);
  1502. WARN(1, "%s:%u: still cached fw still present?\n",
  1503. __func__, __LINE__);
  1504. }
  1505. if (i2400m->fw_name == NULL) {
  1506. dev_err(dev, "firmware n/a: can't cache\n");
  1507. i2400m_fw = (void *) ~0;
  1508. goto out;
  1509. }
  1510. i2400m_fw = kzalloc(sizeof(*i2400m_fw), GFP_ATOMIC);
  1511. if (i2400m_fw == NULL)
  1512. goto out;
  1513. kref_init(&i2400m_fw->kref);
  1514. result = request_firmware(&i2400m_fw->fw, i2400m->fw_name, dev);
  1515. if (result < 0) {
  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. }