]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
f4150724 | 2 | /**************************************************************************** |
f7a6d2c4 | 3 | * Driver for Solarflare network controllers and boards |
f4150724 | 4 | * Copyright 2005-2006 Fen Systems Ltd. |
f7a6d2c4 | 5 | * Copyright 2006-2013 Solarflare Communications Inc. |
f4150724 BH |
6 | */ |
7 | ||
8 | #include <linux/module.h> | |
9 | #include <linux/mtd/mtd.h> | |
5a0e3ad6 | 10 | #include <linux/slab.h> |
76884835 | 11 | #include <linux/rtnetlink.h> |
f4150724 | 12 | |
f4150724 | 13 | #include "net_driver.h" |
ff2ef902 | 14 | #include "efx.h" |
76884835 | 15 | |
76884835 BH |
16 | #define to_efx_mtd_partition(mtd) \ |
17 | container_of(mtd, struct efx_mtd_partition, mtd) | |
18 | ||
f4150724 BH |
19 | /* MTD interface */ |
20 | ||
76884835 BH |
21 | static int efx_mtd_erase(struct mtd_info *mtd, struct erase_info *erase) |
22 | { | |
b766630b | 23 | struct efx_nic *efx = mtd->priv; |
76884835 | 24 | |
e7bfb3fd | 25 | return efx->type->mtd_erase(mtd, erase->addr, erase->len); |
76884835 BH |
26 | } |
27 | ||
28 | static void efx_mtd_sync(struct mtd_info *mtd) | |
29 | { | |
0c605a20 | 30 | struct efx_mtd_partition *part = to_efx_mtd_partition(mtd); |
b766630b | 31 | struct efx_nic *efx = mtd->priv; |
76884835 BH |
32 | int rc; |
33 | ||
45a3fd55 | 34 | rc = efx->type->mtd_sync(mtd); |
76884835 | 35 | if (rc) |
0c605a20 | 36 | pr_err("%s: %s sync failed (%d)\n", |
b766630b | 37 | part->name, part->dev_type_name, rc); |
76884835 BH |
38 | } |
39 | ||
40 | static void efx_mtd_remove_partition(struct efx_mtd_partition *part) | |
41 | { | |
42 | int rc; | |
43 | ||
44 | for (;;) { | |
ee0e87b1 | 45 | rc = mtd_device_unregister(&part->mtd); |
76884835 BH |
46 | if (rc != -EBUSY) |
47 | break; | |
48 | ssleep(1); | |
49 | } | |
50 | WARN_ON(rc); | |
b766630b | 51 | list_del(&part->node); |
76884835 BH |
52 | } |
53 | ||
45a3fd55 BH |
54 | int efx_mtd_add(struct efx_nic *efx, struct efx_mtd_partition *parts, |
55 | size_t n_parts, size_t sizeof_part) | |
76884835 BH |
56 | { |
57 | struct efx_mtd_partition *part; | |
b766630b | 58 | size_t i; |
76884835 | 59 | |
b766630b | 60 | for (i = 0; i < n_parts; i++) { |
141d748e BH |
61 | part = (struct efx_mtd_partition *)((char *)parts + |
62 | i * sizeof_part); | |
76884835 | 63 | |
76884835 BH |
64 | part->mtd.writesize = 1; |
65 | ||
5fb1beec BK |
66 | if (!(part->mtd.flags & MTD_NO_ERASE)) |
67 | part->mtd.flags |= MTD_WRITEABLE; | |
68 | ||
76884835 | 69 | part->mtd.owner = THIS_MODULE; |
b766630b | 70 | part->mtd.priv = efx; |
76884835 | 71 | part->mtd.name = part->name; |
3c3c10bb | 72 | part->mtd._erase = efx_mtd_erase; |
45a3fd55 BH |
73 | part->mtd._read = efx->type->mtd_read; |
74 | part->mtd._write = efx->type->mtd_write; | |
3c3c10bb | 75 | part->mtd._sync = efx_mtd_sync; |
76884835 | 76 | |
45a3fd55 | 77 | efx->type->mtd_rename(part); |
b766630b | 78 | |
ee0e87b1 | 79 | if (mtd_device_register(&part->mtd, NULL, 0)) |
76884835 | 80 | goto fail; |
b766630b BH |
81 | |
82 | /* Add to list in order - efx_mtd_remove() depends on this */ | |
83 | list_add_tail(&part->node, &efx->mtd_list); | |
76884835 BH |
84 | } |
85 | ||
76884835 BH |
86 | return 0; |
87 | ||
88 | fail: | |
141d748e BH |
89 | while (i--) { |
90 | part = (struct efx_mtd_partition *)((char *)parts + | |
91 | i * sizeof_part); | |
92 | efx_mtd_remove_partition(part); | |
93 | } | |
7c43161c | 94 | /* Failure is unlikely here, but probably means we're out of memory */ |
76884835 BH |
95 | return -ENOMEM; |
96 | } | |
97 | ||
98 | void efx_mtd_remove(struct efx_nic *efx) | |
f4150724 | 99 | { |
b766630b | 100 | struct efx_mtd_partition *parts, *part, *next; |
76884835 BH |
101 | |
102 | WARN_ON(efx_dev_registered(efx)); | |
103 | ||
b766630b BH |
104 | if (list_empty(&efx->mtd_list)) |
105 | return; | |
106 | ||
107 | parts = list_first_entry(&efx->mtd_list, struct efx_mtd_partition, | |
108 | node); | |
109 | ||
110 | list_for_each_entry_safe(part, next, &efx->mtd_list, node) | |
111 | efx_mtd_remove_partition(part); | |
112 | ||
113 | kfree(parts); | |
76884835 BH |
114 | } |
115 | ||
116 | void efx_mtd_rename(struct efx_nic *efx) | |
117 | { | |
b766630b | 118 | struct efx_mtd_partition *part; |
76884835 BH |
119 | |
120 | ASSERT_RTNL(); | |
121 | ||
b766630b | 122 | list_for_each_entry(part, &efx->mtd_list, node) |
45a3fd55 | 123 | efx->type->mtd_rename(part); |
8880f4ec | 124 | } |