inotify: remove broken mask checks causing unmount to be EINVAL
[linux/fpc-iii.git] / drivers / virtio / virtio_ring.c
blobdc2eed14378ff76ad3d63d891d0a98c1583ce423
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. */
28 #ifdef CONFIG_SMP
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()
35 #else
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()
42 #endif
44 #ifdef DEBUG
45 /* For development, we want to crash whenever the ring is screwed. */
46 #define BAD_RING(_vq, fmt, args...) \
47 do { \
48 dev_err(&(_vq)->vq.vdev->dev, \
49 "%s:"fmt, (_vq)->vq.name, ##args); \
50 BUG(); \
51 } while (0)
52 /* Caller is supposed to guarantee no reentry. */
53 #define START_USE(_vq) \
54 do { \
55 if ((_vq)->in_use) \
56 panic("%s:in_use = %i\n", \
57 (_vq)->vq.name, (_vq)->in_use); \
58 (_vq)->in_use = __LINE__; \
59 } while (0)
60 #define END_USE(_vq) \
61 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
62 #else
63 #define BAD_RING(_vq, fmt, args...) \
64 do { \
65 dev_err(&_vq->vq.vdev->dev, \
66 "%s:"fmt, (_vq)->vq.name, ##args); \
67 (_vq)->broken = true; \
68 } while (0)
69 #define START_USE(vq)
70 #define END_USE(vq)
71 #endif
73 struct vring_virtqueue
75 struct virtqueue vq;
77 /* Actual memory layout for this queue */
78 struct vring vring;
80 /* Other side has made a mess, don't try any more. */
81 bool broken;
83 /* Host supports indirect buffers */
84 bool indirect;
86 /* Host publishes avail event idx */
87 bool event;
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. */
97 u16 last_used_idx;
99 /* How to notify other side. FIXME: commonalize hcalls! */
100 void (*notify)(struct virtqueue *vq);
102 #ifdef DEBUG
103 /* They're supposed to lock for us. */
104 unsigned int in_use;
105 #endif
107 /* Tokens for callbacks. */
108 void *data[];
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[],
116 unsigned int out,
117 unsigned int in,
118 gfp_t gfp)
120 struct vring_desc *desc;
121 unsigned head;
122 int i;
125 * We require lowmem mappings for the descriptors because
126 * otherwise virt_to_phys will give us bogus addresses in the
127 * virtqueue.
129 gfp &= ~(__GFP_HIGHMEM | __GFP_HIGH);
131 desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp);
132 if (!desc)
133 return -ENOMEM;
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;
140 desc[i].next = i+1;
141 sg++;
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;
147 desc[i].next = i+1;
148 sg++;
151 /* Last one doesn't continue. */
152 desc[i-1].flags &= ~VRING_DESC_F_NEXT;
153 desc[i-1].next = 0;
155 /* We're about to use a buffer */
156 vq->num_free--;
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;
167 return head;
170 int virtqueue_add_buf_gfp(struct virtqueue *_vq,
171 struct scatterlist sg[],
172 unsigned int out,
173 unsigned int in,
174 void *data,
175 gfp_t gfp)
177 struct vring_virtqueue *vq = to_vvq(_vq);
178 unsigned int i, avail, uninitialized_var(prev);
179 int head;
181 START_USE(vq);
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))
190 goto add_head;
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. */
202 if (out)
203 vq->notify(&vq->vq);
204 END_USE(vq);
205 return -ENOSPC;
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;
216 prev = i;
217 sg++;
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;
223 prev = i;
224 sg++;
226 /* Last one doesn't continue. */
227 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
229 /* Update free pointer */
230 vq->free_head = i;
232 add_head:
233 /* Set token. */
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);
242 END_USE(vq);
244 return vq->num_free;
246 EXPORT_SYMBOL_GPL(virtqueue_add_buf_gfp);
248 void virtqueue_kick(struct virtqueue *_vq)
250 struct vring_virtqueue *vq = to_vvq(_vq);
251 u16 new, old;
252 START_USE(vq);
253 /* Descriptors and available array need to be set before we expose the
254 * new available array entries. */
255 virtio_wmb();
257 old = vq->vring.avail->idx;
258 new = vq->vring.avail->idx = old + vq->num_added;
259 vq->num_added = 0;
261 /* Need to update avail index before checking if we should notify */
262 virtio_mb();
264 if (vq->event ?
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. */
268 vq->notify(&vq->vq);
270 END_USE(vq);
272 EXPORT_SYMBOL_GPL(virtqueue_kick);
274 static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
276 unsigned int i;
278 /* Clear data ptr. */
279 vq->data[head] = NULL;
281 /* Put back on free list: find end */
282 i = head;
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;
290 vq->num_free++;
293 vq->vring.desc[i].next = vq->free_head;
294 vq->free_head = head;
295 /* Plus final descriptor */
296 vq->num_free++;
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);
307 void *ret;
308 unsigned int i;
310 START_USE(vq);
312 if (unlikely(vq->broken)) {
313 END_USE(vq);
314 return NULL;
317 if (!more_used(vq)) {
318 pr_debug("No more buffers in queue\n");
319 END_USE(vq);
320 return NULL;
323 /* Only get used array entries after they have been exposed by host. */
324 virtio_rmb();
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);
331 return NULL;
333 if (unlikely(!vq->data[i])) {
334 BAD_RING(vq, "id %u is not a head!\n", i);
335 return NULL;
338 /* detach_buf clears data, so grab it now. */
339 ret = vq->data[i];
340 detach_buf(vq, i);
341 vq->last_used_idx++;
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;
347 virtio_mb();
350 END_USE(vq);
351 return ret;
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);
367 START_USE(vq);
369 /* We optimistically turn back on interrupts, then check if there was
370 * more to do. */
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;
376 virtio_mb();
377 if (unlikely(more_used(vq))) {
378 END_USE(vq);
379 return false;
382 END_USE(vq);
383 return true;
385 EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
387 bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
389 struct vring_virtqueue *vq = to_vvq(_vq);
390 u16 bufs;
392 START_USE(vq);
394 /* We optimistically turn back on interrupts, then check if there was
395 * more to do. */
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;
403 virtio_mb();
404 if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
405 END_USE(vq);
406 return false;
409 END_USE(vq);
410 return true;
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);
417 unsigned int i;
418 void *buf;
420 START_USE(vq);
422 for (i = 0; i < vq->vring.num; i++) {
423 if (!vq->data[i])
424 continue;
425 /* detach_buf clears data, so grab it now. */
426 buf = vq->data[i];
427 detach_buf(vq, i);
428 vq->vring.avail->idx--;
429 END_USE(vq);
430 return buf;
432 /* That should have freed everything. */
433 BUG_ON(vq->num_free != vq->vring.num);
435 END_USE(vq);
436 return NULL;
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);
446 return IRQ_NONE;
449 if (unlikely(vq->broken))
450 return IRQ_HANDLED;
452 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
453 if (vq->vq.callback)
454 vq->vq.callback(&vq->vq);
456 return IRQ_HANDLED;
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,
463 void *pages,
464 void (*notify)(struct virtqueue *),
465 void (*callback)(struct virtqueue *),
466 const char *name)
468 struct vring_virtqueue *vq;
469 unsigned int i;
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);
474 return NULL;
477 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
478 if (!vq)
479 return NULL;
481 vring_init(&vq->vring, num, pages, vring_align);
482 vq->vq.callback = callback;
483 vq->vq.vdev = vdev;
484 vq->vq.name = name;
485 vq->notify = notify;
486 vq->broken = false;
487 vq->last_used_idx = 0;
488 vq->num_added = 0;
489 list_add_tail(&vq->vq.list, &vdev->vqs);
490 #ifdef DEBUG
491 vq->in_use = false;
492 #endif
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. */
498 if (!callback)
499 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
501 /* Put everything in free lists. */
502 vq->num_free = num;
503 vq->free_head = 0;
504 for (i = 0; i < num-1; i++) {
505 vq->vring.desc[i].next = i+1;
506 vq->data[i] = NULL;
508 vq->data[i] = NULL;
510 return &vq->vq;
512 EXPORT_SYMBOL_GPL(vring_new_virtqueue);
514 void vring_del_virtqueue(struct virtqueue *vq)
516 list_del(&vq->list);
517 kfree(to_vvq(vq));
519 EXPORT_SYMBOL_GPL(vring_del_virtqueue);
521 /* Manipulates transport-specific feature bits. */
522 void vring_transport_features(struct virtio_device *vdev)
524 unsigned int i;
526 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
527 switch (i) {
528 case VIRTIO_RING_F_INDIRECT_DESC:
529 break;
530 case VIRTIO_RING_F_EVENT_IDX:
531 break;
532 default:
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");