xref: /freebsd/sys/dev/cfi/cfi_disk.c (revision 069ac184)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009 Sam Leffler, Errno Consulting
5  * Copyright (c) 2012-2013, SRI International
6  * All rights reserved.
7  *
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
11  * programme.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
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.
21  *
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.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bio.h>
37 #include <sys/bus.h>
38 #include <sys/conf.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/module.h>
44 #include <sys/rman.h>
45 #include <sys/sysctl.h>
46 #include <sys/taskqueue.h>
47 
48 #include <machine/bus.h>
49 
50 #include <dev/cfi/cfi_var.h>
51 
52 #include <geom/geom.h>
53 #include <geom/geom_disk.h>
54 
55 struct cfi_disk_softc {
56 	struct cfi_softc *parent;
57 	struct disk	*disk;
58 	int		flags;
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 */
64 };
65 
66 #define	CFI_DISK_SECSIZE	512
67 #define	CFI_DISK_MAXIOSIZE	65536
68 
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 *);
76 
77 static int
78 cfi_disk_probe(device_t dev)
79 {
80 	return 0;
81 }
82 
83 static int
84 cfi_disk_attach(device_t dev)
85 {
86 	struct cfi_disk_softc *sc = device_get_softc(dev);
87 
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)
93 		return EINVAL;
94 
95 	sc->disk = disk_alloc();
96 	if (sc->disk == NULL)
97 		return ENOMEM;
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) {
111 		/*
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.
116 		 */
117 		sc->disk->d_stripesize =
118 		    sc->parent->sc_region[sc->parent->sc_regions-1].r_blksz;
119 	} else
120 		sc->disk->d_stripesize = sc->disk->d_mediasize;
121 	sc->disk->d_drv1 = sc;
122 	disk_create(sc->disk, DISK_VERSION);
123 
124 	mtx_init(&sc->qlock, "CFID I/O lock", NULL, MTX_DEF);
125 	bioq_init(&sc->bioq);
126 
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");
130 
131 	TASK_INIT(&sc->iotask, 0, cfi_io_proc, sc);
132 
133 	return 0;
134 }
135 
136 static int
137 cfi_disk_detach(device_t dev)
138 {
139 	struct cfi_disk_softc *sc = device_get_softc(dev);
140 
141 	if (sc->flags & CFI_DISK_OPEN)
142 		return EBUSY;
143 	taskqueue_free(sc->tq);
144 	/* XXX drain bioq */
145 	disk_destroy(sc->disk);
146 	mtx_destroy(&sc->qlock);
147 	return 0;
148 }
149 
150 static int
151 cfi_disk_open(struct disk *dp)
152 {
153 	struct cfi_disk_softc *sc = dp->d_drv1;
154 
155 	/* XXX no interlock with /dev/cfi */
156 	sc->flags |= CFI_DISK_OPEN;
157 	return 0;
158 }
159 
160 static int
161 cfi_disk_close(struct disk *dp)
162 {
163 	struct cfi_disk_softc *sc = dp->d_drv1;
164 
165 	sc->flags &= ~CFI_DISK_OPEN;
166 	return 0;
167 }
168 
169 static void
170 cfi_disk_read(struct cfi_softc *sc, struct bio *bp)
171 {
172 	long resid;
173 
174 	KASSERT(sc->sc_width == 1 || sc->sc_width == 2 || sc->sc_width == 4,
175 	    ("sc_width %d", sc->sc_width));
176 
177 	if (sc->sc_writing) {
178 		bp->bio_error = cfi_block_finish(sc);
179 		if (bp->bio_error) {
180 			bp->bio_flags |= BIO_ERROR;
181 			goto done;
182 		}
183 	}
184 	if (bp->bio_offset > sc->sc_size) {
185 		bp->bio_flags |= BIO_ERROR;
186 		bp->bio_error = EIO;
187 		goto done;
188 	}
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;
195 		}
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;
201 		}
202 	} else {
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;
207 		}
208 	}
209 	bp->bio_resid = resid;
210 done:
211 	biodone(bp);
212 }
213 
214 static void
215 cfi_disk_write(struct cfi_softc *sc, struct bio *bp)
216 {
217 	long resid;
218 	u_int top;
219 
220 	KASSERT(sc->sc_width == 1 || sc->sc_width == 2 || sc->sc_width == 4,
221 	    ("sc_width %d", sc->sc_width));
222 
223 	if (bp->bio_offset > sc->sc_size) {
224 		bp->bio_flags |= BIO_ERROR;
225 		bp->bio_error = EIO;
226 		goto done;
227 	}
228 	resid = bp->bio_bcount;
229 	while (resid > 0) {
230 		/*
231 		 * Finish the current block if we're about to write
232 		 * to a different block.
233 		 */
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);
239 		}
240 
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);
244 			if (bp->bio_error) {
245 				bp->bio_flags |= BIO_ERROR;
246 				goto done;
247 			}
248 		}
249 
250 		top = sc->sc_wrofs + sc->sc_wrbufsz;
251 		bcopy(bp->bio_data,
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);
255 	}
256 	bp->bio_resid = resid;
257 done:
258 	biodone(bp);
259 }
260 
261 static void
262 cfi_io_proc(void *arg, int pending)
263 {
264 	struct cfi_disk_softc *sc = arg;
265 	struct cfi_softc *cfi = sc->parent;
266 	struct bio *bp;
267 
268 	for (;;) {
269 		mtx_lock(&sc->qlock);
270 		bp = bioq_takefirst(&sc->bioq);
271 		mtx_unlock(&sc->qlock);
272 		if (bp == NULL)
273 			break;
274 
275 		switch (bp->bio_cmd) {
276 		case BIO_READ:
277 			cfi_disk_read(cfi, bp);
278 			break;
279 		case BIO_WRITE:
280 			cfi_disk_write(cfi, bp);
281 			break;
282 		}
283 	}
284 }
285 
286 static int
287 cfi_disk_getattr(struct bio *bp)
288 {
289 	struct cfi_disk_softc *dsc;
290 	struct cfi_softc *sc;
291 	device_t dev;
292 
293 	if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
294 		return (ENXIO);
295 
296 	dsc = bp->bio_disk->d_drv1;
297 	sc = dsc->parent;
298 	dev = sc->sc_dev;
299 
300 	if (strcmp(bp->bio_attribute, "CFI::device") == 0) {
301 		if (bp->bio_length != sizeof(dev))
302 			return (EFAULT);
303 		bcopy(&dev, bp->bio_data, sizeof(dev));
304 	} else
305 		return (-1);
306 	return (0);
307 }
308 
309 static void
310 cfi_disk_strategy(struct bio *bp)
311 {
312 	struct cfi_disk_softc *sc = bp->bio_disk->d_drv1;
313 
314 	if (sc == NULL) {
315 		biofinish(bp, NULL, EINVAL);
316 		return;
317 	}
318 	if (bp->bio_bcount == 0) {
319 		bp->bio_resid = bp->bio_bcount;
320 		biodone(bp);
321 		return;
322 	}
323 	switch (bp->bio_cmd) {
324 	case BIO_READ:
325 	case BIO_WRITE:
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);
331 		break;
332 	default:
333 		biofinish(bp, NULL, EOPNOTSUPP);
334 		break;
335 	}
336 }
337 
338 static int
339 cfi_disk_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
340 	struct thread *td)
341 {
342 	return EINVAL;
343 }
344 
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),
349 	{ 0, 0 }
350 };
351 static driver_t cfi_disk_driver = {
352 	"cfid",
353 	cfi_disk_methods,
354 	sizeof(struct cfi_disk_softc),
355 };
356 DRIVER_MODULE(cfid, cfi, cfi_disk_driver, NULL, NULL);
357