fw.c 50 KB

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