2 * Intel Wireless WiMAX Connection 2400m
6 * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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.
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
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.
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.
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.
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.
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.
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.
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.
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()].
87 * Once firmware is uploaded, we are good to go :)
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.
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.
101 * BACKEND IMPLEMENTATION
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)
110 * i2400m_barker_db_init Called by i2400m_driver_init()
111 * i2400m_barker_db_add
113 * i2400m_barker_db_exit Called by i2400m_driver_exit()
115 * i2400m_dev_bootstrap Called by __i2400m_dev_start()
118 * i2400m_fw_hdr_check
123 * i2400m_bootrom_init
127 * i2400m_dnload_init_signed
128 * i2400m_dnload_init_nonsigned
129 * i2400m_download_chunk
133 * i2400m_dnload_finalize
137 * i2400m->bus_bm_cmd_send()
138 * i2400m->bus_bm_wait_for_ack
139 * __i2400m_bm_ack_verify
140 * i2400m_is_boot_barker
142 * i2400m_bm_cmd_prepare Used by bus-drivers to prep
143 * commands before sending
145 #include <linux/firmware.h>
146 #include <linux/sched.h>
147 #include <linux/usb.h>
151 #define D_SUBMODULE fw
152 #include "debug-levels.h"
155 static const __le32 i2400m_ACK_BARKER[4] = {
156 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)
164 * Prepare a boot-mode command for delivery
166 * @cmd: pointer to bootrom header to prepare
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.
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.
175 void i2400m_bm_cmd_prepare(struct i2400m_bootrom_header *cmd)
177 if (i2400m_brh_get_use_checksum(cmd)) {
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);
187 EXPORT_SYMBOL_GPL(i2400m_bm_cmd_prepare);
191 * Database of known barkers.
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.
198 static struct i2400m_barker_db {
201 static size_t i2400m_barker_db_used, i2400m_barker_db_size;
205 int i2400m_zrealloc_2x(void **ptr, size_t *_count, size_t el_size,
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);
214 /* zero the other half or the whole thing if old_count
217 memset(nptr, 0, new_size);
219 memset(nptr + old_size, 0, old_size);
229 * Add a barker to the database
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.
235 int i2400m_barker_db_add(u32 barker_id)
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);
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);
256 void i2400m_barker_db_exit(void)
258 kfree(i2400m_barker_db);
259 i2400m_barker_db = NULL;
260 i2400m_barker_db_size = 0;
261 i2400m_barker_db_used = 0;
266 * Helper function to add all the known stable barkers to the barker
270 int i2400m_barker_db_known_barkers(void)
274 result = i2400m_barker_db_add(I2400M_NBOOT_BARKER);
277 result = i2400m_barker_db_add(I2400M_SBOOT_BARKER);
286 * Initialize the barker database
288 * This can only be used from the module_init function for this
289 * module; this is to avoid the need to do locking.
291 * @options: command line argument with extra barkers to
292 * recognize. This is a comma-separated list of 32-bit hex
293 * numbers. They are appended to the existing list. Setting 0
294 * cleans the existing list and starts a new one.
296 int i2400m_barker_db_init(const char *_options)
299 char *options = NULL, *options_orig, *token;
301 i2400m_barker_db = NULL;
302 i2400m_barker_db_size = 0;
303 i2400m_barker_db_used = 0;
305 result = i2400m_barker_db_known_barkers();
308 /* parse command line options from i2400m.barkers */
309 if (_options != NULL) {
312 options_orig = kstrdup(_options, GFP_KERNEL);
313 if (options_orig == NULL)
315 options = options_orig;
317 while ((token = strsep(&options, ",")) != NULL) {
318 if (*token == '\0') /* eat joint commas */
320 if (sscanf(token, "%x", &barker) != 1
321 || barker > 0xffffffff) {
322 printk(KERN_ERR "%s: can't recognize "
323 "i2400m.barkers value '%s' as "
330 /* clean list and start new */
331 i2400m_barker_db_exit();
334 result = i2400m_barker_db_add(barker);
344 kfree(i2400m_barker_db);
350 * Recognize a boot barker
352 * @buf: buffer where the boot barker.
353 * @buf_size: size of the buffer (has to be 16 bytes). It is passed
354 * here so the function can check it for the caller.
356 * Note that as a side effect, upon identifying the obtained boot
357 * barker, this function will set i2400m->barker to point to the right
358 * barker database entry. Subsequent calls to the function will result
359 * in verifying that the same type of boot barker is returned when the
360 * device [re]boots (as long as the same device instance is used).
362 * Return: 0 if @buf matches a known boot barker. -ENOENT if the
363 * buffer in @buf doesn't match any boot barker in the database or
364 * -EILSEQ if the buffer doesn't have the right size.
366 int i2400m_is_boot_barker(struct i2400m *i2400m,
367 const void *buf, size_t buf_size)
370 struct device *dev = i2400m_dev(i2400m);
371 struct i2400m_barker_db *barker;
375 if (buf_size != sizeof(i2400m_barker_db[i].data))
378 /* Short circuit if we have already discovered the barker
379 * associated with the device. */
381 && !memcmp(buf, i2400m->barker, sizeof(i2400m->barker->data))) {
382 unsigned index = (i2400m->barker - i2400m_barker_db)
383 / sizeof(*i2400m->barker);
384 d_printf(2, dev, "boot barker cache-confirmed #%u/%08x\n",
385 index, le32_to_cpu(i2400m->barker->data[0]));
389 for (i = 0; i < i2400m_barker_db_used; i++) {
390 barker = &i2400m_barker_db[i];
391 BUILD_BUG_ON(sizeof(barker->data) != 16);
392 if (memcmp(buf, barker->data, sizeof(barker->data)))
395 if (i2400m->barker == NULL) {
396 i2400m->barker = barker;
397 d_printf(1, dev, "boot barker set to #%u/%08x\n",
398 i, le32_to_cpu(barker->data[0]));
399 if (barker->data[0] == le32_to_cpu(I2400M_NBOOT_BARKER))
403 } else if (i2400m->barker != barker) {
404 dev_err(dev, "HW inconsistency: device "
405 "reports a different boot barker "
406 "than set (from %08x to %08x)\n",
407 le32_to_cpu(i2400m->barker->data[0]),
408 le32_to_cpu(barker->data[0]));
411 d_printf(2, dev, "boot barker confirmed #%u/%08x\n",
412 i, le32_to_cpu(barker->data[0]));
418 EXPORT_SYMBOL_GPL(i2400m_is_boot_barker);
422 * Verify the ack data received
424 * Given a reply to a boot mode command, chew it and verify everything
427 * @opcode: opcode which generated this ack. For error messages.
428 * @ack: pointer to ack data we received
429 * @ack_size: size of that data buffer
430 * @flags: I2400M_BM_CMD_* flags we called the command with.
432 * Way too long function -- maybe it should be further split
435 ssize_t __i2400m_bm_ack_verify(struct i2400m *i2400m, int opcode,
436 struct i2400m_bootrom_header *ack,
437 size_t ack_size, int flags)
439 ssize_t result = -ENOMEM;
440 struct device *dev = i2400m_dev(i2400m);
442 d_fnstart(8, dev, "(i2400m %p opcode %d ack %p size %zu)\n",
443 i2400m, opcode, ack, ack_size);
444 if (ack_size < sizeof(*ack)) {
446 dev_err(dev, "boot-mode cmd %d: HW BUG? notification didn't "
447 "return enough data (%zu bytes vs %zu expected)\n",
448 opcode, ack_size, sizeof(*ack));
449 goto error_ack_short;
451 result = i2400m_is_boot_barker(i2400m, ack, ack_size);
453 result = -ERESTARTSYS;
454 d_printf(6, dev, "boot-mode cmd %d: HW boot barker\n", opcode);
457 if (ack_size == sizeof(i2400m_ACK_BARKER)
458 && memcmp(ack, i2400m_ACK_BARKER, sizeof(*ack)) == 0) {
460 d_printf(3, dev, "boot-mode cmd %d: HW reboot ack barker\n",
462 goto error_reboot_ack;
465 if (flags & I2400M_BM_CMD_RAW)
467 ack->data_size = le32_to_cpu(ack->data_size);
468 ack->target_addr = le32_to_cpu(ack->target_addr);
469 ack->block_checksum = le32_to_cpu(ack->block_checksum);
470 d_printf(5, dev, "boot-mode cmd %d: notification for opcode %u "
471 "response %u csum %u rr %u da %u\n",
472 opcode, i2400m_brh_get_opcode(ack),
473 i2400m_brh_get_response(ack),
474 i2400m_brh_get_use_checksum(ack),
475 i2400m_brh_get_response_required(ack),
476 i2400m_brh_get_direct_access(ack));
478 if (i2400m_brh_get_signature(ack) != 0xcbbc) {
479 dev_err(dev, "boot-mode cmd %d: HW BUG? wrong signature "
480 "0x%04x\n", opcode, i2400m_brh_get_signature(ack));
481 goto error_ack_signature;
483 if (opcode != -1 && opcode != i2400m_brh_get_opcode(ack)) {
484 dev_err(dev, "boot-mode cmd %d: HW BUG? "
485 "received response for opcode %u, expected %u\n",
486 opcode, i2400m_brh_get_opcode(ack), opcode);
487 goto error_ack_opcode;
489 if (i2400m_brh_get_response(ack) != 0) { /* failed? */
490 dev_err(dev, "boot-mode cmd %d: error; hw response %u\n",
491 opcode, i2400m_brh_get_response(ack));
492 goto error_ack_failed;
494 if (ack_size < ack->data_size + sizeof(*ack)) {
495 dev_err(dev, "boot-mode cmd %d: SW BUG "
496 "driver provided only %zu bytes for %zu bytes "
497 "of data\n", opcode, ack_size,
498 (size_t) le32_to_cpu(ack->data_size) + sizeof(*ack));
499 goto error_ack_short_buffer;
502 /* Don't you love this stack of empty targets? Well, I don't
503 * either, but it helps track exactly who comes in here and
505 error_ack_short_buffer:
513 d_fnend(8, dev, "(i2400m %p opcode %d ack %p size %zu) = %d\n",
514 i2400m, opcode, ack, ack_size, (int) result);
520 * i2400m_bm_cmd - Execute a boot mode command
522 * @cmd: buffer containing the command data (pointing at the header).
523 * This data can be ANYWHERE (for USB, we will copy it to an
524 * specific buffer). Make sure everything is in proper little
527 * A raw buffer can be also sent, just cast it and set flags to
530 * This function will generate a checksum for you if the
531 * checksum bit in the command is set (unless I2400M_BM_CMD_RAW
534 * You can use the i2400m->bm_cmd_buf to stage your commands and
537 * If NULL, no command is sent (we just wait for an ack).
539 * @cmd_size: size of the command. Will be auto padded to the
540 * bus-specific drivers padding requirements.
542 * @ack: buffer where to place the acknowledgement. If it is a regular
543 * command response, all fields will be returned with the right,
546 * You *cannot* use i2400m->bm_ack_buf for this buffer.
548 * @ack_size: size of @ack, 16 aligned; you need to provide at least
549 * sizeof(*ack) bytes and then enough to contain the return data
552 * @flags: see I2400M_BM_CMD_* above.
554 * @returns: bytes received by the notification; if < 0, an errno code
555 * denoting an error or:
557 * -ERESTARTSYS The device has rebooted
559 * Executes a boot-mode command and waits for a response, doing basic
560 * validation on it; if a zero length response is received, it retries
561 * waiting for a response until a non-zero one is received (timing out
562 * after %I2400M_BOOT_RETRIES retries).
565 ssize_t i2400m_bm_cmd(struct i2400m *i2400m,
566 const struct i2400m_bootrom_header *cmd, size_t cmd_size,
567 struct i2400m_bootrom_header *ack, size_t ack_size,
570 ssize_t result = -ENOMEM, rx_bytes;
571 struct device *dev = i2400m_dev(i2400m);
572 int opcode = cmd == NULL ? -1 : i2400m_brh_get_opcode(cmd);
574 d_fnstart(6, dev, "(i2400m %p cmd %p size %zu ack %p size %zu)\n",
575 i2400m, cmd, cmd_size, ack, ack_size);
576 BUG_ON(ack_size < sizeof(*ack));
577 BUG_ON(i2400m->boot_mode == 0);
579 if (cmd != NULL) { /* send the command */
580 result = i2400m->bus_bm_cmd_send(i2400m, cmd, cmd_size, flags);
583 if ((flags & I2400M_BM_CMD_RAW) == 0)
585 "boot-mode cmd %d csum %u rr %u da %u: "
586 "addr 0x%04x size %u block csum 0x%04x\n",
587 opcode, i2400m_brh_get_use_checksum(cmd),
588 i2400m_brh_get_response_required(cmd),
589 i2400m_brh_get_direct_access(cmd),
590 cmd->target_addr, cmd->data_size,
591 cmd->block_checksum);
593 result = i2400m->bus_bm_wait_for_ack(i2400m, ack, ack_size);
595 dev_err(dev, "boot-mode cmd %d: error waiting for an ack: %d\n",
596 opcode, (int) result); /* bah, %zd doesn't work */
597 goto error_wait_for_ack;
600 /* verify the ack and read more if neccessary [result is the
601 * final amount of bytes we get in the ack] */
602 result = __i2400m_bm_ack_verify(i2400m, opcode, ack, ack_size, flags);
605 /* Don't you love this stack of empty targets? Well, I don't
606 * either, but it helps track exactly who comes in here and
612 d_fnend(6, dev, "(i2400m %p cmd %p size %zu ack %p size %zu) = %d\n",
613 i2400m, cmd, cmd_size, ack, ack_size, (int) result);
619 * i2400m_download_chunk - write a single chunk of data to the device's memory
621 * @i2400m: device descriptor
622 * @buf: the buffer to write
623 * @buf_len: length of the buffer to write
624 * @addr: address in the device memory space
625 * @direct: bootrom write mode
626 * @do_csum: should a checksum validation be performed
628 static int i2400m_download_chunk(struct i2400m *i2400m, const void *chunk,
629 size_t __chunk_len, unsigned long addr,
630 unsigned int direct, unsigned int do_csum)
633 size_t chunk_len = ALIGN(__chunk_len, I2400M_PL_ALIGN);
634 struct device *dev = i2400m_dev(i2400m);
636 struct i2400m_bootrom_header cmd;
637 u8 cmd_payload[chunk_len];
638 } __attribute__((packed)) *buf;
639 struct i2400m_bootrom_header ack;
641 d_fnstart(5, dev, "(i2400m %p chunk %p __chunk_len %zu addr 0x%08lx "
642 "direct %u do_csum %u)\n", i2400m, chunk, __chunk_len,
643 addr, direct, do_csum);
644 buf = i2400m->bm_cmd_buf;
645 memcpy(buf->cmd_payload, chunk, __chunk_len);
646 memset(buf->cmd_payload + __chunk_len, 0xad, chunk_len - __chunk_len);
648 buf->cmd.command = i2400m_brh_command(I2400M_BRH_WRITE,
649 __chunk_len & 0x3 ? 0 : do_csum,
650 __chunk_len & 0xf ? 0 : direct);
651 buf->cmd.target_addr = cpu_to_le32(addr);
652 buf->cmd.data_size = cpu_to_le32(__chunk_len);
653 ret = i2400m_bm_cmd(i2400m, &buf->cmd, sizeof(buf->cmd) + chunk_len,
654 &ack, sizeof(ack), 0);
657 d_fnend(5, dev, "(i2400m %p chunk %p __chunk_len %zu addr 0x%08lx "
658 "direct %u do_csum %u) = %d\n", i2400m, chunk, __chunk_len,
659 addr, direct, do_csum, ret);
665 * Download a BCF file's sections to the device
667 * @i2400m: device descriptor
668 * @bcf: pointer to firmware data (followed by the payloads). Assumed
669 * verified and consistent.
670 * @bcf_len: length (in bytes) of the @bcf buffer.
672 * Returns: < 0 errno code on error or the offset to the jump instruction.
674 * Given a BCF file, downloads each section (a command and a payload)
675 * to the device's address space. Actually, it just executes each
676 * command i the BCF file.
678 * The section size has to be aligned to 4 bytes AND the padding has
679 * to be taken from the firmware file, as the signature takes it into
683 ssize_t i2400m_dnload_bcf(struct i2400m *i2400m,
684 const struct i2400m_bcf_hdr *bcf, size_t bcf_len)
687 struct device *dev = i2400m_dev(i2400m);
688 size_t offset, /* iterator offset */
689 data_size, /* Size of the data payload */
690 section_size, /* Size of the whole section (cmd + payload) */
692 const struct i2400m_bootrom_header *bh;
693 struct i2400m_bootrom_header ack;
695 d_fnstart(3, dev, "(i2400m %p bcf %p bcf_len %zu)\n",
696 i2400m, bcf, bcf_len);
697 /* Iterate over the command blocks in the BCF file that start
698 * after the header */
699 offset = le32_to_cpu(bcf->header_len) * sizeof(u32);
700 while (1) { /* start sending the file */
701 bh = (void *) bcf + offset;
702 data_size = le32_to_cpu(bh->data_size);
703 section_size = ALIGN(sizeof(*bh) + data_size, 4);
705 "downloading section #%zu (@%zu %zu B) to 0x%08x\n",
706 section, offset, sizeof(*bh) + data_size,
707 le32_to_cpu(bh->target_addr));
708 if (i2400m_brh_get_opcode(bh) == I2400M_BRH_SIGNED_JUMP) {
709 /* Secure boot needs to stop here */
710 d_printf(5, dev, "signed jump found @%zu\n", offset);
713 if (offset + section_size == bcf_len)
714 /* Non-secure boot stops here */
716 if (offset + section_size > bcf_len) {
717 dev_err(dev, "fw %s: bad section #%zu, "
718 "end (@%zu) beyond EOF (@%zu)\n",
719 i2400m->fw_name, section,
720 offset + section_size, bcf_len);
722 goto error_section_beyond_eof;
725 ret = i2400m_bm_cmd(i2400m, bh, section_size,
726 &ack, sizeof(ack), I2400M_BM_CMD_RAW);
728 dev_err(dev, "fw %s: section #%zu (@%zu %zu B) "
729 "failed %d\n", i2400m->fw_name, section,
730 offset, sizeof(*bh) + data_size, (int) ret);
733 offset += section_size;
737 error_section_beyond_eof:
739 d_fnend(3, dev, "(i2400m %p bcf %p bcf_len %zu) = %d\n",
740 i2400m, bcf, bcf_len, (int) ret);
746 * Indicate if the device emitted a reboot barker that indicates
750 unsigned i2400m_boot_is_signed(struct i2400m *i2400m)
752 return likely(i2400m->sboot);
757 * Do the final steps of uploading firmware
759 * Depending on the boot mode (signed vs non-signed), different
760 * actions need to be taken.
763 int i2400m_dnload_finalize(struct i2400m *i2400m,
764 const struct i2400m_bcf_hdr *bcf, size_t offset)
767 struct device *dev = i2400m_dev(i2400m);
768 struct i2400m_bootrom_header *cmd, ack;
770 struct i2400m_bootrom_header cmd;
772 } __attribute__((packed)) *cmd_buf;
773 size_t signature_block_offset, signature_block_size;
775 d_fnstart(3, dev, "offset %zu\n", offset);
776 cmd = (void *) bcf + offset;
777 if (i2400m_boot_is_signed(i2400m) == 0) {
778 struct i2400m_bootrom_header jump_ack;
779 d_printf(1, dev, "unsecure boot, jumping to 0x%08x\n",
780 le32_to_cpu(cmd->target_addr));
781 cmd_buf = i2400m->bm_cmd_buf;
782 memcpy(&cmd_buf->cmd, cmd, sizeof(*cmd));
784 /* now cmd points to the actual bootrom_header in cmd_buf */
785 i2400m_brh_set_opcode(cmd, I2400M_BRH_JUMP);
787 ret = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
788 &jump_ack, sizeof(jump_ack), 0);
790 d_printf(1, dev, "secure boot, jumping to 0x%08x\n",
791 le32_to_cpu(cmd->target_addr));
792 cmd_buf = i2400m->bm_cmd_buf;
793 memcpy(&cmd_buf->cmd, cmd, sizeof(*cmd));
794 signature_block_offset =
796 + le32_to_cpu(bcf->key_size) * sizeof(u32)
797 + le32_to_cpu(bcf->exponent_size) * sizeof(u32);
798 signature_block_size =
799 le32_to_cpu(bcf->modulus_size) * sizeof(u32);
800 memcpy(cmd_buf->cmd_pl, (void *) bcf + signature_block_offset,
801 signature_block_size);
802 ret = i2400m_bm_cmd(i2400m, &cmd_buf->cmd,
803 sizeof(cmd_buf->cmd) + signature_block_size,
804 &ack, sizeof(ack), I2400M_BM_CMD_RAW);
806 d_fnend(3, dev, "returning %d\n", ret);
812 * i2400m_bootrom_init - Reboots a powered device into boot mode
814 * @i2400m: device descriptor
816 * I2400M_BRI_SOFT: a reboot barker has been seen
817 * already, so don't wait for it.
819 * I2400M_BRI_NO_REBOOT: Don't send a reboot command, but wait
820 * for a reboot barker notification. This is a one shot; if
821 * the state machine needs to send a reboot command it will.
825 * < 0 errno code on error, 0 if ok.
829 * Tries hard enough to put the device in boot-mode. There are two
830 * main phases to this:
832 * a. (1) send a reboot command and (2) get a reboot barker
834 * b. (1) echo/ack the reboot sending the reboot barker back and (2)
835 * getting an ack barker in return
837 * We want to skip (a) in some cases [soft]. The state machine is
838 * horrible, but it is basically: on each phase, send what has to be
839 * sent (if any), wait for the answer and act on the answer. We might
840 * have to backtrack and retry, so we keep a max tries counter for
843 * It sucks because we don't know ahead of time which is going to be
844 * the reboot barker (the device might send different ones depending
845 * on its EEPROM config) and once the device reboots and waits for the
846 * echo/ack reboot barker being sent back, it doesn't understand
847 * anything else. So we can be left at the point where we don't know
848 * what to send to it -- cold reset and bus reset seem to have little
849 * effect. So the function iterates (in this case) through all the
850 * known barkers and tries them all until an ACK is
851 * received. Otherwise, it gives up.
853 * If we get a timeout after sending a warm reset, we do it again.
855 int i2400m_bootrom_init(struct i2400m *i2400m, enum i2400m_bri flags)
858 struct device *dev = i2400m_dev(i2400m);
859 struct i2400m_bootrom_header *cmd;
860 struct i2400m_bootrom_header ack;
861 int count = i2400m->bus_bm_retries;
862 int ack_timeout_cnt = 1;
865 BUILD_BUG_ON(sizeof(*cmd) != sizeof(i2400m_barker_db[0].data));
866 BUILD_BUG_ON(sizeof(ack) != sizeof(i2400m_ACK_BARKER));
868 d_fnstart(4, dev, "(i2400m %p flags 0x%08x)\n", i2400m, flags);
870 cmd = i2400m->bm_cmd_buf;
871 if (flags & I2400M_BRI_SOFT)
877 d_printf(4, dev, "device reboot: reboot command [%d # left]\n",
879 if ((flags & I2400M_BRI_NO_REBOOT) == 0)
880 i2400m->bus_reset(i2400m, I2400M_RT_WARM);
881 result = i2400m_bm_cmd(i2400m, NULL, 0, &ack, sizeof(ack),
883 flags &= ~I2400M_BRI_NO_REBOOT;
887 * at this point, i2400m_bm_cmd(), through
888 * __i2400m_bm_ack_process(), has updated
889 * i2400m->barker and we are good to go.
891 d_printf(4, dev, "device reboot: got reboot barker\n");
893 case -EISCONN: /* we don't know how it got here...but we follow it */
894 d_printf(4, dev, "device reboot: got ack barker - whatever\n");
898 * Device has timed out, we might be in boot mode
899 * already and expecting an ack; if we don't know what
900 * the barker is, we just send them all. Cold reset
901 * and bus reset don't work. Beats me.
903 if (i2400m->barker != NULL) {
904 dev_err(dev, "device boot: reboot barker timed out, "
905 "trying (set) %08x echo/ack\n",
906 le32_to_cpu(i2400m->barker->data[0]));
909 for (i = 0; i < i2400m_barker_db_used; i++) {
910 struct i2400m_barker_db *barker = &i2400m_barker_db[i];
911 memcpy(cmd, barker->data, sizeof(barker->data));
912 result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
915 if (result == -EISCONN) {
916 dev_warn(dev, "device boot: got ack barker "
917 "after sending echo/ack barker "
918 "#%d/%08x; rebooting j.i.c.\n",
919 i, le32_to_cpu(barker->data[0]));
920 flags &= ~I2400M_BRI_NO_REBOOT;
924 dev_err(dev, "device boot: tried all the echo/acks, could "
925 "not get device to respond; giving up");
928 case -ESHUTDOWN: /* dev is gone */
929 case -EINTR: /* user cancelled */
932 dev_err(dev, "device reboot: error %d while waiting "
933 "for reboot barker - rebooting\n", result);
934 d_dump(1, dev, &ack, result);
937 /* At this point we ack back with 4 REBOOT barkers and expect
938 * 4 ACK barkers. This is ugly, as we send a raw command --
939 * hence the cast. _bm_cmd() will catch the reboot ack
940 * notification and report it as -EISCONN. */
942 d_printf(4, dev, "device reboot ack: sending ack [%d # left]\n", count);
943 memcpy(cmd, i2400m->barker->data, sizeof(i2400m->barker->data));
944 result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
945 &ack, sizeof(ack), I2400M_BM_CMD_RAW);
948 d_printf(4, dev, "reboot ack: got reboot barker - retrying\n");
953 d_printf(4, dev, "reboot ack: got ack barker - good\n");
955 case -ETIMEDOUT: /* no response, maybe it is the other type? */
956 if (ack_timeout_cnt-- < 0) {
957 d_printf(4, dev, "reboot ack timedout: retrying\n");
960 dev_err(dev, "reboot ack timedout too long: "
966 case -ESHUTDOWN: /* dev is gone */
969 dev_err(dev, "device reboot ack: error %d while waiting for "
970 "reboot ack barker - rebooting\n", result);
973 d_printf(2, dev, "device reboot ack: got ack barker - boot done\n");
977 d_fnend(4, dev, "(i2400m %p flags 0x%08x) = %d\n",
978 i2400m, flags, result);
982 dev_err(dev, "Timed out waiting for reboot ack\n");
991 * The position this function reads is fixed in device memory and
992 * always available, even without firmware.
994 * Note we specify we want to read only six bytes, but provide space
995 * for 16, as we always get it rounded up.
997 int i2400m_read_mac_addr(struct i2400m *i2400m)
1000 struct device *dev = i2400m_dev(i2400m);
1001 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
1002 struct i2400m_bootrom_header *cmd;
1004 struct i2400m_bootrom_header ack;
1006 } __attribute__((packed)) ack_buf;
1008 d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
1009 cmd = i2400m->bm_cmd_buf;
1010 cmd->command = i2400m_brh_command(I2400M_BRH_READ, 0, 1);
1011 cmd->target_addr = cpu_to_le32(0x00203fe8);
1012 cmd->data_size = cpu_to_le32(6);
1013 result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
1014 &ack_buf.ack, sizeof(ack_buf), 0);
1016 dev_err(dev, "BM: read mac addr failed: %d\n", result);
1017 goto error_read_mac;
1020 "mac addr is %02x:%02x:%02x:%02x:%02x:%02x\n",
1021 ack_buf.ack_pl[0], ack_buf.ack_pl[1],
1022 ack_buf.ack_pl[2], ack_buf.ack_pl[3],
1023 ack_buf.ack_pl[4], ack_buf.ack_pl[5]);
1024 if (i2400m->bus_bm_mac_addr_impaired == 1) {
1025 ack_buf.ack_pl[0] = 0x00;
1026 ack_buf.ack_pl[1] = 0x16;
1027 ack_buf.ack_pl[2] = 0xd3;
1028 get_random_bytes(&ack_buf.ack_pl[3], 3);
1029 dev_err(dev, "BM is MAC addr impaired, faking MAC addr to "
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]);
1036 net_dev->addr_len = ETH_ALEN;
1037 memcpy(net_dev->perm_addr, ack_buf.ack_pl, ETH_ALEN);
1038 memcpy(net_dev->dev_addr, ack_buf.ack_pl, ETH_ALEN);
1040 d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, result);
1046 * Initialize a non signed boot
1048 * This implies sending some magic values to the device's memory. Note
1049 * we convert the values to little endian in the same array
1053 int i2400m_dnload_init_nonsigned(struct i2400m *i2400m)
1057 struct device *dev = i2400m_dev(i2400m);
1058 d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
1059 if (i2400m->bus_bm_pokes_table) {
1060 while (i2400m->bus_bm_pokes_table[i].address) {
1061 ret = i2400m_download_chunk(
1063 &i2400m->bus_bm_pokes_table[i].data,
1064 sizeof(i2400m->bus_bm_pokes_table[i].data),
1065 i2400m->bus_bm_pokes_table[i].address, 1, 1);
1071 d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, ret);
1077 * Initialize the signed boot process
1079 * @i2400m: device descriptor
1081 * @bcf_hdr: pointer to the firmware header; assumes it is fully in
1082 * memory (it has gone through basic validation).
1084 * Returns: 0 if ok, < 0 errno code on error, -ERESTARTSYS if the hw
1087 * This writes the firmware BCF header to the device using the
1088 * HASH_PAYLOAD_ONLY command.
1091 int i2400m_dnload_init_signed(struct i2400m *i2400m,
1092 const struct i2400m_bcf_hdr *bcf_hdr)
1095 struct device *dev = i2400m_dev(i2400m);
1097 struct i2400m_bootrom_header cmd;
1098 struct i2400m_bcf_hdr cmd_pl;
1099 } __attribute__((packed)) *cmd_buf;
1100 struct i2400m_bootrom_header ack;
1102 d_fnstart(5, dev, "(i2400m %p bcf_hdr %p)\n", i2400m, bcf_hdr);
1103 cmd_buf = i2400m->bm_cmd_buf;
1104 cmd_buf->cmd.command =
1105 i2400m_brh_command(I2400M_BRH_HASH_PAYLOAD_ONLY, 0, 0);
1106 cmd_buf->cmd.target_addr = 0;
1107 cmd_buf->cmd.data_size = cpu_to_le32(sizeof(cmd_buf->cmd_pl));
1108 memcpy(&cmd_buf->cmd_pl, bcf_hdr, sizeof(*bcf_hdr));
1109 ret = i2400m_bm_cmd(i2400m, &cmd_buf->cmd, sizeof(*cmd_buf),
1110 &ack, sizeof(ack), 0);
1113 d_fnend(5, dev, "(i2400m %p bcf_hdr %p) = %d\n", i2400m, bcf_hdr, ret);
1119 * Initialize the firmware download at the device size
1121 * Multiplex to the one that matters based on the device's mode
1122 * (signed or non-signed).
1125 int i2400m_dnload_init(struct i2400m *i2400m, const struct i2400m_bcf_hdr *bcf)
1128 struct device *dev = i2400m_dev(i2400m);
1130 if (i2400m_boot_is_signed(i2400m)) {
1131 d_printf(1, dev, "signed boot\n");
1132 result = i2400m_dnload_init_signed(i2400m, bcf);
1133 if (result == -ERESTARTSYS)
1136 dev_err(dev, "firmware %s: signed boot download "
1137 "initialization failed: %d\n",
1138 i2400m->fw_name, result);
1140 /* non-signed boot process without pokes */
1141 d_printf(1, dev, "non-signed boot\n");
1142 result = i2400m_dnload_init_nonsigned(i2400m);
1143 if (result == -ERESTARTSYS)
1146 dev_err(dev, "firmware %s: non-signed download "
1147 "initialization failed: %d\n",
1148 i2400m->fw_name, result);
1155 * Run consistency tests on the firmware file and load up headers
1157 * Check for the firmware being made for the i2400m device,
1158 * etc...These checks are mostly informative, as the device will make
1159 * them too; but the driver's response is more informative on what
1162 * This will also look at all the headers present on the firmware
1163 * file, and update i2400m->fw_bcf_hdr to point to them.
1166 int i2400m_fw_hdr_check(struct i2400m *i2400m,
1167 const struct i2400m_bcf_hdr *bcf_hdr,
1168 size_t index, size_t offset)
1170 struct device *dev = i2400m_dev(i2400m);
1172 unsigned module_type, header_len, major_version, minor_version,
1173 module_id, module_vendor, date, size;
1175 module_type = bcf_hdr->module_type;
1176 header_len = sizeof(u32) * le32_to_cpu(bcf_hdr->header_len);
1177 major_version = (le32_to_cpu(bcf_hdr->header_version) & 0xffff0000)
1179 minor_version = le32_to_cpu(bcf_hdr->header_version) & 0x0000ffff;
1180 module_id = le32_to_cpu(bcf_hdr->module_id);
1181 module_vendor = le32_to_cpu(bcf_hdr->module_vendor);
1182 date = le32_to_cpu(bcf_hdr->date);
1183 size = sizeof(u32) * le32_to_cpu(bcf_hdr->size);
1185 d_printf(1, dev, "firmware %s #%d@%08x: BCF header "
1186 "type:vendor:id 0x%x:%x:%x v%u.%u (%zu/%zu B) built %08x\n",
1187 i2400m->fw_name, index, offset,
1188 module_type, module_vendor, module_id,
1189 major_version, minor_version, header_len, size, date);
1192 if (major_version != 1) {
1193 dev_err(dev, "firmware %s #%d@%08x: major header version "
1194 "v%u.%u not supported\n",
1195 i2400m->fw_name, index, offset,
1196 major_version, minor_version);
1200 if (module_type != 6) { /* built for the right hardware? */
1201 dev_err(dev, "firmware %s #%d@%08x: unexpected module "
1202 "type 0x%x; aborting\n",
1203 i2400m->fw_name, index, offset,
1208 if (module_vendor != 0x8086) {
1209 dev_err(dev, "firmware %s #%d@%08x: unexpected module "
1210 "vendor 0x%x; aborting\n",
1211 i2400m->fw_name, index, offset, module_vendor);
1215 if (date < 0x20080300)
1216 dev_warn(dev, "firmware %s #%d@%08x: build date %08x "
1217 "too old; unsupported\n",
1218 i2400m->fw_name, index, offset, date);
1224 * Run consistency tests on the firmware file and load up headers
1226 * Check for the firmware being made for the i2400m device,
1227 * etc...These checks are mostly informative, as the device will make
1228 * them too; but the driver's response is more informative on what
1231 * This will also look at all the headers present on the firmware
1232 * file, and update i2400m->fw_hdrs to point to them.
1235 int i2400m_fw_check(struct i2400m *i2400m, const void *bcf, size_t bcf_size)
1238 struct device *dev = i2400m_dev(i2400m);
1240 const struct i2400m_bcf_hdr *bcf_hdr;
1241 const void *itr, *next, *top;
1242 unsigned slots = 0, used_slots = 0;
1244 for (itr = bcf, top = itr + bcf_size;
1246 headers++, itr = next) {
1247 size_t leftover, offset, header_len, size;
1249 leftover = top - itr;
1250 offset = itr - (const void *) bcf;
1251 if (leftover <= sizeof(*bcf_hdr)) {
1252 dev_err(dev, "firmware %s: %zu B left at @%x, "
1253 "not enough for BCF header\n",
1254 i2400m->fw_name, leftover, offset);
1258 /* Only the first header is supposed to be followed by
1260 header_len = sizeof(u32) * le32_to_cpu(bcf_hdr->header_len);
1261 size = sizeof(u32) * le32_to_cpu(bcf_hdr->size);
1265 next = itr + header_len;
1267 result = i2400m_fw_hdr_check(i2400m, bcf_hdr, headers, offset);
1270 if (used_slots + 1 >= slots) {
1271 /* +1 -> we need to account for the one we'll
1272 * occupy and at least an extra one for
1273 * always being NULL */
1274 result = i2400m_zrealloc_2x(
1275 (void **) &i2400m->fw_hdrs, &slots,
1276 sizeof(i2400m->fw_hdrs[0]),
1279 goto error_zrealloc;
1281 i2400m->fw_hdrs[used_slots] = bcf_hdr;
1285 dev_err(dev, "firmware %s: no usable headers found\n",
1296 * Download the firmware to the device
1298 * @i2400m: device descriptor
1299 * @bcf: pointer to loaded (and minimally verified for consistency)
1301 * @bcf_size: size of the @bcf buffer (header plus payloads)
1303 * The process for doing this is described in this file's header.
1305 * Note we only reinitialize boot-mode if the flags say so. Some hw
1306 * iterations need it, some don't. In any case, if we loop, we always
1307 * need to reinitialize the boot room, hence the flags modification.
1310 int i2400m_fw_dnload(struct i2400m *i2400m, const struct i2400m_bcf_hdr *bcf,
1311 size_t bcf_size, enum i2400m_bri flags)
1314 struct device *dev = i2400m_dev(i2400m);
1315 int count = i2400m->bus_bm_retries;
1317 d_fnstart(5, dev, "(i2400m %p bcf %p size %zu)\n",
1318 i2400m, bcf, bcf_size);
1319 i2400m->boot_mode = 1;
1320 wmb(); /* Make sure other readers see it */
1324 dev_err(dev, "device rebooted too many times, aborting\n");
1325 goto error_too_many_reboots;
1327 if (flags & I2400M_BRI_MAC_REINIT) {
1328 ret = i2400m_bootrom_init(i2400m, flags);
1330 dev_err(dev, "bootrom init failed: %d\n", ret);
1331 goto error_bootrom_init;
1334 flags |= I2400M_BRI_MAC_REINIT;
1337 * Initialize the download, push the bytes to the device and
1338 * then jump to the new firmware. Note @ret is passed with the
1339 * offset of the jump instruction to _dnload_finalize()
1341 ret = i2400m_dnload_init(i2400m, bcf); /* Init device's dnload */
1342 if (ret == -ERESTARTSYS)
1343 goto error_dev_rebooted;
1345 goto error_dnload_init;
1347 ret = i2400m_dnload_bcf(i2400m, bcf, bcf_size);
1348 if (ret == -ERESTARTSYS)
1349 goto error_dev_rebooted;
1351 dev_err(dev, "fw %s: download failed: %d\n",
1352 i2400m->fw_name, ret);
1353 goto error_dnload_bcf;
1356 ret = i2400m_dnload_finalize(i2400m, bcf, ret);
1357 if (ret == -ERESTARTSYS)
1358 goto error_dev_rebooted;
1360 dev_err(dev, "fw %s: "
1361 "download finalization failed: %d\n",
1362 i2400m->fw_name, ret);
1363 goto error_dnload_finalize;
1366 d_printf(2, dev, "fw %s successfully uploaded\n",
1368 i2400m->boot_mode = 0;
1369 wmb(); /* Make sure i2400m_msg_to_dev() sees boot_mode */
1370 error_dnload_finalize:
1374 error_too_many_reboots:
1375 d_fnend(5, dev, "(i2400m %p bcf %p size %zu) = %d\n",
1376 i2400m, bcf, bcf_size, ret);
1380 dev_err(dev, "device rebooted, %d tries left\n", count);
1381 /* we got the notification already, no need to wait for it again */
1382 flags |= I2400M_BRI_SOFT;
1388 * i2400m_dev_bootstrap - Bring the device to a known state and upload firmware
1390 * @i2400m: device descriptor
1392 * Returns: >= 0 if ok, < 0 errno code on error.
1394 * This sets up the firmware upload environment, loads the firmware
1395 * file from disk, verifies and then calls the firmware upload process
1398 * Can be called either from probe, or after a warm reset. Can not be
1399 * called from within an interrupt. All the flow in this code is
1400 * single-threade; all I/Os are synchronous.
1402 int i2400m_dev_bootstrap(struct i2400m *i2400m, enum i2400m_bri flags)
1405 struct device *dev = i2400m_dev(i2400m);
1406 const struct firmware *fw;
1407 const struct i2400m_bcf_hdr *bcf; /* Firmware data */
1408 const char *fw_name;
1410 d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
1412 /* Load firmware files to memory. */
1413 for (itr = 0, bcf = NULL, ret = -ENOENT; ; itr++) {
1414 fw_name = i2400m->bus_fw_names[itr];
1415 if (fw_name == NULL) {
1416 dev_err(dev, "Could not find a usable firmware image\n");
1420 d_printf(1, dev, "trying firmware %s (%d)\n", fw_name, itr);
1421 ret = request_firmware(&fw, fw_name, dev);
1423 dev_err(dev, "fw %s: cannot load file: %d\n",
1427 bcf = (void *) fw->data;
1428 i2400m->fw_name = fw_name;
1429 ret = i2400m_fw_check(i2400m, bcf, fw->size);
1431 ret = i2400m_fw_dnload(i2400m, bcf, fw->size, flags);
1433 dev_err(dev, "%s: cannot use: %d, skipping\n",
1435 kfree(i2400m->fw_hdrs);
1436 i2400m->fw_hdrs = NULL;
1437 release_firmware(fw);
1438 if (ret >= 0) /* firmware loaded succesfully */
1441 d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, ret);
1444 EXPORT_SYMBOL_GPL(i2400m_dev_bootstrap);