xref: /freebsd/usr.sbin/bhyve/pci_virtio_block.c (revision d485c77f203fb0f4cdc08dea5ff81631b51d8809)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 NetApp, Inc.
5  * All rights reserved.
6  * Copyright 2020 Joyent, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/linker_set.h>
37 #include <sys/stat.h>
38 #include <sys/uio.h>
39 #include <sys/ioctl.h>
40 #include <sys/disk.h>
41 
42 #include <machine/vmm_snapshot.h>
43 
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <stdint.h>
49 #include <string.h>
50 #include <strings.h>
51 #include <unistd.h>
52 #include <assert.h>
53 #include <pthread.h>
54 #include <md5.h>
55 
56 #include "bhyverun.h"
57 #include "debug.h"
58 #include "pci_emul.h"
59 #include "virtio.h"
60 #include "block_if.h"
61 
62 #define	VTBLK_BSIZE	512
63 #define	VTBLK_RINGSZ	128
64 
65 _Static_assert(VTBLK_RINGSZ <= BLOCKIF_RING_MAX, "Each ring entry must be able to queue a request");
66 
67 #define	VTBLK_S_OK	0
68 #define	VTBLK_S_IOERR	1
69 #define	VTBLK_S_UNSUPP	2
70 
71 #define	VTBLK_BLK_ID_BYTES	20 + 1
72 
73 /* Capability bits */
74 #define	VTBLK_F_BARRIER		(1 << 0)	/* Does host support barriers? */
75 #define	VTBLK_F_SIZE_MAX	(1 << 1)	/* Indicates maximum segment size */
76 #define	VTBLK_F_SEG_MAX		(1 << 2)	/* Indicates maximum # of segments */
77 #define	VTBLK_F_GEOMETRY	(1 << 4)	/* Legacy geometry available  */
78 #define	VTBLK_F_RO		(1 << 5)	/* Disk is read-only */
79 #define	VTBLK_F_BLK_SIZE	(1 << 6)	/* Block size of disk is available*/
80 #define	VTBLK_F_SCSI		(1 << 7)	/* Supports scsi command passthru */
81 #define	VTBLK_F_FLUSH		(1 << 9)	/* Writeback mode enabled after reset */
82 #define	VTBLK_F_WCE		(1 << 9)	/* Legacy alias for FLUSH */
83 #define	VTBLK_F_TOPOLOGY	(1 << 10)	/* Topology information is available */
84 #define	VTBLK_F_CONFIG_WCE	(1 << 11)	/* Writeback mode available in config */
85 #define	VTBLK_F_MQ		(1 << 12)	/* Multi-Queue */
86 #define	VTBLK_F_DISCARD		(1 << 13)	/* Trim blocks */
87 #define	VTBLK_F_WRITE_ZEROES	(1 << 14)	/* Write zeros */
88 
89 /*
90  * Host capabilities
91  */
92 #define	VTBLK_S_HOSTCAPS      \
93   ( VTBLK_F_SEG_MAX  |						    \
94     VTBLK_F_BLK_SIZE |						    \
95     VTBLK_F_FLUSH    |						    \
96     VTBLK_F_TOPOLOGY |						    \
97     VIRTIO_RING_F_INDIRECT_DESC )	/* indirect descriptors */
98 
99 /*
100  * The current blockif_delete() interface only allows a single delete
101  * request at a time.
102  */
103 #define	VTBLK_MAX_DISCARD_SEG	1
104 
105 /*
106  * An arbitrary limit to prevent excessive latency due to large
107  * delete requests.
108  */
109 #define	VTBLK_MAX_DISCARD_SECT	((16 << 20) / VTBLK_BSIZE)	/* 16 MiB */
110 
111 /*
112  * Config space "registers"
113  */
114 struct vtblk_config {
115 	uint64_t	vbc_capacity;
116 	uint32_t	vbc_size_max;
117 	uint32_t	vbc_seg_max;
118 	struct {
119 		uint16_t cylinders;
120 		uint8_t heads;
121 		uint8_t sectors;
122 	} vbc_geometry;
123 	uint32_t	vbc_blk_size;
124 	struct {
125 		uint8_t physical_block_exp;
126 		uint8_t alignment_offset;
127 		uint16_t min_io_size;
128 		uint32_t opt_io_size;
129 	} vbc_topology;
130 	uint8_t		vbc_writeback;
131 	uint8_t		unused0[1];
132 	uint16_t	num_queues;
133 	uint32_t	max_discard_sectors;
134 	uint32_t	max_discard_seg;
135 	uint32_t	discard_sector_alignment;
136 	uint32_t	max_write_zeroes_sectors;
137 	uint32_t	max_write_zeroes_seg;
138 	uint8_t		write_zeroes_may_unmap;
139 	uint8_t		unused1[3];
140 } __packed;
141 
142 /*
143  * Fixed-size block header
144  */
145 struct virtio_blk_hdr {
146 #define	VBH_OP_READ		0
147 #define	VBH_OP_WRITE		1
148 #define	VBH_OP_SCSI_CMD		2
149 #define	VBH_OP_SCSI_CMD_OUT	3
150 #define	VBH_OP_FLUSH		4
151 #define	VBH_OP_FLUSH_OUT	5
152 #define	VBH_OP_IDENT		8
153 #define	VBH_OP_DISCARD		11
154 #define	VBH_OP_WRITE_ZEROES	13
155 
156 #define	VBH_FLAG_BARRIER	0x80000000	/* OR'ed into vbh_type */
157 	uint32_t	vbh_type;
158 	uint32_t	vbh_ioprio;
159 	uint64_t	vbh_sector;
160 } __packed;
161 
162 /*
163  * Debug printf
164  */
165 static int pci_vtblk_debug;
166 #define	DPRINTF(params) if (pci_vtblk_debug) PRINTLN params
167 #define	WPRINTF(params) PRINTLN params
168 
169 struct pci_vtblk_ioreq {
170 	struct blockif_req		io_req;
171 	struct pci_vtblk_softc		*io_sc;
172 	uint8_t				*io_status;
173 	uint16_t			io_idx;
174 };
175 
176 struct virtio_blk_discard_write_zeroes {
177 	uint64_t	sector;
178 	uint32_t	num_sectors;
179 	struct {
180 		uint32_t unmap:1;
181 		uint32_t reserved:31;
182 	} flags;
183 };
184 
185 /*
186  * Per-device softc
187  */
188 struct pci_vtblk_softc {
189 	struct virtio_softc vbsc_vs;
190 	pthread_mutex_t vsc_mtx;
191 	struct vqueue_info vbsc_vq;
192 	struct vtblk_config vbsc_cfg;
193 	struct virtio_consts vbsc_consts;
194 	struct blockif_ctxt *bc;
195 	char vbsc_ident[VTBLK_BLK_ID_BYTES];
196 	struct pci_vtblk_ioreq vbsc_ios[VTBLK_RINGSZ];
197 };
198 
199 static void pci_vtblk_reset(void *);
200 static void pci_vtblk_notify(void *, struct vqueue_info *);
201 static int pci_vtblk_cfgread(void *, int, int, uint32_t *);
202 static int pci_vtblk_cfgwrite(void *, int, int, uint32_t);
203 #ifdef BHYVE_SNAPSHOT
204 static void pci_vtblk_pause(void *);
205 static void pci_vtblk_resume(void *);
206 static int pci_vtblk_snapshot(void *, struct vm_snapshot_meta *);
207 #endif
208 
209 static struct virtio_consts vtblk_vi_consts = {
210 	"vtblk",		/* our name */
211 	1,			/* we support 1 virtqueue */
212 	sizeof(struct vtblk_config),	/* config reg size */
213 	pci_vtblk_reset,	/* reset */
214 	pci_vtblk_notify,	/* device-wide qnotify */
215 	pci_vtblk_cfgread,	/* read PCI config */
216 	pci_vtblk_cfgwrite,	/* write PCI config */
217 	NULL,			/* apply negotiated features */
218 	VTBLK_S_HOSTCAPS,	/* our capabilities */
219 #ifdef BHYVE_SNAPSHOT
220 	pci_vtblk_pause,	/* pause blockif threads */
221 	pci_vtblk_resume,	/* resume blockif threads */
222 	pci_vtblk_snapshot,	/* save / restore device state */
223 #endif
224 };
225 
226 static void
227 pci_vtblk_reset(void *vsc)
228 {
229 	struct pci_vtblk_softc *sc = vsc;
230 
231 	DPRINTF(("vtblk: device reset requested !"));
232 	vi_reset_dev(&sc->vbsc_vs);
233 }
234 
235 static void
236 pci_vtblk_done_locked(struct pci_vtblk_ioreq *io, int err)
237 {
238 	struct pci_vtblk_softc *sc = io->io_sc;
239 
240 	/* convert errno into a virtio block error return */
241 	if (err == EOPNOTSUPP || err == ENOSYS)
242 		*io->io_status = VTBLK_S_UNSUPP;
243 	else if (err != 0)
244 		*io->io_status = VTBLK_S_IOERR;
245 	else
246 		*io->io_status = VTBLK_S_OK;
247 
248 	/*
249 	 * Return the descriptor back to the host.
250 	 * We wrote 1 byte (our status) to host.
251 	 */
252 	vq_relchain(&sc->vbsc_vq, io->io_idx, 1);
253 	vq_endchains(&sc->vbsc_vq, 0);
254 }
255 
256 #ifdef BHYVE_SNAPSHOT
257 static void
258 pci_vtblk_pause(void *vsc)
259 {
260 	struct pci_vtblk_softc *sc = vsc;
261 
262 	DPRINTF(("vtblk: device pause requested !\n"));
263 	blockif_pause(sc->bc);
264 }
265 
266 static void
267 pci_vtblk_resume(void *vsc)
268 {
269 	struct pci_vtblk_softc *sc = vsc;
270 
271 	DPRINTF(("vtblk: device resume requested !\n"));
272 	blockif_resume(sc->bc);
273 }
274 
275 static int
276 pci_vtblk_snapshot(void *vsc, struct vm_snapshot_meta *meta)
277 {
278 	int ret;
279 	struct pci_vtblk_softc *sc = vsc;
280 
281 	SNAPSHOT_VAR_OR_LEAVE(sc->vbsc_cfg, meta, ret, done);
282 	SNAPSHOT_BUF_OR_LEAVE(sc->vbsc_ident, sizeof(sc->vbsc_ident),
283 			      meta, ret, done);
284 
285 done:
286 	return (ret);
287 }
288 #endif
289 
290 static void
291 pci_vtblk_done(struct blockif_req *br, int err)
292 {
293 	struct pci_vtblk_ioreq *io = br->br_param;
294 	struct pci_vtblk_softc *sc = io->io_sc;
295 
296 	pthread_mutex_lock(&sc->vsc_mtx);
297 	pci_vtblk_done_locked(io, err);
298 	pthread_mutex_unlock(&sc->vsc_mtx);
299 }
300 
301 static void
302 pci_vtblk_proc(struct pci_vtblk_softc *sc, struct vqueue_info *vq)
303 {
304 	struct virtio_blk_hdr *vbh;
305 	struct pci_vtblk_ioreq *io;
306 	int i, n;
307 	int err;
308 	ssize_t iolen;
309 	int writeop, type;
310 	struct iovec iov[BLOCKIF_IOV_MAX + 2];
311 	uint16_t idx, flags[BLOCKIF_IOV_MAX + 2];
312 	struct virtio_blk_discard_write_zeroes *discard;
313 
314 	n = vq_getchain(vq, &idx, iov, BLOCKIF_IOV_MAX + 2, flags);
315 
316 	/*
317 	 * The first descriptor will be the read-only fixed header,
318 	 * and the last is for status (hence +2 above and below).
319 	 * The remaining iov's are the actual data I/O vectors.
320 	 *
321 	 * XXX - note - this fails on crash dump, which does a
322 	 * VIRTIO_BLK_T_FLUSH with a zero transfer length
323 	 */
324 	assert(n >= 2 && n <= BLOCKIF_IOV_MAX + 2);
325 
326 	io = &sc->vbsc_ios[idx];
327 	assert((flags[0] & VRING_DESC_F_WRITE) == 0);
328 	assert(iov[0].iov_len == sizeof(struct virtio_blk_hdr));
329 	vbh = (struct virtio_blk_hdr *)iov[0].iov_base;
330 	memcpy(&io->io_req.br_iov, &iov[1], sizeof(struct iovec) * (n - 2));
331 	io->io_req.br_iovcnt = n - 2;
332 	io->io_req.br_offset = vbh->vbh_sector * VTBLK_BSIZE;
333 	io->io_status = (uint8_t *)iov[--n].iov_base;
334 	assert(iov[n].iov_len == 1);
335 	assert(flags[n] & VRING_DESC_F_WRITE);
336 
337 	/*
338 	 * XXX
339 	 * The guest should not be setting the BARRIER flag because
340 	 * we don't advertise the capability.
341 	 */
342 	type = vbh->vbh_type & ~VBH_FLAG_BARRIER;
343 	writeop = (type == VBH_OP_WRITE || type == VBH_OP_DISCARD);
344 
345 	iolen = 0;
346 	for (i = 1; i < n; i++) {
347 		/*
348 		 * - write op implies read-only descriptor,
349 		 * - read/ident op implies write-only descriptor,
350 		 * therefore test the inverse of the descriptor bit
351 		 * to the op.
352 		 */
353 		assert(((flags[i] & VRING_DESC_F_WRITE) == 0) == writeop);
354 		iolen += iov[i].iov_len;
355 	}
356 	io->io_req.br_resid = iolen;
357 
358 	DPRINTF(("virtio-block: %s op, %zd bytes, %d segs, offset %ld",
359 		 writeop ? "write/discard" : "read/ident", iolen, i - 1,
360 		 io->io_req.br_offset));
361 
362 	switch (type) {
363 	case VBH_OP_READ:
364 		err = blockif_read(sc->bc, &io->io_req);
365 		break;
366 	case VBH_OP_WRITE:
367 		err = blockif_write(sc->bc, &io->io_req);
368 		break;
369 	case VBH_OP_DISCARD:
370 		/*
371 		 * We currently only support a single request, if the guest
372 		 * has submitted a request that doesn't conform to the
373 		 * requirements, we return a error.
374 		 */
375 		if (iov[1].iov_len != sizeof (*discard)) {
376 			pci_vtblk_done_locked(io, EINVAL);
377 			return;
378 		}
379 
380 		/* The segments to discard are provided rather than data */
381 		discard = (struct virtio_blk_discard_write_zeroes *)
382 		    iov[1].iov_base;
383 
384 		/*
385 		 * virtio v1.1 5.2.6.2:
386 		 * The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP
387 		 * for discard and write zeroes commands if any unknown flag is
388 		 * set. Furthermore, the device MUST set the status byte to
389 		 * VIRTIO_BLK_S_UNSUPP for discard commands if the unmap flag
390 		 * is set.
391 		 *
392 		 * Currently there are no known flags for a DISCARD request.
393 		 */
394 		if (discard->flags.unmap != 0 || discard->flags.reserved != 0) {
395 			pci_vtblk_done_locked(io, ENOTSUP);
396 			return;
397 		}
398 
399 		/* Make sure the request doesn't exceed our size limit */
400 		if (discard->num_sectors > VTBLK_MAX_DISCARD_SECT) {
401 			pci_vtblk_done_locked(io, EINVAL);
402 			return;
403 		}
404 
405 		io->io_req.br_offset = discard->sector * VTBLK_BSIZE;
406 		io->io_req.br_resid = discard->num_sectors * VTBLK_BSIZE;
407 		err = blockif_delete(sc->bc, &io->io_req);
408 		break;
409 	case VBH_OP_FLUSH:
410 	case VBH_OP_FLUSH_OUT:
411 		err = blockif_flush(sc->bc, &io->io_req);
412 		break;
413 	case VBH_OP_IDENT:
414 		/* Assume a single buffer */
415 		/* S/n equal to buffer is not zero-terminated. */
416 		memset(iov[1].iov_base, 0, iov[1].iov_len);
417 		strncpy(iov[1].iov_base, sc->vbsc_ident,
418 		    MIN(iov[1].iov_len, sizeof(sc->vbsc_ident)));
419 		pci_vtblk_done_locked(io, 0);
420 		return;
421 	default:
422 		pci_vtblk_done_locked(io, EOPNOTSUPP);
423 		return;
424 	}
425 	assert(err == 0);
426 }
427 
428 static void
429 pci_vtblk_notify(void *vsc, struct vqueue_info *vq)
430 {
431 	struct pci_vtblk_softc *sc = vsc;
432 
433 	while (vq_has_descs(vq))
434 		pci_vtblk_proc(sc, vq);
435 }
436 
437 static int
438 pci_vtblk_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
439 {
440 	char bident[sizeof("XX:X:X")];
441 	struct blockif_ctxt *bctxt;
442 	MD5_CTX mdctx;
443 	u_char digest[16];
444 	struct pci_vtblk_softc *sc;
445 	off_t size;
446 	int i, sectsz, sts, sto;
447 
448 	if (opts == NULL) {
449 		WPRINTF(("virtio-block: backing device required"));
450 		return (1);
451 	}
452 
453 	/*
454 	 * The supplied backing file has to exist
455 	 */
456 	snprintf(bident, sizeof(bident), "%d:%d", pi->pi_slot, pi->pi_func);
457 	bctxt = blockif_open(opts, bident);
458 	if (bctxt == NULL) {
459 		perror("Could not open backing file");
460 		return (1);
461 	}
462 
463 	size = blockif_size(bctxt);
464 	sectsz = blockif_sectsz(bctxt);
465 	blockif_psectsz(bctxt, &sts, &sto);
466 
467 	sc = calloc(1, sizeof(struct pci_vtblk_softc));
468 	sc->bc = bctxt;
469 	for (i = 0; i < VTBLK_RINGSZ; i++) {
470 		struct pci_vtblk_ioreq *io = &sc->vbsc_ios[i];
471 		io->io_req.br_callback = pci_vtblk_done;
472 		io->io_req.br_param = io;
473 		io->io_sc = sc;
474 		io->io_idx = i;
475 	}
476 
477 	bcopy(&vtblk_vi_consts, &sc->vbsc_consts, sizeof (vtblk_vi_consts));
478 	if (blockif_candelete(sc->bc))
479 		sc->vbsc_consts.vc_hv_caps |= VTBLK_F_DISCARD;
480 
481 	pthread_mutex_init(&sc->vsc_mtx, NULL);
482 
483 	/* init virtio softc and virtqueues */
484 	vi_softc_linkup(&sc->vbsc_vs, &sc->vbsc_consts, sc, pi, &sc->vbsc_vq);
485 	sc->vbsc_vs.vs_mtx = &sc->vsc_mtx;
486 
487 	sc->vbsc_vq.vq_qsize = VTBLK_RINGSZ;
488 	/* sc->vbsc_vq.vq_notify = we have no per-queue notify */
489 
490 	/*
491 	 * Create an identifier for the backing file. Use parts of the
492 	 * md5 sum of the filename
493 	 */
494 	MD5Init(&mdctx);
495 	MD5Update(&mdctx, opts, strlen(opts));
496 	MD5Final(digest, &mdctx);
497 	snprintf(sc->vbsc_ident, VTBLK_BLK_ID_BYTES,
498 	    "BHYVE-%02X%02X-%02X%02X-%02X%02X",
499 	    digest[0], digest[1], digest[2], digest[3], digest[4], digest[5]);
500 
501 	/* setup virtio block config space */
502 	sc->vbsc_cfg.vbc_capacity = size / VTBLK_BSIZE; /* 512-byte units */
503 	sc->vbsc_cfg.vbc_size_max = 0;	/* not negotiated */
504 
505 	/*
506 	 * If Linux is presented with a seg_max greater than the virtio queue
507 	 * size, it can stumble into situations where it violates its own
508 	 * invariants and panics.  For safety, we keep seg_max clamped, paying
509 	 * heed to the two extra descriptors needed for the header and status
510 	 * of a request.
511 	 */
512 	sc->vbsc_cfg.vbc_seg_max = MIN(VTBLK_RINGSZ - 2, BLOCKIF_IOV_MAX);
513 	sc->vbsc_cfg.vbc_geometry.cylinders = 0;	/* no geometry */
514 	sc->vbsc_cfg.vbc_geometry.heads = 0;
515 	sc->vbsc_cfg.vbc_geometry.sectors = 0;
516 	sc->vbsc_cfg.vbc_blk_size = sectsz;
517 	sc->vbsc_cfg.vbc_topology.physical_block_exp =
518 	    (sts > sectsz) ? (ffsll(sts / sectsz) - 1) : 0;
519 	sc->vbsc_cfg.vbc_topology.alignment_offset =
520 	    (sto != 0) ? ((sts - sto) / sectsz) : 0;
521 	sc->vbsc_cfg.vbc_topology.min_io_size = 0;
522 	sc->vbsc_cfg.vbc_topology.opt_io_size = 0;
523 	sc->vbsc_cfg.vbc_writeback = 0;
524 	sc->vbsc_cfg.max_discard_sectors = VTBLK_MAX_DISCARD_SECT;
525 	sc->vbsc_cfg.max_discard_seg = VTBLK_MAX_DISCARD_SEG;
526 	sc->vbsc_cfg.discard_sector_alignment = MAX(sectsz, sts) / VTBLK_BSIZE;
527 
528 	/*
529 	 * Should we move some of this into virtio.c?  Could
530 	 * have the device, class, and subdev_0 as fields in
531 	 * the virtio constants structure.
532 	 */
533 	pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_BLOCK);
534 	pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
535 	pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_STORAGE);
536 	pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_BLOCK);
537 	pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
538 
539 	if (vi_intr_init(&sc->vbsc_vs, 1, fbsdrun_virtio_msix())) {
540 		blockif_close(sc->bc);
541 		free(sc);
542 		return (1);
543 	}
544 	vi_set_io_bar(&sc->vbsc_vs, 0);
545 	return (0);
546 }
547 
548 static int
549 pci_vtblk_cfgwrite(void *vsc, int offset, int size, uint32_t value)
550 {
551 
552 	DPRINTF(("vtblk: write to readonly reg %d", offset));
553 	return (1);
554 }
555 
556 static int
557 pci_vtblk_cfgread(void *vsc, int offset, int size, uint32_t *retval)
558 {
559 	struct pci_vtblk_softc *sc = vsc;
560 	void *ptr;
561 
562 	/* our caller has already verified offset and size */
563 	ptr = (uint8_t *)&sc->vbsc_cfg + offset;
564 	memcpy(retval, ptr, size);
565 	return (0);
566 }
567 
568 struct pci_devemu pci_de_vblk = {
569 	.pe_emu =	"virtio-blk",
570 	.pe_init =	pci_vtblk_init,
571 	.pe_barwrite =	vi_pci_write,
572 	.pe_barread =	vi_pci_read,
573 #ifdef BHYVE_SNAPSHOT
574 	.pe_snapshot =	vi_pci_snapshot,
575 #endif
576 };
577 PCI_EMUL_SET(pci_de_vblk);
578