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 size = sizeof(device_t); 195 if (g_io_getattr("SPI::device", cp, &size, 196 &dev)) 197 break; 198 } 199 } 200 201 nslices = g_flashmap_load(dev, &head); 202 if (nslices == 0) 203 break; 204 205 g_flashmap_modify(gp, cp->provider->name, 206 cp->provider->sectorsize, &head); 207 } while (0); 208 209 g_access(cp, -1, 0, 0); 210 211 STAILQ_FOREACH_SAFE(slice, &head, sl_link, slice_temp) { 212 free(slice, M_FLASHMAP); 213 } 214 215 if (LIST_EMPTY(&gp->provider)) { 216 g_slice_spoiled(cp); 217 return (NULL); 218 } 219 return (gp); 220 } 221 222 static void 223 g_flashmap_config(struct gctl_req *req, struct g_class *mp, const char *verb) 224 { 225 226 gctl_error(req, "unknown config verb"); 227 } 228 229 static int 230 g_flashmap_load(device_t dev, struct g_flashmap_head *head) 231 { 232 struct flash_slice *slices; 233 struct g_flashmap_slice *slice; 234 uint32_t i, buf_size; 235 int nslices = 0; 236 237 buf_size = sizeof(struct flash_slice) * FLASH_SLICES_MAX_NUM; 238 slices = malloc(buf_size, M_FLASHMAP, M_WAITOK | M_ZERO); 239 if (flash_fill_slices && 240 flash_fill_slices(dev, slices, &nslices) == 0) { 241 for (i = 0; i < nslices; i++) { 242 slice = malloc(sizeof(struct g_flashmap_slice), 243 M_FLASHMAP, M_WAITOK); 244 245 slice->sl_name = slices[i].label; 246 slice->sl_start = slices[i].base; 247 slice->sl_end = slices[i].base + slices[i].size - 1; 248 249 STAILQ_INSERT_TAIL(head, slice, sl_link); 250 } 251 } 252 253 free(slices, M_FLASHMAP); 254 return (nslices); 255 } 256 257 void flash_register_slicer(int (*slicer)(device_t, struct flash_slice *, int *)) 258 { 259 260 flash_fill_slices = slicer; 261 } 262 263 static struct g_class g_flashmap_class = { 264 .name = FLASHMAP_CLASS_NAME, 265 .version = G_VERSION, 266 .taste = g_flashmap_taste, 267 .dumpconf = g_flashmap_dumpconf, 268 .ioctl = g_flashmap_ioctl, 269 .ctlreq = g_flashmap_config, 270 }; 271 272 DECLARE_GEOM_CLASS(g_flashmap_class, g_flashmap); 273