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