2 * Copyright (c) 2009 Sam Leffler, Errno Consulting
3 * Copyright (c) 2012-2013, SRI International
6 * Portions of this software were developed by SRI International and the
7 * University of Cambridge Computer Laboratory under DARPA/AFRL contract
8 * (FA8750-10-C-0237) ("CTSRD"), as part of the DARPA CRASH research
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
43 #include <sys/mutex.h>
44 #include <sys/module.h>
46 #include <sys/sysctl.h>
47 #include <sys/taskqueue.h>
49 #include <machine/bus.h>
51 #include <dev/cfi/cfi_var.h>
53 #include <geom/geom.h>
54 #include <geom/geom_disk.h>
56 struct cfi_disk_softc {
57 struct cfi_softc *parent;
60 #define CFI_DISK_OPEN 0x0001
61 struct bio_queue_head bioq; /* bio queue */
62 struct mtx qlock; /* bioq lock */
63 struct taskqueue *tq; /* private task queue for i/o request */
64 struct task iotask; /* i/o processing */
67 #define CFI_DISK_SECSIZE 512
68 #define CFI_DISK_MAXIOSIZE 65536
70 static int cfi_disk_detach(device_t);
71 static int cfi_disk_open(struct disk *);
72 static int cfi_disk_close(struct disk *);
73 static void cfi_io_proc(void *, int);
74 static int cfi_disk_getattr(struct bio *);
75 static void cfi_disk_strategy(struct bio *);
76 static int cfi_disk_ioctl(struct disk *, u_long, void *, int, struct thread *);
79 cfi_disk_probe(device_t dev)
85 cfi_disk_attach(device_t dev)
87 struct cfi_disk_softc *sc = device_get_softc(dev);
89 sc->parent = device_get_softc(device_get_parent(dev));
90 /* validate interface width; assumed by other code */
91 if (sc->parent->sc_width != 1 &&
92 sc->parent->sc_width != 2 &&
93 sc->parent->sc_width != 4)
96 sc->disk = disk_alloc();
99 sc->disk->d_name = "cfid";
100 sc->disk->d_unit = device_get_unit(dev);
101 sc->disk->d_open = cfi_disk_open;
102 sc->disk->d_close = cfi_disk_close;
103 sc->disk->d_strategy = cfi_disk_strategy;
104 sc->disk->d_ioctl = cfi_disk_ioctl;
105 sc->disk->d_dump = NULL; /* NB: no dumps */
106 sc->disk->d_getattr = cfi_disk_getattr;
107 sc->disk->d_sectorsize = CFI_DISK_SECSIZE;
108 sc->disk->d_mediasize = sc->parent->sc_size;
109 sc->disk->d_maxsize = CFI_DISK_MAXIOSIZE;
110 /* NB: use stripesize to hold the erase/region size */
111 if (sc->parent->sc_regions) {
113 * Multiple regions, use the last one. This is a
114 * total hack as it's (presently) used only by
115 * geom_redboot to locate the FIS directory which
116 * lies at the start of the last erase region.
118 sc->disk->d_stripesize =
119 sc->parent->sc_region[sc->parent->sc_regions-1].r_blksz;
121 sc->disk->d_stripesize = sc->disk->d_mediasize;
122 sc->disk->d_drv1 = sc;
123 disk_create(sc->disk, DISK_VERSION);
125 mtx_init(&sc->qlock, "CFID I/O lock", NULL, MTX_DEF);
126 bioq_init(&sc->bioq);
128 sc->tq = taskqueue_create("cfid_taskq", M_NOWAIT,
129 taskqueue_thread_enqueue, &sc->tq);
130 taskqueue_start_threads(&sc->tq, 1, PI_DISK, "cfid taskq");
132 TASK_INIT(&sc->iotask, 0, cfi_io_proc, sc);
138 cfi_disk_detach(device_t dev)
140 struct cfi_disk_softc *sc = device_get_softc(dev);
142 if (sc->flags & CFI_DISK_OPEN)
144 taskqueue_free(sc->tq);
146 disk_destroy(sc->disk);
147 mtx_destroy(&sc->qlock);
152 cfi_disk_open(struct disk *dp)
154 struct cfi_disk_softc *sc = dp->d_drv1;
156 /* XXX no interlock with /dev/cfi */
157 sc->flags |= CFI_DISK_OPEN;
162 cfi_disk_close(struct disk *dp)
164 struct cfi_disk_softc *sc = dp->d_drv1;
166 sc->flags &= ~CFI_DISK_OPEN;
171 cfi_disk_read(struct cfi_softc *sc, struct bio *bp)
175 KASSERT(sc->sc_width == 1 || sc->sc_width == 2 || sc->sc_width == 4,
176 ("sc_width %d", sc->sc_width));
178 if (sc->sc_writing) {
179 bp->bio_error = cfi_block_finish(sc);
181 bp->bio_flags |= BIO_ERROR;
185 if (bp->bio_offset > sc->sc_size) {
186 bp->bio_flags |= BIO_ERROR;
190 resid = bp->bio_bcount;
191 if (sc->sc_width == 1) {
192 uint8_t *dp = (uint8_t *)bp->bio_data;
193 while (resid > 0 && bp->bio_offset < sc->sc_size) {
194 *dp++ = cfi_read_raw(sc, bp->bio_offset);
195 bp->bio_offset += 1, resid -= 1;
197 } else if (sc->sc_width == 2) {
198 uint16_t *dp = (uint16_t *)bp->bio_data;
199 while (resid > 0 && bp->bio_offset < sc->sc_size) {
200 *dp++ = cfi_read_raw(sc, bp->bio_offset);
201 bp->bio_offset += 2, resid -= 2;
204 uint32_t *dp = (uint32_t *)bp->bio_data;
205 while (resid > 0 && bp->bio_offset < sc->sc_size) {
206 *dp++ = cfi_read_raw(sc, bp->bio_offset);
207 bp->bio_offset += 4, resid -= 4;
210 bp->bio_resid = resid;
216 cfi_disk_write(struct cfi_softc *sc, struct bio *bp)
221 KASSERT(sc->sc_width == 1 || sc->sc_width == 2 || sc->sc_width == 4,
222 ("sc_width %d", sc->sc_width));
224 if (bp->bio_offset > sc->sc_size) {
225 bp->bio_flags |= BIO_ERROR;
229 resid = bp->bio_bcount;
232 * Finish the current block if we're about to write
233 * to a different block.
235 if (sc->sc_writing) {
236 top = sc->sc_wrofs + sc->sc_wrbufsz;
237 if (bp->bio_offset < sc->sc_wrofs ||
238 bp->bio_offset >= top)
239 cfi_block_finish(sc);
242 /* Start writing to a (new) block if applicable. */
243 if (!sc->sc_writing) {
244 bp->bio_error = cfi_block_start(sc, bp->bio_offset);
246 bp->bio_flags |= BIO_ERROR;
251 top = sc->sc_wrofs + sc->sc_wrbufsz;
253 sc->sc_wrbuf + bp->bio_offset - sc->sc_wrofs,
254 MIN(top - bp->bio_offset, resid));
255 resid -= MIN(top - bp->bio_offset, resid);
257 bp->bio_resid = resid;
263 cfi_io_proc(void *arg, int pending)
265 struct cfi_disk_softc *sc = arg;
266 struct cfi_softc *cfi = sc->parent;
270 mtx_lock(&sc->qlock);
271 bp = bioq_takefirst(&sc->bioq);
272 mtx_unlock(&sc->qlock);
276 switch (bp->bio_cmd) {
278 cfi_disk_read(cfi, bp);
281 cfi_disk_write(cfi, bp);
288 cfi_disk_getattr(struct bio *bp)
290 struct cfi_disk_softc *dsc;
291 struct cfi_softc *sc;
294 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
297 dsc = bp->bio_disk->d_drv1;
301 if (strcmp(bp->bio_attribute, "CFI::device") == 0) {
302 if (bp->bio_length != sizeof(dev))
304 bcopy(&dev, bp->bio_data, sizeof(dev));
312 cfi_disk_strategy(struct bio *bp)
314 struct cfi_disk_softc *sc = bp->bio_disk->d_drv1;
318 if (bp->bio_bcount == 0) {
319 bp->bio_resid = bp->bio_bcount;
323 switch (bp->bio_cmd) {
326 mtx_lock(&sc->qlock);
327 /* no value in sorting requests? */
328 bioq_insert_tail(&sc->bioq, bp);
329 mtx_unlock(&sc->qlock);
330 taskqueue_enqueue(sc->tq, &sc->iotask);
335 bp->bio_flags |= BIO_ERROR;
336 bp->bio_error = EINVAL;
341 cfi_disk_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
347 static device_method_t cfi_disk_methods[] = {
348 DEVMETHOD(device_probe, cfi_disk_probe),
349 DEVMETHOD(device_attach, cfi_disk_attach),
350 DEVMETHOD(device_detach, cfi_disk_detach),
354 static driver_t cfi_disk_driver = {
357 sizeof(struct cfi_disk_softc),
359 DRIVER_MODULE(cfid, cfi, cfi_disk_driver, cfi_diskclass, 0, NULL);