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>
23 #include <linux/slab.h>
24 #include <linux/module.h>
26 /* virtio guest is communicating with a virtual "device" that actually runs on
27 * a host processor. Memory barriers are used to control SMP effects. */
29 /* Where possible, use SMP barriers which are more lightweight than mandatory
30 * barriers, because mandatory barriers control MMIO effects on accesses
31 * through relaxed memory I/O windows (which virtio does not use). */
32 #define virtio_mb() smp_mb()
33 #define virtio_rmb() smp_rmb()
34 #define virtio_wmb() smp_wmb()
36 /* We must force memory ordering even if guest is UP since host could be
37 * running on another CPU, but SMP barriers are defined to barrier() in that
38 * configuration. So fall back to mandatory barriers instead. */
39 #define virtio_mb() mb()
40 #define virtio_rmb() rmb()
41 #define virtio_wmb() wmb()
45 /* For development, we want to crash whenever the ring is screwed. */
46 #define BAD_RING(_vq, fmt, args...) \
48 dev_err(&(_vq)->vq.vdev->dev, \
49 "%s:"fmt, (_vq)->vq.name, ##args); \
52 /* Caller is supposed to guarantee no reentry. */
53 #define START_USE(_vq) \
56 panic("%s:in_use = %i\n", \
57 (_vq)->vq.name, (_vq)->in_use); \
58 (_vq)->in_use = __LINE__; \
60 #define END_USE(_vq) \
61 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
63 #define BAD_RING(_vq, fmt, args...) \
65 dev_err(&_vq->vq.vdev->dev, \
66 "%s:"fmt, (_vq)->vq.name, ##args); \
67 (_vq)->broken = true; \
73 struct vring_virtqueue
77 /* Actual memory layout for this queue */
80 /* Other side has made a mess, don't try any more. */
83 /* Host supports indirect buffers */
86 /* Host publishes avail event idx */
89 /* Number of free buffers */
90 unsigned int num_free;
91 /* Head of free buffer list. */
92 unsigned int free_head;
93 /* Number we've added since last sync. */
94 unsigned int num_added;
96 /* Last used index we've seen. */
99 /* How to notify other side. FIXME: commonalize hcalls! */
100 void (*notify)(struct virtqueue *vq);
103 /* They're supposed to lock for us. */
107 /* Tokens for callbacks. */
111 #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
113 /* Set up an indirect table of descriptors and add it to the queue. */
114 static int vring_add_indirect(struct vring_virtqueue *vq,
115 struct scatterlist sg[],
120 struct vring_desc *desc;
125 * We require lowmem mappings for the descriptors because
126 * otherwise virt_to_phys will give us bogus addresses in the
129 gfp &= ~(__GFP_HIGHMEM | __GFP_HIGH);
131 desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp);
135 /* Transfer entries from the sg list into the indirect page */
136 for (i = 0; i < out; i++) {
137 desc[i].flags = VRING_DESC_F_NEXT;
138 desc[i].addr = sg_phys(sg);
139 desc[i].len = sg->length;
143 for (; i < (out + in); i++) {
144 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
145 desc[i].addr = sg_phys(sg);
146 desc[i].len = sg->length;
151 /* Last one doesn't continue. */
152 desc[i-1].flags &= ~VRING_DESC_F_NEXT;
155 /* We're about to use a buffer */
158 /* Use a single buffer which doesn't continue */
159 head = vq->free_head;
160 vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
161 vq->vring.desc[head].addr = virt_to_phys(desc);
162 vq->vring.desc[head].len = i * sizeof(struct vring_desc);
164 /* Update free pointer */
165 vq->free_head = vq->vring.desc[head].next;
170 int virtqueue_add_buf_gfp(struct virtqueue *_vq,
171 struct scatterlist sg[],
177 struct vring_virtqueue *vq = to_vvq(_vq);
178 unsigned int i, avail, uninitialized_var(prev);
183 BUG_ON(data == NULL);
185 /* If the host supports indirect descriptor tables, and we have multiple
186 * buffers, then go indirect. FIXME: tune this threshold */
187 if (vq->indirect && (out + in) > 1 && vq->num_free) {
188 head = vring_add_indirect(vq, sg, out, in, gfp);
189 if (likely(head >= 0))
193 BUG_ON(out + in > vq->vring.num);
194 BUG_ON(out + in == 0);
196 if (vq->num_free < out + in) {
197 pr_debug("Can't add buf len %i - avail = %i\n",
198 out + in, vq->num_free);
199 /* FIXME: for historical reasons, we force a notify here if
200 * there are outgoing parts to the buffer. Presumably the
201 * host should service the ring ASAP. */
208 /* We're about to use some buffers from the free list. */
209 vq->num_free -= out + in;
211 head = vq->free_head;
212 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
213 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
214 vq->vring.desc[i].addr = sg_phys(sg);
215 vq->vring.desc[i].len = sg->length;
219 for (; in; i = vq->vring.desc[i].next, in--) {
220 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
221 vq->vring.desc[i].addr = sg_phys(sg);
222 vq->vring.desc[i].len = sg->length;
226 /* Last one doesn't continue. */
227 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
229 /* Update free pointer */
234 vq->data[head] = data;
236 /* Put entry in available array (but don't update avail->idx until they
237 * do sync). FIXME: avoid modulus here? */
238 avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
239 vq->vring.avail->ring[avail] = head;
241 pr_debug("Added buffer head %i to %p\n", head, vq);
246 EXPORT_SYMBOL_GPL(virtqueue_add_buf_gfp);
248 void virtqueue_kick(struct virtqueue *_vq)
250 struct vring_virtqueue *vq = to_vvq(_vq);
253 /* Descriptors and available array need to be set before we expose the
254 * new available array entries. */
257 old = vq->vring.avail->idx;
258 new = vq->vring.avail->idx = old + vq->num_added;
261 /* Need to update avail index before checking if we should notify */
265 vring_need_event(vring_avail_event(&vq->vring), new, old) :
266 !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
267 /* Prod other side to tell it about changes. */
272 EXPORT_SYMBOL_GPL(virtqueue_kick);
274 static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
278 /* Clear data ptr. */
279 vq->data[head] = NULL;
281 /* Put back on free list: find end */
284 /* Free the indirect table */
285 if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
286 kfree(phys_to_virt(vq->vring.desc[i].addr));
288 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
289 i = vq->vring.desc[i].next;
293 vq->vring.desc[i].next = vq->free_head;
294 vq->free_head = head;
295 /* Plus final descriptor */
299 static inline bool more_used(const struct vring_virtqueue *vq)
301 return vq->last_used_idx != vq->vring.used->idx;
304 void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
306 struct vring_virtqueue *vq = to_vvq(_vq);
312 if (unlikely(vq->broken)) {
317 if (!more_used(vq)) {
318 pr_debug("No more buffers in queue\n");
323 /* Only get used array entries after they have been exposed by host. */
326 i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
327 *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
329 if (unlikely(i >= vq->vring.num)) {
330 BAD_RING(vq, "id %u out of range\n", i);
333 if (unlikely(!vq->data[i])) {
334 BAD_RING(vq, "id %u is not a head!\n", i);
338 /* detach_buf clears data, so grab it now. */
342 /* If we expect an interrupt for the next entry, tell host
343 * by writing event index and flush out the write before
344 * the read in the next get_buf call. */
345 if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
346 vring_used_event(&vq->vring) = vq->last_used_idx;
353 EXPORT_SYMBOL_GPL(virtqueue_get_buf);
355 void virtqueue_disable_cb(struct virtqueue *_vq)
357 struct vring_virtqueue *vq = to_vvq(_vq);
359 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
361 EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
364 * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
365 * @vq: the struct virtqueue we're talking about.
367 * This re-enables callbacks; it returns current queue state
368 * in an opaque unsigned value. This value should be later tested by
369 * virtqueue_poll, to detect a possible race between the driver checking for
370 * more work, and enabling callbacks.
372 * Caller must ensure we don't call this with other virtqueue
373 * operations at the same time (except where noted).
375 unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
377 struct vring_virtqueue *vq = to_vvq(_vq);
382 /* We optimistically turn back on interrupts, then check if there was
384 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
385 * either clear the flags bit or point the event index at the next
386 * entry. Always do both to keep code simple. */
387 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
388 vring_used_event(&vq->vring) = last_used_idx = vq->last_used_idx;
390 return last_used_idx;
392 EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
395 * virtqueue_poll - query pending used buffers
396 * @vq: the struct virtqueue we're talking about.
397 * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
399 * Returns "true" if there are pending used buffers in the queue.
401 * This does not need to be serialized.
403 bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
405 struct vring_virtqueue *vq = to_vvq(_vq);
408 return (u16)last_used_idx != vq->vring.used->idx;
410 EXPORT_SYMBOL_GPL(virtqueue_poll);
413 * virtqueue_enable_cb - restart callbacks after disable_cb.
414 * @vq: the struct virtqueue we're talking about.
416 * This re-enables callbacks; it returns "false" if there are pending
417 * buffers in the queue, to detect a possible race between the driver
418 * checking for more work, and enabling callbacks.
420 * Caller must ensure we don't call this with other virtqueue
421 * operations at the same time (except where noted).
423 bool virtqueue_enable_cb(struct virtqueue *_vq)
425 unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
426 return !virtqueue_poll(_vq, last_used_idx);
428 EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
430 bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
432 struct vring_virtqueue *vq = to_vvq(_vq);
437 /* We optimistically turn back on interrupts, then check if there was
439 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
440 * either clear the flags bit or point the event index at the next
441 * entry. Always do both to keep code simple. */
442 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
443 /* TODO: tune this threshold */
444 bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
445 vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
447 if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
455 EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
457 void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
459 struct vring_virtqueue *vq = to_vvq(_vq);
465 for (i = 0; i < vq->vring.num; i++) {
468 /* detach_buf clears data, so grab it now. */
471 vq->vring.avail->idx--;
475 /* That should have freed everything. */
476 BUG_ON(vq->num_free != vq->vring.num);
481 EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
483 irqreturn_t vring_interrupt(int irq, void *_vq)
485 struct vring_virtqueue *vq = to_vvq(_vq);
487 if (!more_used(vq)) {
488 pr_debug("virtqueue interrupt with no work for %p\n", vq);
492 if (unlikely(vq->broken))
495 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
497 vq->vq.callback(&vq->vq);
501 EXPORT_SYMBOL_GPL(vring_interrupt);
503 struct virtqueue *vring_new_virtqueue(unsigned int num,
504 unsigned int vring_align,
505 struct virtio_device *vdev,
507 void (*notify)(struct virtqueue *),
508 void (*callback)(struct virtqueue *),
511 struct vring_virtqueue *vq;
514 /* We assume num is a power of 2. */
515 if (num & (num - 1)) {
516 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
520 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
524 vring_init(&vq->vring, num, pages, vring_align);
525 vq->vq.callback = callback;
530 vq->last_used_idx = 0;
532 list_add_tail(&vq->vq.list, &vdev->vqs);
537 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
538 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
540 /* No callback? Tell other side not to bother us. */
542 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
544 /* Put everything in free lists. */
547 for (i = 0; i < num-1; i++) {
548 vq->vring.desc[i].next = i+1;
555 EXPORT_SYMBOL_GPL(vring_new_virtqueue);
557 void vring_del_virtqueue(struct virtqueue *vq)
562 EXPORT_SYMBOL_GPL(vring_del_virtqueue);
564 /* Manipulates transport-specific feature bits. */
565 void vring_transport_features(struct virtio_device *vdev)
569 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
571 case VIRTIO_RING_F_INDIRECT_DESC:
573 case VIRTIO_RING_F_EVENT_IDX:
576 /* We don't understand this bit. */
577 clear_bit(i, vdev->features);
581 EXPORT_SYMBOL_GPL(vring_transport_features);
583 /* return the size of the vring within the virtqueue */
584 unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
587 struct vring_virtqueue *vq = to_vvq(_vq);
589 return vq->vring.num;
591 EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
593 MODULE_LICENSE("GPL");