fw.c 50 KB

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