blob: 827f7e0426108dcf6b9a9c8bc04b3ab7a337db39 [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>
23
Michael S. Tsirkind57ed952010-01-28 00:42:23 +020024/* virtio guest is communicating with a virtual "device" that actually runs on
25 * a host processor. Memory barriers are used to control SMP effects. */
26#ifdef CONFIG_SMP
27/* Where possible, use SMP barriers which are more lightweight than mandatory
28 * barriers, because mandatory barriers control MMIO effects on accesses
29 * through relaxed memory I/O windows (which virtio does not use). */
30#define virtio_mb() smp_mb()
31#define virtio_rmb() smp_rmb()
32#define virtio_wmb() smp_wmb()
33#else
34/* We must force memory ordering even if guest is UP since host could be
35 * running on another CPU, but SMP barriers are defined to barrier() in that
36 * configuration. So fall back to mandatory barriers instead. */
37#define virtio_mb() mb()
38#define virtio_rmb() rmb()
39#define virtio_wmb() wmb()
40#endif
41
Rusty Russell0a8a69d2007-10-22 11:03:40 +100042#ifdef DEBUG
43/* For development, we want to crash whenever the ring is screwed. */
Rusty Russell9499f5e2009-06-12 22:16:35 -060044#define BAD_RING(_vq, fmt, args...) \
45 do { \
46 dev_err(&(_vq)->vq.vdev->dev, \
47 "%s:"fmt, (_vq)->vq.name, ##args); \
48 BUG(); \
49 } while (0)
Rusty Russellc5f841f2009-03-30 21:55:22 -060050/* Caller is supposed to guarantee no reentry. */
51#define START_USE(_vq) \
52 do { \
53 if ((_vq)->in_use) \
Rusty Russell9499f5e2009-06-12 22:16:35 -060054 panic("%s:in_use = %i\n", \
55 (_vq)->vq.name, (_vq)->in_use); \
Rusty Russellc5f841f2009-03-30 21:55:22 -060056 (_vq)->in_use = __LINE__; \
Rusty Russell9499f5e2009-06-12 22:16:35 -060057 } while (0)
Roel Kluin3a35ce72009-01-22 16:42:57 +010058#define END_USE(_vq) \
Rusty Russell97a545a2010-02-24 14:22:22 -060059 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100060#else
Rusty Russell9499f5e2009-06-12 22:16:35 -060061#define BAD_RING(_vq, fmt, args...) \
62 do { \
63 dev_err(&_vq->vq.vdev->dev, \
64 "%s:"fmt, (_vq)->vq.name, ##args); \
65 (_vq)->broken = true; \
66 } while (0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100067#define START_USE(vq)
68#define END_USE(vq)
69#endif
70
71struct vring_virtqueue
72{
73 struct virtqueue vq;
74
75 /* Actual memory layout for this queue */
76 struct vring vring;
77
78 /* Other side has made a mess, don't try any more. */
79 bool broken;
80
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +010081 /* Host supports indirect buffers */
82 bool indirect;
83
Rusty Russell0a8a69d2007-10-22 11:03:40 +100084 /* Number of free buffers */
85 unsigned int num_free;
86 /* Head of free buffer list. */
87 unsigned int free_head;
88 /* Number we've added since last sync. */
89 unsigned int num_added;
90
91 /* Last used index we've seen. */
Anthony Liguori1bc49532007-11-07 15:49:24 -060092 u16 last_used_idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +100093
94 /* How to notify other side. FIXME: commonalize hcalls! */
95 void (*notify)(struct virtqueue *vq);
96
97#ifdef DEBUG
98 /* They're supposed to lock for us. */
99 unsigned int in_use;
100#endif
101
102 /* Tokens for callbacks. */
103 void *data[];
104};
105
106#define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
107
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100108/* Set up an indirect table of descriptors and add it to the queue. */
109static int vring_add_indirect(struct vring_virtqueue *vq,
110 struct scatterlist sg[],
111 unsigned int out,
112 unsigned int in)
113{
114 struct vring_desc *desc;
115 unsigned head;
116 int i;
117
118 desc = kmalloc((out + in) * sizeof(struct vring_desc), GFP_ATOMIC);
119 if (!desc)
120 return vq->vring.num;
121
122 /* Transfer entries from the sg list into the indirect page */
123 for (i = 0; i < out; i++) {
124 desc[i].flags = VRING_DESC_F_NEXT;
125 desc[i].addr = sg_phys(sg);
126 desc[i].len = sg->length;
127 desc[i].next = i+1;
128 sg++;
129 }
130 for (; i < (out + in); i++) {
131 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
132 desc[i].addr = sg_phys(sg);
133 desc[i].len = sg->length;
134 desc[i].next = i+1;
135 sg++;
136 }
137
138 /* Last one doesn't continue. */
139 desc[i-1].flags &= ~VRING_DESC_F_NEXT;
140 desc[i-1].next = 0;
141
142 /* We're about to use a buffer */
143 vq->num_free--;
144
145 /* Use a single buffer which doesn't continue */
146 head = vq->free_head;
147 vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
148 vq->vring.desc[head].addr = virt_to_phys(desc);
149 vq->vring.desc[head].len = i * sizeof(struct vring_desc);
150
151 /* Update free pointer */
152 vq->free_head = vq->vring.desc[head].next;
153
154 return head;
155}
156
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000157static int vring_add_buf(struct virtqueue *_vq,
158 struct scatterlist sg[],
159 unsigned int out,
160 unsigned int in,
161 void *data)
162{
163 struct vring_virtqueue *vq = to_vvq(_vq);
164 unsigned int i, avail, head, uninitialized_var(prev);
165
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100166 START_USE(vq);
167
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000168 BUG_ON(data == NULL);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100169
170 /* If the host supports indirect descriptor tables, and we have multiple
171 * buffers, then go indirect. FIXME: tune this threshold */
172 if (vq->indirect && (out + in) > 1 && vq->num_free) {
173 head = vring_add_indirect(vq, sg, out, in);
174 if (head != vq->vring.num)
175 goto add_head;
176 }
177
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000178 BUG_ON(out + in > vq->vring.num);
179 BUG_ON(out + in == 0);
180
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000181 if (vq->num_free < out + in) {
182 pr_debug("Can't add buf len %i - avail = %i\n",
183 out + in, vq->num_free);
Rusty Russell44653ea2008-07-25 12:06:04 -0500184 /* FIXME: for historical reasons, we force a notify here if
185 * there are outgoing parts to the buffer. Presumably the
186 * host should service the ring ASAP. */
187 if (out)
188 vq->notify(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000189 END_USE(vq);
190 return -ENOSPC;
191 }
192
193 /* We're about to use some buffers from the free list. */
194 vq->num_free -= out + in;
195
196 head = vq->free_head;
197 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
198 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
Rusty Russell15f9c892008-02-04 23:50:05 -0500199 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000200 vq->vring.desc[i].len = sg->length;
201 prev = i;
202 sg++;
203 }
204 for (; in; i = vq->vring.desc[i].next, in--) {
205 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
Rusty Russell15f9c892008-02-04 23:50:05 -0500206 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000207 vq->vring.desc[i].len = sg->length;
208 prev = i;
209 sg++;
210 }
211 /* Last one doesn't continue. */
212 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
213
214 /* Update free pointer */
215 vq->free_head = i;
216
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100217add_head:
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000218 /* Set token. */
219 vq->data[head] = data;
220
221 /* Put entry in available array (but don't update avail->idx until they
222 * do sync). FIXME: avoid modulus here? */
223 avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
224 vq->vring.avail->ring[avail] = head;
225
226 pr_debug("Added buffer head %i to %p\n", head, vq);
227 END_USE(vq);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600228
229 /* If we're indirect, we can fit many (assuming not OOM). */
230 if (vq->indirect)
231 return vq->num_free ? vq->vring.num : 0;
232 return vq->num_free;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000233}
234
235static void vring_kick(struct virtqueue *_vq)
236{
237 struct vring_virtqueue *vq = to_vvq(_vq);
238 START_USE(vq);
239 /* Descriptors and available array need to be set before we expose the
240 * new available array entries. */
Michael S. Tsirkind57ed952010-01-28 00:42:23 +0200241 virtio_wmb();
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000242
243 vq->vring.avail->idx += vq->num_added;
244 vq->num_added = 0;
245
246 /* Need to update avail index before checking if we should notify */
Michael S. Tsirkind57ed952010-01-28 00:42:23 +0200247 virtio_mb();
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000248
249 if (!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
250 /* Prod other side to tell it about changes. */
251 vq->notify(&vq->vq);
252
253 END_USE(vq);
254}
255
256static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
257{
258 unsigned int i;
259
260 /* Clear data ptr. */
261 vq->data[head] = NULL;
262
263 /* Put back on free list: find end */
264 i = head;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100265
266 /* Free the indirect table */
267 if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
268 kfree(phys_to_virt(vq->vring.desc[i].addr));
269
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000270 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
271 i = vq->vring.desc[i].next;
272 vq->num_free++;
273 }
274
275 vq->vring.desc[i].next = vq->free_head;
276 vq->free_head = head;
277 /* Plus final descriptor */
278 vq->num_free++;
279}
280
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000281static inline bool more_used(const struct vring_virtqueue *vq)
282{
283 return vq->last_used_idx != vq->vring.used->idx;
284}
285
286static void *vring_get_buf(struct virtqueue *_vq, unsigned int *len)
287{
288 struct vring_virtqueue *vq = to_vvq(_vq);
289 void *ret;
290 unsigned int i;
291
292 START_USE(vq);
293
Rusty Russell5ef82752008-05-02 21:50:43 -0500294 if (unlikely(vq->broken)) {
295 END_USE(vq);
296 return NULL;
297 }
298
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000299 if (!more_used(vq)) {
300 pr_debug("No more buffers in queue\n");
301 END_USE(vq);
302 return NULL;
303 }
304
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200305 /* Only get used array entries after they have been exposed by host. */
Michael S. Tsirkind57ed952010-01-28 00:42:23 +0200306 virtio_rmb();
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200307
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000308 i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
309 *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
310
311 if (unlikely(i >= vq->vring.num)) {
312 BAD_RING(vq, "id %u out of range\n", i);
313 return NULL;
314 }
315 if (unlikely(!vq->data[i])) {
316 BAD_RING(vq, "id %u is not a head!\n", i);
317 return NULL;
318 }
319
320 /* detach_buf clears data, so grab it now. */
321 ret = vq->data[i];
322 detach_buf(vq, i);
323 vq->last_used_idx++;
324 END_USE(vq);
325 return ret;
326}
327
Rusty Russell18445c42008-02-04 23:49:57 -0500328static void vring_disable_cb(struct virtqueue *_vq)
329{
330 struct vring_virtqueue *vq = to_vvq(_vq);
331
Rusty Russell18445c42008-02-04 23:49:57 -0500332 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
Rusty Russell18445c42008-02-04 23:49:57 -0500333}
334
335static bool vring_enable_cb(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000336{
337 struct vring_virtqueue *vq = to_vvq(_vq);
338
339 START_USE(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000340
341 /* We optimistically turn back on interrupts, then check if there was
342 * more to do. */
343 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
Michael S. Tsirkind57ed952010-01-28 00:42:23 +0200344 virtio_mb();
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000345 if (unlikely(more_used(vq))) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000346 END_USE(vq);
347 return false;
348 }
349
350 END_USE(vq);
351 return true;
352}
353
354irqreturn_t vring_interrupt(int irq, void *_vq)
355{
356 struct vring_virtqueue *vq = to_vvq(_vq);
357
358 if (!more_used(vq)) {
359 pr_debug("virtqueue interrupt with no work for %p\n", vq);
360 return IRQ_NONE;
361 }
362
363 if (unlikely(vq->broken))
364 return IRQ_HANDLED;
365
366 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
Rusty Russell18445c42008-02-04 23:49:57 -0500367 if (vq->vq.callback)
368 vq->vq.callback(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000369
370 return IRQ_HANDLED;
371}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500372EXPORT_SYMBOL_GPL(vring_interrupt);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000373
374static struct virtqueue_ops vring_vq_ops = {
375 .add_buf = vring_add_buf,
376 .get_buf = vring_get_buf,
377 .kick = vring_kick,
Rusty Russell18445c42008-02-04 23:49:57 -0500378 .disable_cb = vring_disable_cb,
379 .enable_cb = vring_enable_cb,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000380};
381
382struct virtqueue *vring_new_virtqueue(unsigned int num,
Rusty Russell87c7d572008-12-30 09:26:03 -0600383 unsigned int vring_align,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000384 struct virtio_device *vdev,
385 void *pages,
386 void (*notify)(struct virtqueue *),
Rusty Russell9499f5e2009-06-12 22:16:35 -0600387 void (*callback)(struct virtqueue *),
388 const char *name)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000389{
390 struct vring_virtqueue *vq;
391 unsigned int i;
392
Rusty Russell42b36cc2007-11-12 13:39:18 +1100393 /* We assume num is a power of 2. */
394 if (num & (num - 1)) {
395 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
396 return NULL;
397 }
398
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000399 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
400 if (!vq)
401 return NULL;
402
Rusty Russell87c7d572008-12-30 09:26:03 -0600403 vring_init(&vq->vring, num, pages, vring_align);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000404 vq->vq.callback = callback;
405 vq->vq.vdev = vdev;
406 vq->vq.vq_ops = &vring_vq_ops;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600407 vq->vq.name = name;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000408 vq->notify = notify;
409 vq->broken = false;
410 vq->last_used_idx = 0;
411 vq->num_added = 0;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600412 list_add_tail(&vq->vq.list, &vdev->vqs);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000413#ifdef DEBUG
414 vq->in_use = false;
415#endif
416
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100417 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
418
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000419 /* No callback? Tell other side not to bother us. */
420 if (!callback)
421 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
422
423 /* Put everything in free lists. */
424 vq->num_free = num;
425 vq->free_head = 0;
426 for (i = 0; i < num-1; i++)
427 vq->vring.desc[i].next = i+1;
428
429 return &vq->vq;
430}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500431EXPORT_SYMBOL_GPL(vring_new_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000432
433void vring_del_virtqueue(struct virtqueue *vq)
434{
Rusty Russell9499f5e2009-06-12 22:16:35 -0600435 list_del(&vq->list);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000436 kfree(to_vvq(vq));
437}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500438EXPORT_SYMBOL_GPL(vring_del_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000439
Rusty Russelle34f8722008-07-25 12:06:13 -0500440/* Manipulates transport-specific feature bits. */
441void vring_transport_features(struct virtio_device *vdev)
442{
443 unsigned int i;
444
445 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
446 switch (i) {
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100447 case VIRTIO_RING_F_INDIRECT_DESC:
448 break;
Rusty Russelle34f8722008-07-25 12:06:13 -0500449 default:
450 /* We don't understand this bit. */
451 clear_bit(i, vdev->features);
452 }
453 }
454}
455EXPORT_SYMBOL_GPL(vring_transport_features);
456
Rusty Russellc6fd4702008-02-04 23:50:05 -0500457MODULE_LICENSE("GPL");