#ifndef _VIRTIO_H_
#define _VIRTIO_H_
#include <dev/virtio/virtio_endian.h>
#include <dev/virtio/virtio_ids.h>
#include <dev/virtio/virtio_config.h>
#ifdef _KERNEL
struct sbuf;
struct vq_alloc_info;
#define VIRTIO_MAX_INDIRECT ((int) (PAGE_SIZE / 16))
enum {
VIRTIO_IVAR_DEVTYPE = BUS_IVARS_PRIVATE,
VIRTIO_IVAR_FEATURE_DESC,
VIRTIO_IVAR_VENDOR,
VIRTIO_IVAR_DEVICE,
VIRTIO_IVAR_SUBVENDOR,
VIRTIO_IVAR_SUBDEVICE,
VIRTIO_IVAR_MODERN
};
struct virtio_feature_desc {
uint64_t vfd_val;
const char *vfd_str;
};
#define VIRTIO_DRIVER_MODULE(name, driver, evh, arg) \
DRIVER_MODULE(name, virtio_mmio, driver, evh, arg); \
DRIVER_MODULE(name, virtio_pci, driver, evh, arg)
struct virtio_pnp_match {
uint32_t device_type;
const char *description;
};
#define VIRTIO_SIMPLE_PNPINFO(driver, devtype, desc) \
static const struct virtio_pnp_match driver ## _match = { \
.device_type = devtype, \
.description = desc, \
}; \
MODULE_PNP_INFO("U32:device_type;D:#", virtio_mmio, driver, \
&driver ## _match, 1); \
MODULE_PNP_INFO("U32:device_type;D:#", virtio_pci, driver, \
&driver ## _match, 1)
#define VIRTIO_SIMPLE_PROBE(dev, driver) \
(virtio_simple_probe(dev, &driver ## _match))
const char *virtio_device_name(uint16_t devid);
void virtio_describe(device_t dev, const char *msg,
uint64_t features, struct virtio_feature_desc *desc);
int virtio_describe_sbuf(struct sbuf *sb, uint64_t features,
struct virtio_feature_desc *desc);
uint64_t virtio_filter_transport_features(uint64_t features);
bool virtio_bus_is_modern(device_t dev);
void virtio_read_device_config_array(device_t dev, bus_size_t offset,
void *dst, int size, int count);
void virtio_read_ivar(device_t dev, int ivar, uintptr_t *val);
void virtio_write_ivar(device_t dev, int ivar, uintptr_t val);
uint64_t virtio_negotiate_features(device_t dev, uint64_t child_features);
int virtio_finalize_features(device_t dev);
int virtio_alloc_virtqueues(device_t dev, int nvqs,
struct vq_alloc_info *info);
int virtio_setup_intr(device_t dev, enum intr_type type);
bool virtio_with_feature(device_t dev, uint64_t feature);
void virtio_stop(device_t dev);
int virtio_config_generation(device_t dev);
int virtio_reinit(device_t dev, uint64_t features);
void virtio_reinit_complete(device_t dev);
int virtio_child_pnpinfo(device_t busdev, device_t child, struct sbuf *sb);
void virtio_read_device_config(device_t dev, bus_size_t offset,
void *dst, int length);
void virtio_write_device_config(device_t dev, bus_size_t offset,
const void *src, int length);
#define VIRTIO_RDWR_DEVICE_CONFIG(size, type) \
static inline type \
__CONCAT(virtio_read_dev_config_,size)(device_t dev, \
bus_size_t offset) \
{ \
type val; \
virtio_read_device_config(dev, offset, &val, sizeof(type)); \
return (val); \
} \
\
static inline void \
__CONCAT(virtio_write_dev_config_,size)(device_t dev, \
bus_size_t offset, type val) \
{ \
virtio_write_device_config(dev, offset, &val, sizeof(type)); \
}
VIRTIO_RDWR_DEVICE_CONFIG(1, uint8_t);
VIRTIO_RDWR_DEVICE_CONFIG(2, uint16_t);
VIRTIO_RDWR_DEVICE_CONFIG(4, uint32_t);
#undef VIRTIO_RDWR_DEVICE_CONFIG
#define VIRTIO_READ_IVAR(name, ivar) \
static inline int \
__CONCAT(virtio_get_,name)(device_t dev) \
{ \
uintptr_t val; \
virtio_read_ivar(dev, ivar, &val); \
return ((int) val); \
}
VIRTIO_READ_IVAR(device_type, VIRTIO_IVAR_DEVTYPE);
VIRTIO_READ_IVAR(vendor, VIRTIO_IVAR_VENDOR);
VIRTIO_READ_IVAR(device, VIRTIO_IVAR_DEVICE);
VIRTIO_READ_IVAR(subvendor, VIRTIO_IVAR_SUBVENDOR);
VIRTIO_READ_IVAR(subdevice, VIRTIO_IVAR_SUBDEVICE);
VIRTIO_READ_IVAR(modern, VIRTIO_IVAR_MODERN);
#undef VIRTIO_READ_IVAR
#define VIRTIO_WRITE_IVAR(name, ivar) \
static inline void \
__CONCAT(virtio_set_,name)(device_t dev, void *val) \
{ \
virtio_write_ivar(dev, ivar, (uintptr_t) val); \
}
VIRTIO_WRITE_IVAR(feature_desc, VIRTIO_IVAR_FEATURE_DESC);
#undef VIRTIO_WRITE_IVAR
static inline int
virtio_simple_probe(device_t dev, const struct virtio_pnp_match *match)
{
if (virtio_get_device_type(dev) != match->device_type)
return (ENXIO);
device_set_desc(dev, match->description);
return (BUS_PROBE_DEFAULT);
}
#endif
#endif