blob: 28b5338fff715141572b9f40f266b62f74f6134a [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! */
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +103084 bool (*notify)(struct virtqueue *vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +100085
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
Rusty Russell13816c72013-03-20 15:37:09 +1030101static inline struct scatterlist *sg_next_chained(struct scatterlist *sg,
102 unsigned int *count)
103{
104 return sg_next(sg);
105}
106
107static inline struct scatterlist *sg_next_arr(struct scatterlist *sg,
108 unsigned int *count)
109{
110 if (--(*count) == 0)
111 return NULL;
112 return sg + 1;
113}
114
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100115/* Set up an indirect table of descriptors and add it to the queue. */
Rusty Russell13816c72013-03-20 15:37:09 +1030116static inline int vring_add_indirect(struct vring_virtqueue *vq,
117 struct scatterlist *sgs[],
118 struct scatterlist *(*next)
119 (struct scatterlist *, unsigned int *),
120 unsigned int total_sg,
121 unsigned int total_out,
122 unsigned int total_in,
123 unsigned int out_sgs,
124 unsigned int in_sgs,
125 gfp_t gfp)
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100126{
127 struct vring_desc *desc;
128 unsigned head;
Rusty Russell13816c72013-03-20 15:37:09 +1030129 struct scatterlist *sg;
130 int i, n;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100131
Will Deaconb92b1b82012-10-19 14:03:33 +0100132 /*
133 * We require lowmem mappings for the descriptors because
134 * otherwise virt_to_phys will give us bogus addresses in the
135 * virtqueue.
136 */
137 gfp &= ~(__GFP_HIGHMEM | __GFP_HIGH);
138
Rusty Russell13816c72013-03-20 15:37:09 +1030139 desc = kmalloc(total_sg * sizeof(struct vring_desc), gfp);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100140 if (!desc)
Michael S. Tsirkin686d3632010-06-10 18:16:11 +0300141 return -ENOMEM;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100142
Rusty Russell13816c72013-03-20 15:37:09 +1030143 /* Transfer entries from the sg lists into the indirect page */
144 i = 0;
145 for (n = 0; n < out_sgs; n++) {
146 for (sg = sgs[n]; sg; sg = next(sg, &total_out)) {
147 desc[i].flags = VRING_DESC_F_NEXT;
148 desc[i].addr = sg_phys(sg);
149 desc[i].len = sg->length;
150 desc[i].next = i+1;
151 i++;
152 }
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100153 }
Rusty Russell13816c72013-03-20 15:37:09 +1030154 for (; n < (out_sgs + in_sgs); n++) {
155 for (sg = sgs[n]; sg; sg = next(sg, &total_in)) {
156 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
157 desc[i].addr = sg_phys(sg);
158 desc[i].len = sg->length;
159 desc[i].next = i+1;
160 i++;
161 }
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100162 }
Rusty Russell13816c72013-03-20 15:37:09 +1030163 BUG_ON(i != total_sg);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100164
165 /* Last one doesn't continue. */
166 desc[i-1].flags &= ~VRING_DESC_F_NEXT;
167 desc[i-1].next = 0;
168
169 /* We're about to use a buffer */
Rusty Russell06ca2872012-10-16 23:56:14 +1030170 vq->vq.num_free--;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100171
172 /* Use a single buffer which doesn't continue */
173 head = vq->free_head;
174 vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
175 vq->vring.desc[head].addr = virt_to_phys(desc);
Rusty Russellbb478d82013-10-14 18:08:45 +1030176 /* kmemleak gives a false positive, as it's hidden by virt_to_phys */
177 kmemleak_ignore(desc);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100178 vq->vring.desc[head].len = i * sizeof(struct vring_desc);
179
180 /* Update free pointer */
181 vq->free_head = vq->vring.desc[head].next;
182
183 return head;
184}
185
Rusty Russell13816c72013-03-20 15:37:09 +1030186static inline int virtqueue_add(struct virtqueue *_vq,
187 struct scatterlist *sgs[],
188 struct scatterlist *(*next)
189 (struct scatterlist *, unsigned int *),
190 unsigned int total_out,
191 unsigned int total_in,
192 unsigned int out_sgs,
193 unsigned int in_sgs,
194 void *data,
195 gfp_t gfp)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000196{
197 struct vring_virtqueue *vq = to_vvq(_vq);
Rusty Russell13816c72013-03-20 15:37:09 +1030198 struct scatterlist *sg;
199 unsigned int i, n, avail, uninitialized_var(prev), total_sg;
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930200 int head;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000201
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100202 START_USE(vq);
203
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000204 BUG_ON(data == NULL);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100205
Rusty Russelle93300b2012-01-12 15:44:43 +1030206#ifdef DEBUG
207 {
208 ktime_t now = ktime_get();
209
210 /* No kick or get, with .1 second between? Warn. */
211 if (vq->last_add_time_valid)
212 WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time))
213 > 100);
214 vq->last_add_time = now;
215 vq->last_add_time_valid = true;
216 }
217#endif
218
Rusty Russell13816c72013-03-20 15:37:09 +1030219 total_sg = total_in + total_out;
220
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100221 /* If the host supports indirect descriptor tables, and we have multiple
222 * buffers, then go indirect. FIXME: tune this threshold */
Rusty Russell13816c72013-03-20 15:37:09 +1030223 if (vq->indirect && total_sg > 1 && vq->vq.num_free) {
224 head = vring_add_indirect(vq, sgs, next, total_sg, total_out,
225 total_in,
226 out_sgs, in_sgs, gfp);
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930227 if (likely(head >= 0))
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100228 goto add_head;
229 }
230
Rusty Russell13816c72013-03-20 15:37:09 +1030231 BUG_ON(total_sg > vq->vring.num);
232 BUG_ON(total_sg == 0);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000233
Rusty Russell13816c72013-03-20 15:37:09 +1030234 if (vq->vq.num_free < total_sg) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000235 pr_debug("Can't add buf len %i - avail = %i\n",
Rusty Russell13816c72013-03-20 15:37:09 +1030236 total_sg, vq->vq.num_free);
Rusty Russell44653ea2008-07-25 12:06:04 -0500237 /* FIXME: for historical reasons, we force a notify here if
238 * there are outgoing parts to the buffer. Presumably the
239 * host should service the ring ASAP. */
Rusty Russell13816c72013-03-20 15:37:09 +1030240 if (out_sgs)
Rusty Russell44653ea2008-07-25 12:06:04 -0500241 vq->notify(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000242 END_USE(vq);
243 return -ENOSPC;
244 }
245
246 /* We're about to use some buffers from the free list. */
Rusty Russell13816c72013-03-20 15:37:09 +1030247 vq->vq.num_free -= total_sg;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000248
Rusty Russell13816c72013-03-20 15:37:09 +1030249 head = i = vq->free_head;
250 for (n = 0; n < out_sgs; n++) {
251 for (sg = sgs[n]; sg; sg = next(sg, &total_out)) {
252 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
253 vq->vring.desc[i].addr = sg_phys(sg);
254 vq->vring.desc[i].len = sg->length;
255 prev = i;
256 i = vq->vring.desc[i].next;
257 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000258 }
Rusty Russell13816c72013-03-20 15:37:09 +1030259 for (; n < (out_sgs + in_sgs); n++) {
260 for (sg = sgs[n]; sg; sg = next(sg, &total_in)) {
261 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
262 vq->vring.desc[i].addr = sg_phys(sg);
263 vq->vring.desc[i].len = sg->length;
264 prev = i;
265 i = vq->vring.desc[i].next;
266 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000267 }
268 /* Last one doesn't continue. */
269 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
270
271 /* Update free pointer */
272 vq->free_head = i;
273
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100274add_head:
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000275 /* Set token. */
276 vq->data[head] = data;
277
278 /* Put entry in available array (but don't update avail->idx until they
Rusty Russell3b720b82012-01-12 15:44:43 +1030279 * do sync). */
Rusty Russellee7cd892012-01-12 15:44:43 +1030280 avail = (vq->vring.avail->idx & (vq->vring.num-1));
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000281 vq->vring.avail->ring[avail] = head;
282
Rusty Russellee7cd892012-01-12 15:44:43 +1030283 /* Descriptors and available array need to be set before we expose the
284 * new available array entries. */
Rusty Russella9a0fef2013-03-18 13:22:19 +1030285 virtio_wmb(vq->weak_barriers);
Rusty Russellee7cd892012-01-12 15:44:43 +1030286 vq->vring.avail->idx++;
287 vq->num_added++;
288
289 /* This is very unlikely, but theoretically possible. Kick
290 * just in case. */
291 if (unlikely(vq->num_added == (1 << 16) - 1))
292 virtqueue_kick(_vq);
293
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000294 pr_debug("Added buffer head %i to %p\n", head, vq);
295 END_USE(vq);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600296
Rusty Russell98e8c6b2012-10-16 23:56:15 +1030297 return 0;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000298}
Rusty Russell13816c72013-03-20 15:37:09 +1030299
300/**
Rusty Russell13816c72013-03-20 15:37:09 +1030301 * virtqueue_add_sgs - expose buffers to other end
302 * @vq: the struct virtqueue we're talking about.
303 * @sgs: array of terminated scatterlists.
304 * @out_num: the number of scatterlists readable by other side
305 * @in_num: the number of scatterlists which are writable (after readable ones)
306 * @data: the token identifying the buffer.
307 * @gfp: how to do memory allocations (if necessary).
308 *
309 * Caller must ensure we don't call this with other virtqueue operations
310 * at the same time (except where noted).
311 *
312 * Returns zero or a negative error (ie. ENOSPC, ENOMEM).
313 */
314int virtqueue_add_sgs(struct virtqueue *_vq,
315 struct scatterlist *sgs[],
316 unsigned int out_sgs,
317 unsigned int in_sgs,
318 void *data,
319 gfp_t gfp)
320{
321 unsigned int i, total_out, total_in;
322
323 /* Count them first. */
324 for (i = total_out = total_in = 0; i < out_sgs; i++) {
325 struct scatterlist *sg;
326 for (sg = sgs[i]; sg; sg = sg_next(sg))
327 total_out++;
328 }
329 for (; i < out_sgs + in_sgs; i++) {
330 struct scatterlist *sg;
331 for (sg = sgs[i]; sg; sg = sg_next(sg))
332 total_in++;
333 }
334 return virtqueue_add(_vq, sgs, sg_next_chained,
335 total_out, total_in, out_sgs, in_sgs, data, gfp);
336}
337EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
338
339/**
Rusty Russell282edb32013-03-20 15:44:26 +1030340 * virtqueue_add_outbuf - expose output buffers to other end
341 * @vq: the struct virtqueue we're talking about.
342 * @sgs: array of scatterlists (need not be terminated!)
343 * @num: the number of scatterlists readable by other side
344 * @data: the token identifying the buffer.
345 * @gfp: how to do memory allocations (if necessary).
346 *
347 * Caller must ensure we don't call this with other virtqueue operations
348 * at the same time (except where noted).
349 *
350 * Returns zero or a negative error (ie. ENOSPC, ENOMEM).
351 */
352int virtqueue_add_outbuf(struct virtqueue *vq,
353 struct scatterlist sg[], unsigned int num,
354 void *data,
355 gfp_t gfp)
356{
357 return virtqueue_add(vq, &sg, sg_next_arr, num, 0, 1, 0, data, gfp);
358}
359EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
360
361/**
362 * virtqueue_add_inbuf - expose input buffers to other end
363 * @vq: the struct virtqueue we're talking about.
364 * @sgs: array of scatterlists (need not be terminated!)
365 * @num: the number of scatterlists writable by other side
366 * @data: the token identifying the buffer.
367 * @gfp: how to do memory allocations (if necessary).
368 *
369 * Caller must ensure we don't call this with other virtqueue operations
370 * at the same time (except where noted).
371 *
372 * Returns zero or a negative error (ie. ENOSPC, ENOMEM).
373 */
374int virtqueue_add_inbuf(struct virtqueue *vq,
375 struct scatterlist sg[], unsigned int num,
376 void *data,
377 gfp_t gfp)
378{
379 return virtqueue_add(vq, &sg, sg_next_arr, 0, num, 0, 1, data, gfp);
380}
381EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
382
383/**
Rusty Russell41f03772012-01-12 15:44:43 +1030384 * virtqueue_kick_prepare - first half of split virtqueue_kick call.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030385 * @vq: the struct virtqueue
386 *
Rusty Russell41f03772012-01-12 15:44:43 +1030387 * Instead of virtqueue_kick(), you can do:
388 * if (virtqueue_kick_prepare(vq))
389 * virtqueue_notify(vq);
Rusty Russell5dfc1762012-01-12 15:44:42 +1030390 *
Rusty Russell41f03772012-01-12 15:44:43 +1030391 * This is sometimes useful because the virtqueue_kick_prepare() needs
392 * to be serialized, but the actual virtqueue_notify() call does not.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030393 */
Rusty Russell41f03772012-01-12 15:44:43 +1030394bool virtqueue_kick_prepare(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000395{
396 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300397 u16 new, old;
Rusty Russell41f03772012-01-12 15:44:43 +1030398 bool needs_kick;
399
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000400 START_USE(vq);
Jason Wanga72caae2012-01-20 16:17:08 +0800401 /* We need to expose available array entries before checking avail
402 * event. */
Rusty Russella9a0fef2013-03-18 13:22:19 +1030403 virtio_mb(vq->weak_barriers);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000404
Rusty Russellee7cd892012-01-12 15:44:43 +1030405 old = vq->vring.avail->idx - vq->num_added;
406 new = vq->vring.avail->idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000407 vq->num_added = 0;
408
Rusty Russelle93300b2012-01-12 15:44:43 +1030409#ifdef DEBUG
410 if (vq->last_add_time_valid) {
411 WARN_ON(ktime_to_ms(ktime_sub(ktime_get(),
412 vq->last_add_time)) > 100);
413 }
414 vq->last_add_time_valid = false;
415#endif
416
Rusty Russell41f03772012-01-12 15:44:43 +1030417 if (vq->event) {
418 needs_kick = vring_need_event(vring_avail_event(&vq->vring),
419 new, old);
420 } else {
421 needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
422 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000423 END_USE(vq);
Rusty Russell41f03772012-01-12 15:44:43 +1030424 return needs_kick;
425}
426EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
427
428/**
429 * virtqueue_notify - second half of split virtqueue_kick call.
430 * @vq: the struct virtqueue
431 *
432 * This does not need to be serialized.
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030433 *
434 * Returns false if host notify failed or queue is broken, otherwise true.
Rusty Russell41f03772012-01-12 15:44:43 +1030435 */
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030436bool virtqueue_notify(struct virtqueue *_vq)
Rusty Russell41f03772012-01-12 15:44:43 +1030437{
438 struct vring_virtqueue *vq = to_vvq(_vq);
439
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030440 if (unlikely(vq->broken))
441 return false;
442
Rusty Russell41f03772012-01-12 15:44:43 +1030443 /* Prod other side to tell it about changes. */
Heinz Graalfs2342d6a2013-11-05 21:20:27 +1030444 if (!vq->notify(_vq)) {
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030445 vq->broken = true;
446 return false;
447 }
448 return true;
Rusty Russell41f03772012-01-12 15:44:43 +1030449}
450EXPORT_SYMBOL_GPL(virtqueue_notify);
451
452/**
453 * virtqueue_kick - update after add_buf
454 * @vq: the struct virtqueue
455 *
Rusty Russellb3087e42013-05-20 12:15:44 +0930456 * After one or more virtqueue_add_* calls, invoke this to kick
Rusty Russell41f03772012-01-12 15:44:43 +1030457 * the other side.
458 *
459 * Caller must ensure we don't call this with other virtqueue
460 * operations at the same time (except where noted).
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030461 *
462 * Returns false if kick failed, otherwise true.
Rusty Russell41f03772012-01-12 15:44:43 +1030463 */
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030464bool virtqueue_kick(struct virtqueue *vq)
Rusty Russell41f03772012-01-12 15:44:43 +1030465{
466 if (virtqueue_kick_prepare(vq))
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030467 return virtqueue_notify(vq);
468 return true;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000469}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300470EXPORT_SYMBOL_GPL(virtqueue_kick);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000471
472static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
473{
474 unsigned int i;
475
476 /* Clear data ptr. */
477 vq->data[head] = NULL;
478
479 /* Put back on free list: find end */
480 i = head;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100481
482 /* Free the indirect table */
483 if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
484 kfree(phys_to_virt(vq->vring.desc[i].addr));
485
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000486 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
487 i = vq->vring.desc[i].next;
Rusty Russell06ca2872012-10-16 23:56:14 +1030488 vq->vq.num_free++;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000489 }
490
491 vq->vring.desc[i].next = vq->free_head;
492 vq->free_head = head;
493 /* Plus final descriptor */
Rusty Russell06ca2872012-10-16 23:56:14 +1030494 vq->vq.num_free++;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000495}
496
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000497static inline bool more_used(const struct vring_virtqueue *vq)
498{
499 return vq->last_used_idx != vq->vring.used->idx;
500}
501
Rusty Russell5dfc1762012-01-12 15:44:42 +1030502/**
503 * virtqueue_get_buf - get the next used buffer
504 * @vq: the struct virtqueue we're talking about.
505 * @len: the length written into the buffer
506 *
507 * If the driver wrote data into the buffer, @len will be set to the
508 * amount written. This means you don't need to clear the buffer
509 * beforehand to ensure there's no data leakage in the case of short
510 * writes.
511 *
512 * Caller must ensure we don't call this with other virtqueue
513 * operations at the same time (except where noted).
514 *
515 * Returns NULL if there are no used buffers, or the "data" token
Rusty Russellb3087e42013-05-20 12:15:44 +0930516 * handed to virtqueue_add_*().
Rusty Russell5dfc1762012-01-12 15:44:42 +1030517 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300518void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000519{
520 struct vring_virtqueue *vq = to_vvq(_vq);
521 void *ret;
522 unsigned int i;
Rusty Russell3b720b82012-01-12 15:44:43 +1030523 u16 last_used;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000524
525 START_USE(vq);
526
Rusty Russell5ef82752008-05-02 21:50:43 -0500527 if (unlikely(vq->broken)) {
528 END_USE(vq);
529 return NULL;
530 }
531
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000532 if (!more_used(vq)) {
533 pr_debug("No more buffers in queue\n");
534 END_USE(vq);
535 return NULL;
536 }
537
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200538 /* Only get used array entries after they have been exposed by host. */
Rusty Russella9a0fef2013-03-18 13:22:19 +1030539 virtio_rmb(vq->weak_barriers);
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200540
Rusty Russell3b720b82012-01-12 15:44:43 +1030541 last_used = (vq->last_used_idx & (vq->vring.num - 1));
542 i = vq->vring.used->ring[last_used].id;
543 *len = vq->vring.used->ring[last_used].len;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000544
545 if (unlikely(i >= vq->vring.num)) {
546 BAD_RING(vq, "id %u out of range\n", i);
547 return NULL;
548 }
549 if (unlikely(!vq->data[i])) {
550 BAD_RING(vq, "id %u is not a head!\n", i);
551 return NULL;
552 }
553
554 /* detach_buf clears data, so grab it now. */
555 ret = vq->data[i];
556 detach_buf(vq, i);
557 vq->last_used_idx++;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300558 /* If we expect an interrupt for the next entry, tell host
559 * by writing event index and flush out the write before
560 * the read in the next get_buf call. */
561 if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
562 vring_used_event(&vq->vring) = vq->last_used_idx;
Rusty Russella9a0fef2013-03-18 13:22:19 +1030563 virtio_mb(vq->weak_barriers);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300564 }
565
Rusty Russelle93300b2012-01-12 15:44:43 +1030566#ifdef DEBUG
567 vq->last_add_time_valid = false;
568#endif
569
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000570 END_USE(vq);
571 return ret;
572}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300573EXPORT_SYMBOL_GPL(virtqueue_get_buf);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000574
Rusty Russell5dfc1762012-01-12 15:44:42 +1030575/**
576 * virtqueue_disable_cb - disable callbacks
577 * @vq: the struct virtqueue we're talking about.
578 *
579 * Note that this is not necessarily synchronous, hence unreliable and only
580 * useful as an optimization.
581 *
582 * Unlike other operations, this need not be serialized.
583 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300584void virtqueue_disable_cb(struct virtqueue *_vq)
Rusty Russell18445c42008-02-04 23:49:57 -0500585{
586 struct vring_virtqueue *vq = to_vvq(_vq);
587
Rusty Russell18445c42008-02-04 23:49:57 -0500588 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
Rusty Russell18445c42008-02-04 23:49:57 -0500589}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300590EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
Rusty Russell18445c42008-02-04 23:49:57 -0500591
Rusty Russell5dfc1762012-01-12 15:44:42 +1030592/**
Michael S. Tsirkincc229882013-07-09 13:19:18 +0300593 * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
594 * @vq: the struct virtqueue we're talking about.
595 *
596 * This re-enables callbacks; it returns current queue state
597 * in an opaque unsigned value. This value should be later tested by
598 * virtqueue_poll, to detect a possible race between the driver checking for
599 * more work, and enabling callbacks.
600 *
601 * Caller must ensure we don't call this with other virtqueue
602 * operations at the same time (except where noted).
603 */
604unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
605{
606 struct vring_virtqueue *vq = to_vvq(_vq);
607 u16 last_used_idx;
608
609 START_USE(vq);
610
611 /* We optimistically turn back on interrupts, then check if there was
612 * more to do. */
613 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
614 * either clear the flags bit or point the event index at the next
615 * entry. Always do both to keep code simple. */
616 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
617 vring_used_event(&vq->vring) = last_used_idx = vq->last_used_idx;
618 END_USE(vq);
619 return last_used_idx;
620}
621EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
622
623/**
624 * virtqueue_poll - query pending used buffers
625 * @vq: the struct virtqueue we're talking about.
626 * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
627 *
628 * Returns "true" if there are pending used buffers in the queue.
629 *
630 * This does not need to be serialized.
631 */
632bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
633{
634 struct vring_virtqueue *vq = to_vvq(_vq);
635
636 virtio_mb(vq->weak_barriers);
637 return (u16)last_used_idx != vq->vring.used->idx;
638}
639EXPORT_SYMBOL_GPL(virtqueue_poll);
640
641/**
Rusty Russell5dfc1762012-01-12 15:44:42 +1030642 * virtqueue_enable_cb - restart callbacks after disable_cb.
643 * @vq: the struct virtqueue we're talking about.
644 *
645 * This re-enables callbacks; it returns "false" if there are pending
646 * buffers in the queue, to detect a possible race between the driver
647 * checking for more work, and enabling callbacks.
648 *
649 * Caller must ensure we don't call this with other virtqueue
650 * operations at the same time (except where noted).
651 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300652bool virtqueue_enable_cb(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000653{
Michael S. Tsirkincc229882013-07-09 13:19:18 +0300654 unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
655 return !virtqueue_poll(_vq, last_used_idx);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000656}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300657EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000658
Rusty Russell5dfc1762012-01-12 15:44:42 +1030659/**
660 * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
661 * @vq: the struct virtqueue we're talking about.
662 *
663 * This re-enables callbacks but hints to the other side to delay
664 * interrupts until most of the available buffers have been processed;
665 * it returns "false" if there are many pending buffers in the queue,
666 * to detect a possible race between the driver checking for more work,
667 * and enabling callbacks.
668 *
669 * Caller must ensure we don't call this with other virtqueue
670 * operations at the same time (except where noted).
671 */
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300672bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
673{
674 struct vring_virtqueue *vq = to_vvq(_vq);
675 u16 bufs;
676
677 START_USE(vq);
678
679 /* We optimistically turn back on interrupts, then check if there was
680 * more to do. */
681 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
682 * either clear the flags bit or point the event index at the next
683 * entry. Always do both to keep code simple. */
684 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
685 /* TODO: tune this threshold */
686 bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
687 vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
Rusty Russella9a0fef2013-03-18 13:22:19 +1030688 virtio_mb(vq->weak_barriers);
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300689 if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
690 END_USE(vq);
691 return false;
692 }
693
694 END_USE(vq);
695 return true;
696}
697EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
698
Rusty Russell5dfc1762012-01-12 15:44:42 +1030699/**
700 * virtqueue_detach_unused_buf - detach first unused buffer
701 * @vq: the struct virtqueue we're talking about.
702 *
Rusty Russellb3087e42013-05-20 12:15:44 +0930703 * Returns NULL or the "data" token handed to virtqueue_add_*().
Rusty Russell5dfc1762012-01-12 15:44:42 +1030704 * This is not valid on an active queue; it is useful only for device
705 * shutdown.
706 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300707void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
Shirley Mac021eac2010-01-18 19:15:23 +0530708{
709 struct vring_virtqueue *vq = to_vvq(_vq);
710 unsigned int i;
711 void *buf;
712
713 START_USE(vq);
714
715 for (i = 0; i < vq->vring.num; i++) {
716 if (!vq->data[i])
717 continue;
718 /* detach_buf clears data, so grab it now. */
719 buf = vq->data[i];
720 detach_buf(vq, i);
Amit Shahb3258ff2011-03-16 19:12:10 +0530721 vq->vring.avail->idx--;
Shirley Mac021eac2010-01-18 19:15:23 +0530722 END_USE(vq);
723 return buf;
724 }
725 /* That should have freed everything. */
Rusty Russell06ca2872012-10-16 23:56:14 +1030726 BUG_ON(vq->vq.num_free != vq->vring.num);
Shirley Mac021eac2010-01-18 19:15:23 +0530727
728 END_USE(vq);
729 return NULL;
730}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300731EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
Shirley Mac021eac2010-01-18 19:15:23 +0530732
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000733irqreturn_t vring_interrupt(int irq, void *_vq)
734{
735 struct vring_virtqueue *vq = to_vvq(_vq);
736
737 if (!more_used(vq)) {
738 pr_debug("virtqueue interrupt with no work for %p\n", vq);
739 return IRQ_NONE;
740 }
741
742 if (unlikely(vq->broken))
743 return IRQ_HANDLED;
744
745 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
Rusty Russell18445c42008-02-04 23:49:57 -0500746 if (vq->vq.callback)
747 vq->vq.callback(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000748
749 return IRQ_HANDLED;
750}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500751EXPORT_SYMBOL_GPL(vring_interrupt);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000752
Jason Wang17bb6d42012-08-28 13:54:13 +0200753struct virtqueue *vring_new_virtqueue(unsigned int index,
754 unsigned int num,
Rusty Russell87c7d572008-12-30 09:26:03 -0600755 unsigned int vring_align,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000756 struct virtio_device *vdev,
Rusty Russell7b21e342012-01-12 15:44:42 +1030757 bool weak_barriers,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000758 void *pages,
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030759 bool (*notify)(struct virtqueue *),
Rusty Russell9499f5e2009-06-12 22:16:35 -0600760 void (*callback)(struct virtqueue *),
761 const char *name)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000762{
763 struct vring_virtqueue *vq;
764 unsigned int i;
765
Rusty Russell42b36cc2007-11-12 13:39:18 +1100766 /* We assume num is a power of 2. */
767 if (num & (num - 1)) {
768 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
769 return NULL;
770 }
771
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000772 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
773 if (!vq)
774 return NULL;
775
Rusty Russell87c7d572008-12-30 09:26:03 -0600776 vring_init(&vq->vring, num, pages, vring_align);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000777 vq->vq.callback = callback;
778 vq->vq.vdev = vdev;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600779 vq->vq.name = name;
Rusty Russell06ca2872012-10-16 23:56:14 +1030780 vq->vq.num_free = num;
781 vq->vq.index = index;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000782 vq->notify = notify;
Rusty Russell7b21e342012-01-12 15:44:42 +1030783 vq->weak_barriers = weak_barriers;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000784 vq->broken = false;
785 vq->last_used_idx = 0;
786 vq->num_added = 0;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600787 list_add_tail(&vq->vq.list, &vdev->vqs);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000788#ifdef DEBUG
789 vq->in_use = false;
Rusty Russelle93300b2012-01-12 15:44:43 +1030790 vq->last_add_time_valid = false;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000791#endif
792
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100793 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300794 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100795
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000796 /* No callback? Tell other side not to bother us. */
797 if (!callback)
798 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
799
800 /* Put everything in free lists. */
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000801 vq->free_head = 0;
Amit Shah3b870622010-02-12 10:32:14 +0530802 for (i = 0; i < num-1; i++) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000803 vq->vring.desc[i].next = i+1;
Amit Shah3b870622010-02-12 10:32:14 +0530804 vq->data[i] = NULL;
805 }
806 vq->data[i] = NULL;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000807
808 return &vq->vq;
809}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500810EXPORT_SYMBOL_GPL(vring_new_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000811
812void vring_del_virtqueue(struct virtqueue *vq)
813{
Rusty Russell9499f5e2009-06-12 22:16:35 -0600814 list_del(&vq->list);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000815 kfree(to_vvq(vq));
816}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500817EXPORT_SYMBOL_GPL(vring_del_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000818
Rusty Russelle34f8722008-07-25 12:06:13 -0500819/* Manipulates transport-specific feature bits. */
820void vring_transport_features(struct virtio_device *vdev)
821{
822 unsigned int i;
823
824 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
825 switch (i) {
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100826 case VIRTIO_RING_F_INDIRECT_DESC:
827 break;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300828 case VIRTIO_RING_F_EVENT_IDX:
829 break;
Rusty Russelle34f8722008-07-25 12:06:13 -0500830 default:
831 /* We don't understand this bit. */
832 clear_bit(i, vdev->features);
833 }
834 }
835}
836EXPORT_SYMBOL_GPL(vring_transport_features);
837
Rusty Russell5dfc1762012-01-12 15:44:42 +1030838/**
839 * virtqueue_get_vring_size - return the size of the virtqueue's vring
840 * @vq: the struct virtqueue containing the vring of interest.
841 *
842 * Returns the size of the vring. This is mainly used for boasting to
843 * userspace. Unlike other operations, this need not be serialized.
844 */
Rick Jones8f9f4662011-10-19 08:10:59 +0000845unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
846{
847
848 struct vring_virtqueue *vq = to_vvq(_vq);
849
850 return vq->vring.num;
851}
852EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
853
Heinz Graalfsb3b32c92013-10-29 09:40:19 +1030854bool virtqueue_is_broken(struct virtqueue *_vq)
855{
856 struct vring_virtqueue *vq = to_vvq(_vq);
857
858 return vq->broken;
859}
860EXPORT_SYMBOL_GPL(virtqueue_is_broken);
861
Rusty Russellc6fd4702008-02-04 23:50:05 -0500862MODULE_LICENSE("GPL");