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 /* Host supports indirect buffers */
67 /* Number of free buffers */
68 unsigned int num_free
;
69 /* Head of free buffer list. */
70 unsigned int free_head
;
71 /* Number we've added since last sync. */
72 unsigned int num_added
;
74 /* Last used index we've seen. */
77 /* How to notify other side. FIXME: commonalize hcalls! */
78 void (*notify
)(struct virtqueue
*vq
);
81 /* They're supposed to lock for us. */
85 /* Tokens for callbacks. */
89 #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
91 /* Set up an indirect table of descriptors and add it to the queue. */
92 static int vring_add_indirect(struct vring_virtqueue
*vq
,
93 struct scatterlist sg
[],
97 struct vring_desc
*desc
;
101 desc
= kmalloc((out
+ in
) * sizeof(struct vring_desc
), GFP_ATOMIC
);
103 return vq
->vring
.num
;
105 /* Transfer entries from the sg list into the indirect page */
106 for (i
= 0; i
< out
; i
++) {
107 desc
[i
].flags
= VRING_DESC_F_NEXT
;
108 desc
[i
].addr
= sg_phys(sg
);
109 desc
[i
].len
= sg
->length
;
113 for (; i
< (out
+ in
); i
++) {
114 desc
[i
].flags
= VRING_DESC_F_NEXT
|VRING_DESC_F_WRITE
;
115 desc
[i
].addr
= sg_phys(sg
);
116 desc
[i
].len
= sg
->length
;
121 /* Last one doesn't continue. */
122 desc
[i
-1].flags
&= ~VRING_DESC_F_NEXT
;
125 /* We're about to use a buffer */
128 /* Use a single buffer which doesn't continue */
129 head
= vq
->free_head
;
130 vq
->vring
.desc
[head
].flags
= VRING_DESC_F_INDIRECT
;
131 vq
->vring
.desc
[head
].addr
= virt_to_phys(desc
);
132 vq
->vring
.desc
[head
].len
= i
* sizeof(struct vring_desc
);
134 /* Update free pointer */
135 vq
->free_head
= vq
->vring
.desc
[head
].next
;
140 static int vring_add_buf(struct virtqueue
*_vq
,
141 struct scatterlist sg
[],
146 struct vring_virtqueue
*vq
= to_vvq(_vq
);
147 unsigned int i
, avail
, head
, uninitialized_var(prev
);
151 BUG_ON(data
== NULL
);
153 /* If the host supports indirect descriptor tables, and we have multiple
154 * buffers, then go indirect. FIXME: tune this threshold */
155 if (vq
->indirect
&& (out
+ in
) > 1 && vq
->num_free
) {
156 head
= vring_add_indirect(vq
, sg
, out
, in
);
157 if (head
!= vq
->vring
.num
)
161 BUG_ON(out
+ in
> vq
->vring
.num
);
162 BUG_ON(out
+ in
== 0);
164 if (vq
->num_free
< out
+ in
) {
165 pr_debug("Can't add buf len %i - avail = %i\n",
166 out
+ in
, vq
->num_free
);
167 /* FIXME: for historical reasons, we force a notify here if
168 * there are outgoing parts to the buffer. Presumably the
169 * host should service the ring ASAP. */
176 /* We're about to use some buffers from the free list. */
177 vq
->num_free
-= out
+ in
;
179 head
= vq
->free_head
;
180 for (i
= vq
->free_head
; out
; i
= vq
->vring
.desc
[i
].next
, out
--) {
181 vq
->vring
.desc
[i
].flags
= VRING_DESC_F_NEXT
;
182 vq
->vring
.desc
[i
].addr
= sg_phys(sg
);
183 vq
->vring
.desc
[i
].len
= sg
->length
;
187 for (; in
; i
= vq
->vring
.desc
[i
].next
, in
--) {
188 vq
->vring
.desc
[i
].flags
= VRING_DESC_F_NEXT
|VRING_DESC_F_WRITE
;
189 vq
->vring
.desc
[i
].addr
= sg_phys(sg
);
190 vq
->vring
.desc
[i
].len
= sg
->length
;
194 /* Last one doesn't continue. */
195 vq
->vring
.desc
[prev
].flags
&= ~VRING_DESC_F_NEXT
;
197 /* Update free pointer */
202 vq
->data
[head
] = data
;
204 /* Put entry in available array (but don't update avail->idx until they
205 * do sync). FIXME: avoid modulus here? */
206 avail
= (vq
->vring
.avail
->idx
+ vq
->num_added
++) % vq
->vring
.num
;
207 vq
->vring
.avail
->ring
[avail
] = head
;
209 pr_debug("Added buffer head %i to %p\n", head
, vq
);
212 /* If we're indirect, we can fit many (assuming not OOM). */
214 return vq
->num_free
? vq
->vring
.num
: 0;
218 static void vring_kick(struct virtqueue
*_vq
)
220 struct vring_virtqueue
*vq
= to_vvq(_vq
);
222 /* Descriptors and available array need to be set before we expose the
223 * new available array entries. */
226 vq
->vring
.avail
->idx
+= vq
->num_added
;
229 /* Need to update avail index before checking if we should notify */
232 if (!(vq
->vring
.used
->flags
& VRING_USED_F_NO_NOTIFY
))
233 /* Prod other side to tell it about changes. */
239 static void detach_buf(struct vring_virtqueue
*vq
, unsigned int head
)
243 /* Clear data ptr. */
244 vq
->data
[head
] = NULL
;
246 /* Put back on free list: find end */
249 /* Free the indirect table */
250 if (vq
->vring
.desc
[i
].flags
& VRING_DESC_F_INDIRECT
)
251 kfree(phys_to_virt(vq
->vring
.desc
[i
].addr
));
253 while (vq
->vring
.desc
[i
].flags
& VRING_DESC_F_NEXT
) {
254 i
= vq
->vring
.desc
[i
].next
;
258 vq
->vring
.desc
[i
].next
= vq
->free_head
;
259 vq
->free_head
= head
;
260 /* Plus final descriptor */
264 static inline bool more_used(const struct vring_virtqueue
*vq
)
266 return vq
->last_used_idx
!= vq
->vring
.used
->idx
;
269 static void *vring_get_buf(struct virtqueue
*_vq
, unsigned int *len
)
271 struct vring_virtqueue
*vq
= to_vvq(_vq
);
277 if (unlikely(vq
->broken
)) {
282 if (!more_used(vq
)) {
283 pr_debug("No more buffers in queue\n");
288 /* Only get used array entries after they have been exposed by host. */
291 i
= vq
->vring
.used
->ring
[vq
->last_used_idx
%vq
->vring
.num
].id
;
292 *len
= vq
->vring
.used
->ring
[vq
->last_used_idx
%vq
->vring
.num
].len
;
294 if (unlikely(i
>= vq
->vring
.num
)) {
295 BAD_RING(vq
, "id %u out of range\n", i
);
298 if (unlikely(!vq
->data
[i
])) {
299 BAD_RING(vq
, "id %u is not a head!\n", i
);
303 /* detach_buf clears data, so grab it now. */
311 static void vring_disable_cb(struct virtqueue
*_vq
)
313 struct vring_virtqueue
*vq
= to_vvq(_vq
);
315 vq
->vring
.avail
->flags
|= VRING_AVAIL_F_NO_INTERRUPT
;
318 static bool vring_enable_cb(struct virtqueue
*_vq
)
320 struct vring_virtqueue
*vq
= to_vvq(_vq
);
324 /* We optimistically turn back on interrupts, then check if there was
326 vq
->vring
.avail
->flags
&= ~VRING_AVAIL_F_NO_INTERRUPT
;
328 if (unlikely(more_used(vq
))) {
337 irqreturn_t
vring_interrupt(int irq
, void *_vq
)
339 struct vring_virtqueue
*vq
= to_vvq(_vq
);
341 if (!more_used(vq
)) {
342 pr_debug("virtqueue interrupt with no work for %p\n", vq
);
346 if (unlikely(vq
->broken
))
349 pr_debug("virtqueue callback for %p (%p)\n", vq
, vq
->vq
.callback
);
351 vq
->vq
.callback(&vq
->vq
);
355 EXPORT_SYMBOL_GPL(vring_interrupt
);
357 static struct virtqueue_ops vring_vq_ops
= {
358 .add_buf
= vring_add_buf
,
359 .get_buf
= vring_get_buf
,
361 .disable_cb
= vring_disable_cb
,
362 .enable_cb
= vring_enable_cb
,
365 struct virtqueue
*vring_new_virtqueue(unsigned int num
,
366 unsigned int vring_align
,
367 struct virtio_device
*vdev
,
369 void (*notify
)(struct virtqueue
*),
370 void (*callback
)(struct virtqueue
*),
373 struct vring_virtqueue
*vq
;
376 /* We assume num is a power of 2. */
377 if (num
& (num
- 1)) {
378 dev_warn(&vdev
->dev
, "Bad virtqueue length %u\n", num
);
382 vq
= kmalloc(sizeof(*vq
) + sizeof(void *)*num
, GFP_KERNEL
);
386 vring_init(&vq
->vring
, num
, pages
, vring_align
);
387 vq
->vq
.callback
= callback
;
389 vq
->vq
.vq_ops
= &vring_vq_ops
;
393 vq
->last_used_idx
= 0;
395 list_add_tail(&vq
->vq
.list
, &vdev
->vqs
);
400 vq
->indirect
= virtio_has_feature(vdev
, VIRTIO_RING_F_INDIRECT_DESC
);
402 /* No callback? Tell other side not to bother us. */
404 vq
->vring
.avail
->flags
|= VRING_AVAIL_F_NO_INTERRUPT
;
406 /* Put everything in free lists. */
409 for (i
= 0; i
< num
-1; i
++)
410 vq
->vring
.desc
[i
].next
= i
+1;
414 EXPORT_SYMBOL_GPL(vring_new_virtqueue
);
416 void vring_del_virtqueue(struct virtqueue
*vq
)
421 EXPORT_SYMBOL_GPL(vring_del_virtqueue
);
423 /* Manipulates transport-specific feature bits. */
424 void vring_transport_features(struct virtio_device
*vdev
)
428 for (i
= VIRTIO_TRANSPORT_F_START
; i
< VIRTIO_TRANSPORT_F_END
; i
++) {
430 case VIRTIO_RING_F_INDIRECT_DESC
:
433 /* We don't understand this bit. */
434 clear_bit(i
, vdev
->features
);
438 EXPORT_SYMBOL_GPL(vring_transport_features
);
440 MODULE_LICENSE("GPL");