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