blob: 0f90634bcb85d170e64ce6e2addee12f5fb37f82 [file] [log] [blame]
Rusty Russell0a8a69d2007-10-22 11:03:40 +10001/* 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>
Rusty Russelle34f8722008-07-25 12:06:13 -050021#include <linux/virtio_config.h>
Rusty Russell0a8a69d2007-10-22 11:03:40 +100022#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Rusty Russell0a8a69d2007-10-22 11:03:40 +100024
Michael S. Tsirkind57ed952010-01-28 00:42:23 +020025/* 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
Rusty Russell0a8a69d2007-10-22 11:03:40 +100043#ifdef DEBUG
44/* For development, we want to crash whenever the ring is screwed. */
Rusty Russell9499f5e2009-06-12 22:16:35 -060045#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)
Rusty Russellc5f841f2009-03-30 21:55:22 -060051/* Caller is supposed to guarantee no reentry. */
52#define START_USE(_vq) \
53 do { \
54 if ((_vq)->in_use) \
Rusty Russell9499f5e2009-06-12 22:16:35 -060055 panic("%s:in_use = %i\n", \
56 (_vq)->vq.name, (_vq)->in_use); \
Rusty Russellc5f841f2009-03-30 21:55:22 -060057 (_vq)->in_use = __LINE__; \
Rusty Russell9499f5e2009-06-12 22:16:35 -060058 } while (0)
Roel Kluin3a35ce72009-01-22 16:42:57 +010059#define END_USE(_vq) \
Rusty Russell97a545a2010-02-24 14:22:22 -060060 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100061#else
Rusty Russell9499f5e2009-06-12 22:16:35 -060062#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)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100068#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
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +010082 /* Host supports indirect buffers */
83 bool indirect;
84
Rusty Russell0a8a69d2007-10-22 11:03:40 +100085 /* 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. */
Anthony Liguori1bc49532007-11-07 15:49:24 -060093 u16 last_used_idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +100094
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
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100109/* 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,
113 unsigned int in)
114{
115 struct vring_desc *desc;
116 unsigned head;
117 int i;
118
119 desc = kmalloc((out + in) * sizeof(struct vring_desc), GFP_ATOMIC);
120 if (!desc)
121 return vq->vring.num;
122
123 /* Transfer entries from the sg list into the indirect page */
124 for (i = 0; i < out; i++) {
125 desc[i].flags = VRING_DESC_F_NEXT;
126 desc[i].addr = sg_phys(sg);
127 desc[i].len = sg->length;
128 desc[i].next = i+1;
129 sg++;
130 }
131 for (; i < (out + in); i++) {
132 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
133 desc[i].addr = sg_phys(sg);
134 desc[i].len = sg->length;
135 desc[i].next = i+1;
136 sg++;
137 }
138
139 /* Last one doesn't continue. */
140 desc[i-1].flags &= ~VRING_DESC_F_NEXT;
141 desc[i-1].next = 0;
142
143 /* We're about to use a buffer */
144 vq->num_free--;
145
146 /* Use a single buffer which doesn't continue */
147 head = vq->free_head;
148 vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
149 vq->vring.desc[head].addr = virt_to_phys(desc);
150 vq->vring.desc[head].len = i * sizeof(struct vring_desc);
151
152 /* Update free pointer */
153 vq->free_head = vq->vring.desc[head].next;
154
155 return head;
156}
157
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000158static int vring_add_buf(struct virtqueue *_vq,
159 struct scatterlist sg[],
160 unsigned int out,
161 unsigned int in,
162 void *data)
163{
164 struct vring_virtqueue *vq = to_vvq(_vq);
165 unsigned int i, avail, head, uninitialized_var(prev);
166
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100167 START_USE(vq);
168
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000169 BUG_ON(data == NULL);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100170
171 /* If the host supports indirect descriptor tables, and we have multiple
172 * buffers, then go indirect. FIXME: tune this threshold */
173 if (vq->indirect && (out + in) > 1 && vq->num_free) {
174 head = vring_add_indirect(vq, sg, out, in);
175 if (head != vq->vring.num)
176 goto add_head;
177 }
178
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000179 BUG_ON(out + in > vq->vring.num);
180 BUG_ON(out + in == 0);
181
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000182 if (vq->num_free < out + in) {
183 pr_debug("Can't add buf len %i - avail = %i\n",
184 out + in, vq->num_free);
Rusty Russell44653ea2008-07-25 12:06:04 -0500185 /* FIXME: for historical reasons, we force a notify here if
186 * there are outgoing parts to the buffer. Presumably the
187 * host should service the ring ASAP. */
188 if (out)
189 vq->notify(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000190 END_USE(vq);
191 return -ENOSPC;
192 }
193
194 /* We're about to use some buffers from the free list. */
195 vq->num_free -= out + in;
196
197 head = vq->free_head;
198 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
199 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
Rusty Russell15f9c892008-02-04 23:50:05 -0500200 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000201 vq->vring.desc[i].len = sg->length;
202 prev = i;
203 sg++;
204 }
205 for (; in; i = vq->vring.desc[i].next, in--) {
206 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
Rusty Russell15f9c892008-02-04 23:50:05 -0500207 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000208 vq->vring.desc[i].len = sg->length;
209 prev = i;
210 sg++;
211 }
212 /* Last one doesn't continue. */
213 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
214
215 /* Update free pointer */
216 vq->free_head = i;
217
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100218add_head:
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000219 /* Set token. */
220 vq->data[head] = data;
221
222 /* Put entry in available array (but don't update avail->idx until they
223 * do sync). FIXME: avoid modulus here? */
224 avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
225 vq->vring.avail->ring[avail] = head;
226
227 pr_debug("Added buffer head %i to %p\n", head, vq);
228 END_USE(vq);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600229
230 /* If we're indirect, we can fit many (assuming not OOM). */
231 if (vq->indirect)
232 return vq->num_free ? vq->vring.num : 0;
233 return vq->num_free;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000234}
235
236static void vring_kick(struct virtqueue *_vq)
237{
238 struct vring_virtqueue *vq = to_vvq(_vq);
239 START_USE(vq);
240 /* Descriptors and available array need to be set before we expose the
241 * new available array entries. */
Michael S. Tsirkind57ed952010-01-28 00:42:23 +0200242 virtio_wmb();
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000243
244 vq->vring.avail->idx += vq->num_added;
245 vq->num_added = 0;
246
247 /* Need to update avail index before checking if we should notify */
Michael S. Tsirkind57ed952010-01-28 00:42:23 +0200248 virtio_mb();
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000249
250 if (!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
251 /* Prod other side to tell it about changes. */
252 vq->notify(&vq->vq);
253
254 END_USE(vq);
255}
256
257static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
258{
259 unsigned int i;
260
261 /* Clear data ptr. */
262 vq->data[head] = NULL;
263
264 /* Put back on free list: find end */
265 i = head;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100266
267 /* Free the indirect table */
268 if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
269 kfree(phys_to_virt(vq->vring.desc[i].addr));
270
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000271 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
272 i = vq->vring.desc[i].next;
273 vq->num_free++;
274 }
275
276 vq->vring.desc[i].next = vq->free_head;
277 vq->free_head = head;
278 /* Plus final descriptor */
279 vq->num_free++;
280}
281
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000282static inline bool more_used(const struct vring_virtqueue *vq)
283{
284 return vq->last_used_idx != vq->vring.used->idx;
285}
286
287static void *vring_get_buf(struct virtqueue *_vq, unsigned int *len)
288{
289 struct vring_virtqueue *vq = to_vvq(_vq);
290 void *ret;
291 unsigned int i;
292
293 START_USE(vq);
294
Rusty Russell5ef82752008-05-02 21:50:43 -0500295 if (unlikely(vq->broken)) {
296 END_USE(vq);
297 return NULL;
298 }
299
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000300 if (!more_used(vq)) {
301 pr_debug("No more buffers in queue\n");
302 END_USE(vq);
303 return NULL;
304 }
305
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200306 /* Only get used array entries after they have been exposed by host. */
Michael S. Tsirkind57ed952010-01-28 00:42:23 +0200307 virtio_rmb();
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200308
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000309 i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
310 *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
311
312 if (unlikely(i >= vq->vring.num)) {
313 BAD_RING(vq, "id %u out of range\n", i);
314 return NULL;
315 }
316 if (unlikely(!vq->data[i])) {
317 BAD_RING(vq, "id %u is not a head!\n", i);
318 return NULL;
319 }
320
321 /* detach_buf clears data, so grab it now. */
322 ret = vq->data[i];
323 detach_buf(vq, i);
324 vq->last_used_idx++;
325 END_USE(vq);
326 return ret;
327}
328
Rusty Russell18445c42008-02-04 23:49:57 -0500329static void vring_disable_cb(struct virtqueue *_vq)
330{
331 struct vring_virtqueue *vq = to_vvq(_vq);
332
Rusty Russell18445c42008-02-04 23:49:57 -0500333 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
Rusty Russell18445c42008-02-04 23:49:57 -0500334}
335
336static bool vring_enable_cb(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000337{
338 struct vring_virtqueue *vq = to_vvq(_vq);
339
340 START_USE(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000341
342 /* We optimistically turn back on interrupts, then check if there was
343 * more to do. */
344 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
Michael S. Tsirkind57ed952010-01-28 00:42:23 +0200345 virtio_mb();
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000346 if (unlikely(more_used(vq))) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000347 END_USE(vq);
348 return false;
349 }
350
351 END_USE(vq);
352 return true;
353}
354
Shirley Mac021eac2010-01-18 19:15:23 +0530355static void *vring_detach_unused_buf(struct virtqueue *_vq)
356{
357 struct vring_virtqueue *vq = to_vvq(_vq);
358 unsigned int i;
359 void *buf;
360
361 START_USE(vq);
362
363 for (i = 0; i < vq->vring.num; i++) {
364 if (!vq->data[i])
365 continue;
366 /* detach_buf clears data, so grab it now. */
367 buf = vq->data[i];
368 detach_buf(vq, i);
369 END_USE(vq);
370 return buf;
371 }
372 /* That should have freed everything. */
373 BUG_ON(vq->num_free != vq->vring.num);
374
375 END_USE(vq);
376 return NULL;
377}
378
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000379irqreturn_t vring_interrupt(int irq, void *_vq)
380{
381 struct vring_virtqueue *vq = to_vvq(_vq);
382
383 if (!more_used(vq)) {
384 pr_debug("virtqueue interrupt with no work for %p\n", vq);
385 return IRQ_NONE;
386 }
387
388 if (unlikely(vq->broken))
389 return IRQ_HANDLED;
390
391 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
Rusty Russell18445c42008-02-04 23:49:57 -0500392 if (vq->vq.callback)
393 vq->vq.callback(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000394
395 return IRQ_HANDLED;
396}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500397EXPORT_SYMBOL_GPL(vring_interrupt);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000398
399static struct virtqueue_ops vring_vq_ops = {
400 .add_buf = vring_add_buf,
401 .get_buf = vring_get_buf,
402 .kick = vring_kick,
Rusty Russell18445c42008-02-04 23:49:57 -0500403 .disable_cb = vring_disable_cb,
404 .enable_cb = vring_enable_cb,
Shirley Mac021eac2010-01-18 19:15:23 +0530405 .detach_unused_buf = vring_detach_unused_buf,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000406};
407
408struct virtqueue *vring_new_virtqueue(unsigned int num,
Rusty Russell87c7d572008-12-30 09:26:03 -0600409 unsigned int vring_align,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000410 struct virtio_device *vdev,
411 void *pages,
412 void (*notify)(struct virtqueue *),
Rusty Russell9499f5e2009-06-12 22:16:35 -0600413 void (*callback)(struct virtqueue *),
414 const char *name)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000415{
416 struct vring_virtqueue *vq;
417 unsigned int i;
418
Rusty Russell42b36cc2007-11-12 13:39:18 +1100419 /* We assume num is a power of 2. */
420 if (num & (num - 1)) {
421 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
422 return NULL;
423 }
424
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000425 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
426 if (!vq)
427 return NULL;
428
Rusty Russell87c7d572008-12-30 09:26:03 -0600429 vring_init(&vq->vring, num, pages, vring_align);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000430 vq->vq.callback = callback;
431 vq->vq.vdev = vdev;
432 vq->vq.vq_ops = &vring_vq_ops;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600433 vq->vq.name = name;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000434 vq->notify = notify;
435 vq->broken = false;
436 vq->last_used_idx = 0;
437 vq->num_added = 0;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600438 list_add_tail(&vq->vq.list, &vdev->vqs);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000439#ifdef DEBUG
440 vq->in_use = false;
441#endif
442
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100443 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
444
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000445 /* No callback? Tell other side not to bother us. */
446 if (!callback)
447 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
448
449 /* Put everything in free lists. */
450 vq->num_free = num;
451 vq->free_head = 0;
Amit Shah3b870622010-02-12 10:32:14 +0530452 for (i = 0; i < num-1; i++) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000453 vq->vring.desc[i].next = i+1;
Amit Shah3b870622010-02-12 10:32:14 +0530454 vq->data[i] = NULL;
455 }
456 vq->data[i] = NULL;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000457
458 return &vq->vq;
459}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500460EXPORT_SYMBOL_GPL(vring_new_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000461
462void vring_del_virtqueue(struct virtqueue *vq)
463{
Rusty Russell9499f5e2009-06-12 22:16:35 -0600464 list_del(&vq->list);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000465 kfree(to_vvq(vq));
466}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500467EXPORT_SYMBOL_GPL(vring_del_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000468
Rusty Russelle34f8722008-07-25 12:06:13 -0500469/* Manipulates transport-specific feature bits. */
470void vring_transport_features(struct virtio_device *vdev)
471{
472 unsigned int i;
473
474 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
475 switch (i) {
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100476 case VIRTIO_RING_F_INDIRECT_DESC:
477 break;
Rusty Russelle34f8722008-07-25 12:06:13 -0500478 default:
479 /* We don't understand this bit. */
480 clear_bit(i, vdev->features);
481 }
482 }
483}
484EXPORT_SYMBOL_GPL(vring_transport_features);
485
Rusty Russellc6fd4702008-02-04 23:50:05 -0500486MODULE_LICENSE("GPL");