ALSA: hda - Add quirk for Dell Vostro 1220
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / virtio / virtio_ring.c
blobdd4370b45763dd75747adc93260e05bfc6e507f5
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. */
27 #ifdef CONFIG_SMP
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()
34 #else
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()
41 #endif
43 #ifdef DEBUG
44 /* For development, we want to crash whenever the ring is screwed. */
45 #define BAD_RING(_vq, fmt, args...) \
46 do { \
47 dev_err(&(_vq)->vq.vdev->dev, \
48 "%s:"fmt, (_vq)->vq.name, ##args); \
49 BUG(); \
50 } while (0)
51 /* Caller is supposed to guarantee no reentry. */
52 #define START_USE(_vq) \
53 do { \
54 if ((_vq)->in_use) \
55 panic("%s:in_use = %i\n", \
56 (_vq)->vq.name, (_vq)->in_use); \
57 (_vq)->in_use = __LINE__; \
58 } while (0)
59 #define END_USE(_vq) \
60 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
61 #else
62 #define BAD_RING(_vq, fmt, args...) \
63 do { \
64 dev_err(&_vq->vq.vdev->dev, \
65 "%s:"fmt, (_vq)->vq.name, ##args); \
66 (_vq)->broken = true; \
67 } while (0)
68 #define START_USE(vq)
69 #define END_USE(vq)
70 #endif
72 struct vring_virtqueue
74 struct virtqueue vq;
76 /* Actual memory layout for this queue */
77 struct vring vring;
79 /* Other side has made a mess, don't try any more. */
80 bool broken;
82 /* Host supports indirect buffers */
83 bool indirect;
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. */
93 u16 last_used_idx;
95 /* How to notify other side. FIXME: commonalize hcalls! */
96 void (*notify)(struct virtqueue *vq);
98 #ifdef DEBUG
99 /* They're supposed to lock for us. */
100 unsigned int in_use;
101 #endif
103 /* Tokens for callbacks. */
104 void *data[];
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[],
112 unsigned int out,
113 unsigned int in)
115 struct vring_desc *desc;
116 unsigned head;
117 int i;
119 desc = kmalloc((out + in) * sizeof(struct vring_desc), GFP_ATOMIC);
120 if (!desc)
121 return -ENOMEM;
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;
128 desc[i].next = i+1;
129 sg++;
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;
135 desc[i].next = i+1;
136 sg++;
139 /* Last one doesn't continue. */
140 desc[i-1].flags &= ~VRING_DESC_F_NEXT;
141 desc[i-1].next = 0;
143 /* We're about to use a buffer */
144 vq->num_free--;
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;
155 return head;
158 static int vring_add_buf(struct virtqueue *_vq,
159 struct scatterlist sg[],
160 unsigned int out,
161 unsigned int in,
162 void *data)
164 struct vring_virtqueue *vq = to_vvq(_vq);
165 unsigned int i, avail, uninitialized_var(prev);
166 int head;
168 START_USE(vq);
170 BUG_ON(data == NULL);
172 /* If the host supports indirect descriptor tables, and we have multiple
173 * buffers, then go indirect. FIXME: tune this threshold */
174 if (vq->indirect && (out + in) > 1 && vq->num_free) {
175 head = vring_add_indirect(vq, sg, out, in);
176 if (likely(head >= 0))
177 goto add_head;
180 BUG_ON(out + in > vq->vring.num);
181 BUG_ON(out + in == 0);
183 if (vq->num_free < out + in) {
184 pr_debug("Can't add buf len %i - avail = %i\n",
185 out + in, vq->num_free);
186 /* FIXME: for historical reasons, we force a notify here if
187 * there are outgoing parts to the buffer. Presumably the
188 * host should service the ring ASAP. */
189 if (out)
190 vq->notify(&vq->vq);
191 END_USE(vq);
192 return -ENOSPC;
195 /* We're about to use some buffers from the free list. */
196 vq->num_free -= out + in;
198 head = vq->free_head;
199 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
200 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
201 vq->vring.desc[i].addr = sg_phys(sg);
202 vq->vring.desc[i].len = sg->length;
203 prev = i;
204 sg++;
206 for (; in; i = vq->vring.desc[i].next, in--) {
207 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
208 vq->vring.desc[i].addr = sg_phys(sg);
209 vq->vring.desc[i].len = sg->length;
210 prev = i;
211 sg++;
213 /* Last one doesn't continue. */
214 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
216 /* Update free pointer */
217 vq->free_head = i;
219 add_head:
220 /* Set token. */
221 vq->data[head] = data;
223 /* Put entry in available array (but don't update avail->idx until they
224 * do sync). FIXME: avoid modulus here? */
225 avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
226 vq->vring.avail->ring[avail] = head;
228 pr_debug("Added buffer head %i to %p\n", head, vq);
229 END_USE(vq);
231 /* If we're indirect, we can fit many (assuming not OOM). */
232 if (vq->indirect)
233 return vq->num_free ? vq->vring.num : 0;
234 return vq->num_free;
237 static void vring_kick(struct virtqueue *_vq)
239 struct vring_virtqueue *vq = to_vvq(_vq);
240 START_USE(vq);
241 /* Descriptors and available array need to be set before we expose the
242 * new available array entries. */
243 virtio_wmb();
245 vq->vring.avail->idx += vq->num_added;
246 vq->num_added = 0;
248 /* Need to update avail index before checking if we should notify */
249 virtio_mb();
251 if (!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
252 /* Prod other side to tell it about changes. */
253 vq->notify(&vq->vq);
255 END_USE(vq);
258 static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
260 unsigned int i;
262 /* Clear data ptr. */
263 vq->data[head] = NULL;
265 /* Put back on free list: find end */
266 i = head;
268 /* Free the indirect table */
269 if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
270 kfree(phys_to_virt(vq->vring.desc[i].addr));
272 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
273 i = vq->vring.desc[i].next;
274 vq->num_free++;
277 vq->vring.desc[i].next = vq->free_head;
278 vq->free_head = head;
279 /* Plus final descriptor */
280 vq->num_free++;
283 static inline bool more_used(const struct vring_virtqueue *vq)
285 return vq->last_used_idx != vq->vring.used->idx;
288 static void *vring_get_buf(struct virtqueue *_vq, unsigned int *len)
290 struct vring_virtqueue *vq = to_vvq(_vq);
291 void *ret;
292 unsigned int i;
294 START_USE(vq);
296 if (unlikely(vq->broken)) {
297 END_USE(vq);
298 return NULL;
301 if (!more_used(vq)) {
302 pr_debug("No more buffers in queue\n");
303 END_USE(vq);
304 return NULL;
307 /* Only get used array entries after they have been exposed by host. */
308 virtio_rmb();
310 i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
311 *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
313 if (unlikely(i >= vq->vring.num)) {
314 BAD_RING(vq, "id %u out of range\n", i);
315 return NULL;
317 if (unlikely(!vq->data[i])) {
318 BAD_RING(vq, "id %u is not a head!\n", i);
319 return NULL;
322 /* detach_buf clears data, so grab it now. */
323 ret = vq->data[i];
324 detach_buf(vq, i);
325 vq->last_used_idx++;
326 END_USE(vq);
327 return ret;
330 static void vring_disable_cb(struct virtqueue *_vq)
332 struct vring_virtqueue *vq = to_vvq(_vq);
334 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
337 static bool vring_enable_cb(struct virtqueue *_vq)
339 struct vring_virtqueue *vq = to_vvq(_vq);
341 START_USE(vq);
343 /* We optimistically turn back on interrupts, then check if there was
344 * more to do. */
345 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
346 virtio_mb();
347 if (unlikely(more_used(vq))) {
348 END_USE(vq);
349 return false;
352 END_USE(vq);
353 return true;
356 static void *vring_detach_unused_buf(struct virtqueue *_vq)
358 struct vring_virtqueue *vq = to_vvq(_vq);
359 unsigned int i;
360 void *buf;
362 START_USE(vq);
364 for (i = 0; i < vq->vring.num; i++) {
365 if (!vq->data[i])
366 continue;
367 /* detach_buf clears data, so grab it now. */
368 buf = vq->data[i];
369 detach_buf(vq, i);
370 END_USE(vq);
371 return buf;
373 /* That should have freed everything. */
374 BUG_ON(vq->num_free != vq->vring.num);
376 END_USE(vq);
377 return NULL;
380 irqreturn_t vring_interrupt(int irq, void *_vq)
382 struct vring_virtqueue *vq = to_vvq(_vq);
384 if (!more_used(vq)) {
385 pr_debug("virtqueue interrupt with no work for %p\n", vq);
386 return IRQ_NONE;
389 if (unlikely(vq->broken))
390 return IRQ_HANDLED;
392 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
393 if (vq->vq.callback)
394 vq->vq.callback(&vq->vq);
396 return IRQ_HANDLED;
398 EXPORT_SYMBOL_GPL(vring_interrupt);
400 static struct virtqueue_ops vring_vq_ops = {
401 .add_buf = vring_add_buf,
402 .get_buf = vring_get_buf,
403 .kick = vring_kick,
404 .disable_cb = vring_disable_cb,
405 .enable_cb = vring_enable_cb,
406 .detach_unused_buf = vring_detach_unused_buf,
409 struct virtqueue *vring_new_virtqueue(unsigned int num,
410 unsigned int vring_align,
411 struct virtio_device *vdev,
412 void *pages,
413 void (*notify)(struct virtqueue *),
414 void (*callback)(struct virtqueue *),
415 const char *name)
417 struct vring_virtqueue *vq;
418 unsigned int i;
420 /* We assume num is a power of 2. */
421 if (num & (num - 1)) {
422 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
423 return NULL;
426 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
427 if (!vq)
428 return NULL;
430 vring_init(&vq->vring, num, pages, vring_align);
431 vq->vq.callback = callback;
432 vq->vq.vdev = vdev;
433 vq->vq.vq_ops = &vring_vq_ops;
434 vq->vq.name = name;
435 vq->notify = notify;
436 vq->broken = false;
437 vq->last_used_idx = 0;
438 vq->num_added = 0;
439 list_add_tail(&vq->vq.list, &vdev->vqs);
440 #ifdef DEBUG
441 vq->in_use = false;
442 #endif
444 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
446 /* No callback? Tell other side not to bother us. */
447 if (!callback)
448 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
450 /* Put everything in free lists. */
451 vq->num_free = num;
452 vq->free_head = 0;
453 for (i = 0; i < num-1; i++) {
454 vq->vring.desc[i].next = i+1;
455 vq->data[i] = NULL;
457 vq->data[i] = NULL;
459 return &vq->vq;
461 EXPORT_SYMBOL_GPL(vring_new_virtqueue);
463 void vring_del_virtqueue(struct virtqueue *vq)
465 list_del(&vq->list);
466 kfree(to_vvq(vq));
468 EXPORT_SYMBOL_GPL(vring_del_virtqueue);
470 /* Manipulates transport-specific feature bits. */
471 void vring_transport_features(struct virtio_device *vdev)
473 unsigned int i;
475 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
476 switch (i) {
477 case VIRTIO_RING_F_INDIRECT_DESC:
478 break;
479 default:
480 /* We don't understand this bit. */
481 clear_bit(i, vdev->features);
485 EXPORT_SYMBOL_GPL(vring_transport_features);
487 MODULE_LICENSE("GPL");