fw.c 50 KB

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