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__; \
41 #define END_USE(_vq) \
42 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; mb(); } while(0)
44 #define BAD_RING(_vq, fmt, args...) \
46 dev_err(&_vq->vq.vdev->dev, \
47 "%s:"fmt, (_vq)->vq.name, ##args); \
48 (_vq)->broken = true; \
54 struct vring_virtqueue
58 /* Actual memory layout for this queue */
61 /* Other side has made a mess, don't try any more. */
64 /* Number of free buffers */
65 unsigned int num_free;
66 /* Head of free buffer list. */
67 unsigned int free_head;
68 /* Number we've added since last sync. */
69 unsigned int num_added;
71 /* Last used index we've seen. */
74 /* How to notify other side. FIXME: commonalize hcalls! */
75 void (*notify)(struct virtqueue *vq);
78 /* They're supposed to lock for us. */
82 /* Tokens for callbacks. */
86 #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
88 static int vring_add_buf(struct virtqueue *_vq,
89 struct scatterlist sg[],
94 struct vring_virtqueue *vq = to_vvq(_vq);
95 unsigned int i, avail, head, uninitialized_var(prev);
98 BUG_ON(out + in > vq->vring.num);
99 BUG_ON(out + in == 0);
103 if (vq->num_free < out + in) {
104 pr_debug("Can't add buf len %i - avail = %i\n",
105 out + in, vq->num_free);
106 /* FIXME: for historical reasons, we force a notify here if
107 * there are outgoing parts to the buffer. Presumably the
108 * host should service the ring ASAP. */
115 /* We're about to use some buffers from the free list. */
116 vq->num_free -= out + in;
118 head = vq->free_head;
119 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
120 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
121 vq->vring.desc[i].addr = sg_phys(sg);
122 vq->vring.desc[i].len = sg->length;
126 for (; in; i = vq->vring.desc[i].next, in--) {
127 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
128 vq->vring.desc[i].addr = sg_phys(sg);
129 vq->vring.desc[i].len = sg->length;
133 /* Last one doesn't continue. */
134 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
136 /* Update free pointer */
140 vq->data[head] = data;
142 /* Put entry in available array (but don't update avail->idx until they
143 * do sync). FIXME: avoid modulus here? */
144 avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
145 vq->vring.avail->ring[avail] = head;
147 pr_debug("Added buffer head %i to %p\n", head, vq);
152 static void vring_kick(struct virtqueue *_vq)
154 struct vring_virtqueue *vq = to_vvq(_vq);
156 /* Descriptors and available array need to be set before we expose the
157 * new available array entries. */
160 vq->vring.avail->idx += vq->num_added;
163 /* Need to update avail index before checking if we should notify */
166 if (!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
167 /* Prod other side to tell it about changes. */
173 static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
177 /* Clear data ptr. */
178 vq->data[head] = NULL;
180 /* Put back on free list: find end */
182 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
183 i = vq->vring.desc[i].next;
187 vq->vring.desc[i].next = vq->free_head;
188 vq->free_head = head;
189 /* Plus final descriptor */
193 static inline bool more_used(const struct vring_virtqueue *vq)
195 return vq->last_used_idx != vq->vring.used->idx;
198 static void *vring_get_buf(struct virtqueue *_vq, unsigned int *len)
200 struct vring_virtqueue *vq = to_vvq(_vq);
206 if (unlikely(vq->broken)) {
211 if (!more_used(vq)) {
212 pr_debug("No more buffers in queue\n");
217 i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
218 *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
220 if (unlikely(i >= vq->vring.num)) {
221 BAD_RING(vq, "id %u out of range\n", i);
224 if (unlikely(!vq->data[i])) {
225 BAD_RING(vq, "id %u is not a head!\n", i);
229 /* detach_buf clears data, so grab it now. */
237 static void vring_disable_cb(struct virtqueue *_vq)
239 struct vring_virtqueue *vq = to_vvq(_vq);
241 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
244 static bool vring_enable_cb(struct virtqueue *_vq)
246 struct vring_virtqueue *vq = to_vvq(_vq);
250 /* We optimistically turn back on interrupts, then check if there was
252 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
254 if (unlikely(more_used(vq))) {
263 irqreturn_t vring_interrupt(int irq, void *_vq)
265 struct vring_virtqueue *vq = to_vvq(_vq);
267 if (!more_used(vq)) {
268 pr_debug("virtqueue interrupt with no work for %p\n", vq);
272 if (unlikely(vq->broken))
275 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
277 vq->vq.callback(&vq->vq);
281 EXPORT_SYMBOL_GPL(vring_interrupt);
283 static struct virtqueue_ops vring_vq_ops = {
284 .add_buf = vring_add_buf,
285 .get_buf = vring_get_buf,
287 .disable_cb = vring_disable_cb,
288 .enable_cb = vring_enable_cb,
291 struct virtqueue *vring_new_virtqueue(unsigned int num,
292 unsigned int vring_align,
293 struct virtio_device *vdev,
295 void (*notify)(struct virtqueue *),
296 void (*callback)(struct virtqueue *),
299 struct vring_virtqueue *vq;
302 /* We assume num is a power of 2. */
303 if (num & (num - 1)) {
304 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
308 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
312 vring_init(&vq->vring, num, pages, vring_align);
313 vq->vq.callback = callback;
315 vq->vq.vq_ops = &vring_vq_ops;
319 vq->last_used_idx = 0;
321 list_add_tail(&vq->vq.list, &vdev->vqs);
326 /* No callback? Tell other side not to bother us. */
328 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
330 /* Put everything in free lists. */
333 for (i = 0; i < num-1; i++)
334 vq->vring.desc[i].next = i+1;
338 EXPORT_SYMBOL_GPL(vring_new_virtqueue);
340 void vring_del_virtqueue(struct virtqueue *vq)
345 EXPORT_SYMBOL_GPL(vring_del_virtqueue);
347 /* Manipulates transport-specific feature bits. */
348 void vring_transport_features(struct virtio_device *vdev)
352 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
355 /* We don't understand this bit. */
356 clear_bit(i, vdev->features);
360 EXPORT_SYMBOL_GPL(vring_transport_features);
362 MODULE_LICENSE("GPL");