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