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