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 *);
74 MALLOC_DECLARE(M_FLASHMAP);
75 MALLOC_DEFINE(M_FLASHMAP, "geom_flashmap", "GEOM flash memory slicer class");
78 g_flashmap_print(struct g_flashmap_slice *slice)
81 printf("%08jx-%08jx: %s (%juKB)\n", (uintmax_t)slice->sl_start,
82 (uintmax_t)slice->sl_end, slice->sl_name,
83 (uintmax_t)(slice->sl_end - slice->sl_start) / 1024);
87 g_flashmap_modify(struct g_geom *gp, const char *devname, int secsize,
88 struct g_flashmap_head *slices)
90 struct g_flashmap_slice *slice;
96 STAILQ_FOREACH(slice, slices, sl_link) {
98 printf("%s: slice ", devname);
99 g_flashmap_print(slice);
102 error = g_slice_config(gp, i++, G_SLICE_CONFIG_CHECK,
104 slice->sl_end - slice->sl_start + 1,
105 secsize, "%ss.%s", gp->name, slice->sl_name);
112 STAILQ_FOREACH(slice, slices, sl_link) {
113 error = g_slice_config(gp, i++, G_SLICE_CONFIG_SET,
115 slice->sl_end - slice->sl_start + 1,
116 secsize, "%ss.%s", gp->name, slice->sl_name);
126 g_flashmap_start(struct bio *bp)
133 g_flashmap_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
134 struct g_consumer *cp __unused, struct g_provider *pp)
136 struct g_slicer *gsp;
139 g_slice_dumpconf(sb, indent, gp, cp, pp);
143 g_flashmap_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag,
146 struct g_consumer *cp;
149 if (cmd != NAND_IO_GET_CHIP_PARAM)
152 cp = LIST_FIRST(&pp->geom->consumer);
155 gp = cp->provider->geom;
156 if (gp->ioctl == NULL)
159 return (gp->ioctl(cp->provider, cmd, data, fflag, td));
163 static struct g_geom *
164 g_flashmap_taste(struct g_class *mp, struct g_provider *pp, int flags)
166 struct g_geom *gp = NULL;
167 struct g_consumer *cp;
168 struct g_flashmap_head head;
169 struct g_flashmap_slice *slice, *slice_temp;
173 g_trace(G_T_TOPOLOGY, "flashmap_taste(%s,%s)", mp->name, pp->name);
176 if (flags == G_TF_NORMAL &&
177 !strcmp(pp->geom->class->name, FLASHMAP_CLASS_NAME))
180 gp = g_slice_new(mp, FLASH_SLICES_MAX_NUM, pp, &cp, NULL, 0,
188 size = sizeof(device_t);
189 if (g_io_getattr("NAND::device", cp, &size, &dev))
192 nslices = g_flashmap_load(dev, &head);
196 g_flashmap_modify(gp, cp->provider->name,
197 cp->provider->sectorsize, &head);
200 g_access(cp, -1, 0, 0);
202 STAILQ_FOREACH_SAFE(slice, &head, sl_link, slice_temp) {
203 free(slice, M_FLASHMAP);
206 if (LIST_EMPTY(&gp->provider)) {
214 g_flashmap_config(struct gctl_req *req, struct g_class *mp, const char *verb)
217 gctl_error(req, "unknown config verb");
221 g_flashmap_load(device_t dev, struct g_flashmap_head *head)
223 struct flash_slice *slices;
224 struct g_flashmap_slice *slice;
225 uint32_t i, buf_size;
228 buf_size = sizeof(struct flash_slice) * FLASH_SLICES_MAX_NUM;
229 slices = malloc(buf_size, M_FLASHMAP, M_WAITOK | M_ZERO);
230 if (flash_fill_slices(dev, slices, &nslices) == 0) {
231 for (i = 0; i < nslices; i++) {
232 slice = malloc(sizeof(struct g_flashmap_slice),
233 M_FLASHMAP, M_WAITOK);
235 slice->sl_name = slices[i].label;
236 slice->sl_start = slices[i].base;
237 slice->sl_end = slices[i].base + slices[i].size - 1;
239 STAILQ_INSERT_TAIL(head, slice, sl_link);
243 free(slices, M_FLASHMAP);
247 static struct g_class g_flashmap_class = {
248 .name = FLASHMAP_CLASS_NAME,
249 .version = G_VERSION,
250 .taste = g_flashmap_taste,
251 .dumpconf = g_flashmap_dumpconf,
252 .ioctl = g_flashmap_ioctl,
253 .ctlreq = g_flashmap_config,
256 DECLARE_GEOM_CLASS(g_flashmap_class, g_flashmap);