treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / ethernet / sfc / mtd.c
blob273c08e5455fca3e892e4190b86d12fcd4e280f7
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_efx_mtd_partition(mtd) \
17 container_of(mtd, struct efx_mtd_partition, mtd)
19 /* MTD interface */
21 static int efx_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
23 struct efx_nic *efx = mtd->priv;
25 return efx->type->mtd_erase(mtd, erase->addr, erase->len);
28 static void efx_mtd_sync(struct mtd_info *mtd)
30 struct efx_mtd_partition *part = to_efx_mtd_partition(mtd);
31 struct efx_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 efx_mtd_remove_partition(struct efx_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 efx_mtd_add(struct efx_nic *efx, struct efx_mtd_partition *parts,
55 size_t n_parts, size_t sizeof_part)
57 struct efx_mtd_partition *part;
58 size_t i;
60 for (i = 0; i < n_parts; i++) {
61 part = (struct efx_mtd_partition *)((char *)parts +
62 i * sizeof_part);
64 part->mtd.writesize = 1;
66 if (!(part->mtd.flags & MTD_NO_ERASE))
67 part->mtd.flags |= MTD_WRITEABLE;
69 part->mtd.owner = THIS_MODULE;
70 part->mtd.priv = efx;
71 part->mtd.name = part->name;
72 part->mtd._erase = efx_mtd_erase;
73 part->mtd._read = efx->type->mtd_read;
74 part->mtd._write = efx->type->mtd_write;
75 part->mtd._sync = efx_mtd_sync;
77 efx->type->mtd_rename(part);
79 if (mtd_device_register(&part->mtd, NULL, 0))
80 goto fail;
82 /* Add to list in order - efx_mtd_remove() depends on this */
83 list_add_tail(&part->node, &efx->mtd_list);
86 return 0;
88 fail:
89 while (i--) {
90 part = (struct efx_mtd_partition *)((char *)parts +
91 i * sizeof_part);
92 efx_mtd_remove_partition(part);
94 /* Failure is unlikely here, but probably means we're out of memory */
95 return -ENOMEM;
98 void efx_mtd_remove(struct efx_nic *efx)
100 struct efx_mtd_partition *parts, *part, *next;
102 WARN_ON(efx_dev_registered(efx));
104 if (list_empty(&efx->mtd_list))
105 return;
107 parts = list_first_entry(&efx->mtd_list, struct efx_mtd_partition,
108 node);
110 list_for_each_entry_safe(part, next, &efx->mtd_list, node)
111 efx_mtd_remove_partition(part);
113 kfree(parts);
116 void efx_mtd_rename(struct efx_nic *efx)
118 struct efx_mtd_partition *part;
120 ASSERT_RTNL();
122 list_for_each_entry(part, &efx->mtd_list, node)
123 efx->type->mtd_rename(part);