WIP FPC-III support
[linux/fpc-iii.git] / drivers / net / ethernet / sfc / falcon / mtd.c
blob15bd47bf9e8e86402c2b07db82e36c6f89446904
1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3 * Driver for Solarflare network controllers and boards
4 * Copyright 2005-2006 Fen Systems Ltd.
5 * Copyright 2006-2013 Solarflare Communications Inc.
6 */
8 #include <linux/module.h>
9 #include <linux/mtd/mtd.h>
10 #include <linux/slab.h>
11 #include <linux/rtnetlink.h>
13 #include "net_driver.h"
14 #include "efx.h"
16 #define to_ef4_mtd_partition(mtd) \
17 container_of(mtd, struct ef4_mtd_partition, mtd)
19 /* MTD interface */
21 static int ef4_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
23 struct ef4_nic *efx = mtd->priv;
25 return efx->type->mtd_erase(mtd, erase->addr, erase->len);
28 static void ef4_mtd_sync(struct mtd_info *mtd)
30 struct ef4_mtd_partition *part = to_ef4_mtd_partition(mtd);
31 struct ef4_nic *efx = mtd->priv;
32 int rc;
34 rc = efx->type->mtd_sync(mtd);
35 if (rc)
36 pr_err("%s: %s sync failed (%d)\n",
37 part->name, part->dev_type_name, rc);
40 static void ef4_mtd_remove_partition(struct ef4_mtd_partition *part)
42 int rc;
44 for (;;) {
45 rc = mtd_device_unregister(&part->mtd);
46 if (rc != -EBUSY)
47 break;
48 ssleep(1);
50 WARN_ON(rc);
51 list_del(&part->node);
54 int ef4_mtd_add(struct ef4_nic *efx, struct ef4_mtd_partition *parts,
55 size_t n_parts, size_t sizeof_part)
57 struct ef4_mtd_partition *part;
58 size_t i;
60 for (i = 0; i < n_parts; i++) {
61 part = (struct ef4_mtd_partition *)((char *)parts +
62 i * sizeof_part);
64 part->mtd.writesize = 1;
66 part->mtd.owner = THIS_MODULE;
67 part->mtd.priv = efx;
68 part->mtd.name = part->name;
69 part->mtd._erase = ef4_mtd_erase;
70 part->mtd._read = efx->type->mtd_read;
71 part->mtd._write = efx->type->mtd_write;
72 part->mtd._sync = ef4_mtd_sync;
74 efx->type->mtd_rename(part);
76 if (mtd_device_register(&part->mtd, NULL, 0))
77 goto fail;
79 /* Add to list in order - ef4_mtd_remove() depends on this */
80 list_add_tail(&part->node, &efx->mtd_list);
83 return 0;
85 fail:
86 while (i--) {
87 part = (struct ef4_mtd_partition *)((char *)parts +
88 i * sizeof_part);
89 ef4_mtd_remove_partition(part);
91 /* Failure is unlikely here, but probably means we're out of memory */
92 return -ENOMEM;
95 void ef4_mtd_remove(struct ef4_nic *efx)
97 struct ef4_mtd_partition *parts, *part, *next;
99 WARN_ON(ef4_dev_registered(efx));
101 if (list_empty(&efx->mtd_list))
102 return;
104 parts = list_first_entry(&efx->mtd_list, struct ef4_mtd_partition,
105 node);
107 list_for_each_entry_safe(part, next, &efx->mtd_list, node)
108 ef4_mtd_remove_partition(part);
110 kfree(parts);
113 void ef4_mtd_rename(struct ef4_nic *efx)
115 struct ef4_mtd_partition *part;
117 ASSERT_RTNL();
119 list_for_each_entry(part, &efx->mtd_list, node)
120 efx->type->mtd_rename(part);