2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
35 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/module.h>
45 #include <sys/sysctl.h>
46 #include <sys/taskqueue.h>
48 #include <machine/bus.h>
50 #include <dev/cfi/cfi_var.h>
52 #include <geom/geom.h>
53 #include <geom/geom_disk.h>
55 struct cfi_disk_softc {
56 struct cfi_softc *parent;
59 #define CFI_DISK_OPEN 0x0001
60 struct bio_queue_head bioq; /* bio queue */
61 struct mtx qlock; /* bioq lock */
62 struct taskqueue *tq; /* private task queue for i/o request */
63 struct task iotask; /* i/o processing */
66 #define CFI_DISK_SECSIZE 512
67 #define CFI_DISK_MAXIOSIZE 65536
69 static int cfi_disk_detach(device_t);
70 static int cfi_disk_open(struct disk *);
71 static int cfi_disk_close(struct disk *);
72 static void cfi_io_proc(void *, int);
73 static int cfi_disk_getattr(struct bio *);
74 static void cfi_disk_strategy(struct bio *);
75 static int cfi_disk_ioctl(struct disk *, u_long, void *, int, struct thread *);
78 cfi_disk_probe(device_t dev)
84 cfi_disk_attach(device_t dev)
86 struct cfi_disk_softc *sc = device_get_softc(dev);
88 sc->parent = device_get_softc(device_get_parent(dev));
89 /* validate interface width; assumed by other code */
90 if (sc->parent->sc_width != 1 &&
91 sc->parent->sc_width != 2 &&
92 sc->parent->sc_width != 4)
95 sc->disk = disk_alloc();
98 sc->disk->d_name = "cfid";
99 sc->disk->d_unit = device_get_unit(dev);
100 sc->disk->d_open = cfi_disk_open;
101 sc->disk->d_close = cfi_disk_close;
102 sc->disk->d_strategy = cfi_disk_strategy;
103 sc->disk->d_ioctl = cfi_disk_ioctl;
104 sc->disk->d_dump = NULL; /* NB: no dumps */
105 sc->disk->d_getattr = cfi_disk_getattr;
106 sc->disk->d_sectorsize = CFI_DISK_SECSIZE;
107 sc->disk->d_mediasize = sc->parent->sc_size;
108 sc->disk->d_maxsize = CFI_DISK_MAXIOSIZE;
109 /* NB: use stripesize to hold the erase/region size */
110 if (sc->parent->sc_regions) {
112 * Multiple regions, use the last one. This is a
113 * total hack as it's (presently) used only by
114 * geom_redboot to locate the FIS directory which
115 * lies at the start of the last erase region.
117 sc->disk->d_stripesize =
118 sc->parent->sc_region[sc->parent->sc_regions-1].r_blksz;
120 sc->disk->d_stripesize = sc->disk->d_mediasize;
121 sc->disk->d_drv1 = sc;
122 disk_create(sc->disk, DISK_VERSION);
124 mtx_init(&sc->qlock, "CFID I/O lock", NULL, MTX_DEF);
125 bioq_init(&sc->bioq);
127 sc->tq = taskqueue_create("cfid_taskq", M_NOWAIT,
128 taskqueue_thread_enqueue, &sc->tq);
129 taskqueue_start_threads(&sc->tq, 1, PI_DISK, "cfid taskq");
131 TASK_INIT(&sc->iotask, 0, cfi_io_proc, sc);
137 cfi_disk_detach(device_t dev)
139 struct cfi_disk_softc *sc = device_get_softc(dev);
141 if (sc->flags & CFI_DISK_OPEN)
143 taskqueue_free(sc->tq);
145 disk_destroy(sc->disk);
146 mtx_destroy(&sc->qlock);
151 cfi_disk_open(struct disk *dp)
153 struct cfi_disk_softc *sc = dp->d_drv1;
155 /* XXX no interlock with /dev/cfi */
156 sc->flags |= CFI_DISK_OPEN;
161 cfi_disk_close(struct disk *dp)
163 struct cfi_disk_softc *sc = dp->d_drv1;
165 sc->flags &= ~CFI_DISK_OPEN;
170 cfi_disk_read(struct cfi_softc *sc, struct bio *bp)
174 KASSERT(sc->sc_width == 1 || sc->sc_width == 2 || sc->sc_width == 4,
175 ("sc_width %d", sc->sc_width));
177 if (sc->sc_writing) {
178 bp->bio_error = cfi_block_finish(sc);
180 bp->bio_flags |= BIO_ERROR;
184 if (bp->bio_offset > sc->sc_size) {
185 bp->bio_flags |= BIO_ERROR;
189 resid = bp->bio_bcount;
190 if (sc->sc_width == 1) {
191 uint8_t *dp = (uint8_t *)bp->bio_data;
192 while (resid > 0 && bp->bio_offset < sc->sc_size) {
193 *dp++ = cfi_read_raw(sc, bp->bio_offset);
194 bp->bio_offset += 1, resid -= 1;
196 } else if (sc->sc_width == 2) {
197 uint16_t *dp = (uint16_t *)bp->bio_data;
198 while (resid > 0 && bp->bio_offset < sc->sc_size) {
199 *dp++ = cfi_read_raw(sc, bp->bio_offset);
200 bp->bio_offset += 2, resid -= 2;
203 uint32_t *dp = (uint32_t *)bp->bio_data;
204 while (resid > 0 && bp->bio_offset < sc->sc_size) {
205 *dp++ = cfi_read_raw(sc, bp->bio_offset);
206 bp->bio_offset += 4, resid -= 4;
209 bp->bio_resid = resid;
215 cfi_disk_write(struct cfi_softc *sc, struct bio *bp)
220 KASSERT(sc->sc_width == 1 || sc->sc_width == 2 || sc->sc_width == 4,
221 ("sc_width %d", sc->sc_width));
223 if (bp->bio_offset > sc->sc_size) {
224 bp->bio_flags |= BIO_ERROR;
228 resid = bp->bio_bcount;
231 * Finish the current block if we're about to write
232 * to a different block.
234 if (sc->sc_writing) {
235 top = sc->sc_wrofs + sc->sc_wrbufsz;
236 if (bp->bio_offset < sc->sc_wrofs ||
237 bp->bio_offset >= top)
238 cfi_block_finish(sc);
241 /* Start writing to a (new) block if applicable. */
242 if (!sc->sc_writing) {
243 bp->bio_error = cfi_block_start(sc, bp->bio_offset);
245 bp->bio_flags |= BIO_ERROR;
250 top = sc->sc_wrofs + sc->sc_wrbufsz;
252 sc->sc_wrbuf + bp->bio_offset - sc->sc_wrofs,
253 MIN(top - bp->bio_offset, resid));
254 resid -= MIN(top - bp->bio_offset, resid);
256 bp->bio_resid = resid;
262 cfi_io_proc(void *arg, int pending)
264 struct cfi_disk_softc *sc = arg;
265 struct cfi_softc *cfi = sc->parent;
269 mtx_lock(&sc->qlock);
270 bp = bioq_takefirst(&sc->bioq);
271 mtx_unlock(&sc->qlock);
275 switch (bp->bio_cmd) {
277 cfi_disk_read(cfi, bp);
280 cfi_disk_write(cfi, bp);
287 cfi_disk_getattr(struct bio *bp)
289 struct cfi_disk_softc *dsc;
290 struct cfi_softc *sc;
293 if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
296 dsc = bp->bio_disk->d_drv1;
300 if (strcmp(bp->bio_attribute, "CFI::device") == 0) {
301 if (bp->bio_length != sizeof(dev))
303 bcopy(&dev, bp->bio_data, sizeof(dev));
310 cfi_disk_strategy(struct bio *bp)
312 struct cfi_disk_softc *sc = bp->bio_disk->d_drv1;
315 biofinish(bp, NULL, EINVAL);
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);
333 biofinish(bp, NULL, EOPNOTSUPP);
339 cfi_disk_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
345 static device_method_t cfi_disk_methods[] = {
346 DEVMETHOD(device_probe, cfi_disk_probe),
347 DEVMETHOD(device_attach, cfi_disk_attach),
348 DEVMETHOD(device_detach, cfi_disk_detach),
351 static driver_t cfi_disk_driver = {
354 sizeof(struct cfi_disk_softc),
356 DRIVER_MODULE(cfid, cfi, cfi_disk_driver, NULL, NULL);