blob: 33a4ce009bcc5f596b35794d9f15c91439d449f7 [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 Russelle93300b2012-01-12 15:44:43 +103025#include <linux/hrtimer.h>
Rusty Russell0a8a69d2007-10-22 11:03:40 +100026
Michael S. Tsirkind57ed952010-01-28 00:42:23 +020027/* virtio guest is communicating with a virtual "device" that actually runs on
28 * a host processor. Memory barriers are used to control SMP effects. */
29#ifdef CONFIG_SMP
30/* Where possible, use SMP barriers which are more lightweight than mandatory
31 * barriers, because mandatory barriers control MMIO effects on accesses
Rusty Russell7b21e342012-01-12 15:44:42 +103032 * through relaxed memory I/O windows (which virtio-pci does not use). */
33#define virtio_mb(vq) \
34 do { if ((vq)->weak_barriers) smp_mb(); else mb(); } while(0)
35#define virtio_rmb(vq) \
36 do { if ((vq)->weak_barriers) smp_rmb(); else rmb(); } while(0)
37#define virtio_wmb(vq) \
Jason Wang4dbc5d92012-01-20 16:16:59 +080038 do { if ((vq)->weak_barriers) smp_wmb(); else wmb(); } while(0)
Michael S. Tsirkind57ed952010-01-28 00:42:23 +020039#else
40/* We must force memory ordering even if guest is UP since host could be
41 * running on another CPU, but SMP barriers are defined to barrier() in that
42 * configuration. So fall back to mandatory barriers instead. */
Rusty Russell7b21e342012-01-12 15:44:42 +103043#define virtio_mb(vq) mb()
44#define virtio_rmb(vq) rmb()
45#define virtio_wmb(vq) wmb()
Michael S. Tsirkind57ed952010-01-28 00:42:23 +020046#endif
47
Rusty Russell0a8a69d2007-10-22 11:03:40 +100048#ifdef DEBUG
49/* For development, we want to crash whenever the ring is screwed. */
Rusty Russell9499f5e2009-06-12 22:16:35 -060050#define BAD_RING(_vq, fmt, args...) \
51 do { \
52 dev_err(&(_vq)->vq.vdev->dev, \
53 "%s:"fmt, (_vq)->vq.name, ##args); \
54 BUG(); \
55 } while (0)
Rusty Russellc5f841f2009-03-30 21:55:22 -060056/* Caller is supposed to guarantee no reentry. */
57#define START_USE(_vq) \
58 do { \
59 if ((_vq)->in_use) \
Rusty Russell9499f5e2009-06-12 22:16:35 -060060 panic("%s:in_use = %i\n", \
61 (_vq)->vq.name, (_vq)->in_use); \
Rusty Russellc5f841f2009-03-30 21:55:22 -060062 (_vq)->in_use = __LINE__; \
Rusty Russell9499f5e2009-06-12 22:16:35 -060063 } while (0)
Roel Kluin3a35ce72009-01-22 16:42:57 +010064#define END_USE(_vq) \
Rusty Russell97a545a2010-02-24 14:22:22 -060065 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100066#else
Rusty Russell9499f5e2009-06-12 22:16:35 -060067#define BAD_RING(_vq, fmt, args...) \
68 do { \
69 dev_err(&_vq->vq.vdev->dev, \
70 "%s:"fmt, (_vq)->vq.name, ##args); \
71 (_vq)->broken = true; \
72 } while (0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100073#define START_USE(vq)
74#define END_USE(vq)
75#endif
76
77struct vring_virtqueue
78{
79 struct virtqueue vq;
80
81 /* Actual memory layout for this queue */
82 struct vring vring;
83
Rusty Russell7b21e342012-01-12 15:44:42 +103084 /* Can we use weak barriers? */
85 bool weak_barriers;
86
Rusty Russell0a8a69d2007-10-22 11:03:40 +100087 /* Other side has made a mess, don't try any more. */
88 bool broken;
89
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +010090 /* Host supports indirect buffers */
91 bool indirect;
92
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +030093 /* Host publishes avail event idx */
94 bool event;
95
Rusty Russell0a8a69d2007-10-22 11:03:40 +100096 /* Head of free buffer list. */
97 unsigned int free_head;
98 /* Number we've added since last sync. */
99 unsigned int num_added;
100
101 /* Last used index we've seen. */
Anthony Liguori1bc49532007-11-07 15:49:24 -0600102 u16 last_used_idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000103
104 /* How to notify other side. FIXME: commonalize hcalls! */
105 void (*notify)(struct virtqueue *vq);
106
107#ifdef DEBUG
108 /* They're supposed to lock for us. */
109 unsigned int in_use;
Rusty Russelle93300b2012-01-12 15:44:43 +1030110
111 /* Figure out if their kicks are too delayed. */
112 bool last_add_time_valid;
113 ktime_t last_add_time;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000114#endif
115
116 /* Tokens for callbacks. */
117 void *data[];
118};
119
120#define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
121
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100122/* Set up an indirect table of descriptors and add it to the queue. */
123static int vring_add_indirect(struct vring_virtqueue *vq,
124 struct scatterlist sg[],
125 unsigned int out,
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300126 unsigned int in,
127 gfp_t gfp)
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100128{
129 struct vring_desc *desc;
130 unsigned head;
131 int i;
132
Will Deaconb92b1b82012-10-19 14:03:33 +0100133 /*
134 * We require lowmem mappings for the descriptors because
135 * otherwise virt_to_phys will give us bogus addresses in the
136 * virtqueue.
137 */
138 gfp &= ~(__GFP_HIGHMEM | __GFP_HIGH);
139
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300140 desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100141 if (!desc)
Michael S. Tsirkin686d3632010-06-10 18:16:11 +0300142 return -ENOMEM;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100143
144 /* Transfer entries from the sg list into the indirect page */
145 for (i = 0; i < out; i++) {
146 desc[i].flags = VRING_DESC_F_NEXT;
147 desc[i].addr = sg_phys(sg);
148 desc[i].len = sg->length;
149 desc[i].next = i+1;
150 sg++;
151 }
152 for (; i < (out + in); i++) {
153 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
154 desc[i].addr = sg_phys(sg);
155 desc[i].len = sg->length;
156 desc[i].next = i+1;
157 sg++;
158 }
159
160 /* Last one doesn't continue. */
161 desc[i-1].flags &= ~VRING_DESC_F_NEXT;
162 desc[i-1].next = 0;
163
164 /* We're about to use a buffer */
Rusty Russell06ca2872012-10-16 23:56:14 +1030165 vq->vq.num_free--;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100166
167 /* Use a single buffer which doesn't continue */
168 head = vq->free_head;
169 vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
170 vq->vring.desc[head].addr = virt_to_phys(desc);
171 vq->vring.desc[head].len = i * sizeof(struct vring_desc);
172
173 /* Update free pointer */
174 vq->free_head = vq->vring.desc[head].next;
175
176 return head;
177}
178
Rusty Russell5dfc1762012-01-12 15:44:42 +1030179/**
Rusty Russellf96fde42012-01-12 15:44:42 +1030180 * virtqueue_add_buf - expose buffer to other end
Rusty Russell5dfc1762012-01-12 15:44:42 +1030181 * @vq: the struct virtqueue we're talking about.
182 * @sg: the description of the buffer(s).
183 * @out_num: the number of sg readable by other side
184 * @in_num: the number of sg which are writable (after readable ones)
185 * @data: the token identifying the buffer.
186 * @gfp: how to do memory allocations (if necessary).
187 *
188 * Caller must ensure we don't call this with other virtqueue operations
189 * at the same time (except where noted).
190 *
191 * Returns remaining capacity of queue or a negative error
192 * (ie. ENOSPC). Note that it only really makes sense to treat all
193 * positive return values as "available": indirect buffers mean that
194 * we can put an entire sg[] array inside a single queue entry.
195 */
Rusty Russellf96fde42012-01-12 15:44:42 +1030196int virtqueue_add_buf(struct virtqueue *_vq,
197 struct scatterlist sg[],
198 unsigned int out,
199 unsigned int in,
200 void *data,
201 gfp_t gfp)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000202{
203 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930204 unsigned int i, avail, uninitialized_var(prev);
205 int head;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000206
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100207 START_USE(vq);
208
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000209 BUG_ON(data == NULL);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100210
Rusty Russelle93300b2012-01-12 15:44:43 +1030211#ifdef DEBUG
212 {
213 ktime_t now = ktime_get();
214
215 /* No kick or get, with .1 second between? Warn. */
216 if (vq->last_add_time_valid)
217 WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time))
218 > 100);
219 vq->last_add_time = now;
220 vq->last_add_time_valid = true;
221 }
222#endif
223
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100224 /* If the host supports indirect descriptor tables, and we have multiple
225 * buffers, then go indirect. FIXME: tune this threshold */
Rusty Russell06ca2872012-10-16 23:56:14 +1030226 if (vq->indirect && (out + in) > 1 && vq->vq.num_free) {
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300227 head = vring_add_indirect(vq, sg, out, in, gfp);
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930228 if (likely(head >= 0))
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100229 goto add_head;
230 }
231
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000232 BUG_ON(out + in > vq->vring.num);
233 BUG_ON(out + in == 0);
234
Rusty Russell06ca2872012-10-16 23:56:14 +1030235 if (vq->vq.num_free < out + in) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000236 pr_debug("Can't add buf len %i - avail = %i\n",
Rusty Russell06ca2872012-10-16 23:56:14 +1030237 out + in, vq->vq.num_free);
Rusty Russell44653ea2008-07-25 12:06:04 -0500238 /* FIXME: for historical reasons, we force a notify here if
239 * there are outgoing parts to the buffer. Presumably the
240 * host should service the ring ASAP. */
241 if (out)
242 vq->notify(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000243 END_USE(vq);
244 return -ENOSPC;
245 }
246
247 /* We're about to use some buffers from the free list. */
Rusty Russell06ca2872012-10-16 23:56:14 +1030248 vq->vq.num_free -= out + in;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000249
250 head = vq->free_head;
251 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
252 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
Rusty Russell15f9c892008-02-04 23:50:05 -0500253 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000254 vq->vring.desc[i].len = sg->length;
255 prev = i;
256 sg++;
257 }
258 for (; in; i = vq->vring.desc[i].next, in--) {
259 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
Rusty Russell15f9c892008-02-04 23:50:05 -0500260 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000261 vq->vring.desc[i].len = sg->length;
262 prev = i;
263 sg++;
264 }
265 /* Last one doesn't continue. */
266 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
267
268 /* Update free pointer */
269 vq->free_head = i;
270
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100271add_head:
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000272 /* Set token. */
273 vq->data[head] = data;
274
275 /* Put entry in available array (but don't update avail->idx until they
Rusty Russell3b720b82012-01-12 15:44:43 +1030276 * do sync). */
Rusty Russellee7cd892012-01-12 15:44:43 +1030277 avail = (vq->vring.avail->idx & (vq->vring.num-1));
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000278 vq->vring.avail->ring[avail] = head;
279
Rusty Russellee7cd892012-01-12 15:44:43 +1030280 /* Descriptors and available array need to be set before we expose the
281 * new available array entries. */
282 virtio_wmb(vq);
283 vq->vring.avail->idx++;
284 vq->num_added++;
285
286 /* This is very unlikely, but theoretically possible. Kick
287 * just in case. */
288 if (unlikely(vq->num_added == (1 << 16) - 1))
289 virtqueue_kick(_vq);
290
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000291 pr_debug("Added buffer head %i to %p\n", head, vq);
292 END_USE(vq);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600293
Rusty Russell06ca2872012-10-16 23:56:14 +1030294 return vq->vq.num_free;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000295}
Rusty Russellf96fde42012-01-12 15:44:42 +1030296EXPORT_SYMBOL_GPL(virtqueue_add_buf);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000297
Rusty Russell5dfc1762012-01-12 15:44:42 +1030298/**
Rusty Russell41f03772012-01-12 15:44:43 +1030299 * virtqueue_kick_prepare - first half of split virtqueue_kick call.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030300 * @vq: the struct virtqueue
301 *
Rusty Russell41f03772012-01-12 15:44:43 +1030302 * Instead of virtqueue_kick(), you can do:
303 * if (virtqueue_kick_prepare(vq))
304 * virtqueue_notify(vq);
Rusty Russell5dfc1762012-01-12 15:44:42 +1030305 *
Rusty Russell41f03772012-01-12 15:44:43 +1030306 * This is sometimes useful because the virtqueue_kick_prepare() needs
307 * to be serialized, but the actual virtqueue_notify() call does not.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030308 */
Rusty Russell41f03772012-01-12 15:44:43 +1030309bool virtqueue_kick_prepare(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000310{
311 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300312 u16 new, old;
Rusty Russell41f03772012-01-12 15:44:43 +1030313 bool needs_kick;
314
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000315 START_USE(vq);
Jason Wanga72caae2012-01-20 16:17:08 +0800316 /* We need to expose available array entries before checking avail
317 * event. */
318 virtio_mb(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000319
Rusty Russellee7cd892012-01-12 15:44:43 +1030320 old = vq->vring.avail->idx - vq->num_added;
321 new = vq->vring.avail->idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000322 vq->num_added = 0;
323
Rusty Russelle93300b2012-01-12 15:44:43 +1030324#ifdef DEBUG
325 if (vq->last_add_time_valid) {
326 WARN_ON(ktime_to_ms(ktime_sub(ktime_get(),
327 vq->last_add_time)) > 100);
328 }
329 vq->last_add_time_valid = false;
330#endif
331
Rusty Russell41f03772012-01-12 15:44:43 +1030332 if (vq->event) {
333 needs_kick = vring_need_event(vring_avail_event(&vq->vring),
334 new, old);
335 } else {
336 needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
337 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000338 END_USE(vq);
Rusty Russell41f03772012-01-12 15:44:43 +1030339 return needs_kick;
340}
341EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
342
343/**
344 * virtqueue_notify - second half of split virtqueue_kick call.
345 * @vq: the struct virtqueue
346 *
347 * This does not need to be serialized.
348 */
349void virtqueue_notify(struct virtqueue *_vq)
350{
351 struct vring_virtqueue *vq = to_vvq(_vq);
352
353 /* Prod other side to tell it about changes. */
354 vq->notify(_vq);
355}
356EXPORT_SYMBOL_GPL(virtqueue_notify);
357
358/**
359 * virtqueue_kick - update after add_buf
360 * @vq: the struct virtqueue
361 *
362 * After one or more virtqueue_add_buf calls, invoke this to kick
363 * the other side.
364 *
365 * Caller must ensure we don't call this with other virtqueue
366 * operations at the same time (except where noted).
367 */
368void virtqueue_kick(struct virtqueue *vq)
369{
370 if (virtqueue_kick_prepare(vq))
371 virtqueue_notify(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000372}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300373EXPORT_SYMBOL_GPL(virtqueue_kick);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000374
375static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
376{
377 unsigned int i;
378
379 /* Clear data ptr. */
380 vq->data[head] = NULL;
381
382 /* Put back on free list: find end */
383 i = head;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100384
385 /* Free the indirect table */
386 if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
387 kfree(phys_to_virt(vq->vring.desc[i].addr));
388
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000389 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
390 i = vq->vring.desc[i].next;
Rusty Russell06ca2872012-10-16 23:56:14 +1030391 vq->vq.num_free++;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000392 }
393
394 vq->vring.desc[i].next = vq->free_head;
395 vq->free_head = head;
396 /* Plus final descriptor */
Rusty Russell06ca2872012-10-16 23:56:14 +1030397 vq->vq.num_free++;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000398}
399
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000400static inline bool more_used(const struct vring_virtqueue *vq)
401{
402 return vq->last_used_idx != vq->vring.used->idx;
403}
404
Rusty Russell5dfc1762012-01-12 15:44:42 +1030405/**
406 * virtqueue_get_buf - get the next used buffer
407 * @vq: the struct virtqueue we're talking about.
408 * @len: the length written into the buffer
409 *
410 * If the driver wrote data into the buffer, @len will be set to the
411 * amount written. This means you don't need to clear the buffer
412 * beforehand to ensure there's no data leakage in the case of short
413 * writes.
414 *
415 * Caller must ensure we don't call this with other virtqueue
416 * operations at the same time (except where noted).
417 *
418 * Returns NULL if there are no used buffers, or the "data" token
Rusty Russellf96fde42012-01-12 15:44:42 +1030419 * handed to virtqueue_add_buf().
Rusty Russell5dfc1762012-01-12 15:44:42 +1030420 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300421void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000422{
423 struct vring_virtqueue *vq = to_vvq(_vq);
424 void *ret;
425 unsigned int i;
Rusty Russell3b720b82012-01-12 15:44:43 +1030426 u16 last_used;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000427
428 START_USE(vq);
429
Rusty Russell5ef82752008-05-02 21:50:43 -0500430 if (unlikely(vq->broken)) {
431 END_USE(vq);
432 return NULL;
433 }
434
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000435 if (!more_used(vq)) {
436 pr_debug("No more buffers in queue\n");
437 END_USE(vq);
438 return NULL;
439 }
440
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200441 /* Only get used array entries after they have been exposed by host. */
Rusty Russell7b21e342012-01-12 15:44:42 +1030442 virtio_rmb(vq);
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200443
Rusty Russell3b720b82012-01-12 15:44:43 +1030444 last_used = (vq->last_used_idx & (vq->vring.num - 1));
445 i = vq->vring.used->ring[last_used].id;
446 *len = vq->vring.used->ring[last_used].len;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000447
448 if (unlikely(i >= vq->vring.num)) {
449 BAD_RING(vq, "id %u out of range\n", i);
450 return NULL;
451 }
452 if (unlikely(!vq->data[i])) {
453 BAD_RING(vq, "id %u is not a head!\n", i);
454 return NULL;
455 }
456
457 /* detach_buf clears data, so grab it now. */
458 ret = vq->data[i];
459 detach_buf(vq, i);
460 vq->last_used_idx++;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300461 /* If we expect an interrupt for the next entry, tell host
462 * by writing event index and flush out the write before
463 * the read in the next get_buf call. */
464 if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
465 vring_used_event(&vq->vring) = vq->last_used_idx;
Rusty Russell7b21e342012-01-12 15:44:42 +1030466 virtio_mb(vq);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300467 }
468
Rusty Russelle93300b2012-01-12 15:44:43 +1030469#ifdef DEBUG
470 vq->last_add_time_valid = false;
471#endif
472
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000473 END_USE(vq);
474 return ret;
475}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300476EXPORT_SYMBOL_GPL(virtqueue_get_buf);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000477
Rusty Russell5dfc1762012-01-12 15:44:42 +1030478/**
479 * virtqueue_disable_cb - disable callbacks
480 * @vq: the struct virtqueue we're talking about.
481 *
482 * Note that this is not necessarily synchronous, hence unreliable and only
483 * useful as an optimization.
484 *
485 * Unlike other operations, this need not be serialized.
486 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300487void virtqueue_disable_cb(struct virtqueue *_vq)
Rusty Russell18445c42008-02-04 23:49:57 -0500488{
489 struct vring_virtqueue *vq = to_vvq(_vq);
490
Rusty Russell18445c42008-02-04 23:49:57 -0500491 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
Rusty Russell18445c42008-02-04 23:49:57 -0500492}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300493EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
Rusty Russell18445c42008-02-04 23:49:57 -0500494
Rusty Russell5dfc1762012-01-12 15:44:42 +1030495/**
496 * virtqueue_enable_cb - restart callbacks after disable_cb.
497 * @vq: the struct virtqueue we're talking about.
498 *
499 * This re-enables callbacks; it returns "false" if there are pending
500 * buffers in the queue, to detect a possible race between the driver
501 * checking for more work, and enabling callbacks.
502 *
503 * Caller must ensure we don't call this with other virtqueue
504 * operations at the same time (except where noted).
505 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300506bool virtqueue_enable_cb(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000507{
508 struct vring_virtqueue *vq = to_vvq(_vq);
509
510 START_USE(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000511
512 /* We optimistically turn back on interrupts, then check if there was
513 * more to do. */
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300514 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
515 * either clear the flags bit or point the event index at the next
516 * entry. Always do both to keep code simple. */
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000517 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300518 vring_used_event(&vq->vring) = vq->last_used_idx;
Rusty Russell7b21e342012-01-12 15:44:42 +1030519 virtio_mb(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000520 if (unlikely(more_used(vq))) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000521 END_USE(vq);
522 return false;
523 }
524
525 END_USE(vq);
526 return true;
527}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300528EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000529
Rusty Russell5dfc1762012-01-12 15:44:42 +1030530/**
531 * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
532 * @vq: the struct virtqueue we're talking about.
533 *
534 * This re-enables callbacks but hints to the other side to delay
535 * interrupts until most of the available buffers have been processed;
536 * it returns "false" if there are many pending buffers in the queue,
537 * to detect a possible race between the driver checking for more work,
538 * and enabling callbacks.
539 *
540 * Caller must ensure we don't call this with other virtqueue
541 * operations at the same time (except where noted).
542 */
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300543bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
544{
545 struct vring_virtqueue *vq = to_vvq(_vq);
546 u16 bufs;
547
548 START_USE(vq);
549
550 /* We optimistically turn back on interrupts, then check if there was
551 * more to do. */
552 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
553 * either clear the flags bit or point the event index at the next
554 * entry. Always do both to keep code simple. */
555 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
556 /* TODO: tune this threshold */
557 bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
558 vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
Rusty Russell7b21e342012-01-12 15:44:42 +1030559 virtio_mb(vq);
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300560 if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
561 END_USE(vq);
562 return false;
563 }
564
565 END_USE(vq);
566 return true;
567}
568EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
569
Rusty Russell5dfc1762012-01-12 15:44:42 +1030570/**
571 * virtqueue_detach_unused_buf - detach first unused buffer
572 * @vq: the struct virtqueue we're talking about.
573 *
Rusty Russellf96fde42012-01-12 15:44:42 +1030574 * Returns NULL or the "data" token handed to virtqueue_add_buf().
Rusty Russell5dfc1762012-01-12 15:44:42 +1030575 * This is not valid on an active queue; it is useful only for device
576 * shutdown.
577 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300578void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
Shirley Mac021eac2010-01-18 19:15:23 +0530579{
580 struct vring_virtqueue *vq = to_vvq(_vq);
581 unsigned int i;
582 void *buf;
583
584 START_USE(vq);
585
586 for (i = 0; i < vq->vring.num; i++) {
587 if (!vq->data[i])
588 continue;
589 /* detach_buf clears data, so grab it now. */
590 buf = vq->data[i];
591 detach_buf(vq, i);
Amit Shahb3258ff2011-03-16 19:12:10 +0530592 vq->vring.avail->idx--;
Shirley Mac021eac2010-01-18 19:15:23 +0530593 END_USE(vq);
594 return buf;
595 }
596 /* That should have freed everything. */
Rusty Russell06ca2872012-10-16 23:56:14 +1030597 BUG_ON(vq->vq.num_free != vq->vring.num);
Shirley Mac021eac2010-01-18 19:15:23 +0530598
599 END_USE(vq);
600 return NULL;
601}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300602EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
Shirley Mac021eac2010-01-18 19:15:23 +0530603
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000604irqreturn_t vring_interrupt(int irq, void *_vq)
605{
606 struct vring_virtqueue *vq = to_vvq(_vq);
607
608 if (!more_used(vq)) {
609 pr_debug("virtqueue interrupt with no work for %p\n", vq);
610 return IRQ_NONE;
611 }
612
613 if (unlikely(vq->broken))
614 return IRQ_HANDLED;
615
616 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
Rusty Russell18445c42008-02-04 23:49:57 -0500617 if (vq->vq.callback)
618 vq->vq.callback(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000619
620 return IRQ_HANDLED;
621}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500622EXPORT_SYMBOL_GPL(vring_interrupt);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000623
Jason Wang17bb6d42012-08-28 13:54:13 +0200624struct virtqueue *vring_new_virtqueue(unsigned int index,
625 unsigned int num,
Rusty Russell87c7d572008-12-30 09:26:03 -0600626 unsigned int vring_align,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000627 struct virtio_device *vdev,
Rusty Russell7b21e342012-01-12 15:44:42 +1030628 bool weak_barriers,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000629 void *pages,
630 void (*notify)(struct virtqueue *),
Rusty Russell9499f5e2009-06-12 22:16:35 -0600631 void (*callback)(struct virtqueue *),
632 const char *name)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000633{
634 struct vring_virtqueue *vq;
635 unsigned int i;
636
Rusty Russell42b36cc2007-11-12 13:39:18 +1100637 /* We assume num is a power of 2. */
638 if (num & (num - 1)) {
639 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
640 return NULL;
641 }
642
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000643 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
644 if (!vq)
645 return NULL;
646
Rusty Russell87c7d572008-12-30 09:26:03 -0600647 vring_init(&vq->vring, num, pages, vring_align);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000648 vq->vq.callback = callback;
649 vq->vq.vdev = vdev;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600650 vq->vq.name = name;
Rusty Russell06ca2872012-10-16 23:56:14 +1030651 vq->vq.num_free = num;
652 vq->vq.index = index;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000653 vq->notify = notify;
Rusty Russell7b21e342012-01-12 15:44:42 +1030654 vq->weak_barriers = weak_barriers;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000655 vq->broken = false;
656 vq->last_used_idx = 0;
657 vq->num_added = 0;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600658 list_add_tail(&vq->vq.list, &vdev->vqs);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000659#ifdef DEBUG
660 vq->in_use = false;
Rusty Russelle93300b2012-01-12 15:44:43 +1030661 vq->last_add_time_valid = false;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000662#endif
663
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100664 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300665 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100666
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000667 /* No callback? Tell other side not to bother us. */
668 if (!callback)
669 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
670
671 /* Put everything in free lists. */
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000672 vq->free_head = 0;
Amit Shah3b870622010-02-12 10:32:14 +0530673 for (i = 0; i < num-1; i++) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000674 vq->vring.desc[i].next = i+1;
Amit Shah3b870622010-02-12 10:32:14 +0530675 vq->data[i] = NULL;
676 }
677 vq->data[i] = NULL;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000678
679 return &vq->vq;
680}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500681EXPORT_SYMBOL_GPL(vring_new_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000682
683void vring_del_virtqueue(struct virtqueue *vq)
684{
Rusty Russell9499f5e2009-06-12 22:16:35 -0600685 list_del(&vq->list);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000686 kfree(to_vvq(vq));
687}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500688EXPORT_SYMBOL_GPL(vring_del_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000689
Rusty Russelle34f8722008-07-25 12:06:13 -0500690/* Manipulates transport-specific feature bits. */
691void vring_transport_features(struct virtio_device *vdev)
692{
693 unsigned int i;
694
695 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
696 switch (i) {
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100697 case VIRTIO_RING_F_INDIRECT_DESC:
698 break;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300699 case VIRTIO_RING_F_EVENT_IDX:
700 break;
Rusty Russelle34f8722008-07-25 12:06:13 -0500701 default:
702 /* We don't understand this bit. */
703 clear_bit(i, vdev->features);
704 }
705 }
706}
707EXPORT_SYMBOL_GPL(vring_transport_features);
708
Rusty Russell5dfc1762012-01-12 15:44:42 +1030709/**
710 * virtqueue_get_vring_size - return the size of the virtqueue's vring
711 * @vq: the struct virtqueue containing the vring of interest.
712 *
713 * Returns the size of the vring. This is mainly used for boasting to
714 * userspace. Unlike other operations, this need not be serialized.
715 */
Rick Jones8f9f4662011-10-19 08:10:59 +0000716unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
717{
718
719 struct vring_virtqueue *vq = to_vvq(_vq);
720
721 return vq->vring.num;
722}
723EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
724
Rusty Russellc6fd4702008-02-04 23:50:05 -0500725MODULE_LICENSE("GPL");