2011-05-13 04:54:01 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2011 NetApp, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/linker_set.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/ioctl.h>
|
2012-12-08 02:37:18 +00:00
|
|
|
#include <sys/disk.h>
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2012-12-13 01:58:11 +00:00
|
|
|
#include "bhyverun.h"
|
2011-05-13 04:54:01 +00:00
|
|
|
#include "pci_emul.h"
|
|
|
|
#include "virtio.h"
|
|
|
|
|
|
|
|
#define VTBLK_RINGSZ 64
|
|
|
|
|
|
|
|
#define VTBLK_MAXSEGS 32
|
|
|
|
|
|
|
|
#define VTBLK_S_OK 0
|
|
|
|
#define VTBLK_S_IOERR 1
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Host capabilities
|
|
|
|
*/
|
|
|
|
#define VTBLK_S_HOSTCAPS \
|
|
|
|
( 0x00000004 | /* host maximum request segments */ \
|
2013-07-17 23:37:33 +00:00
|
|
|
VIRTIO_RING_F_INDIRECT_DESC ) /* indirect descriptors */
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
/*
|
2013-07-17 23:37:33 +00:00
|
|
|
* Config space "registers"
|
2011-05-13 04:54:01 +00:00
|
|
|
*/
|
|
|
|
struct vtblk_config {
|
|
|
|
uint64_t vbc_capacity;
|
|
|
|
uint32_t vbc_size_max;
|
|
|
|
uint32_t vbc_seg_max;
|
|
|
|
uint16_t vbc_geom_c;
|
|
|
|
uint8_t vbc_geom_h;
|
|
|
|
uint8_t vbc_geom_s;
|
|
|
|
uint32_t vbc_blk_size;
|
|
|
|
uint32_t vbc_sectors_max;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fixed-size block header
|
|
|
|
*/
|
|
|
|
struct virtio_blk_hdr {
|
2013-02-26 20:02:17 +00:00
|
|
|
#define VBH_OP_READ 0
|
|
|
|
#define VBH_OP_WRITE 1
|
|
|
|
#define VBH_FLAG_BARRIER 0x80000000 /* OR'ed into vbh_type */
|
2011-05-13 04:54:01 +00:00
|
|
|
uint32_t vbh_type;
|
|
|
|
uint32_t vbh_ioprio;
|
|
|
|
uint64_t vbh_sector;
|
|
|
|
} __packed;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Debug printf
|
|
|
|
*/
|
|
|
|
static int pci_vtblk_debug;
|
|
|
|
#define DPRINTF(params) if (pci_vtblk_debug) printf params
|
|
|
|
#define WPRINTF(params) printf params
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Per-device softc
|
|
|
|
*/
|
|
|
|
struct pci_vtblk_softc {
|
2013-07-17 23:37:33 +00:00
|
|
|
struct virtio_softc vbsc_vs;
|
|
|
|
struct vqueue_info vbsc_vq;
|
2011-05-13 04:54:01 +00:00
|
|
|
int vbsc_fd;
|
|
|
|
struct vtblk_config vbsc_cfg;
|
|
|
|
};
|
|
|
|
|
2013-07-17 23:37:33 +00:00
|
|
|
static void pci_vtblk_reset(void *);
|
|
|
|
static void pci_vtblk_notify(void *, struct vqueue_info *);
|
|
|
|
static int pci_vtblk_cfgread(void *, int, int, uint32_t *);
|
|
|
|
static int pci_vtblk_cfgwrite(void *, int, int, uint32_t);
|
|
|
|
|
|
|
|
static struct virtio_consts vtblk_vi_consts = {
|
|
|
|
"vtblk", /* our name */
|
|
|
|
1, /* we support 1 virtqueue */
|
|
|
|
sizeof(struct vtblk_config), /* config reg size */
|
|
|
|
pci_vtblk_reset, /* reset */
|
|
|
|
pci_vtblk_notify, /* device-wide qnotify */
|
|
|
|
pci_vtblk_cfgread, /* read PCI config */
|
|
|
|
pci_vtblk_cfgwrite, /* write PCI config */
|
|
|
|
VTBLK_S_HOSTCAPS, /* our capabilities */
|
|
|
|
};
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
static void
|
2013-07-17 23:37:33 +00:00
|
|
|
pci_vtblk_reset(void *vsc)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
2013-07-17 23:37:33 +00:00
|
|
|
struct pci_vtblk_softc *sc = vsc;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2013-07-17 23:37:33 +00:00
|
|
|
DPRINTF(("vtblk: device reset requested !\n"));
|
|
|
|
vi_reset_dev(&sc->vbsc_vs);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-07-17 23:37:33 +00:00
|
|
|
pci_vtblk_proc(struct pci_vtblk_softc *sc, struct vqueue_info *vq)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
|
|
|
struct virtio_blk_hdr *vbh;
|
|
|
|
uint8_t *status;
|
2013-07-17 23:37:33 +00:00
|
|
|
int i, n;
|
2011-05-13 04:54:01 +00:00
|
|
|
int err;
|
|
|
|
int iolen;
|
2013-07-17 23:37:33 +00:00
|
|
|
int writeop, type;
|
2011-05-13 04:54:01 +00:00
|
|
|
off_t offset;
|
2013-07-17 23:37:33 +00:00
|
|
|
struct iovec iov[VTBLK_MAXSEGS + 2];
|
|
|
|
uint16_t flags[VTBLK_MAXSEGS + 2];
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2013-07-17 23:37:33 +00:00
|
|
|
n = vq_getchain(vq, iov, VTBLK_MAXSEGS + 2, flags);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
/*
|
2013-07-17 23:37:33 +00:00
|
|
|
* The first descriptor will be the read-only fixed header,
|
|
|
|
* and the last is for status (hence +2 above and below).
|
|
|
|
* The remaining iov's are the actual data I/O vectors.
|
|
|
|
*
|
|
|
|
* XXX - note - this fails on crash dump, which does a
|
|
|
|
* VIRTIO_BLK_T_FLUSH with a zero transfer length
|
2011-05-13 04:54:01 +00:00
|
|
|
*/
|
2013-07-17 23:37:33 +00:00
|
|
|
assert (n >= 3 && n < VTBLK_MAXSEGS + 2);
|
|
|
|
|
|
|
|
assert((flags[0] & VRING_DESC_F_WRITE) == 0);
|
|
|
|
assert(iov[0].iov_len == sizeof(struct virtio_blk_hdr));
|
|
|
|
vbh = iov[0].iov_base;
|
|
|
|
|
|
|
|
status = iov[--n].iov_base;
|
|
|
|
assert(iov[n].iov_len == 1);
|
|
|
|
assert(flags[n] & VRING_DESC_F_WRITE);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2013-02-26 20:02:17 +00:00
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* The guest should not be setting the BARRIER flag because
|
|
|
|
* we don't advertise the capability.
|
|
|
|
*/
|
|
|
|
type = vbh->vbh_type & ~VBH_FLAG_BARRIER;
|
|
|
|
writeop = (type == VBH_OP_WRITE);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
offset = vbh->vbh_sector * DEV_BSIZE;
|
|
|
|
|
2013-07-17 23:37:33 +00:00
|
|
|
iolen = 0;
|
|
|
|
for (i = 1; i < n; i++) {
|
2011-05-13 04:54:01 +00:00
|
|
|
/*
|
|
|
|
* - write op implies read-only descriptor,
|
|
|
|
* - read op implies write-only descriptor,
|
|
|
|
* therefore test the inverse of the descriptor bit
|
|
|
|
* to the op.
|
|
|
|
*/
|
2013-07-17 23:37:33 +00:00
|
|
|
assert(((flags[i] & VRING_DESC_F_WRITE) == 0) == writeop);
|
|
|
|
iolen += iov[i].iov_len;
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DPRINTF(("virtio-block: %s op, %d bytes, %d segs, offset %ld\n\r",
|
2013-07-17 23:37:33 +00:00
|
|
|
writeop ? "write" : "read", iolen, i - 1, offset));
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2013-04-23 16:40:39 +00:00
|
|
|
if (writeop)
|
2013-07-17 23:37:33 +00:00
|
|
|
err = pwritev(sc->vbsc_fd, iov + 1, i - 1, offset);
|
2013-04-23 16:40:39 +00:00
|
|
|
else
|
2013-07-17 23:37:33 +00:00
|
|
|
err = preadv(sc->vbsc_fd, iov + 1, i - 1, offset);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
*status = err < 0 ? VTBLK_S_IOERR : VTBLK_S_OK;
|
|
|
|
|
|
|
|
/*
|
2013-07-17 23:37:33 +00:00
|
|
|
* Return the descriptor back to the host.
|
|
|
|
* We wrote 1 byte (our status) to host.
|
2011-05-13 04:54:01 +00:00
|
|
|
*/
|
2013-07-17 23:37:33 +00:00
|
|
|
vq_relchain(vq, 1);
|
2013-04-23 16:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-07-17 23:37:33 +00:00
|
|
|
pci_vtblk_notify(void *vsc, struct vqueue_info *vq)
|
2013-04-23 16:40:39 +00:00
|
|
|
{
|
2013-07-17 23:37:33 +00:00
|
|
|
struct pci_vtblk_softc *sc = vsc;
|
2013-04-23 16:40:39 +00:00
|
|
|
|
2013-07-17 23:37:33 +00:00
|
|
|
vq_startchains(vq);
|
|
|
|
while (vq_has_descs(vq))
|
|
|
|
pci_vtblk_proc(sc, vq);
|
|
|
|
vq_endchains(vq, 1); /* Generate interrupt if appropriate. */
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
pci_vtblk_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
|
|
|
|
{
|
|
|
|
struct stat sbuf;
|
|
|
|
struct pci_vtblk_softc *sc;
|
2012-12-08 02:37:18 +00:00
|
|
|
off_t size;
|
2011-05-13 04:54:01 +00:00
|
|
|
int fd;
|
2012-12-08 02:37:18 +00:00
|
|
|
int sectsz;
|
2013-07-17 23:37:33 +00:00
|
|
|
int use_msix;
|
2013-02-01 16:58:59 +00:00
|
|
|
const char *env_msi;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
if (opts == NULL) {
|
|
|
|
printf("virtio-block: backing device required\n");
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The supplied backing file has to exist
|
|
|
|
*/
|
|
|
|
fd = open(opts, O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("Could not open backing file");
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fstat(fd, &sbuf) < 0) {
|
|
|
|
perror("Could not stat backing file");
|
|
|
|
close(fd);
|
|
|
|
return (1);
|
|
|
|
}
|
2012-12-08 02:37:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Deal with raw devices
|
|
|
|
*/
|
|
|
|
size = sbuf.st_size;
|
|
|
|
sectsz = DEV_BSIZE;
|
|
|
|
if (S_ISCHR(sbuf.st_mode)) {
|
|
|
|
if (ioctl(fd, DIOCGMEDIASIZE, &size) < 0 ||
|
|
|
|
ioctl(fd, DIOCGSECTORSIZE, §sz)) {
|
|
|
|
perror("Could not fetch dev blk/sector size");
|
|
|
|
close(fd);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
assert(size != 0);
|
|
|
|
assert(sectsz != 0);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
sc = malloc(sizeof(struct pci_vtblk_softc));
|
|
|
|
memset(sc, 0, sizeof(struct pci_vtblk_softc));
|
|
|
|
|
2013-07-17 23:37:33 +00:00
|
|
|
/* record fd of storage device/file */
|
2011-05-13 04:54:01 +00:00
|
|
|
sc->vbsc_fd = fd;
|
|
|
|
|
2013-07-17 23:37:33 +00:00
|
|
|
/* init virtio softc and virtqueues */
|
|
|
|
vi_softc_linkup(&sc->vbsc_vs, &vtblk_vi_consts, sc, pi, &sc->vbsc_vq);
|
|
|
|
sc->vbsc_vq.vq_qsize = VTBLK_RINGSZ;
|
|
|
|
/* sc->vbsc_vq.vq_notify = we have no per-queue notify */
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
/* setup virtio block config space */
|
2012-12-08 02:37:18 +00:00
|
|
|
sc->vbsc_cfg.vbc_capacity = size / sectsz;
|
2011-05-13 04:54:01 +00:00
|
|
|
sc->vbsc_cfg.vbc_seg_max = VTBLK_MAXSEGS;
|
2012-12-08 02:37:18 +00:00
|
|
|
sc->vbsc_cfg.vbc_blk_size = sectsz;
|
2011-05-13 04:54:01 +00:00
|
|
|
sc->vbsc_cfg.vbc_size_max = 0; /* not negotiated */
|
|
|
|
sc->vbsc_cfg.vbc_geom_c = 0; /* no geometry */
|
|
|
|
sc->vbsc_cfg.vbc_geom_h = 0;
|
|
|
|
sc->vbsc_cfg.vbc_geom_s = 0;
|
|
|
|
sc->vbsc_cfg.vbc_sectors_max = 0;
|
|
|
|
|
2013-07-17 23:37:33 +00:00
|
|
|
/*
|
|
|
|
* Should we move some of this into virtio.c? Could
|
|
|
|
* have the device, class, and subdev_0 as fields in
|
|
|
|
* the virtio constants structure.
|
|
|
|
*/
|
2011-05-13 04:54:01 +00:00
|
|
|
pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_BLOCK);
|
|
|
|
pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
|
|
|
|
pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_STORAGE);
|
|
|
|
pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_BLOCK);
|
2013-02-01 16:58:59 +00:00
|
|
|
|
2013-07-17 23:37:33 +00:00
|
|
|
use_msix = 1;
|
2013-02-01 16:58:59 +00:00
|
|
|
if ((env_msi = getenv("BHYVE_USE_MSI"))) {
|
|
|
|
if (strcasecmp(env_msi, "yes") == 0)
|
|
|
|
use_msix = 0;
|
|
|
|
}
|
2013-07-17 23:37:33 +00:00
|
|
|
if (vi_intr_init(&sc->vbsc_vs, 1, use_msix))
|
|
|
|
return (1);
|
|
|
|
vi_set_io_bar(&sc->vbsc_vs, 0);
|
2011-05-13 04:54:01 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2013-07-17 23:37:33 +00:00
|
|
|
static int
|
|
|
|
pci_vtblk_cfgwrite(void *vsc, int offset, int size, uint32_t value)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
2012-10-19 18:11:17 +00:00
|
|
|
|
2013-07-17 23:37:33 +00:00
|
|
|
DPRINTF(("vtblk: write to readonly reg %d\n\r", offset));
|
|
|
|
return (1);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 23:37:33 +00:00
|
|
|
static int
|
|
|
|
pci_vtblk_cfgread(void *vsc, int offset, int size, uint32_t *retval)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
2013-07-17 23:37:33 +00:00
|
|
|
struct pci_vtblk_softc *sc = vsc;
|
2011-06-07 18:35:45 +00:00
|
|
|
void *ptr;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2013-07-17 23:37:33 +00:00
|
|
|
/* our caller has already verified offset and size */
|
|
|
|
ptr = (uint8_t *)&sc->vbsc_cfg + offset;
|
|
|
|
memcpy(retval, ptr, size);
|
|
|
|
return (0);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct pci_devemu pci_de_vblk = {
|
2012-10-19 18:11:17 +00:00
|
|
|
.pe_emu = "virtio-blk",
|
|
|
|
.pe_init = pci_vtblk_init,
|
2013-07-17 23:37:33 +00:00
|
|
|
.pe_barwrite = vi_pci_write,
|
|
|
|
.pe_barread = vi_pci_read
|
2011-05-13 04:54:01 +00:00
|
|
|
};
|
|
|
|
PCI_EMUL_SET(pci_de_vblk);
|