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