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