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