iPXE
virtio-ring.h
Go to the documentation of this file.
1 #ifndef _VIRTIO_RING_H_
2 # define _VIRTIO_RING_H_
3 
4 #include <ipxe/virtio-pci.h>
5 #include <ipxe/dma.h>
6 
7 /* Status byte for guest to report progress, and synchronize features. */
8 /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
9 #define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
10 /* We have found a driver for the device. */
11 #define VIRTIO_CONFIG_S_DRIVER 2
12 /* Driver has used its parts of the config, and is happy */
13 #define VIRTIO_CONFIG_S_DRIVER_OK 4
14 /* Driver has finished configuring features */
15 #define VIRTIO_CONFIG_S_FEATURES_OK 8
16 /* We've given up on this device. */
17 #define VIRTIO_CONFIG_S_FAILED 0x80
18 
19 /* Virtio feature flags used to negotiate device and driver features. */
20 /* Can the device handle any descriptor layout? */
21 #define VIRTIO_F_ANY_LAYOUT 27
22 /* v1.0 compliant. */
23 #define VIRTIO_F_VERSION_1 32
24 #define VIRTIO_F_IOMMU_PLATFORM 33
25 
26 #define MAX_QUEUE_NUM (256)
27 
28 #define VRING_DESC_F_NEXT 1
29 #define VRING_DESC_F_WRITE 2
30 
31 #define VRING_AVAIL_F_NO_INTERRUPT 1
32 
33 #define VRING_USED_F_NO_NOTIFY 1
34 
35 struct vring_desc
36 {
41 };
42 
44 {
47  u16 ring[0];
48 };
49 
51 {
54 };
55 
56 struct vring_used
57 {
61 };
62 
63 struct vring {
64  unsigned int num;
65  struct vring_desc *desc;
66  struct vring_avail *avail;
67  struct vring_used *used;
68 };
69 
70 #define vring_size(num) \
71  (((((sizeof(struct vring_desc) * num) + \
72  (sizeof(struct vring_avail) + sizeof(u16) * num)) \
73  + PAGE_MASK) & ~PAGE_MASK) + \
74  (sizeof(struct vring_used) + sizeof(struct vring_used_elem) * num))
75 
77  unsigned char *queue;
78  size_t queue_size;
79  struct dma_mapping map;
80  struct dma_device *dma;
81  struct vring vring;
84  void **vdata;
86  /* PCI */
89 };
90 
91 struct vring_list {
93  unsigned int length;
94 };
95 
96 static inline void vring_init(struct vring *vr,
97  unsigned int num, unsigned char *queue)
98 {
99  unsigned int i;
100  unsigned long pa;
101 
102  vr->num = num;
103 
104  /* physical address of desc must be page aligned */
105 
106  pa = virt_to_phys(queue);
107  pa = (pa + PAGE_MASK) & ~PAGE_MASK;
108  vr->desc = phys_to_virt(pa);
109 
110  vr->avail = (struct vring_avail *)&vr->desc[num];
111 
112  /* physical address of used must be page aligned */
113 
114  pa = virt_to_phys(&vr->avail->ring[num]);
115  pa = (pa + PAGE_MASK) & ~PAGE_MASK;
116  vr->used = phys_to_virt(pa);
117 
118  for (i = 0; i < num - 1; i++)
119  vr->desc[i].next = i + 1;
120  vr->desc[i].next = 0;
121 }
122 
123 static inline void vring_enable_cb(struct vring_virtqueue *vq)
124 {
126 }
127 
128 static inline void vring_disable_cb(struct vring_virtqueue *vq)
129 {
131 }
132 
133 
134 /*
135  * vring_more_used
136  *
137  * is there some used buffers ?
138  *
139  */
140 
141 static inline int vring_more_used(struct vring_virtqueue *vq)
142 {
143  wmb();
144  return vq->last_used_idx != vq->vring.used->idx;
145 }
146 
147 void vring_detach(struct vring_virtqueue *vq, unsigned int head);
148 void *vring_get_buf(struct vring_virtqueue *vq, unsigned int *len);
149 void vring_add_buf(struct vring_virtqueue *vq, struct vring_list list[],
150  unsigned int out, unsigned int in,
151  void *index, int num_added);
152 void vring_kick(struct virtio_pci_modern_device *vdev, unsigned int ioaddr,
153  struct vring_virtqueue *vq, int num_added);
154 
155 #endif /* _VIRTIO_RING_H_ */
uint16_t u16
Definition: stdint.h:21
wmb()
unsigned char * queue
Definition: virtio-ring.h:77
DMA mappings.
static int vring_more_used(struct vring_virtqueue *vq)
Definition: virtio-ring.h:141
struct vring vring
Definition: virtio-ring.h:81
void vring_detach(struct vring_virtqueue *vq, unsigned int head)
Definition: virtio-ring.c:37
__be32 in[4]
Definition: CIB_PRM.h:35
size_t queue_size
Definition: virtio-ring.h:78
struct dma_device * dma
Definition: virtio-ring.h:80
#define PAGE_MASK
Page mask.
Definition: io.h:30
struct vring_avail * avail
Definition: virtio-ring.h:66
struct vring_used_elem ring[]
Definition: virtio-ring.h:60
long index
Definition: bigint.h:61
uint8_t head
Head number.
Definition: int13.h:34
static void vring_disable_cb(struct vring_virtqueue *vq)
Definition: virtio-ring.h:128
static void vring_enable_cb(struct vring_virtqueue *vq)
Definition: virtio-ring.h:123
static void vring_init(struct vring *vr, unsigned int num, unsigned char *queue)
Definition: virtio-ring.h:96
static __always_inline unsigned long virt_to_phys(volatile const void *addr)
Convert virtual address to a physical address.
Definition: uaccess.h:287
uint32_t num
Definition: multiboot.h:12
static __always_inline void * phys_to_virt(unsigned long phys_addr)
Convert physical address to a virtual address.
Definition: uaccess.h:299
static unsigned long ioaddr
Definition: davicom.c:129
uint64_t u64
Definition: stdint.h:25
__be32 out[4]
Definition: CIB_PRM.h:36
u16 ring[0]
Definition: virtio-ring.h:47
physaddr_t addr
Definition: virtio-ring.h:92
struct dma_mapping map
Definition: virtio-ring.h:79
struct virtio_pci_region notification
Definition: virtio-ring.h:88
#define VRING_AVAIL_F_NO_INTERRUPT
Definition: virtio-ring.h:31
unsigned int num
Definition: virtio-ring.h:64
struct virtio_net_hdr_modern * empty_header
Definition: virtio-ring.h:85
unsigned long physaddr_t
Definition: stdint.h:20
struct vring_desc * desc
Definition: virtio-ring.h:65
unsigned int length
Definition: virtio-ring.h:93
void vring_add_buf(struct vring_virtqueue *vq, struct vring_list list[], unsigned int out, unsigned int in, void *index, int num_added)
Definition: virtio-ring.c:86
void vring_kick(struct virtio_pci_modern_device *vdev, unsigned int ioaddr, struct vring_virtqueue *vq, int num_added)
Definition: virtio-ring.c:125
struct vring_used * used
Definition: virtio-ring.h:67
uint16_t queue
Queue ID.
Definition: ena.h:22
A DMA mapping.
Definition: dma.h:32
uint32_t len
Length.
Definition: ena.h:14
uint32_t u32
Definition: stdint.h:23
void * vring_get_buf(struct vring_virtqueue *vq, unsigned int *len)
Definition: virtio-ring.c:62
A DMA-capable device.
Definition: dma.h:47