blob: c7a2c208f6eaded137c1f0d9ea73ea0a14100e65 [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
31 * through relaxed memory I/O windows (which virtio does not use). */
32#define virtio_mb() smp_mb()
33#define virtio_rmb() smp_rmb()
34#define virtio_wmb() smp_wmb()
35#else
36/* We must force memory ordering even if guest is UP since host could be
37 * running on another CPU, but SMP barriers are defined to barrier() in that
38 * configuration. So fall back to mandatory barriers instead. */
39#define virtio_mb() mb()
40#define virtio_rmb() rmb()
41#define virtio_wmb() wmb()
42#endif
43
Rusty Russell0a8a69d2007-10-22 11:03:40 +100044#ifdef DEBUG
45/* For development, we want to crash whenever the ring is screwed. */
Rusty Russell9499f5e2009-06-12 22:16:35 -060046#define BAD_RING(_vq, fmt, args...) \
47 do { \
48 dev_err(&(_vq)->vq.vdev->dev, \
49 "%s:"fmt, (_vq)->vq.name, ##args); \
50 BUG(); \
51 } while (0)
Rusty Russellc5f841f2009-03-30 21:55:22 -060052/* Caller is supposed to guarantee no reentry. */
53#define START_USE(_vq) \
54 do { \
55 if ((_vq)->in_use) \
Rusty Russell9499f5e2009-06-12 22:16:35 -060056 panic("%s:in_use = %i\n", \
57 (_vq)->vq.name, (_vq)->in_use); \
Rusty Russellc5f841f2009-03-30 21:55:22 -060058 (_vq)->in_use = __LINE__; \
Rusty Russell9499f5e2009-06-12 22:16:35 -060059 } while (0)
Roel Kluin3a35ce72009-01-22 16:42:57 +010060#define END_USE(_vq) \
Rusty Russell97a545a2010-02-24 14:22:22 -060061 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100062#else
Rusty Russell9499f5e2009-06-12 22:16:35 -060063#define BAD_RING(_vq, fmt, args...) \
64 do { \
65 dev_err(&_vq->vq.vdev->dev, \
66 "%s:"fmt, (_vq)->vq.name, ##args); \
67 (_vq)->broken = true; \
68 } while (0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100069#define START_USE(vq)
70#define END_USE(vq)
71#endif
72
73struct vring_virtqueue
74{
75 struct virtqueue vq;
76
77 /* Actual memory layout for this queue */
78 struct vring vring;
79
80 /* Other side has made a mess, don't try any more. */
81 bool broken;
82
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +010083 /* Host supports indirect buffers */
84 bool indirect;
85
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +030086 /* Host publishes avail event idx */
87 bool event;
88
Rusty Russell0a8a69d2007-10-22 11:03:40 +100089 /* Number of free buffers */
90 unsigned int num_free;
91 /* Head of free buffer list. */
92 unsigned int free_head;
93 /* Number we've added since last sync. */
94 unsigned int num_added;
95
96 /* Last used index we've seen. */
Anthony Liguori1bc49532007-11-07 15:49:24 -060097 u16 last_used_idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +100098
99 /* How to notify other side. FIXME: commonalize hcalls! */
100 void (*notify)(struct virtqueue *vq);
101
102#ifdef DEBUG
103 /* They're supposed to lock for us. */
104 unsigned int in_use;
105#endif
106
107 /* Tokens for callbacks. */
108 void *data[];
109};
110
111#define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
112
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100113/* Set up an indirect table of descriptors and add it to the queue. */
114static int vring_add_indirect(struct vring_virtqueue *vq,
115 struct scatterlist sg[],
116 unsigned int out,
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300117 unsigned int in,
118 gfp_t gfp)
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100119{
120 struct vring_desc *desc;
121 unsigned head;
122 int i;
123
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300124 desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100125 if (!desc)
Michael S. Tsirkin686d3632010-06-10 18:16:11 +0300126 return -ENOMEM;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100127
128 /* Transfer entries from the sg list into the indirect page */
129 for (i = 0; i < out; i++) {
130 desc[i].flags = VRING_DESC_F_NEXT;
131 desc[i].addr = sg_phys(sg);
132 desc[i].len = sg->length;
133 desc[i].next = i+1;
134 sg++;
135 }
136 for (; i < (out + in); i++) {
137 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
138 desc[i].addr = sg_phys(sg);
139 desc[i].len = sg->length;
140 desc[i].next = i+1;
141 sg++;
142 }
143
144 /* Last one doesn't continue. */
145 desc[i-1].flags &= ~VRING_DESC_F_NEXT;
146 desc[i-1].next = 0;
147
148 /* We're about to use a buffer */
149 vq->num_free--;
150
151 /* Use a single buffer which doesn't continue */
152 head = vq->free_head;
153 vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
154 vq->vring.desc[head].addr = virt_to_phys(desc);
155 vq->vring.desc[head].len = i * sizeof(struct vring_desc);
156
157 /* Update free pointer */
158 vq->free_head = vq->vring.desc[head].next;
159
160 return head;
161}
162
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300163int virtqueue_add_buf_gfp(struct virtqueue *_vq,
164 struct scatterlist sg[],
165 unsigned int out,
166 unsigned int in,
167 void *data,
168 gfp_t gfp)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000169{
170 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930171 unsigned int i, avail, uninitialized_var(prev);
172 int head;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000173
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100174 START_USE(vq);
175
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000176 BUG_ON(data == NULL);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100177
178 /* If the host supports indirect descriptor tables, and we have multiple
179 * buffers, then go indirect. FIXME: tune this threshold */
180 if (vq->indirect && (out + in) > 1 && vq->num_free) {
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300181 head = vring_add_indirect(vq, sg, out, in, gfp);
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930182 if (likely(head >= 0))
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100183 goto add_head;
184 }
185
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000186 BUG_ON(out + in > vq->vring.num);
187 BUG_ON(out + in == 0);
188
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000189 if (vq->num_free < out + in) {
190 pr_debug("Can't add buf len %i - avail = %i\n",
191 out + in, vq->num_free);
Rusty Russell44653ea2008-07-25 12:06:04 -0500192 /* FIXME: for historical reasons, we force a notify here if
193 * there are outgoing parts to the buffer. Presumably the
194 * host should service the ring ASAP. */
195 if (out)
196 vq->notify(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000197 END_USE(vq);
198 return -ENOSPC;
199 }
200
201 /* We're about to use some buffers from the free list. */
202 vq->num_free -= out + in;
203
204 head = vq->free_head;
205 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
206 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
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 for (; in; i = vq->vring.desc[i].next, in--) {
213 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
Rusty Russell15f9c892008-02-04 23:50:05 -0500214 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000215 vq->vring.desc[i].len = sg->length;
216 prev = i;
217 sg++;
218 }
219 /* Last one doesn't continue. */
220 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
221
222 /* Update free pointer */
223 vq->free_head = i;
224
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100225add_head:
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000226 /* Set token. */
227 vq->data[head] = data;
228
229 /* Put entry in available array (but don't update avail->idx until they
230 * do sync). FIXME: avoid modulus here? */
231 avail = (vq->vring.avail->idx + vq->num_added++) % vq->vring.num;
232 vq->vring.avail->ring[avail] = head;
233
234 pr_debug("Added buffer head %i to %p\n", head, vq);
235 END_USE(vq);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600236
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600237 return vq->num_free;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000238}
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300239EXPORT_SYMBOL_GPL(virtqueue_add_buf_gfp);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000240
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300241void virtqueue_kick(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000242{
243 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300244 u16 new, old;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000245 START_USE(vq);
246 /* Descriptors and available array need to be set before we expose the
247 * new available array entries. */
Michael S. Tsirkind57ed952010-01-28 00:42:23 +0200248 virtio_wmb();
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000249
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300250 old = vq->vring.avail->idx;
251 new = vq->vring.avail->idx = old + vq->num_added;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000252 vq->num_added = 0;
253
254 /* Need to update avail index before checking if we should notify */
Michael S. Tsirkind57ed952010-01-28 00:42:23 +0200255 virtio_mb();
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000256
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300257 if (vq->event ?
258 vring_need_event(vring_avail_event(&vq->vring), new, old) :
259 !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY))
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000260 /* Prod other side to tell it about changes. */
261 vq->notify(&vq->vq);
262
263 END_USE(vq);
264}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300265EXPORT_SYMBOL_GPL(virtqueue_kick);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000266
267static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
268{
269 unsigned int i;
270
271 /* Clear data ptr. */
272 vq->data[head] = NULL;
273
274 /* Put back on free list: find end */
275 i = head;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100276
277 /* Free the indirect table */
278 if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
279 kfree(phys_to_virt(vq->vring.desc[i].addr));
280
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000281 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
282 i = vq->vring.desc[i].next;
283 vq->num_free++;
284 }
285
286 vq->vring.desc[i].next = vq->free_head;
287 vq->free_head = head;
288 /* Plus final descriptor */
289 vq->num_free++;
290}
291
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000292static inline bool more_used(const struct vring_virtqueue *vq)
293{
294 return vq->last_used_idx != vq->vring.used->idx;
295}
296
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300297void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000298{
299 struct vring_virtqueue *vq = to_vvq(_vq);
300 void *ret;
301 unsigned int i;
302
303 START_USE(vq);
304
Rusty Russell5ef82752008-05-02 21:50:43 -0500305 if (unlikely(vq->broken)) {
306 END_USE(vq);
307 return NULL;
308 }
309
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000310 if (!more_used(vq)) {
311 pr_debug("No more buffers in queue\n");
312 END_USE(vq);
313 return NULL;
314 }
315
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200316 /* Only get used array entries after they have been exposed by host. */
Michael S. Tsirkind57ed952010-01-28 00:42:23 +0200317 virtio_rmb();
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200318
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000319 i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id;
320 *len = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].len;
321
322 if (unlikely(i >= vq->vring.num)) {
323 BAD_RING(vq, "id %u out of range\n", i);
324 return NULL;
325 }
326 if (unlikely(!vq->data[i])) {
327 BAD_RING(vq, "id %u is not a head!\n", i);
328 return NULL;
329 }
330
331 /* detach_buf clears data, so grab it now. */
332 ret = vq->data[i];
333 detach_buf(vq, i);
334 vq->last_used_idx++;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300335 /* If we expect an interrupt for the next entry, tell host
336 * by writing event index and flush out the write before
337 * the read in the next get_buf call. */
338 if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
339 vring_used_event(&vq->vring) = vq->last_used_idx;
340 virtio_mb();
341 }
342
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000343 END_USE(vq);
344 return ret;
345}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300346EXPORT_SYMBOL_GPL(virtqueue_get_buf);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000347
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300348void virtqueue_disable_cb(struct virtqueue *_vq)
Rusty Russell18445c42008-02-04 23:49:57 -0500349{
350 struct vring_virtqueue *vq = to_vvq(_vq);
351
Rusty Russell18445c42008-02-04 23:49:57 -0500352 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
Rusty Russell18445c42008-02-04 23:49:57 -0500353}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300354EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
Rusty Russell18445c42008-02-04 23:49:57 -0500355
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300356bool virtqueue_enable_cb(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000357{
358 struct vring_virtqueue *vq = to_vvq(_vq);
359
360 START_USE(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000361
362 /* We optimistically turn back on interrupts, then check if there was
363 * more to do. */
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300364 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
365 * either clear the flags bit or point the event index at the next
366 * entry. Always do both to keep code simple. */
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000367 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300368 vring_used_event(&vq->vring) = vq->last_used_idx;
Michael S. Tsirkind57ed952010-01-28 00:42:23 +0200369 virtio_mb();
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000370 if (unlikely(more_used(vq))) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000371 END_USE(vq);
372 return false;
373 }
374
375 END_USE(vq);
376 return true;
377}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300378EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000379
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300380bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
381{
382 struct vring_virtqueue *vq = to_vvq(_vq);
383 u16 bufs;
384
385 START_USE(vq);
386
387 /* We optimistically turn back on interrupts, then check if there was
388 * more to do. */
389 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
390 * either clear the flags bit or point the event index at the next
391 * entry. Always do both to keep code simple. */
392 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
393 /* TODO: tune this threshold */
394 bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
395 vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
396 virtio_mb();
397 if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
398 END_USE(vq);
399 return false;
400 }
401
402 END_USE(vq);
403 return true;
404}
405EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
406
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300407void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
Shirley Mac021eac2010-01-18 19:15:23 +0530408{
409 struct vring_virtqueue *vq = to_vvq(_vq);
410 unsigned int i;
411 void *buf;
412
413 START_USE(vq);
414
415 for (i = 0; i < vq->vring.num; i++) {
416 if (!vq->data[i])
417 continue;
418 /* detach_buf clears data, so grab it now. */
419 buf = vq->data[i];
420 detach_buf(vq, i);
Amit Shahb3258ff2011-03-16 19:12:10 +0530421 vq->vring.avail->idx--;
Shirley Mac021eac2010-01-18 19:15:23 +0530422 END_USE(vq);
423 return buf;
424 }
425 /* That should have freed everything. */
426 BUG_ON(vq->num_free != vq->vring.num);
427
428 END_USE(vq);
429 return NULL;
430}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300431EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
Shirley Mac021eac2010-01-18 19:15:23 +0530432
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000433irqreturn_t vring_interrupt(int irq, void *_vq)
434{
435 struct vring_virtqueue *vq = to_vvq(_vq);
436
437 if (!more_used(vq)) {
438 pr_debug("virtqueue interrupt with no work for %p\n", vq);
439 return IRQ_NONE;
440 }
441
442 if (unlikely(vq->broken))
443 return IRQ_HANDLED;
444
445 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
Rusty Russell18445c42008-02-04 23:49:57 -0500446 if (vq->vq.callback)
447 vq->vq.callback(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000448
449 return IRQ_HANDLED;
450}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500451EXPORT_SYMBOL_GPL(vring_interrupt);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000452
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000453struct virtqueue *vring_new_virtqueue(unsigned int num,
Rusty Russell87c7d572008-12-30 09:26:03 -0600454 unsigned int vring_align,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000455 struct virtio_device *vdev,
456 void *pages,
457 void (*notify)(struct virtqueue *),
Rusty Russell9499f5e2009-06-12 22:16:35 -0600458 void (*callback)(struct virtqueue *),
459 const char *name)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000460{
461 struct vring_virtqueue *vq;
462 unsigned int i;
463
Rusty Russell42b36cc2007-11-12 13:39:18 +1100464 /* We assume num is a power of 2. */
465 if (num & (num - 1)) {
466 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
467 return NULL;
468 }
469
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000470 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
471 if (!vq)
472 return NULL;
473
Rusty Russell87c7d572008-12-30 09:26:03 -0600474 vring_init(&vq->vring, num, pages, vring_align);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000475 vq->vq.callback = callback;
476 vq->vq.vdev = vdev;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600477 vq->vq.name = name;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000478 vq->notify = notify;
479 vq->broken = false;
480 vq->last_used_idx = 0;
481 vq->num_added = 0;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600482 list_add_tail(&vq->vq.list, &vdev->vqs);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000483#ifdef DEBUG
484 vq->in_use = false;
485#endif
486
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100487 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300488 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100489
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000490 /* No callback? Tell other side not to bother us. */
491 if (!callback)
492 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
493
494 /* Put everything in free lists. */
495 vq->num_free = num;
496 vq->free_head = 0;
Amit Shah3b870622010-02-12 10:32:14 +0530497 for (i = 0; i < num-1; i++) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000498 vq->vring.desc[i].next = i+1;
Amit Shah3b870622010-02-12 10:32:14 +0530499 vq->data[i] = NULL;
500 }
501 vq->data[i] = NULL;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000502
503 return &vq->vq;
504}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500505EXPORT_SYMBOL_GPL(vring_new_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000506
507void vring_del_virtqueue(struct virtqueue *vq)
508{
Rusty Russell9499f5e2009-06-12 22:16:35 -0600509 list_del(&vq->list);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000510 kfree(to_vvq(vq));
511}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500512EXPORT_SYMBOL_GPL(vring_del_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000513
Rusty Russelle34f8722008-07-25 12:06:13 -0500514/* Manipulates transport-specific feature bits. */
515void vring_transport_features(struct virtio_device *vdev)
516{
517 unsigned int i;
518
519 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
520 switch (i) {
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100521 case VIRTIO_RING_F_INDIRECT_DESC:
522 break;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300523 case VIRTIO_RING_F_EVENT_IDX:
524 break;
Rusty Russelle34f8722008-07-25 12:06:13 -0500525 default:
526 /* We don't understand this bit. */
527 clear_bit(i, vdev->features);
528 }
529 }
530}
531EXPORT_SYMBOL_GPL(vring_transport_features);
532
Rick Jones8f9f4662011-10-19 08:10:59 +0000533/* return the size of the vring within the virtqueue */
534unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
535{
536
537 struct vring_virtqueue *vq = to_vvq(_vq);
538
539 return vq->vring.num;
540}
541EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
542
Rusty Russellc6fd4702008-02-04 23:50:05 -0500543MODULE_LICENSE("GPL");