2 * Copyright (c) 2012 Semihalf
3 * Copyright (c) 2009 Jakub Klama <jakub.klama@uj.edu.pl>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/endian.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/fcntl.h>
36 #include <sys/malloc.h>
40 #include <sys/mutex.h>
41 #include <sys/slicer.h>
43 #include <geom/geom.h>
44 #include <geom/geom_slice.h>
45 #include <geom/geom_disk.h>
46 #include <dev/nand/nand_dev.h>
48 #define FLASHMAP_CLASS_NAME "Flashmap"
50 struct g_flashmap_slice {
55 STAILQ_ENTRY(g_flashmap_slice) sl_link;
58 STAILQ_HEAD(g_flashmap_head, g_flashmap_slice);
60 static void g_flashmap_print(struct g_flashmap_slice *);
61 static int g_flashmap_modify(struct g_geom *, const char *,
62 int, struct g_flashmap_head *);
63 static int g_flashmap_start(struct bio *);
64 static int g_flashmap_ioctl(struct g_provider *, u_long, void *,
65 int, struct thread *);
66 static void g_flashmap_dumpconf(struct sbuf *, const char *,
67 struct g_geom *, struct g_consumer *, struct g_provider *);
68 static struct g_geom *g_flashmap_taste(struct g_class *,
69 struct g_provider *, int);
70 static void g_flashmap_config(struct gctl_req *, struct g_class *,
72 static int g_flashmap_load(device_t, struct g_flashmap_head *);
73 static int (*flash_fill_slices)(device_t, struct flash_slice *, int *) =
74 fdt_flash_fill_slices;
76 MALLOC_DECLARE(M_FLASHMAP);
77 MALLOC_DEFINE(M_FLASHMAP, "geom_flashmap", "GEOM flash memory slicer class");
80 g_flashmap_print(struct g_flashmap_slice *slice)
83 printf("%08jx-%08jx: %s (%juKB)\n", (uintmax_t)slice->sl_start,
84 (uintmax_t)slice->sl_end, slice->sl_name,
85 (uintmax_t)(slice->sl_end - slice->sl_start) / 1024);
89 g_flashmap_modify(struct g_geom *gp, const char *devname, int secsize,
90 struct g_flashmap_head *slices)
92 struct g_flashmap_slice *slice;
98 STAILQ_FOREACH(slice, slices, sl_link) {
100 printf("%s: slice ", devname);
101 g_flashmap_print(slice);
104 error = g_slice_config(gp, i++, G_SLICE_CONFIG_CHECK,
106 slice->sl_end - slice->sl_start + 1,
107 secsize, "%ss.%s", gp->name, slice->sl_name);
114 STAILQ_FOREACH(slice, slices, sl_link) {
115 error = g_slice_config(gp, i++, G_SLICE_CONFIG_SET,
117 slice->sl_end - slice->sl_start + 1,
118 secsize, "%ss.%s", gp->name, slice->sl_name);
128 g_flashmap_start(struct bio *bp)
135 g_flashmap_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
136 struct g_consumer *cp __unused, struct g_provider *pp)
138 struct g_slicer *gsp;
141 g_slice_dumpconf(sb, indent, gp, cp, pp);
145 g_flashmap_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag,
148 struct g_consumer *cp;
151 if (cmd != NAND_IO_GET_CHIP_PARAM)
154 cp = LIST_FIRST(&pp->geom->consumer);
157 gp = cp->provider->geom;
158 if (gp->ioctl == NULL)
161 return (gp->ioctl(cp->provider, cmd, data, fflag, td));
165 static struct g_geom *
166 g_flashmap_taste(struct g_class *mp, struct g_provider *pp, int flags)
168 struct g_geom *gp = NULL;
169 struct g_consumer *cp;
170 struct g_flashmap_head head;
171 struct g_flashmap_slice *slice, *slice_temp;
175 g_trace(G_T_TOPOLOGY, "flashmap_taste(%s,%s)", mp->name, pp->name);
178 if (flags == G_TF_NORMAL &&
179 strcmp(pp->geom->class->name, G_DISK_CLASS_NAME) != 0)
182 gp = g_slice_new(mp, FLASH_SLICES_MAX_NUM, pp, &cp, NULL, 0,
190 size = sizeof(device_t);
191 if (g_io_getattr("NAND::device", cp, &size, &dev)) {
192 size = sizeof(device_t);
193 if (g_io_getattr("CFI::device", cp, &size, &dev)) {
194 size = sizeof(device_t);
195 if (g_io_getattr("SPI::device", cp, &size,
201 nslices = g_flashmap_load(dev, &head);
205 g_flashmap_modify(gp, cp->provider->name,
206 cp->provider->sectorsize, &head);
209 g_access(cp, -1, 0, 0);
211 STAILQ_FOREACH_SAFE(slice, &head, sl_link, slice_temp) {
212 free(slice, M_FLASHMAP);
215 if (LIST_EMPTY(&gp->provider)) {
223 g_flashmap_config(struct gctl_req *req, struct g_class *mp, const char *verb)
226 gctl_error(req, "unknown config verb");
230 g_flashmap_load(device_t dev, struct g_flashmap_head *head)
232 struct flash_slice *slices;
233 struct g_flashmap_slice *slice;
234 uint32_t i, buf_size;
237 buf_size = sizeof(struct flash_slice) * FLASH_SLICES_MAX_NUM;
238 slices = malloc(buf_size, M_FLASHMAP, M_WAITOK | M_ZERO);
239 if (flash_fill_slices &&
240 flash_fill_slices(dev, slices, &nslices) == 0) {
241 for (i = 0; i < nslices; i++) {
242 slice = malloc(sizeof(struct g_flashmap_slice),
243 M_FLASHMAP, M_WAITOK);
245 slice->sl_name = slices[i].label;
246 slice->sl_start = slices[i].base;
247 slice->sl_end = slices[i].base + slices[i].size - 1;
249 STAILQ_INSERT_TAIL(head, slice, sl_link);
253 free(slices, M_FLASHMAP);
257 void flash_register_slicer(int (*slicer)(device_t, struct flash_slice *, int *))
260 flash_fill_slices = slicer;
263 static struct g_class g_flashmap_class = {
264 .name = FLASHMAP_CLASS_NAME,
265 .version = G_VERSION,
266 .taste = g_flashmap_taste,
267 .dumpconf = g_flashmap_dumpconf,
268 .ioctl = g_flashmap_ioctl,
269 .ctlreq = g_flashmap_config,
272 DECLARE_GEOM_CLASS(g_flashmap_class, g_flashmap);