]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/virtio/virtio_ring.c
virtio: remove bogus barriers from DEBUG version of virtio_ring.c
[net-next-2.6.git] / drivers / virtio / virtio_ring.c
CommitLineData
0a8a69dd
RR
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>
e34f8725 21#include <linux/virtio_config.h>
0a8a69dd
RR
22#include <linux/device.h>
23
24#ifdef DEBUG
25/* For development, we want to crash whenever the ring is screwed. */
9499f5e7
RR
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)
c5f841f1
RR
32/* Caller is supposed to guarantee no reentry. */
33#define START_USE(_vq) \
34 do { \
35 if ((_vq)->in_use) \
9499f5e7
RR
36 panic("%s:in_use = %i\n", \
37 (_vq)->vq.name, (_vq)->in_use); \
c5f841f1 38 (_vq)->in_use = __LINE__; \
9499f5e7 39 } while (0)
3a35ce7d 40#define END_USE(_vq) \
97a545ab 41 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
0a8a69dd 42#else
9499f5e7
RR
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)
0a8a69dd
RR
49#define START_USE(vq)
50#define END_USE(vq)
51#endif
52
53struct 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
9fa29b9d
MM
63 /* Host supports indirect buffers */
64 bool indirect;
65
0a8a69dd
RR
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. */
1bc4953e 74 u16 last_used_idx;
0a8a69dd
RR
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
9fa29b9d
MM
90/* Set up an indirect table of descriptors and add it to the queue. */
91static 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
0a8a69dd
RR
139static 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
9fa29b9d
MM
148 START_USE(vq);
149
0a8a69dd 150 BUG_ON(data == NULL);
9fa29b9d
MM
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
0a8a69dd
RR
160 BUG_ON(out + in > vq->vring.num);
161 BUG_ON(out + in == 0);
162
0a8a69dd
RR
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);
44653eae
RR
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);
0a8a69dd
RR
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;
15f9c890 181 vq->vring.desc[i].addr = sg_phys(sg);
0a8a69dd
RR
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;
15f9c890 188 vq->vring.desc[i].addr = sg_phys(sg);
0a8a69dd
RR
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
9fa29b9d 199add_head:
0a8a69dd
RR
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);
3c1b27d5
RR
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;
0a8a69dd
RR
215}
216
217static 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
238static 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;
9fa29b9d
MM
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
0a8a69dd
RR
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
0a8a69dd
RR
263static inline bool more_used(const struct vring_virtqueue *vq)
264{
265 return vq->last_used_idx != vq->vring.used->idx;
266}
267
268static 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
5ef82752
RR
276 if (unlikely(vq->broken)) {
277 END_USE(vq);
278 return NULL;
279 }
280
0a8a69dd
RR
281 if (!more_used(vq)) {
282 pr_debug("No more buffers in queue\n");
283 END_USE(vq);
284 return NULL;
285 }
286
2d61ba95
MT
287 /* Only get used array entries after they have been exposed by host. */
288 rmb();
289
0a8a69dd
RR
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
18445c4d
RR
310static void vring_disable_cb(struct virtqueue *_vq)
311{
312 struct vring_virtqueue *vq = to_vvq(_vq);
313
18445c4d 314 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
18445c4d
RR
315}
316
317static bool vring_enable_cb(struct virtqueue *_vq)
0a8a69dd
RR
318{
319 struct vring_virtqueue *vq = to_vvq(_vq);
320
321 START_USE(vq);
0a8a69dd
RR
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))) {
0a8a69dd
RR
328 END_USE(vq);
329 return false;
330 }
331
332 END_USE(vq);
333 return true;
334}
335
336irqreturn_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);
18445c4d
RR
349 if (vq->vq.callback)
350 vq->vq.callback(&vq->vq);
0a8a69dd
RR
351
352 return IRQ_HANDLED;
353}
c6fd4701 354EXPORT_SYMBOL_GPL(vring_interrupt);
0a8a69dd
RR
355
356static struct virtqueue_ops vring_vq_ops = {
357 .add_buf = vring_add_buf,
358 .get_buf = vring_get_buf,
359 .kick = vring_kick,
18445c4d
RR
360 .disable_cb = vring_disable_cb,
361 .enable_cb = vring_enable_cb,
0a8a69dd
RR
362};
363
364struct virtqueue *vring_new_virtqueue(unsigned int num,
87c7d57c 365 unsigned int vring_align,
0a8a69dd
RR
366 struct virtio_device *vdev,
367 void *pages,
368 void (*notify)(struct virtqueue *),
9499f5e7
RR
369 void (*callback)(struct virtqueue *),
370 const char *name)
0a8a69dd
RR
371{
372 struct vring_virtqueue *vq;
373 unsigned int i;
374
42b36cc0
RR
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
0a8a69dd
RR
381 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
382 if (!vq)
383 return NULL;
384
87c7d57c 385 vring_init(&vq->vring, num, pages, vring_align);
0a8a69dd
RR
386 vq->vq.callback = callback;
387 vq->vq.vdev = vdev;
388 vq->vq.vq_ops = &vring_vq_ops;
9499f5e7 389 vq->vq.name = name;
0a8a69dd
RR
390 vq->notify = notify;
391 vq->broken = false;
392 vq->last_used_idx = 0;
393 vq->num_added = 0;
9499f5e7 394 list_add_tail(&vq->vq.list, &vdev->vqs);
0a8a69dd
RR
395#ifdef DEBUG
396 vq->in_use = false;
397#endif
398
9fa29b9d
MM
399 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
400
0a8a69dd
RR
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}
c6fd4701 413EXPORT_SYMBOL_GPL(vring_new_virtqueue);
0a8a69dd
RR
414
415void vring_del_virtqueue(struct virtqueue *vq)
416{
9499f5e7 417 list_del(&vq->list);
0a8a69dd
RR
418 kfree(to_vvq(vq));
419}
c6fd4701 420EXPORT_SYMBOL_GPL(vring_del_virtqueue);
0a8a69dd 421
e34f8725
RR
422/* Manipulates transport-specific feature bits. */
423void 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) {
9fa29b9d
MM
429 case VIRTIO_RING_F_INDIRECT_DESC:
430 break;
e34f8725
RR
431 default:
432 /* We don't understand this bit. */
433 clear_bit(i, vdev->features);
434 }
435 }
436}
437EXPORT_SYMBOL_GPL(vring_transport_features);
438
c6fd4701 439MODULE_LICENSE("GPL");