iPXE
Data Structures | Macros | Functions
virtio-ring.h File Reference
#include <ipxe/virtio-pci.h>

Go to the source code of this file.

Data Structures

struct  vring_desc
 
struct  vring_avail
 
struct  vring_used_elem
 
struct  vring_used
 
struct  vring
 
struct  vring_virtqueue
 
struct  vring_list
 

Macros

#define VIRTIO_CONFIG_S_ACKNOWLEDGE   1
 
#define VIRTIO_CONFIG_S_DRIVER   2
 
#define VIRTIO_CONFIG_S_DRIVER_OK   4
 
#define VIRTIO_CONFIG_S_FEATURES_OK   8
 
#define VIRTIO_CONFIG_S_FAILED   0x80
 
#define VIRTIO_F_ANY_LAYOUT   27
 
#define VIRTIO_F_VERSION_1   32
 
#define VIRTIO_F_IOMMU_PLATFORM   33
 
#define MAX_QUEUE_NUM   (256)
 
#define VRING_DESC_F_NEXT   1
 
#define VRING_DESC_F_WRITE   2
 
#define VRING_AVAIL_F_NO_INTERRUPT   1
 
#define VRING_USED_F_NO_NOTIFY   1
 
#define vring_size(num)
 

Functions

static void vring_init (struct vring *vr, unsigned int num, unsigned char *queue)
 
static void vring_enable_cb (struct vring_virtqueue *vq)
 
static void vring_disable_cb (struct vring_virtqueue *vq)
 
static int vring_more_used (struct vring_virtqueue *vq)
 
void vring_detach (struct vring_virtqueue *vq, unsigned int head)
 
void * vring_get_buf (struct vring_virtqueue *vq, unsigned int *len)
 
void vring_add_buf (struct vring_virtqueue *vq, struct vring_list list[], unsigned int out, unsigned int in, void *index, int num_added)
 
void vring_kick (struct virtio_pci_modern_device *vdev, unsigned int ioaddr, struct vring_virtqueue *vq, int num_added)
 

Macro Definition Documentation

◆ VIRTIO_CONFIG_S_ACKNOWLEDGE

#define VIRTIO_CONFIG_S_ACKNOWLEDGE   1

Definition at line 8 of file virtio-ring.h.

◆ VIRTIO_CONFIG_S_DRIVER

#define VIRTIO_CONFIG_S_DRIVER   2

Definition at line 10 of file virtio-ring.h.

◆ VIRTIO_CONFIG_S_DRIVER_OK

#define VIRTIO_CONFIG_S_DRIVER_OK   4

Definition at line 12 of file virtio-ring.h.

◆ VIRTIO_CONFIG_S_FEATURES_OK

#define VIRTIO_CONFIG_S_FEATURES_OK   8

Definition at line 14 of file virtio-ring.h.

◆ VIRTIO_CONFIG_S_FAILED

#define VIRTIO_CONFIG_S_FAILED   0x80

Definition at line 16 of file virtio-ring.h.

◆ VIRTIO_F_ANY_LAYOUT

#define VIRTIO_F_ANY_LAYOUT   27

Definition at line 20 of file virtio-ring.h.

◆ VIRTIO_F_VERSION_1

#define VIRTIO_F_VERSION_1   32

Definition at line 22 of file virtio-ring.h.

◆ VIRTIO_F_IOMMU_PLATFORM

#define VIRTIO_F_IOMMU_PLATFORM   33

Definition at line 23 of file virtio-ring.h.

◆ MAX_QUEUE_NUM

#define MAX_QUEUE_NUM   (256)

Definition at line 25 of file virtio-ring.h.

◆ VRING_DESC_F_NEXT

#define VRING_DESC_F_NEXT   1

Definition at line 27 of file virtio-ring.h.

◆ VRING_DESC_F_WRITE

#define VRING_DESC_F_WRITE   2

Definition at line 28 of file virtio-ring.h.

◆ VRING_AVAIL_F_NO_INTERRUPT

#define VRING_AVAIL_F_NO_INTERRUPT   1

Definition at line 30 of file virtio-ring.h.

◆ VRING_USED_F_NO_NOTIFY

#define VRING_USED_F_NO_NOTIFY   1

Definition at line 32 of file virtio-ring.h.

◆ vring_size

#define vring_size (   num)
Value:
(((((sizeof(struct vring_desc) * num) + \
(sizeof(struct vring_avail) + sizeof(u16) * num)) \
+ PAGE_MASK) & ~PAGE_MASK) + \
(sizeof(struct vring_used) + sizeof(struct vring_used_elem) * num))
#define u16
Definition: vga.h:20
#define PAGE_MASK
Page mask.
Definition: io.h:30
char unsigned long * num
Definition: xenstore.h:17

Definition at line 69 of file virtio-ring.h.

Function Documentation

◆ vring_init()

static void vring_init ( struct vring vr,
unsigned int  num,
unsigned char *  queue 
)
inlinestatic

Definition at line 91 of file virtio-ring.h.

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 }
#define PAGE_MASK
Page mask.
Definition: io.h:30
struct vring_avail * avail
Definition: virtio-ring.h:65
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
u16 ring[0]
Definition: virtio-ring.h:46
char unsigned long * num
Definition: xenstore.h:17
unsigned int num
Definition: virtio-ring.h:63
struct vring_desc * desc
Definition: virtio-ring.h:64
struct vring_used * used
Definition: virtio-ring.h:66
uint16_t queue
Queue ID.
Definition: ena.h:22

References vring::avail, vring::desc, vring_desc::next, num, vring::num, PAGE_MASK, phys_to_virt(), queue, vring_avail::ring, vring::used, and virt_to_phys().

Referenced by vp_find_vq(), and vpm_find_vqs().

◆ vring_enable_cb()

static void vring_enable_cb ( struct vring_virtqueue vq)
inlinestatic

Definition at line 118 of file virtio-ring.h.

119 {
121 }
struct vring vring
Definition: virtio-ring.h:77
struct vring_avail * avail
Definition: virtio-ring.h:65
#define VRING_AVAIL_F_NO_INTERRUPT
Definition: virtio-ring.h:30

References vring::avail, vring_avail::flags, vring_virtqueue::vring, and VRING_AVAIL_F_NO_INTERRUPT.

Referenced by virtnet_irq().

◆ vring_disable_cb()

static void vring_disable_cb ( struct vring_virtqueue vq)
inlinestatic

Definition at line 123 of file virtio-ring.h.

124 {
126 }
struct vring vring
Definition: virtio-ring.h:77
struct vring_avail * avail
Definition: virtio-ring.h:65
#define VRING_AVAIL_F_NO_INTERRUPT
Definition: virtio-ring.h:30

References vring::avail, vring_avail::flags, vring_virtqueue::vring, and VRING_AVAIL_F_NO_INTERRUPT.

Referenced by virtnet_irq().

◆ vring_more_used()

static int vring_more_used ( struct vring_virtqueue vq)
inlinestatic

Definition at line 136 of file virtio-ring.h.

137 {
138  wmb();
139  return vq->last_used_idx != vq->vring.used->idx;
140 }
wmb()
struct vring vring
Definition: virtio-ring.h:77
struct vring_used * used
Definition: virtio-ring.h:66

References vring_used::idx, vring_virtqueue::last_used_idx, vring::used, vring_virtqueue::vring, and wmb().

Referenced by virtnet_process_rx_packets(), virtnet_process_tx_packets(), and vring_get_buf().

◆ vring_detach()

void vring_detach ( struct vring_virtqueue vq,
unsigned int  head 
)

Definition at line 37 of file virtio-ring.c.

38 {
39  struct vring *vr = &vq->vring;
40  unsigned int i;
41 
42  /* find end of given descriptor */
43 
44  i = head;
45  while (vr->desc[i].flags & VRING_DESC_F_NEXT)
46  i = vr->desc[i].next;
47 
48  /* link it with free list and point to it */
49 
50  vr->desc[i].next = vq->free_head;
51  wmb();
52  vq->free_head = head;
53 }
wmb()
struct vring vring
Definition: virtio-ring.h:77
uint8_t head
Head number.
Definition: int13.h:34
struct vring_desc * desc
Definition: virtio-ring.h:64
#define VRING_DESC_F_NEXT
Definition: virtio-ring.h:27

References vring::desc, vring_desc::flags, vring_virtqueue::free_head, head, vring_desc::next, vring_virtqueue::vring, VRING_DESC_F_NEXT, and wmb().

Referenced by vring_get_buf().

◆ vring_get_buf()

void* vring_get_buf ( struct vring_virtqueue vq,
unsigned int *  len 
)

Definition at line 62 of file virtio-ring.c.

63 {
64  struct vring *vr = &vq->vring;
65  struct vring_used_elem *elem;
66  u32 id;
67  void *opaque;
68 
69  BUG_ON(!vring_more_used(vq));
70 
71  elem = &vr->used->ring[vq->last_used_idx % vr->num];
72  wmb();
73  id = elem->id;
74  if (len != NULL)
75  *len = elem->len;
76 
77  opaque = vq->vdata[id];
78 
79  vring_detach(vq, id);
80 
81  vq->last_used_idx++;
82 
83  return opaque;
84 }
wmb()
static int vring_more_used(struct vring_virtqueue *vq)
Definition: virtio-ring.h:136
struct vring vring
Definition: virtio-ring.h:77
#define BUG_ON(condition)
Definition: virtio-ring.c:29
struct vring_used_elem ring[]
Definition: virtio-ring.h:59
void vring_detach(struct vring_virtqueue *vq, unsigned int head)
Definition: virtio-ring.c:37
uint8_t id
Request identifier.
Definition: ena.h:12
unsigned int num
Definition: virtio-ring.h:63
uint32_t len
Length.
Definition: ena.h:14
struct vring_used * used
Definition: virtio-ring.h:66
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
uint32_t u32
Definition: stdint.h:23

References BUG_ON, id, vring_used_elem::id, vring_virtqueue::last_used_idx, len, vring_used_elem::len, NULL, vring::num, vring_used::ring, vring::used, vring_virtqueue::vdata, vring_virtqueue::vring, vring_detach(), vring_more_used(), and wmb().

Referenced by virtnet_process_rx_packets(), and virtnet_process_tx_packets().

◆ vring_add_buf()

void vring_add_buf ( struct vring_virtqueue vq,
struct vring_list  list[],
unsigned int  out,
unsigned int  in,
void *  index,
int  num_added 
)

Definition at line 86 of file virtio-ring.c.

90 {
91  struct vring *vr = &vq->vring;
92  int i, avail, head, prev;
93 
94  BUG_ON(out + in == 0);
95 
96  prev = 0;
97  head = vq->free_head;
98  for (i = head; out; i = vr->desc[i].next, out--) {
99 
100  vr->desc[i].flags = VRING_DESC_F_NEXT;
101  vr->desc[i].addr = (u64)virt_to_phys(list->addr);
102  vr->desc[i].len = list->length;
103  prev = i;
104  list++;
105  }
106  for ( ; in; i = vr->desc[i].next, in--) {
107 
109  vr->desc[i].addr = (u64)virt_to_phys(list->addr);
110  vr->desc[i].len = list->length;
111  prev = i;
112  list++;
113  }
114  vr->desc[prev].flags &= ~VRING_DESC_F_NEXT;
115 
116  vq->free_head = i;
117 
118  vq->vdata[head] = opaque;
119 
120  avail = (vr->avail->idx + num_added) % vr->num;
121  vr->avail->ring[avail] = head;
122  wmb();
123 }
wmb()
struct vring vring
Definition: virtio-ring.h:77
__be32 in[4]
Definition: CIB_PRM.h:35
#define BUG_ON(condition)
Definition: virtio-ring.c:29
#define VRING_DESC_F_WRITE
Definition: virtio-ring.h:28
struct vring_avail * avail
Definition: virtio-ring.h:65
uint8_t head
Head number.
Definition: int13.h:34
static __always_inline unsigned long virt_to_phys(volatile const void *addr)
Convert virtual address to a physical address.
Definition: uaccess.h:287
uint64_t u64
Definition: stdint.h:25
__be32 out[4]
Definition: CIB_PRM.h:36
u16 ring[0]
Definition: virtio-ring.h:46
unsigned int num
Definition: virtio-ring.h:63
struct vring_desc * desc
Definition: virtio-ring.h:64
#define VRING_DESC_F_NEXT
Definition: virtio-ring.h:27
unsigned int length
Definition: virtio-ring.h:88
char * addr
Definition: virtio-ring.h:87

References vring_desc::addr, vring_list::addr, vring::avail, BUG_ON, vring::desc, vring_desc::flags, vring_virtqueue::free_head, head, vring_avail::idx, in, vring_desc::len, vring_list::length, vring_desc::next, vring::num, out, vring_avail::ring, vring_virtqueue::vdata, virt_to_phys(), vring_virtqueue::vring, VRING_DESC_F_NEXT, VRING_DESC_F_WRITE, and wmb().

Referenced by virtnet_enqueue_iob().

◆ vring_kick()

void vring_kick ( struct virtio_pci_modern_device vdev,
unsigned int  ioaddr,
struct vring_virtqueue vq,
int  num_added 
)

Definition at line 125 of file virtio-ring.c.

127 {
128  struct vring *vr = &vq->vring;
129 
130  wmb();
131  vr->avail->idx += num_added;
132 
133  mb();
134  if (!(vr->used->flags & VRING_USED_F_NO_NOTIFY)) {
135  if (vdev) {
136  /* virtio 1.0 */
137  vpm_notify(vdev, vq);
138  } else {
139  /* legacy virtio */
141  }
142  }
143 }
wmb()
struct vring vring
Definition: virtio-ring.h:77
struct vring_avail * avail
Definition: virtio-ring.h:65
static void vp_notify(unsigned int ioaddr, int queue_index)
Definition: virtio-pci.h:181
void vpm_notify(struct virtio_pci_modern_device *vdev, struct vring_virtqueue *vq)
Definition: virtio-pci.c:344
static unsigned long ioaddr
Definition: davicom.c:129
#define VRING_USED_F_NO_NOTIFY
Definition: virtio-ring.h:32
void mb(void)
Memory barrier.
struct vring_used * used
Definition: virtio-ring.h:66

References vring::avail, vring_used::flags, vring_avail::idx, ioaddr, mb(), vring_virtqueue::queue_index, vring::used, vp_notify(), vpm_notify(), vring_virtqueue::vring, VRING_USED_F_NO_NOTIFY, and wmb().

Referenced by virtnet_enqueue_iob().