Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Intel Wireless WiMAX Connection 2400m |
| 3 | * Firmware uploader |
| 4 | * |
| 5 | * |
| 6 | * Copyright (C) 2007-2008 Intel Corporation. All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * * Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * * Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * * Neither the name of Intel Corporation nor the names of its |
| 19 | * contributors may be used to endorse or promote products derived |
| 20 | * from this software without specific prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | * |
| 34 | * |
| 35 | * Intel Corporation <linux-wimax@intel.com> |
| 36 | * Yanir Lubetkin <yanirx.lubetkin@intel.com> |
| 37 | * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> |
| 38 | * - Initial implementation |
| 39 | * |
| 40 | * |
| 41 | * THE PROCEDURE |
| 42 | * |
Inaky Perez-Gonzalez | aba3792 | 2009-09-03 15:14:29 -0700 | [diff] [blame] | 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. |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 46 | * |
| 47 | * The 2400m enters boot mode when it is first connected to the |
| 48 | * system, when it crashes and when you ask it to reboot. There are |
| 49 | * two submodes of the boot mode: signed and non-signed. Signed takes |
| 50 | * firmwares signed with a certain private key, non-signed takes any |
| 51 | * firmware. Normal hardware takes only signed firmware. |
| 52 | * |
Inaky Perez-Gonzalez | aba3792 | 2009-09-03 15:14:29 -0700 | [diff] [blame] | 53 | * On boot mode, in USB, we write to the device using the bulk out |
| 54 | * endpoint and read from it in the notification endpoint. In SDIO we |
| 55 | * talk to it via the write address and read from the read address. |
| 56 | * |
| 57 | * Upon entrance to boot mode, the device sends (preceeded with a few |
| 58 | * zero length packets (ZLPs) on the notification endpoint in USB) a |
| 59 | * reboot barker (4 le32 words with the same value). We ack it by |
| 60 | * sending the same barker to the device. The device acks with a |
| 61 | * reboot ack barker (4 le32 words with value I2400M_ACK_BARKER) and |
| 62 | * then is fully booted. At this point we can upload the firmware. |
| 63 | * |
| 64 | * Note that different iterations of the device and EEPROM |
| 65 | * configurations will send different [re]boot barkers; these are |
| 66 | * collected in i2400m_barker_db along with the firmware |
| 67 | * characteristics they require. |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 68 | * |
| 69 | * This process is accomplished by the i2400m_bootrom_init() |
| 70 | * function. All the device interaction happens through the |
| 71 | * i2400m_bm_cmd() [boot mode command]. Special return values will |
Inaky Perez-Gonzalez | aba3792 | 2009-09-03 15:14:29 -0700 | [diff] [blame] | 72 | * indicate if the device did reset during the process. |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 73 | * |
| 74 | * After this, we read the MAC address and then (if needed) |
| 75 | * reinitialize the device. We need to read it ahead of time because |
| 76 | * in the future, we might not upload the firmware until userspace |
| 77 | * 'ifconfig up's the device. |
| 78 | * |
| 79 | * We can then upload the firmware file. The file is composed of a BCF |
| 80 | * header (basic data, keys and signatures) and a list of write |
| 81 | * commands and payloads. We first upload the header |
| 82 | * [i2400m_dnload_init()] and then pass the commands and payloads |
| 83 | * verbatim to the i2400m_bm_cmd() function |
| 84 | * [i2400m_dnload_bcf()]. Then we tell the device to jump to the new |
| 85 | * firmware [i2400m_dnload_finalize()]. |
| 86 | * |
| 87 | * Once firmware is uploaded, we are good to go :) |
| 88 | * |
| 89 | * When we don't know in which mode we are, we first try by sending a |
| 90 | * warm reset request that will take us to boot-mode. If we time out |
| 91 | * waiting for a reboot barker, that means maybe we are already in |
| 92 | * boot mode, so we send a reboot barker. |
| 93 | * |
| 94 | * COMMAND EXECUTION |
| 95 | * |
| 96 | * This code (and process) is single threaded; for executing commands, |
| 97 | * we post a URB to the notification endpoint, post the command, wait |
| 98 | * for data on the notification buffer. We don't need to worry about |
| 99 | * others as we know we are the only ones in there. |
| 100 | * |
| 101 | * BACKEND IMPLEMENTATION |
| 102 | * |
| 103 | * This code is bus-generic; the bus-specific driver provides back end |
| 104 | * implementations to send a boot mode command to the device and to |
| 105 | * read an acknolwedgement from it (or an asynchronous notification) |
| 106 | * from it. |
| 107 | * |
| 108 | * ROADMAP |
| 109 | * |
Inaky Perez-Gonzalez | aba3792 | 2009-09-03 15:14:29 -0700 | [diff] [blame] | 110 | * 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-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 115 | * i2400m_dev_bootstrap Called by __i2400m_dev_start() |
| 116 | * request_firmware |
| 117 | * i2400m_fw_check |
| 118 | * i2400m_fw_dnload |
| 119 | * release_firmware |
| 120 | * |
| 121 | * i2400m_fw_dnload |
| 122 | * i2400m_bootrom_init |
| 123 | * i2400m_bm_cmd |
| 124 | * i2400m->bus_reset |
| 125 | * i2400m_dnload_init |
| 126 | * i2400m_dnload_init_signed |
| 127 | * i2400m_dnload_init_nonsigned |
| 128 | * i2400m_download_chunk |
| 129 | * i2400m_bm_cmd |
| 130 | * i2400m_dnload_bcf |
| 131 | * i2400m_bm_cmd |
| 132 | * i2400m_dnload_finalize |
| 133 | * i2400m_bm_cmd |
| 134 | * |
| 135 | * i2400m_bm_cmd |
| 136 | * i2400m->bus_bm_cmd_send() |
| 137 | * i2400m->bus_bm_wait_for_ack |
| 138 | * __i2400m_bm_ack_verify |
Inaky Perez-Gonzalez | aba3792 | 2009-09-03 15:14:29 -0700 | [diff] [blame] | 139 | * i2400m_is_boot_barker |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 140 | * |
| 141 | * i2400m_bm_cmd_prepare Used by bus-drivers to prep |
| 142 | * commands before sending |
| 143 | */ |
| 144 | #include <linux/firmware.h> |
| 145 | #include <linux/sched.h> |
| 146 | #include <linux/usb.h> |
| 147 | #include "i2400m.h" |
| 148 | |
| 149 | |
| 150 | #define D_SUBMODULE fw |
| 151 | #include "debug-levels.h" |
| 152 | |
| 153 | |
| 154 | static const __le32 i2400m_ACK_BARKER[4] = { |
Harvey Harrison | ee43777 | 2009-02-01 00:43:54 -0800 | [diff] [blame] | 155 | cpu_to_le32(I2400M_ACK_BARKER), |
| 156 | cpu_to_le32(I2400M_ACK_BARKER), |
| 157 | cpu_to_le32(I2400M_ACK_BARKER), |
| 158 | cpu_to_le32(I2400M_ACK_BARKER) |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | |
| 162 | /** |
| 163 | * Prepare a boot-mode command for delivery |
| 164 | * |
| 165 | * @cmd: pointer to bootrom header to prepare |
| 166 | * |
| 167 | * Computes checksum if so needed. After calling this function, DO NOT |
| 168 | * modify the command or header as the checksum won't work anymore. |
| 169 | * |
| 170 | * We do it from here because some times we cannot do it in the |
| 171 | * original context the command was sent (it is a const), so when we |
| 172 | * copy it to our staging buffer, we add the checksum there. |
| 173 | */ |
| 174 | void i2400m_bm_cmd_prepare(struct i2400m_bootrom_header *cmd) |
| 175 | { |
| 176 | if (i2400m_brh_get_use_checksum(cmd)) { |
| 177 | int i; |
| 178 | u32 checksum = 0; |
| 179 | const u32 *checksum_ptr = (void *) cmd->payload; |
| 180 | for (i = 0; i < cmd->data_size / 4; i++) |
| 181 | checksum += cpu_to_le32(*checksum_ptr++); |
| 182 | checksum += cmd->command + cmd->target_addr + cmd->data_size; |
| 183 | cmd->block_checksum = cpu_to_le32(checksum); |
| 184 | } |
| 185 | } |
| 186 | EXPORT_SYMBOL_GPL(i2400m_bm_cmd_prepare); |
| 187 | |
| 188 | |
| 189 | /* |
Inaky Perez-Gonzalez | aba3792 | 2009-09-03 15:14:29 -0700 | [diff] [blame] | 190 | * Database of known barkers. |
| 191 | * |
| 192 | * A barker is what the device sends indicating he is ready to be |
| 193 | * bootloaded. Different versions of the device will send different |
| 194 | * barkers. Depending on the barker, it might mean the device wants |
| 195 | * some kind of firmware or the other. |
| 196 | */ |
| 197 | static struct i2400m_barker_db { |
| 198 | __le32 data[4]; |
| 199 | } *i2400m_barker_db; |
| 200 | static size_t i2400m_barker_db_used, i2400m_barker_db_size; |
| 201 | |
| 202 | |
| 203 | static |
| 204 | int i2400m_zrealloc_2x(void **ptr, size_t *_count, size_t el_size, |
| 205 | gfp_t gfp_flags) |
| 206 | { |
| 207 | size_t old_count = *_count, |
| 208 | new_count = old_count ? 2 * old_count : 2, |
| 209 | old_size = el_size * old_count, |
| 210 | new_size = el_size * new_count; |
| 211 | void *nptr = krealloc(*ptr, new_size, gfp_flags); |
| 212 | if (nptr) { |
| 213 | /* zero the other half or the whole thing if old_count |
| 214 | * was zero */ |
| 215 | if (old_size == 0) |
| 216 | memset(nptr, 0, new_size); |
| 217 | else |
| 218 | memset(nptr + old_size, 0, old_size); |
| 219 | *_count = new_count; |
| 220 | *ptr = nptr; |
| 221 | return 0; |
| 222 | } else |
| 223 | return -ENOMEM; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | /* |
| 228 | * Add a barker to the database |
| 229 | * |
| 230 | * This cannot used outside of this module and only at at module_init |
| 231 | * time. This is to avoid the need to do locking. |
| 232 | */ |
| 233 | static |
| 234 | int i2400m_barker_db_add(u32 barker_id) |
| 235 | { |
| 236 | int result; |
| 237 | |
| 238 | struct i2400m_barker_db *barker; |
| 239 | if (i2400m_barker_db_used >= i2400m_barker_db_size) { |
| 240 | result = i2400m_zrealloc_2x( |
| 241 | (void **) &i2400m_barker_db, &i2400m_barker_db_size, |
| 242 | sizeof(i2400m_barker_db[0]), GFP_KERNEL); |
| 243 | if (result < 0) |
| 244 | return result; |
| 245 | } |
| 246 | barker = i2400m_barker_db + i2400m_barker_db_used++; |
| 247 | barker->data[0] = le32_to_cpu(barker_id); |
| 248 | barker->data[1] = le32_to_cpu(barker_id); |
| 249 | barker->data[2] = le32_to_cpu(barker_id); |
| 250 | barker->data[3] = le32_to_cpu(barker_id); |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | void i2400m_barker_db_exit(void) |
| 256 | { |
| 257 | kfree(i2400m_barker_db); |
| 258 | i2400m_barker_db = NULL; |
| 259 | i2400m_barker_db_size = 0; |
| 260 | i2400m_barker_db_used = 0; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | /* |
| 265 | * Helper function to add all the known stable barkers to the barker |
| 266 | * database. |
| 267 | */ |
| 268 | static |
| 269 | int i2400m_barker_db_known_barkers(void) |
| 270 | { |
| 271 | int result; |
| 272 | |
| 273 | result = i2400m_barker_db_add(I2400M_NBOOT_BARKER); |
| 274 | if (result < 0) |
| 275 | goto error_add; |
| 276 | result = i2400m_barker_db_add(I2400M_SBOOT_BARKER); |
| 277 | if (result < 0) |
| 278 | goto error_add; |
| 279 | error_add: |
| 280 | return result; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | /* |
| 285 | * Initialize the barker database |
| 286 | * |
| 287 | * This can only be used from the module_init function for this |
| 288 | * module; this is to avoid the need to do locking. |
| 289 | * |
| 290 | * @options: command line argument with extra barkers to |
| 291 | * recognize. This is a comma-separated list of 32-bit hex |
| 292 | * numbers. They are appended to the existing list. Setting 0 |
| 293 | * cleans the existing list and starts a new one. |
| 294 | */ |
| 295 | int i2400m_barker_db_init(const char *_options) |
| 296 | { |
| 297 | int result; |
| 298 | char *options = NULL, *options_orig, *token; |
| 299 | |
| 300 | i2400m_barker_db = NULL; |
| 301 | i2400m_barker_db_size = 0; |
| 302 | i2400m_barker_db_used = 0; |
| 303 | |
| 304 | result = i2400m_barker_db_known_barkers(); |
| 305 | if (result < 0) |
| 306 | goto error_add; |
| 307 | /* parse command line options from i2400m.barkers */ |
| 308 | if (_options != NULL) { |
| 309 | unsigned barker; |
| 310 | |
| 311 | options_orig = kstrdup(_options, GFP_KERNEL); |
| 312 | if (options_orig == NULL) |
| 313 | goto error_parse; |
| 314 | options = options_orig; |
| 315 | |
| 316 | while ((token = strsep(&options, ",")) != NULL) { |
| 317 | if (*token == '\0') /* eat joint commas */ |
| 318 | continue; |
| 319 | if (sscanf(token, "%x", &barker) != 1 |
| 320 | || barker > 0xffffffff) { |
| 321 | printk(KERN_ERR "%s: can't recognize " |
| 322 | "i2400m.barkers value '%s' as " |
| 323 | "a 32-bit number\n", |
| 324 | __func__, token); |
| 325 | result = -EINVAL; |
| 326 | goto error_parse; |
| 327 | } |
| 328 | if (barker == 0) { |
| 329 | /* clean list and start new */ |
| 330 | i2400m_barker_db_exit(); |
| 331 | continue; |
| 332 | } |
| 333 | result = i2400m_barker_db_add(barker); |
| 334 | if (result < 0) |
| 335 | goto error_add; |
| 336 | } |
| 337 | kfree(options_orig); |
| 338 | } |
| 339 | return 0; |
| 340 | |
| 341 | error_parse: |
| 342 | error_add: |
| 343 | kfree(i2400m_barker_db); |
| 344 | return result; |
| 345 | } |
| 346 | |
| 347 | |
| 348 | /* |
| 349 | * Recognize a boot barker |
| 350 | * |
| 351 | * @buf: buffer where the boot barker. |
| 352 | * @buf_size: size of the buffer (has to be 16 bytes). It is passed |
| 353 | * here so the function can check it for the caller. |
| 354 | * |
| 355 | * Note that as a side effect, upon identifying the obtained boot |
| 356 | * barker, this function will set i2400m->barker to point to the right |
| 357 | * barker database entry. Subsequent calls to the function will result |
| 358 | * in verifying that the same type of boot barker is returned when the |
| 359 | * device [re]boots (as long as the same device instance is used). |
| 360 | * |
| 361 | * Return: 0 if @buf matches a known boot barker. -ENOENT if the |
| 362 | * buffer in @buf doesn't match any boot barker in the database or |
| 363 | * -EILSEQ if the buffer doesn't have the right size. |
| 364 | */ |
| 365 | int i2400m_is_boot_barker(struct i2400m *i2400m, |
| 366 | const void *buf, size_t buf_size) |
| 367 | { |
| 368 | int result; |
| 369 | struct device *dev = i2400m_dev(i2400m); |
| 370 | struct i2400m_barker_db *barker; |
| 371 | int i; |
| 372 | |
| 373 | result = -ENOENT; |
| 374 | if (buf_size != sizeof(i2400m_barker_db[i].data)) |
| 375 | return result; |
| 376 | |
| 377 | /* Short circuit if we have already discovered the barker |
| 378 | * associated with the device. */ |
| 379 | if (i2400m->barker |
| 380 | && !memcmp(buf, i2400m->barker, sizeof(i2400m->barker->data))) { |
| 381 | unsigned index = (i2400m->barker - i2400m_barker_db) |
| 382 | / sizeof(*i2400m->barker); |
| 383 | d_printf(2, dev, "boot barker cache-confirmed #%u/%08x\n", |
| 384 | index, le32_to_cpu(i2400m->barker->data[0])); |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | for (i = 0; i < i2400m_barker_db_used; i++) { |
| 389 | barker = &i2400m_barker_db[i]; |
| 390 | BUILD_BUG_ON(sizeof(barker->data) != 16); |
| 391 | if (memcmp(buf, barker->data, sizeof(barker->data))) |
| 392 | continue; |
| 393 | |
| 394 | if (i2400m->barker == NULL) { |
| 395 | i2400m->barker = barker; |
| 396 | d_printf(1, dev, "boot barker set to #%u/%08x\n", |
| 397 | i, le32_to_cpu(barker->data[0])); |
| 398 | if (barker->data[0] == le32_to_cpu(I2400M_NBOOT_BARKER)) |
| 399 | i2400m->sboot = 0; |
| 400 | else |
| 401 | i2400m->sboot = 1; |
| 402 | } else if (i2400m->barker != barker) { |
| 403 | dev_err(dev, "HW inconsistency: device " |
| 404 | "reports a different boot barker " |
| 405 | "than set (from %08x to %08x)\n", |
| 406 | le32_to_cpu(i2400m->barker->data[0]), |
| 407 | le32_to_cpu(barker->data[0])); |
| 408 | result = -EIO; |
| 409 | } else |
| 410 | d_printf(2, dev, "boot barker confirmed #%u/%08x\n", |
| 411 | i, le32_to_cpu(barker->data[0])); |
| 412 | result = 0; |
| 413 | break; |
| 414 | } |
| 415 | return result; |
| 416 | } |
| 417 | EXPORT_SYMBOL_GPL(i2400m_is_boot_barker); |
| 418 | |
| 419 | |
| 420 | /* |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 421 | * Verify the ack data received |
| 422 | * |
| 423 | * Given a reply to a boot mode command, chew it and verify everything |
| 424 | * is ok. |
| 425 | * |
| 426 | * @opcode: opcode which generated this ack. For error messages. |
| 427 | * @ack: pointer to ack data we received |
| 428 | * @ack_size: size of that data buffer |
| 429 | * @flags: I2400M_BM_CMD_* flags we called the command with. |
| 430 | * |
| 431 | * Way too long function -- maybe it should be further split |
| 432 | */ |
| 433 | static |
| 434 | ssize_t __i2400m_bm_ack_verify(struct i2400m *i2400m, int opcode, |
| 435 | struct i2400m_bootrom_header *ack, |
| 436 | size_t ack_size, int flags) |
| 437 | { |
| 438 | ssize_t result = -ENOMEM; |
| 439 | struct device *dev = i2400m_dev(i2400m); |
| 440 | |
| 441 | d_fnstart(8, dev, "(i2400m %p opcode %d ack %p size %zu)\n", |
| 442 | i2400m, opcode, ack, ack_size); |
| 443 | if (ack_size < sizeof(*ack)) { |
| 444 | result = -EIO; |
| 445 | dev_err(dev, "boot-mode cmd %d: HW BUG? notification didn't " |
| 446 | "return enough data (%zu bytes vs %zu expected)\n", |
| 447 | opcode, ack_size, sizeof(*ack)); |
| 448 | goto error_ack_short; |
| 449 | } |
Inaky Perez-Gonzalez | aba3792 | 2009-09-03 15:14:29 -0700 | [diff] [blame] | 450 | result = i2400m_is_boot_barker(i2400m, ack, ack_size); |
| 451 | if (result >= 0) { |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 452 | result = -ERESTARTSYS; |
Inaky Perez-Gonzalez | aba3792 | 2009-09-03 15:14:29 -0700 | [diff] [blame] | 453 | d_printf(6, dev, "boot-mode cmd %d: HW boot barker\n", opcode); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 454 | goto error_reboot; |
| 455 | } |
| 456 | if (ack_size == sizeof(i2400m_ACK_BARKER) |
| 457 | && memcmp(ack, i2400m_ACK_BARKER, sizeof(*ack)) == 0) { |
| 458 | result = -EISCONN; |
| 459 | d_printf(3, dev, "boot-mode cmd %d: HW reboot ack barker\n", |
| 460 | opcode); |
| 461 | goto error_reboot_ack; |
| 462 | } |
| 463 | result = 0; |
| 464 | if (flags & I2400M_BM_CMD_RAW) |
| 465 | goto out_raw; |
| 466 | ack->data_size = le32_to_cpu(ack->data_size); |
| 467 | ack->target_addr = le32_to_cpu(ack->target_addr); |
| 468 | ack->block_checksum = le32_to_cpu(ack->block_checksum); |
| 469 | d_printf(5, dev, "boot-mode cmd %d: notification for opcode %u " |
| 470 | "response %u csum %u rr %u da %u\n", |
| 471 | opcode, i2400m_brh_get_opcode(ack), |
| 472 | i2400m_brh_get_response(ack), |
| 473 | i2400m_brh_get_use_checksum(ack), |
| 474 | i2400m_brh_get_response_required(ack), |
| 475 | i2400m_brh_get_direct_access(ack)); |
| 476 | result = -EIO; |
| 477 | if (i2400m_brh_get_signature(ack) != 0xcbbc) { |
| 478 | dev_err(dev, "boot-mode cmd %d: HW BUG? wrong signature " |
| 479 | "0x%04x\n", opcode, i2400m_brh_get_signature(ack)); |
| 480 | goto error_ack_signature; |
| 481 | } |
| 482 | if (opcode != -1 && opcode != i2400m_brh_get_opcode(ack)) { |
| 483 | dev_err(dev, "boot-mode cmd %d: HW BUG? " |
| 484 | "received response for opcode %u, expected %u\n", |
| 485 | opcode, i2400m_brh_get_opcode(ack), opcode); |
| 486 | goto error_ack_opcode; |
| 487 | } |
| 488 | if (i2400m_brh_get_response(ack) != 0) { /* failed? */ |
| 489 | dev_err(dev, "boot-mode cmd %d: error; hw response %u\n", |
| 490 | opcode, i2400m_brh_get_response(ack)); |
| 491 | goto error_ack_failed; |
| 492 | } |
| 493 | if (ack_size < ack->data_size + sizeof(*ack)) { |
| 494 | dev_err(dev, "boot-mode cmd %d: SW BUG " |
| 495 | "driver provided only %zu bytes for %zu bytes " |
| 496 | "of data\n", opcode, ack_size, |
| 497 | (size_t) le32_to_cpu(ack->data_size) + sizeof(*ack)); |
| 498 | goto error_ack_short_buffer; |
| 499 | } |
| 500 | result = ack_size; |
| 501 | /* Don't you love this stack of empty targets? Well, I don't |
| 502 | * either, but it helps track exactly who comes in here and |
| 503 | * why :) */ |
| 504 | error_ack_short_buffer: |
| 505 | error_ack_failed: |
| 506 | error_ack_opcode: |
| 507 | error_ack_signature: |
| 508 | out_raw: |
| 509 | error_reboot_ack: |
| 510 | error_reboot: |
| 511 | error_ack_short: |
| 512 | d_fnend(8, dev, "(i2400m %p opcode %d ack %p size %zu) = %d\n", |
| 513 | i2400m, opcode, ack, ack_size, (int) result); |
| 514 | return result; |
| 515 | } |
| 516 | |
| 517 | |
| 518 | /** |
| 519 | * i2400m_bm_cmd - Execute a boot mode command |
| 520 | * |
| 521 | * @cmd: buffer containing the command data (pointing at the header). |
| 522 | * This data can be ANYWHERE (for USB, we will copy it to an |
| 523 | * specific buffer). Make sure everything is in proper little |
| 524 | * endian. |
| 525 | * |
| 526 | * A raw buffer can be also sent, just cast it and set flags to |
| 527 | * I2400M_BM_CMD_RAW. |
| 528 | * |
| 529 | * This function will generate a checksum for you if the |
| 530 | * checksum bit in the command is set (unless I2400M_BM_CMD_RAW |
| 531 | * is set). |
| 532 | * |
| 533 | * You can use the i2400m->bm_cmd_buf to stage your commands and |
| 534 | * send them. |
| 535 | * |
| 536 | * If NULL, no command is sent (we just wait for an ack). |
| 537 | * |
| 538 | * @cmd_size: size of the command. Will be auto padded to the |
| 539 | * bus-specific drivers padding requirements. |
| 540 | * |
| 541 | * @ack: buffer where to place the acknowledgement. If it is a regular |
| 542 | * command response, all fields will be returned with the right, |
| 543 | * native endianess. |
| 544 | * |
| 545 | * You *cannot* use i2400m->bm_ack_buf for this buffer. |
| 546 | * |
| 547 | * @ack_size: size of @ack, 16 aligned; you need to provide at least |
| 548 | * sizeof(*ack) bytes and then enough to contain the return data |
| 549 | * from the command |
| 550 | * |
| 551 | * @flags: see I2400M_BM_CMD_* above. |
| 552 | * |
| 553 | * @returns: bytes received by the notification; if < 0, an errno code |
| 554 | * denoting an error or: |
| 555 | * |
| 556 | * -ERESTARTSYS The device has rebooted |
| 557 | * |
| 558 | * Executes a boot-mode command and waits for a response, doing basic |
| 559 | * validation on it; if a zero length response is received, it retries |
| 560 | * waiting for a response until a non-zero one is received (timing out |
| 561 | * after %I2400M_BOOT_RETRIES retries). |
| 562 | */ |
| 563 | static |
| 564 | ssize_t i2400m_bm_cmd(struct i2400m *i2400m, |
| 565 | const struct i2400m_bootrom_header *cmd, size_t cmd_size, |
| 566 | struct i2400m_bootrom_header *ack, size_t ack_size, |
| 567 | int flags) |
| 568 | { |
| 569 | ssize_t result = -ENOMEM, rx_bytes; |
| 570 | struct device *dev = i2400m_dev(i2400m); |
| 571 | int opcode = cmd == NULL ? -1 : i2400m_brh_get_opcode(cmd); |
| 572 | |
| 573 | d_fnstart(6, dev, "(i2400m %p cmd %p size %zu ack %p size %zu)\n", |
| 574 | i2400m, cmd, cmd_size, ack, ack_size); |
| 575 | BUG_ON(ack_size < sizeof(*ack)); |
| 576 | BUG_ON(i2400m->boot_mode == 0); |
| 577 | |
| 578 | if (cmd != NULL) { /* send the command */ |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 579 | result = i2400m->bus_bm_cmd_send(i2400m, cmd, cmd_size, flags); |
| 580 | if (result < 0) |
| 581 | goto error_cmd_send; |
| 582 | if ((flags & I2400M_BM_CMD_RAW) == 0) |
| 583 | d_printf(5, dev, |
| 584 | "boot-mode cmd %d csum %u rr %u da %u: " |
| 585 | "addr 0x%04x size %u block csum 0x%04x\n", |
| 586 | opcode, i2400m_brh_get_use_checksum(cmd), |
| 587 | i2400m_brh_get_response_required(cmd), |
| 588 | i2400m_brh_get_direct_access(cmd), |
| 589 | cmd->target_addr, cmd->data_size, |
| 590 | cmd->block_checksum); |
| 591 | } |
| 592 | result = i2400m->bus_bm_wait_for_ack(i2400m, ack, ack_size); |
| 593 | if (result < 0) { |
| 594 | dev_err(dev, "boot-mode cmd %d: error waiting for an ack: %d\n", |
| 595 | opcode, (int) result); /* bah, %zd doesn't work */ |
| 596 | goto error_wait_for_ack; |
| 597 | } |
| 598 | rx_bytes = result; |
| 599 | /* verify the ack and read more if neccessary [result is the |
| 600 | * final amount of bytes we get in the ack] */ |
| 601 | result = __i2400m_bm_ack_verify(i2400m, opcode, ack, ack_size, flags); |
| 602 | if (result < 0) |
| 603 | goto error_bad_ack; |
| 604 | /* Don't you love this stack of empty targets? Well, I don't |
| 605 | * either, but it helps track exactly who comes in here and |
| 606 | * why :) */ |
| 607 | result = rx_bytes; |
| 608 | error_bad_ack: |
| 609 | error_wait_for_ack: |
| 610 | error_cmd_send: |
| 611 | d_fnend(6, dev, "(i2400m %p cmd %p size %zu ack %p size %zu) = %d\n", |
| 612 | i2400m, cmd, cmd_size, ack, ack_size, (int) result); |
| 613 | return result; |
| 614 | } |
| 615 | |
| 616 | |
| 617 | /** |
| 618 | * i2400m_download_chunk - write a single chunk of data to the device's memory |
| 619 | * |
| 620 | * @i2400m: device descriptor |
| 621 | * @buf: the buffer to write |
| 622 | * @buf_len: length of the buffer to write |
| 623 | * @addr: address in the device memory space |
| 624 | * @direct: bootrom write mode |
| 625 | * @do_csum: should a checksum validation be performed |
| 626 | */ |
| 627 | static int i2400m_download_chunk(struct i2400m *i2400m, const void *chunk, |
| 628 | size_t __chunk_len, unsigned long addr, |
| 629 | unsigned int direct, unsigned int do_csum) |
| 630 | { |
| 631 | int ret; |
Inaky Perez-Gonzalez | 8593a19 | 2009-05-20 16:53:30 -0700 | [diff] [blame] | 632 | size_t chunk_len = ALIGN(__chunk_len, I2400M_PL_ALIGN); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 633 | struct device *dev = i2400m_dev(i2400m); |
| 634 | struct { |
| 635 | struct i2400m_bootrom_header cmd; |
| 636 | u8 cmd_payload[chunk_len]; |
| 637 | } __attribute__((packed)) *buf; |
| 638 | struct i2400m_bootrom_header ack; |
| 639 | |
| 640 | d_fnstart(5, dev, "(i2400m %p chunk %p __chunk_len %zu addr 0x%08lx " |
| 641 | "direct %u do_csum %u)\n", i2400m, chunk, __chunk_len, |
| 642 | addr, direct, do_csum); |
| 643 | buf = i2400m->bm_cmd_buf; |
| 644 | memcpy(buf->cmd_payload, chunk, __chunk_len); |
| 645 | memset(buf->cmd_payload + __chunk_len, 0xad, chunk_len - __chunk_len); |
| 646 | |
| 647 | buf->cmd.command = i2400m_brh_command(I2400M_BRH_WRITE, |
| 648 | __chunk_len & 0x3 ? 0 : do_csum, |
| 649 | __chunk_len & 0xf ? 0 : direct); |
| 650 | buf->cmd.target_addr = cpu_to_le32(addr); |
| 651 | buf->cmd.data_size = cpu_to_le32(__chunk_len); |
| 652 | ret = i2400m_bm_cmd(i2400m, &buf->cmd, sizeof(buf->cmd) + chunk_len, |
| 653 | &ack, sizeof(ack), 0); |
| 654 | if (ret >= 0) |
| 655 | ret = 0; |
| 656 | d_fnend(5, dev, "(i2400m %p chunk %p __chunk_len %zu addr 0x%08lx " |
| 657 | "direct %u do_csum %u) = %d\n", i2400m, chunk, __chunk_len, |
| 658 | addr, direct, do_csum, ret); |
| 659 | return ret; |
| 660 | } |
| 661 | |
| 662 | |
| 663 | /* |
| 664 | * Download a BCF file's sections to the device |
| 665 | * |
| 666 | * @i2400m: device descriptor |
| 667 | * @bcf: pointer to firmware data (followed by the payloads). Assumed |
| 668 | * verified and consistent. |
| 669 | * @bcf_len: length (in bytes) of the @bcf buffer. |
| 670 | * |
| 671 | * Returns: < 0 errno code on error or the offset to the jump instruction. |
| 672 | * |
| 673 | * Given a BCF file, downloads each section (a command and a payload) |
| 674 | * to the device's address space. Actually, it just executes each |
| 675 | * command i the BCF file. |
| 676 | * |
| 677 | * The section size has to be aligned to 4 bytes AND the padding has |
| 678 | * to be taken from the firmware file, as the signature takes it into |
| 679 | * account. |
| 680 | */ |
| 681 | static |
| 682 | ssize_t i2400m_dnload_bcf(struct i2400m *i2400m, |
| 683 | const struct i2400m_bcf_hdr *bcf, size_t bcf_len) |
| 684 | { |
| 685 | ssize_t ret; |
| 686 | struct device *dev = i2400m_dev(i2400m); |
| 687 | size_t offset, /* iterator offset */ |
| 688 | data_size, /* Size of the data payload */ |
| 689 | section_size, /* Size of the whole section (cmd + payload) */ |
| 690 | section = 1; |
| 691 | const struct i2400m_bootrom_header *bh; |
| 692 | struct i2400m_bootrom_header ack; |
| 693 | |
| 694 | d_fnstart(3, dev, "(i2400m %p bcf %p bcf_len %zu)\n", |
| 695 | i2400m, bcf, bcf_len); |
| 696 | /* Iterate over the command blocks in the BCF file that start |
| 697 | * after the header */ |
| 698 | offset = le32_to_cpu(bcf->header_len) * sizeof(u32); |
| 699 | while (1) { /* start sending the file */ |
| 700 | bh = (void *) bcf + offset; |
| 701 | data_size = le32_to_cpu(bh->data_size); |
| 702 | section_size = ALIGN(sizeof(*bh) + data_size, 4); |
| 703 | d_printf(7, dev, |
| 704 | "downloading section #%zu (@%zu %zu B) to 0x%08x\n", |
| 705 | section, offset, sizeof(*bh) + data_size, |
| 706 | le32_to_cpu(bh->target_addr)); |
| 707 | if (i2400m_brh_get_opcode(bh) == I2400M_BRH_SIGNED_JUMP) { |
| 708 | /* Secure boot needs to stop here */ |
| 709 | d_printf(5, dev, "signed jump found @%zu\n", offset); |
| 710 | break; |
| 711 | } |
| 712 | if (offset + section_size == bcf_len) |
| 713 | /* Non-secure boot stops here */ |
| 714 | break; |
| 715 | if (offset + section_size > bcf_len) { |
| 716 | dev_err(dev, "fw %s: bad section #%zu, " |
| 717 | "end (@%zu) beyond EOF (@%zu)\n", |
Inaky Perez-Gonzalez | 1039abb | 2009-02-28 23:42:47 +0000 | [diff] [blame] | 718 | i2400m->fw_name, section, |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 719 | offset + section_size, bcf_len); |
| 720 | ret = -EINVAL; |
| 721 | goto error_section_beyond_eof; |
| 722 | } |
| 723 | __i2400m_msleep(20); |
| 724 | ret = i2400m_bm_cmd(i2400m, bh, section_size, |
| 725 | &ack, sizeof(ack), I2400M_BM_CMD_RAW); |
| 726 | if (ret < 0) { |
| 727 | dev_err(dev, "fw %s: section #%zu (@%zu %zu B) " |
Inaky Perez-Gonzalez | 1039abb | 2009-02-28 23:42:47 +0000 | [diff] [blame] | 728 | "failed %d\n", i2400m->fw_name, section, |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 729 | offset, sizeof(*bh) + data_size, (int) ret); |
| 730 | goto error_send; |
| 731 | } |
| 732 | offset += section_size; |
| 733 | section++; |
| 734 | } |
| 735 | ret = offset; |
| 736 | error_section_beyond_eof: |
| 737 | error_send: |
| 738 | d_fnend(3, dev, "(i2400m %p bcf %p bcf_len %zu) = %d\n", |
| 739 | i2400m, bcf, bcf_len, (int) ret); |
| 740 | return ret; |
| 741 | } |
| 742 | |
| 743 | |
| 744 | /* |
Inaky Perez-Gonzalez | 32742e6 | 2009-09-03 15:56:40 -0700 | [diff] [blame] | 745 | * Indicate if the device emitted a reboot barker that indicates |
| 746 | * "signed boot" |
| 747 | */ |
| 748 | static |
| 749 | unsigned i2400m_boot_is_signed(struct i2400m *i2400m) |
| 750 | { |
| 751 | return likely(i2400m->sboot); |
| 752 | } |
| 753 | |
| 754 | |
| 755 | /* |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 756 | * Do the final steps of uploading firmware |
| 757 | * |
| 758 | * Depending on the boot mode (signed vs non-signed), different |
| 759 | * actions need to be taken. |
| 760 | */ |
| 761 | static |
| 762 | int i2400m_dnload_finalize(struct i2400m *i2400m, |
| 763 | const struct i2400m_bcf_hdr *bcf, size_t offset) |
| 764 | { |
| 765 | int ret = 0; |
| 766 | struct device *dev = i2400m_dev(i2400m); |
| 767 | struct i2400m_bootrom_header *cmd, ack; |
| 768 | struct { |
| 769 | struct i2400m_bootrom_header cmd; |
| 770 | u8 cmd_pl[0]; |
| 771 | } __attribute__((packed)) *cmd_buf; |
| 772 | size_t signature_block_offset, signature_block_size; |
| 773 | |
| 774 | d_fnstart(3, dev, "offset %zu\n", offset); |
| 775 | cmd = (void *) bcf + offset; |
Inaky Perez-Gonzalez | 32742e6 | 2009-09-03 15:56:40 -0700 | [diff] [blame] | 776 | if (i2400m_boot_is_signed(i2400m) == 0) { |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 777 | struct i2400m_bootrom_header jump_ack; |
Dirk Brandewie | ead6823 | 2009-05-12 06:22:27 -0700 | [diff] [blame] | 778 | d_printf(1, dev, "unsecure boot, jumping to 0x%08x\n", |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 779 | le32_to_cpu(cmd->target_addr)); |
Cindy H Kao | 8d8fe19 | 2009-08-18 19:27:18 -0700 | [diff] [blame] | 780 | cmd_buf = i2400m->bm_cmd_buf; |
| 781 | memcpy(&cmd_buf->cmd, cmd, sizeof(*cmd)); |
| 782 | cmd = &cmd_buf->cmd; |
| 783 | /* now cmd points to the actual bootrom_header in cmd_buf */ |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 784 | i2400m_brh_set_opcode(cmd, I2400M_BRH_JUMP); |
| 785 | cmd->data_size = 0; |
| 786 | ret = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd), |
| 787 | &jump_ack, sizeof(jump_ack), 0); |
| 788 | } else { |
Dirk Brandewie | ead6823 | 2009-05-12 06:22:27 -0700 | [diff] [blame] | 789 | d_printf(1, dev, "secure boot, jumping to 0x%08x\n", |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 790 | le32_to_cpu(cmd->target_addr)); |
| 791 | cmd_buf = i2400m->bm_cmd_buf; |
| 792 | memcpy(&cmd_buf->cmd, cmd, sizeof(*cmd)); |
| 793 | signature_block_offset = |
| 794 | sizeof(*bcf) |
| 795 | + le32_to_cpu(bcf->key_size) * sizeof(u32) |
| 796 | + le32_to_cpu(bcf->exponent_size) * sizeof(u32); |
| 797 | signature_block_size = |
| 798 | le32_to_cpu(bcf->modulus_size) * sizeof(u32); |
| 799 | memcpy(cmd_buf->cmd_pl, (void *) bcf + signature_block_offset, |
| 800 | signature_block_size); |
| 801 | ret = i2400m_bm_cmd(i2400m, &cmd_buf->cmd, |
| 802 | sizeof(cmd_buf->cmd) + signature_block_size, |
| 803 | &ack, sizeof(ack), I2400M_BM_CMD_RAW); |
| 804 | } |
| 805 | d_fnend(3, dev, "returning %d\n", ret); |
| 806 | return ret; |
| 807 | } |
| 808 | |
| 809 | |
| 810 | /** |
| 811 | * i2400m_bootrom_init - Reboots a powered device into boot mode |
| 812 | * |
| 813 | * @i2400m: device descriptor |
| 814 | * @flags: |
Inaky Perez-Gonzalez | 923d708 | 2009-09-04 14:50:59 -0700 | [diff] [blame^] | 815 | * I2400M_BRI_SOFT: a reboot barker has been seen |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 816 | * already, so don't wait for it. |
| 817 | * |
| 818 | * I2400M_BRI_NO_REBOOT: Don't send a reboot command, but wait |
| 819 | * for a reboot barker notification. This is a one shot; if |
| 820 | * the state machine needs to send a reboot command it will. |
| 821 | * |
| 822 | * Returns: |
| 823 | * |
| 824 | * < 0 errno code on error, 0 if ok. |
| 825 | * |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 826 | * Description: |
| 827 | * |
| 828 | * Tries hard enough to put the device in boot-mode. There are two |
| 829 | * main phases to this: |
| 830 | * |
| 831 | * a. (1) send a reboot command and (2) get a reboot barker |
Inaky Perez-Gonzalez | 923d708 | 2009-09-04 14:50:59 -0700 | [diff] [blame^] | 832 | * |
| 833 | * b. (1) echo/ack the reboot sending the reboot barker back and (2) |
| 834 | * getting an ack barker in return |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 835 | * |
| 836 | * We want to skip (a) in some cases [soft]. The state machine is |
| 837 | * horrible, but it is basically: on each phase, send what has to be |
| 838 | * sent (if any), wait for the answer and act on the answer. We might |
| 839 | * have to backtrack and retry, so we keep a max tries counter for |
| 840 | * that. |
| 841 | * |
Inaky Perez-Gonzalez | 923d708 | 2009-09-04 14:50:59 -0700 | [diff] [blame^] | 842 | * It sucks because we don't know ahead of time which is going to be |
| 843 | * the reboot barker (the device might send different ones depending |
| 844 | * on its EEPROM config) and once the device reboots and waits for the |
| 845 | * echo/ack reboot barker being sent back, it doesn't understand |
| 846 | * anything else. So we can be left at the point where we don't know |
| 847 | * what to send to it -- cold reset and bus reset seem to have little |
| 848 | * effect. So the function iterates (in this case) through all the |
| 849 | * known barkers and tries them all until an ACK is |
| 850 | * received. Otherwise, it gives up. |
| 851 | * |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 852 | * If we get a timeout after sending a warm reset, we do it again. |
| 853 | */ |
| 854 | int i2400m_bootrom_init(struct i2400m *i2400m, enum i2400m_bri flags) |
| 855 | { |
| 856 | int result; |
| 857 | struct device *dev = i2400m_dev(i2400m); |
| 858 | struct i2400m_bootrom_header *cmd; |
| 859 | struct i2400m_bootrom_header ack; |
Dirk Brandewie | c308365 | 2009-08-13 13:48:29 -0700 | [diff] [blame] | 860 | int count = i2400m->bus_bm_retries; |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 861 | int ack_timeout_cnt = 1; |
Inaky Perez-Gonzalez | 923d708 | 2009-09-04 14:50:59 -0700 | [diff] [blame^] | 862 | unsigned i; |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 863 | |
Inaky Perez-Gonzalez | aba3792 | 2009-09-03 15:14:29 -0700 | [diff] [blame] | 864 | BUILD_BUG_ON(sizeof(*cmd) != sizeof(i2400m_barker_db[0].data)); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 865 | BUILD_BUG_ON(sizeof(ack) != sizeof(i2400m_ACK_BARKER)); |
| 866 | |
| 867 | d_fnstart(4, dev, "(i2400m %p flags 0x%08x)\n", i2400m, flags); |
| 868 | result = -ENOMEM; |
| 869 | cmd = i2400m->bm_cmd_buf; |
| 870 | if (flags & I2400M_BRI_SOFT) |
| 871 | goto do_reboot_ack; |
| 872 | do_reboot: |
Inaky Perez-Gonzalez | 923d708 | 2009-09-04 14:50:59 -0700 | [diff] [blame^] | 873 | ack_timeout_cnt = 1; |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 874 | if (--count < 0) |
| 875 | goto error_timeout; |
| 876 | d_printf(4, dev, "device reboot: reboot command [%d # left]\n", |
| 877 | count); |
| 878 | if ((flags & I2400M_BRI_NO_REBOOT) == 0) |
| 879 | i2400m->bus_reset(i2400m, I2400M_RT_WARM); |
| 880 | result = i2400m_bm_cmd(i2400m, NULL, 0, &ack, sizeof(ack), |
| 881 | I2400M_BM_CMD_RAW); |
| 882 | flags &= ~I2400M_BRI_NO_REBOOT; |
| 883 | switch (result) { |
| 884 | case -ERESTARTSYS: |
Inaky Perez-Gonzalez | 923d708 | 2009-09-04 14:50:59 -0700 | [diff] [blame^] | 885 | /* |
| 886 | * at this point, i2400m_bm_cmd(), through |
| 887 | * __i2400m_bm_ack_process(), has updated |
| 888 | * i2400m->barker and we are good to go. |
| 889 | */ |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 890 | d_printf(4, dev, "device reboot: got reboot barker\n"); |
| 891 | break; |
| 892 | case -EISCONN: /* we don't know how it got here...but we follow it */ |
| 893 | d_printf(4, dev, "device reboot: got ack barker - whatever\n"); |
| 894 | goto do_reboot; |
Inaky Perez-Gonzalez | 923d708 | 2009-09-04 14:50:59 -0700 | [diff] [blame^] | 895 | case -ETIMEDOUT: |
| 896 | /* |
| 897 | * Device has timed out, we might be in boot mode |
| 898 | * already and expecting an ack; if we don't know what |
| 899 | * the barker is, we just send them all. Cold reset |
| 900 | * and bus reset don't work. Beats me. |
| 901 | */ |
| 902 | if (i2400m->barker != NULL) { |
| 903 | dev_err(dev, "device boot: reboot barker timed out, " |
| 904 | "trying (set) %08x echo/ack\n", |
| 905 | le32_to_cpu(i2400m->barker->data[0])); |
Inaky Perez-Gonzalez | aba3792 | 2009-09-03 15:14:29 -0700 | [diff] [blame] | 906 | goto do_reboot_ack; |
| 907 | } |
Inaky Perez-Gonzalez | 923d708 | 2009-09-04 14:50:59 -0700 | [diff] [blame^] | 908 | for (i = 0; i < i2400m_barker_db_used; i++) { |
| 909 | struct i2400m_barker_db *barker = &i2400m_barker_db[i]; |
| 910 | memcpy(cmd, barker->data, sizeof(barker->data)); |
| 911 | result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd), |
| 912 | &ack, sizeof(ack), |
| 913 | I2400M_BM_CMD_RAW); |
| 914 | if (result == -EISCONN) { |
| 915 | dev_warn(dev, "device boot: got ack barker " |
| 916 | "after sending echo/ack barker " |
| 917 | "#%d/%08x; rebooting j.i.c.\n", |
| 918 | i, le32_to_cpu(barker->data[0])); |
| 919 | flags &= ~I2400M_BRI_NO_REBOOT; |
| 920 | goto do_reboot; |
| 921 | } |
| 922 | } |
| 923 | dev_err(dev, "device boot: tried all the echo/acks, could " |
| 924 | "not get device to respond; giving up"); |
| 925 | result = -ESHUTDOWN; |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 926 | case -EPROTO: |
| 927 | case -ESHUTDOWN: /* dev is gone */ |
| 928 | case -EINTR: /* user cancelled */ |
| 929 | goto error_dev_gone; |
| 930 | default: |
| 931 | dev_err(dev, "device reboot: error %d while waiting " |
| 932 | "for reboot barker - rebooting\n", result); |
Inaky Perez-Gonzalez | 923d708 | 2009-09-04 14:50:59 -0700 | [diff] [blame^] | 933 | d_dump(1, dev, &ack, result); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 934 | goto do_reboot; |
| 935 | } |
| 936 | /* At this point we ack back with 4 REBOOT barkers and expect |
| 937 | * 4 ACK barkers. This is ugly, as we send a raw command -- |
| 938 | * hence the cast. _bm_cmd() will catch the reboot ack |
| 939 | * notification and report it as -EISCONN. */ |
| 940 | do_reboot_ack: |
| 941 | d_printf(4, dev, "device reboot ack: sending ack [%d # left]\n", count); |
Inaky Perez-Gonzalez | aba3792 | 2009-09-03 15:14:29 -0700 | [diff] [blame] | 942 | memcpy(cmd, i2400m->barker->data, sizeof(i2400m->barker->data)); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 943 | result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd), |
| 944 | &ack, sizeof(ack), I2400M_BM_CMD_RAW); |
| 945 | switch (result) { |
| 946 | case -ERESTARTSYS: |
| 947 | d_printf(4, dev, "reboot ack: got reboot barker - retrying\n"); |
| 948 | if (--count < 0) |
| 949 | goto error_timeout; |
| 950 | goto do_reboot_ack; |
| 951 | case -EISCONN: |
| 952 | d_printf(4, dev, "reboot ack: got ack barker - good\n"); |
| 953 | break; |
| 954 | case -ETIMEDOUT: /* no response, maybe it is the other type? */ |
Inaky Perez-Gonzalez | aba3792 | 2009-09-03 15:14:29 -0700 | [diff] [blame] | 955 | if (ack_timeout_cnt-- < 0) { |
| 956 | d_printf(4, dev, "reboot ack timedout: retrying\n"); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 957 | goto do_reboot_ack; |
| 958 | } else { |
| 959 | dev_err(dev, "reboot ack timedout too long: " |
| 960 | "trying reboot\n"); |
| 961 | goto do_reboot; |
| 962 | } |
| 963 | break; |
| 964 | case -EPROTO: |
| 965 | case -ESHUTDOWN: /* dev is gone */ |
| 966 | goto error_dev_gone; |
| 967 | default: |
| 968 | dev_err(dev, "device reboot ack: error %d while waiting for " |
| 969 | "reboot ack barker - rebooting\n", result); |
| 970 | goto do_reboot; |
| 971 | } |
| 972 | d_printf(2, dev, "device reboot ack: got ack barker - boot done\n"); |
| 973 | result = 0; |
| 974 | exit_timeout: |
| 975 | error_dev_gone: |
| 976 | d_fnend(4, dev, "(i2400m %p flags 0x%08x) = %d\n", |
| 977 | i2400m, flags, result); |
| 978 | return result; |
| 979 | |
| 980 | error_timeout: |
Inaky Perez-Gonzalez | 6e053d6 | 2009-06-05 09:31:26 +0800 | [diff] [blame] | 981 | dev_err(dev, "Timed out waiting for reboot ack\n"); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 982 | result = -ETIMEDOUT; |
| 983 | goto exit_timeout; |
| 984 | } |
| 985 | |
| 986 | |
| 987 | /* |
| 988 | * Read the MAC addr |
| 989 | * |
| 990 | * The position this function reads is fixed in device memory and |
| 991 | * always available, even without firmware. |
| 992 | * |
| 993 | * Note we specify we want to read only six bytes, but provide space |
| 994 | * for 16, as we always get it rounded up. |
| 995 | */ |
| 996 | int i2400m_read_mac_addr(struct i2400m *i2400m) |
| 997 | { |
| 998 | int result; |
| 999 | struct device *dev = i2400m_dev(i2400m); |
| 1000 | struct net_device *net_dev = i2400m->wimax_dev.net_dev; |
| 1001 | struct i2400m_bootrom_header *cmd; |
| 1002 | struct { |
| 1003 | struct i2400m_bootrom_header ack; |
| 1004 | u8 ack_pl[16]; |
| 1005 | } __attribute__((packed)) ack_buf; |
| 1006 | |
| 1007 | d_fnstart(5, dev, "(i2400m %p)\n", i2400m); |
| 1008 | cmd = i2400m->bm_cmd_buf; |
| 1009 | cmd->command = i2400m_brh_command(I2400M_BRH_READ, 0, 1); |
| 1010 | cmd->target_addr = cpu_to_le32(0x00203fe8); |
| 1011 | cmd->data_size = cpu_to_le32(6); |
| 1012 | result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd), |
| 1013 | &ack_buf.ack, sizeof(ack_buf), 0); |
| 1014 | if (result < 0) { |
| 1015 | dev_err(dev, "BM: read mac addr failed: %d\n", result); |
| 1016 | goto error_read_mac; |
| 1017 | } |
| 1018 | d_printf(2, dev, |
| 1019 | "mac addr is %02x:%02x:%02x:%02x:%02x:%02x\n", |
| 1020 | ack_buf.ack_pl[0], ack_buf.ack_pl[1], |
| 1021 | ack_buf.ack_pl[2], ack_buf.ack_pl[3], |
| 1022 | ack_buf.ack_pl[4], ack_buf.ack_pl[5]); |
| 1023 | if (i2400m->bus_bm_mac_addr_impaired == 1) { |
| 1024 | ack_buf.ack_pl[0] = 0x00; |
| 1025 | ack_buf.ack_pl[1] = 0x16; |
| 1026 | ack_buf.ack_pl[2] = 0xd3; |
| 1027 | get_random_bytes(&ack_buf.ack_pl[3], 3); |
| 1028 | dev_err(dev, "BM is MAC addr impaired, faking MAC addr to " |
| 1029 | "mac addr is %02x:%02x:%02x:%02x:%02x:%02x\n", |
| 1030 | ack_buf.ack_pl[0], ack_buf.ack_pl[1], |
| 1031 | ack_buf.ack_pl[2], ack_buf.ack_pl[3], |
| 1032 | ack_buf.ack_pl[4], ack_buf.ack_pl[5]); |
| 1033 | result = 0; |
| 1034 | } |
| 1035 | net_dev->addr_len = ETH_ALEN; |
| 1036 | memcpy(net_dev->perm_addr, ack_buf.ack_pl, ETH_ALEN); |
| 1037 | memcpy(net_dev->dev_addr, ack_buf.ack_pl, ETH_ALEN); |
| 1038 | error_read_mac: |
| 1039 | d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, result); |
| 1040 | return result; |
| 1041 | } |
| 1042 | |
| 1043 | |
| 1044 | /* |
| 1045 | * Initialize a non signed boot |
| 1046 | * |
| 1047 | * This implies sending some magic values to the device's memory. Note |
| 1048 | * we convert the values to little endian in the same array |
| 1049 | * declaration. |
| 1050 | */ |
| 1051 | static |
| 1052 | int i2400m_dnload_init_nonsigned(struct i2400m *i2400m) |
| 1053 | { |
Dirk Brandewie | 7308a0c | 2009-05-21 11:56:34 -0700 | [diff] [blame] | 1054 | unsigned i = 0; |
| 1055 | int ret = 0; |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1056 | struct device *dev = i2400m_dev(i2400m); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1057 | d_fnstart(5, dev, "(i2400m %p)\n", i2400m); |
Dirk Brandewie | 7308a0c | 2009-05-21 11:56:34 -0700 | [diff] [blame] | 1058 | if (i2400m->bus_bm_pokes_table) { |
| 1059 | while (i2400m->bus_bm_pokes_table[i].address) { |
| 1060 | ret = i2400m_download_chunk( |
| 1061 | i2400m, |
| 1062 | &i2400m->bus_bm_pokes_table[i].data, |
| 1063 | sizeof(i2400m->bus_bm_pokes_table[i].data), |
| 1064 | i2400m->bus_bm_pokes_table[i].address, 1, 1); |
| 1065 | if (ret < 0) |
| 1066 | break; |
| 1067 | i++; |
| 1068 | } |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1069 | } |
| 1070 | d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, ret); |
| 1071 | return ret; |
| 1072 | } |
| 1073 | |
| 1074 | |
| 1075 | /* |
| 1076 | * Initialize the signed boot process |
| 1077 | * |
| 1078 | * @i2400m: device descriptor |
| 1079 | * |
| 1080 | * @bcf_hdr: pointer to the firmware header; assumes it is fully in |
| 1081 | * memory (it has gone through basic validation). |
| 1082 | * |
| 1083 | * Returns: 0 if ok, < 0 errno code on error, -ERESTARTSYS if the hw |
| 1084 | * rebooted. |
| 1085 | * |
| 1086 | * This writes the firmware BCF header to the device using the |
| 1087 | * HASH_PAYLOAD_ONLY command. |
| 1088 | */ |
| 1089 | static |
| 1090 | int i2400m_dnload_init_signed(struct i2400m *i2400m, |
| 1091 | const struct i2400m_bcf_hdr *bcf_hdr) |
| 1092 | { |
| 1093 | int ret; |
| 1094 | struct device *dev = i2400m_dev(i2400m); |
| 1095 | struct { |
| 1096 | struct i2400m_bootrom_header cmd; |
| 1097 | struct i2400m_bcf_hdr cmd_pl; |
| 1098 | } __attribute__((packed)) *cmd_buf; |
| 1099 | struct i2400m_bootrom_header ack; |
| 1100 | |
| 1101 | d_fnstart(5, dev, "(i2400m %p bcf_hdr %p)\n", i2400m, bcf_hdr); |
| 1102 | cmd_buf = i2400m->bm_cmd_buf; |
| 1103 | cmd_buf->cmd.command = |
| 1104 | i2400m_brh_command(I2400M_BRH_HASH_PAYLOAD_ONLY, 0, 0); |
| 1105 | cmd_buf->cmd.target_addr = 0; |
| 1106 | cmd_buf->cmd.data_size = cpu_to_le32(sizeof(cmd_buf->cmd_pl)); |
| 1107 | memcpy(&cmd_buf->cmd_pl, bcf_hdr, sizeof(*bcf_hdr)); |
| 1108 | ret = i2400m_bm_cmd(i2400m, &cmd_buf->cmd, sizeof(*cmd_buf), |
| 1109 | &ack, sizeof(ack), 0); |
| 1110 | if (ret >= 0) |
| 1111 | ret = 0; |
| 1112 | d_fnend(5, dev, "(i2400m %p bcf_hdr %p) = %d\n", i2400m, bcf_hdr, ret); |
| 1113 | return ret; |
| 1114 | } |
| 1115 | |
| 1116 | |
| 1117 | /* |
| 1118 | * Initialize the firmware download at the device size |
| 1119 | * |
| 1120 | * Multiplex to the one that matters based on the device's mode |
| 1121 | * (signed or non-signed). |
| 1122 | */ |
| 1123 | static |
| 1124 | int i2400m_dnload_init(struct i2400m *i2400m, const struct i2400m_bcf_hdr *bcf) |
| 1125 | { |
| 1126 | int result; |
| 1127 | struct device *dev = i2400m_dev(i2400m); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1128 | |
Inaky Perez-Gonzalez | 32742e6 | 2009-09-03 15:56:40 -0700 | [diff] [blame] | 1129 | if (i2400m_boot_is_signed(i2400m)) { |
| 1130 | d_printf(1, dev, "signed boot\n"); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1131 | result = i2400m_dnload_init_signed(i2400m, bcf); |
| 1132 | if (result == -ERESTARTSYS) |
| 1133 | return result; |
| 1134 | if (result < 0) |
Inaky Perez-Gonzalez | 32742e6 | 2009-09-03 15:56:40 -0700 | [diff] [blame] | 1135 | dev_err(dev, "firmware %s: signed boot download " |
| 1136 | "initialization failed: %d\n", |
| 1137 | i2400m->fw_name, result); |
| 1138 | } else { |
| 1139 | /* non-signed boot process without pokes */ |
| 1140 | d_printf(1, dev, "non-signed boot\n"); |
| 1141 | result = i2400m_dnload_init_nonsigned(i2400m); |
| 1142 | if (result == -ERESTARTSYS) |
| 1143 | return result; |
| 1144 | if (result < 0) |
| 1145 | dev_err(dev, "firmware %s: non-signed download " |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1146 | "initialization failed: %d\n", |
Inaky Perez-Gonzalez | 1039abb | 2009-02-28 23:42:47 +0000 | [diff] [blame] | 1147 | i2400m->fw_name, result); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1148 | } |
| 1149 | return result; |
| 1150 | } |
| 1151 | |
| 1152 | |
| 1153 | /* |
| 1154 | * Run quick consistency tests on the firmware file |
| 1155 | * |
| 1156 | * Check for the firmware being made for the i2400m device, |
| 1157 | * etc...These checks are mostly informative, as the device will make |
| 1158 | * them too; but the driver's response is more informative on what |
| 1159 | * went wrong. |
| 1160 | */ |
| 1161 | static |
| 1162 | int i2400m_fw_check(struct i2400m *i2400m, |
| 1163 | const struct i2400m_bcf_hdr *bcf, |
| 1164 | size_t bcf_size) |
| 1165 | { |
| 1166 | int result; |
| 1167 | struct device *dev = i2400m_dev(i2400m); |
| 1168 | unsigned module_type, header_len, major_version, minor_version, |
| 1169 | module_id, module_vendor, date, size; |
| 1170 | |
| 1171 | /* Check hard errors */ |
| 1172 | result = -EINVAL; |
| 1173 | if (bcf_size < sizeof(*bcf)) { /* big enough header? */ |
| 1174 | dev_err(dev, "firmware %s too short: " |
| 1175 | "%zu B vs %zu (at least) expected\n", |
Inaky Perez-Gonzalez | 1039abb | 2009-02-28 23:42:47 +0000 | [diff] [blame] | 1176 | i2400m->fw_name, bcf_size, sizeof(*bcf)); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1177 | goto error; |
| 1178 | } |
| 1179 | |
| 1180 | module_type = bcf->module_type; |
| 1181 | header_len = sizeof(u32) * le32_to_cpu(bcf->header_len); |
| 1182 | major_version = le32_to_cpu(bcf->header_version) & 0xffff0000 >> 16; |
| 1183 | minor_version = le32_to_cpu(bcf->header_version) & 0x0000ffff; |
| 1184 | module_id = le32_to_cpu(bcf->module_id); |
| 1185 | module_vendor = le32_to_cpu(bcf->module_vendor); |
| 1186 | date = le32_to_cpu(bcf->date); |
| 1187 | size = sizeof(u32) * le32_to_cpu(bcf->size); |
| 1188 | |
| 1189 | if (bcf_size != size) { /* annoyingly paranoid */ |
| 1190 | dev_err(dev, "firmware %s: bad size, got " |
| 1191 | "%zu B vs %u expected\n", |
Inaky Perez-Gonzalez | 1039abb | 2009-02-28 23:42:47 +0000 | [diff] [blame] | 1192 | i2400m->fw_name, bcf_size, size); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1193 | goto error; |
| 1194 | } |
| 1195 | |
| 1196 | d_printf(2, dev, "type 0x%x id 0x%x vendor 0x%x; header v%u.%u (%zu B) " |
| 1197 | "date %08x (%zu B)\n", |
| 1198 | module_type, module_id, module_vendor, |
| 1199 | major_version, minor_version, (size_t) header_len, |
| 1200 | date, (size_t) size); |
| 1201 | |
| 1202 | if (module_type != 6) { /* built for the right hardware? */ |
| 1203 | dev_err(dev, "bad fw %s: unexpected module type 0x%x; " |
Inaky Perez-Gonzalez | 1039abb | 2009-02-28 23:42:47 +0000 | [diff] [blame] | 1204 | "aborting\n", i2400m->fw_name, module_type); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1205 | goto error; |
| 1206 | } |
| 1207 | |
| 1208 | /* Check soft-er errors */ |
| 1209 | result = 0; |
| 1210 | if (module_vendor != 0x8086) |
| 1211 | dev_err(dev, "bad fw %s? unexpected vendor 0x%04x\n", |
Inaky Perez-Gonzalez | 1039abb | 2009-02-28 23:42:47 +0000 | [diff] [blame] | 1212 | i2400m->fw_name, module_vendor); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1213 | if (date < 0x20080300) |
| 1214 | dev_err(dev, "bad fw %s? build date too old %08x\n", |
Inaky Perez-Gonzalez | 1039abb | 2009-02-28 23:42:47 +0000 | [diff] [blame] | 1215 | i2400m->fw_name, date); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1216 | error: |
| 1217 | return result; |
| 1218 | } |
| 1219 | |
| 1220 | |
| 1221 | /* |
| 1222 | * Download the firmware to the device |
| 1223 | * |
| 1224 | * @i2400m: device descriptor |
| 1225 | * @bcf: pointer to loaded (and minimally verified for consistency) |
| 1226 | * firmware |
| 1227 | * @bcf_size: size of the @bcf buffer (header plus payloads) |
| 1228 | * |
| 1229 | * The process for doing this is described in this file's header. |
| 1230 | * |
| 1231 | * Note we only reinitialize boot-mode if the flags say so. Some hw |
| 1232 | * iterations need it, some don't. In any case, if we loop, we always |
| 1233 | * need to reinitialize the boot room, hence the flags modification. |
| 1234 | */ |
| 1235 | static |
| 1236 | int i2400m_fw_dnload(struct i2400m *i2400m, const struct i2400m_bcf_hdr *bcf, |
| 1237 | size_t bcf_size, enum i2400m_bri flags) |
| 1238 | { |
| 1239 | int ret = 0; |
| 1240 | struct device *dev = i2400m_dev(i2400m); |
Inaky Perez-Gonzalez | ecddfd5 | 2009-06-03 16:13:14 +0800 | [diff] [blame] | 1241 | int count = i2400m->bus_bm_retries; |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1242 | |
| 1243 | d_fnstart(5, dev, "(i2400m %p bcf %p size %zu)\n", |
| 1244 | i2400m, bcf, bcf_size); |
| 1245 | i2400m->boot_mode = 1; |
Inaky Perez-Gonzalez | b4013f9 | 2009-06-03 09:45:55 +0800 | [diff] [blame] | 1246 | wmb(); /* Make sure other readers see it */ |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1247 | hw_reboot: |
| 1248 | if (count-- == 0) { |
| 1249 | ret = -ERESTARTSYS; |
| 1250 | dev_err(dev, "device rebooted too many times, aborting\n"); |
| 1251 | goto error_too_many_reboots; |
| 1252 | } |
| 1253 | if (flags & I2400M_BRI_MAC_REINIT) { |
| 1254 | ret = i2400m_bootrom_init(i2400m, flags); |
| 1255 | if (ret < 0) { |
| 1256 | dev_err(dev, "bootrom init failed: %d\n", ret); |
| 1257 | goto error_bootrom_init; |
| 1258 | } |
| 1259 | } |
| 1260 | flags |= I2400M_BRI_MAC_REINIT; |
| 1261 | |
| 1262 | /* |
| 1263 | * Initialize the download, push the bytes to the device and |
| 1264 | * then jump to the new firmware. Note @ret is passed with the |
| 1265 | * offset of the jump instruction to _dnload_finalize() |
| 1266 | */ |
| 1267 | ret = i2400m_dnload_init(i2400m, bcf); /* Init device's dnload */ |
| 1268 | if (ret == -ERESTARTSYS) |
| 1269 | goto error_dev_rebooted; |
| 1270 | if (ret < 0) |
| 1271 | goto error_dnload_init; |
| 1272 | |
| 1273 | ret = i2400m_dnload_bcf(i2400m, bcf, bcf_size); |
| 1274 | if (ret == -ERESTARTSYS) |
| 1275 | goto error_dev_rebooted; |
| 1276 | if (ret < 0) { |
| 1277 | dev_err(dev, "fw %s: download failed: %d\n", |
Inaky Perez-Gonzalez | 1039abb | 2009-02-28 23:42:47 +0000 | [diff] [blame] | 1278 | i2400m->fw_name, ret); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1279 | goto error_dnload_bcf; |
| 1280 | } |
| 1281 | |
| 1282 | ret = i2400m_dnload_finalize(i2400m, bcf, ret); |
| 1283 | if (ret == -ERESTARTSYS) |
| 1284 | goto error_dev_rebooted; |
| 1285 | if (ret < 0) { |
| 1286 | dev_err(dev, "fw %s: " |
| 1287 | "download finalization failed: %d\n", |
Inaky Perez-Gonzalez | 1039abb | 2009-02-28 23:42:47 +0000 | [diff] [blame] | 1288 | i2400m->fw_name, ret); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1289 | goto error_dnload_finalize; |
| 1290 | } |
| 1291 | |
| 1292 | d_printf(2, dev, "fw %s successfully uploaded\n", |
Inaky Perez-Gonzalez | 1039abb | 2009-02-28 23:42:47 +0000 | [diff] [blame] | 1293 | i2400m->fw_name); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1294 | i2400m->boot_mode = 0; |
Inaky Perez-Gonzalez | b4013f9 | 2009-06-03 09:45:55 +0800 | [diff] [blame] | 1295 | wmb(); /* Make sure i2400m_msg_to_dev() sees boot_mode */ |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1296 | error_dnload_finalize: |
| 1297 | error_dnload_bcf: |
| 1298 | error_dnload_init: |
| 1299 | error_bootrom_init: |
| 1300 | error_too_many_reboots: |
| 1301 | d_fnend(5, dev, "(i2400m %p bcf %p size %zu) = %d\n", |
| 1302 | i2400m, bcf, bcf_size, ret); |
| 1303 | return ret; |
| 1304 | |
| 1305 | error_dev_rebooted: |
| 1306 | dev_err(dev, "device rebooted, %d tries left\n", count); |
| 1307 | /* we got the notification already, no need to wait for it again */ |
| 1308 | flags |= I2400M_BRI_SOFT; |
| 1309 | goto hw_reboot; |
| 1310 | } |
| 1311 | |
| 1312 | |
| 1313 | /** |
| 1314 | * i2400m_dev_bootstrap - Bring the device to a known state and upload firmware |
| 1315 | * |
| 1316 | * @i2400m: device descriptor |
| 1317 | * |
| 1318 | * Returns: >= 0 if ok, < 0 errno code on error. |
| 1319 | * |
| 1320 | * This sets up the firmware upload environment, loads the firmware |
| 1321 | * file from disk, verifies and then calls the firmware upload process |
| 1322 | * per se. |
| 1323 | * |
| 1324 | * Can be called either from probe, or after a warm reset. Can not be |
| 1325 | * called from within an interrupt. All the flow in this code is |
| 1326 | * single-threade; all I/Os are synchronous. |
| 1327 | */ |
| 1328 | int i2400m_dev_bootstrap(struct i2400m *i2400m, enum i2400m_bri flags) |
| 1329 | { |
Inaky Perez-Gonzalez | ebc5f62 | 2009-09-03 15:53:30 -0700 | [diff] [blame] | 1330 | int ret, itr; |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1331 | struct device *dev = i2400m_dev(i2400m); |
| 1332 | const struct firmware *fw; |
| 1333 | const struct i2400m_bcf_hdr *bcf; /* Firmware data */ |
Inaky Perez-Gonzalez | 1039abb | 2009-02-28 23:42:47 +0000 | [diff] [blame] | 1334 | const char *fw_name; |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1335 | |
| 1336 | d_fnstart(5, dev, "(i2400m %p)\n", i2400m); |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1337 | |
Inaky Perez-Gonzalez | 1039abb | 2009-02-28 23:42:47 +0000 | [diff] [blame] | 1338 | /* Load firmware files to memory. */ |
Inaky Perez-Gonzalez | ebc5f62 | 2009-09-03 15:53:30 -0700 | [diff] [blame] | 1339 | for (itr = 0, bcf = NULL, ret = -ENOENT; ; itr++) { |
Inaky Perez-Gonzalez | 1039abb | 2009-02-28 23:42:47 +0000 | [diff] [blame] | 1340 | fw_name = i2400m->bus_fw_names[itr]; |
| 1341 | if (fw_name == NULL) { |
| 1342 | dev_err(dev, "Could not find a usable firmware image\n"); |
| 1343 | ret = -ENOENT; |
Inaky Perez-Gonzalez | ebc5f62 | 2009-09-03 15:53:30 -0700 | [diff] [blame] | 1344 | break; |
Inaky Perez-Gonzalez | 1039abb | 2009-02-28 23:42:47 +0000 | [diff] [blame] | 1345 | } |
Inaky Perez-Gonzalez | ebc5f62 | 2009-09-03 15:53:30 -0700 | [diff] [blame] | 1346 | d_printf(1, dev, "trying firmware %s (%d)\n", fw_name, itr); |
Inaky Perez-Gonzalez | 1039abb | 2009-02-28 23:42:47 +0000 | [diff] [blame] | 1347 | ret = request_firmware(&fw, fw_name, dev); |
Inaky Perez-Gonzalez | ebc5f62 | 2009-09-03 15:53:30 -0700 | [diff] [blame] | 1348 | if (ret < 0) { |
Inaky Perez-Gonzalez | 1039abb | 2009-02-28 23:42:47 +0000 | [diff] [blame] | 1349 | dev_err(dev, "fw %s: cannot load file: %d\n", |
| 1350 | fw_name, ret); |
Inaky Perez-Gonzalez | ebc5f62 | 2009-09-03 15:53:30 -0700 | [diff] [blame] | 1351 | continue; |
| 1352 | } |
| 1353 | bcf = (void *) fw->data; |
| 1354 | i2400m->fw_name = fw_name; |
| 1355 | ret = i2400m_fw_check(i2400m, bcf, fw->size); |
| 1356 | if (ret >= 0) { |
| 1357 | ret = i2400m_fw_dnload(i2400m, bcf, fw->size, flags); |
| 1358 | if (ret >= 0) |
| 1359 | break; |
| 1360 | } else |
| 1361 | dev_err(dev, "%s: cannot use, skipping\n", fw_name); |
| 1362 | release_firmware(fw); |
Inaky Perez-Gonzalez | 1039abb | 2009-02-28 23:42:47 +0000 | [diff] [blame] | 1363 | } |
Inaky Perez-Gonzalez | 467cc39 | 2008-12-20 16:57:46 -0800 | [diff] [blame] | 1364 | d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, ret); |
| 1365 | return ret; |
| 1366 | } |
| 1367 | EXPORT_SYMBOL_GPL(i2400m_dev_bootstrap); |