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
);
363 bool virtqueue_enable_cb(struct virtqueue
*_vq
)
365 struct vring_virtqueue
*vq
= to_vvq(_vq
);
369 /* We optimistically turn back on interrupts, then check if there was
371 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
372 * either clear the flags bit or point the event index at the next
373 * entry. Always do both to keep code simple. */
374 vq
->vring
.avail
->flags
&= ~VRING_AVAIL_F_NO_INTERRUPT
;
375 vring_used_event(&vq
->vring
) = vq
->last_used_idx
;
377 if (unlikely(more_used(vq
))) {
385 EXPORT_SYMBOL_GPL(virtqueue_enable_cb
);
387 bool virtqueue_enable_cb_delayed(struct virtqueue
*_vq
)
389 struct vring_virtqueue
*vq
= to_vvq(_vq
);
394 /* We optimistically turn back on interrupts, then check if there was
396 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
397 * either clear the flags bit or point the event index at the next
398 * entry. Always do both to keep code simple. */
399 vq
->vring
.avail
->flags
&= ~VRING_AVAIL_F_NO_INTERRUPT
;
400 /* TODO: tune this threshold */
401 bufs
= (u16
)(vq
->vring
.avail
->idx
- vq
->last_used_idx
) * 3 / 4;
402 vring_used_event(&vq
->vring
) = vq
->last_used_idx
+ bufs
;
404 if (unlikely((u16
)(vq
->vring
.used
->idx
- vq
->last_used_idx
) > bufs
)) {
412 EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed
);
414 void *virtqueue_detach_unused_buf(struct virtqueue
*_vq
)
416 struct vring_virtqueue
*vq
= to_vvq(_vq
);
422 for (i
= 0; i
< vq
->vring
.num
; i
++) {
425 /* detach_buf clears data, so grab it now. */
428 vq
->vring
.avail
->idx
--;
432 /* That should have freed everything. */
433 BUG_ON(vq
->num_free
!= vq
->vring
.num
);
438 EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf
);
440 irqreturn_t
vring_interrupt(int irq
, void *_vq
)
442 struct vring_virtqueue
*vq
= to_vvq(_vq
);
444 if (!more_used(vq
)) {
445 pr_debug("virtqueue interrupt with no work for %p\n", vq
);
449 if (unlikely(vq
->broken
))
452 pr_debug("virtqueue callback for %p (%p)\n", vq
, vq
->vq
.callback
);
454 vq
->vq
.callback(&vq
->vq
);
458 EXPORT_SYMBOL_GPL(vring_interrupt
);
460 struct virtqueue
*vring_new_virtqueue(unsigned int num
,
461 unsigned int vring_align
,
462 struct virtio_device
*vdev
,
464 void (*notify
)(struct virtqueue
*),
465 void (*callback
)(struct virtqueue
*),
468 struct vring_virtqueue
*vq
;
471 /* We assume num is a power of 2. */
472 if (num
& (num
- 1)) {
473 dev_warn(&vdev
->dev
, "Bad virtqueue length %u\n", num
);
477 vq
= kmalloc(sizeof(*vq
) + sizeof(void *)*num
, GFP_KERNEL
);
481 vring_init(&vq
->vring
, num
, pages
, vring_align
);
482 vq
->vq
.callback
= callback
;
487 vq
->last_used_idx
= 0;
489 list_add_tail(&vq
->vq
.list
, &vdev
->vqs
);
494 vq
->indirect
= virtio_has_feature(vdev
, VIRTIO_RING_F_INDIRECT_DESC
);
495 vq
->event
= virtio_has_feature(vdev
, VIRTIO_RING_F_EVENT_IDX
);
497 /* No callback? Tell other side not to bother us. */
499 vq
->vring
.avail
->flags
|= VRING_AVAIL_F_NO_INTERRUPT
;
501 /* Put everything in free lists. */
504 for (i
= 0; i
< num
-1; i
++) {
505 vq
->vring
.desc
[i
].next
= i
+1;
512 EXPORT_SYMBOL_GPL(vring_new_virtqueue
);
514 void vring_del_virtqueue(struct virtqueue
*vq
)
519 EXPORT_SYMBOL_GPL(vring_del_virtqueue
);
521 /* Manipulates transport-specific feature bits. */
522 void vring_transport_features(struct virtio_device
*vdev
)
526 for (i
= VIRTIO_TRANSPORT_F_START
; i
< VIRTIO_TRANSPORT_F_END
; i
++) {
528 case VIRTIO_RING_F_INDIRECT_DESC
:
530 case VIRTIO_RING_F_EVENT_IDX
:
533 /* We don't understand this bit. */
534 clear_bit(i
, vdev
->features
);
538 EXPORT_SYMBOL_GPL(vring_transport_features
);
540 /* return the size of the vring within the virtqueue */
541 unsigned int virtqueue_get_vring_size(struct virtqueue
*_vq
)
544 struct vring_virtqueue
*vq
= to_vvq(_vq
);
546 return vq
->vring
.num
;
548 EXPORT_SYMBOL_GPL(virtqueue_get_vring_size
);
550 MODULE_LICENSE("GPL");