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>
25 /* virtio guest is communicating with a virtual "device" that actually runs on
26 * a host processor. Memory barriers are used to control SMP effects. */
28 /* Where possible, use SMP barriers which are more lightweight than mandatory
29 * barriers, because mandatory barriers control MMIO effects on accesses
30 * through relaxed memory I/O windows (which virtio does not use). */
31 #define virtio_mb() smp_mb()
32 #define virtio_rmb() smp_rmb()
33 #define virtio_wmb() smp_wmb()
35 /* We must force memory ordering even if guest is UP since host could be
36 * running on another CPU, but SMP barriers are defined to barrier() in that
37 * configuration. So fall back to mandatory barriers instead. */
38 #define virtio_mb() mb()
39 #define virtio_rmb() rmb()
40 #define virtio_wmb() wmb()
44 /* For development, we want to crash whenever the ring is screwed. */
45 #define BAD_RING(_vq, fmt, args...) \
47 dev_err(&(_vq)->vq.vdev->dev, \
48 "%s:"fmt, (_vq)->vq.name, ##args); \
51 /* Caller is supposed to guarantee no reentry. */
52 #define START_USE(_vq) \
55 panic("%s:in_use = %i\n", \
56 (_vq)->vq.name, (_vq)->in_use); \
57 (_vq)->in_use = __LINE__; \
59 #define END_USE(_vq) \
60 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
62 #define BAD_RING(_vq, fmt, args...) \
64 dev_err(&_vq->vq.vdev->dev, \
65 "%s:"fmt, (_vq)->vq.name, ##args); \
66 (_vq)->broken = true; \
72 struct vring_virtqueue
76 /* Actual memory layout for this queue */
79 /* Other side has made a mess, don't try any more. */
82 /* Host supports indirect buffers */
85 /* Number of free buffers */
86 unsigned int num_free
;
87 /* Head of free buffer list. */
88 unsigned int free_head
;
89 /* Number we've added since last sync. */
90 unsigned int num_added
;
92 /* Last used index we've seen. */
95 /* How to notify other side. FIXME: commonalize hcalls! */
96 void (*notify
)(struct virtqueue
*vq
);
99 /* They're supposed to lock for us. */
103 /* Tokens for callbacks. */
107 #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
109 /* Set up an indirect table of descriptors and add it to the queue. */
110 static int vring_add_indirect(struct vring_virtqueue
*vq
,
111 struct scatterlist sg
[],
115 struct vring_desc
*desc
;
119 desc
= kmalloc((out
+ in
) * sizeof(struct vring_desc
), GFP_ATOMIC
);
121 return vq
->vring
.num
;
123 /* Transfer entries from the sg list into the indirect page */
124 for (i
= 0; i
< out
; i
++) {
125 desc
[i
].flags
= VRING_DESC_F_NEXT
;
126 desc
[i
].addr
= sg_phys(sg
);
127 desc
[i
].len
= sg
->length
;
131 for (; i
< (out
+ in
); i
++) {
132 desc
[i
].flags
= VRING_DESC_F_NEXT
|VRING_DESC_F_WRITE
;
133 desc
[i
].addr
= sg_phys(sg
);
134 desc
[i
].len
= sg
->length
;
139 /* Last one doesn't continue. */
140 desc
[i
-1].flags
&= ~VRING_DESC_F_NEXT
;
143 /* We're about to use a buffer */
146 /* Use a single buffer which doesn't continue */
147 head
= vq
->free_head
;
148 vq
->vring
.desc
[head
].flags
= VRING_DESC_F_INDIRECT
;
149 vq
->vring
.desc
[head
].addr
= virt_to_phys(desc
);
150 vq
->vring
.desc
[head
].len
= i
* sizeof(struct vring_desc
);
152 /* Update free pointer */
153 vq
->free_head
= vq
->vring
.desc
[head
].next
;
158 static int vring_add_buf(struct virtqueue
*_vq
,
159 struct scatterlist sg
[],
164 struct vring_virtqueue
*vq
= to_vvq(_vq
);
165 unsigned int i
, avail
, head
, uninitialized_var(prev
);
169 BUG_ON(data
== NULL
);
171 /* If the host supports indirect descriptor tables, and we have multiple
172 * buffers, then go indirect. FIXME: tune this threshold */
173 if (vq
->indirect
&& (out
+ in
) > 1 && vq
->num_free
) {
174 head
= vring_add_indirect(vq
, sg
, out
, in
);
175 if (head
!= vq
->vring
.num
)
179 BUG_ON(out
+ in
> vq
->vring
.num
);
180 BUG_ON(out
+ in
== 0);
182 if (vq
->num_free
< out
+ in
) {
183 pr_debug("Can't add buf len %i - avail = %i\n",
184 out
+ in
, vq
->num_free
);
185 /* FIXME: for historical reasons, we force a notify here if
186 * there are outgoing parts to the buffer. Presumably the
187 * host should service the ring ASAP. */
194 /* We're about to use some buffers from the free list. */
195 vq
->num_free
-= out
+ in
;
197 head
= vq
->free_head
;
198 for (i
= vq
->free_head
; out
; i
= vq
->vring
.desc
[i
].next
, out
--) {
199 vq
->vring
.desc
[i
].flags
= VRING_DESC_F_NEXT
;
200 vq
->vring
.desc
[i
].addr
= sg_phys(sg
);
201 vq
->vring
.desc
[i
].len
= sg
->length
;
205 for (; in
; i
= vq
->vring
.desc
[i
].next
, in
--) {
206 vq
->vring
.desc
[i
].flags
= VRING_DESC_F_NEXT
|VRING_DESC_F_WRITE
;
207 vq
->vring
.desc
[i
].addr
= sg_phys(sg
);
208 vq
->vring
.desc
[i
].len
= sg
->length
;
212 /* Last one doesn't continue. */
213 vq
->vring
.desc
[prev
].flags
&= ~VRING_DESC_F_NEXT
;
215 /* Update free pointer */
220 vq
->data
[head
] = data
;
222 /* Put entry in available array (but don't update avail->idx until they
223 * do sync). FIXME: avoid modulus here? */
224 avail
= (vq
->vring
.avail
->idx
+ vq
->num_added
++) % vq
->vring
.num
;
225 vq
->vring
.avail
->ring
[avail
] = head
;
227 pr_debug("Added buffer head %i to %p\n", head
, vq
);
230 /* If we're indirect, we can fit many (assuming not OOM). */
232 return vq
->num_free
? vq
->vring
.num
: 0;
236 static void vring_kick(struct virtqueue
*_vq
)
238 struct vring_virtqueue
*vq
= to_vvq(_vq
);
240 /* Descriptors and available array need to be set before we expose the
241 * new available array entries. */
244 vq
->vring
.avail
->idx
+= vq
->num_added
;
247 /* Need to update avail index before checking if we should notify */
250 if (!(vq
->vring
.used
->flags
& VRING_USED_F_NO_NOTIFY
))
251 /* Prod other side to tell it about changes. */
257 static void detach_buf(struct vring_virtqueue
*vq
, unsigned int head
)
261 /* Clear data ptr. */
262 vq
->data
[head
] = NULL
;
264 /* Put back on free list: find end */
267 /* Free the indirect table */
268 if (vq
->vring
.desc
[i
].flags
& VRING_DESC_F_INDIRECT
)
269 kfree(phys_to_virt(vq
->vring
.desc
[i
].addr
));
271 while (vq
->vring
.desc
[i
].flags
& VRING_DESC_F_NEXT
) {
272 i
= vq
->vring
.desc
[i
].next
;
276 vq
->vring
.desc
[i
].next
= vq
->free_head
;
277 vq
->free_head
= head
;
278 /* Plus final descriptor */
282 static inline bool more_used(const struct vring_virtqueue
*vq
)
284 return vq
->last_used_idx
!= vq
->vring
.used
->idx
;
287 static void *vring_get_buf(struct virtqueue
*_vq
, unsigned int *len
)
289 struct vring_virtqueue
*vq
= to_vvq(_vq
);
295 if (unlikely(vq
->broken
)) {
300 if (!more_used(vq
)) {
301 pr_debug("No more buffers in queue\n");
306 /* Only get used array entries after they have been exposed by host. */
309 i
= vq
->vring
.used
->ring
[vq
->last_used_idx
%vq
->vring
.num
].id
;
310 *len
= vq
->vring
.used
->ring
[vq
->last_used_idx
%vq
->vring
.num
].len
;
312 if (unlikely(i
>= vq
->vring
.num
)) {
313 BAD_RING(vq
, "id %u out of range\n", i
);
316 if (unlikely(!vq
->data
[i
])) {
317 BAD_RING(vq
, "id %u is not a head!\n", i
);
321 /* detach_buf clears data, so grab it now. */
329 static void vring_disable_cb(struct virtqueue
*_vq
)
331 struct vring_virtqueue
*vq
= to_vvq(_vq
);
333 vq
->vring
.avail
->flags
|= VRING_AVAIL_F_NO_INTERRUPT
;
336 static bool vring_enable_cb(struct virtqueue
*_vq
)
338 struct vring_virtqueue
*vq
= to_vvq(_vq
);
342 /* We optimistically turn back on interrupts, then check if there was
344 vq
->vring
.avail
->flags
&= ~VRING_AVAIL_F_NO_INTERRUPT
;
346 if (unlikely(more_used(vq
))) {
355 static void *vring_detach_unused_buf(struct virtqueue
*_vq
)
357 struct vring_virtqueue
*vq
= to_vvq(_vq
);
363 for (i
= 0; i
< vq
->vring
.num
; i
++) {
366 /* detach_buf clears data, so grab it now. */
372 /* That should have freed everything. */
373 BUG_ON(vq
->num_free
!= vq
->vring
.num
);
379 irqreturn_t
vring_interrupt(int irq
, void *_vq
)
381 struct vring_virtqueue
*vq
= to_vvq(_vq
);
383 if (!more_used(vq
)) {
384 pr_debug("virtqueue interrupt with no work for %p\n", vq
);
388 if (unlikely(vq
->broken
))
391 pr_debug("virtqueue callback for %p (%p)\n", vq
, vq
->vq
.callback
);
393 vq
->vq
.callback(&vq
->vq
);
397 EXPORT_SYMBOL_GPL(vring_interrupt
);
399 static struct virtqueue_ops vring_vq_ops
= {
400 .add_buf
= vring_add_buf
,
401 .get_buf
= vring_get_buf
,
403 .disable_cb
= vring_disable_cb
,
404 .enable_cb
= vring_enable_cb
,
405 .detach_unused_buf
= vring_detach_unused_buf
,
408 struct virtqueue
*vring_new_virtqueue(unsigned int num
,
409 unsigned int vring_align
,
410 struct virtio_device
*vdev
,
412 void (*notify
)(struct virtqueue
*),
413 void (*callback
)(struct virtqueue
*),
416 struct vring_virtqueue
*vq
;
419 /* We assume num is a power of 2. */
420 if (num
& (num
- 1)) {
421 dev_warn(&vdev
->dev
, "Bad virtqueue length %u\n", num
);
425 vq
= kmalloc(sizeof(*vq
) + sizeof(void *)*num
, GFP_KERNEL
);
429 vring_init(&vq
->vring
, num
, pages
, vring_align
);
430 vq
->vq
.callback
= callback
;
432 vq
->vq
.vq_ops
= &vring_vq_ops
;
436 vq
->last_used_idx
= 0;
438 list_add_tail(&vq
->vq
.list
, &vdev
->vqs
);
443 vq
->indirect
= virtio_has_feature(vdev
, VIRTIO_RING_F_INDIRECT_DESC
);
445 /* No callback? Tell other side not to bother us. */
447 vq
->vring
.avail
->flags
|= VRING_AVAIL_F_NO_INTERRUPT
;
449 /* Put everything in free lists. */
452 for (i
= 0; i
< num
-1; i
++) {
453 vq
->vring
.desc
[i
].next
= i
+1;
460 EXPORT_SYMBOL_GPL(vring_new_virtqueue
);
462 void vring_del_virtqueue(struct virtqueue
*vq
)
467 EXPORT_SYMBOL_GPL(vring_del_virtqueue
);
469 /* Manipulates transport-specific feature bits. */
470 void vring_transport_features(struct virtio_device
*vdev
)
474 for (i
= VIRTIO_TRANSPORT_F_START
; i
< VIRTIO_TRANSPORT_F_END
; i
++) {
476 case VIRTIO_RING_F_INDIRECT_DESC
:
479 /* We don't understand this bit. */
480 clear_bit(i
, vdev
->features
);
484 EXPORT_SYMBOL_GPL(vring_transport_features
);
486 MODULE_LICENSE("GPL");