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