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