blob: 50da92046092f20504b8e668ae7acf2e0759373a [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>
Paul Gortmakerb5a2c4f2011-07-03 16:20:30 -040024#include <linux/module.h>
Rusty Russell0a8a69d2007-10-22 11:03:40 +100025
Michael S. Tsirkind57ed952010-01-28 00:42:23 +020026/* 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
Rusty Russell7b21e342012-01-12 15:44:42 +103031 * 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)
Michael S. Tsirkind57ed952010-01-28 00:42:23 +020038#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. */
Rusty Russell7b21e342012-01-12 15:44:42 +103042#define virtio_mb(vq) mb()
43#define virtio_rmb(vq) rmb()
44#define virtio_wmb(vq) wmb()
Michael S. Tsirkind57ed952010-01-28 00:42:23 +020045#endif
46
Rusty Russell0a8a69d2007-10-22 11:03:40 +100047#ifdef DEBUG
48/* For development, we want to crash whenever the ring is screwed. */
Rusty Russell9499f5e2009-06-12 22:16:35 -060049#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)
Rusty Russellc5f841f2009-03-30 21:55:22 -060055/* Caller is supposed to guarantee no reentry. */
56#define START_USE(_vq) \
57 do { \
58 if ((_vq)->in_use) \
Rusty Russell9499f5e2009-06-12 22:16:35 -060059 panic("%s:in_use = %i\n", \
60 (_vq)->vq.name, (_vq)->in_use); \
Rusty Russellc5f841f2009-03-30 21:55:22 -060061 (_vq)->in_use = __LINE__; \
Rusty Russell9499f5e2009-06-12 22:16:35 -060062 } while (0)
Roel Kluin3a35ce72009-01-22 16:42:57 +010063#define END_USE(_vq) \
Rusty Russell97a545a2010-02-24 14:22:22 -060064 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100065#else
Rusty Russell9499f5e2009-06-12 22:16:35 -060066#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)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100072#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
Rusty Russell7b21e342012-01-12 15:44:42 +103083 /* Can we use weak barriers? */
84 bool weak_barriers;
85
Rusty Russell0a8a69d2007-10-22 11:03:40 +100086 /* Other side has made a mess, don't try any more. */
87 bool broken;
88
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +010089 /* Host supports indirect buffers */
90 bool indirect;
91
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +030092 /* Host publishes avail event idx */
93 bool event;
94
Rusty Russell0a8a69d2007-10-22 11:03:40 +100095 /* 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. */
Anthony Liguori1bc49532007-11-07 15:49:24 -0600103 u16 last_used_idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000104
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
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100119/* 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,
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300123 unsigned int in,
124 gfp_t gfp)
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100125{
126 struct vring_desc *desc;
127 unsigned head;
128 int i;
129
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300130 desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100131 if (!desc)
Michael S. Tsirkin686d3632010-06-10 18:16:11 +0300132 return -ENOMEM;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100133
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
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300169int 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)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000175{
176 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930177 unsigned int i, avail, uninitialized_var(prev);
178 int head;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000179
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100180 START_USE(vq);
181
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000182 BUG_ON(data == NULL);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100183
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) {
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300187 head = vring_add_indirect(vq, sg, out, in, gfp);
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930188 if (likely(head >= 0))
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100189 goto add_head;
190 }
191
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000192 BUG_ON(out + in > vq->vring.num);
193 BUG_ON(out + in == 0);
194
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000195 if (vq->num_free < out + in) {
196 pr_debug("Can't add buf len %i - avail = %i\n",
197 out + in, vq->num_free);
Rusty Russell44653ea2008-07-25 12:06:04 -0500198 /* 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);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000203 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;
Rusty Russell15f9c892008-02-04 23:50:05 -0500213 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000214 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;
Rusty Russell15f9c892008-02-04 23:50:05 -0500220 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000221 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
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100231add_head:
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000232 /* 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);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600242
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600243 return vq->num_free;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000244}
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300245EXPORT_SYMBOL_GPL(virtqueue_add_buf_gfp);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000246
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300247void virtqueue_kick(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000248{
249 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300250 u16 new, old;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000251 START_USE(vq);
252 /* Descriptors and available array need to be set before we expose the
253 * new available array entries. */
Rusty Russell7b21e342012-01-12 15:44:42 +1030254 virtio_wmb(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000255
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300256 old = vq->vring.avail->idx;
257 new = vq->vring.avail->idx = old + vq->num_added;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000258 vq->num_added = 0;
259
260 /* Need to update avail index before checking if we should notify */
Rusty Russell7b21e342012-01-12 15:44:42 +1030261 virtio_mb(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000262
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300263 if (vq->event ?
264 vring_need_event(vring_avail_event(&vq->vring), new, old) :
265 !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000266 /* Prod other side to tell it about changes. */
267 vq->notify(&vq->vq);
268
269 END_USE(vq);
270}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300271EXPORT_SYMBOL_GPL(virtqueue_kick);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000272
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;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100282
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
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000287 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
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000298static inline bool more_used(const struct vring_virtqueue *vq)
299{
300 return vq->last_used_idx != vq->vring.used->idx;
301}
302
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300303void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000304{
305 struct vring_virtqueue *vq = to_vvq(_vq);
306 void *ret;
307 unsigned int i;
308
309 START_USE(vq);
310
Rusty Russell5ef82752008-05-02 21:50:43 -0500311 if (unlikely(vq->broken)) {
312 END_USE(vq);
313 return NULL;
314 }
315
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000316 if (!more_used(vq)) {
317 pr_debug("No more buffers in queue\n");
318 END_USE(vq);
319 return NULL;
320 }
321
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200322 /* Only get used array entries after they have been exposed by host. */
Rusty Russell7b21e342012-01-12 15:44:42 +1030323 virtio_rmb(vq);
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200324
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000325 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++;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300341 /* 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;
Rusty Russell7b21e342012-01-12 15:44:42 +1030346 virtio_mb(vq);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300347 }
348
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000349 END_USE(vq);
350 return ret;
351}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300352EXPORT_SYMBOL_GPL(virtqueue_get_buf);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000353
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300354void virtqueue_disable_cb(struct virtqueue *_vq)
Rusty Russell18445c42008-02-04 23:49:57 -0500355{
356 struct vring_virtqueue *vq = to_vvq(_vq);
357
Rusty Russell18445c42008-02-04 23:49:57 -0500358 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
Rusty Russell18445c42008-02-04 23:49:57 -0500359}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300360EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
Rusty Russell18445c42008-02-04 23:49:57 -0500361
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300362bool virtqueue_enable_cb(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000363{
364 struct vring_virtqueue *vq = to_vvq(_vq);
365
366 START_USE(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000367
368 /* We optimistically turn back on interrupts, then check if there was
369 * more to do. */
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300370 /* 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. */
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000373 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300374 vring_used_event(&vq->vring) = vq->last_used_idx;
Rusty Russell7b21e342012-01-12 15:44:42 +1030375 virtio_mb(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000376 if (unlikely(more_used(vq))) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000377 END_USE(vq);
378 return false;
379 }
380
381 END_USE(vq);
382 return true;
383}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300384EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000385
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300386bool 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;
Rusty Russell7b21e342012-01-12 15:44:42 +1030402 virtio_mb(vq);
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300403 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
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300413void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
Shirley Mac021eac2010-01-18 19:15:23 +0530414{
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);
Amit Shahb3258ff2011-03-16 19:12:10 +0530427 vq->vring.avail->idx--;
Shirley Mac021eac2010-01-18 19:15:23 +0530428 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}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300437EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
Shirley Mac021eac2010-01-18 19:15:23 +0530438
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000439irqreturn_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);
Rusty Russell18445c42008-02-04 23:49:57 -0500452 if (vq->vq.callback)
453 vq->vq.callback(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000454
455 return IRQ_HANDLED;
456}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500457EXPORT_SYMBOL_GPL(vring_interrupt);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000458
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000459struct virtqueue *vring_new_virtqueue(unsigned int num,
Rusty Russell87c7d572008-12-30 09:26:03 -0600460 unsigned int vring_align,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000461 struct virtio_device *vdev,
Rusty Russell7b21e342012-01-12 15:44:42 +1030462 bool weak_barriers,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000463 void *pages,
464 void (*notify)(struct virtqueue *),
Rusty Russell9499f5e2009-06-12 22:16:35 -0600465 void (*callback)(struct virtqueue *),
466 const char *name)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000467{
468 struct vring_virtqueue *vq;
469 unsigned int i;
470
Rusty Russell42b36cc2007-11-12 13:39:18 +1100471 /* 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
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000477 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
478 if (!vq)
479 return NULL;
480
Rusty Russell87c7d572008-12-30 09:26:03 -0600481 vring_init(&vq->vring, num, pages, vring_align);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000482 vq->vq.callback = callback;
483 vq->vq.vdev = vdev;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600484 vq->vq.name = name;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000485 vq->notify = notify;
Rusty Russell7b21e342012-01-12 15:44:42 +1030486 vq->weak_barriers = weak_barriers;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000487 vq->broken = false;
488 vq->last_used_idx = 0;
489 vq->num_added = 0;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600490 list_add_tail(&vq->vq.list, &vdev->vqs);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000491#ifdef DEBUG
492 vq->in_use = false;
493#endif
494
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100495 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300496 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100497
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000498 /* 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;
Amit Shah3b870622010-02-12 10:32:14 +0530505 for (i = 0; i < num-1; i++) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000506 vq->vring.desc[i].next = i+1;
Amit Shah3b870622010-02-12 10:32:14 +0530507 vq->data[i] = NULL;
508 }
509 vq->data[i] = NULL;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000510
511 return &vq->vq;
512}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500513EXPORT_SYMBOL_GPL(vring_new_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000514
515void vring_del_virtqueue(struct virtqueue *vq)
516{
Rusty Russell9499f5e2009-06-12 22:16:35 -0600517 list_del(&vq->list);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000518 kfree(to_vvq(vq));
519}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500520EXPORT_SYMBOL_GPL(vring_del_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000521
Rusty Russelle34f8722008-07-25 12:06:13 -0500522/* 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) {
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100529 case VIRTIO_RING_F_INDIRECT_DESC:
530 break;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300531 case VIRTIO_RING_F_EVENT_IDX:
532 break;
Rusty Russelle34f8722008-07-25 12:06:13 -0500533 default:
534 /* We don't understand this bit. */
535 clear_bit(i, vdev->features);
536 }
537 }
538}
539EXPORT_SYMBOL_GPL(vring_transport_features);
540
Rick Jones8f9f4662011-10-19 08:10:59 +0000541/* 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
Rusty Russellc6fd4702008-02-04 23:50:05 -0500551MODULE_LICENSE("GPL");