blob: 19287cf568d42d142bc7e16c2cff425ad65f3f22 [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 /* Number of free buffers */
97 unsigned int num_free;
98 /* Head of free buffer list. */
99 unsigned int free_head;
100 /* Number we've added since last sync. */
101 unsigned int num_added;
102
103 /* Last used index we've seen. */
Anthony Liguori1bc49532007-11-07 15:49:24 -0600104 u16 last_used_idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000105
106 /* How to notify other side. FIXME: commonalize hcalls! */
107 void (*notify)(struct virtqueue *vq);
108
109#ifdef DEBUG
110 /* They're supposed to lock for us. */
111 unsigned int in_use;
Rusty Russelle93300b2012-01-12 15:44:43 +1030112
113 /* Figure out if their kicks are too delayed. */
114 bool last_add_time_valid;
115 ktime_t last_add_time;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000116#endif
117
118 /* Tokens for callbacks. */
119 void *data[];
120};
121
122#define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
123
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100124/* Set up an indirect table of descriptors and add it to the queue. */
125static int vring_add_indirect(struct vring_virtqueue *vq,
126 struct scatterlist sg[],
127 unsigned int out,
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300128 unsigned int in,
129 gfp_t gfp)
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100130{
131 struct vring_desc *desc;
132 unsigned head;
133 int i;
134
Will Deacon55357482012-10-19 14:03:33 +0100135 /*
136 * We require lowmem mappings for the descriptors because
137 * otherwise virt_to_phys will give us bogus addresses in the
138 * virtqueue.
139 */
140 gfp &= ~(__GFP_HIGHMEM | __GFP_HIGH);
141
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300142 desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100143 if (!desc)
Michael S. Tsirkin686d3632010-06-10 18:16:11 +0300144 return -ENOMEM;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100145
146 /* Transfer entries from the sg list into the indirect page */
147 for (i = 0; i < out; i++) {
148 desc[i].flags = VRING_DESC_F_NEXT;
149 desc[i].addr = sg_phys(sg);
150 desc[i].len = sg->length;
151 desc[i].next = i+1;
152 sg++;
153 }
154 for (; i < (out + in); i++) {
155 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
156 desc[i].addr = sg_phys(sg);
157 desc[i].len = sg->length;
158 desc[i].next = i+1;
159 sg++;
160 }
161
162 /* Last one doesn't continue. */
163 desc[i-1].flags &= ~VRING_DESC_F_NEXT;
164 desc[i-1].next = 0;
165
166 /* We're about to use a buffer */
167 vq->num_free--;
168
169 /* Use a single buffer which doesn't continue */
170 head = vq->free_head;
171 vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
172 vq->vring.desc[head].addr = virt_to_phys(desc);
173 vq->vring.desc[head].len = i * sizeof(struct vring_desc);
174
175 /* Update free pointer */
176 vq->free_head = vq->vring.desc[head].next;
177
178 return head;
179}
180
Rusty Russell5dfc1762012-01-12 15:44:42 +1030181/**
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700182 * vring_add_buf - expose buffer to other end
Rusty Russell5dfc1762012-01-12 15:44:42 +1030183 * @vq: the struct virtqueue we're talking about.
184 * @sg: the description of the buffer(s).
185 * @out_num: the number of sg readable by other side
186 * @in_num: the number of sg which are writable (after readable ones)
187 * @data: the token identifying the buffer.
188 * @gfp: how to do memory allocations (if necessary).
189 *
190 * Caller must ensure we don't call this with other virtqueue operations
191 * at the same time (except where noted).
192 *
193 * Returns remaining capacity of queue or a negative error
194 * (ie. ENOSPC). Note that it only really makes sense to treat all
195 * positive return values as "available": indirect buffers mean that
196 * we can put an entire sg[] array inside a single queue entry.
197 */
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700198static int vring_add_buf(struct virtqueue *_vq,
Rusty Russellf96fde42012-01-12 15:44:42 +1030199 struct scatterlist sg[],
200 unsigned int out,
201 unsigned int in,
202 void *data,
203 gfp_t gfp)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000204{
205 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930206 unsigned int i, avail, uninitialized_var(prev);
207 int head;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000208
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100209 START_USE(vq);
210
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000211 BUG_ON(data == NULL);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100212
Rusty Russelle93300b2012-01-12 15:44:43 +1030213#ifdef DEBUG
214 {
215 ktime_t now = ktime_get();
216
217 /* No kick or get, with .1 second between? Warn. */
218 if (vq->last_add_time_valid)
219 WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time))
220 > 100);
221 vq->last_add_time = now;
222 vq->last_add_time_valid = true;
223 }
224#endif
225
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100226 /* If the host supports indirect descriptor tables, and we have multiple
227 * buffers, then go indirect. FIXME: tune this threshold */
228 if (vq->indirect && (out + in) > 1 && vq->num_free) {
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300229 head = vring_add_indirect(vq, sg, out, in, gfp);
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930230 if (likely(head >= 0))
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100231 goto add_head;
232 }
233
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000234 BUG_ON(out + in > vq->vring.num);
235 BUG_ON(out + in == 0);
236
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000237 if (vq->num_free < out + in) {
238 pr_debug("Can't add buf len %i - avail = %i\n",
239 out + in, vq->num_free);
Rusty Russell44653ea2008-07-25 12:06:04 -0500240 /* FIXME: for historical reasons, we force a notify here if
241 * there are outgoing parts to the buffer. Presumably the
242 * host should service the ring ASAP. */
243 if (out)
244 vq->notify(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000245 END_USE(vq);
246 return -ENOSPC;
247 }
248
249 /* We're about to use some buffers from the free list. */
250 vq->num_free -= out + in;
251
252 head = vq->free_head;
253 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
254 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
Rusty Russell15f9c892008-02-04 23:50:05 -0500255 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000256 vq->vring.desc[i].len = sg->length;
257 prev = i;
258 sg++;
259 }
260 for (; in; i = vq->vring.desc[i].next, in--) {
261 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
Rusty Russell15f9c892008-02-04 23:50:05 -0500262 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000263 vq->vring.desc[i].len = sg->length;
264 prev = i;
265 sg++;
266 }
267 /* Last one doesn't continue. */
268 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
269
270 /* Update free pointer */
271 vq->free_head = i;
272
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100273add_head:
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000274 /* Set token. */
275 vq->data[head] = data;
276
277 /* Put entry in available array (but don't update avail->idx until they
Rusty Russell3b720b82012-01-12 15:44:43 +1030278 * do sync). */
Rusty Russellee7cd892012-01-12 15:44:43 +1030279 avail = (vq->vring.avail->idx & (vq->vring.num-1));
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000280 vq->vring.avail->ring[avail] = head;
281
Rusty Russellee7cd892012-01-12 15:44:43 +1030282 /* Descriptors and available array need to be set before we expose the
283 * new available array entries. */
284 virtio_wmb(vq);
285 vq->vring.avail->idx++;
286 vq->num_added++;
287
288 /* This is very unlikely, but theoretically possible. Kick
289 * just in case. */
290 if (unlikely(vq->num_added == (1 << 16) - 1))
291 virtqueue_kick(_vq);
292
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000293 pr_debug("Added buffer head %i to %p\n", head, vq);
294 END_USE(vq);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600295
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600296 return vq->num_free;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000297}
298
Rusty Russell5dfc1762012-01-12 15:44:42 +1030299/**
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700300 * vring_kick_prepare - first half of split vring_kick call.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030301 * @vq: the struct virtqueue
302 *
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700303 * Instead of vring_kick(), you can do:
304 * if (vring_kick_prepare(vq))
305 * vring_kick_notify(vq);
Rusty Russell5dfc1762012-01-12 15:44:42 +1030306 *
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700307 * This is sometimes useful because the vring_kick_prepare() needs
308 * to be serialized, but the actual vring_kick_notify() call does not.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030309 */
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700310static bool vring_kick_prepare(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000311{
312 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300313 u16 new, old;
Rusty Russell41f03772012-01-12 15:44:43 +1030314 bool needs_kick;
315
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000316 START_USE(vq);
Jason Wanga72caae2012-01-20 16:17:08 +0800317 /* We need to expose available array entries before checking avail
318 * event. */
319 virtio_mb(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000320
Rusty Russellee7cd892012-01-12 15:44:43 +1030321 old = vq->vring.avail->idx - vq->num_added;
322 new = vq->vring.avail->idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000323 vq->num_added = 0;
324
Rusty Russelle93300b2012-01-12 15:44:43 +1030325#ifdef DEBUG
326 if (vq->last_add_time_valid) {
327 WARN_ON(ktime_to_ms(ktime_sub(ktime_get(),
328 vq->last_add_time)) > 100);
329 }
330 vq->last_add_time_valid = false;
331#endif
332
Rusty Russell41f03772012-01-12 15:44:43 +1030333 if (vq->event) {
334 needs_kick = vring_need_event(vring_avail_event(&vq->vring),
335 new, old);
336 } else {
337 needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
338 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000339 END_USE(vq);
Rusty Russell41f03772012-01-12 15:44:43 +1030340 return needs_kick;
341}
Rusty Russell41f03772012-01-12 15:44:43 +1030342
343/**
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700344 * vring_kick_notify - second half of split virtqueue_kick call.
Rusty Russell41f03772012-01-12 15:44:43 +1030345 * @vq: the struct virtqueue
346 *
347 * This does not need to be serialized.
348 */
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700349static void vring_kick_notify(struct virtqueue *_vq)
Rusty Russell41f03772012-01-12 15:44:43 +1030350{
351 struct vring_virtqueue *vq = to_vvq(_vq);
352
353 /* Prod other side to tell it about changes. */
354 vq->notify(_vq);
355}
Rusty Russell41f03772012-01-12 15:44:43 +1030356
357/**
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700358 * vring_kick - update after add_buf
Rusty Russell41f03772012-01-12 15:44:43 +1030359 * @vq: the struct virtqueue
360 *
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700361 * After one or more vring_add_buf calls, invoke this to kick
Rusty Russell41f03772012-01-12 15:44:43 +1030362 * the other side.
363 *
364 * Caller must ensure we don't call this with other virtqueue
365 * operations at the same time (except where noted).
366 */
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700367static void vring_kick(struct virtqueue *vq)
Rusty Russell41f03772012-01-12 15:44:43 +1030368{
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700369 if (vring_kick_prepare(vq))
370 vring_kick_notify(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000371}
372
373static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
374{
375 unsigned int i;
376
377 /* Clear data ptr. */
378 vq->data[head] = NULL;
379
380 /* Put back on free list: find end */
381 i = head;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100382
383 /* Free the indirect table */
384 if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
385 kfree(phys_to_virt(vq->vring.desc[i].addr));
386
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000387 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
388 i = vq->vring.desc[i].next;
389 vq->num_free++;
390 }
391
392 vq->vring.desc[i].next = vq->free_head;
393 vq->free_head = head;
394 /* Plus final descriptor */
395 vq->num_free++;
396}
397
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000398static inline bool more_used(const struct vring_virtqueue *vq)
399{
400 return vq->last_used_idx != vq->vring.used->idx;
401}
402
Rusty Russell5dfc1762012-01-12 15:44:42 +1030403/**
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700404 * vring_get_buf - get the next used buffer
Rusty Russell5dfc1762012-01-12 15:44:42 +1030405 * @vq: the struct virtqueue we're talking about.
406 * @len: the length written into the buffer
407 *
408 * If the driver wrote data into the buffer, @len will be set to the
409 * amount written. This means you don't need to clear the buffer
410 * beforehand to ensure there's no data leakage in the case of short
411 * writes.
412 *
413 * Caller must ensure we don't call this with other virtqueue
414 * operations at the same time (except where noted).
415 *
416 * Returns NULL if there are no used buffers, or the "data" token
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700417 * handed to vring_add_buf().
Rusty Russell5dfc1762012-01-12 15:44:42 +1030418 */
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700419static void *vring_get_buf(struct virtqueue *_vq, unsigned int *len)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000420{
421 struct vring_virtqueue *vq = to_vvq(_vq);
422 void *ret;
423 unsigned int i;
Rusty Russell3b720b82012-01-12 15:44:43 +1030424 u16 last_used;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000425
426 START_USE(vq);
427
Rusty Russell5ef82752008-05-02 21:50:43 -0500428 if (unlikely(vq->broken)) {
429 END_USE(vq);
430 return NULL;
431 }
432
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000433 if (!more_used(vq)) {
434 pr_debug("No more buffers in queue\n");
435 END_USE(vq);
436 return NULL;
437 }
438
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200439 /* Only get used array entries after they have been exposed by host. */
Rusty Russell7b21e342012-01-12 15:44:42 +1030440 virtio_rmb(vq);
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200441
Rusty Russell3b720b82012-01-12 15:44:43 +1030442 last_used = (vq->last_used_idx & (vq->vring.num - 1));
443 i = vq->vring.used->ring[last_used].id;
444 *len = vq->vring.used->ring[last_used].len;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000445
446 if (unlikely(i >= vq->vring.num)) {
447 BAD_RING(vq, "id %u out of range\n", i);
448 return NULL;
449 }
450 if (unlikely(!vq->data[i])) {
451 BAD_RING(vq, "id %u is not a head!\n", i);
452 return NULL;
453 }
454
455 /* detach_buf clears data, so grab it now. */
456 ret = vq->data[i];
457 detach_buf(vq, i);
458 vq->last_used_idx++;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300459 /* If we expect an interrupt for the next entry, tell host
460 * by writing event index and flush out the write before
461 * the read in the next get_buf call. */
462 if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
463 vring_used_event(&vq->vring) = vq->last_used_idx;
Rusty Russell7b21e342012-01-12 15:44:42 +1030464 virtio_mb(vq);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300465 }
466
Rusty Russelle93300b2012-01-12 15:44:43 +1030467#ifdef DEBUG
468 vq->last_add_time_valid = false;
469#endif
470
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000471 END_USE(vq);
472 return ret;
473}
474
Rusty Russell5dfc1762012-01-12 15:44:42 +1030475/**
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700476 * vring_disable_cb - disable callbacks
Rusty Russell5dfc1762012-01-12 15:44:42 +1030477 * @vq: the struct virtqueue we're talking about.
478 *
479 * Note that this is not necessarily synchronous, hence unreliable and only
480 * useful as an optimization.
481 *
482 * Unlike other operations, this need not be serialized.
483 */
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700484static void vring_disable_cb(struct virtqueue *_vq)
Rusty Russell18445c42008-02-04 23:49:57 -0500485{
486 struct vring_virtqueue *vq = to_vvq(_vq);
487
Rusty Russell18445c42008-02-04 23:49:57 -0500488 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
Rusty Russell18445c42008-02-04 23:49:57 -0500489}
490
Rusty Russell5dfc1762012-01-12 15:44:42 +1030491/**
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700492 * vring_enable_cb - restart callbacks after disable_cb.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030493 * @vq: the struct virtqueue we're talking about.
494 *
Michael S. Tsirkinb0d97db2013-07-09 13:19:18 +0300495 * This re-enables callbacks; it returns current queue state
496 * in an opaque unsigned value. This value should be later tested by
497 * virtqueue_poll, to detect a possible race between the driver checking for
498 * more work, and enabling callbacks.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030499 *
500 * Caller must ensure we don't call this with other virtqueue
501 * operations at the same time (except where noted).
502 */
Michael S. Tsirkinb0d97db2013-07-09 13:19:18 +0300503unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000504{
505 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkinb0d97db2013-07-09 13:19:18 +0300506 u16 last_used_idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000507
508 START_USE(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000509
510 /* We optimistically turn back on interrupts, then check if there was
511 * more to do. */
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300512 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
513 * either clear the flags bit or point the event index at the next
514 * entry. Always do both to keep code simple. */
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000515 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
Michael S. Tsirkinb0d97db2013-07-09 13:19:18 +0300516 vring_used_event(&vq->vring) = last_used_idx = vq->last_used_idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000517 END_USE(vq);
Michael S. Tsirkinb0d97db2013-07-09 13:19:18 +0300518 return last_used_idx;
519}
520EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
521
522/**
523 * virtqueue_poll - query pending used buffers
524 * @vq: the struct virtqueue we're talking about.
525 * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
526 *
527 * Returns "true" if there are pending used buffers in the queue.
528 *
529 * This does not need to be serialized.
530 */
531bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
532{
533 struct vring_virtqueue *vq = to_vvq(_vq);
534
535 virtio_mb(vq);
536 return (u16)last_used_idx != vq->vring.used->idx;
537}
538EXPORT_SYMBOL_GPL(virtqueue_poll);
539
540/**
541 * virtqueue_enable_cb - restart callbacks after disable_cb.
542 * @vq: the struct virtqueue we're talking about.
543 *
544 * This re-enables callbacks; it returns "false" if there are pending
545 * buffers in the queue, to detect a possible race between the driver
546 * checking for more work, and enabling callbacks.
547 *
548 * Caller must ensure we don't call this with other virtqueue
549 * operations at the same time (except where noted).
550 */
551bool virtqueue_enable_cb(struct virtqueue *_vq)
552{
553 unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
554 return !virtqueue_poll(_vq, last_used_idx);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000555}
556
Rusty Russell5dfc1762012-01-12 15:44:42 +1030557/**
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700558 * vring_enable_cb_delayed - restart callbacks after disable_cb.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030559 * @vq: the struct virtqueue we're talking about.
560 *
561 * This re-enables callbacks but hints to the other side to delay
562 * interrupts until most of the available buffers have been processed;
563 * it returns "false" if there are many pending buffers in the queue,
564 * to detect a possible race between the driver checking for more work,
565 * and enabling callbacks.
566 *
567 * Caller must ensure we don't call this with other virtqueue
568 * operations at the same time (except where noted).
569 */
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700570static bool vring_enable_cb_delayed(struct virtqueue *_vq)
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300571{
572 struct vring_virtqueue *vq = to_vvq(_vq);
573 u16 bufs;
574
575 START_USE(vq);
576
577 /* We optimistically turn back on interrupts, then check if there was
578 * more to do. */
579 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
580 * either clear the flags bit or point the event index at the next
581 * entry. Always do both to keep code simple. */
582 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
583 /* TODO: tune this threshold */
584 bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
585 vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
Rusty Russell7b21e342012-01-12 15:44:42 +1030586 virtio_mb(vq);
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300587 if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
588 END_USE(vq);
589 return false;
590 }
591
592 END_USE(vq);
593 return true;
594}
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300595
Rusty Russell5dfc1762012-01-12 15:44:42 +1030596/**
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700597 * vring_detach_unused_buf - detach first unused buffer
Rusty Russell5dfc1762012-01-12 15:44:42 +1030598 * @vq: the struct virtqueue we're talking about.
599 *
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700600 * Returns NULL or the "data" token handed to vring_add_buf().
Rusty Russell5dfc1762012-01-12 15:44:42 +1030601 * This is not valid on an active queue; it is useful only for device
602 * shutdown.
603 */
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700604static void *vring_detach_unused_buf(struct virtqueue *_vq)
Shirley Mac021eac2010-01-18 19:15:23 +0530605{
606 struct vring_virtqueue *vq = to_vvq(_vq);
607 unsigned int i;
608 void *buf;
609
610 START_USE(vq);
611
612 for (i = 0; i < vq->vring.num; i++) {
613 if (!vq->data[i])
614 continue;
615 /* detach_buf clears data, so grab it now. */
616 buf = vq->data[i];
617 detach_buf(vq, i);
Amit Shahb3258ff2011-03-16 19:12:10 +0530618 vq->vring.avail->idx--;
Shirley Mac021eac2010-01-18 19:15:23 +0530619 END_USE(vq);
620 return buf;
621 }
622 /* That should have freed everything. */
623 BUG_ON(vq->num_free != vq->vring.num);
624
625 END_USE(vq);
626 return NULL;
627}
628
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000629irqreturn_t vring_interrupt(int irq, void *_vq)
630{
631 struct vring_virtqueue *vq = to_vvq(_vq);
632
633 if (!more_used(vq)) {
634 pr_debug("virtqueue interrupt with no work for %p\n", vq);
635 return IRQ_NONE;
636 }
637
638 if (unlikely(vq->broken))
639 return IRQ_HANDLED;
640
641 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
Rusty Russell18445c42008-02-04 23:49:57 -0500642 if (vq->vq.callback)
643 vq->vq.callback(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000644
645 return IRQ_HANDLED;
646}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500647EXPORT_SYMBOL_GPL(vring_interrupt);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000648
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700649/**
650 * get_vring_size - return the size of the virtqueue's vring
651 * @vq: the struct virtqueue containing the vring of interest.
652 *
653 * Returns the size of the vring. This is mainly used for boasting to
654 * userspace. Unlike other operations, this need not be serialized.
655 */
656static unsigned int get_vring_size(struct virtqueue *_vq)
657{
658
659 struct vring_virtqueue *vq = to_vvq(_vq);
660
661 return vq->vring.num;
662}
663
664static struct virtqueue_ops vring_vq_ops = {
665 .add_buf = vring_add_buf,
666 .get_buf = vring_get_buf,
667 .kick = vring_kick,
668 .kick_prepare = vring_kick_prepare,
669 .kick_notify = vring_kick_notify,
670 .disable_cb = vring_disable_cb,
671 .enable_cb = vring_enable_cb,
672 .enable_cb_delayed = vring_enable_cb_delayed,
673 .detach_unused_buf = vring_detach_unused_buf,
674 .get_impl_size = get_vring_size,
675};
676
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000677struct virtqueue *vring_new_virtqueue(unsigned int num,
Rusty Russell87c7d572008-12-30 09:26:03 -0600678 unsigned int vring_align,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000679 struct virtio_device *vdev,
Rusty Russell7b21e342012-01-12 15:44:42 +1030680 bool weak_barriers,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000681 void *pages,
682 void (*notify)(struct virtqueue *),
Rusty Russell9499f5e2009-06-12 22:16:35 -0600683 void (*callback)(struct virtqueue *),
684 const char *name)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000685{
686 struct vring_virtqueue *vq;
687 unsigned int i;
688
Rusty Russell42b36cc2007-11-12 13:39:18 +1100689 /* We assume num is a power of 2. */
690 if (num & (num - 1)) {
691 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
692 return NULL;
693 }
694
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000695 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
696 if (!vq)
697 return NULL;
698
Rusty Russell87c7d572008-12-30 09:26:03 -0600699 vring_init(&vq->vring, num, pages, vring_align);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000700 vq->vq.callback = callback;
701 vq->vq.vdev = vdev;
Yatin Manerkarc89a2d02012-06-12 15:27:41 -0700702 vq->vq.vq_ops = &vring_vq_ops;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600703 vq->vq.name = name;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000704 vq->notify = notify;
Rusty Russell7b21e342012-01-12 15:44:42 +1030705 vq->weak_barriers = weak_barriers;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000706 vq->broken = false;
707 vq->last_used_idx = 0;
708 vq->num_added = 0;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600709 list_add_tail(&vq->vq.list, &vdev->vqs);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000710#ifdef DEBUG
711 vq->in_use = false;
Rusty Russelle93300b2012-01-12 15:44:43 +1030712 vq->last_add_time_valid = false;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000713#endif
714
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100715 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300716 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100717
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000718 /* No callback? Tell other side not to bother us. */
719 if (!callback)
720 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
721
722 /* Put everything in free lists. */
723 vq->num_free = num;
724 vq->free_head = 0;
Amit Shah3b870622010-02-12 10:32:14 +0530725 for (i = 0; i < num-1; i++) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000726 vq->vring.desc[i].next = i+1;
Amit Shah3b870622010-02-12 10:32:14 +0530727 vq->data[i] = NULL;
728 }
729 vq->data[i] = NULL;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000730
731 return &vq->vq;
732}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500733EXPORT_SYMBOL_GPL(vring_new_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000734
735void vring_del_virtqueue(struct virtqueue *vq)
736{
Rusty Russell9499f5e2009-06-12 22:16:35 -0600737 list_del(&vq->list);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000738 kfree(to_vvq(vq));
739}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500740EXPORT_SYMBOL_GPL(vring_del_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000741
Rusty Russelle34f8722008-07-25 12:06:13 -0500742/* Manipulates transport-specific feature bits. */
743void vring_transport_features(struct virtio_device *vdev)
744{
745 unsigned int i;
746
747 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
748 switch (i) {
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100749 case VIRTIO_RING_F_INDIRECT_DESC:
750 break;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300751 case VIRTIO_RING_F_EVENT_IDX:
752 break;
Rusty Russelle34f8722008-07-25 12:06:13 -0500753 default:
754 /* We don't understand this bit. */
755 clear_bit(i, vdev->features);
756 }
757 }
758}
759EXPORT_SYMBOL_GPL(vring_transport_features);
760
Rusty Russellc6fd4702008-02-04 23:50:05 -0500761MODULE_LICENSE("GPL");