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/virtio_config.h>
22 #include <linux/device.h>
25 /* For development, we want to crash whenever the ring is screwed. */
26 #define BAD_RING(_vq, fmt, args...) \
28 dev_err(&(_vq)->vq.vdev->dev, \
29 "%s:"fmt, (_vq)->vq.name, ##args); \
32 /* Caller is supposed to guarantee no reentry. */
33 #define START_USE(_vq) \
36 panic("%s:in_use = %i\n", \
37 (_vq)->vq.name, (_vq)->in_use); \
38 (_vq)->in_use = __LINE__; \
40 #define END_USE(_vq) \
41 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
43 #define BAD_RING(_vq, fmt, args...) \
45 dev_err(&_vq->vq.vdev->dev, \
46 "%s:"fmt, (_vq)->vq.name, ##args); \
47 (_vq)->broken = true; \
53 struct vring_virtqueue
57 /* Actual memory layout for this queue */
60 /* Other side has made a mess, don't try any more. */
63 /* Host supports indirect buffers */
66 /* Number of free buffers */
67 unsigned int num_free;
68 /* Head of free buffer list. */
69 unsigned int free_head;
70 /* Number we've added since last sync. */
71 unsigned int num_added;
73 /* Last used index we've seen. */
76 /* How to notify other side. FIXME: commonalize hcalls! */
77 void (*notify)(struct virtqueue *vq);
80 /* They're supposed to lock for us. */
84 /* Tokens for callbacks. */
88 #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
90 /* Set up an indirect table of descriptors and add it to the queue. */
91 static int vring_add_indirect(struct vring_virtqueue *vq,
92 struct scatterlist sg[],
96 struct vring_desc *desc;
100 desc = kmalloc((out + in) * sizeof(struct vring_desc), GFP_ATOMIC);
102 return vq->vring.num;
104 /* Transfer entries from the sg list into the indirect page */
105 for (i = 0; i < out; i++) {
106 desc[i].flags = VRING_DESC_F_NEXT;
107 desc[i].addr = sg_phys(sg);
108 desc[i].len = sg->length;
112 for (; i < (out + in); i++) {
113 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
114 desc[i].addr = sg_phys(sg);
115 desc[i].len = sg->length;
120 /* Last one doesn't continue. */
121 desc[i-1].flags &= ~VRING_DESC_F_NEXT;
124 /* We're about to use a buffer */
127 /* Use a single buffer which doesn't continue */
128 head = vq->free_head;
129 vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
130 vq->vring.desc[head].addr = virt_to_phys(desc);
131 vq->vring.desc[head].len = i * sizeof(struct vring_desc);
133 /* Update free pointer */
134 vq->free_head = vq->vring.desc[head].next;
139 static int vring_add_buf(struct virtqueue *_vq,
140 struct scatterlist sg[],
145 struct vring_virtqueue *vq = to_vvq(_vq);
146 unsigned int i, avail, head, uninitialized_var(prev);
150 BUG_ON(data == NULL);
152 /* If the host supports indirect descriptor tables, and we have multiple
153 * buffers, then go indirect. FIXME: tune this threshold */
154 if (vq->indirect && (out + in) > 1 && vq->num_free) {
155 head = vring_add_indirect(vq, sg, out, in);
156 if (head != vq->vring.num)
160 BUG_ON(out + in > vq->vring.num);
161 BUG_ON(out + in == 0);
163 if (vq->num_free < out + in) {
164 pr_debug("Can't add buf len %i - avail = %i\n",
165 out + in, vq->num_free);
166 /* FIXME: for historical reasons, we force a notify here if
167 * there are outgoing parts to the buffer. Presumably the
168 * host should service the ring ASAP. */
175 /* We're about to use some buffers from the free list. */
176 vq->num_free -= out + in;
178 head = vq->free_head;
179 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
180 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
181 vq->vring.desc[i].addr = sg_phys(sg);
182 vq->vring.desc[i].len = sg->length;
186 for (; in; i = vq->vring.desc[i].next, in--) {
187 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
188 vq->vring.desc[i].addr = sg_phys(sg);
189 vq->vring.desc[i].len = sg->length;
193 /* Last one doesn't continue. */
194 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
196 /* Update free pointer */
201 vq->data[head] = data;
203 /* Put entry in available array (but don't update avail->idx until they
204 * do sync). FIXME: avoid modulus here? */
205 avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
206 vq->vring.avail->ring[avail] = head;
208 pr_debug("Added buffer head %i to %p\n", head, vq);
211 /* If we're indirect, we can fit many (assuming not OOM). */
213 return vq->num_free ? vq->vring.num : 0;
217 static void vring_kick(struct virtqueue *_vq)
219 struct vring_virtqueue *vq = to_vvq(_vq);
221 /* Descriptors and available array need to be set before we expose the
222 * new available array entries. */
225 vq->vring.avail->idx += vq->num_added;
228 /* Need to update avail index before checking if we should notify */
231 if (!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
232 /* Prod other side to tell it about changes. */
238 static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
242 /* Clear data ptr. */
243 vq->data[head] = NULL;
245 /* Put back on free list: find end */
248 /* Free the indirect table */
249 if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
250 kfree(phys_to_virt(vq->vring.desc[i].addr));
252 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
253 i = vq->vring.desc[i].next;
257 vq->vring.desc[i].next = vq->free_head;
258 vq->free_head = head;
259 /* Plus final descriptor */
263 static inline bool more_used(const struct vring_virtqueue *vq)
265 return vq->last_used_idx != vq->vring.used->idx;
268 static void *vring_get_buf(struct virtqueue *_vq, unsigned int *len)
270 struct vring_virtqueue *vq = to_vvq(_vq);
276 if (unlikely(vq->broken)) {
281 if (!more_used(vq)) {
282 pr_debug("No more buffers in queue\n");
287 /* Only get used array entries after they have been exposed by host. */
290 i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
291 *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
293 if (unlikely(i >= vq->vring.num)) {
294 BAD_RING(vq, "id %u out of range\n", i);
297 if (unlikely(!vq->data[i])) {
298 BAD_RING(vq, "id %u is not a head!\n", i);
302 /* detach_buf clears data, so grab it now. */
310 static void vring_disable_cb(struct virtqueue *_vq)
312 struct vring_virtqueue *vq = to_vvq(_vq);
314 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
317 static bool vring_enable_cb(struct virtqueue *_vq)
319 struct vring_virtqueue *vq = to_vvq(_vq);
323 /* We optimistically turn back on interrupts, then check if there was
325 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
327 if (unlikely(more_used(vq))) {
336 irqreturn_t vring_interrupt(int irq, void *_vq)
338 struct vring_virtqueue *vq = to_vvq(_vq);
340 if (!more_used(vq)) {
341 pr_debug("virtqueue interrupt with no work for %p\n", vq);
345 if (unlikely(vq->broken))
348 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
350 vq->vq.callback(&vq->vq);
354 EXPORT_SYMBOL_GPL(vring_interrupt);
356 static struct virtqueue_ops vring_vq_ops = {
357 .add_buf = vring_add_buf,
358 .get_buf = vring_get_buf,
360 .disable_cb = vring_disable_cb,
361 .enable_cb = vring_enable_cb,
364 struct virtqueue *vring_new_virtqueue(unsigned int num,
365 unsigned int vring_align,
366 struct virtio_device *vdev,
368 void (*notify)(struct virtqueue *),
369 void (*callback)(struct virtqueue *),
372 struct vring_virtqueue *vq;
375 /* We assume num is a power of 2. */
376 if (num & (num - 1)) {
377 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
381 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
385 vring_init(&vq->vring, num, pages, vring_align);
386 vq->vq.callback = callback;
388 vq->vq.vq_ops = &vring_vq_ops;
392 vq->last_used_idx = 0;
394 list_add_tail(&vq->vq.list, &vdev->vqs);
399 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
401 /* No callback? Tell other side not to bother us. */
403 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
405 /* Put everything in free lists. */
408 for (i = 0; i < num-1; i++)
409 vq->vring.desc[i].next = i+1;
413 EXPORT_SYMBOL_GPL(vring_new_virtqueue);
415 void vring_del_virtqueue(struct virtqueue *vq)
420 EXPORT_SYMBOL_GPL(vring_del_virtqueue);
422 /* Manipulates transport-specific feature bits. */
423 void vring_transport_features(struct virtio_device *vdev)
427 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
429 case VIRTIO_RING_F_INDIRECT_DESC:
432 /* We don't understand this bit. */
433 clear_bit(i, vdev->features);
437 EXPORT_SYMBOL_GPL(vring_transport_features);
439 MODULE_LICENSE("GPL");