1ee97d402a48e1d39079760a4571fc1a1d549d98
[pandora-kernel.git] / drivers / virtio / virtio_ring.c
1 /* Virtio ring implementation.
2  *
3  *  Copyright 2007 Rusty Russell IBM Corporation
4  *
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.
9  *
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.
14  *
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
18  */
19 #include <linux/virtio.h>
20 #include <linux/virtio_ring.h>
21 #include <linux/virtio_config.h>
22 #include <linux/device.h>
23
24 #ifdef DEBUG
25 /* For development, we want to crash whenever the ring is screwed. */
26 #define BAD_RING(_vq, fmt, args...)                             \
27         do {                                                    \
28                 dev_err(&(_vq)->vq.vdev->dev,                   \
29                         "%s:"fmt, (_vq)->vq.name, ##args);      \
30                 BUG();                                          \
31         } while (0)
32 /* Caller is supposed to guarantee no reentry. */
33 #define START_USE(_vq)                                          \
34         do {                                                    \
35                 if ((_vq)->in_use)                              \
36                         panic("%s:in_use = %i\n",               \
37                               (_vq)->vq.name, (_vq)->in_use);   \
38                 (_vq)->in_use = __LINE__;                       \
39         } while (0)
40 #define END_USE(_vq) \
41         do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
42 #else
43 #define BAD_RING(_vq, fmt, args...)                             \
44         do {                                                    \
45                 dev_err(&_vq->vq.vdev->dev,                     \
46                         "%s:"fmt, (_vq)->vq.name, ##args);      \
47                 (_vq)->broken = true;                           \
48         } while (0)
49 #define START_USE(vq)
50 #define END_USE(vq)
51 #endif
52
53 struct vring_virtqueue
54 {
55         struct virtqueue vq;
56
57         /* Actual memory layout for this queue */
58         struct vring vring;
59
60         /* Other side has made a mess, don't try any more. */
61         bool broken;
62
63         /* Host supports indirect buffers */
64         bool indirect;
65
66         /* Number of free buffers */
67         unsigned int num_free;
68         /* Head of free buffer list. */
69         unsigned int free_head;
70         /* Number we've added since last sync. */
71         unsigned int num_added;
72
73         /* Last used index we've seen. */
74         u16 last_used_idx;
75
76         /* How to notify other side. FIXME: commonalize hcalls! */
77         void (*notify)(struct virtqueue *vq);
78
79 #ifdef DEBUG
80         /* They're supposed to lock for us. */
81         unsigned int in_use;
82 #endif
83
84         /* Tokens for callbacks. */
85         void *data[];
86 };
87
88 #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
89
90 /* Set up an indirect table of descriptors and add it to the queue. */
91 static int vring_add_indirect(struct vring_virtqueue *vq,
92                               struct scatterlist sg[],
93                               unsigned int out,
94                               unsigned int in)
95 {
96         struct vring_desc *desc;
97         unsigned head;
98         int i;
99
100         desc = kmalloc((out + in) * sizeof(struct vring_desc), GFP_ATOMIC);
101         if (!desc)
102                 return vq->vring.num;
103
104         /* Transfer entries from the sg list into the indirect page */
105         for (i = 0; i < out; i++) {
106                 desc[i].flags = VRING_DESC_F_NEXT;
107                 desc[i].addr = sg_phys(sg);
108                 desc[i].len = sg->length;
109                 desc[i].next = i+1;
110                 sg++;
111         }
112         for (; i < (out + in); i++) {
113                 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
114                 desc[i].addr = sg_phys(sg);
115                 desc[i].len = sg->length;
116                 desc[i].next = i+1;
117                 sg++;
118         }
119
120         /* Last one doesn't continue. */
121         desc[i-1].flags &= ~VRING_DESC_F_NEXT;
122         desc[i-1].next = 0;
123
124         /* We're about to use a buffer */
125         vq->num_free--;
126
127         /* Use a single buffer which doesn't continue */
128         head = vq->free_head;
129         vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
130         vq->vring.desc[head].addr = virt_to_phys(desc);
131         vq->vring.desc[head].len = i * sizeof(struct vring_desc);
132
133         /* Update free pointer */
134         vq->free_head = vq->vring.desc[head].next;
135
136         return head;
137 }
138
139 static int vring_add_buf(struct virtqueue *_vq,
140                          struct scatterlist sg[],
141                          unsigned int out,
142                          unsigned int in,
143                          void *data)
144 {
145         struct vring_virtqueue *vq = to_vvq(_vq);
146         unsigned int i, avail, head, uninitialized_var(prev);
147
148         START_USE(vq);
149
150         BUG_ON(data == NULL);
151
152         /* If the host supports indirect descriptor tables, and we have multiple
153          * buffers, then go indirect. FIXME: tune this threshold */
154         if (vq->indirect && (out + in) > 1 && vq->num_free) {
155                 head = vring_add_indirect(vq, sg, out, in);
156                 if (head != vq->vring.num)
157                         goto add_head;
158         }
159
160         BUG_ON(out + in > vq->vring.num);
161         BUG_ON(out + in == 0);
162
163         if (vq->num_free < out + in) {
164                 pr_debug("Can't add buf len %i - avail = %i\n",
165                          out + in, vq->num_free);
166                 /* FIXME: for historical reasons, we force a notify here if
167                  * there are outgoing parts to the buffer.  Presumably the
168                  * host should service the ring ASAP. */
169                 if (out)
170                         vq->notify(&vq->vq);
171                 END_USE(vq);
172                 return -ENOSPC;
173         }
174
175         /* We're about to use some buffers from the free list. */
176         vq->num_free -= out + in;
177
178         head = vq->free_head;
179         for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
180                 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
181                 vq->vring.desc[i].addr = sg_phys(sg);
182                 vq->vring.desc[i].len = sg->length;
183                 prev = i;
184                 sg++;
185         }
186         for (; in; i = vq->vring.desc[i].next, in--) {
187                 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
188                 vq->vring.desc[i].addr = sg_phys(sg);
189                 vq->vring.desc[i].len = sg->length;
190                 prev = i;
191                 sg++;
192         }
193         /* Last one doesn't continue. */
194         vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
195
196         /* Update free pointer */
197         vq->free_head = i;
198
199 add_head:
200         /* Set token. */
201         vq->data[head] = data;
202
203         /* Put entry in available array (but don't update avail->idx until they
204          * do sync).  FIXME: avoid modulus here? */
205         avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
206         vq->vring.avail->ring[avail] = head;
207
208         pr_debug("Added buffer head %i to %p\n", head, vq);
209         END_USE(vq);
210
211         /* If we're indirect, we can fit many (assuming not OOM). */
212         if (vq->indirect)
213                 return vq->num_free ? vq->vring.num : 0;
214         return vq->num_free;
215 }
216
217 static void vring_kick(struct virtqueue *_vq)
218 {
219         struct vring_virtqueue *vq = to_vvq(_vq);
220         START_USE(vq);
221         /* Descriptors and available array need to be set before we expose the
222          * new available array entries. */
223         wmb();
224
225         vq->vring.avail->idx += vq->num_added;
226         vq->num_added = 0;
227
228         /* Need to update avail index before checking if we should notify */
229         mb();
230
231         if (!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
232                 /* Prod other side to tell it about changes. */
233                 vq->notify(&vq->vq);
234
235         END_USE(vq);
236 }
237
238 static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
239 {
240         unsigned int i;
241
242         /* Clear data ptr. */
243         vq->data[head] = NULL;
244
245         /* Put back on free list: find end */
246         i = head;
247
248         /* Free the indirect table */
249         if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
250                 kfree(phys_to_virt(vq->vring.desc[i].addr));
251
252         while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
253                 i = vq->vring.desc[i].next;
254                 vq->num_free++;
255         }
256
257         vq->vring.desc[i].next = vq->free_head;
258         vq->free_head = head;
259         /* Plus final descriptor */
260         vq->num_free++;
261 }
262
263 static inline bool more_used(const struct vring_virtqueue *vq)
264 {
265         return vq->last_used_idx != vq->vring.used->idx;
266 }
267
268 static void *vring_get_buf(struct virtqueue *_vq, unsigned int *len)
269 {
270         struct vring_virtqueue *vq = to_vvq(_vq);
271         void *ret;
272         unsigned int i;
273
274         START_USE(vq);
275
276         if (unlikely(vq->broken)) {
277                 END_USE(vq);
278                 return NULL;
279         }
280
281         if (!more_used(vq)) {
282                 pr_debug("No more buffers in queue\n");
283                 END_USE(vq);
284                 return NULL;
285         }
286
287         /* Only get used array entries after they have been exposed by host. */
288         rmb();
289
290         i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
291         *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
292
293         if (unlikely(i >= vq->vring.num)) {
294                 BAD_RING(vq, "id %u out of range\n", i);
295                 return NULL;
296         }
297         if (unlikely(!vq->data[i])) {
298                 BAD_RING(vq, "id %u is not a head!\n", i);
299                 return NULL;
300         }
301
302         /* detach_buf clears data, so grab it now. */
303         ret = vq->data[i];
304         detach_buf(vq, i);
305         vq->last_used_idx++;
306         END_USE(vq);
307         return ret;
308 }
309
310 static void vring_disable_cb(struct virtqueue *_vq)
311 {
312         struct vring_virtqueue *vq = to_vvq(_vq);
313
314         vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
315 }
316
317 static bool vring_enable_cb(struct virtqueue *_vq)
318 {
319         struct vring_virtqueue *vq = to_vvq(_vq);
320
321         START_USE(vq);
322
323         /* We optimistically turn back on interrupts, then check if there was
324          * more to do. */
325         vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
326         mb();
327         if (unlikely(more_used(vq))) {
328                 END_USE(vq);
329                 return false;
330         }
331
332         END_USE(vq);
333         return true;
334 }
335
336 irqreturn_t vring_interrupt(int irq, void *_vq)
337 {
338         struct vring_virtqueue *vq = to_vvq(_vq);
339
340         if (!more_used(vq)) {
341                 pr_debug("virtqueue interrupt with no work for %p\n", vq);
342                 return IRQ_NONE;
343         }
344
345         if (unlikely(vq->broken))
346                 return IRQ_HANDLED;
347
348         pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
349         if (vq->vq.callback)
350                 vq->vq.callback(&vq->vq);
351
352         return IRQ_HANDLED;
353 }
354 EXPORT_SYMBOL_GPL(vring_interrupt);
355
356 static struct virtqueue_ops vring_vq_ops = {
357         .add_buf = vring_add_buf,
358         .get_buf = vring_get_buf,
359         .kick = vring_kick,
360         .disable_cb = vring_disable_cb,
361         .enable_cb = vring_enable_cb,
362 };
363
364 struct virtqueue *vring_new_virtqueue(unsigned int num,
365                                       unsigned int vring_align,
366                                       struct virtio_device *vdev,
367                                       void *pages,
368                                       void (*notify)(struct virtqueue *),
369                                       void (*callback)(struct virtqueue *),
370                                       const char *name)
371 {
372         struct vring_virtqueue *vq;
373         unsigned int i;
374
375         /* We assume num is a power of 2. */
376         if (num & (num - 1)) {
377                 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
378                 return NULL;
379         }
380
381         vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
382         if (!vq)
383                 return NULL;
384
385         vring_init(&vq->vring, num, pages, vring_align);
386         vq->vq.callback = callback;
387         vq->vq.vdev = vdev;
388         vq->vq.vq_ops = &vring_vq_ops;
389         vq->vq.name = name;
390         vq->notify = notify;
391         vq->broken = false;
392         vq->last_used_idx = 0;
393         vq->num_added = 0;
394         list_add_tail(&vq->vq.list, &vdev->vqs);
395 #ifdef DEBUG
396         vq->in_use = false;
397 #endif
398
399         vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
400
401         /* No callback?  Tell other side not to bother us. */
402         if (!callback)
403                 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
404
405         /* Put everything in free lists. */
406         vq->num_free = num;
407         vq->free_head = 0;
408         for (i = 0; i < num-1; i++)
409                 vq->vring.desc[i].next = i+1;
410
411         return &vq->vq;
412 }
413 EXPORT_SYMBOL_GPL(vring_new_virtqueue);
414
415 void vring_del_virtqueue(struct virtqueue *vq)
416 {
417         list_del(&vq->list);
418         kfree(to_vvq(vq));
419 }
420 EXPORT_SYMBOL_GPL(vring_del_virtqueue);
421
422 /* Manipulates transport-specific feature bits. */
423 void vring_transport_features(struct virtio_device *vdev)
424 {
425         unsigned int i;
426
427         for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
428                 switch (i) {
429                 case VIRTIO_RING_F_INDIRECT_DESC:
430                         break;
431                 default:
432                         /* We don't understand this bit. */
433                         clear_bit(i, vdev->features);
434                 }
435         }
436 }
437 EXPORT_SYMBOL_GPL(vring_transport_features);
438
439 MODULE_LICENSE("GPL");