xref: /netbsd-src/sys/dev/pci/ld_virtio.c (revision 70f7362772ba52b749c976fb5e86e39a8b2c9afc)
1 /*	$NetBSD: ld_virtio.c,v 1.34 2024/03/09 11:04:22 isaki 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.34 2024/03/09 11:04:22 isaki 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/bufq.h>
36 #include <sys/bus.h>
37 #include <sys/device.h>
38 #include <sys/disk.h>
39 #include <sys/mutex.h>
40 #include <sys/module.h>
41 
42 #include <dev/ldvar.h>
43 #include <dev/pci/virtioreg.h>
44 #include <dev/pci/virtiovar.h>
45 
46 #include "ioconf.h"
47 
48 /*
49  * ld_virtioreg:
50  */
51 /* Configuration registers */
52 #define VIRTIO_BLK_CONFIG_CAPACITY	0 /* 64bit */
53 #define VIRTIO_BLK_CONFIG_SIZE_MAX	8 /* 32bit */
54 #define VIRTIO_BLK_CONFIG_SEG_MAX	12 /* 32bit */
55 #define VIRTIO_BLK_CONFIG_GEOMETRY_C	16 /* 16bit */
56 #define VIRTIO_BLK_CONFIG_GEOMETRY_H	18 /* 8bit */
57 #define VIRTIO_BLK_CONFIG_GEOMETRY_S	19 /* 8bit */
58 #define VIRTIO_BLK_CONFIG_BLK_SIZE	20 /* 32bit */
59 #define VIRTIO_BLK_CONFIG_WRITEBACK	32 /* 8bit */
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 #define VIRTIO_BLK_F_TOPOLOGY	(1<<10)
71 #define VIRTIO_BLK_F_CONFIG_WCE	(1<<11)
72 
73 /*
74  * Each block request uses at least two segments - one for the header
75  * and one for the status.
76 */
77 #define	VIRTIO_BLK_CTRL_SEGMENTS	2
78 
79 #define VIRTIO_BLK_FLAG_BITS			\
80 	VIRTIO_COMMON_FLAG_BITS			\
81 	"b\x0b" "CONFIG_WCE\0"			\
82 	"b\x0a" "TOPOLOGY\0"			\
83 	"b\x09" "FLUSH\0"			\
84 	"b\x07" "SCSI\0"			\
85 	"b\x06" "BLK_SIZE\0"			\
86 	"b\x05" "RO\0"				\
87 	"b\x04" "GEOMETRY\0"			\
88 	"b\x02" "SEG_MAX\0"			\
89 	"b\x01" "SIZE_MAX\0"			\
90 	"b\x00" "BARRIER\0"
91 
92 /* Command */
93 #define VIRTIO_BLK_T_IN		0
94 #define VIRTIO_BLK_T_OUT	1
95 #define VIRTIO_BLK_T_FLUSH	4
96 #define VIRTIO_BLK_T_BARRIER	0x80000000
97 
98 /* Sector */
99 #define VIRTIO_BLK_BSIZE	512
100 
101 /* Status */
102 #define VIRTIO_BLK_S_OK		0
103 #define VIRTIO_BLK_S_IOERR	1
104 #define VIRTIO_BLK_S_UNSUPP	2
105 
106 /* Request header structure */
107 struct virtio_blk_req_hdr {
108 	uint32_t	type;	/* VIRTIO_BLK_T_* */
109 	uint32_t	ioprio;
110 	uint64_t	sector;
111 } __packed;
112 /* payload and 1 byte status follows */
113 
114 
115 /*
116  * ld_virtiovar:
117  */
118 struct virtio_blk_req {
119 	struct virtio_blk_req_hdr	vr_hdr;
120 	uint8_t				vr_status;
121 	struct buf			*vr_bp;
122 #define DUMMY_VR_BP				((void *)1)
123 	bus_dmamap_t			vr_cmdsts;
124 	bus_dmamap_t			vr_payload;
125 };
126 
127 struct ld_virtio_softc {
128 	struct ld_softc		sc_ld;
129 	device_t		sc_dev;
130 
131 	struct virtio_softc	*sc_virtio;
132 	struct virtqueue	sc_vq;
133 
134 	struct virtio_blk_req	*sc_reqs;
135 	bus_dma_segment_t	sc_reqs_seg;
136 
137 	int			sc_readonly;
138 
139 	enum {
140 		SYNC_FREE, SYNC_BUSY, SYNC_DONE
141 	}			sc_sync_use;
142 	kcondvar_t		sc_sync_wait;
143 	kmutex_t		sc_sync_wait_lock;
144 	uint8_t			sc_sync_status;
145 };
146 
147 static int	ld_virtio_match(device_t, cfdata_t, void *);
148 static void	ld_virtio_attach(device_t, device_t, void *);
149 static int	ld_virtio_detach(device_t, int);
150 
151 CFATTACH_DECL_NEW(ld_virtio, sizeof(struct ld_virtio_softc),
152     ld_virtio_match, ld_virtio_attach, ld_virtio_detach, NULL);
153 
154 static int
155 ld_virtio_match(device_t parent, cfdata_t match, void *aux)
156 {
157 	struct virtio_attach_args *va = aux;
158 
159 	if (va->sc_childdevid == VIRTIO_DEVICE_ID_BLOCK)
160 		return 1;
161 
162 	return 0;
163 }
164 
165 static int ld_virtio_vq_done(struct virtqueue *);
166 static int ld_virtio_dump(struct ld_softc *, void *, int, int);
167 static int ld_virtio_start(struct ld_softc *, struct buf *);
168 static int ld_virtio_ioctl(struct ld_softc *, u_long, void *, int32_t, bool);
169 
170 static int
171 ld_virtio_alloc_reqs(struct ld_virtio_softc *sc, int qsize)
172 {
173 	int allocsize, r, rsegs, i;
174 	struct ld_softc *ld = &sc->sc_ld;
175 	void *vaddr;
176 
177 	allocsize = sizeof(struct virtio_blk_req) * qsize;
178 	r = bus_dmamem_alloc(virtio_dmat(sc->sc_virtio), allocsize, 0, 0,
179 			     &sc->sc_reqs_seg, 1, &rsegs, BUS_DMA_WAITOK);
180 	if (r != 0) {
181 		aprint_error_dev(sc->sc_dev,
182 				 "DMA memory allocation failed, size %d, "
183 				 "error code %d\n", allocsize, r);
184 		goto err_none;
185 	}
186 	r = bus_dmamem_map(virtio_dmat(sc->sc_virtio),
187 			   &sc->sc_reqs_seg, 1, allocsize,
188 			   &vaddr, BUS_DMA_WAITOK);
189 	if (r != 0) {
190 		aprint_error_dev(sc->sc_dev,
191 				 "DMA memory map failed, "
192 				 "error code %d\n", r);
193 		goto err_dmamem_alloc;
194 	}
195 	sc->sc_reqs = vaddr;
196 	memset(vaddr, 0, allocsize);
197 	for (i = 0; i < qsize; i++) {
198 		struct virtio_blk_req *vr = &sc->sc_reqs[i];
199 		r = bus_dmamap_create(virtio_dmat(sc->sc_virtio),
200 				      offsetof(struct virtio_blk_req, vr_bp),
201 				      1,
202 				      offsetof(struct virtio_blk_req, vr_bp),
203 				      0,
204 				      BUS_DMA_WAITOK|BUS_DMA_ALLOCNOW,
205 				      &vr->vr_cmdsts);
206 		if (r != 0) {
207 			aprint_error_dev(sc->sc_dev,
208 					 "command dmamap creation failed, "
209 					 "error code %d\n", r);
210 			goto err_reqs;
211 		}
212 		r = bus_dmamap_load(virtio_dmat(sc->sc_virtio), vr->vr_cmdsts,
213 				    &vr->vr_hdr,
214 				    offsetof(struct virtio_blk_req, vr_bp),
215 				    NULL, BUS_DMA_WAITOK);
216 		if (r != 0) {
217 			aprint_error_dev(sc->sc_dev,
218 					 "command dmamap load failed, "
219 					 "error code %d\n", r);
220 			goto err_reqs;
221 		}
222 		r = bus_dmamap_create(virtio_dmat(sc->sc_virtio),
223 				      ld->sc_maxxfer,
224 				      (ld->sc_maxxfer / NBPG) +
225 				      VIRTIO_BLK_CTRL_SEGMENTS,
226 				      ld->sc_maxxfer,
227 				      0,
228 				      BUS_DMA_WAITOK|BUS_DMA_ALLOCNOW,
229 				      &vr->vr_payload);
230 		if (r != 0) {
231 			aprint_error_dev(sc->sc_dev,
232 					 "payload dmamap creation failed, "
233 					 "error code %d\n", r);
234 			goto err_reqs;
235 		}
236 	}
237 	return 0;
238 
239 err_reqs:
240 	for (i = 0; i < qsize; i++) {
241 		struct virtio_blk_req *vr = &sc->sc_reqs[i];
242 		if (vr->vr_cmdsts) {
243 			bus_dmamap_destroy(virtio_dmat(sc->sc_virtio),
244 					   vr->vr_cmdsts);
245 			vr->vr_cmdsts = 0;
246 		}
247 		if (vr->vr_payload) {
248 			bus_dmamap_destroy(virtio_dmat(sc->sc_virtio),
249 					   vr->vr_payload);
250 			vr->vr_payload = 0;
251 		}
252 	}
253 	bus_dmamem_unmap(virtio_dmat(sc->sc_virtio), sc->sc_reqs, allocsize);
254 err_dmamem_alloc:
255 	bus_dmamem_free(virtio_dmat(sc->sc_virtio), &sc->sc_reqs_seg, 1);
256 err_none:
257 	return -1;
258 }
259 
260 static void
261 ld_virtio_attach(device_t parent, device_t self, void *aux)
262 {
263 	struct ld_virtio_softc *sc = device_private(self);
264 	struct ld_softc *ld = &sc->sc_ld;
265 	struct virtio_softc *vsc = device_private(parent);
266 	uint64_t features;
267 	int qsize, maxxfersize, maxnsegs;
268 
269 	if (virtio_child(vsc) != NULL) {
270 		aprint_normal(": child already attached for %s; "
271 			      "something wrong...\n", device_xname(parent));
272 		return;
273 	}
274 
275 	sc->sc_dev = self;
276 	sc->sc_virtio = vsc;
277 
278 	virtio_child_attach_start(vsc, self, IPL_BIO,
279 	    (VIRTIO_BLK_F_SIZE_MAX | VIRTIO_BLK_F_SEG_MAX |
280 	     VIRTIO_BLK_F_GEOMETRY | VIRTIO_BLK_F_RO | VIRTIO_BLK_F_BLK_SIZE |
281 	     VIRTIO_BLK_F_FLUSH | VIRTIO_BLK_F_CONFIG_WCE),
282 	    VIRTIO_BLK_FLAG_BITS);
283 
284 	features = virtio_features(vsc);
285 	if (features == 0)
286 		goto err;
287 
288 	if (features & VIRTIO_BLK_F_RO)
289 		sc->sc_readonly = 1;
290 	else
291 		sc->sc_readonly = 0;
292 
293 	if (features & VIRTIO_BLK_F_BLK_SIZE) {
294 		ld->sc_secsize = virtio_read_device_config_4(vsc,
295 					VIRTIO_BLK_CONFIG_BLK_SIZE);
296 	} else
297 		ld->sc_secsize = VIRTIO_BLK_BSIZE;
298 
299 	/* At least genfs_io assumes maxxfer == MAXPHYS. */
300 	if (features & VIRTIO_BLK_F_SIZE_MAX) {
301 		maxxfersize = virtio_read_device_config_4(vsc,
302 		    VIRTIO_BLK_CONFIG_SIZE_MAX);
303 		if (maxxfersize < MAXPHYS) {
304 			aprint_error_dev(sc->sc_dev,
305 			    "Too small SIZE_MAX %dK minimum is %dK\n",
306 			    maxxfersize / 1024, MAXPHYS / 1024);
307 			// goto err;
308 			maxxfersize = MAXPHYS;
309 		} else if (maxxfersize > MAXPHYS) {
310 			aprint_normal_dev(sc->sc_dev,
311 			    "Clip SIZE_MAX from %dK to %dK\n",
312 			    maxxfersize / 1024,
313 			    MAXPHYS / 1024);
314 			maxxfersize = MAXPHYS;
315 		}
316 	} else
317 		maxxfersize = MAXPHYS;
318 
319 	if (features & VIRTIO_BLK_F_SEG_MAX) {
320 		maxnsegs = virtio_read_device_config_4(vsc,
321 		    VIRTIO_BLK_CONFIG_SEG_MAX);
322 		if (maxnsegs == 0) {
323 			aprint_error_dev(sc->sc_dev,
324 			    "Invalid SEG_MAX %d\n", maxnsegs);
325 			goto err;
326 		}
327 	} else
328 		maxnsegs = maxxfersize / NBPG;
329 
330 	maxnsegs += VIRTIO_BLK_CTRL_SEGMENTS;
331 
332 	virtio_init_vq_vqdone(vsc, &sc->sc_vq, 0,
333 	    ld_virtio_vq_done);
334 
335 	if (virtio_alloc_vq(vsc, &sc->sc_vq, maxxfersize, maxnsegs,
336 	    "I/O request") != 0) {
337 		goto err;
338 	}
339 	qsize = sc->sc_vq.vq_num;
340 
341 	if (virtio_child_attach_finish(vsc, &sc->sc_vq, 1,
342 	    NULL, VIRTIO_F_INTR_MSIX) != 0)
343 		goto err;
344 
345 	ld->sc_dv = self;
346 	ld->sc_secperunit = virtio_read_device_config_8(vsc,
347 	    VIRTIO_BLK_CONFIG_CAPACITY) / (ld->sc_secsize / VIRTIO_BLK_BSIZE);
348 	ld->sc_maxxfer = maxxfersize;
349 	if (features & VIRTIO_BLK_F_GEOMETRY) {
350 		ld->sc_ncylinders = virtio_read_device_config_2(vsc,
351 					VIRTIO_BLK_CONFIG_GEOMETRY_C);
352 		ld->sc_nheads     = virtio_read_device_config_1(vsc,
353 					VIRTIO_BLK_CONFIG_GEOMETRY_H);
354 		ld->sc_nsectors   = virtio_read_device_config_1(vsc,
355 					VIRTIO_BLK_CONFIG_GEOMETRY_S);
356 	}
357 	ld->sc_maxqueuecnt = qsize - 1; /* reserve slot for dumps, flushes */
358 
359 	if (ld_virtio_alloc_reqs(sc, qsize) < 0)
360 		goto err;
361 
362 	cv_init(&sc->sc_sync_wait, "vblksync");
363 	mutex_init(&sc->sc_sync_wait_lock, MUTEX_DEFAULT, IPL_BIO);
364 	sc->sc_sync_use = SYNC_FREE;
365 
366 	ld->sc_dump = ld_virtio_dump;
367 	ld->sc_start = ld_virtio_start;
368 	ld->sc_ioctl = ld_virtio_ioctl;
369 
370 	ld->sc_flags = LDF_ENABLED | LDF_MPSAFE;
371 	ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
372 
373 	return;
374 
375 err:
376 	virtio_child_attach_failed(vsc);
377 	return;
378 }
379 
380 static int
381 ld_virtio_start(struct ld_softc *ld, struct buf *bp)
382 {
383 	/* splbio */
384 	struct ld_virtio_softc *sc = device_private(ld->sc_dv);
385 	struct virtio_softc *vsc = sc->sc_virtio;
386 	struct virtqueue *vq = &sc->sc_vq;
387 	struct virtio_blk_req *vr;
388 	int r;
389 	int isread = (bp->b_flags & B_READ);
390 	int slot;
391 
392 	if (sc->sc_readonly && !isread)
393 		return EIO;
394 
395 	r = virtio_enqueue_prep(vsc, vq, &slot);
396 	if (r != 0)
397 		return r;
398 
399 	vr = &sc->sc_reqs[slot];
400 	KASSERT(vr->vr_bp == NULL);
401 
402 	r = bus_dmamap_load(virtio_dmat(vsc), vr->vr_payload,
403 			    bp->b_data, bp->b_bcount, NULL,
404 			    ((isread?BUS_DMA_READ:BUS_DMA_WRITE)
405 			     |BUS_DMA_NOWAIT));
406 	if (r != 0) {
407 		aprint_error_dev(sc->sc_dev,
408 		    "payload dmamap failed, error code %d\n", r);
409 		virtio_enqueue_abort(vsc, vq, slot);
410 		return r;
411 	}
412 
413 	r = virtio_enqueue_reserve(vsc, vq, slot, vr->vr_payload->dm_nsegs +
414 	    VIRTIO_BLK_CTRL_SEGMENTS);
415 	if (r != 0) {
416 		bus_dmamap_unload(virtio_dmat(vsc), vr->vr_payload);
417 		return r;
418 	}
419 
420 	vr->vr_bp = bp;
421 	vr->vr_hdr.type   = virtio_rw32(vsc,
422 			isread ? VIRTIO_BLK_T_IN : VIRTIO_BLK_T_OUT);
423 	vr->vr_hdr.ioprio = virtio_rw32(vsc, 0);
424 	vr->vr_hdr.sector = virtio_rw64(vsc,
425 			bp->b_rawblkno * sc->sc_ld.sc_secsize /
426 			VIRTIO_BLK_BSIZE);
427 
428 	bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
429 			0, sizeof(struct virtio_blk_req_hdr),
430 			BUS_DMASYNC_PREWRITE);
431 	bus_dmamap_sync(virtio_dmat(vsc), vr->vr_payload,
432 			0, bp->b_bcount,
433 			isread?BUS_DMASYNC_PREREAD:BUS_DMASYNC_PREWRITE);
434 	bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
435 			offsetof(struct virtio_blk_req, vr_status),
436 			sizeof(uint8_t),
437 			BUS_DMASYNC_PREREAD);
438 
439 	virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
440 			 0, sizeof(struct virtio_blk_req_hdr),
441 			 true);
442 	virtio_enqueue(vsc, vq, slot, vr->vr_payload, !isread);
443 	virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
444 			 offsetof(struct virtio_blk_req, vr_status),
445 			 sizeof(uint8_t),
446 			 false);
447 	virtio_enqueue_commit(vsc, vq, slot, true);
448 
449 	return 0;
450 }
451 
452 static void
453 ld_virtio_vq_done1(struct ld_virtio_softc *sc, struct virtio_softc *vsc,
454 		   struct virtqueue *vq, int slot)
455 {
456 	struct virtio_blk_req *vr = &sc->sc_reqs[slot];
457 	struct buf *bp = vr->vr_bp;
458 
459 	vr->vr_bp = NULL;
460 
461 	bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
462 			0, sizeof(struct virtio_blk_req_hdr),
463 			BUS_DMASYNC_POSTWRITE);
464 	bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
465 			sizeof(struct virtio_blk_req_hdr), sizeof(uint8_t),
466 			BUS_DMASYNC_POSTREAD);
467 	if (bp == DUMMY_VR_BP) {
468 		mutex_enter(&sc->sc_sync_wait_lock);
469 		sc->sc_sync_status = vr->vr_status;
470 		sc->sc_sync_use = SYNC_DONE;
471 		cv_broadcast(&sc->sc_sync_wait);
472 		mutex_exit(&sc->sc_sync_wait_lock);
473 		virtio_dequeue_commit(vsc, vq, slot);
474 		return;
475 	}
476 	bus_dmamap_sync(virtio_dmat(vsc), vr->vr_payload,
477 			0, bp->b_bcount,
478 			(bp->b_flags & B_READ)?BUS_DMASYNC_POSTREAD
479 					      :BUS_DMASYNC_POSTWRITE);
480 	bus_dmamap_unload(virtio_dmat(vsc), vr->vr_payload);
481 
482 	if (vr->vr_status != VIRTIO_BLK_S_OK) {
483 		bp->b_error = EIO;
484 		bp->b_resid = bp->b_bcount;
485 	} else {
486 		bp->b_error = 0;
487 		bp->b_resid = 0;
488 	}
489 
490 	virtio_dequeue_commit(vsc, vq, slot);
491 
492 	lddone(&sc->sc_ld, bp);
493 }
494 
495 static int
496 ld_virtio_vq_done(struct virtqueue *vq)
497 {
498 	struct virtio_softc *vsc = vq->vq_owner;
499 	struct ld_virtio_softc *sc = device_private(virtio_child(vsc));
500 	int r = 0;
501 	int slot;
502 
503 again:
504 	if (virtio_dequeue(vsc, vq, &slot, NULL))
505 		return r;
506 	r = 1;
507 
508 	ld_virtio_vq_done1(sc, vsc, vq, slot);
509 	goto again;
510 }
511 
512 static int
513 ld_virtio_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
514 {
515 	struct ld_virtio_softc *sc = device_private(ld->sc_dv);
516 	struct virtio_softc *vsc = sc->sc_virtio;
517 	struct virtqueue *vq = &sc->sc_vq;
518 	struct virtio_blk_req *vr;
519 	int slot, r;
520 
521 	if (sc->sc_readonly)
522 		return EIO;
523 
524 	r = virtio_enqueue_prep(vsc, vq, &slot);
525 	if (r != 0) {
526 		if (r == EAGAIN) { /* no free slot; dequeue first */
527 			delay(100);
528 			ld_virtio_vq_done(vq);
529 			r = virtio_enqueue_prep(vsc, vq, &slot);
530 			if (r != 0)
531 				return r;
532 		}
533 		return r;
534 	}
535 	vr = &sc->sc_reqs[slot];
536 	r = bus_dmamap_load(virtio_dmat(vsc), vr->vr_payload,
537 			    data, blkcnt*ld->sc_secsize, NULL,
538 			    BUS_DMA_WRITE|BUS_DMA_NOWAIT);
539 	if (r != 0)
540 		return r;
541 
542 	r = virtio_enqueue_reserve(vsc, vq, slot, vr->vr_payload->dm_nsegs +
543 	    VIRTIO_BLK_CTRL_SEGMENTS);
544 	if (r != 0) {
545 		bus_dmamap_unload(virtio_dmat(vsc), vr->vr_payload);
546 		return r;
547 	}
548 
549 	vr->vr_bp = (void*)0xdeadbeef;
550 	vr->vr_hdr.type   = virtio_rw32(vsc, VIRTIO_BLK_T_OUT);
551 	vr->vr_hdr.ioprio = virtio_rw32(vsc, 0);
552 	vr->vr_hdr.sector = virtio_rw64(vsc,
553 			(daddr_t) blkno * ld->sc_secsize /
554 			VIRTIO_BLK_BSIZE);
555 
556 	bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
557 			0, sizeof(struct virtio_blk_req_hdr),
558 			BUS_DMASYNC_PREWRITE);
559 	bus_dmamap_sync(virtio_dmat(vsc), vr->vr_payload,
560 			0, blkcnt*ld->sc_secsize,
561 			BUS_DMASYNC_PREWRITE);
562 	bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
563 			offsetof(struct virtio_blk_req, vr_status),
564 			sizeof(uint8_t),
565 			BUS_DMASYNC_PREREAD);
566 
567 	virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
568 			 0, sizeof(struct virtio_blk_req_hdr),
569 			 true);
570 	virtio_enqueue(vsc, vq, slot, vr->vr_payload, true);
571 	virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
572 			 offsetof(struct virtio_blk_req, vr_status),
573 			 sizeof(uint8_t),
574 			 false);
575 	virtio_enqueue_commit(vsc, vq, slot, true);
576 
577 	for ( ; ; ) {
578 		int dslot;
579 
580 		r = virtio_dequeue(vsc, vq, &dslot, NULL);
581 		if (r != 0)
582 			continue;
583 		if (dslot != slot) {
584 			ld_virtio_vq_done1(sc, vsc, vq, dslot);
585 			continue;
586 		} else
587 			break;
588 	}
589 
590 	bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
591 			0, sizeof(struct virtio_blk_req_hdr),
592 			BUS_DMASYNC_POSTWRITE);
593 	bus_dmamap_sync(virtio_dmat(vsc), vr->vr_payload,
594 			0, blkcnt*ld->sc_secsize,
595 			BUS_DMASYNC_POSTWRITE);
596 	bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
597 			offsetof(struct virtio_blk_req, vr_status),
598 			sizeof(uint8_t),
599 			BUS_DMASYNC_POSTREAD);
600 	if (vr->vr_status == VIRTIO_BLK_S_OK)
601 		r = 0;
602 	else
603 		r = EIO;
604 	virtio_dequeue_commit(vsc, vq, slot);
605 
606 	return r;
607 }
608 
609 static int
610 ld_virtio_detach(device_t self, int flags)
611 {
612 	struct ld_virtio_softc *sc = device_private(self);
613 	struct ld_softc *ld = &sc->sc_ld;
614 	bus_dma_tag_t dmat = virtio_dmat(sc->sc_virtio);
615 	int r, i, qsize;
616 
617 	qsize = sc->sc_vq.vq_num;
618 	r = ldbegindetach(ld, flags);
619 	if (r != 0)
620 		return r;
621 	virtio_reset(sc->sc_virtio);
622 	virtio_free_vq(sc->sc_virtio, &sc->sc_vq);
623 
624 	for (i = 0; i < qsize; i++) {
625 		bus_dmamap_destroy(dmat,
626 				   sc->sc_reqs[i].vr_cmdsts);
627 		bus_dmamap_destroy(dmat,
628 				   sc->sc_reqs[i].vr_payload);
629 	}
630 	bus_dmamem_unmap(dmat, sc->sc_reqs,
631 			 sizeof(struct virtio_blk_req) * qsize);
632 	bus_dmamem_free(dmat, &sc->sc_reqs_seg, 1);
633 
634 	ldenddetach(ld);
635 
636 	cv_destroy(&sc->sc_sync_wait);
637 	mutex_destroy(&sc->sc_sync_wait_lock);
638 
639 	virtio_child_detach(sc->sc_virtio);
640 
641 	return 0;
642 }
643 
644 static int
645 ld_virtio_flush(struct ld_softc *ld, bool poll)
646 {
647 	struct ld_virtio_softc * const sc = device_private(ld->sc_dv);
648 	struct virtio_softc * const vsc = sc->sc_virtio;
649 	const uint64_t features = virtio_features(vsc);
650 	struct virtqueue *vq = &sc->sc_vq;
651 	struct virtio_blk_req *vr;
652 	int slot;
653 	int r;
654 
655 	if ((features & VIRTIO_BLK_F_FLUSH) == 0)
656 		return 0;
657 
658 	mutex_enter(&sc->sc_sync_wait_lock);
659 	while (sc->sc_sync_use != SYNC_FREE) {
660 		if (poll) {
661 			mutex_exit(&sc->sc_sync_wait_lock);
662 			ld_virtio_vq_done(vq);
663 			mutex_enter(&sc->sc_sync_wait_lock);
664 			continue;
665 		}
666 		cv_wait(&sc->sc_sync_wait, &sc->sc_sync_wait_lock);
667 	}
668 	sc->sc_sync_use = SYNC_BUSY;
669 	mutex_exit(&sc->sc_sync_wait_lock);
670 
671 	r = virtio_enqueue_prep(vsc, vq, &slot);
672 	if (r != 0) {
673 		return r;
674 	}
675 
676 	vr = &sc->sc_reqs[slot];
677 	KASSERT(vr->vr_bp == NULL);
678 
679 	r = virtio_enqueue_reserve(vsc, vq, slot, VIRTIO_BLK_CTRL_SEGMENTS);
680 	if (r != 0) {
681 		return r;
682 	}
683 
684 	vr->vr_bp = DUMMY_VR_BP;
685 	vr->vr_hdr.type   = virtio_rw32(vsc, VIRTIO_BLK_T_FLUSH);
686 	vr->vr_hdr.ioprio = virtio_rw32(vsc, 0);
687 	vr->vr_hdr.sector = virtio_rw64(vsc, 0);
688 
689 	bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
690 			0, sizeof(struct virtio_blk_req_hdr),
691 			BUS_DMASYNC_PREWRITE);
692 	bus_dmamap_sync(virtio_dmat(vsc), vr->vr_cmdsts,
693 			offsetof(struct virtio_blk_req, vr_status),
694 			sizeof(uint8_t),
695 			BUS_DMASYNC_PREREAD);
696 
697 	virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
698 			 0, sizeof(struct virtio_blk_req_hdr),
699 			 true);
700 	virtio_enqueue_p(vsc, vq, slot, vr->vr_cmdsts,
701 			 offsetof(struct virtio_blk_req, vr_status),
702 			 sizeof(uint8_t),
703 			 false);
704 	virtio_enqueue_commit(vsc, vq, slot, true);
705 
706 	mutex_enter(&sc->sc_sync_wait_lock);
707 	while (sc->sc_sync_use != SYNC_DONE) {
708 		if (poll) {
709 			mutex_exit(&sc->sc_sync_wait_lock);
710 			ld_virtio_vq_done(vq);
711 			mutex_enter(&sc->sc_sync_wait_lock);
712 			continue;
713 		}
714 		cv_wait(&sc->sc_sync_wait, &sc->sc_sync_wait_lock);
715 	}
716 
717 	if (sc->sc_sync_status == VIRTIO_BLK_S_OK)
718 		r = 0;
719 	else
720 		r = EIO;
721 
722 	sc->sc_sync_use = SYNC_FREE;
723 	cv_broadcast(&sc->sc_sync_wait);
724 	mutex_exit(&sc->sc_sync_wait_lock);
725 
726 	return r;
727 }
728 
729 static int
730 ld_virtio_getcache(struct ld_softc *ld, int *bitsp)
731 {
732 	struct ld_virtio_softc * const sc = device_private(ld->sc_dv);
733 	struct virtio_softc * const vsc = sc->sc_virtio;
734 	const uint64_t features = virtio_features(vsc);
735 
736 	*bitsp = DKCACHE_READ;
737 	if ((features & VIRTIO_BLK_F_CONFIG_WCE) != 0)
738 		*bitsp |= DKCACHE_WCHANGE;
739 	if (virtio_read_device_config_1(vsc,
740 	    VIRTIO_BLK_CONFIG_WRITEBACK) != 0x00)
741 		*bitsp |= DKCACHE_WRITE;
742 
743 	return 0;
744 }
745 
746 static int
747 ld_virtio_setcache(struct ld_softc *ld, int bits)
748 {
749 	struct ld_virtio_softc * const sc = device_private(ld->sc_dv);
750 	struct virtio_softc * const vsc = sc->sc_virtio;
751 	const uint8_t wce = (bits & DKCACHE_WRITE) ? 0x01 : 0x00;
752 
753 	virtio_write_device_config_1(vsc,
754 	    VIRTIO_BLK_CONFIG_WRITEBACK, wce);
755 	if (virtio_read_device_config_1(vsc,
756 	    VIRTIO_BLK_CONFIG_WRITEBACK) != wce)
757 		return EIO;
758 
759 	return 0;
760 }
761 
762 static int
763 ld_virtio_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag, bool poll)
764 {
765 	int error;
766 
767 	switch (cmd) {
768 	case DIOCCACHESYNC:
769 		error = ld_virtio_flush(ld, poll);
770 		break;
771 
772 	case DIOCGCACHE:
773 		error = ld_virtio_getcache(ld, (int *)addr);
774 		break;
775 
776 	case DIOCSCACHE:
777 		error = ld_virtio_setcache(ld, *(int *)addr);
778 		break;
779 
780 	default:
781 		error = EPASSTHROUGH;
782 		break;
783 	}
784 
785 	return error;
786 }
787 
788 MODULE(MODULE_CLASS_DRIVER, ld_virtio, "ld,virtio");
789 
790 #ifdef _MODULE
791 /*
792  * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
793  * XXX it will be defined in the common-code module
794  */
795 #undef  CFDRIVER_DECL
796 #define CFDRIVER_DECL(name, class, attr)
797 #include "ioconf.c"
798 #endif
799 
800 static int
801 ld_virtio_modcmd(modcmd_t cmd, void *opaque)
802 {
803 #ifdef _MODULE
804 	/*
805 	 * We ignore the cfdriver_vec[] that ioconf provides, since
806 	 * the cfdrivers are attached already.
807 	 */
808 	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
809 #endif
810 	int error = 0;
811 
812 #ifdef _MODULE
813 	switch (cmd) {
814 	case MODULE_CMD_INIT:
815 		error = config_init_component(no_cfdriver_vec,
816 		    cfattach_ioconf_ld_virtio, cfdata_ioconf_ld_virtio);
817 		break;
818 	case MODULE_CMD_FINI:
819 		error = config_fini_component(no_cfdriver_vec,
820 		    cfattach_ioconf_ld_virtio, cfdata_ioconf_ld_virtio);
821 		break;
822 	default:
823 		error = ENOTTY;
824 		break;
825 	}
826 #endif
827 
828 	return error;
829 }
830