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/endian.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/fcntl.h> 36 #include <sys/malloc.h> 37 #include <sys/bio.h> 38 #include <sys/bus.h> 39 #include <sys/lock.h> 40 #include <sys/mutex.h> 41 #include <sys/slicer.h> 42 43 #include <geom/geom.h> 44 #include <geom/geom_slice.h> 45 #include <geom/geom_disk.h> 46 #include <dev/nand/nand_dev.h> 47 48 #define FLASHMAP_CLASS_NAME "Flashmap" 49 50 struct g_flashmap_slice { 51 off_t sl_start; 52 off_t sl_end; 53 const char *sl_name; 54 55 STAILQ_ENTRY(g_flashmap_slice) sl_link; 56 }; 57 58 STAILQ_HEAD(g_flashmap_head, g_flashmap_slice); 59 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 *, 71 const char *); 72 static int g_flashmap_load(device_t, struct g_flashmap_head *); 73 static int (*flash_fill_slices)(device_t, struct flash_slice *, int *) = 74 fdt_flash_fill_slices; 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, "%ss.%s", 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_start(struct bio *bp) 129 { 130 131 return (0); 132 } 133 134 static void 135 g_flashmap_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 136 struct g_consumer *cp __unused, struct g_provider *pp) 137 { 138 struct g_slicer *gsp; 139 140 gsp = gp->softc; 141 g_slice_dumpconf(sb, indent, gp, cp, pp); 142 } 143 144 static int 145 g_flashmap_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, 146 struct thread *td) 147 { 148 struct g_consumer *cp; 149 struct g_geom *gp; 150 151 if (cmd != NAND_IO_GET_CHIP_PARAM) 152 return (ENOIOCTL); 153 154 cp = LIST_FIRST(&pp->geom->consumer); 155 if (cp == NULL) 156 return (ENOIOCTL); 157 gp = cp->provider->geom; 158 if (gp->ioctl == NULL) 159 return (ENOIOCTL); 160 161 return (gp->ioctl(cp->provider, cmd, data, fflag, td)); 162 } 163 164 165 static struct g_geom * 166 g_flashmap_taste(struct g_class *mp, struct g_provider *pp, int flags) 167 { 168 struct g_geom *gp = NULL; 169 struct g_consumer *cp; 170 struct g_flashmap_head head; 171 struct g_flashmap_slice *slice, *slice_temp; 172 device_t dev; 173 int nslices, size; 174 175 g_trace(G_T_TOPOLOGY, "flashmap_taste(%s,%s)", mp->name, pp->name); 176 g_topology_assert(); 177 178 if (flags == G_TF_NORMAL && 179 strcmp(pp->geom->class->name, G_DISK_CLASS_NAME) != 0) 180 return (NULL); 181 182 gp = g_slice_new(mp, FLASH_SLICES_MAX_NUM, pp, &cp, NULL, 0, 183 g_flashmap_start); 184 if (gp == NULL) 185 return (NULL); 186 187 STAILQ_INIT(&head); 188 189 do { 190 size = sizeof(device_t); 191 if (g_io_getattr("NAND::device", cp, &size, &dev)) { 192 size = sizeof(device_t); 193 if (g_io_getattr("CFI::device", cp, &size, &dev)) 194 break; 195 } 196 197 nslices = g_flashmap_load(dev, &head); 198 if (nslices == 0) 199 break; 200 201 g_flashmap_modify(gp, cp->provider->name, 202 cp->provider->sectorsize, &head); 203 } while (0); 204 205 g_access(cp, -1, 0, 0); 206 207 STAILQ_FOREACH_SAFE(slice, &head, sl_link, slice_temp) { 208 free(slice, M_FLASHMAP); 209 } 210 211 if (LIST_EMPTY(&gp->provider)) { 212 g_slice_spoiled(cp); 213 return (NULL); 214 } 215 return (gp); 216 } 217 218 static void 219 g_flashmap_config(struct gctl_req *req, struct g_class *mp, const char *verb) 220 { 221 222 gctl_error(req, "unknown config verb"); 223 } 224 225 static int 226 g_flashmap_load(device_t dev, struct g_flashmap_head *head) 227 { 228 struct flash_slice *slices; 229 struct g_flashmap_slice *slice; 230 uint32_t i, buf_size; 231 int nslices = 0; 232 233 buf_size = sizeof(struct flash_slice) * FLASH_SLICES_MAX_NUM; 234 slices = malloc(buf_size, M_FLASHMAP, M_WAITOK | M_ZERO); 235 if (flash_fill_slices && 236 flash_fill_slices(dev, slices, &nslices) == 0) { 237 for (i = 0; i < nslices; i++) { 238 slice = malloc(sizeof(struct g_flashmap_slice), 239 M_FLASHMAP, M_WAITOK); 240 241 slice->sl_name = slices[i].label; 242 slice->sl_start = slices[i].base; 243 slice->sl_end = slices[i].base + slices[i].size - 1; 244 245 STAILQ_INSERT_TAIL(head, slice, sl_link); 246 } 247 } 248 249 free(slices, M_FLASHMAP); 250 return (nslices); 251 } 252 253 void flash_register_slicer(int (*slicer)(device_t, struct flash_slice *, int *)) 254 { 255 256 flash_fill_slices = slicer; 257 } 258 259 static struct g_class g_flashmap_class = { 260 .name = FLASHMAP_CLASS_NAME, 261 .version = G_VERSION, 262 .taste = g_flashmap_taste, 263 .dumpconf = g_flashmap_dumpconf, 264 .ioctl = g_flashmap_ioctl, 265 .ctlreq = g_flashmap_config, 266 }; 267 268 DECLARE_GEOM_CLASS(g_flashmap_class, g_flashmap); 269