blob: 897e0be698c6d4e3f92969b42d1bb059552187d4 [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-Gonzalezaba37922009-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-Gonzalezaba37922009-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-Gonzalezaba37922009-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-Gonzalezaba37922009-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-Gonzalezaba37922009-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-Gonzalezaba37922009-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;
280error_add:
281 return result;
282}
283
284
285/*
286 * Initialize the barker database
287 *
288 * This can only be used from the module_init function for this
289 * module; this is to avoid the need to do locking.
290 *
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.
295 */
296int i2400m_barker_db_init(const char *_options)
297{
298 int result;
299 char *options = NULL, *options_orig, *token;
300
301 i2400m_barker_db = NULL;
302 i2400m_barker_db_size = 0;
303 i2400m_barker_db_used = 0;
304
305 result = i2400m_barker_db_known_barkers();
306 if (result < 0)
307 goto error_add;
308 /* parse command line options from i2400m.barkers */
309 if (_options != NULL) {
310 unsigned barker;
311
312 options_orig = kstrdup(_options, GFP_KERNEL);
313 if (options_orig == NULL)
314 goto error_parse;
315 options = options_orig;
316
317 while ((token = strsep(&options, ",")) != NULL) {
318 if (*token == '\0') /* eat joint commas */
319 continue;
320 if (sscanf(token, "%x", &barker) != 1
321 || barker > 0xffffffff) {
322 printk(KERN_ERR "%s: can't recognize "
323 "i2400m.barkers value '%s' as "
324 "a 32-bit number\n",
325 __func__, token);
326 result = -EINVAL;
327 goto error_parse;
328 }
329 if (barker == 0) {
330 /* clean list and start new */
331 i2400m_barker_db_exit();
332 continue;
333 }
334 result = i2400m_barker_db_add(barker);
335 if (result < 0)
336 goto error_add;
337 }
338 kfree(options_orig);
339 }
340 return 0;
341
342error_parse:
343error_add:
344 kfree(i2400m_barker_db);
345 return result;
346}
347
348
349/*
350 * Recognize a boot barker
351 *
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.
355 *
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).
361 *
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.
365 */
366int i2400m_is_boot_barker(struct i2400m *i2400m,
367 const void *buf, size_t buf_size)
368{
369 int result;
370 struct device *dev = i2400m_dev(i2400m);
371 struct i2400m_barker_db *barker;
372 int i;
373
374 result = -ENOENT;
375 if (buf_size != sizeof(i2400m_barker_db[i].data))
376 return result;
377
378 /* Short circuit if we have already discovered the barker
379 * associated with the device. */
380 if (i2400m->barker
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]));
386 return 0;
387 }
388
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)))
393 continue;
394
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))
400 i2400m->sboot = 0;
401 else
402 i2400m->sboot = 1;
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]));
409 result = -EIO;
410 } else
411 d_printf(2, dev, "boot barker confirmed #%u/%08x\n",
412 i, le32_to_cpu(barker->data[0]));
413 result = 0;
414 break;
415 }
416 return result;
417}
418EXPORT_SYMBOL_GPL(i2400m_is_boot_barker);
419
420
421/*
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800422 * Verify the ack data received
423 *
424 * Given a reply to a boot mode command, chew it and verify everything
425 * is ok.
426 *
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.
431 *
432 * Way too long function -- maybe it should be further split
433 */
434static
435ssize_t __i2400m_bm_ack_verify(struct i2400m *i2400m, int opcode,
436 struct i2400m_bootrom_header *ack,
437 size_t ack_size, int flags)
438{
439 ssize_t result = -ENOMEM;
440 struct device *dev = i2400m_dev(i2400m);
441
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)) {
445 result = -EIO;
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;
450 }
Inaky Perez-Gonzalezaba37922009-09-03 15:14:29 -0700451 result = i2400m_is_boot_barker(i2400m, ack, ack_size);
452 if (result >= 0) {
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800453 result = -ERESTARTSYS;
Inaky Perez-Gonzalezaba37922009-09-03 15:14:29 -0700454 d_printf(6, dev, "boot-mode cmd %d: HW boot barker\n", opcode);
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800455 goto error_reboot;
456 }
457 if (ack_size == sizeof(i2400m_ACK_BARKER)
458 && memcmp(ack, i2400m_ACK_BARKER, sizeof(*ack)) == 0) {
459 result = -EISCONN;
460 d_printf(3, dev, "boot-mode cmd %d: HW reboot ack barker\n",
461 opcode);
462 goto error_reboot_ack;
463 }
464 result = 0;
465 if (flags & I2400M_BM_CMD_RAW)
466 goto out_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));
477 result = -EIO;
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;
482 }
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;
488 }
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;
493 }
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;
500 }
501 result = ack_size;
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
504 * why :) */
505error_ack_short_buffer:
506error_ack_failed:
507error_ack_opcode:
508error_ack_signature:
509out_raw:
510error_reboot_ack:
511error_reboot:
512error_ack_short:
513 d_fnend(8, dev, "(i2400m %p opcode %d ack %p size %zu) = %d\n",
514 i2400m, opcode, ack, ack_size, (int) result);
515 return result;
516}
517
518
519/**
520 * i2400m_bm_cmd - Execute a boot mode command
521 *
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
525 * endian.
526 *
527 * A raw buffer can be also sent, just cast it and set flags to
528 * I2400M_BM_CMD_RAW.
529 *
530 * This function will generate a checksum for you if the
531 * checksum bit in the command is set (unless I2400M_BM_CMD_RAW
532 * is set).
533 *
534 * You can use the i2400m->bm_cmd_buf to stage your commands and
535 * send them.
536 *
537 * If NULL, no command is sent (we just wait for an ack).
538 *
539 * @cmd_size: size of the command. Will be auto padded to the
540 * bus-specific drivers padding requirements.
541 *
542 * @ack: buffer where to place the acknowledgement. If it is a regular
543 * command response, all fields will be returned with the right,
544 * native endianess.
545 *
546 * You *cannot* use i2400m->bm_ack_buf for this buffer.
547 *
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
550 * from the command
551 *
552 * @flags: see I2400M_BM_CMD_* above.
553 *
554 * @returns: bytes received by the notification; if < 0, an errno code
555 * denoting an error or:
556 *
557 * -ERESTARTSYS The device has rebooted
558 *
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).
563 */
564static
565ssize_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,
568 int flags)
569{
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);
573
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);
578
579 if (cmd != NULL) { /* send the command */
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800580 result = i2400m->bus_bm_cmd_send(i2400m, cmd, cmd_size, flags);
581 if (result < 0)
582 goto error_cmd_send;
583 if ((flags & I2400M_BM_CMD_RAW) == 0)
584 d_printf(5, dev,
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);
592 }
593 result = i2400m->bus_bm_wait_for_ack(i2400m, ack, ack_size);
594 if (result < 0) {
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;
598 }
599 rx_bytes = result;
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);
603 if (result < 0)
604 goto error_bad_ack;
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
607 * why :) */
608 result = rx_bytes;
609error_bad_ack:
610error_wait_for_ack:
611error_cmd_send:
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);
614 return result;
615}
616
617
618/**
619 * i2400m_download_chunk - write a single chunk of data to the device's memory
620 *
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
627 */
628static 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)
631{
632 int ret;
Inaky Perez-Gonzalez8593a192009-05-20 16:53:30 -0700633 size_t chunk_len = ALIGN(__chunk_len, I2400M_PL_ALIGN);
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800634 struct device *dev = i2400m_dev(i2400m);
635 struct {
636 struct i2400m_bootrom_header cmd;
637 u8 cmd_payload[chunk_len];
638 } __attribute__((packed)) *buf;
639 struct i2400m_bootrom_header ack;
640
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);
647
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);
655 if (ret >= 0)
656 ret = 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);
660 return ret;
661}
662
663
664/*
665 * Download a BCF file's sections to the device
666 *
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.
671 *
672 * Returns: < 0 errno code on error or the offset to the jump instruction.
673 *
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.
677 *
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
680 * account.
681 */
682static
683ssize_t i2400m_dnload_bcf(struct i2400m *i2400m,
684 const struct i2400m_bcf_hdr *bcf, size_t bcf_len)
685{
686 ssize_t ret;
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) */
691 section = 1;
692 const struct i2400m_bootrom_header *bh;
693 struct i2400m_bootrom_header ack;
694
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);
704 d_printf(7, dev,
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);
711 break;
712 }
713 if (offset + section_size == bcf_len)
714 /* Non-secure boot stops here */
715 break;
716 if (offset + section_size > bcf_len) {
717 dev_err(dev, "fw %s: bad section #%zu, "
718 "end (@%zu) beyond EOF (@%zu)\n",
Inaky Perez-Gonzalez1039abb2009-02-28 23:42:47 +0000719 i2400m->fw_name, section,
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800720 offset + section_size, bcf_len);
721 ret = -EINVAL;
722 goto error_section_beyond_eof;
723 }
724 __i2400m_msleep(20);
725 ret = i2400m_bm_cmd(i2400m, bh, section_size,
726 &ack, sizeof(ack), I2400M_BM_CMD_RAW);
727 if (ret < 0) {
728 dev_err(dev, "fw %s: section #%zu (@%zu %zu B) "
Inaky Perez-Gonzalez1039abb2009-02-28 23:42:47 +0000729 "failed %d\n", i2400m->fw_name, section,
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800730 offset, sizeof(*bh) + data_size, (int) ret);
731 goto error_send;
732 }
733 offset += section_size;
734 section++;
735 }
736 ret = offset;
737error_section_beyond_eof:
738error_send:
739 d_fnend(3, dev, "(i2400m %p bcf %p bcf_len %zu) = %d\n",
740 i2400m, bcf, bcf_len, (int) ret);
741 return ret;
742}
743
744
745/*
Inaky Perez-Gonzalez32742e62009-09-03 15:56:40 -0700746 * Indicate if the device emitted a reboot barker that indicates
747 * "signed boot"
748 */
749static
750unsigned i2400m_boot_is_signed(struct i2400m *i2400m)
751{
752 return likely(i2400m->sboot);
753}
754
755
756/*
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800757 * Do the final steps of uploading firmware
758 *
759 * Depending on the boot mode (signed vs non-signed), different
760 * actions need to be taken.
761 */
762static
763int i2400m_dnload_finalize(struct i2400m *i2400m,
764 const struct i2400m_bcf_hdr *bcf, size_t offset)
765{
766 int ret = 0;
767 struct device *dev = i2400m_dev(i2400m);
768 struct i2400m_bootrom_header *cmd, ack;
769 struct {
770 struct i2400m_bootrom_header cmd;
771 u8 cmd_pl[0];
772 } __attribute__((packed)) *cmd_buf;
773 size_t signature_block_offset, signature_block_size;
774
775 d_fnstart(3, dev, "offset %zu\n", offset);
776 cmd = (void *) bcf + offset;
Inaky Perez-Gonzalez32742e62009-09-03 15:56:40 -0700777 if (i2400m_boot_is_signed(i2400m) == 0) {
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800778 struct i2400m_bootrom_header jump_ack;
Dirk Brandewieead68232009-05-12 06:22:27 -0700779 d_printf(1, dev, "unsecure boot, jumping to 0x%08x\n",
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800780 le32_to_cpu(cmd->target_addr));
Cindy H Kao8d8fe192009-08-18 19:27:18 -0700781 cmd_buf = i2400m->bm_cmd_buf;
782 memcpy(&cmd_buf->cmd, cmd, sizeof(*cmd));
783 cmd = &cmd_buf->cmd;
784 /* now cmd points to the actual bootrom_header in cmd_buf */
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800785 i2400m_brh_set_opcode(cmd, I2400M_BRH_JUMP);
786 cmd->data_size = 0;
787 ret = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
788 &jump_ack, sizeof(jump_ack), 0);
789 } else {
Dirk Brandewieead68232009-05-12 06:22:27 -0700790 d_printf(1, dev, "secure boot, jumping to 0x%08x\n",
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800791 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 =
795 sizeof(*bcf)
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);
805 }
806 d_fnend(3, dev, "returning %d\n", ret);
807 return ret;
808}
809
810
811/**
812 * i2400m_bootrom_init - Reboots a powered device into boot mode
813 *
814 * @i2400m: device descriptor
815 * @flags:
Inaky Perez-Gonzalez923d7082009-09-04 14:50:59 -0700816 * I2400M_BRI_SOFT: a reboot barker has been seen
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800817 * already, so don't wait for it.
818 *
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.
822 *
823 * Returns:
824 *
825 * < 0 errno code on error, 0 if ok.
826 *
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800827 * Description:
828 *
829 * Tries hard enough to put the device in boot-mode. There are two
830 * main phases to this:
831 *
832 * a. (1) send a reboot command and (2) get a reboot barker
Inaky Perez-Gonzalez923d7082009-09-04 14:50:59 -0700833 *
834 * b. (1) echo/ack the reboot sending the reboot barker back and (2)
835 * getting an ack barker in return
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800836 *
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
841 * that.
842 *
Inaky Perez-Gonzalez923d7082009-09-04 14:50:59 -0700843 * 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.
852 *
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800853 * If we get a timeout after sending a warm reset, we do it again.
854 */
855int i2400m_bootrom_init(struct i2400m *i2400m, enum i2400m_bri flags)
856{
857 int result;
858 struct device *dev = i2400m_dev(i2400m);
859 struct i2400m_bootrom_header *cmd;
860 struct i2400m_bootrom_header ack;
Dirk Brandewiec3083652009-08-13 13:48:29 -0700861 int count = i2400m->bus_bm_retries;
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800862 int ack_timeout_cnt = 1;
Inaky Perez-Gonzalez923d7082009-09-04 14:50:59 -0700863 unsigned i;
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800864
Inaky Perez-Gonzalezaba37922009-09-03 15:14:29 -0700865 BUILD_BUG_ON(sizeof(*cmd) != sizeof(i2400m_barker_db[0].data));
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800866 BUILD_BUG_ON(sizeof(ack) != sizeof(i2400m_ACK_BARKER));
867
868 d_fnstart(4, dev, "(i2400m %p flags 0x%08x)\n", i2400m, flags);
869 result = -ENOMEM;
870 cmd = i2400m->bm_cmd_buf;
871 if (flags & I2400M_BRI_SOFT)
872 goto do_reboot_ack;
873do_reboot:
Inaky Perez-Gonzalez923d7082009-09-04 14:50:59 -0700874 ack_timeout_cnt = 1;
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800875 if (--count < 0)
876 goto error_timeout;
877 d_printf(4, dev, "device reboot: reboot command [%d # left]\n",
878 count);
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),
882 I2400M_BM_CMD_RAW);
883 flags &= ~I2400M_BRI_NO_REBOOT;
884 switch (result) {
885 case -ERESTARTSYS:
Inaky Perez-Gonzalez923d7082009-09-04 14:50:59 -0700886 /*
887 * at this point, i2400m_bm_cmd(), through
888 * __i2400m_bm_ack_process(), has updated
889 * i2400m->barker and we are good to go.
890 */
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800891 d_printf(4, dev, "device reboot: got reboot barker\n");
892 break;
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");
895 goto do_reboot;
Inaky Perez-Gonzalez923d7082009-09-04 14:50:59 -0700896 case -ETIMEDOUT:
897 /*
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.
902 */
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]));
Inaky Perez-Gonzalezaba37922009-09-03 15:14:29 -0700907 goto do_reboot_ack;
908 }
Inaky Perez-Gonzalez923d7082009-09-04 14:50:59 -0700909 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),
913 &ack, sizeof(ack),
914 I2400M_BM_CMD_RAW);
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;
921 goto do_reboot;
922 }
923 }
924 dev_err(dev, "device boot: tried all the echo/acks, could "
925 "not get device to respond; giving up");
926 result = -ESHUTDOWN;
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800927 case -EPROTO:
928 case -ESHUTDOWN: /* dev is gone */
929 case -EINTR: /* user cancelled */
930 goto error_dev_gone;
931 default:
932 dev_err(dev, "device reboot: error %d while waiting "
933 "for reboot barker - rebooting\n", result);
Inaky Perez-Gonzalez923d7082009-09-04 14:50:59 -0700934 d_dump(1, dev, &ack, result);
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800935 goto do_reboot;
936 }
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. */
941do_reboot_ack:
942 d_printf(4, dev, "device reboot ack: sending ack [%d # left]\n", count);
Inaky Perez-Gonzalezaba37922009-09-03 15:14:29 -0700943 memcpy(cmd, i2400m->barker->data, sizeof(i2400m->barker->data));
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800944 result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
945 &ack, sizeof(ack), I2400M_BM_CMD_RAW);
946 switch (result) {
947 case -ERESTARTSYS:
948 d_printf(4, dev, "reboot ack: got reboot barker - retrying\n");
949 if (--count < 0)
950 goto error_timeout;
951 goto do_reboot_ack;
952 case -EISCONN:
953 d_printf(4, dev, "reboot ack: got ack barker - good\n");
954 break;
955 case -ETIMEDOUT: /* no response, maybe it is the other type? */
Inaky Perez-Gonzalezaba37922009-09-03 15:14:29 -0700956 if (ack_timeout_cnt-- < 0) {
957 d_printf(4, dev, "reboot ack timedout: retrying\n");
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800958 goto do_reboot_ack;
959 } else {
960 dev_err(dev, "reboot ack timedout too long: "
961 "trying reboot\n");
962 goto do_reboot;
963 }
964 break;
965 case -EPROTO:
966 case -ESHUTDOWN: /* dev is gone */
967 goto error_dev_gone;
968 default:
969 dev_err(dev, "device reboot ack: error %d while waiting for "
970 "reboot ack barker - rebooting\n", result);
971 goto do_reboot;
972 }
973 d_printf(2, dev, "device reboot ack: got ack barker - boot done\n");
974 result = 0;
975exit_timeout:
976error_dev_gone:
977 d_fnend(4, dev, "(i2400m %p flags 0x%08x) = %d\n",
978 i2400m, flags, result);
979 return result;
980
981error_timeout:
Inaky Perez-Gonzalez6e053d62009-06-05 09:31:26 +0800982 dev_err(dev, "Timed out waiting for reboot ack\n");
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -0800983 result = -ETIMEDOUT;
984 goto exit_timeout;
985}
986
987
988/*
989 * Read the MAC addr
990 *
991 * The position this function reads is fixed in device memory and
992 * always available, even without firmware.
993 *
994 * Note we specify we want to read only six bytes, but provide space
995 * for 16, as we always get it rounded up.
996 */
997int i2400m_read_mac_addr(struct i2400m *i2400m)
998{
999 int result;
1000 struct device *dev = i2400m_dev(i2400m);
1001 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
1002 struct i2400m_bootrom_header *cmd;
1003 struct {
1004 struct i2400m_bootrom_header ack;
1005 u8 ack_pl[16];
1006 } __attribute__((packed)) ack_buf;
1007
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);
1015 if (result < 0) {
1016 dev_err(dev, "BM: read mac addr failed: %d\n", result);
1017 goto error_read_mac;
1018 }
1019 d_printf(2, dev,
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]);
1034 result = 0;
1035 }
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);
1039error_read_mac:
1040 d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, result);
1041 return result;
1042}
1043
1044
1045/*
1046 * Initialize a non signed boot
1047 *
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
1050 * declaration.
1051 */
1052static
1053int i2400m_dnload_init_nonsigned(struct i2400m *i2400m)
1054{
Dirk Brandewie7308a0c2009-05-21 11:56:34 -07001055 unsigned i = 0;
1056 int ret = 0;
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001057 struct device *dev = i2400m_dev(i2400m);
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001058 d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
Dirk Brandewie7308a0c2009-05-21 11:56:34 -07001059 if (i2400m->bus_bm_pokes_table) {
1060 while (i2400m->bus_bm_pokes_table[i].address) {
1061 ret = i2400m_download_chunk(
1062 i2400m,
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);
1066 if (ret < 0)
1067 break;
1068 i++;
1069 }
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001070 }
1071 d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, ret);
1072 return ret;
1073}
1074
1075
1076/*
1077 * Initialize the signed boot process
1078 *
1079 * @i2400m: device descriptor
1080 *
1081 * @bcf_hdr: pointer to the firmware header; assumes it is fully in
1082 * memory (it has gone through basic validation).
1083 *
1084 * Returns: 0 if ok, < 0 errno code on error, -ERESTARTSYS if the hw
1085 * rebooted.
1086 *
1087 * This writes the firmware BCF header to the device using the
1088 * HASH_PAYLOAD_ONLY command.
1089 */
1090static
1091int i2400m_dnload_init_signed(struct i2400m *i2400m,
1092 const struct i2400m_bcf_hdr *bcf_hdr)
1093{
1094 int ret;
1095 struct device *dev = i2400m_dev(i2400m);
1096 struct {
1097 struct i2400m_bootrom_header cmd;
1098 struct i2400m_bcf_hdr cmd_pl;
1099 } __attribute__((packed)) *cmd_buf;
1100 struct i2400m_bootrom_header ack;
1101
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);
1111 if (ret >= 0)
1112 ret = 0;
1113 d_fnend(5, dev, "(i2400m %p bcf_hdr %p) = %d\n", i2400m, bcf_hdr, ret);
1114 return ret;
1115}
1116
1117
1118/*
1119 * Initialize the firmware download at the device size
1120 *
1121 * Multiplex to the one that matters based on the device's mode
1122 * (signed or non-signed).
1123 */
1124static
1125int i2400m_dnload_init(struct i2400m *i2400m, const struct i2400m_bcf_hdr *bcf)
1126{
1127 int result;
1128 struct device *dev = i2400m_dev(i2400m);
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001129
Inaky Perez-Gonzalez32742e62009-09-03 15:56:40 -07001130 if (i2400m_boot_is_signed(i2400m)) {
1131 d_printf(1, dev, "signed boot\n");
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001132 result = i2400m_dnload_init_signed(i2400m, bcf);
1133 if (result == -ERESTARTSYS)
1134 return result;
1135 if (result < 0)
Inaky Perez-Gonzalez32742e62009-09-03 15:56:40 -07001136 dev_err(dev, "firmware %s: signed boot download "
1137 "initialization failed: %d\n",
1138 i2400m->fw_name, result);
1139 } else {
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)
1144 return result;
1145 if (result < 0)
1146 dev_err(dev, "firmware %s: non-signed download "
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001147 "initialization failed: %d\n",
Inaky Perez-Gonzalez1039abb2009-02-28 23:42:47 +00001148 i2400m->fw_name, result);
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001149 }
1150 return result;
1151}
1152
1153
1154/*
Inaky Perez-Gonzalezbfc44182009-09-04 17:07:21 -07001155 * Run consistency tests on the firmware file and load up headers
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001156 *
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
1160 * went wrong.
Inaky Perez-Gonzalezbfc44182009-09-04 17:07:21 -07001161 *
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.
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001164 */
1165static
Inaky Perez-Gonzalezbfc44182009-09-04 17:07:21 -07001166int i2400m_fw_hdr_check(struct i2400m *i2400m,
1167 const struct i2400m_bcf_hdr *bcf_hdr,
1168 size_t index, size_t offset)
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001169{
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001170 struct device *dev = i2400m_dev(i2400m);
Inaky Perez-Gonzalezbfc44182009-09-04 17:07:21 -07001171
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001172 unsigned module_type, header_len, major_version, minor_version,
1173 module_id, module_vendor, date, size;
1174
Inaky Perez-Gonzalezbfc44182009-09-04 17:07:21 -07001175 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)
1178 >> 16;
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);
1184
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);
1190
1191 /* Hard errors */
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);
1197 return -EBADF;
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001198 }
1199
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001200 if (module_type != 6) { /* built for the right hardware? */
Inaky Perez-Gonzalezbfc44182009-09-04 17:07:21 -07001201 dev_err(dev, "firmware %s #%d@%08x: unexpected module "
1202 "type 0x%x; aborting\n",
1203 i2400m->fw_name, index, offset,
1204 module_type);
1205 return -EBADF;
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001206 }
1207
Inaky Perez-Gonzalezbfc44182009-09-04 17:07:21 -07001208 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);
1212 return -EBADF;
Inaky Perez-Gonzalezfabce1a2009-09-04 14:53:43 -07001213 }
1214
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001215 if (date < 0x20080300)
Inaky Perez-Gonzalezbfc44182009-09-04 17:07:21 -07001216 dev_warn(dev, "firmware %s #%d@%08x: build date %08x "
1217 "too old; unsupported\n",
1218 i2400m->fw_name, index, offset, date);
1219 return 0;
1220}
1221
1222
1223/*
1224 * Run consistency tests on the firmware file and load up headers
1225 *
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
1229 * went wrong.
1230 *
1231 * This will also look at all the headers present on the firmware
1232 * file, and update i2400m->fw_hdrs to point to them.
1233 */
1234static
1235int i2400m_fw_check(struct i2400m *i2400m, const void *bcf, size_t bcf_size)
1236{
1237 int result;
1238 struct device *dev = i2400m_dev(i2400m);
1239 size_t headers = 0;
1240 const struct i2400m_bcf_hdr *bcf_hdr;
1241 const void *itr, *next, *top;
1242 unsigned slots = 0, used_slots = 0;
1243
1244 for (itr = bcf, top = itr + bcf_size;
1245 itr < top;
1246 headers++, itr = next) {
1247 size_t leftover, offset, header_len, size;
1248
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);
1255 break;
1256 }
1257 bcf_hdr = itr;
1258 /* Only the first header is supposed to be followed by
1259 * payload */
1260 header_len = sizeof(u32) * le32_to_cpu(bcf_hdr->header_len);
1261 size = sizeof(u32) * le32_to_cpu(bcf_hdr->size);
1262 if (headers == 0)
1263 next = itr + size;
1264 else
1265 next = itr + header_len;
1266
1267 result = i2400m_fw_hdr_check(i2400m, bcf_hdr, headers, offset);
1268 if (result < 0)
1269 continue;
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]),
1277 GFP_KERNEL);
1278 if (result < 0)
1279 goto error_zrealloc;
1280 }
1281 i2400m->fw_hdrs[used_slots] = bcf_hdr;
1282 used_slots++;
1283 }
1284 if (headers == 0) {
1285 dev_err(dev, "firmware %s: no usable headers found\n",
1286 i2400m->fw_name);
1287 result = -EBADF;
1288 } else
1289 result = 0;
1290error_zrealloc:
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001291 return result;
1292}
1293
1294
1295/*
1296 * Download the firmware to the device
1297 *
1298 * @i2400m: device descriptor
1299 * @bcf: pointer to loaded (and minimally verified for consistency)
1300 * firmware
1301 * @bcf_size: size of the @bcf buffer (header plus payloads)
1302 *
1303 * The process for doing this is described in this file's header.
1304 *
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.
1308 */
1309static
1310int i2400m_fw_dnload(struct i2400m *i2400m, const struct i2400m_bcf_hdr *bcf,
1311 size_t bcf_size, enum i2400m_bri flags)
1312{
1313 int ret = 0;
1314 struct device *dev = i2400m_dev(i2400m);
Inaky Perez-Gonzalezecddfd52009-06-03 16:13:14 +08001315 int count = i2400m->bus_bm_retries;
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001316
1317 d_fnstart(5, dev, "(i2400m %p bcf %p size %zu)\n",
1318 i2400m, bcf, bcf_size);
1319 i2400m->boot_mode = 1;
Inaky Perez-Gonzalezb4013f92009-06-03 09:45:55 +08001320 wmb(); /* Make sure other readers see it */
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001321hw_reboot:
1322 if (count-- == 0) {
1323 ret = -ERESTARTSYS;
1324 dev_err(dev, "device rebooted too many times, aborting\n");
1325 goto error_too_many_reboots;
1326 }
1327 if (flags & I2400M_BRI_MAC_REINIT) {
1328 ret = i2400m_bootrom_init(i2400m, flags);
1329 if (ret < 0) {
1330 dev_err(dev, "bootrom init failed: %d\n", ret);
1331 goto error_bootrom_init;
1332 }
1333 }
1334 flags |= I2400M_BRI_MAC_REINIT;
1335
1336 /*
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()
1340 */
1341 ret = i2400m_dnload_init(i2400m, bcf); /* Init device's dnload */
1342 if (ret == -ERESTARTSYS)
1343 goto error_dev_rebooted;
1344 if (ret < 0)
1345 goto error_dnload_init;
1346
1347 ret = i2400m_dnload_bcf(i2400m, bcf, bcf_size);
1348 if (ret == -ERESTARTSYS)
1349 goto error_dev_rebooted;
1350 if (ret < 0) {
1351 dev_err(dev, "fw %s: download failed: %d\n",
Inaky Perez-Gonzalez1039abb2009-02-28 23:42:47 +00001352 i2400m->fw_name, ret);
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001353 goto error_dnload_bcf;
1354 }
1355
1356 ret = i2400m_dnload_finalize(i2400m, bcf, ret);
1357 if (ret == -ERESTARTSYS)
1358 goto error_dev_rebooted;
1359 if (ret < 0) {
1360 dev_err(dev, "fw %s: "
1361 "download finalization failed: %d\n",
Inaky Perez-Gonzalez1039abb2009-02-28 23:42:47 +00001362 i2400m->fw_name, ret);
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001363 goto error_dnload_finalize;
1364 }
1365
1366 d_printf(2, dev, "fw %s successfully uploaded\n",
Inaky Perez-Gonzalez1039abb2009-02-28 23:42:47 +00001367 i2400m->fw_name);
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001368 i2400m->boot_mode = 0;
Inaky Perez-Gonzalezb4013f92009-06-03 09:45:55 +08001369 wmb(); /* Make sure i2400m_msg_to_dev() sees boot_mode */
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001370error_dnload_finalize:
1371error_dnload_bcf:
1372error_dnload_init:
1373error_bootrom_init:
1374error_too_many_reboots:
1375 d_fnend(5, dev, "(i2400m %p bcf %p size %zu) = %d\n",
1376 i2400m, bcf, bcf_size, ret);
1377 return ret;
1378
1379error_dev_rebooted:
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;
1383 goto hw_reboot;
1384}
1385
1386
1387/**
1388 * i2400m_dev_bootstrap - Bring the device to a known state and upload firmware
1389 *
1390 * @i2400m: device descriptor
1391 *
1392 * Returns: >= 0 if ok, < 0 errno code on error.
1393 *
1394 * This sets up the firmware upload environment, loads the firmware
1395 * file from disk, verifies and then calls the firmware upload process
1396 * per se.
1397 *
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.
1401 */
1402int i2400m_dev_bootstrap(struct i2400m *i2400m, enum i2400m_bri flags)
1403{
Inaky Perez-Gonzalezebc5f622009-09-03 15:53:30 -07001404 int ret, itr;
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001405 struct device *dev = i2400m_dev(i2400m);
1406 const struct firmware *fw;
1407 const struct i2400m_bcf_hdr *bcf; /* Firmware data */
Inaky Perez-Gonzalez1039abb2009-02-28 23:42:47 +00001408 const char *fw_name;
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001409
1410 d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001411
Inaky Perez-Gonzalez1039abb2009-02-28 23:42:47 +00001412 /* Load firmware files to memory. */
Inaky Perez-Gonzalezebc5f622009-09-03 15:53:30 -07001413 for (itr = 0, bcf = NULL, ret = -ENOENT; ; itr++) {
Inaky Perez-Gonzalez1039abb2009-02-28 23:42:47 +00001414 fw_name = i2400m->bus_fw_names[itr];
1415 if (fw_name == NULL) {
1416 dev_err(dev, "Could not find a usable firmware image\n");
1417 ret = -ENOENT;
Inaky Perez-Gonzalezebc5f622009-09-03 15:53:30 -07001418 break;
Inaky Perez-Gonzalez1039abb2009-02-28 23:42:47 +00001419 }
Inaky Perez-Gonzalezebc5f622009-09-03 15:53:30 -07001420 d_printf(1, dev, "trying firmware %s (%d)\n", fw_name, itr);
Inaky Perez-Gonzalez1039abb2009-02-28 23:42:47 +00001421 ret = request_firmware(&fw, fw_name, dev);
Inaky Perez-Gonzalezebc5f622009-09-03 15:53:30 -07001422 if (ret < 0) {
Inaky Perez-Gonzalez1039abb2009-02-28 23:42:47 +00001423 dev_err(dev, "fw %s: cannot load file: %d\n",
1424 fw_name, ret);
Inaky Perez-Gonzalezebc5f622009-09-03 15:53:30 -07001425 continue;
1426 }
1427 bcf = (void *) fw->data;
1428 i2400m->fw_name = fw_name;
1429 ret = i2400m_fw_check(i2400m, bcf, fw->size);
Inaky Perez-Gonzalezbfc44182009-09-04 17:07:21 -07001430 if (ret >= 0)
Inaky Perez-Gonzalezebc5f622009-09-03 15:53:30 -07001431 ret = i2400m_fw_dnload(i2400m, bcf, fw->size, flags);
Inaky Perez-Gonzalezbfc44182009-09-04 17:07:21 -07001432 if (ret < 0)
1433 dev_err(dev, "%s: cannot use: %d, skipping\n",
1434 fw_name, ret);
1435 kfree(i2400m->fw_hdrs);
1436 i2400m->fw_hdrs = NULL;
Inaky Perez-Gonzalezebc5f622009-09-03 15:53:30 -07001437 release_firmware(fw);
Inaky Perez-Gonzalezbfc44182009-09-04 17:07:21 -07001438 if (ret >= 0) /* firmware loaded succesfully */
1439 break;
Inaky Perez-Gonzalez1039abb2009-02-28 23:42:47 +00001440 }
Inaky Perez-Gonzalez467cc392008-12-20 16:57:46 -08001441 d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, ret);
1442 return ret;
1443}
1444EXPORT_SYMBOL_GPL(i2400m_dev_bootstrap);