virtio_pci: split up vp_interrupt
[safe/jmp/linux-2.6] / 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                 mb();                                           \
40         } while (0)
41 #define END_USE(_vq) \
42         do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; mb(); } while(0)
43 #else
44 #define BAD_RING(_vq, fmt, args...)                             \
45         do {                                                    \
46                 dev_err(&_vq->vq.vdev->dev,                     \
47                         "%s:"fmt, (_vq)->vq.name, ##args);      \
48                 (_vq)->broken = true;                           \
49         } while (0)
50 #define START_USE(vq)
51 #define END_USE(vq)
52 #endif
53
54 struct vring_virtqueue
55 {
56         struct virtqueue vq;
57
58         /* Actual memory layout for this queue */
59         struct vring vring;
60
61         /* Other side has made a mess, don't try any more. */
62         bool broken;
63
64         /* Number of free buffers */
65         unsigned int num_free;
66         /* Head of free buffer list. */
67         unsigned int free_head;
68         /* Number we've added since last sync. */
69         unsigned int num_added;
70
71         /* Last used index we've seen. */
72         u16 last_used_idx;
73
74         /* How to notify other side. FIXME: commonalize hcalls! */
75         void (*notify)(struct virtqueue *vq);
76
77 #ifdef DEBUG
78         /* They're supposed to lock for us. */
79         unsigned int in_use;
80 #endif
81
82         /* Tokens for callbacks. */
83         void *data[];
84 };
85
86 #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
87
88 static int vring_add_buf(struct virtqueue *_vq,
89                          struct scatterlist sg[],
90                          unsigned int out,
91                          unsigned int in,
92                          void *data)
93 {
94         struct vring_virtqueue *vq = to_vvq(_vq);
95         unsigned int i, avail, head, uninitialized_var(prev);
96
97         BUG_ON(data == NULL);
98         BUG_ON(out + in > vq->vring.num);
99         BUG_ON(out + in == 0);
100
101         START_USE(vq);
102
103         if (vq->num_free < out + in) {
104                 pr_debug("Can't add buf len %i - avail = %i\n",
105                          out + in, vq->num_free);
106                 /* FIXME: for historical reasons, we force a notify here if
107                  * there are outgoing parts to the buffer.  Presumably the
108                  * host should service the ring ASAP. */
109                 if (out)
110                         vq->notify(&vq->vq);
111                 END_USE(vq);
112                 return -ENOSPC;
113         }
114
115         /* We're about to use some buffers from the free list. */
116         vq->num_free -= out + in;
117
118         head = vq->free_head;
119         for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
120                 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
121                 vq->vring.desc[i].addr = sg_phys(sg);
122                 vq->vring.desc[i].len = sg->length;
123                 prev = i;
124                 sg++;
125         }
126         for (; in; i = vq->vring.desc[i].next, in--) {
127                 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
128                 vq->vring.desc[i].addr = sg_phys(sg);
129                 vq->vring.desc[i].len = sg->length;
130                 prev = i;
131                 sg++;
132         }
133         /* Last one doesn't continue. */
134         vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
135
136         /* Update free pointer */
137         vq->free_head = i;
138
139         /* Set token. */
140         vq->data[head] = data;
141
142         /* Put entry in available array (but don't update avail->idx until they
143          * do sync).  FIXME: avoid modulus here? */
144         avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
145         vq->vring.avail->ring[avail] = head;
146
147         pr_debug("Added buffer head %i to %p\n", head, vq);
148         END_USE(vq);
149         return 0;
150 }
151
152 static void vring_kick(struct virtqueue *_vq)
153 {
154         struct vring_virtqueue *vq = to_vvq(_vq);
155         START_USE(vq);
156         /* Descriptors and available array need to be set before we expose the
157          * new available array entries. */
158         wmb();
159
160         vq->vring.avail->idx += vq->num_added;
161         vq->num_added = 0;
162
163         /* Need to update avail index before checking if we should notify */
164         mb();
165
166         if (!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
167                 /* Prod other side to tell it about changes. */
168                 vq->notify(&vq->vq);
169
170         END_USE(vq);
171 }
172
173 static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
174 {
175         unsigned int i;
176
177         /* Clear data ptr. */
178         vq->data[head] = NULL;
179
180         /* Put back on free list: find end */
181         i = head;
182         while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
183                 i = vq->vring.desc[i].next;
184                 vq->num_free++;
185         }
186
187         vq->vring.desc[i].next = vq->free_head;
188         vq->free_head = head;
189         /* Plus final descriptor */
190         vq->num_free++;
191 }
192
193 static inline bool more_used(const struct vring_virtqueue *vq)
194 {
195         return vq->last_used_idx != vq->vring.used->idx;
196 }
197
198 static void *vring_get_buf(struct virtqueue *_vq, unsigned int *len)
199 {
200         struct vring_virtqueue *vq = to_vvq(_vq);
201         void *ret;
202         unsigned int i;
203
204         START_USE(vq);
205
206         if (unlikely(vq->broken)) {
207                 END_USE(vq);
208                 return NULL;
209         }
210
211         if (!more_used(vq)) {
212                 pr_debug("No more buffers in queue\n");
213                 END_USE(vq);
214                 return NULL;
215         }
216
217         i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
218         *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
219
220         if (unlikely(i >= vq->vring.num)) {
221                 BAD_RING(vq, "id %u out of range\n", i);
222                 return NULL;
223         }
224         if (unlikely(!vq->data[i])) {
225                 BAD_RING(vq, "id %u is not a head!\n", i);
226                 return NULL;
227         }
228
229         /* detach_buf clears data, so grab it now. */
230         ret = vq->data[i];
231         detach_buf(vq, i);
232         vq->last_used_idx++;
233         END_USE(vq);
234         return ret;
235 }
236
237 static void vring_disable_cb(struct virtqueue *_vq)
238 {
239         struct vring_virtqueue *vq = to_vvq(_vq);
240
241         vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
242 }
243
244 static bool vring_enable_cb(struct virtqueue *_vq)
245 {
246         struct vring_virtqueue *vq = to_vvq(_vq);
247
248         START_USE(vq);
249
250         /* We optimistically turn back on interrupts, then check if there was
251          * more to do. */
252         vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
253         mb();
254         if (unlikely(more_used(vq))) {
255                 END_USE(vq);
256                 return false;
257         }
258
259         END_USE(vq);
260         return true;
261 }
262
263 irqreturn_t vring_interrupt(int irq, void *_vq)
264 {
265         struct vring_virtqueue *vq = to_vvq(_vq);
266
267         if (!more_used(vq)) {
268                 pr_debug("virtqueue interrupt with no work for %p\n", vq);
269                 return IRQ_NONE;
270         }
271
272         if (unlikely(vq->broken))
273                 return IRQ_HANDLED;
274
275         pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
276         if (vq->vq.callback)
277                 vq->vq.callback(&vq->vq);
278
279         return IRQ_HANDLED;
280 }
281 EXPORT_SYMBOL_GPL(vring_interrupt);
282
283 static struct virtqueue_ops vring_vq_ops = {
284         .add_buf = vring_add_buf,
285         .get_buf = vring_get_buf,
286         .kick = vring_kick,
287         .disable_cb = vring_disable_cb,
288         .enable_cb = vring_enable_cb,
289 };
290
291 struct virtqueue *vring_new_virtqueue(unsigned int num,
292                                       unsigned int vring_align,
293                                       struct virtio_device *vdev,
294                                       void *pages,
295                                       void (*notify)(struct virtqueue *),
296                                       void (*callback)(struct virtqueue *),
297                                       const char *name)
298 {
299         struct vring_virtqueue *vq;
300         unsigned int i;
301
302         /* We assume num is a power of 2. */
303         if (num & (num - 1)) {
304                 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
305                 return NULL;
306         }
307
308         vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
309         if (!vq)
310                 return NULL;
311
312         vring_init(&vq->vring, num, pages, vring_align);
313         vq->vq.callback = callback;
314         vq->vq.vdev = vdev;
315         vq->vq.vq_ops = &vring_vq_ops;
316         vq->vq.name = name;
317         vq->notify = notify;
318         vq->broken = false;
319         vq->last_used_idx = 0;
320         vq->num_added = 0;
321         list_add_tail(&vq->vq.list, &vdev->vqs);
322 #ifdef DEBUG
323         vq->in_use = false;
324 #endif
325
326         /* No callback?  Tell other side not to bother us. */
327         if (!callback)
328                 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
329
330         /* Put everything in free lists. */
331         vq->num_free = num;
332         vq->free_head = 0;
333         for (i = 0; i < num-1; i++)
334                 vq->vring.desc[i].next = i+1;
335
336         return &vq->vq;
337 }
338 EXPORT_SYMBOL_GPL(vring_new_virtqueue);
339
340 void vring_del_virtqueue(struct virtqueue *vq)
341 {
342         list_del(&vq->list);
343         kfree(to_vvq(vq));
344 }
345 EXPORT_SYMBOL_GPL(vring_del_virtqueue);
346
347 /* Manipulates transport-specific feature bits. */
348 void vring_transport_features(struct virtio_device *vdev)
349 {
350         unsigned int i;
351
352         for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
353                 switch (i) {
354                 default:
355                         /* We don't understand this bit. */
356                         clear_bit(i, vdev->features);
357                 }
358         }
359 }
360 EXPORT_SYMBOL_GPL(vring_transport_features);
361
362 MODULE_LICENSE("GPL");