xref: /freebsd/sys/geom/geom_flashmap.c (revision d93a896ef95946b0bf1219866fcb324b78543444)
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_slice.h>
41 #include <geom/geom_disk.h>
42 
43 #include <dev/nand/nand_dev.h>
44 
45 #define	FLASHMAP_CLASS_NAME "Flashmap"
46 
47 struct g_flashmap_slice {
48 	off_t		sl_start;
49 	off_t		sl_end;
50 	const char	*sl_name;
51 
52 	STAILQ_ENTRY(g_flashmap_slice) sl_link;
53 };
54 
55 STAILQ_HEAD(g_flashmap_head, g_flashmap_slice);
56 
57 static struct {
58 	const char	*type;
59 	flash_slicer_t	slicer;
60 } g_flashmap_slicers[] = {
61 	{ "NAND::device",	NULL },
62 	{ "CFI::device",	NULL },
63 	{ "SPI::device",	NULL },
64 	{ "MMC::device",	NULL }
65 };
66 
67 static g_ioctl_t g_flashmap_ioctl;
68 static g_taste_t g_flashmap_taste;
69 
70 static int g_flashmap_load(device_t dev, struct g_provider *pp,
71     flash_slicer_t slicer, struct g_flashmap_head *head);
72 static int g_flashmap_modify(struct g_geom *gp, const char *devname,
73     int secsize, struct g_flashmap_head *slices);
74 static void g_flashmap_print(struct g_flashmap_slice *slice);
75 
76 MALLOC_DECLARE(M_FLASHMAP);
77 MALLOC_DEFINE(M_FLASHMAP, "geom_flashmap", "GEOM flash memory slicer class");
78 
79 static void
80 g_flashmap_print(struct g_flashmap_slice *slice)
81 {
82 
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);
86 }
87 
88 static int
89 g_flashmap_modify(struct g_geom *gp, const char *devname, int secsize,
90     struct g_flashmap_head *slices)
91 {
92 	struct g_flashmap_slice *slice;
93 	int i, error;
94 
95 	g_topology_assert();
96 
97 	i = 0;
98 	STAILQ_FOREACH(slice, slices, sl_link) {
99 		if (bootverbose) {
100 			printf("%s: slice ", devname);
101 			g_flashmap_print(slice);
102 		}
103 
104 		error = g_slice_config(gp, i++, G_SLICE_CONFIG_CHECK,
105 		    slice->sl_start,
106 		    slice->sl_end - slice->sl_start + 1,
107 		    secsize, FLASH_SLICES_FMT, gp->name, slice->sl_name);
108 
109 		if (error)
110 			return (error);
111 	}
112 
113 	i = 0;
114 	STAILQ_FOREACH(slice, slices, sl_link) {
115 		error = g_slice_config(gp, i++, G_SLICE_CONFIG_SET,
116 		    slice->sl_start,
117 		    slice->sl_end - slice->sl_start + 1,
118 		    secsize, "%ss.%s", gp->name, slice->sl_name);
119 
120 		if (error)
121 			return (error);
122 	}
123 
124 	return (0);
125 }
126 
127 static int
128 g_flashmap_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag,
129     struct thread *td)
130 {
131 	struct g_consumer *cp;
132 	struct g_geom *gp;
133 
134 	if (cmd != NAND_IO_GET_CHIP_PARAM)
135 		return (ENOIOCTL);
136 
137 	cp = LIST_FIRST(&pp->geom->consumer);
138 	if (cp == NULL)
139 		return (ENOIOCTL);
140 	gp = cp->provider->geom;
141 	if (gp->ioctl == NULL)
142 		return (ENOIOCTL);
143 
144 	return (gp->ioctl(cp->provider, cmd, data, fflag, td));
145 }
146 
147 static struct g_geom *
148 g_flashmap_taste(struct g_class *mp, struct g_provider *pp, int flags)
149 {
150 	struct g_geom *gp;
151 	struct g_consumer *cp;
152 	struct g_flashmap_head head;
153 	struct g_flashmap_slice *slice, *slice_temp;
154 	flash_slicer_t slicer;
155 	device_t dev;
156 	int i, size;
157 
158 	g_trace(G_T_TOPOLOGY, "flashmap_taste(%s,%s)", mp->name, pp->name);
159 	g_topology_assert();
160 
161 	if (flags == G_TF_NORMAL &&
162 	    strcmp(pp->geom->class->name, G_DISK_CLASS_NAME) != 0)
163 		return (NULL);
164 
165 	gp = g_slice_new(mp, FLASH_SLICES_MAX_NUM, pp, &cp, NULL, 0, NULL);
166 	if (gp == NULL)
167 		return (NULL);
168 
169 	STAILQ_INIT(&head);
170 
171 	do {
172 		slicer = NULL;
173 		for (i = 0; i < nitems(g_flashmap_slicers); i++) {
174 			size = sizeof(device_t);
175 			if (g_io_getattr(g_flashmap_slicers[i].type, cp,
176 			    &size, &dev) == 0) {
177 				slicer = g_flashmap_slicers[i].slicer;
178 				break;
179 			}
180 		}
181 		if (slicer == NULL)
182 			break;
183 
184 		if (g_flashmap_load(dev, pp, slicer, &head) == 0)
185 			break;
186 
187 		g_flashmap_modify(gp, cp->provider->name,
188 		    cp->provider->sectorsize, &head);
189 	} while (0);
190 
191 	g_access(cp, -1, 0, 0);
192 
193 	STAILQ_FOREACH_SAFE(slice, &head, sl_link, slice_temp)
194 		free(slice, M_FLASHMAP);
195 
196 	if (LIST_EMPTY(&gp->provider)) {
197 		g_slice_spoiled(cp);
198 		return (NULL);
199 	}
200 	return (gp);
201 }
202 
203 static int
204 g_flashmap_load(device_t dev, struct g_provider *pp, flash_slicer_t slicer,
205     struct g_flashmap_head *head)
206 {
207 	struct flash_slice *slices;
208 	struct g_flashmap_slice *slice;
209 	int i, nslices = 0;
210 
211 	slices = malloc(sizeof(struct flash_slice) * FLASH_SLICES_MAX_NUM,
212 	    M_FLASHMAP, M_WAITOK | M_ZERO);
213 	if (slicer(dev, pp->name, slices, &nslices) == 0) {
214 		for (i = 0; i < nslices; i++) {
215 			slice = malloc(sizeof(struct g_flashmap_slice),
216 			    M_FLASHMAP, M_WAITOK);
217 
218 			slice->sl_name = slices[i].label;
219 			slice->sl_start = slices[i].base;
220 			slice->sl_end = slices[i].base + slices[i].size - 1;
221 
222 			STAILQ_INSERT_TAIL(head, slice, sl_link);
223 		}
224 	}
225 
226 	free(slices, M_FLASHMAP);
227 	return (nslices);
228 }
229 
230 void flash_register_slicer(flash_slicer_t slicer, u_int type, bool force)
231 {
232 
233 	g_topology_lock();
234 	if (g_flashmap_slicers[type].slicer == NULL || force == TRUE)
235 		g_flashmap_slicers[type].slicer = slicer;
236 	g_topology_unlock();
237 }
238 
239 static struct g_class g_flashmap_class = {
240 	.name = FLASHMAP_CLASS_NAME,
241 	.version = G_VERSION,
242 	.taste = g_flashmap_taste,
243 	.ioctl = g_flashmap_ioctl,
244 };
245 
246 DECLARE_GEOM_CLASS(g_flashmap_class, g_flashmap);
247 MODULE_VERSION(g_flashmap, 0);
248