1 /* Virtio ring implementation.
3 * Copyright 2007 Rusty Russell IBM Corporation
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <linux/virtio.h>
20 #include <linux/virtio_ring.h>
21 #include <linux/device.h>
24 /* For development, we want to crash whenever the ring is screwed. */
25 #define BAD_RING(vq, fmt...) \
26 do { dev_err(&vq->vq.vdev->dev, fmt); BUG(); } while(0)
27 #define START_USE(vq) \
28 do { if ((vq)->in_use) panic("in_use = %i\n", (vq)->in_use); (vq)->in_use = __LINE__; mb(); } while(0)
30 do { BUG_ON(!(vq)->in_use); (vq)->in_use = 0; mb(); } while(0)
32 #define BAD_RING(vq, fmt...) \
33 do { dev_err(&vq->vq.vdev->dev, fmt); (vq)->broken = true; } while(0)
38 struct vring_virtqueue
42 /* Actual memory layout for this queue */
45 /* Other side has made a mess, don't try any more. */
48 /* Number of free buffers */
49 unsigned int num_free;
50 /* Head of free buffer list. */
51 unsigned int free_head;
52 /* Number we've added since last sync. */
53 unsigned int num_added;
55 /* Last used index we've seen. */
58 /* How to notify other side. FIXME: commonalize hcalls! */
59 void (*notify)(struct virtqueue *vq);
62 /* They're supposed to lock for us. */
66 /* Tokens for callbacks. */
70 #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
72 static int vring_add_buf(struct virtqueue *_vq,
73 struct scatterlist sg[],
78 struct vring_virtqueue *vq = to_vvq(_vq);
79 unsigned int i, avail, head, uninitialized_var(prev);
82 BUG_ON(out + in > vq->vring.num);
83 BUG_ON(out + in == 0);
87 if (vq->num_free < out + in) {
88 pr_debug("Can't add buf len %i - avail = %i\n",
89 out + in, vq->num_free);
90 /* We notify *even if* VRING_USED_F_NO_NOTIFY is set here. */
96 /* We're about to use some buffers from the free list. */
97 vq->num_free -= out + in;
100 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
101 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
102 vq->vring.desc[i].addr = (page_to_pfn(sg_page(sg))<<PAGE_SHIFT)
104 vq->vring.desc[i].len = sg->length;
108 for (; in; i = vq->vring.desc[i].next, in--) {
109 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
110 vq->vring.desc[i].addr = (page_to_pfn(sg_page(sg))<<PAGE_SHIFT)
112 vq->vring.desc[i].len = sg->length;
116 /* Last one doesn't continue. */
117 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
119 /* Update free pointer */
123 vq->data[head] = data;
125 /* Put entry in available array (but don't update avail->idx until they
126 * do sync). FIXME: avoid modulus here? */
127 avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
128 vq->vring.avail->ring[avail] = head;
130 pr_debug("Added buffer head %i to %p\n", head, vq);
135 static void vring_kick(struct virtqueue *_vq)
137 struct vring_virtqueue *vq = to_vvq(_vq);
139 /* Descriptors and available array need to be set before we expose the
140 * new available array entries. */
143 vq->vring.avail->idx += vq->num_added;
146 /* Need to update avail index before checking if we should notify */
149 if (!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
150 /* Prod other side to tell it about changes. */
156 static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
160 /* Clear data ptr. */
161 vq->data[head] = NULL;
163 /* Put back on free list: find end */
165 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
166 i = vq->vring.desc[i].next;
170 vq->vring.desc[i].next = vq->free_head;
171 vq->free_head = head;
172 /* Plus final descriptor */
176 static inline bool more_used(const struct vring_virtqueue *vq)
178 return vq->last_used_idx != vq->vring.used->idx;
181 static void *vring_get_buf(struct virtqueue *_vq, unsigned int *len)
183 struct vring_virtqueue *vq = to_vvq(_vq);
189 if (!more_used(vq)) {
190 pr_debug("No more buffers in queue\n");
195 i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
196 *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
198 if (unlikely(i >= vq->vring.num)) {
199 BAD_RING(vq, "id %u out of range\n", i);
202 if (unlikely(!vq->data[i])) {
203 BAD_RING(vq, "id %u is not a head!\n", i);
207 /* detach_buf clears data, so grab it now. */
215 static void vring_disable_cb(struct virtqueue *_vq)
217 struct vring_virtqueue *vq = to_vvq(_vq);
220 BUG_ON(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT);
221 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
225 static bool vring_enable_cb(struct virtqueue *_vq)
227 struct vring_virtqueue *vq = to_vvq(_vq);
230 BUG_ON(!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT));
232 /* We optimistically turn back on interrupts, then check if there was
234 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
236 if (unlikely(more_used(vq))) {
237 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
246 irqreturn_t vring_interrupt(int irq, void *_vq)
248 struct vring_virtqueue *vq = to_vvq(_vq);
250 if (!more_used(vq)) {
251 pr_debug("virtqueue interrupt with no work for %p\n", vq);
255 if (unlikely(vq->broken))
258 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
260 vq->vq.callback(&vq->vq);
265 static struct virtqueue_ops vring_vq_ops = {
266 .add_buf = vring_add_buf,
267 .get_buf = vring_get_buf,
269 .disable_cb = vring_disable_cb,
270 .enable_cb = vring_enable_cb,
273 struct virtqueue *vring_new_virtqueue(unsigned int num,
274 struct virtio_device *vdev,
276 void (*notify)(struct virtqueue *),
277 void (*callback)(struct virtqueue *))
279 struct vring_virtqueue *vq;
282 /* We assume num is a power of 2. */
283 if (num & (num - 1)) {
284 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
288 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
292 vring_init(&vq->vring, num, pages, PAGE_SIZE);
293 vq->vq.callback = callback;
295 vq->vq.vq_ops = &vring_vq_ops;
298 vq->last_used_idx = 0;
304 /* No callback? Tell other side not to bother us. */
306 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
308 /* Put everything in free lists. */
311 for (i = 0; i < num-1; i++)
312 vq->vring.desc[i].next = i+1;
317 void vring_del_virtqueue(struct virtqueue *vq)