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