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/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
36 #include <sys/mutex.h>
38 #include <sys/slicer.h>
40 #include <geom/geom.h>
41 #include <geom/geom_slice.h>
42 #include <geom/geom_disk.h>
44 #include <dev/nand/nand_dev.h>
46 #define FLASHMAP_CLASS_NAME "Flashmap"
48 struct g_flashmap_slice {
53 STAILQ_ENTRY(g_flashmap_slice) sl_link;
56 STAILQ_HEAD(g_flashmap_head, g_flashmap_slice);
60 flash_slicer_t slicer;
61 } g_flashmap_slicers[] = {
62 { "NAND::device", NULL },
63 { "CFI::device", NULL },
64 { "SPI::device", NULL },
65 { "MMC::device", NULL }
68 static g_ioctl_t g_flashmap_ioctl;
69 static g_taste_t g_flashmap_taste;
71 static int g_flashmap_load(device_t dev, struct g_provider *pp,
72 flash_slicer_t slicer, struct g_flashmap_head *head);
73 static int g_flashmap_modify(struct g_geom *gp, const char *devname,
74 int secsize, struct g_flashmap_head *slices);
75 static void g_flashmap_print(struct g_flashmap_slice *slice);
77 MALLOC_DECLARE(M_FLASHMAP);
78 MALLOC_DEFINE(M_FLASHMAP, "geom_flashmap", "GEOM flash memory slicer class");
81 g_flashmap_print(struct g_flashmap_slice *slice)
84 printf("%08jx-%08jx: %s (%juKB)\n", (uintmax_t)slice->sl_start,
85 (uintmax_t)slice->sl_end, slice->sl_name,
86 (uintmax_t)(slice->sl_end - slice->sl_start) / 1024);
90 g_flashmap_modify(struct g_geom *gp, const char *devname, int secsize,
91 struct g_flashmap_head *slices)
93 struct g_flashmap_slice *slice;
99 STAILQ_FOREACH(slice, slices, sl_link) {
101 printf("%s: slice ", devname);
102 g_flashmap_print(slice);
105 error = g_slice_config(gp, i++, G_SLICE_CONFIG_CHECK,
107 slice->sl_end - slice->sl_start + 1,
108 secsize, FLASH_SLICES_FMT, gp->name, slice->sl_name);
115 STAILQ_FOREACH(slice, slices, sl_link) {
116 error = g_slice_config(gp, i++, G_SLICE_CONFIG_SET,
118 slice->sl_end - slice->sl_start + 1,
119 secsize, "%ss.%s", gp->name, slice->sl_name);
129 g_flashmap_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag,
132 struct g_consumer *cp;
135 if (cmd != NAND_IO_GET_CHIP_PARAM)
138 cp = LIST_FIRST(&pp->geom->consumer);
141 gp = cp->provider->geom;
142 if (gp->ioctl == NULL)
145 return (gp->ioctl(cp->provider, cmd, data, fflag, td));
148 static struct g_geom *
149 g_flashmap_taste(struct g_class *mp, struct g_provider *pp, int flags)
152 struct g_consumer *cp;
153 struct g_flashmap_head head;
154 struct g_flashmap_slice *slice, *slice_temp;
155 flash_slicer_t slicer;
159 g_trace(G_T_TOPOLOGY, "flashmap_taste(%s,%s)", mp->name, pp->name);
162 if (flags == G_TF_NORMAL &&
163 strcmp(pp->geom->class->name, G_DISK_CLASS_NAME) != 0)
166 gp = g_slice_new(mp, FLASH_SLICES_MAX_NUM, pp, &cp, NULL, 0, NULL);
174 for (i = 0; i < nitems(g_flashmap_slicers); i++) {
175 size = sizeof(device_t);
176 if (g_io_getattr(g_flashmap_slicers[i].type, cp,
178 slicer = g_flashmap_slicers[i].slicer;
185 if (g_flashmap_load(dev, pp, slicer, &head) == 0)
188 g_flashmap_modify(gp, cp->provider->name,
189 cp->provider->sectorsize, &head);
192 g_access(cp, -1, 0, 0);
194 STAILQ_FOREACH_SAFE(slice, &head, sl_link, slice_temp)
195 free(slice, M_FLASHMAP);
197 if (LIST_EMPTY(&gp->provider)) {
205 g_flashmap_load(device_t dev, struct g_provider *pp, flash_slicer_t slicer,
206 struct g_flashmap_head *head)
208 struct flash_slice *slices;
209 struct g_flashmap_slice *slice;
212 slices = malloc(sizeof(struct flash_slice) * FLASH_SLICES_MAX_NUM,
213 M_FLASHMAP, M_WAITOK | M_ZERO);
214 if (slicer(dev, pp->name, slices, &nslices) == 0) {
215 for (i = 0; i < nslices; i++) {
216 slice = malloc(sizeof(struct g_flashmap_slice),
217 M_FLASHMAP, M_WAITOK);
219 slice->sl_name = slices[i].label;
220 slice->sl_start = slices[i].base;
221 slice->sl_end = slices[i].base + slices[i].size - 1;
223 STAILQ_INSERT_TAIL(head, slice, sl_link);
227 free(slices, M_FLASHMAP);
231 void flash_register_slicer(flash_slicer_t slicer, u_int type, bool force)
236 if (g_flashmap_slicers[type].slicer == NULL || force == TRUE)
237 g_flashmap_slicers[type].slicer = slicer;
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,
249 DECLARE_GEOM_CLASS(g_flashmap_class, g_flashmap);
250 MODULE_VERSION(g_flashmap, 0);