11 #include <linux/bitops.h>
12 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/rtnetlink.h>
25 #define EFX_SPI_VERIFY_BUF_LEN 16
60 #define efx_for_each_partition(part, efx_mtd) \
61 for ((part) = &(efx_mtd)->part[0]; \
62 (part) != &(efx_mtd)->part[(efx_mtd)->n_parts]; \
65 #define to_efx_mtd_partition(mtd) \
66 container_of(mtd, struct efx_mtd_partition, mtd)
68 static int falcon_mtd_probe(
struct efx_nic *efx);
69 static int siena_mtd_probe(
struct efx_nic *efx);
83 for (i = 0; i < 40; i++) {
88 &status,
sizeof(status));
96 pr_err(
"%s: timed out waiting for %s\n", part->
name, efx_mtd->
name);
109 &status,
sizeof(status));
113 if (!(status & unlock_mask))
123 status &= ~unlock_mask;
125 NULL,
sizeof(status));
138 struct efx_mtd *efx_mtd = part->
mtd.priv;
141 unsigned pos, block_len;
152 rc = efx_spi_unlock(efx, spi);
162 rc = efx_spi_slow_wait(part,
false);
165 memset(empty, 0xff,
sizeof(empty));
166 for (pos = 0; pos < len; pos += block_len) {
167 block_len =
min(len - pos,
sizeof(buffer));
172 if (
memcmp(empty, buffer, block_len))
188 struct efx_mtd *efx_mtd = mtd->
priv;
191 rc = efx_mtd->
ops->erase(mtd, erase->
addr, erase->
len);
202 static void efx_mtd_sync(
struct mtd_info *mtd)
205 struct efx_mtd *efx_mtd = mtd->
priv;
208 rc = efx_mtd->
ops->sync(mtd);
210 pr_err(
"%s: %s sync failed (%d)\n",
227 static void efx_mtd_remove_device(
struct efx_mtd *efx_mtd)
232 efx_mtd_remove_partition(part);
237 static
void efx_mtd_rename_device(
struct efx_mtd *efx_mtd)
244 "%
s %
s:%02
x", efx_mtd->efx->name,
248 "%
s %
s", efx_mtd->efx->name,
258 efx_mtd_rename_device(efx_mtd);
261 part->
mtd.writesize = 1;
264 part->
mtd.priv = efx_mtd;
266 part->
mtd._erase = efx_mtd_erase;
267 part->
mtd._read = efx_mtd->ops->read;
268 part->
mtd._write = efx_mtd->ops->write;
269 part->
mtd._sync = efx_mtd_sync;
275 list_add(&efx_mtd->node, &efx->mtd_list);
279 while (part != &efx_mtd->part[0]) {
281 efx_mtd_remove_partition(part);
289 struct efx_mtd *efx_mtd, *
next;
291 WARN_ON(efx_dev_registered(efx));
294 efx_mtd_remove_device(efx_mtd);
299 struct efx_mtd *efx_mtd;
304 efx_mtd_rename_device(efx_mtd);
310 return siena_mtd_probe(efx);
312 return falcon_mtd_probe(efx);
317 static int falcon_mtd_read(
struct mtd_info *mtd, loff_t start,
318 size_t len,
size_t *retlen,
u8 *buffer)
321 struct efx_mtd *efx_mtd = mtd->
priv;
336 static int falcon_mtd_erase(
struct mtd_info *mtd, loff_t start,
size_t len)
339 struct efx_mtd *efx_mtd = mtd->
priv;
347 rc = efx_spi_erase(part, part->
offset + start, len);
352 static int falcon_mtd_write(
struct mtd_info *mtd, loff_t start,
353 size_t len,
size_t *retlen,
const u8 *buffer)
356 struct efx_mtd *efx_mtd = mtd->
priv;
371 static int falcon_mtd_sync(
struct mtd_info *mtd)
374 struct efx_mtd *efx_mtd = mtd->
priv;
380 rc = efx_spi_slow_wait(part,
true);
386 .read = falcon_mtd_read,
387 .erase = falcon_mtd_erase,
388 .write = falcon_mtd_write,
389 .sync = falcon_mtd_sync,
392 static int falcon_mtd_probe(
struct efx_nic *efx)
396 struct efx_mtd *efx_mtd;
403 efx_mtd = kzalloc(
sizeof(*efx_mtd) +
sizeof(efx_mtd->
part[0]),
409 efx_mtd->
name =
"flash";
410 efx_mtd->
ops = &falcon_mtd_ops;
418 efx_mtd->
part[0].type_name =
"sfc_flash_bootrom";
420 rc = efx_mtd_probe_device(efx, efx_mtd);
429 efx_mtd = kzalloc(
sizeof(*efx_mtd) +
sizeof(efx_mtd->
part[0]),
435 efx_mtd->
name =
"EEPROM";
436 efx_mtd->
ops = &falcon_mtd_ops;
441 efx_mtd->
part[0].mtd.size =
446 efx_mtd->
part[0].type_name =
"sfc_bootconfig";
448 rc = efx_mtd_probe_device(efx, efx_mtd);
460 static int siena_mtd_read(
struct mtd_info *mtd, loff_t start,
461 size_t len,
size_t *retlen,
u8 *buffer)
464 struct efx_mtd *efx_mtd = mtd->
priv;
471 while (offset < end) {
481 *retlen = offset -
start;
485 static int siena_mtd_erase(
struct mtd_info *mtd, loff_t start,
size_t len)
488 struct efx_mtd *efx_mtd = mtd->
priv;
490 loff_t offset = start & ~((loff_t)(mtd->
erasesize - 1));
491 loff_t end =
min_t(loff_t, start + len, mtd->
size);
492 size_t chunk = part->
mtd.erasesize;
495 if (!part->
mcdi.updating) {
499 part->
mcdi.updating =
true;
505 while (offset < end) {
516 static int siena_mtd_write(
struct mtd_info *mtd, loff_t start,
517 size_t len,
size_t *retlen,
const u8 *buffer)
520 struct efx_mtd *efx_mtd = mtd->
priv;
522 loff_t offset =
start;
523 loff_t end =
min_t(loff_t, start + len, mtd->
size);
527 if (!part->
mcdi.updating) {
531 part->
mcdi.updating =
true;
534 while (offset < end) {
544 *retlen = offset -
start;
548 static int siena_mtd_sync(
struct mtd_info *mtd)
551 struct efx_mtd *efx_mtd = mtd->
priv;
555 if (part->
mcdi.updating) {
556 part->
mcdi.updating =
false;
564 .read = siena_mtd_read,
565 .erase = siena_mtd_erase,
566 .write = siena_mtd_write,
567 .sync = siena_mtd_sync,
591 static int siena_mtd_probe_partition(
struct efx_nic *efx,
592 struct efx_mtd *efx_mtd,
593 unsigned int part_id,
598 size_t size, erase_size;
603 siena_nvram_types[type].name ==
NULL)
606 info = &siena_nvram_types[
type];
608 if (info->
port != efx_port_num(efx))
623 part->
mtd.erasesize = erase_size;
628 static int siena_mtd_get_fw_subtypes(
struct efx_nic *efx,
629 struct efx_mtd *efx_mtd)
648 struct efx_mtd *efx_mtd;
659 efx_mtd = kzalloc(
sizeof(*efx_mtd) +
665 efx_mtd->
name =
"Siena NVRAM manager";
667 efx_mtd->
ops = &siena_mtd_ops;
672 while (nvram_types != 0) {
673 if (nvram_types & 1) {
674 rc = siena_mtd_probe_partition(efx, efx_mtd,
685 rc = siena_mtd_get_fw_subtypes(efx, efx_mtd);
689 rc = efx_mtd_probe_device(efx, efx_mtd);