1/*-
2 * Copyright (c) 2009 Sam Leffler, Errno Consulting
3 * Copyright (c) 2012-2013, SRI International
4 * All rights reserved.
5 *
6 * Portions of this software were developed by SRI International and the
7 * University of Cambridge Computer Laboratory under DARPA/AFRL contract
8 * (FA8750-10-C-0237) ("CTSRD"), as part of the DARPA CRASH research
9 * programme.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34
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
56struct 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
70static int cfi_disk_detach(device_t);
71static int cfi_disk_open(struct disk *);
72static int cfi_disk_close(struct disk *);
73static void cfi_io_proc(void *, int);
74static int cfi_disk_getattr(struct bio *);
75static void cfi_disk_strategy(struct bio *);
76static int cfi_disk_ioctl(struct disk *, u_long, void *, int, struct thread *);
77
78static int
79cfi_disk_probe(device_t dev)
80{
81	return 0;
82}
83
84static int
85cfi_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
137static int
138cfi_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
151static int
152cfi_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
161static int
162cfi_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
170static void
171cfi_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;
211done:
212	biodone(bp);
213}
214
215static void
216cfi_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;
258done:
259	biodone(bp);
260}
261
262static void
263cfi_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
287static int
288cfi_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
311static void
312cfi_disk_strategy(struct bio *bp)
313{
314	struct cfi_disk_softc *sc = bp->bio_disk->d_drv1;
315
316	if (sc == NULL)
317		goto invalid;
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		return;
332	}
333	/* fall thru... */
334invalid:
335	bp->bio_flags |= BIO_ERROR;
336	bp->bio_error = EINVAL;
337	biodone(bp);
338}
339
340static int
341cfi_disk_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
342	struct thread *td)
343{
344	return EINVAL;
345}
346
347static device_method_t cfi_disk_methods[] = {
348	DEVMETHOD(device_probe,		cfi_disk_probe),
349	DEVMETHOD(device_attach,	cfi_disk_attach),
350	DEVMETHOD(device_detach,	cfi_disk_detach),
351
352	{ 0, 0 }
353};
354static driver_t cfi_disk_driver = {
355	"cfid",
356	cfi_disk_methods,
357	sizeof(struct cfi_disk_softc),
358};
359DRIVER_MODULE(cfid, cfi, cfi_disk_driver, cfi_diskclass, 0, NULL);
360