2 * Copyright (c) 2009 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
37 #include <sys/mutex.h>
38 #include <sys/module.h>
40 #include <sys/sysctl.h>
41 #include <sys/taskqueue.h>
43 #include <machine/bus.h>
45 #include <dev/cfi/cfi_var.h>
47 #include <geom/geom_disk.h>
49 struct cfi_disk_softc {
50 struct cfi_softc *parent;
53 #define CFI_DISK_OPEN 0x0001
54 struct bio_queue_head bioq; /* bio queue */
55 struct mtx qlock; /* bioq lock */
56 struct taskqueue *tq; /* private task queue for i/o request */
57 struct task iotask; /* i/o processing */
60 #define CFI_DISK_SECSIZE 512
61 #define CFI_DISK_MAXIOSIZE 65536
63 static int cfi_disk_detach(device_t);
64 static int cfi_disk_open(struct disk *);
65 static int cfi_disk_close(struct disk *);
66 static void cfi_io_proc(void *, int);
67 static void cfi_disk_strategy(struct bio *);
68 static int cfi_disk_ioctl(struct disk *, u_long, void *, int, struct thread *);
71 cfi_disk_probe(device_t dev)
77 cfi_disk_attach(device_t dev)
79 struct cfi_disk_softc *sc = device_get_softc(dev);
81 sc->parent = device_get_softc(device_get_parent(dev));
82 /* validate interface width; assumed by other code */
83 if (sc->parent->sc_width != 1 &&
84 sc->parent->sc_width != 2 &&
85 sc->parent->sc_width != 4)
88 sc->disk = disk_alloc();
91 sc->disk->d_name = "cfid";
92 sc->disk->d_unit = device_get_unit(dev);
93 sc->disk->d_open = cfi_disk_open;
94 sc->disk->d_close = cfi_disk_close;
95 sc->disk->d_strategy = cfi_disk_strategy;
96 sc->disk->d_ioctl = cfi_disk_ioctl;
97 sc->disk->d_dump = NULL; /* NB: no dumps */
98 sc->disk->d_sectorsize = CFI_DISK_SECSIZE;
99 sc->disk->d_mediasize = sc->parent->sc_size;
100 sc->disk->d_maxsize = CFI_DISK_MAXIOSIZE;
101 /* NB: use stripesize to hold the erase/region size */
102 if (sc->parent->sc_regions) {
104 * Multiple regions, use the last one. This is a
105 * total hack as it's (presently) used only by
106 * geom_redboot to locate the FIS directory which
107 * lies at the start of the last erase region.
109 sc->disk->d_stripesize =
110 sc->parent->sc_region[sc->parent->sc_regions-1].r_blksz;
112 sc->disk->d_stripesize = sc->disk->d_mediasize;
113 sc->disk->d_drv1 = sc;
114 disk_create(sc->disk, DISK_VERSION);
116 mtx_init(&sc->qlock, "CFID I/O lock", NULL, MTX_DEF);
117 bioq_init(&sc->bioq);
119 sc->tq = taskqueue_create("cfid_taskq", M_NOWAIT,
120 taskqueue_thread_enqueue, &sc->tq);
121 taskqueue_start_threads(&sc->tq, 1, PI_DISK, "cfid taskq");
123 TASK_INIT(&sc->iotask, 0, cfi_io_proc, sc);
129 cfi_disk_detach(device_t dev)
131 struct cfi_disk_softc *sc = device_get_softc(dev);
133 if (sc->flags & CFI_DISK_OPEN)
135 taskqueue_free(sc->tq);
137 disk_destroy(sc->disk);
138 mtx_destroy(&sc->qlock);
143 cfi_disk_open(struct disk *dp)
145 struct cfi_disk_softc *sc = dp->d_drv1;
147 /* XXX no interlock with /dev/cfi */
148 sc->flags |= CFI_DISK_OPEN;
153 cfi_disk_close(struct disk *dp)
155 struct cfi_disk_softc *sc = dp->d_drv1;
157 sc->flags &= ~CFI_DISK_OPEN;
162 cfi_disk_read(struct cfi_softc *sc, struct bio *bp)
166 KASSERT(sc->sc_width == 1 || sc->sc_width == 2 || sc->sc_width == 4,
167 ("sc_width %d", sc->sc_width));
169 if (sc->sc_writing) {
170 bp->bio_error = cfi_block_finish(sc);
172 bp->bio_flags |= BIO_ERROR;
176 if (bp->bio_offset > sc->sc_size) {
177 bp->bio_flags |= BIO_ERROR;
181 resid = bp->bio_bcount;
182 if (sc->sc_width == 1) {
183 uint8_t *dp = (uint8_t *)bp->bio_data;
184 while (resid > 0 && bp->bio_offset < sc->sc_size) {
185 *dp++ = cfi_read(sc, bp->bio_offset);
186 bp->bio_offset += 1, resid -= 1;
188 } else if (sc->sc_width == 2) {
189 uint16_t *dp = (uint16_t *)bp->bio_data;
190 while (resid > 0 && bp->bio_offset < sc->sc_size) {
191 *dp++ = cfi_read(sc, bp->bio_offset);
192 bp->bio_offset += 2, resid -= 2;
195 uint32_t *dp = (uint32_t *)bp->bio_data;
196 while (resid > 0 && bp->bio_offset < sc->sc_size) {
197 *dp++ = cfi_read(sc, bp->bio_offset);
198 bp->bio_offset += 4, resid -= 4;
201 bp->bio_resid = resid;
207 cfi_disk_write(struct cfi_softc *sc, struct bio *bp)
212 KASSERT(sc->sc_width == 1 || sc->sc_width == 2 || sc->sc_width == 4,
213 ("sc_width %d", sc->sc_width));
215 if (bp->bio_offset > sc->sc_size) {
216 bp->bio_flags |= BIO_ERROR;
220 resid = bp->bio_bcount;
223 * Finish the current block if we're about to write
224 * to a different block.
226 if (sc->sc_writing) {
227 top = sc->sc_wrofs + sc->sc_wrbufsz;
228 if (bp->bio_offset < sc->sc_wrofs ||
229 bp->bio_offset >= top)
230 cfi_block_finish(sc);
233 /* Start writing to a (new) block if applicable. */
234 if (!sc->sc_writing) {
235 bp->bio_error = cfi_block_start(sc, bp->bio_offset);
237 bp->bio_flags |= BIO_ERROR;
242 top = sc->sc_wrofs + sc->sc_wrbufsz;
244 sc->sc_wrbuf + bp->bio_offset - sc->sc_wrofs,
245 MIN(top - bp->bio_offset, resid));
246 resid -= MIN(top - bp->bio_offset, resid);
248 bp->bio_resid = resid;
254 cfi_io_proc(void *arg, int pending)
256 struct cfi_disk_softc *sc = arg;
257 struct cfi_softc *cfi = sc->parent;
261 mtx_lock(&sc->qlock);
262 bp = bioq_takefirst(&sc->bioq);
263 mtx_unlock(&sc->qlock);
267 switch (bp->bio_cmd) {
269 cfi_disk_read(cfi, bp);
272 cfi_disk_write(cfi, bp);
279 cfi_disk_strategy(struct bio *bp)
281 struct cfi_disk_softc *sc = bp->bio_disk->d_drv1;
285 if (bp->bio_bcount == 0) {
286 bp->bio_resid = bp->bio_bcount;
290 switch (bp->bio_cmd) {
293 mtx_lock(&sc->qlock);
294 /* no value in sorting requests? */
295 bioq_insert_tail(&sc->bioq, bp);
296 mtx_unlock(&sc->qlock);
297 taskqueue_enqueue(sc->tq, &sc->iotask);
302 bp->bio_flags |= BIO_ERROR;
303 bp->bio_error = EINVAL;
308 cfi_disk_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
314 static device_method_t cfi_disk_methods[] = {
315 DEVMETHOD(device_probe, cfi_disk_probe),
316 DEVMETHOD(device_attach, cfi_disk_attach),
317 DEVMETHOD(device_detach, cfi_disk_detach),
321 static driver_t cfi_disk_driver = {
324 sizeof(struct cfi_disk_softc),
326 DRIVER_MODULE(cfid, cfi, cfi_disk_driver, cfi_diskclass, 0, NULL);