2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Sam Leffler, Errno Consulting
5 * Copyright (c) 2012-2013, SRI International
8 * Portions of this software were developed by SRI International and the
9 * University of Cambridge Computer Laboratory under DARPA/AFRL contract
10 * (FA8750-10-C-0237) ("CTSRD"), as part of the DARPA CRASH research
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
45 #include <sys/mutex.h>
46 #include <sys/module.h>
48 #include <sys/sysctl.h>
49 #include <sys/taskqueue.h>
51 #include <machine/bus.h>
53 #include <dev/cfi/cfi_var.h>
55 #include <geom/geom.h>
56 #include <geom/geom_disk.h>
58 struct cfi_disk_softc {
59 struct cfi_softc *parent;
62 #define CFI_DISK_OPEN 0x0001
63 struct bio_queue_head bioq; /* bio queue */
64 struct mtx qlock; /* bioq lock */
65 struct taskqueue *tq; /* private task queue for i/o request */
66 struct task iotask; /* i/o processing */
69 #define CFI_DISK_SECSIZE 512
70 #define CFI_DISK_MAXIOSIZE 65536
72 static int cfi_disk_detach(device_t);
73 static int cfi_disk_open(struct disk *);
74 static int cfi_disk_close(struct disk *);
75 static void cfi_io_proc(void *, int);
76 static int cfi_disk_getattr(struct bio *);
77 static void cfi_disk_strategy(struct bio *);
78 static int cfi_disk_ioctl(struct disk *, u_long, void *, int, struct thread *);
81 cfi_disk_probe(device_t dev)
87 cfi_disk_attach(device_t dev)
89 struct cfi_disk_softc *sc = device_get_softc(dev);
91 sc->parent = device_get_softc(device_get_parent(dev));
92 /* validate interface width; assumed by other code */
93 if (sc->parent->sc_width != 1 &&
94 sc->parent->sc_width != 2 &&
95 sc->parent->sc_width != 4)
98 sc->disk = disk_alloc();
101 sc->disk->d_name = "cfid";
102 sc->disk->d_unit = device_get_unit(dev);
103 sc->disk->d_open = cfi_disk_open;
104 sc->disk->d_close = cfi_disk_close;
105 sc->disk->d_strategy = cfi_disk_strategy;
106 sc->disk->d_ioctl = cfi_disk_ioctl;
107 sc->disk->d_dump = NULL; /* NB: no dumps */
108 sc->disk->d_getattr = cfi_disk_getattr;
109 sc->disk->d_sectorsize = CFI_DISK_SECSIZE;
110 sc->disk->d_mediasize = sc->parent->sc_size;
111 sc->disk->d_maxsize = CFI_DISK_MAXIOSIZE;
112 /* NB: use stripesize to hold the erase/region size */
113 if (sc->parent->sc_regions) {
115 * Multiple regions, use the last one. This is a
116 * total hack as it's (presently) used only by
117 * geom_redboot to locate the FIS directory which
118 * lies at the start of the last erase region.
120 sc->disk->d_stripesize =
121 sc->parent->sc_region[sc->parent->sc_regions-1].r_blksz;
123 sc->disk->d_stripesize = sc->disk->d_mediasize;
124 sc->disk->d_drv1 = sc;
125 disk_create(sc->disk, DISK_VERSION);
127 mtx_init(&sc->qlock, "CFID I/O lock", NULL, MTX_DEF);
128 bioq_init(&sc->bioq);
130 sc->tq = taskqueue_create("cfid_taskq", M_NOWAIT,
131 taskqueue_thread_enqueue, &sc->tq);
132 taskqueue_start_threads(&sc->tq, 1, PI_DISK, "cfid taskq");
134 TASK_INIT(&sc->iotask, 0, cfi_io_proc, sc);
140 cfi_disk_detach(device_t dev)
142 struct cfi_disk_softc *sc = device_get_softc(dev);
144 if (sc->flags & CFI_DISK_OPEN)
146 taskqueue_free(sc->tq);
148 disk_destroy(sc->disk);
149 mtx_destroy(&sc->qlock);
154 cfi_disk_open(struct disk *dp)
156 struct cfi_disk_softc *sc = dp->d_drv1;
158 /* XXX no interlock with /dev/cfi */
159 sc->flags |= CFI_DISK_OPEN;
164 cfi_disk_close(struct disk *dp)
166 struct cfi_disk_softc *sc = dp->d_drv1;
168 sc->flags &= ~CFI_DISK_OPEN;
173 cfi_disk_read(struct cfi_softc *sc, struct bio *bp)
177 KASSERT(sc->sc_width == 1 || sc->sc_width == 2 || sc->sc_width == 4,
178 ("sc_width %d", sc->sc_width));
180 if (sc->sc_writing) {
181 bp->bio_error = cfi_block_finish(sc);
183 bp->bio_flags |= BIO_ERROR;
187 if (bp->bio_offset > sc->sc_size) {
188 bp->bio_flags |= BIO_ERROR;
192 resid = bp->bio_bcount;
193 if (sc->sc_width == 1) {
194 uint8_t *dp = (uint8_t *)bp->bio_data;
195 while (resid > 0 && bp->bio_offset < sc->sc_size) {
196 *dp++ = cfi_read_raw(sc, bp->bio_offset);
197 bp->bio_offset += 1, resid -= 1;
199 } else if (sc->sc_width == 2) {
200 uint16_t *dp = (uint16_t *)bp->bio_data;
201 while (resid > 0 && bp->bio_offset < sc->sc_size) {
202 *dp++ = cfi_read_raw(sc, bp->bio_offset);
203 bp->bio_offset += 2, resid -= 2;
206 uint32_t *dp = (uint32_t *)bp->bio_data;
207 while (resid > 0 && bp->bio_offset < sc->sc_size) {
208 *dp++ = cfi_read_raw(sc, bp->bio_offset);
209 bp->bio_offset += 4, resid -= 4;
212 bp->bio_resid = resid;
218 cfi_disk_write(struct cfi_softc *sc, struct bio *bp)
223 KASSERT(sc->sc_width == 1 || sc->sc_width == 2 || sc->sc_width == 4,
224 ("sc_width %d", sc->sc_width));
226 if (bp->bio_offset > sc->sc_size) {
227 bp->bio_flags |= BIO_ERROR;
231 resid = bp->bio_bcount;
234 * Finish the current block if we're about to write
235 * to a different block.
237 if (sc->sc_writing) {
238 top = sc->sc_wrofs + sc->sc_wrbufsz;
239 if (bp->bio_offset < sc->sc_wrofs ||
240 bp->bio_offset >= top)
241 cfi_block_finish(sc);
244 /* Start writing to a (new) block if applicable. */
245 if (!sc->sc_writing) {
246 bp->bio_error = cfi_block_start(sc, bp->bio_offset);
248 bp->bio_flags |= BIO_ERROR;
253 top = sc->sc_wrofs + sc->sc_wrbufsz;
255 sc->sc_wrbuf + bp->bio_offset - sc->sc_wrofs,
256 MIN(top - bp->bio_offset, resid));
257 resid -= MIN(top - bp->bio_offset, resid);
259 bp->bio_resid = resid;
265 cfi_io_proc(void *arg, int pending)
267 struct cfi_disk_softc *sc = arg;
268 struct cfi_softc *cfi = sc->parent;
272 mtx_lock(&sc->qlock);
273 bp = bioq_takefirst(&sc->bioq);
274 mtx_unlock(&sc->qlock);
278 switch (bp->bio_cmd) {
280 cfi_disk_read(cfi, bp);
283 cfi_disk_write(cfi, bp);
290 cfi_disk_getattr(struct bio *bp)
292 struct cfi_disk_softc *dsc;
293 struct cfi_softc *sc;
296 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
299 dsc = bp->bio_disk->d_drv1;
303 if (strcmp(bp->bio_attribute, "CFI::device") == 0) {
304 if (bp->bio_length != sizeof(dev))
306 bcopy(&dev, bp->bio_data, sizeof(dev));
314 cfi_disk_strategy(struct bio *bp)
316 struct cfi_disk_softc *sc = bp->bio_disk->d_drv1;
320 if (bp->bio_bcount == 0) {
321 bp->bio_resid = bp->bio_bcount;
325 switch (bp->bio_cmd) {
328 mtx_lock(&sc->qlock);
329 /* no value in sorting requests? */
330 bioq_insert_tail(&sc->bioq, bp);
331 mtx_unlock(&sc->qlock);
332 taskqueue_enqueue(sc->tq, &sc->iotask);
337 bp->bio_flags |= BIO_ERROR;
338 bp->bio_error = EINVAL;
343 cfi_disk_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
349 static device_method_t cfi_disk_methods[] = {
350 DEVMETHOD(device_probe, cfi_disk_probe),
351 DEVMETHOD(device_attach, cfi_disk_attach),
352 DEVMETHOD(device_detach, cfi_disk_detach),
356 static driver_t cfi_disk_driver = {
359 sizeof(struct cfi_disk_softc),
361 DRIVER_MODULE(cfid, cfi, cfi_disk_driver, cfi_diskclass, 0, NULL);