blob: 1ee97d402a48e1d39079760a4571fc1a1d549d98 [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
24#ifdef DEBUG
25/* For development, we want to crash whenever the ring is screwed. */
Rusty Russell9499f5e2009-06-12 22:16:35 -060026#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)
Rusty Russellc5f841f2009-03-30 21:55:22 -060032/* Caller is supposed to guarantee no reentry. */
33#define START_USE(_vq) \
34 do { \
35 if ((_vq)->in_use) \
Rusty Russell9499f5e2009-06-12 22:16:35 -060036 panic("%s:in_use = %i\n", \
37 (_vq)->vq.name, (_vq)->in_use); \
Rusty Russellc5f841f2009-03-30 21:55:22 -060038 (_vq)->in_use = __LINE__; \
Rusty Russell9499f5e2009-06-12 22:16:35 -060039 } while (0)
Roel Kluin3a35ce72009-01-22 16:42:57 +010040#define END_USE(_vq) \
Rusty Russell97a545a2010-02-24 14:22:22 -060041 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100042#else
Rusty Russell9499f5e2009-06-12 22:16:35 -060043#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)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100049#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
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +010063 /* Host supports indirect buffers */
64 bool indirect;
65
Rusty Russell0a8a69d2007-10-22 11:03:40 +100066 /* 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. */
Anthony Liguori1bc49532007-11-07 15:49:24 -060074 u16 last_used_idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +100075
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
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +010090/* 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
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000139static 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
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100148 START_USE(vq);
149
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000150 BUG_ON(data == NULL);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100151
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
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000160 BUG_ON(out + in > vq->vring.num);
161 BUG_ON(out + in == 0);
162
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000163 if (vq->num_free < out + in) {
164 pr_debug("Can't add buf len %i - avail = %i\n",
165 out + in, vq->num_free);
Rusty Russell44653ea2008-07-25 12:06:04 -0500166 /* 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);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000171 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;
Rusty Russell15f9c892008-02-04 23:50:05 -0500181 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000182 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;
Rusty Russell15f9c892008-02-04 23:50:05 -0500188 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000189 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
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100199add_head:
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000200 /* 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);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600210
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;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000215}
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;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100247
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
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000252 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
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000263static 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
Rusty Russell5ef82752008-05-02 21:50:43 -0500276 if (unlikely(vq->broken)) {
277 END_USE(vq);
278 return NULL;
279 }
280
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000281 if (!more_used(vq)) {
282 pr_debug("No more buffers in queue\n");
283 END_USE(vq);
284 return NULL;
285 }
286
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200287 /* Only get used array entries after they have been exposed by host. */
288 rmb();
289
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000290 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
Rusty Russell18445c42008-02-04 23:49:57 -0500310static void vring_disable_cb(struct virtqueue *_vq)
311{
312 struct vring_virtqueue *vq = to_vvq(_vq);
313
Rusty Russell18445c42008-02-04 23:49:57 -0500314 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
Rusty Russell18445c42008-02-04 23:49:57 -0500315}
316
317static bool vring_enable_cb(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000318{
319 struct vring_virtqueue *vq = to_vvq(_vq);
320
321 START_USE(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000322
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))) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000328 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);
Rusty Russell18445c42008-02-04 23:49:57 -0500349 if (vq->vq.callback)
350 vq->vq.callback(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000351
352 return IRQ_HANDLED;
353}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500354EXPORT_SYMBOL_GPL(vring_interrupt);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000355
356static struct virtqueue_ops vring_vq_ops = {
357 .add_buf = vring_add_buf,
358 .get_buf = vring_get_buf,
359 .kick = vring_kick,
Rusty Russell18445c42008-02-04 23:49:57 -0500360 .disable_cb = vring_disable_cb,
361 .enable_cb = vring_enable_cb,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000362};
363
364struct virtqueue *vring_new_virtqueue(unsigned int num,
Rusty Russell87c7d572008-12-30 09:26:03 -0600365 unsigned int vring_align,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000366 struct virtio_device *vdev,
367 void *pages,
368 void (*notify)(struct virtqueue *),
Rusty Russell9499f5e2009-06-12 22:16:35 -0600369 void (*callback)(struct virtqueue *),
370 const char *name)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000371{
372 struct vring_virtqueue *vq;
373 unsigned int i;
374
Rusty Russell42b36cc2007-11-12 13:39:18 +1100375 /* 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
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000381 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
382 if (!vq)
383 return NULL;
384
Rusty Russell87c7d572008-12-30 09:26:03 -0600385 vring_init(&vq->vring, num, pages, vring_align);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000386 vq->vq.callback = callback;
387 vq->vq.vdev = vdev;
388 vq->vq.vq_ops = &vring_vq_ops;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600389 vq->vq.name = name;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000390 vq->notify = notify;
391 vq->broken = false;
392 vq->last_used_idx = 0;
393 vq->num_added = 0;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600394 list_add_tail(&vq->vq.list, &vdev->vqs);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000395#ifdef DEBUG
396 vq->in_use = false;
397#endif
398
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100399 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
400
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000401 /* 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}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500413EXPORT_SYMBOL_GPL(vring_new_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000414
415void vring_del_virtqueue(struct virtqueue *vq)
416{
Rusty Russell9499f5e2009-06-12 22:16:35 -0600417 list_del(&vq->list);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000418 kfree(to_vvq(vq));
419}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500420EXPORT_SYMBOL_GPL(vring_del_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000421
Rusty Russelle34f8722008-07-25 12:06:13 -0500422/* 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) {
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100429 case VIRTIO_RING_F_INDIRECT_DESC:
430 break;
Rusty Russelle34f8722008-07-25 12:06:13 -0500431 default:
432 /* We don't understand this bit. */
433 clear_bit(i, vdev->features);
434 }
435 }
436}
437EXPORT_SYMBOL_GPL(vring_transport_features);
438
Rusty Russellc6fd4702008-02-04 23:50:05 -0500439MODULE_LICENSE("GPL");