blob: 245177c286ae7c3d2120864e66fd023f65669bc0 [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
27#ifdef DEBUG
28/* For development, we want to crash whenever the ring is screwed. */
Rusty Russell9499f5e2009-06-12 22:16:35 -060029#define BAD_RING(_vq, fmt, args...) \
30 do { \
31 dev_err(&(_vq)->vq.vdev->dev, \
32 "%s:"fmt, (_vq)->vq.name, ##args); \
33 BUG(); \
34 } while (0)
Rusty Russellc5f841f2009-03-30 21:55:22 -060035/* Caller is supposed to guarantee no reentry. */
36#define START_USE(_vq) \
37 do { \
38 if ((_vq)->in_use) \
Rusty Russell9499f5e2009-06-12 22:16:35 -060039 panic("%s:in_use = %i\n", \
40 (_vq)->vq.name, (_vq)->in_use); \
Rusty Russellc5f841f2009-03-30 21:55:22 -060041 (_vq)->in_use = __LINE__; \
Rusty Russell9499f5e2009-06-12 22:16:35 -060042 } while (0)
Roel Kluin3a35ce72009-01-22 16:42:57 +010043#define END_USE(_vq) \
Rusty Russell97a545a2010-02-24 14:22:22 -060044 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100045#else
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 (_vq)->broken = true; \
51 } while (0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100052#define START_USE(vq)
53#define END_USE(vq)
54#endif
55
56struct vring_virtqueue
57{
58 struct virtqueue vq;
59
60 /* Actual memory layout for this queue */
61 struct vring vring;
62
Rusty Russell7b21e342012-01-12 15:44:42 +103063 /* Can we use weak barriers? */
64 bool weak_barriers;
65
Rusty Russell0a8a69d2007-10-22 11:03:40 +100066 /* Other side has made a mess, don't try any more. */
67 bool broken;
68
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +010069 /* Host supports indirect buffers */
70 bool indirect;
71
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +030072 /* Host publishes avail event idx */
73 bool event;
74
Rusty Russell0a8a69d2007-10-22 11:03:40 +100075 /* Head of free buffer list. */
76 unsigned int free_head;
77 /* Number we've added since last sync. */
78 unsigned int num_added;
79
80 /* Last used index we've seen. */
Anthony Liguori1bc49532007-11-07 15:49:24 -060081 u16 last_used_idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +100082
83 /* How to notify other side. FIXME: commonalize hcalls! */
84 void (*notify)(struct virtqueue *vq);
85
86#ifdef DEBUG
87 /* They're supposed to lock for us. */
88 unsigned int in_use;
Rusty Russelle93300b2012-01-12 15:44:43 +103089
90 /* Figure out if their kicks are too delayed. */
91 bool last_add_time_valid;
92 ktime_t last_add_time;
Rusty Russell0a8a69d2007-10-22 11:03:40 +100093#endif
94
95 /* Tokens for callbacks. */
96 void *data[];
97};
98
99#define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
100
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100101/* Set up an indirect table of descriptors and add it to the queue. */
102static int vring_add_indirect(struct vring_virtqueue *vq,
103 struct scatterlist sg[],
104 unsigned int out,
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300105 unsigned int in,
106 gfp_t gfp)
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100107{
108 struct vring_desc *desc;
109 unsigned head;
110 int i;
111
Will Deaconb92b1b82012-10-19 14:03:33 +0100112 /*
113 * We require lowmem mappings for the descriptors because
114 * otherwise virt_to_phys will give us bogus addresses in the
115 * virtqueue.
116 */
117 gfp &= ~(__GFP_HIGHMEM | __GFP_HIGH);
118
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300119 desc = kmalloc((out + in) * sizeof(struct vring_desc), gfp);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100120 if (!desc)
Michael S. Tsirkin686d3632010-06-10 18:16:11 +0300121 return -ENOMEM;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100122
123 /* Transfer entries from the sg list into the indirect page */
124 for (i = 0; i < out; i++) {
125 desc[i].flags = VRING_DESC_F_NEXT;
126 desc[i].addr = sg_phys(sg);
127 desc[i].len = sg->length;
128 desc[i].next = i+1;
129 sg++;
130 }
131 for (; i < (out + in); i++) {
132 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
133 desc[i].addr = sg_phys(sg);
134 desc[i].len = sg->length;
135 desc[i].next = i+1;
136 sg++;
137 }
138
139 /* Last one doesn't continue. */
140 desc[i-1].flags &= ~VRING_DESC_F_NEXT;
141 desc[i-1].next = 0;
142
143 /* We're about to use a buffer */
Rusty Russell06ca2872012-10-16 23:56:14 +1030144 vq->vq.num_free--;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100145
146 /* Use a single buffer which doesn't continue */
147 head = vq->free_head;
148 vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
149 vq->vring.desc[head].addr = virt_to_phys(desc);
150 vq->vring.desc[head].len = i * sizeof(struct vring_desc);
151
152 /* Update free pointer */
153 vq->free_head = vq->vring.desc[head].next;
154
155 return head;
156}
157
Rusty Russell5dfc1762012-01-12 15:44:42 +1030158/**
Rusty Russellf96fde42012-01-12 15:44:42 +1030159 * virtqueue_add_buf - expose buffer to other end
Rusty Russell5dfc1762012-01-12 15:44:42 +1030160 * @vq: the struct virtqueue we're talking about.
161 * @sg: the description of the buffer(s).
162 * @out_num: the number of sg readable by other side
163 * @in_num: the number of sg which are writable (after readable ones)
164 * @data: the token identifying the buffer.
165 * @gfp: how to do memory allocations (if necessary).
166 *
167 * Caller must ensure we don't call this with other virtqueue operations
168 * at the same time (except where noted).
169 *
Rusty Russell98e8c6b2012-10-16 23:56:15 +1030170 * Returns zero or a negative error (ie. ENOSPC, ENOMEM).
Rusty Russell5dfc1762012-01-12 15:44:42 +1030171 */
Rusty Russellf96fde42012-01-12 15:44:42 +1030172int virtqueue_add_buf(struct virtqueue *_vq,
173 struct scatterlist sg[],
174 unsigned int out,
175 unsigned int in,
176 void *data,
177 gfp_t gfp)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000178{
179 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930180 unsigned int i, avail, uninitialized_var(prev);
181 int head;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000182
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100183 START_USE(vq);
184
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000185 BUG_ON(data == NULL);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100186
Rusty Russelle93300b2012-01-12 15:44:43 +1030187#ifdef DEBUG
188 {
189 ktime_t now = ktime_get();
190
191 /* No kick or get, with .1 second between? Warn. */
192 if (vq->last_add_time_valid)
193 WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time))
194 > 100);
195 vq->last_add_time = now;
196 vq->last_add_time_valid = true;
197 }
198#endif
199
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100200 /* If the host supports indirect descriptor tables, and we have multiple
201 * buffers, then go indirect. FIXME: tune this threshold */
Rusty Russell06ca2872012-10-16 23:56:14 +1030202 if (vq->indirect && (out + in) > 1 && vq->vq.num_free) {
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +0300203 head = vring_add_indirect(vq, sg, out, in, gfp);
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930204 if (likely(head >= 0))
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100205 goto add_head;
206 }
207
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000208 BUG_ON(out + in > vq->vring.num);
209 BUG_ON(out + in == 0);
210
Rusty Russell06ca2872012-10-16 23:56:14 +1030211 if (vq->vq.num_free < out + in) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000212 pr_debug("Can't add buf len %i - avail = %i\n",
Rusty Russell06ca2872012-10-16 23:56:14 +1030213 out + in, vq->vq.num_free);
Rusty Russell44653ea2008-07-25 12:06:04 -0500214 /* FIXME: for historical reasons, we force a notify here if
215 * there are outgoing parts to the buffer. Presumably the
216 * host should service the ring ASAP. */
217 if (out)
218 vq->notify(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000219 END_USE(vq);
220 return -ENOSPC;
221 }
222
223 /* We're about to use some buffers from the free list. */
Rusty Russell06ca2872012-10-16 23:56:14 +1030224 vq->vq.num_free -= out + in;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000225
226 head = vq->free_head;
227 for (i = vq->free_head; out; i = vq->vring.desc[i].next, out--) {
228 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
Rusty Russell15f9c892008-02-04 23:50:05 -0500229 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000230 vq->vring.desc[i].len = sg->length;
231 prev = i;
232 sg++;
233 }
234 for (; in; i = vq->vring.desc[i].next, in--) {
235 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
Rusty Russell15f9c892008-02-04 23:50:05 -0500236 vq->vring.desc[i].addr = sg_phys(sg);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000237 vq->vring.desc[i].len = sg->length;
238 prev = i;
239 sg++;
240 }
241 /* Last one doesn't continue. */
242 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
243
244 /* Update free pointer */
245 vq->free_head = i;
246
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100247add_head:
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000248 /* Set token. */
249 vq->data[head] = data;
250
251 /* Put entry in available array (but don't update avail->idx until they
Rusty Russell3b720b82012-01-12 15:44:43 +1030252 * do sync). */
Rusty Russellee7cd892012-01-12 15:44:43 +1030253 avail = (vq->vring.avail->idx & (vq->vring.num-1));
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000254 vq->vring.avail->ring[avail] = head;
255
Rusty Russellee7cd892012-01-12 15:44:43 +1030256 /* Descriptors and available array need to be set before we expose the
257 * new available array entries. */
Rusty Russella9a0fef2013-03-18 13:22:19 +1030258 virtio_wmb(vq->weak_barriers);
Rusty Russellee7cd892012-01-12 15:44:43 +1030259 vq->vring.avail->idx++;
260 vq->num_added++;
261
262 /* This is very unlikely, but theoretically possible. Kick
263 * just in case. */
264 if (unlikely(vq->num_added == (1 << 16) - 1))
265 virtqueue_kick(_vq);
266
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000267 pr_debug("Added buffer head %i to %p\n", head, vq);
268 END_USE(vq);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600269
Rusty Russell98e8c6b2012-10-16 23:56:15 +1030270 return 0;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000271}
Rusty Russellf96fde42012-01-12 15:44:42 +1030272EXPORT_SYMBOL_GPL(virtqueue_add_buf);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000273
Rusty Russell5dfc1762012-01-12 15:44:42 +1030274/**
Rusty Russell41f03772012-01-12 15:44:43 +1030275 * virtqueue_kick_prepare - first half of split virtqueue_kick call.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030276 * @vq: the struct virtqueue
277 *
Rusty Russell41f03772012-01-12 15:44:43 +1030278 * Instead of virtqueue_kick(), you can do:
279 * if (virtqueue_kick_prepare(vq))
280 * virtqueue_notify(vq);
Rusty Russell5dfc1762012-01-12 15:44:42 +1030281 *
Rusty Russell41f03772012-01-12 15:44:43 +1030282 * This is sometimes useful because the virtqueue_kick_prepare() needs
283 * to be serialized, but the actual virtqueue_notify() call does not.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030284 */
Rusty Russell41f03772012-01-12 15:44:43 +1030285bool virtqueue_kick_prepare(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000286{
287 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300288 u16 new, old;
Rusty Russell41f03772012-01-12 15:44:43 +1030289 bool needs_kick;
290
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000291 START_USE(vq);
Jason Wanga72caae2012-01-20 16:17:08 +0800292 /* We need to expose available array entries before checking avail
293 * event. */
Rusty Russella9a0fef2013-03-18 13:22:19 +1030294 virtio_mb(vq->weak_barriers);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000295
Rusty Russellee7cd892012-01-12 15:44:43 +1030296 old = vq->vring.avail->idx - vq->num_added;
297 new = vq->vring.avail->idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000298 vq->num_added = 0;
299
Rusty Russelle93300b2012-01-12 15:44:43 +1030300#ifdef DEBUG
301 if (vq->last_add_time_valid) {
302 WARN_ON(ktime_to_ms(ktime_sub(ktime_get(),
303 vq->last_add_time)) > 100);
304 }
305 vq->last_add_time_valid = false;
306#endif
307
Rusty Russell41f03772012-01-12 15:44:43 +1030308 if (vq->event) {
309 needs_kick = vring_need_event(vring_avail_event(&vq->vring),
310 new, old);
311 } else {
312 needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
313 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000314 END_USE(vq);
Rusty Russell41f03772012-01-12 15:44:43 +1030315 return needs_kick;
316}
317EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
318
319/**
320 * virtqueue_notify - second half of split virtqueue_kick call.
321 * @vq: the struct virtqueue
322 *
323 * This does not need to be serialized.
324 */
325void virtqueue_notify(struct virtqueue *_vq)
326{
327 struct vring_virtqueue *vq = to_vvq(_vq);
328
329 /* Prod other side to tell it about changes. */
330 vq->notify(_vq);
331}
332EXPORT_SYMBOL_GPL(virtqueue_notify);
333
334/**
335 * virtqueue_kick - update after add_buf
336 * @vq: the struct virtqueue
337 *
338 * After one or more virtqueue_add_buf calls, invoke this to kick
339 * the other side.
340 *
341 * Caller must ensure we don't call this with other virtqueue
342 * operations at the same time (except where noted).
343 */
344void virtqueue_kick(struct virtqueue *vq)
345{
346 if (virtqueue_kick_prepare(vq))
347 virtqueue_notify(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000348}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300349EXPORT_SYMBOL_GPL(virtqueue_kick);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000350
351static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
352{
353 unsigned int i;
354
355 /* Clear data ptr. */
356 vq->data[head] = NULL;
357
358 /* Put back on free list: find end */
359 i = head;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100360
361 /* Free the indirect table */
362 if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
363 kfree(phys_to_virt(vq->vring.desc[i].addr));
364
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000365 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
366 i = vq->vring.desc[i].next;
Rusty Russell06ca2872012-10-16 23:56:14 +1030367 vq->vq.num_free++;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000368 }
369
370 vq->vring.desc[i].next = vq->free_head;
371 vq->free_head = head;
372 /* Plus final descriptor */
Rusty Russell06ca2872012-10-16 23:56:14 +1030373 vq->vq.num_free++;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000374}
375
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000376static inline bool more_used(const struct vring_virtqueue *vq)
377{
378 return vq->last_used_idx != vq->vring.used->idx;
379}
380
Rusty Russell5dfc1762012-01-12 15:44:42 +1030381/**
382 * virtqueue_get_buf - get the next used buffer
383 * @vq: the struct virtqueue we're talking about.
384 * @len: the length written into the buffer
385 *
386 * If the driver wrote data into the buffer, @len will be set to the
387 * amount written. This means you don't need to clear the buffer
388 * beforehand to ensure there's no data leakage in the case of short
389 * writes.
390 *
391 * Caller must ensure we don't call this with other virtqueue
392 * operations at the same time (except where noted).
393 *
394 * Returns NULL if there are no used buffers, or the "data" token
Rusty Russellf96fde42012-01-12 15:44:42 +1030395 * handed to virtqueue_add_buf().
Rusty Russell5dfc1762012-01-12 15:44:42 +1030396 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300397void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000398{
399 struct vring_virtqueue *vq = to_vvq(_vq);
400 void *ret;
401 unsigned int i;
Rusty Russell3b720b82012-01-12 15:44:43 +1030402 u16 last_used;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000403
404 START_USE(vq);
405
Rusty Russell5ef82752008-05-02 21:50:43 -0500406 if (unlikely(vq->broken)) {
407 END_USE(vq);
408 return NULL;
409 }
410
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000411 if (!more_used(vq)) {
412 pr_debug("No more buffers in queue\n");
413 END_USE(vq);
414 return NULL;
415 }
416
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200417 /* Only get used array entries after they have been exposed by host. */
Rusty Russella9a0fef2013-03-18 13:22:19 +1030418 virtio_rmb(vq->weak_barriers);
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200419
Rusty Russell3b720b82012-01-12 15:44:43 +1030420 last_used = (vq->last_used_idx & (vq->vring.num - 1));
421 i = vq->vring.used->ring[last_used].id;
422 *len = vq->vring.used->ring[last_used].len;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000423
424 if (unlikely(i >= vq->vring.num)) {
425 BAD_RING(vq, "id %u out of range\n", i);
426 return NULL;
427 }
428 if (unlikely(!vq->data[i])) {
429 BAD_RING(vq, "id %u is not a head!\n", i);
430 return NULL;
431 }
432
433 /* detach_buf clears data, so grab it now. */
434 ret = vq->data[i];
435 detach_buf(vq, i);
436 vq->last_used_idx++;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300437 /* If we expect an interrupt for the next entry, tell host
438 * by writing event index and flush out the write before
439 * the read in the next get_buf call. */
440 if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
441 vring_used_event(&vq->vring) = vq->last_used_idx;
Rusty Russella9a0fef2013-03-18 13:22:19 +1030442 virtio_mb(vq->weak_barriers);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300443 }
444
Rusty Russelle93300b2012-01-12 15:44:43 +1030445#ifdef DEBUG
446 vq->last_add_time_valid = false;
447#endif
448
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000449 END_USE(vq);
450 return ret;
451}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300452EXPORT_SYMBOL_GPL(virtqueue_get_buf);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000453
Rusty Russell5dfc1762012-01-12 15:44:42 +1030454/**
455 * virtqueue_disable_cb - disable callbacks
456 * @vq: the struct virtqueue we're talking about.
457 *
458 * Note that this is not necessarily synchronous, hence unreliable and only
459 * useful as an optimization.
460 *
461 * Unlike other operations, this need not be serialized.
462 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300463void virtqueue_disable_cb(struct virtqueue *_vq)
Rusty Russell18445c42008-02-04 23:49:57 -0500464{
465 struct vring_virtqueue *vq = to_vvq(_vq);
466
Rusty Russell18445c42008-02-04 23:49:57 -0500467 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
Rusty Russell18445c42008-02-04 23:49:57 -0500468}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300469EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
Rusty Russell18445c42008-02-04 23:49:57 -0500470
Rusty Russell5dfc1762012-01-12 15:44:42 +1030471/**
472 * virtqueue_enable_cb - restart callbacks after disable_cb.
473 * @vq: the struct virtqueue we're talking about.
474 *
475 * This re-enables callbacks; it returns "false" if there are pending
476 * buffers in the queue, to detect a possible race between the driver
477 * checking for more work, and enabling callbacks.
478 *
479 * Caller must ensure we don't call this with other virtqueue
480 * operations at the same time (except where noted).
481 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300482bool virtqueue_enable_cb(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000483{
484 struct vring_virtqueue *vq = to_vvq(_vq);
485
486 START_USE(vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000487
488 /* We optimistically turn back on interrupts, then check if there was
489 * more to do. */
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300490 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
491 * either clear the flags bit or point the event index at the next
492 * entry. Always do both to keep code simple. */
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000493 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300494 vring_used_event(&vq->vring) = vq->last_used_idx;
Rusty Russella9a0fef2013-03-18 13:22:19 +1030495 virtio_mb(vq->weak_barriers);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000496 if (unlikely(more_used(vq))) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000497 END_USE(vq);
498 return false;
499 }
500
501 END_USE(vq);
502 return true;
503}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300504EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000505
Rusty Russell5dfc1762012-01-12 15:44:42 +1030506/**
507 * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
508 * @vq: the struct virtqueue we're talking about.
509 *
510 * This re-enables callbacks but hints to the other side to delay
511 * interrupts until most of the available buffers have been processed;
512 * it returns "false" if there are many pending buffers in the queue,
513 * to detect a possible race between the driver checking for more work,
514 * and enabling callbacks.
515 *
516 * Caller must ensure we don't call this with other virtqueue
517 * operations at the same time (except where noted).
518 */
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300519bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
520{
521 struct vring_virtqueue *vq = to_vvq(_vq);
522 u16 bufs;
523
524 START_USE(vq);
525
526 /* We optimistically turn back on interrupts, then check if there was
527 * more to do. */
528 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
529 * either clear the flags bit or point the event index at the next
530 * entry. Always do both to keep code simple. */
531 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
532 /* TODO: tune this threshold */
533 bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
534 vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
Rusty Russella9a0fef2013-03-18 13:22:19 +1030535 virtio_mb(vq->weak_barriers);
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300536 if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
537 END_USE(vq);
538 return false;
539 }
540
541 END_USE(vq);
542 return true;
543}
544EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
545
Rusty Russell5dfc1762012-01-12 15:44:42 +1030546/**
547 * virtqueue_detach_unused_buf - detach first unused buffer
548 * @vq: the struct virtqueue we're talking about.
549 *
Rusty Russellf96fde42012-01-12 15:44:42 +1030550 * Returns NULL or the "data" token handed to virtqueue_add_buf().
Rusty Russell5dfc1762012-01-12 15:44:42 +1030551 * This is not valid on an active queue; it is useful only for device
552 * shutdown.
553 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300554void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
Shirley Mac021eac2010-01-18 19:15:23 +0530555{
556 struct vring_virtqueue *vq = to_vvq(_vq);
557 unsigned int i;
558 void *buf;
559
560 START_USE(vq);
561
562 for (i = 0; i < vq->vring.num; i++) {
563 if (!vq->data[i])
564 continue;
565 /* detach_buf clears data, so grab it now. */
566 buf = vq->data[i];
567 detach_buf(vq, i);
Amit Shahb3258ff2011-03-16 19:12:10 +0530568 vq->vring.avail->idx--;
Shirley Mac021eac2010-01-18 19:15:23 +0530569 END_USE(vq);
570 return buf;
571 }
572 /* That should have freed everything. */
Rusty Russell06ca2872012-10-16 23:56:14 +1030573 BUG_ON(vq->vq.num_free != vq->vring.num);
Shirley Mac021eac2010-01-18 19:15:23 +0530574
575 END_USE(vq);
576 return NULL;
577}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300578EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
Shirley Mac021eac2010-01-18 19:15:23 +0530579
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000580irqreturn_t vring_interrupt(int irq, void *_vq)
581{
582 struct vring_virtqueue *vq = to_vvq(_vq);
583
584 if (!more_used(vq)) {
585 pr_debug("virtqueue interrupt with no work for %p\n", vq);
586 return IRQ_NONE;
587 }
588
589 if (unlikely(vq->broken))
590 return IRQ_HANDLED;
591
592 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
Rusty Russell18445c42008-02-04 23:49:57 -0500593 if (vq->vq.callback)
594 vq->vq.callback(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000595
596 return IRQ_HANDLED;
597}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500598EXPORT_SYMBOL_GPL(vring_interrupt);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000599
Jason Wang17bb6d42012-08-28 13:54:13 +0200600struct virtqueue *vring_new_virtqueue(unsigned int index,
601 unsigned int num,
Rusty Russell87c7d572008-12-30 09:26:03 -0600602 unsigned int vring_align,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000603 struct virtio_device *vdev,
Rusty Russell7b21e342012-01-12 15:44:42 +1030604 bool weak_barriers,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000605 void *pages,
606 void (*notify)(struct virtqueue *),
Rusty Russell9499f5e2009-06-12 22:16:35 -0600607 void (*callback)(struct virtqueue *),
608 const char *name)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000609{
610 struct vring_virtqueue *vq;
611 unsigned int i;
612
Rusty Russell42b36cc2007-11-12 13:39:18 +1100613 /* We assume num is a power of 2. */
614 if (num & (num - 1)) {
615 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
616 return NULL;
617 }
618
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000619 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
620 if (!vq)
621 return NULL;
622
Rusty Russell87c7d572008-12-30 09:26:03 -0600623 vring_init(&vq->vring, num, pages, vring_align);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000624 vq->vq.callback = callback;
625 vq->vq.vdev = vdev;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600626 vq->vq.name = name;
Rusty Russell06ca2872012-10-16 23:56:14 +1030627 vq->vq.num_free = num;
628 vq->vq.index = index;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000629 vq->notify = notify;
Rusty Russell7b21e342012-01-12 15:44:42 +1030630 vq->weak_barriers = weak_barriers;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000631 vq->broken = false;
632 vq->last_used_idx = 0;
633 vq->num_added = 0;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600634 list_add_tail(&vq->vq.list, &vdev->vqs);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000635#ifdef DEBUG
636 vq->in_use = false;
Rusty Russelle93300b2012-01-12 15:44:43 +1030637 vq->last_add_time_valid = false;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000638#endif
639
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100640 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300641 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100642
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000643 /* No callback? Tell other side not to bother us. */
644 if (!callback)
645 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
646
647 /* Put everything in free lists. */
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000648 vq->free_head = 0;
Amit Shah3b870622010-02-12 10:32:14 +0530649 for (i = 0; i < num-1; i++) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000650 vq->vring.desc[i].next = i+1;
Amit Shah3b870622010-02-12 10:32:14 +0530651 vq->data[i] = NULL;
652 }
653 vq->data[i] = NULL;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000654
655 return &vq->vq;
656}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500657EXPORT_SYMBOL_GPL(vring_new_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000658
659void vring_del_virtqueue(struct virtqueue *vq)
660{
Rusty Russell9499f5e2009-06-12 22:16:35 -0600661 list_del(&vq->list);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000662 kfree(to_vvq(vq));
663}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500664EXPORT_SYMBOL_GPL(vring_del_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000665
Rusty Russelle34f8722008-07-25 12:06:13 -0500666/* Manipulates transport-specific feature bits. */
667void vring_transport_features(struct virtio_device *vdev)
668{
669 unsigned int i;
670
671 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
672 switch (i) {
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100673 case VIRTIO_RING_F_INDIRECT_DESC:
674 break;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300675 case VIRTIO_RING_F_EVENT_IDX:
676 break;
Rusty Russelle34f8722008-07-25 12:06:13 -0500677 default:
678 /* We don't understand this bit. */
679 clear_bit(i, vdev->features);
680 }
681 }
682}
683EXPORT_SYMBOL_GPL(vring_transport_features);
684
Rusty Russell5dfc1762012-01-12 15:44:42 +1030685/**
686 * virtqueue_get_vring_size - return the size of the virtqueue's vring
687 * @vq: the struct virtqueue containing the vring of interest.
688 *
689 * Returns the size of the vring. This is mainly used for boasting to
690 * userspace. Unlike other operations, this need not be serialized.
691 */
Rick Jones8f9f4662011-10-19 08:10:59 +0000692unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
693{
694
695 struct vring_virtqueue *vq = to_vvq(_vq);
696
697 return vq->vring.num;
698}
699EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
700
Rusty Russellc6fd4702008-02-04 23:50:05 -0500701MODULE_LICENSE("GPL");