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