blob: 579fa693d5d0dbcb6400ed8994df0b49d4d8aea7 [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__; \
39 mb(); \
Rusty Russell9499f5e2009-06-12 22:16:35 -060040 } while (0)
Roel Kluin3a35ce72009-01-22 16:42:57 +010041#define END_USE(_vq) \
42 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; mb(); } while(0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100043#else
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 (_vq)->broken = true; \
49 } while (0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100050#define START_USE(vq)
51#define END_USE(vq)
52#endif
53
54struct vring_virtqueue
55{
56 struct virtqueue vq;
57
58 /* Actual memory layout for this queue */
59 struct vring vring;
60
61 /* Other side has made a mess, don't try any more. */
62 bool broken;
63
64 /* Number of free buffers */
65 unsigned int num_free;
66 /* Head of free buffer list. */
67 unsigned int free_head;
68 /* Number we've added since last sync. */
69 unsigned int num_added;
70
71 /* Last used index we've seen. */
Anthony Liguori1bc49532007-11-07 15:49:24 -060072 u16 last_used_idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +100073
74 /* How to notify other side. FIXME: commonalize hcalls! */
75 void (*notify)(struct virtqueue *vq);
76
77#ifdef DEBUG
78 /* They're supposed to lock for us. */
79 unsigned int in_use;
80#endif
81
82 /* Tokens for callbacks. */
83 void *data[];
84};
85
86#define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
87
88static int vring_add_buf(struct virtqueue *_vq,
89 struct scatterlist sg[],
90 unsigned int out,
91 unsigned int in,
92 void *data)
93{
94 struct vring_virtqueue *vq = to_vvq(_vq);
95 unsigned int i, avail, head, uninitialized_var(prev);
96
97 BUG_ON(data == NULL);
98 BUG_ON(out + in > vq->vring.num);
99 BUG_ON(out + in == 0);
100
101 START_USE(vq);
102
103 if (vq->num_free < out + in) {
104 pr_debug("Can't add buf len %i - avail = %i\n",
105 out + in, vq->num_free);
Rusty Russell44653ea2008-07-25 12:06:04 -0500106 /* FIXME: for historical reasons, we force a notify here if
107 * there are outgoing parts to the buffer. Presumably the
108 * host should service the ring ASAP. */
109 if (out)
110 vq->notify(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000111 END_USE(vq);
112 return -ENOSPC;
113 }
114
115 /* We're about to use some buffers from the free list. */
116 vq->num_free -= out + in;
117
118 head = vq->free_head;
119 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
120 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
Rusty Russell15f9c892008-02-04 23:50:05 -0500121 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000122 vq->vring.desc[i].len = sg->length;
123 prev = i;
124 sg++;
125 }
126 for (; in; i = vq->vring.desc[i].next, in--) {
127 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
Rusty Russell15f9c892008-02-04 23:50:05 -0500128 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000129 vq->vring.desc[i].len = sg->length;
130 prev = i;
131 sg++;
132 }
133 /* Last one doesn't continue. */
134 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
135
136 /* Update free pointer */
137 vq->free_head = i;
138
139 /* Set token. */
140 vq->data[head] = data;
141
142 /* Put entry in available array (but don't update avail->idx until they
143 * do sync). FIXME: avoid modulus here? */
144 avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
145 vq->vring.avail->ring[avail] = head;
146
147 pr_debug("Added buffer head %i to %p\n", head, vq);
148 END_USE(vq);
149 return 0;
150}
151
152static void vring_kick(struct virtqueue *_vq)
153{
154 struct vring_virtqueue *vq = to_vvq(_vq);
155 START_USE(vq);
156 /* Descriptors and available array need to be set before we expose the
157 * new available array entries. */
158 wmb();
159
160 vq->vring.avail->idx += vq->num_added;
161 vq->num_added = 0;
162
163 /* Need to update avail index before checking if we should notify */
164 mb();
165
166 if (!(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
167 /* Prod other side to tell it about changes. */
168 vq->notify(&vq->vq);
169
170 END_USE(vq);
171}
172
173static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
174{
175 unsigned int i;
176
177 /* Clear data ptr. */
178 vq->data[head] = NULL;
179
180 /* Put back on free list: find end */
181 i = head;
182 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
183 i = vq->vring.desc[i].next;
184 vq->num_free++;
185 }
186
187 vq->vring.desc[i].next = vq->free_head;
188 vq->free_head = head;
189 /* Plus final descriptor */
190 vq->num_free++;
191}
192
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000193static inline bool more_used(const struct vring_virtqueue *vq)
194{
195 return vq->last_used_idx != vq->vring.used->idx;
196}
197
198static void *vring_get_buf(struct virtqueue *_vq, unsigned int *len)
199{
200 struct vring_virtqueue *vq = to_vvq(_vq);
201 void *ret;
202 unsigned int i;
203
204 START_USE(vq);
205
Rusty Russell5ef82752008-05-02 21:50:43 -0500206 if (unlikely(vq->broken)) {
207 END_USE(vq);
208 return NULL;
209 }
210
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000211 if (!more_used(vq)) {
212 pr_debug("No more buffers in queue\n");
213 END_USE(vq);
214 return NULL;
215 }
216
217 i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
218 *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
219
220 if (unlikely(i >= vq->vring.num)) {
221 BAD_RING(vq, "id %u out of range\n", i);
222 return NULL;
223 }
224 if (unlikely(!vq->data[i])) {
225 BAD_RING(vq, "id %u is not a head!\n", i);
226 return NULL;
227 }
228
229 /* detach_buf clears data, so grab it now. */
230 ret = vq->data[i];
231 detach_buf(vq, i);
232 vq->last_used_idx++;
233 END_USE(vq);
234 return ret;
235}
236
Rusty Russell18445c42008-02-04 23:49:57 -0500237static void vring_disable_cb(struct virtqueue *_vq)
238{
239 struct vring_virtqueue *vq = to_vvq(_vq);
240
Rusty Russell18445c42008-02-04 23:49:57 -0500241 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
Rusty Russell18445c42008-02-04 23:49:57 -0500242}
243
244static bool vring_enable_cb(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000245{
246 struct vring_virtqueue *vq = to_vvq(_vq);
247
248 START_USE(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000249
250 /* We optimistically turn back on interrupts, then check if there was
251 * more to do. */
252 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
253 mb();
254 if (unlikely(more_used(vq))) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000255 END_USE(vq);
256 return false;
257 }
258
259 END_USE(vq);
260 return true;
261}
262
263irqreturn_t vring_interrupt(int irq, void *_vq)
264{
265 struct vring_virtqueue *vq = to_vvq(_vq);
266
267 if (!more_used(vq)) {
268 pr_debug("virtqueue interrupt with no work for %p\n", vq);
269 return IRQ_NONE;
270 }
271
272 if (unlikely(vq->broken))
273 return IRQ_HANDLED;
274
275 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
Rusty Russell18445c42008-02-04 23:49:57 -0500276 if (vq->vq.callback)
277 vq->vq.callback(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000278
279 return IRQ_HANDLED;
280}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500281EXPORT_SYMBOL_GPL(vring_interrupt);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000282
283static struct virtqueue_ops vring_vq_ops = {
284 .add_buf = vring_add_buf,
285 .get_buf = vring_get_buf,
286 .kick = vring_kick,
Rusty Russell18445c42008-02-04 23:49:57 -0500287 .disable_cb = vring_disable_cb,
288 .enable_cb = vring_enable_cb,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000289};
290
291struct virtqueue *vring_new_virtqueue(unsigned int num,
Rusty Russell87c7d572008-12-30 09:26:03 -0600292 unsigned int vring_align,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000293 struct virtio_device *vdev,
294 void *pages,
295 void (*notify)(struct virtqueue *),
Rusty Russell9499f5e2009-06-12 22:16:35 -0600296 void (*callback)(struct virtqueue *),
297 const char *name)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000298{
299 struct vring_virtqueue *vq;
300 unsigned int i;
301
Rusty Russell42b36cc2007-11-12 13:39:18 +1100302 /* We assume num is a power of 2. */
303 if (num & (num - 1)) {
304 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
305 return NULL;
306 }
307
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000308 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
309 if (!vq)
310 return NULL;
311
Rusty Russell87c7d572008-12-30 09:26:03 -0600312 vring_init(&vq->vring, num, pages, vring_align);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000313 vq->vq.callback = callback;
314 vq->vq.vdev = vdev;
315 vq->vq.vq_ops = &vring_vq_ops;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600316 vq->vq.name = name;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000317 vq->notify = notify;
318 vq->broken = false;
319 vq->last_used_idx = 0;
320 vq->num_added = 0;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600321 list_add_tail(&vq->vq.list, &vdev->vqs);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000322#ifdef DEBUG
323 vq->in_use = false;
324#endif
325
326 /* No callback? Tell other side not to bother us. */
327 if (!callback)
328 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
329
330 /* Put everything in free lists. */
331 vq->num_free = num;
332 vq->free_head = 0;
333 for (i = 0; i < num-1; i++)
334 vq->vring.desc[i].next = i+1;
335
336 return &vq->vq;
337}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500338EXPORT_SYMBOL_GPL(vring_new_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000339
340void vring_del_virtqueue(struct virtqueue *vq)
341{
Rusty Russell9499f5e2009-06-12 22:16:35 -0600342 list_del(&vq->list);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000343 kfree(to_vvq(vq));
344}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500345EXPORT_SYMBOL_GPL(vring_del_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000346
Rusty Russelle34f8722008-07-25 12:06:13 -0500347/* Manipulates transport-specific feature bits. */
348void vring_transport_features(struct virtio_device *vdev)
349{
350 unsigned int i;
351
352 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
353 switch (i) {
354 default:
355 /* We don't understand this bit. */
356 clear_bit(i, vdev->features);
357 }
358 }
359}
360EXPORT_SYMBOL_GPL(vring_transport_features);
361
Rusty Russellc6fd4702008-02-04 23:50:05 -0500362MODULE_LICENSE("GPL");