1 /*-
2 * Copyright (c) 2012 Semihalf
3 * Copyright (c) 2009 Jakub Klama <jakub.klama@uj.edu.pl>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/slicer.h>
38
39 #include <geom/geom.h>
40 #include <geom/geom_disk.h>
41 #include <geom/geom_flashmap.h>
42 #include <geom/geom_slice.h>
43
44 #include <dev/nand/nand_dev.h>
45
46 struct g_flashmap_slice {
47 off_t sl_start;
48 off_t sl_end;
49 const char *sl_name;
50
51 STAILQ_ENTRY(g_flashmap_slice) sl_link;
52 };
53
54 STAILQ_HEAD(g_flashmap_head, g_flashmap_slice);
55
56 static struct {
57 const char *type;
58 flash_slicer_t slicer;
59 } g_flashmap_slicers[] = {
60 { "NAND::device", NULL },
61 { "CFI::device", NULL },
62 { "SPI::device", NULL },
63 { "MMC::device", NULL }
64 };
65
66 static g_ioctl_t g_flashmap_ioctl;
67 static g_taste_t g_flashmap_taste;
68
69 static int g_flashmap_load(device_t dev, struct g_provider *pp,
70 flash_slicer_t slicer, struct g_flashmap_head *head);
71 static int g_flashmap_modify(struct g_flashmap *gfp, struct g_geom *gp,
72 const char *devname, int secsize, struct g_flashmap_head *slices);
73 static void g_flashmap_print(struct g_flashmap_slice *slice);
74
75 MALLOC_DECLARE(M_FLASHMAP);
76 MALLOC_DEFINE(M_FLASHMAP, "geom_flashmap", "GEOM flash memory slicer class");
77
78 static void
79 g_flashmap_print(struct g_flashmap_slice *slice)
80 {
81
82 printf("%08jx-%08jx: %s (%juKB)\n", (uintmax_t)slice->sl_start,
83 (uintmax_t)slice->sl_end, slice->sl_name,
84 (uintmax_t)(slice->sl_end - slice->sl_start) / 1024);
85 }
86
87 static int
88 g_flashmap_modify(struct g_flashmap *gfp, struct g_geom *gp,
89 const char *devname, int secsize, struct g_flashmap_head *slices)
90 {
91 struct g_flashmap_slice *slice;
92 int i, error;
93
94 g_topology_assert();
95
96 i = 0;
97 STAILQ_FOREACH(slice, slices, sl_link) {
98 if (bootverbose) {
99 printf("%s: slice ", devname);
100 g_flashmap_print(slice);
101 }
102
103 error = g_slice_config(gp, i++, G_SLICE_CONFIG_CHECK,
104 slice->sl_start,
105 slice->sl_end - slice->sl_start + 1,
106 secsize, FLASH_SLICES_FMT, gp->name, slice->sl_name);
107
108 if (error)
109 return (error);
110 }
111
112 i = 0;
113 STAILQ_FOREACH(slice, slices, sl_link) {
114 free(__DECONST(void *, gfp->labels[i]), M_FLASHMAP);
115 gfp->labels[i] = strdup(slice->sl_name, M_FLASHMAP);
116 error = g_slice_config(gp, i++, G_SLICE_CONFIG_SET,
117 slice->sl_start,
118 slice->sl_end - slice->sl_start + 1,
119 secsize, "%ss.%s", gp->name, slice->sl_name);
120
121 if (error)
122 return (error);
123 }
124
125 return (0);
126 }
127
128 static int
129 g_flashmap_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag,
130 struct thread *td)
131 {
132 struct g_consumer *cp;
133 struct g_geom *gp;
134
135 if (cmd != NAND_IO_GET_CHIP_PARAM)
136 return (ENOIOCTL);
137
138 cp = LIST_FIRST(&pp->geom->consumer);
139 if (cp == NULL)
140 return (ENOIOCTL);
141 gp = cp->provider->geom;
142 if (gp->ioctl == NULL)
143 return (ENOIOCTL);
144
145 return (gp->ioctl(cp->provider, cmd, data, fflag, td));
146 }
147
148 static struct g_geom *
149 g_flashmap_taste(struct g_class *mp, struct g_provider *pp, int flags)
150 {
151 struct g_geom *gp;
152 struct g_consumer *cp;
153 struct g_flashmap_head head;
154 struct g_flashmap_slice *slice, *slice_temp;
155 struct g_flashmap *gfp;
156 flash_slicer_t slicer;
157 device_t dev;
158 int i, size;
159
160 g_trace(G_T_TOPOLOGY, "flashmap_taste(%s,%s)", mp->name, pp->name);
161 g_topology_assert();
162
163 if (flags == G_TF_NORMAL &&
164 strcmp(pp->geom->class->name, G_DISK_CLASS_NAME) != 0)
165 return (NULL);
166
167 gp = g_slice_new(mp, FLASH_SLICES_MAX_NUM, pp, &cp, (void**)&gfp,
168 sizeof(struct g_flashmap), NULL);
169 if (gp == NULL)
170 return (NULL);
171
172 STAILQ_INIT(&head);
173
174 do {
175 slicer = NULL;
176 for (i = 0; i < nitems(g_flashmap_slicers); i++) {
177 size = sizeof(device_t);
178 if (g_io_getattr(g_flashmap_slicers[i].type, cp,
179 &size, &dev) == 0) {
180 slicer = g_flashmap_slicers[i].slicer;
181 break;
182 }
183 }
184 if (slicer == NULL)
185 break;
186
187 if (g_flashmap_load(dev, pp, slicer, &head) == 0)
188 break;
189
190 g_flashmap_modify(gfp, gp, cp->provider->name,
191 cp->provider->sectorsize, &head);
192 } while (0);
193
194 g_access(cp, -1, 0, 0);
195
196 STAILQ_FOREACH_SAFE(slice, &head, sl_link, slice_temp)
197 free(slice, M_FLASHMAP);
198
199 if (LIST_EMPTY(&gp->provider)) {
200 g_slice_spoiled(cp);
201 return (NULL);
202 }
203 return (gp);
204 }
205
206 static int
207 g_flashmap_load(device_t dev, struct g_provider *pp, flash_slicer_t slicer,
208 struct g_flashmap_head *head)
209 {
210 struct flash_slice *slices;
211 struct g_flashmap_slice *slice;
212 int i, nslices = 0;
213
214 slices = malloc(sizeof(struct flash_slice) * FLASH_SLICES_MAX_NUM,
215 M_FLASHMAP, M_WAITOK | M_ZERO);
216 if (slicer(dev, pp->name, slices, &nslices) == 0) {
217 for (i = 0; i < nslices; i++) {
218 slice = malloc(sizeof(struct g_flashmap_slice),
219 M_FLASHMAP, M_WAITOK);
220
221 slice->sl_name = slices[i].label;
222 slice->sl_start = slices[i].base;
223 slice->sl_end = slices[i].base + slices[i].size - 1;
224
225 STAILQ_INSERT_TAIL(head, slice, sl_link);
226 }
227 }
228
229 free(slices, M_FLASHMAP);
230 return (nslices);
231 }
232
233 void flash_register_slicer(flash_slicer_t slicer, u_int type, bool force)
234 {
235
236 g_topology_lock();
237 if (g_flashmap_slicers[type].slicer == NULL || force == TRUE)
238 g_flashmap_slicers[type].slicer = slicer;
239 g_topology_unlock();
240 }
241
242 static struct g_class g_flashmap_class = {
243 .name = FLASHMAP_CLASS_NAME,
244 .version = G_VERSION,
245 .taste = g_flashmap_taste,
246 .ioctl = g_flashmap_ioctl,
247 };
248
249 DECLARE_GEOM_CLASS(g_flashmap_class, g_flashmap);
250 MODULE_VERSION(g_flashmap, 0);
Cache object: 3414a080f70f309efbd3aa4595f0b8d9
|