HelenOS sources
#ifndef _VIRTIO_PCI_H_
#define _VIRTIO_PCI_H_
#include <ddf/driver.h>
#include <pci_dev_iface.h>
#include <ddi.h>
#include <fibril_synch.h>
#define VIRTIO_PCI_CAP_CAP_LEN(c) ((c) + 2)
#define VIRTIO_PCI_CAP_CFG_TYPE(c) ((c) + 3)
#define VIRTIO_PCI_CAP_BAR(c) ((c) + 4)
#define VIRTIO_PCI_CAP_OFFSET(c) ((c) + 8)
#define VIRTIO_PCI_CAP_LENGTH(c) ((c) + 12)
#define VIRTIO_PCI_CAP_END(c) ((c) + 16)
#define VIRTIO_PCI_CAP_COMMON_CFG 1
#define VIRTIO_PCI_CAP_NOTIFY_CFG 2
#define VIRTIO_PCI_CAP_ISR_CFG 3
#define VIRTIO_PCI_CAP_DEVICE_CFG 4
#define VIRTIO_PCI_CAP_PCI_CFG 5
#define VIRTIO_DEV_STATUS_RESET 0
#define VIRTIO_DEV_STATUS_ACKNOWLEDGE 1
#define VIRTIO_DEV_STATUS_DRIVER 2
#define VIRTIO_DEV_STATUS_DRIVER_OK 4
#define VIRTIO_DEV_STATUS_FEATURES_OK 8
#define VIRTIO_DEV_STATUS_DEVICE_NEEDS_RESET 64
#define VIRTIO_DEV_STATUS_FAILED 128
#define VIRTIO_FEATURES_0_31 0
#define VIRTIO_FEATURES_32_63 1
#define VIRTIO_F_VERSION_1 1
typedef struct virtio_pci_common_cfg {
ioport32_t device_feature_select;
const ioport32_t device_feature;
ioport32_t driver_feature_select;
ioport32_t driver_feature;
ioport16_t msix_config;
const ioport16_t num_queues;
ioport8_t device_status;
const ioport8_t config_generation;
ioport16_t queue_select;
ioport16_t queue_size;
ioport16_t queue_msix_vector;
ioport16_t queue_enable;
const ioport16_t queue_notif_off;
ioport64_t queue_desc;
ioport64_t queue_avail;
ioport64_t queue_used;
} virtio_pci_common_cfg_t;
#define VIRTQ_DESC_F_NEXT 1
#define VIRTQ_DESC_F_WRITE 2
#define VIRTQ_DESC_F_INDIRECT 4
typedef struct virtq_desc {
ioport64_t addr;
ioport32_t len;
ioport16_t flags;
ioport16_t next;
} virtq_desc_t;
#define VIRTQ_AVAIL_F_NO_INTERRUPT 1
typedef struct virtq_avail {
ioport16_t flags;
ioport16_t idx;
ioport16_t ring[];
} virtq_avail_t;
typedef struct virtq_used_elem {
ioport32_t id;
ioport32_t len;
} virtq_used_elem_t;
#define VIRTQ_USED_F_NO_NOTIFY 1
typedef struct virtq_used {
ioport16_t flags;
ioport16_t idx;
virtq_used_elem_t ring[];
} virtq_used_t;
typedef struct {
void *virt;
uintptr_t phys;
size_t size;
fibril_mutex_t lock;
size_t queue_size;
virtq_desc_t *desc;
virtq_avail_t *avail;
virtq_used_t *used;
uint16_t used_last_idx;
ioport16_t *notify;
} virtq_t;
typedef struct {
struct {
bool mapped;
uintptr_t phys_base;
void *mapped_base;
size_t mapped_size;
} bar[PCI_BAR_COUNT];
virtio_pci_common_cfg_t *common_cfg;
void *notify_base;
uint32_t notify_off_multiplier;
ioport8_t *isr;
uintptr_t isr_phys;
void *device_cfg;
virtq_t *queues;
} virtio_dev_t;
extern errno_t virtio_setup_dma_bufs(unsigned int, size_t, bool, void *[],
uintptr_t []);
extern void virtio_teardown_dma_bufs(void *[]);
extern void virtio_virtq_desc_set(virtio_dev_t *vdev, uint16_t, uint16_t,
uint64_t, uint32_t, uint16_t, uint16_t);
extern uint16_t virtio_virtq_desc_get_next(virtio_dev_t *vdev, uint16_t,
uint16_t);
extern void virtio_create_desc_free_list(virtio_dev_t *, uint16_t, uint16_t,
uint16_t *);
extern uint16_t virtio_alloc_desc(virtio_dev_t *, uint16_t, uint16_t *);
extern void virtio_free_desc(virtio_dev_t *, uint16_t, uint16_t *, uint16_t);
extern void virtio_virtq_produce_available(virtio_dev_t *, uint16_t, uint16_t);
extern bool virtio_virtq_consume_used(virtio_dev_t *, uint16_t, uint16_t *,
uint32_t *);
extern errno_t virtio_virtq_setup(virtio_dev_t *, uint16_t, uint16_t);
extern void virtio_virtq_teardown(virtio_dev_t *, uint16_t);
extern errno_t virtio_device_setup_start(virtio_dev_t *, uint32_t);
extern void virtio_device_setup_fail(virtio_dev_t *);
extern void virtio_device_setup_finalize(virtio_dev_t *);
extern errno_t virtio_pci_dev_initialize(ddf_dev_t *, virtio_dev_t *);
extern errno_t virtio_pci_dev_cleanup(virtio_dev_t *);
#endif
HelenOS homepage, sources at GitHub