Changeset View
Changeset View
Standalone View
Standalone View
usr.sbin/bhyve/pci_virtio_block.c
| Show First 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | |||||
| #include <string.h> | #include <string.h> | ||||
| #include <strings.h> | #include <strings.h> | ||||
| #include <unistd.h> | #include <unistd.h> | ||||
| #include <assert.h> | #include <assert.h> | ||||
| #include <pthread.h> | #include <pthread.h> | ||||
| #include <md5.h> | #include <md5.h> | ||||
| #include "bhyverun.h" | #include "bhyverun.h" | ||||
| #include "config.h" | |||||
| #include "debug.h" | #include "debug.h" | ||||
| #include "pci_emul.h" | #include "pci_emul.h" | ||||
| #include "virtio.h" | #include "virtio.h" | ||||
| #include "block_if.h" | #include "block_if.h" | ||||
| #define VTBLK_BSIZE 512 | #define VTBLK_BSIZE 512 | ||||
| #define VTBLK_RINGSZ 128 | #define VTBLK_RINGSZ 128 | ||||
| ▲ Show 20 Lines • Show All 365 Lines • ▼ Show 20 Lines | |||||
| { | { | ||||
| struct pci_vtblk_softc *sc = vsc; | struct pci_vtblk_softc *sc = vsc; | ||||
| while (vq_has_descs(vq)) | while (vq_has_descs(vq)) | ||||
| pci_vtblk_proc(sc, vq); | pci_vtblk_proc(sc, vq); | ||||
| } | } | ||||
| static int | static int | ||||
| pci_vtblk_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) | pci_vtblk_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl) | ||||
| { | { | ||||
| char bident[sizeof("XX:X:X")]; | char bident[sizeof("XX:X:X")]; | ||||
| struct blockif_ctxt *bctxt; | struct blockif_ctxt *bctxt; | ||||
| const char *path; | |||||
| MD5_CTX mdctx; | MD5_CTX mdctx; | ||||
| u_char digest[16]; | u_char digest[16]; | ||||
| struct pci_vtblk_softc *sc; | struct pci_vtblk_softc *sc; | ||||
| off_t size; | off_t size; | ||||
| int i, sectsz, sts, sto; | int i, sectsz, sts, sto; | ||||
| if (opts == NULL) { | |||||
| WPRINTF(("virtio-block: backing device required")); | |||||
| return (1); | |||||
| } | |||||
| /* | /* | ||||
| * The supplied backing file has to exist | * The supplied backing file has to exist | ||||
| */ | */ | ||||
| snprintf(bident, sizeof(bident), "%d:%d", pi->pi_slot, pi->pi_func); | snprintf(bident, sizeof(bident), "%d:%d", pi->pi_slot, pi->pi_func); | ||||
| bctxt = blockif_open(opts, bident); | bctxt = blockif_open(nvl, bident); | ||||
| if (bctxt == NULL) { | if (bctxt == NULL) { | ||||
| perror("Could not open backing file"); | perror("Could not open backing file"); | ||||
| return (1); | return (1); | ||||
| } | } | ||||
| size = blockif_size(bctxt); | size = blockif_size(bctxt); | ||||
| sectsz = blockif_sectsz(bctxt); | sectsz = blockif_sectsz(bctxt); | ||||
| blockif_psectsz(bctxt, &sts, &sto); | blockif_psectsz(bctxt, &sts, &sto); | ||||
| Show All 20 Lines | pci_vtblk_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl) | ||||
| sc->vbsc_vq.vq_qsize = VTBLK_RINGSZ; | sc->vbsc_vq.vq_qsize = VTBLK_RINGSZ; | ||||
| /* sc->vbsc_vq.vq_notify = we have no per-queue notify */ | /* sc->vbsc_vq.vq_notify = we have no per-queue notify */ | ||||
| /* | /* | ||||
| * Create an identifier for the backing file. Use parts of the | * Create an identifier for the backing file. Use parts of the | ||||
| * md5 sum of the filename | * md5 sum of the filename | ||||
| */ | */ | ||||
| path = get_config_value_node(nvl, "path"); | |||||
| MD5Init(&mdctx); | MD5Init(&mdctx); | ||||
| MD5Update(&mdctx, opts, strlen(opts)); | MD5Update(&mdctx, path, strlen(path)); | ||||
| MD5Final(digest, &mdctx); | MD5Final(digest, &mdctx); | ||||
| snprintf(sc->vbsc_ident, VTBLK_BLK_ID_BYTES, | snprintf(sc->vbsc_ident, VTBLK_BLK_ID_BYTES, | ||||
| "BHYVE-%02X%02X-%02X%02X-%02X%02X", | "BHYVE-%02X%02X-%02X%02X-%02X%02X", | ||||
| digest[0], digest[1], digest[2], digest[3], digest[4], digest[5]); | digest[0], digest[1], digest[2], digest[3], digest[4], digest[5]); | ||||
| /* setup virtio block config space */ | /* setup virtio block config space */ | ||||
| sc->vbsc_cfg.vbc_capacity = size / VTBLK_BSIZE; /* 512-byte units */ | sc->vbsc_cfg.vbc_capacity = size / VTBLK_BSIZE; /* 512-byte units */ | ||||
| sc->vbsc_cfg.vbc_size_max = 0; /* not negotiated */ | sc->vbsc_cfg.vbc_size_max = 0; /* not negotiated */ | ||||
| ▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | pci_vtblk_cfgread(void *vsc, int offset, int size, uint32_t *retval) | ||||
| ptr = (uint8_t *)&sc->vbsc_cfg + offset; | ptr = (uint8_t *)&sc->vbsc_cfg + offset; | ||||
| memcpy(retval, ptr, size); | memcpy(retval, ptr, size); | ||||
| return (0); | return (0); | ||||
| } | } | ||||
| struct pci_devemu pci_de_vblk = { | struct pci_devemu pci_de_vblk = { | ||||
| .pe_emu = "virtio-blk", | .pe_emu = "virtio-blk", | ||||
| .pe_init = pci_vtblk_init, | .pe_init = pci_vtblk_init, | ||||
| .pe_legacy_config = blockif_legacy_config, | |||||
| .pe_barwrite = vi_pci_write, | .pe_barwrite = vi_pci_write, | ||||
| .pe_barread = vi_pci_read, | .pe_barread = vi_pci_read, | ||||
| #ifdef BHYVE_SNAPSHOT | #ifdef BHYVE_SNAPSHOT | ||||
| .pe_snapshot = vi_pci_snapshot, | .pe_snapshot = vi_pci_snapshot, | ||||
| #endif | #endif | ||||
| }; | }; | ||||
| PCI_EMUL_SET(pci_de_vblk); | PCI_EMUL_SET(pci_de_vblk); | ||||