fw.c 50 KB

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