]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/dev/cfi/cfi_disk.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / dev / cfi / cfi_disk.c
1 /*-
2  * Copyright (c) 2009 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bio.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>   
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/module.h>
39 #include <sys/rman.h>
40 #include <sys/sysctl.h>
41 #include <sys/taskqueue.h>
42
43 #include <machine/bus.h>
44
45 #include <dev/cfi/cfi_var.h>
46
47 #include <geom/geom_disk.h>
48
49 struct cfi_disk_softc {
50         struct cfi_softc *parent;
51         struct disk     *disk;
52         int             flags;
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 */
58 };
59
60 #define CFI_DISK_SECSIZE        512
61 #define CFI_DISK_MAXIOSIZE      65536
62
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 *);
69
70 static int
71 cfi_disk_probe(device_t dev)
72 {
73         return 0;
74 }
75
76 static int
77 cfi_disk_attach(device_t dev)
78 {
79         struct cfi_disk_softc *sc = device_get_softc(dev);
80
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)
86                 return EINVAL;
87
88         sc->disk = disk_alloc();
89         if (sc->disk == NULL)
90                 return ENOMEM;
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) {
103                 /*
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.
108                  */
109                 sc->disk->d_stripesize =
110                     sc->parent->sc_region[sc->parent->sc_regions-1].r_blksz;
111         } else
112                 sc->disk->d_stripesize = sc->disk->d_mediasize;
113         sc->disk->d_drv1 = sc;
114         disk_create(sc->disk, DISK_VERSION);
115
116         mtx_init(&sc->qlock, "CFID I/O lock", NULL, MTX_DEF);
117         bioq_init(&sc->bioq);
118
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");
122
123         TASK_INIT(&sc->iotask, 0, cfi_io_proc, sc);
124
125         return 0;
126 }
127
128 static int
129 cfi_disk_detach(device_t dev)
130 {
131         struct cfi_disk_softc *sc = device_get_softc(dev);
132
133         if (sc->flags & CFI_DISK_OPEN)
134                 return EBUSY;
135         taskqueue_free(sc->tq);
136         /* XXX drain bioq */
137         disk_destroy(sc->disk);
138         mtx_destroy(&sc->qlock);
139         return 0;
140 }
141
142 static int
143 cfi_disk_open(struct disk *dp)
144 {
145         struct cfi_disk_softc *sc = dp->d_drv1;
146
147         /* XXX no interlock with /dev/cfi */
148         sc->flags |= CFI_DISK_OPEN;
149         return 0;
150 }
151
152 static int
153 cfi_disk_close(struct disk *dp)
154 {
155         struct cfi_disk_softc *sc = dp->d_drv1;
156
157         sc->flags &= ~CFI_DISK_OPEN;
158         return 0;
159 }
160
161 static void
162 cfi_disk_read(struct cfi_softc *sc, struct bio *bp)
163 {
164         long resid;
165
166         KASSERT(sc->sc_width == 1 || sc->sc_width == 2 || sc->sc_width == 4,
167             ("sc_width %d", sc->sc_width));
168
169         if (sc->sc_writing) {
170                 bp->bio_error = cfi_block_finish(sc);
171                 if (bp->bio_error) {
172                         bp->bio_flags |= BIO_ERROR;
173                         goto done;
174                 }
175         }
176         if (bp->bio_offset > sc->sc_size) {
177                 bp->bio_flags |= BIO_ERROR;
178                 bp->bio_error = EIO;
179                 goto done;
180         }
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;
187                 }
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;
193                 }
194         } else {
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;
199                 }
200         }
201         bp->bio_resid = resid;
202 done:
203         biodone(bp);
204 }
205
206 static void
207 cfi_disk_write(struct cfi_softc *sc, struct bio *bp)
208 {
209         long resid;
210         u_int top;
211
212         KASSERT(sc->sc_width == 1 || sc->sc_width == 2 || sc->sc_width == 4,
213             ("sc_width %d", sc->sc_width));
214
215         if (bp->bio_offset > sc->sc_size) {
216                 bp->bio_flags |= BIO_ERROR;
217                 bp->bio_error = EIO;
218                 goto done;
219         }
220         resid = bp->bio_bcount;
221         while (resid > 0) {
222                 /*
223                  * Finish the current block if we're about to write
224                  * to a different block.
225                  */
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);
231                 }
232
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);
236                         if (bp->bio_error) {
237                                 bp->bio_flags |= BIO_ERROR;
238                                 goto done;
239                         }
240                 }
241
242                 top = sc->sc_wrofs + sc->sc_wrbufsz;
243                 bcopy(bp->bio_data,
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);
247         }
248         bp->bio_resid = resid;
249 done:
250         biodone(bp);
251 }
252
253 static void
254 cfi_io_proc(void *arg, int pending)
255 {
256         struct cfi_disk_softc *sc = arg;
257         struct cfi_softc *cfi = sc->parent;
258         struct bio *bp;
259
260         for (;;) {
261                 mtx_lock(&sc->qlock);
262                 bp = bioq_takefirst(&sc->bioq);
263                 mtx_unlock(&sc->qlock);
264                 if (bp == NULL)
265                         break;
266
267                 switch (bp->bio_cmd) {
268                 case BIO_READ:
269                         cfi_disk_read(cfi, bp);
270                         break;
271                 case BIO_WRITE:
272                         cfi_disk_write(cfi, bp);
273                         break;
274                 }
275         }
276 }
277
278 static void
279 cfi_disk_strategy(struct bio *bp)
280 {
281         struct cfi_disk_softc *sc = bp->bio_disk->d_drv1;
282
283         if (sc == NULL)
284                 goto invalid;
285         if (bp->bio_bcount == 0) {
286                 bp->bio_resid = bp->bio_bcount;
287                 biodone(bp);
288                 return;
289         }
290         switch (bp->bio_cmd) {
291         case BIO_READ:
292         case BIO_WRITE:
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);
298                 return;
299         }
300         /* fall thru... */
301 invalid:
302         bp->bio_flags |= BIO_ERROR;
303         bp->bio_error = EINVAL;
304         biodone(bp);
305 }
306
307 static int
308 cfi_disk_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
309         struct thread *td)
310 {
311         return EINVAL;
312 }
313
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),
318
319         { 0, 0 }
320 };
321 static driver_t cfi_disk_driver = {
322         "cfid",
323         cfi_disk_methods,
324         sizeof(struct cfi_disk_softc),
325 };
326 DRIVER_MODULE(cfid, cfi, cfi_disk_driver, cfi_diskclass, 0, NULL);