[contrib] Allow Network Protocol header to display in rom-o-matic
[gpxe.git] / src / drivers / bus / virtio-ring.c
blob1af0cd21d2b61c609041144d7ed2893c69d42166
1 /* virtio-pci.c - virtio ring management
3 * (c) Copyright 2008 Bull S.A.S.
5 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
7 * some parts from Linux Virtio Ring
9 * Copyright Rusty Russell IBM Corporation 2007
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
17 #include "etherboot.h"
18 #include "gpxe/io.h"
19 #include "gpxe/virtio-ring.h"
20 #include "gpxe/virtio-pci.h"
22 #define BUG() do { \
23 printf("BUG: failure at %s:%d/%s()!\n", \
24 __FILE__, __LINE__, __FUNCTION__); \
25 while(1); \
26 } while (0)
27 #define BUG_ON(condition) do { if (condition) BUG(); } while (0)
30 * vring_free
32 * put at the begin of the free list the current desc[head]
35 void vring_detach(struct vring_virtqueue *vq, unsigned int head)
37 struct vring *vr = &vq->vring;
38 unsigned int i;
40 /* find end of given descriptor */
42 i = head;
43 while (vr->desc[i].flags & VRING_DESC_F_NEXT)
44 i = vr->desc[i].next;
46 /* link it with free list and point to it */
48 vr->desc[i].next = vq->free_head;
49 wmb();
50 vq->free_head = head;
54 * vring_get_buf
56 * get a buffer from the used list
60 void *vring_get_buf(struct vring_virtqueue *vq, unsigned int *len)
62 struct vring *vr = &vq->vring;
63 struct vring_used_elem *elem;
64 u32 id;
65 void *opaque;
67 BUG_ON(!vring_more_used(vq));
69 elem = &vr->used->ring[vq->last_used_idx % vr->num];
70 wmb();
71 id = elem->id;
72 if (len != NULL)
73 *len = elem->len;
75 opaque = vq->vdata[id];
77 vring_detach(vq, id);
79 vq->last_used_idx++;
81 return opaque;
84 void vring_add_buf(struct vring_virtqueue *vq,
85 struct vring_list list[],
86 unsigned int out, unsigned int in,
87 void *opaque, int num_added)
89 struct vring *vr = &vq->vring;
90 int i, avail, head, prev;
92 BUG_ON(out + in == 0);
94 prev = 0;
95 head = vq->free_head;
96 for (i = head; out; i = vr->desc[i].next, out--) {
98 vr->desc[i].flags = VRING_DESC_F_NEXT;
99 vr->desc[i].addr = (u64)virt_to_phys(list->addr);
100 vr->desc[i].len = list->length;
101 prev = i;
102 list++;
104 for ( ; in; i = vr->desc[i].next, in--) {
106 vr->desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
107 vr->desc[i].addr = (u64)virt_to_phys(list->addr);
108 vr->desc[i].len = list->length;
109 prev = i;
110 list++;
112 vr->desc[prev].flags &= ~VRING_DESC_F_NEXT;
114 vq->free_head = i;
116 vq->vdata[head] = opaque;
118 avail = (vr->avail->idx + num_added) % vr->num;
119 vr->avail->ring[avail] = head;
120 wmb();
123 void vring_kick(unsigned int ioaddr, struct vring_virtqueue *vq, int num_added)
125 struct vring *vr = &vq->vring;
127 wmb();
128 vr->avail->idx += num_added;
130 mb();
131 if (!(vr->used->flags & VRING_USED_F_NO_NOTIFY))
132 vp_notify(ioaddr, vq->queue_index);