Home | History | Annotate | Line # | Download | only in pci
ld_virtio.c revision 1.6
      1 /*	$NetBSD: ld_virtio.c,v 1.6 2014/07/22 01:55:54 ozaki-r Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2010 Minoura Makoto.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: ld_virtio.c,v 1.6 2014/07/22 01:55:54 ozaki-r Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/systm.h>
     33 #include <sys/kernel.h>
     34 #include <sys/buf.h>
     35 #include <sys/bus.h>
     36 #include <sys/device.h>
     37 #include <sys/disk.h>
     38 #include <sys/mutex.h>
     39 #include <sys/rnd.h>
     40 
     41 #include <dev/pci/pcidevs.h>
     42 #include <dev/pci/pcireg.h>
     43 #include <dev/pci/pcivar.h>
     44 
     45 #include <dev/ldvar.h>
     46 #include <dev/pci/virtioreg.h>
     47 #include <dev/pci/virtiovar.h>
     48 
     49 /*
     50  * ld_virtioreg:
     51  */
     52 /* Configuration registers */
     53 #define VIRTIO_BLK_CONFIG_CAPACITY	0 /* 64bit */
     54 #define VIRTIO_BLK_CONFIG_SIZE_MAX	8 /* 32bit */
     55 #define VIRTIO_BLK_CONFIG_SEG_MAX	12 /* 32bit */
     56 #define VIRTIO_BLK_CONFIG_GEOMETRY_C	16 /* 16bit */
     57 #define VIRTIO_BLK_CONFIG_GEOMETRY_H	18 /* 8bit */
     58 #define VIRTIO_BLK_CONFIG_GEOMETRY_S	19 /* 8bit */
     59 #define VIRTIO_BLK_CONFIG_BLK_SIZE	20 /* 32bit */
     60 
     61 /* Feature bits */
     62 #define VIRTIO_BLK_F_BARRIER	(1<<0)
     63 #define VIRTIO_BLK_F_SIZE_MAX	(1<<1)
     64 #define VIRTIO_BLK_F_SEG_MAX	(1<<2)
     65 #define VIRTIO_BLK_F_GEOMETRY	(1<<4)
     66 #define VIRTIO_BLK_F_RO		(1<<5)
     67 #define VIRTIO_BLK_F_BLK_SIZE	(1<<6)
     68 #define VIRTIO_BLK_F_SCSI	(1<<7)
     69 #define VIRTIO_BLK_F_FLUSH	(1<<9)
     70 
     71 /* Command */
     72 #define VIRTIO_BLK_T_IN		0
     73 #define VIRTIO_BLK_T_OUT	1
     74 #define VIRTIO_BLK_T_BARRIER	0x80000000
     75 
     76 /* Status */
     77 #define VIRTIO_BLK_S_OK		0
     78 #define VIRTIO_BLK_S_IOERR	1
     79 
     80 /* Request header structure */
     81 struct virtio_blk_req_hdr {
     82 	uint32_t	type;	/* VIRTIO_BLK_T_* */
     83 	uint32_t	ioprio;
     84 	uint64_t	sector;
     85 } __packed;
     86 /* 512*virtio_blk_req_hdr.sector byte payload and 1 byte status follows */
     87 
     88 
     89 /*
     90  * ld_virtiovar:
     91  */
     92 struct virtio_blk_req {
     93 	struct virtio_blk_req_hdr	vr_hdr;
     94 	uint8_t				vr_status;
     95 	struct buf			*vr_bp;
     96 	bus_dmamap_t			vr_cmdsts;
     97 	bus_dmamap_t			vr_payload;
     98 };
     99 
    100 struct ld_virtio_softc {
    101 	struct ld_softc		sc_ld;
    102 	device_t		sc_dev;
    103 
    104 	struct virtio_softc	*sc_virtio;
    105 	struct virtqueue	sc_vq[1];
    106 
    107 	struct virtio_blk_req	*sc_reqs;
    108 	bus_dma_segment_t	sc_reqs_segs[1];
    109 
    110 	kmutex_t		sc_lock;
    111 
    112 	int			sc_readonly;
    113 };
    114 
    115 static int	ld_virtio_match(device_t, cfdata_t, void *);
    116 static void	ld_virtio_attach(device_t, device_t, void *);
    117 static int	ld_virtio_detach(device_t, int);
    118 
    119 CFATTACH_DECL_NEW(ld_virtio, sizeof(struct ld_virtio_softc),
    120     ld_virtio_match, ld_virtio_attach, ld_virtio_detach, NULL);
    121 
    122 static int
    123 ld_virtio_match(device_t parent, cfdata_t match, void *aux)
    124 {
    125 	struct virtio_softc *va = aux;
    126 
    127 	if (va->sc_childdevid == PCI_PRODUCT_VIRTIO_BLOCK)
    128 		return 1;
    129 
    130 	return 0;
    131 }
    132 
    133 static int ld_virtio_vq_done(struct virtqueue *);
    134 static int ld_virtio_dump(struct ld_softc *, void *, int, int);
    135 static int ld_virtio_start(struct ld_softc *, struct buf *);
    136 
    137 static int
    138 ld_virtio_alloc_reqs(struct ld_virtio_softc *sc, int qsize)
    139 {
    140 	int allocsize, r, rsegs, i;
    141 	struct ld_softc *ld = &sc->sc_ld;
    142 	void *vaddr;
    143 
    144 	allocsize = sizeof(struct virtio_blk_req) * qsize;
    145 	r = bus_dmamem_alloc(sc->sc_virtio->sc_dmat, allocsize, 0, 0,
    146 			     &sc->sc_reqs_segs[0], 1, &rsegs, BUS_DMA_NOWAIT);
    147 	if (r != 0) {
    148 		aprint_error_dev(sc->sc_dev,
    149 				 "DMA memory allocation failed, size %d, "
    150 				 "error code %d\n", allocsize, r);
    151 		goto err_none;
    152 	}
    153 	r = bus_dmamem_map(sc->sc_virtio->sc_dmat,
    154 			   &sc->sc_reqs_segs[0], 1, allocsize,
    155 			   &vaddr, BUS_DMA_NOWAIT);
    156 	if (r != 0) {
    157 		aprint_error_dev(sc->sc_dev,
    158 				 "DMA memory map failed, "
    159 				 "error code %d\n", r);
    160 		goto err_dmamem_alloc;
    161 	}
    162 	sc->sc_reqs = vaddr;
    163 	memset(vaddr, 0, allocsize);
    164 	for (i = 0; i < qsize; i++) {
    165 		struct virtio_blk_req *vr = &sc->sc_reqs[i];
    166 		r = bus_dmamap_create(sc->sc_virtio->sc_dmat,
    167 				      offsetof(struct virtio_blk_req, vr_bp),
    168 				      1,
    169 				      offsetof(struct virtio_blk_req, vr_bp),
    170 				      0,
    171 				      BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
    172 				      &vr->vr_cmdsts);
    173 		if (r != 0) {
    174 			aprint_error_dev(sc->sc_dev,
    175 					 "command dmamap creation failed, "
    176 					 "error code %d\n", r);
    177 			goto err_reqs;
    178 		}
    179 		r = bus_dmamap_load(sc->sc_virtio->sc_dmat, vr->vr_cmdsts,
    180 				    &vr->vr_hdr,
    181 				    offsetof(struct virtio_blk_req, vr_bp),
    182 				    NULL, BUS_DMA_NOWAIT);
    183 		if (r != 0) {
    184 			aprint_error_dev(sc->sc_dev,
    185 					 "command dmamap load failed, "
    186 					 "error code %d\n", r);
    187 			goto err_reqs;
    188 		}
    189 		r = bus_dmamap_create(sc->sc_virtio->sc_dmat,
    190 				      ld->sc_maxxfer,
    191 				      (ld->sc_maxxfer / NBPG) + 2,
    192 				      ld->sc_maxxfer,
    193 				      0,
    194 				      BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW,
    195 				      &vr->vr_payload);
    196 		if (r != 0) {
    197 			aprint_error_dev(sc->sc_dev,
    198 					 "payload dmamap creation failed, "
    199 					 "error code %d\n", r);
    200 			goto err_reqs;
    201 		}
    202 	}
    203 	return 0;
    204 
    205 err_reqs:
    206 	for (i = 0; i < qsize; i++) {
    207 		struct virtio_blk_req *vr = &sc->sc_reqs[i];
    208 		if (vr->vr_cmdsts) {
    209 			bus_dmamap_destroy(sc->sc_virtio->sc_dmat,
    210 					   vr->vr_cmdsts);
    211 			vr->vr_cmdsts = 0;
    212 		}
    213 		if (vr->vr_payload) {
    214 			bus_dmamap_destroy(sc->sc_virtio->sc_dmat,
    215 					   vr->vr_payload);
    216 			vr->vr_payload = 0;
    217 		}
    218 	}
    219 	bus_dmamem_unmap(sc->sc_virtio->sc_dmat, sc->sc_reqs, allocsize);
    220 err_dmamem_alloc:
    221 	bus_dmamem_free(sc->sc_virtio->sc_dmat, &sc->sc_reqs_segs[0], 1);
    222 err_none:
    223 	return -1;
    224 }
    225 
    226 static void
    227 ld_virtio_attach(device_t parent, device_t self, void *aux)
    228 {
    229 	struct ld_virtio_softc *sc = device_private(self);
    230 	struct ld_softc *ld = &sc->sc_ld;
    231 	struct virtio_softc *vsc = device_private(parent);
    232 	uint32_t features;
    233 	int qsize, maxxfersize;
    234 
    235 	if (vsc->sc_child != NULL) {
    236 		aprint_normal(": child already attached for %s; "
    237 			      "something wrong...\n",
    238 			      device_xname(parent));
    239 		return;
    240 	}
    241 	aprint_normal("\n");
    242 	aprint_naive("\n");
    243 
    244 	sc->sc_dev = self;
    245 	sc->sc_virtio = vsc;
    246 
    247 	vsc->sc_child = self;
    248 	vsc->sc_ipl = IPL_BIO;
    249 	vsc->sc_vqs = &sc->sc_vq[0];
    250 	vsc->sc_nvqs = 1;
    251 	vsc->sc_config_change = 0;
    252 	vsc->sc_intrhand = virtio_vq_intr;
    253 	vsc->sc_flags = 0;
    254 
    255 	features = virtio_negotiate_features(vsc,
    256 					     (VIRTIO_BLK_F_SIZE_MAX |
    257 					      VIRTIO_BLK_F_SEG_MAX |
    258 					      VIRTIO_BLK_F_GEOMETRY |
    259 					      VIRTIO_BLK_F_RO |
    260 					      VIRTIO_BLK_F_BLK_SIZE));
    261 	if (features & VIRTIO_BLK_F_RO)
    262 		sc->sc_readonly = 1;
    263 	else
    264 		sc->sc_readonly = 0;
    265 
    266 	ld->sc_secsize = 512;
    267 	if (features & VIRTIO_BLK_F_BLK_SIZE) {
    268 		ld->sc_secsize = virtio_read_device_config_4(vsc,
    269 					VIRTIO_BLK_CONFIG_BLK_SIZE);
    270 	}
    271 	maxxfersize = MAXPHYS;
    272 #if 0	/* At least genfs_io assumes maxxfer == MAXPHYS. */
    273 	if (features & VIRTIO_BLK_F_SEG_MAX) {
    274 		maxxfersize = virtio_read_device_config_4(vsc,
    275 					VIRTIO_BLK_CONFIG_SEG_MAX)
    276 				* ld->sc_secsize;
    277 		if (maxxfersize > MAXPHYS)
    278 			maxxfersize = MAXPHYS;
    279 	}
    280 #endif
    281 
    282 	if (virtio_alloc_vq(vsc, &sc->sc_vq[0], 0,
    283 			    maxxfersize, maxxfersize / NBPG + 2,
    284 			    "I/O request") != 0) {
    285 		goto err;
    286 	}
    287 	qsize = sc->sc_vq[0].vq_num;
    288 	sc->sc_vq[0].vq_done = ld_virtio_vq_done;
    289 
    290 	ld->sc_dv = self;
    291 	ld->sc_secperunit = virtio_read_device_config_8(vsc,
    292 				VIRTIO_BLK_CONFIG_CAPACITY);
    293 	ld->sc_maxxfer = maxxfersize;
    294 	if (features & VIRTIO_BLK_F_GEOMETRY) {
    295 		ld->sc_ncylinders = virtio_read_device_config_2(vsc,
    296 					VIRTIO_BLK_CONFIG_GEOMETRY_C);
    297 		ld->sc_nheads     = virtio_read_device_config_1(vsc,
    298 					VIRTIO_BLK_CONFIG_GEOMETRY_H);
    299 		ld->sc_nsectors   = virtio_read_device_config_1(vsc,
    300 					VIRTIO_BLK_CONFIG_GEOMETRY_S);
    301 	}
    302 	ld->sc_maxqueuecnt = qsize;
    303 
    304 	if (ld_virtio_alloc_reqs(sc, qsize) < 0)
    305 		goto err;
    306 
    307 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_BIO);
    308 
    309 	ld->sc_dump = ld_virtio_dump;
    310 	ld->sc_flush = NULL;
    311 	ld->sc_start = ld_virtio_start;
    312 
    313 	ld->sc_flags = LDF_ENABLED;
    314 	ldattach(ld);
    315 
    316 	return;
    317 
    318 err:
    319 	vsc->sc_child = (void*)1;
    320 	return;
    321 }
    322 
    323 static int
    324 ld_virtio_start(struct ld_softc *ld, struct buf *bp)
    325 {
    326 	/* splbio */
    327 	struct ld_virtio_softc *sc = device_private(ld->sc_dv);
    328 	struct virtio_softc *vsc = sc->sc_virtio;
    329 	struct virtqueue *vq = &sc->sc_vq[0];
    330 	struct virtio_blk_req *vr;
    331 	int r;
    332 	int isread = (bp->b_flags & B_READ);
    333 	int slot;
    334 
    335 	if (sc->sc_readonly && !isread)
    336 		return EIO;
    337 
    338 	r = virtio_enqueue_prep(vsc, vq, &slot);
    339 	if (r != 0)
    340 		return r;
    341 	vr = &sc->sc_reqs[slot];
    342 	r = bus_dmamap_load(vsc->sc_dmat, vr->vr_payload,
    343 			    bp->b_data, bp->b_bcount, NULL,
    344 			    ((isread?BUS_DMA_READ:BUS_DMA_WRITE)
    345 			     |BUS_DMA_NOWAIT));
    346 	if (r != 0)
    347 		return r;
    348 
    349 	r = virtio_enqueue_reserve(vsc, vq, slot, vr->vr_payload->dm_nsegs + 2);
    350 	if (r != 0) {
    351 		bus_dmamap_unload(vsc->sc_dmat, vr->vr_payload);
    352 		return r;
    353 	}
    354 
    355 	vr->vr_bp = bp;
    356 	vr->vr_hdr.type = isread?VIRTIO_BLK_T_IN:VIRTIO_BLK_T_OUT;
    357 	vr->vr_hdr.ioprio = 0;
    358 	vr->vr_hdr.sector = bp->b_rawblkno * sc->sc_ld.sc_secsize / 512;
    359 
    360 	bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts,
    361 			0, sizeof(struct virtio_blk_req_hdr),
    362 			BUS_DMASYNC_PREWRITE);
    363 	bus_dmamap_sync(vsc->sc_dmat, vr->vr_payload,
    364 			0, bp->b_bcount,
    365 			isread?BUS_DMASYNC_PREREAD:BUS_DMASYNC_PREWRITE);
    366 	bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts,
    367 			offsetof(struct virtio_blk_req, vr_status),
    368 			sizeof(uint8_t),
    369 			BUS_DMASYNC_PREREAD);
    370 
    371 	virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
    372 			 0, sizeof(struct virtio_blk_req_hdr),
    373 			 true);
    374 	virtio_enqueue(vsc, vq, slot, vr->vr_payload, !isread);
    375 	virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
    376 			 offsetof(struct virtio_blk_req, vr_status),
    377 			 sizeof(uint8_t),
    378 			 false);
    379 	virtio_enqueue_commit(vsc, vq, slot, true);
    380 
    381 	return 0;
    382 }
    383 
    384 static void
    385 ld_virtio_vq_done1(struct ld_virtio_softc *sc, struct virtio_softc *vsc,
    386 		   struct virtqueue *vq, int slot)
    387 {
    388 	struct virtio_blk_req *vr = &sc->sc_reqs[slot];
    389 	struct buf *bp = vr->vr_bp;
    390 
    391 	bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts,
    392 			0, sizeof(struct virtio_blk_req_hdr),
    393 			BUS_DMASYNC_POSTWRITE);
    394 	bus_dmamap_sync(vsc->sc_dmat, vr->vr_payload,
    395 			0, bp->b_bcount,
    396 			(bp->b_flags & B_READ)?BUS_DMASYNC_POSTREAD
    397 					      :BUS_DMASYNC_POSTWRITE);
    398 	bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts,
    399 			sizeof(struct virtio_blk_req_hdr), sizeof(uint8_t),
    400 			BUS_DMASYNC_POSTREAD);
    401 
    402 	if (vr->vr_status != VIRTIO_BLK_S_OK) {
    403 		bp->b_error = EIO;
    404 		bp->b_resid = bp->b_bcount;
    405 	} else {
    406 		bp->b_error = 0;
    407 		bp->b_resid = 0;
    408 	}
    409 
    410 	virtio_dequeue_commit(vsc, vq, slot);
    411 
    412 	lddone(&sc->sc_ld, bp);
    413 }
    414 
    415 static int
    416 ld_virtio_vq_done(struct virtqueue *vq)
    417 {
    418 	struct virtio_softc *vsc = vq->vq_owner;
    419 	struct ld_virtio_softc *sc = device_private(vsc->sc_child);
    420 	int r = 0;
    421 	int slot;
    422 
    423 again:
    424 	if (virtio_dequeue(vsc, vq, &slot, NULL))
    425 		return r;
    426 	r = 1;
    427 
    428 	ld_virtio_vq_done1(sc, vsc, vq, slot);
    429 	goto again;
    430 }
    431 
    432 static int
    433 ld_virtio_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    434 {
    435 	struct ld_virtio_softc *sc = device_private(ld->sc_dv);
    436 	struct virtio_softc *vsc = sc->sc_virtio;
    437 	struct virtqueue *vq = &sc->sc_vq[0];
    438 	struct virtio_blk_req *vr;
    439 	int slot, r;
    440 
    441 	if (sc->sc_readonly)
    442 		return EIO;
    443 
    444 	r = virtio_enqueue_prep(vsc, vq, &slot);
    445 	if (r != 0) {
    446 		if (r == EAGAIN) { /* no free slot; dequeue first */
    447 			delay(100);
    448 			ld_virtio_vq_done(vq);
    449 			r = virtio_enqueue_prep(vsc, vq, &slot);
    450 			if (r != 0)
    451 				return r;
    452 		}
    453 		return r;
    454 	}
    455 	vr = &sc->sc_reqs[slot];
    456 	r = bus_dmamap_load(vsc->sc_dmat, vr->vr_payload,
    457 			    data, blkcnt*ld->sc_secsize, NULL,
    458 			    BUS_DMA_WRITE|BUS_DMA_NOWAIT);
    459 	if (r != 0)
    460 		return r;
    461 
    462 	r = virtio_enqueue_reserve(vsc, vq, slot, vr->vr_payload->dm_nsegs + 2);
    463 	if (r != 0) {
    464 		bus_dmamap_unload(vsc->sc_dmat, vr->vr_payload);
    465 		return r;
    466 	}
    467 
    468 	vr->vr_bp = (void*)0xdeadbeef;
    469 	vr->vr_hdr.type = VIRTIO_BLK_T_OUT;
    470 	vr->vr_hdr.ioprio = 0;
    471 	vr->vr_hdr.sector = (daddr_t) blkno * ld->sc_secsize / 512;
    472 
    473 	bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts,
    474 			0, sizeof(struct virtio_blk_req_hdr),
    475 			BUS_DMASYNC_PREWRITE);
    476 	bus_dmamap_sync(vsc->sc_dmat, vr->vr_payload,
    477 			0, blkcnt*ld->sc_secsize,
    478 			BUS_DMASYNC_PREWRITE);
    479 	bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts,
    480 			offsetof(struct virtio_blk_req, vr_status),
    481 			sizeof(uint8_t),
    482 			BUS_DMASYNC_PREREAD);
    483 
    484 	virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
    485 			 0, sizeof(struct virtio_blk_req_hdr),
    486 			 true);
    487 	virtio_enqueue(vsc, vq, slot, vr->vr_payload, true);
    488 	virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
    489 			 offsetof(struct virtio_blk_req, vr_status),
    490 			 sizeof(uint8_t),
    491 			 false);
    492 	virtio_enqueue_commit(vsc, vq, slot, true);
    493 
    494 	for ( ; ; ) {
    495 		int dslot;
    496 
    497 		r = virtio_dequeue(vsc, vq, &dslot, NULL);
    498 		if (r != 0)
    499 			continue;
    500 		if (dslot != slot) {
    501 			ld_virtio_vq_done1(sc, vsc, vq, dslot);
    502 			continue;
    503 		} else
    504 			break;
    505 	}
    506 
    507 	bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts,
    508 			0, sizeof(struct virtio_blk_req_hdr),
    509 			BUS_DMASYNC_POSTWRITE);
    510 	bus_dmamap_sync(vsc->sc_dmat, vr->vr_payload,
    511 			0, blkcnt*ld->sc_secsize,
    512 			BUS_DMASYNC_POSTWRITE);
    513 	bus_dmamap_sync(vsc->sc_dmat, vr->vr_cmdsts,
    514 			offsetof(struct virtio_blk_req, vr_status),
    515 			sizeof(uint8_t),
    516 			BUS_DMASYNC_POSTREAD);
    517 	if (vr->vr_status == VIRTIO_BLK_S_OK)
    518 		r = 0;
    519 	else
    520 		r = EIO;
    521 	virtio_dequeue_commit(vsc, vq, slot);
    522 
    523 	return r;
    524 }
    525 
    526 static int
    527 ld_virtio_detach(device_t self, int flags)
    528 {
    529 	struct ld_virtio_softc *sc = device_private(self);
    530 	struct ld_softc *ld = &sc->sc_ld;
    531 	bus_dma_tag_t dmat = sc->sc_virtio->sc_dmat;
    532 	int r, i, qsize;
    533 
    534 	qsize = sc->sc_vq[0].vq_num;
    535 	r = ldbegindetach(ld, flags);
    536 	if (r != 0)
    537 		return r;
    538 	virtio_reset(sc->sc_virtio);
    539 	virtio_free_vq(sc->sc_virtio, &sc->sc_vq[0]);
    540 
    541 	for (i = 0; i < qsize; i++) {
    542 		bus_dmamap_destroy(dmat,
    543 				   sc->sc_reqs[i].vr_cmdsts);
    544 		bus_dmamap_destroy(dmat,
    545 				   sc->sc_reqs[i].vr_payload);
    546 	}
    547 	bus_dmamem_unmap(dmat, sc->sc_reqs,
    548 			 sizeof(struct virtio_blk_req) * qsize);
    549 	bus_dmamem_free(dmat, &sc->sc_reqs_segs[0], 1);
    550 
    551 	ldenddetach(ld);
    552 
    553 	return 0;
    554 }
    555