1 /****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2006-2010 Solarflare Communications Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
11 #include <linux/bitops.h>
12 #include <linux/module.h>
13 #include <linux/mtd/mtd.h>
14 #include <linux/delay.h>
15 #include <linux/slab.h>
16 #include <linux/rtnetlink.h>
18 #include "net_driver.h"
23 #include "mcdi_pcol.h"
25 #define FALCON_SPI_VERIFY_BUF_LEN 16
27 struct efx_mtd_partition {
28 struct list_head node;
30 const char *dev_type_name;
31 const char *type_name;
32 char name[IFNAMSIZ + 20];
35 struct falcon_mtd_partition {
36 struct efx_mtd_partition common;
37 const struct falcon_spi_device *spi;
42 void (*rename)(struct efx_mtd_partition *part);
43 int (*read)(struct mtd_info *mtd, loff_t start, size_t len,
44 size_t *retlen, u8 *buffer);
45 int (*erase)(struct mtd_info *mtd, loff_t start, size_t len);
46 int (*write)(struct mtd_info *mtd, loff_t start, size_t len,
47 size_t *retlen, const u8 *buffer);
48 int (*sync)(struct mtd_info *mtd);
51 #define to_efx_mtd_partition(mtd) \
52 container_of(mtd, struct efx_mtd_partition, mtd)
54 #define to_falcon_mtd_partition(mtd) \
55 container_of(mtd, struct falcon_mtd_partition, common.mtd)
57 static int falcon_mtd_probe(struct efx_nic *efx);
58 static int siena_mtd_probe(struct efx_nic *efx);
63 falcon_spi_slow_wait(struct falcon_mtd_partition *part, bool uninterruptible)
65 const struct falcon_spi_device *spi = part->spi;
66 struct efx_nic *efx = part->common.mtd.priv;
70 /* Wait up to 4s for flash/EEPROM to finish a slow operation. */
71 for (i = 0; i < 40; i++) {
72 __set_current_state(uninterruptible ?
73 TASK_UNINTERRUPTIBLE : TASK_INTERRUPTIBLE);
74 schedule_timeout(HZ / 10);
75 rc = falcon_spi_cmd(efx, spi, SPI_RDSR, -1, NULL,
76 &status, sizeof(status));
79 if (!(status & SPI_STATUS_NRDY))
81 if (signal_pending(current))
84 pr_err("%s: timed out waiting for %s\n",
85 part->common.name, part->common.dev_type_name);
90 falcon_spi_unlock(struct efx_nic *efx, const struct falcon_spi_device *spi)
92 const u8 unlock_mask = (SPI_STATUS_BP2 | SPI_STATUS_BP1 |
97 rc = falcon_spi_cmd(efx, spi, SPI_RDSR, -1, NULL,
98 &status, sizeof(status));
102 if (!(status & unlock_mask))
103 return 0; /* already unlocked */
105 rc = falcon_spi_cmd(efx, spi, SPI_WREN, -1, NULL, NULL, 0);
108 rc = falcon_spi_cmd(efx, spi, SPI_SST_EWSR, -1, NULL, NULL, 0);
112 status &= ~unlock_mask;
113 rc = falcon_spi_cmd(efx, spi, SPI_WRSR, -1, &status,
114 NULL, sizeof(status));
117 rc = falcon_spi_wait_write(efx, spi);
125 falcon_spi_erase(struct falcon_mtd_partition *part, loff_t start, size_t len)
127 const struct falcon_spi_device *spi = part->spi;
128 struct efx_nic *efx = part->common.mtd.priv;
129 unsigned pos, block_len;
130 u8 empty[FALCON_SPI_VERIFY_BUF_LEN];
131 u8 buffer[FALCON_SPI_VERIFY_BUF_LEN];
134 if (len != spi->erase_size)
137 if (spi->erase_command == 0)
140 rc = falcon_spi_unlock(efx, spi);
143 rc = falcon_spi_cmd(efx, spi, SPI_WREN, -1, NULL, NULL, 0);
146 rc = falcon_spi_cmd(efx, spi, spi->erase_command, start, NULL,
150 rc = falcon_spi_slow_wait(part, false);
152 /* Verify the entire region has been wiped */
153 memset(empty, 0xff, sizeof(empty));
154 for (pos = 0; pos < len; pos += block_len) {
155 block_len = min(len - pos, sizeof(buffer));
156 rc = falcon_spi_read(efx, spi, start + pos, block_len,
160 if (memcmp(empty, buffer, block_len))
163 /* Avoid locking up the system */
165 if (signal_pending(current))
174 static int efx_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
176 struct efx_nic *efx = mtd->priv;
179 rc = efx->mtd_ops->erase(mtd, erase->addr, erase->len);
181 erase->state = MTD_ERASE_DONE;
183 erase->state = MTD_ERASE_FAILED;
184 erase->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
186 mtd_erase_callback(erase);
190 static void efx_mtd_sync(struct mtd_info *mtd)
192 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
193 struct efx_nic *efx = mtd->priv;
196 rc = efx->mtd_ops->sync(mtd);
198 pr_err("%s: %s sync failed (%d)\n",
199 part->name, part->dev_type_name, rc);
202 static void efx_mtd_remove_partition(struct efx_mtd_partition *part)
207 rc = mtd_device_unregister(&part->mtd);
213 list_del(&part->node);
216 static void efx_mtd_rename_partition(struct efx_mtd_partition *part)
218 struct efx_nic *efx = part->mtd.priv;
220 efx->mtd_ops->rename(part);
223 static int efx_mtd_add(struct efx_nic *efx, struct efx_mtd_partition *parts,
224 size_t n_parts, size_t sizeof_part)
226 struct efx_mtd_partition *part;
229 for (i = 0; i < n_parts; i++) {
230 part = (struct efx_mtd_partition *)((char *)parts +
233 part->mtd.writesize = 1;
235 part->mtd.owner = THIS_MODULE;
236 part->mtd.priv = efx;
237 part->mtd.name = part->name;
238 part->mtd._erase = efx_mtd_erase;
239 part->mtd._read = efx->mtd_ops->read;
240 part->mtd._write = efx->mtd_ops->write;
241 part->mtd._sync = efx_mtd_sync;
243 efx_mtd_rename_partition(part);
245 if (mtd_device_register(&part->mtd, NULL, 0))
248 /* Add to list in order - efx_mtd_remove() depends on this */
249 list_add_tail(&part->node, &efx->mtd_list);
256 part = (struct efx_mtd_partition *)((char *)parts +
258 efx_mtd_remove_partition(part);
260 /* Failure is unlikely here, but probably means we're out of memory */
264 void efx_mtd_remove(struct efx_nic *efx)
266 struct efx_mtd_partition *parts, *part, *next;
268 WARN_ON(efx_dev_registered(efx));
270 if (list_empty(&efx->mtd_list))
273 parts = list_first_entry(&efx->mtd_list, struct efx_mtd_partition,
276 list_for_each_entry_safe(part, next, &efx->mtd_list, node)
277 efx_mtd_remove_partition(part);
282 void efx_mtd_rename(struct efx_nic *efx)
284 struct efx_mtd_partition *part;
288 list_for_each_entry(part, &efx->mtd_list, node)
289 efx_mtd_rename_partition(part);
292 int efx_mtd_probe(struct efx_nic *efx)
294 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0)
295 return siena_mtd_probe(efx);
297 return falcon_mtd_probe(efx);
300 /* Implementation of MTD operations for Falcon */
302 static void falcon_mtd_rename(struct efx_mtd_partition *part)
304 struct efx_nic *efx = part->mtd.priv;
306 snprintf(part->name, sizeof(part->name), "%s %s",
307 efx->name, part->type_name);
310 static int falcon_mtd_read(struct mtd_info *mtd, loff_t start,
311 size_t len, size_t *retlen, u8 *buffer)
313 struct falcon_mtd_partition *part = to_falcon_mtd_partition(mtd);
314 struct efx_nic *efx = mtd->priv;
315 struct falcon_nic_data *nic_data = efx->nic_data;
318 rc = mutex_lock_interruptible(&nic_data->spi_lock);
321 rc = falcon_spi_read(efx, part->spi, part->offset + start,
322 len, retlen, buffer);
323 mutex_unlock(&nic_data->spi_lock);
327 static int falcon_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len)
329 struct falcon_mtd_partition *part = to_falcon_mtd_partition(mtd);
330 struct efx_nic *efx = mtd->priv;
331 struct falcon_nic_data *nic_data = efx->nic_data;
334 rc = mutex_lock_interruptible(&nic_data->spi_lock);
337 rc = falcon_spi_erase(part, part->offset + start, len);
338 mutex_unlock(&nic_data->spi_lock);
342 static int falcon_mtd_write(struct mtd_info *mtd, loff_t start,
343 size_t len, size_t *retlen, const u8 *buffer)
345 struct falcon_mtd_partition *part = to_falcon_mtd_partition(mtd);
346 struct efx_nic *efx = mtd->priv;
347 struct falcon_nic_data *nic_data = efx->nic_data;
350 rc = mutex_lock_interruptible(&nic_data->spi_lock);
353 rc = falcon_spi_write(efx, part->spi, part->offset + start,
354 len, retlen, buffer);
355 mutex_unlock(&nic_data->spi_lock);
359 static int falcon_mtd_sync(struct mtd_info *mtd)
361 struct falcon_mtd_partition *part = to_falcon_mtd_partition(mtd);
362 struct efx_nic *efx = mtd->priv;
363 struct falcon_nic_data *nic_data = efx->nic_data;
366 mutex_lock(&nic_data->spi_lock);
367 rc = falcon_spi_slow_wait(part, true);
368 mutex_unlock(&nic_data->spi_lock);
372 static const struct efx_mtd_ops falcon_mtd_ops = {
373 .rename = falcon_mtd_rename,
374 .read = falcon_mtd_read,
375 .erase = falcon_mtd_erase,
376 .write = falcon_mtd_write,
377 .sync = falcon_mtd_sync,
380 static int falcon_mtd_probe(struct efx_nic *efx)
382 struct falcon_nic_data *nic_data = efx->nic_data;
383 struct falcon_mtd_partition *parts;
384 struct falcon_spi_device *spi;
390 efx->mtd_ops = &falcon_mtd_ops;
392 /* Allocate space for maximum number of partitions */
393 parts = kcalloc(2, sizeof(*parts), GFP_KERNEL);
396 spi = &nic_data->spi_flash;
397 if (falcon_spi_present(spi) && spi->size > FALCON_FLASH_BOOTCODE_START) {
398 parts[n_parts].spi = spi;
399 parts[n_parts].offset = FALCON_FLASH_BOOTCODE_START;
400 parts[n_parts].common.dev_type_name = "flash";
401 parts[n_parts].common.type_name = "sfc_flash_bootrom";
402 parts[n_parts].common.mtd.type = MTD_NORFLASH;
403 parts[n_parts].common.mtd.flags = MTD_CAP_NORFLASH;
404 parts[n_parts].common.mtd.size = spi->size - FALCON_FLASH_BOOTCODE_START;
405 parts[n_parts].common.mtd.erasesize = spi->erase_size;
409 spi = &nic_data->spi_eeprom;
410 if (falcon_spi_present(spi) && spi->size > FALCON_EEPROM_BOOTCONFIG_START) {
411 parts[n_parts].spi = spi;
412 parts[n_parts].offset = FALCON_EEPROM_BOOTCONFIG_START;
413 parts[n_parts].common.dev_type_name = "EEPROM";
414 parts[n_parts].common.type_name = "sfc_bootconfig";
415 parts[n_parts].common.mtd.type = MTD_RAM;
416 parts[n_parts].common.mtd.flags = MTD_CAP_RAM;
417 parts[n_parts].common.mtd.size =
418 min(spi->size, FALCON_EEPROM_BOOTCONFIG_END) -
419 FALCON_EEPROM_BOOTCONFIG_START;
420 parts[n_parts].common.mtd.erasesize = spi->erase_size;
424 rc = efx_mtd_add(efx, &parts[0].common, n_parts, sizeof(*parts));
430 /* Implementation of MTD operations for Siena */
432 struct efx_mcdi_mtd_partition {
433 struct efx_mtd_partition common;
439 #define to_efx_mcdi_mtd_partition(mtd) \
440 container_of(mtd, struct efx_mcdi_mtd_partition, common.mtd)
442 static void siena_mtd_rename(struct efx_mtd_partition *part)
444 struct efx_mcdi_mtd_partition *mcdi_part =
445 container_of(part, struct efx_mcdi_mtd_partition, common);
446 struct efx_nic *efx = part->mtd.priv;
448 snprintf(part->name, sizeof(part->name), "%s %s:%02x",
449 efx->name, part->type_name, mcdi_part->fw_subtype);
452 static int siena_mtd_read(struct mtd_info *mtd, loff_t start,
453 size_t len, size_t *retlen, u8 *buffer)
455 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
456 struct efx_nic *efx = mtd->priv;
457 loff_t offset = start;
458 loff_t end = min_t(loff_t, start + len, mtd->size);
462 while (offset < end) {
463 chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
464 rc = efx_mcdi_nvram_read(efx, part->nvram_type, offset,
472 *retlen = offset - start;
476 static int siena_mtd_erase(struct mtd_info *mtd, loff_t start, size_t len)
478 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
479 struct efx_nic *efx = mtd->priv;
480 loff_t offset = start & ~((loff_t)(mtd->erasesize - 1));
481 loff_t end = min_t(loff_t, start + len, mtd->size);
482 size_t chunk = part->common.mtd.erasesize;
485 if (!part->updating) {
486 rc = efx_mcdi_nvram_update_start(efx, part->nvram_type);
489 part->updating = true;
492 /* The MCDI interface can in fact do multiple erase blocks at once;
493 * but erasing may be slow, so we make multiple calls here to avoid
494 * tripping the MCDI RPC timeout. */
495 while (offset < end) {
496 rc = efx_mcdi_nvram_erase(efx, part->nvram_type, offset,
506 static int siena_mtd_write(struct mtd_info *mtd, loff_t start,
507 size_t len, size_t *retlen, const u8 *buffer)
509 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
510 struct efx_nic *efx = mtd->priv;
511 loff_t offset = start;
512 loff_t end = min_t(loff_t, start + len, mtd->size);
516 if (!part->updating) {
517 rc = efx_mcdi_nvram_update_start(efx, part->nvram_type);
520 part->updating = true;
523 while (offset < end) {
524 chunk = min_t(size_t, end - offset, EFX_MCDI_NVRAM_LEN_MAX);
525 rc = efx_mcdi_nvram_write(efx, part->nvram_type, offset,
533 *retlen = offset - start;
537 static int siena_mtd_sync(struct mtd_info *mtd)
539 struct efx_mcdi_mtd_partition *part = to_efx_mcdi_mtd_partition(mtd);
540 struct efx_nic *efx = mtd->priv;
543 if (part->updating) {
544 part->updating = false;
545 rc = efx_mcdi_nvram_update_finish(efx, part->nvram_type);
551 static const struct efx_mtd_ops siena_mtd_ops = {
552 .rename = siena_mtd_rename,
553 .read = siena_mtd_read,
554 .erase = siena_mtd_erase,
555 .write = siena_mtd_write,
556 .sync = siena_mtd_sync,
559 struct siena_nvram_type_info {
564 static const struct siena_nvram_type_info siena_nvram_types[] = {
565 [MC_CMD_NVRAM_TYPE_DISABLED_CALLISTO] = { 0, "sfc_dummy_phy" },
566 [MC_CMD_NVRAM_TYPE_MC_FW] = { 0, "sfc_mcfw" },
567 [MC_CMD_NVRAM_TYPE_MC_FW_BACKUP] = { 0, "sfc_mcfw_backup" },
568 [MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT0] = { 0, "sfc_static_cfg" },
569 [MC_CMD_NVRAM_TYPE_STATIC_CFG_PORT1] = { 1, "sfc_static_cfg" },
570 [MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT0] = { 0, "sfc_dynamic_cfg" },
571 [MC_CMD_NVRAM_TYPE_DYNAMIC_CFG_PORT1] = { 1, "sfc_dynamic_cfg" },
572 [MC_CMD_NVRAM_TYPE_EXP_ROM] = { 0, "sfc_exp_rom" },
573 [MC_CMD_NVRAM_TYPE_EXP_ROM_CFG_PORT0] = { 0, "sfc_exp_rom_cfg" },
574 [MC_CMD_NVRAM_TYPE_EXP_ROM_CFG_PORT1] = { 1, "sfc_exp_rom_cfg" },
575 [MC_CMD_NVRAM_TYPE_PHY_PORT0] = { 0, "sfc_phy_fw" },
576 [MC_CMD_NVRAM_TYPE_PHY_PORT1] = { 1, "sfc_phy_fw" },
577 [MC_CMD_NVRAM_TYPE_FPGA] = { 0, "sfc_fpga" },
580 static int siena_mtd_probe_partition(struct efx_nic *efx,
581 struct efx_mcdi_mtd_partition *part,
584 const struct siena_nvram_type_info *info;
585 size_t size, erase_size;
589 if (type >= ARRAY_SIZE(siena_nvram_types) ||
590 siena_nvram_types[type].name == NULL)
593 info = &siena_nvram_types[type];
595 if (info->port != efx_port_num(efx))
598 rc = efx_mcdi_nvram_info(efx, type, &size, &erase_size, &protected);
602 return -ENODEV; /* hide it */
604 part->nvram_type = type;
605 part->common.dev_type_name = "Siena NVRAM manager";
606 part->common.type_name = info->name;
608 part->common.mtd.type = MTD_NORFLASH;
609 part->common.mtd.flags = MTD_CAP_NORFLASH;
610 part->common.mtd.size = size;
611 part->common.mtd.erasesize = erase_size;
616 static int siena_mtd_get_fw_subtypes(struct efx_nic *efx,
617 struct efx_mcdi_mtd_partition *parts,
620 uint16_t fw_subtype_list[
621 MC_CMD_GET_BOARD_CFG_OUT_FW_SUBTYPE_LIST_MAXNUM];
625 rc = efx_mcdi_get_board_cfg(efx, NULL, fw_subtype_list, NULL);
629 for (i = 0; i < n_parts; i++)
630 parts[i].fw_subtype = fw_subtype_list[parts[i].nvram_type];
635 static int siena_mtd_probe(struct efx_nic *efx)
637 struct efx_mcdi_mtd_partition *parts;
645 efx->mtd_ops = &siena_mtd_ops;
647 rc = efx_mcdi_nvram_types(efx, &nvram_types);
651 parts = kcalloc(hweight32(nvram_types), sizeof(*parts), GFP_KERNEL);
658 while (nvram_types != 0) {
659 if (nvram_types & 1) {
660 rc = siena_mtd_probe_partition(efx, &parts[n_parts],
664 else if (rc != -ENODEV)
671 rc = siena_mtd_get_fw_subtypes(efx, parts, n_parts);
675 rc = efx_mtd_add(efx, &parts[0].common, n_parts, sizeof(*parts));