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