2 * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * BERI virtio block backend driver
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
46 #include <sys/endian.h>
48 #include <sys/vnode.h>
49 #include <sys/fcntl.h>
50 #include <sys/kthread.h>
52 #include <sys/mdioctl.h>
53 #include <sys/namei.h>
55 #include <machine/bus.h>
56 #include <machine/fdt.h>
57 #include <machine/cpu.h>
58 #include <machine/intr.h>
60 #include <dev/fdt/fdt_common.h>
61 #include <dev/ofw/openfirm.h>
62 #include <dev/ofw/ofw_bus.h>
63 #include <dev/ofw/ofw_bus_subr.h>
65 #include <dev/beri/virtio/virtio.h>
66 #include <dev/beri/virtio/virtio_mmio_platform.h>
67 #include <dev/altera/pio/pio.h>
68 #include <dev/virtio/mmio/virtio_mmio.h>
69 #include <dev/virtio/block/virtio_blk.h>
70 #include <dev/virtio/virtio_ids.h>
71 #include <dev/virtio/virtio_config.h>
72 #include <dev/virtio/virtio_ring.h>
76 #define DPRINTF(fmt, ...)
78 /* We use indirect descriptors */
82 #define VTBLK_BLK_ID_BYTES 20
83 #define VTBLK_MAXSEGS 256
85 struct beri_vtblk_softc {
86 struct resource *res[1];
88 bus_space_handle_t bsh;
94 struct vqueue_info vs_queues[NUM_QUEUES];
95 char ident[VTBLK_BLK_ID_BYTES];
98 struct thread *vtblk_ktd;
101 struct md_ioctl *mdio;
102 struct virtio_blk_config *cfg;
105 static struct resource_spec beri_spec[] = {
106 { SYS_RES_MEMORY, 0, RF_ACTIVE },
111 vtblk_rdwr(struct beri_vtblk_softc *sc, struct iovec *iov,
112 int cnt, int offset, int operation, int iolen)
119 bzero(&auio, sizeof(auio));
123 KASSERT(vp != NULL, ("file not opened"));
126 auio.uio_iovcnt = cnt;
127 auio.uio_offset = offset;
128 auio.uio_segflg = UIO_SYSSPACE;
129 auio.uio_rw = operation;
130 auio.uio_resid = iolen;
131 auio.uio_td = curthread;
133 if (operation == 0) {
134 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
135 error = VOP_READ(vp, &auio, IO_DIRECT, sc->cred);
138 (void) vn_start_write(vp, &mp, V_WAIT);
139 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
140 error = VOP_WRITE(vp, &auio, IO_SYNC, sc->cred);
142 vn_finished_write(mp);
149 vtblk_proc(struct beri_vtblk_softc *sc, struct vqueue_info *vq)
151 struct iovec iov[VTBLK_MAXSEGS + 2];
152 uint16_t flags[VTBLK_MAXSEGS + 2];
153 struct virtio_blk_outhdr *vbh;
162 n = vq_getchain(sc->beri_mem_offset, vq, iov,
163 VTBLK_MAXSEGS + 2, flags);
164 KASSERT(n >= 2 && n <= VTBLK_MAXSEGS + 2,
165 ("wrong n value %d", n));
167 tiov = getcopy(iov, n);
168 vbh = iov[0].iov_base;
170 status = iov[n-1].iov_base;
171 KASSERT(iov[n-1].iov_len == 1,
172 ("iov_len == %d", iov[n-1].iov_len));
174 type = be32toh(vbh->type) & ~VIRTIO_BLK_T_BARRIER;
175 offset = be64toh(vbh->sector) * DEV_BSIZE;
178 for (i = 1; i < (n-1); i++) {
179 iolen += iov[i].iov_len;
183 case VIRTIO_BLK_T_OUT:
184 case VIRTIO_BLK_T_IN:
185 err = vtblk_rdwr(sc, tiov + 1, i - 1,
186 offset, type, iolen);
188 case VIRTIO_BLK_T_GET_ID:
189 /* Assume a single buffer */
190 strlcpy(iov[1].iov_base, sc->ident,
191 MIN(iov[1].iov_len, sizeof(sc->ident)));
194 case VIRTIO_BLK_T_FLUSH:
202 if (err == -ENOSYS) {
203 *status = VIRTIO_BLK_S_UNSUPP;
205 *status = VIRTIO_BLK_S_IOERR;
207 *status = VIRTIO_BLK_S_OK;
209 free(tiov, M_DEVBUF);
210 vq_relchain(vq, iov, n, 1);
214 close_file(struct beri_vtblk_softc *sc, struct thread *td)
218 if (sc->vnode != NULL) {
219 vn_lock(sc->vnode, LK_EXCLUSIVE | LK_RETRY);
220 sc->vnode->v_vflag &= ~VV_MD;
221 VOP_UNLOCK(sc->vnode, 0);
222 error = vn_close(sc->vnode, (FREAD|FWRITE),
229 if (sc->cred != NULL)
236 open_file(struct beri_vtblk_softc *sc, struct thread *td)
243 flags = (FREAD | FWRITE);
244 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE,
245 sc->mdio->md_file, td);
246 error = vn_open(&nd, &flags, 0, NULL);
249 NDFREE(&nd, NDF_ONLY_PNBUF);
251 if (nd.ni_vp->v_type != VREG) {
255 error = VOP_GETATTR(nd.ni_vp, &vattr, td->td_ucred);
259 if (VOP_ISLOCKED(nd.ni_vp) != LK_EXCLUSIVE) {
260 vn_lock(nd.ni_vp, LK_UPGRADE | LK_RETRY);
261 if (nd.ni_vp->v_iflag & VI_DOOMED) {
265 nd.ni_vp->v_vflag |= VV_MD;
266 VOP_UNLOCK(nd.ni_vp, 0);
268 sc->vnode = nd.ni_vp;
269 sc->cred = crhold(td->td_ucred);
275 vtblk_notify(struct beri_vtblk_softc *sc)
277 struct vqueue_info *vq;
281 vq = &sc->vs_queues[0];
282 if (!vq_ring_ready(vq))
288 reg = READ2(sc, VIRTIO_MMIO_QUEUE_NOTIFY);
289 queue = be16toh(reg);
291 KASSERT(queue == 0, ("we support single queue only"));
293 /* Process new descriptors */
294 vq = &sc->vs_queues[queue];
295 vq->vq_save_used = be16toh(vq->vq_used->idx);
296 while (vq_has_descs(vq))
299 /* Interrupt the other side */
300 if ((be16toh(vq->vq_avail->flags) & VRING_AVAIL_F_NO_INTERRUPT) == 0) {
301 reg = htobe32(VIRTIO_MMIO_INT_VRING);
302 WRITE4(sc, VIRTIO_MMIO_INTERRUPT_STATUS, reg);
303 PIO_SET(sc->pio_send, Q_INTR, 1);
310 vq_init(struct beri_vtblk_softc *sc)
312 struct vqueue_info *vq;
318 vq = &sc->vs_queues[0];
319 vq->vq_qsize = NUM_DESCS;
321 reg = READ4(sc, VIRTIO_MMIO_QUEUE_PFN);
325 size = vring_size(vq->vq_qsize, VRING_ALIGN);
326 base = paddr_map(sc->beri_mem_offset,
327 (pfn << PAGE_SHIFT), size);
329 /* First pages are descriptors */
330 vq->vq_desc = (struct vring_desc *)base;
331 base += vq->vq_qsize * sizeof(struct vring_desc);
333 /* Then avail ring */
334 vq->vq_avail = (struct vring_avail *)base;
335 base += (2 + vq->vq_qsize + 1) * sizeof(uint16_t);
337 /* Then it's rounded up to the next page */
338 base = (uint8_t *)roundup2((uintptr_t)base, VRING_ALIGN);
340 /* And the last pages are the used ring */
341 vq->vq_used = (struct vring_used *)base;
343 /* Mark queue as allocated, and start at 0 when we use it. */
344 vq->vq_flags = VQ_ALLOC;
345 vq->vq_last_avail = 0;
352 vtblk_thread(void *arg)
354 struct beri_vtblk_softc *sc;
359 sx_xlock(&sc->sc_mtx);
361 err = msleep(sc, &sc->sc_mtx, PCATCH | PZERO, "prd", hz);
364 sx_xunlock(&sc->sc_mtx);
370 backend_info(struct beri_vtblk_softc *sc)
372 struct virtio_blk_config *cfg;
377 /* Specify that we provide block device */
378 reg = htobe32(VIRTIO_ID_BLOCK);
379 WRITE4(sc, VIRTIO_MMIO_DEVICE_ID, reg);
382 reg = htobe32(NUM_DESCS);
383 WRITE4(sc, VIRTIO_MMIO_QUEUE_NUM_MAX, reg);
386 reg = htobe32(VIRTIO_RING_F_INDIRECT_DESC
387 | VIRTIO_BLK_F_BLK_SIZE
388 | VIRTIO_BLK_F_SEG_MAX);
389 WRITE4(sc, VIRTIO_MMIO_HOST_FEATURES, reg);
392 cfg->capacity = htobe64(sc->mdio->md_mediasize / DEV_BSIZE);
393 cfg->size_max = 0; /* not negotiated */
394 cfg->seg_max = htobe32(VTBLK_MAXSEGS);
395 cfg->blk_size = htobe32(DEV_BSIZE);
399 for (i = 0; i < sizeof(struct virtio_blk_config); i+=4) {
400 WRITE4(sc, VIRTIO_MMIO_CONFIG + i, *s);
404 sprintf(sc->ident, "Virtio block backend");
410 vtblk_intr(void *arg)
412 struct beri_vtblk_softc *sc;
418 reg = PIO_READ(sc->pio_recv);
421 PIO_SET(sc->pio_recv, reg, 0);
423 pending = htobe32(reg);
425 if (pending & Q_PFN) {
429 if (pending & Q_NOTIFY) {
435 beri_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
436 int flags, struct thread *td)
438 struct beri_vtblk_softc *sc;
445 /* take file as argument */
446 if (sc->vnode != NULL) {
450 sc->mdio = (struct md_ioctl *)addr;
452 DPRINTF("opening file, td 0x%08x\n", (int)td);
453 err = open_file(sc, td);
456 PIO_SETUP_IRQ(sc->pio_recv, vtblk_intr, sc);
460 if (sc->vnode == 0) {
461 /* File not opened */
465 DPRINTF("closing file, td 0x%08x\n", (int)td);
466 err = close_file(sc, td);
469 PIO_TEARDOWN_IRQ(sc->pio_recv);
478 static struct cdevsw beri_cdevsw = {
479 .d_version = D_VERSION,
480 .d_ioctl = beri_ioctl,
481 .d_name = "virtio block backend",
485 beri_vtblk_probe(device_t dev)
488 if (!ofw_bus_status_okay(dev))
491 if (!ofw_bus_is_compatible(dev, "sri-cambridge,beri-vtblk"))
494 device_set_desc(dev, "SRI-Cambridge BERI block");
495 return (BUS_PROBE_DEFAULT);
499 beri_vtblk_attach(device_t dev)
501 struct beri_vtblk_softc *sc;
504 sc = device_get_softc(dev);
507 if (bus_alloc_resources(dev, beri_spec, sc->res)) {
508 device_printf(dev, "could not allocate resources\n");
512 /* Memory interface */
513 sc->bst = rman_get_bustag(sc->res[0]);
514 sc->bsh = rman_get_bushandle(sc->res[0]);
516 sc->cfg = malloc(sizeof(struct virtio_blk_config),
517 M_DEVBUF, M_NOWAIT|M_ZERO);
519 sx_init(&sc->sc_mtx, device_get_nameunit(sc->dev));
521 error = kthread_add(vtblk_thread, sc, NULL, &sc->vtblk_ktd,
522 0, 0, "beri_virtio_block");
524 device_printf(dev, "cannot create kthread\n");
528 if (setup_offset(dev, &sc->beri_mem_offset) != 0)
530 if (setup_pio(dev, "pio-send", &sc->pio_send) != 0)
532 if (setup_pio(dev, "pio-recv", &sc->pio_recv) != 0)
535 sc->cdev = make_dev(&beri_cdevsw, 0, UID_ROOT, GID_WHEEL,
536 S_IRWXU, "beri_vtblk");
537 if (sc->cdev == NULL) {
538 device_printf(dev, "Failed to create character device.\n");
542 sc->cdev->si_drv1 = sc;
546 static device_method_t beri_vtblk_methods[] = {
547 DEVMETHOD(device_probe, beri_vtblk_probe),
548 DEVMETHOD(device_attach, beri_vtblk_attach),
552 static driver_t beri_vtblk_driver = {
555 sizeof(struct beri_vtblk_softc),
558 static devclass_t beri_vtblk_devclass;
560 DRIVER_MODULE(beri_vtblk, simplebus, beri_vtblk_driver,
561 beri_vtblk_devclass, 0, 0);