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