blob: 7ae3cba2f6247c49f810c9411a7785e514a4fc25 [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>
Joel Stanley6abb2dd2014-02-13 15:03:46 +103026#include <linux/kmemleak.h>
Rusty Russell0a8a69d2007-10-22 11:03:40 +100027
28#ifdef DEBUG
29/* For development, we want to crash whenever the ring is screwed. */
Rusty Russell9499f5e2009-06-12 22:16:35 -060030#define BAD_RING(_vq, fmt, args...) \
31 do { \
32 dev_err(&(_vq)->vq.vdev->dev, \
33 "%s:"fmt, (_vq)->vq.name, ##args); \
34 BUG(); \
35 } while (0)
Rusty Russellc5f841f2009-03-30 21:55:22 -060036/* Caller is supposed to guarantee no reentry. */
37#define START_USE(_vq) \
38 do { \
39 if ((_vq)->in_use) \
Rusty Russell9499f5e2009-06-12 22:16:35 -060040 panic("%s:in_use = %i\n", \
41 (_vq)->vq.name, (_vq)->in_use); \
Rusty Russellc5f841f2009-03-30 21:55:22 -060042 (_vq)->in_use = __LINE__; \
Rusty Russell9499f5e2009-06-12 22:16:35 -060043 } while (0)
Roel Kluin3a35ce72009-01-22 16:42:57 +010044#define END_USE(_vq) \
Rusty Russell97a545a2010-02-24 14:22:22 -060045 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100046#else
Rusty Russell9499f5e2009-06-12 22:16:35 -060047#define BAD_RING(_vq, fmt, args...) \
48 do { \
49 dev_err(&_vq->vq.vdev->dev, \
50 "%s:"fmt, (_vq)->vq.name, ##args); \
51 (_vq)->broken = true; \
52 } while (0)
Rusty Russell0a8a69d2007-10-22 11:03:40 +100053#define START_USE(vq)
54#define END_USE(vq)
55#endif
56
57struct vring_virtqueue
58{
59 struct virtqueue vq;
60
61 /* Actual memory layout for this queue */
62 struct vring vring;
63
Rusty Russell7b21e342012-01-12 15:44:42 +103064 /* Can we use weak barriers? */
65 bool weak_barriers;
66
Rusty Russell0a8a69d2007-10-22 11:03:40 +100067 /* Other side has made a mess, don't try any more. */
68 bool broken;
69
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +010070 /* Host supports indirect buffers */
71 bool indirect;
72
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +030073 /* Host publishes avail event idx */
74 bool event;
75
Rusty Russell0a8a69d2007-10-22 11:03:40 +100076 /* Head of free buffer list. */
77 unsigned int free_head;
78 /* Number we've added since last sync. */
79 unsigned int num_added;
80
81 /* Last used index we've seen. */
Anthony Liguori1bc49532007-11-07 15:49:24 -060082 u16 last_used_idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +100083
84 /* How to notify other side. FIXME: commonalize hcalls! */
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +103085 bool (*notify)(struct virtqueue *vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +100086
87#ifdef DEBUG
88 /* They're supposed to lock for us. */
89 unsigned int in_use;
Rusty Russelle93300b2012-01-12 15:44:43 +103090
91 /* Figure out if their kicks are too delayed. */
92 bool last_add_time_valid;
93 ktime_t last_add_time;
Rusty Russell0a8a69d2007-10-22 11:03:40 +100094#endif
95
96 /* Tokens for callbacks. */
97 void *data[];
98};
99
100#define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
101
Rusty Russell13816c72013-03-20 15:37:09 +1030102static inline struct scatterlist *sg_next_chained(struct scatterlist *sg,
103 unsigned int *count)
104{
105 return sg_next(sg);
106}
107
108static inline struct scatterlist *sg_next_arr(struct scatterlist *sg,
109 unsigned int *count)
110{
111 if (--(*count) == 0)
112 return NULL;
113 return sg + 1;
114}
115
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100116/* Set up an indirect table of descriptors and add it to the queue. */
Rusty Russell13816c72013-03-20 15:37:09 +1030117static inline int vring_add_indirect(struct vring_virtqueue *vq,
118 struct scatterlist *sgs[],
119 struct scatterlist *(*next)
120 (struct scatterlist *, unsigned int *),
121 unsigned int total_sg,
122 unsigned int total_out,
123 unsigned int total_in,
124 unsigned int out_sgs,
125 unsigned int in_sgs,
126 gfp_t gfp)
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100127{
128 struct vring_desc *desc;
129 unsigned head;
Rusty Russell13816c72013-03-20 15:37:09 +1030130 struct scatterlist *sg;
131 int i, n;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100132
Will Deaconb92b1b82012-10-19 14:03:33 +0100133 /*
134 * We require lowmem mappings for the descriptors because
135 * otherwise virt_to_phys will give us bogus addresses in the
136 * virtqueue.
137 */
138 gfp &= ~(__GFP_HIGHMEM | __GFP_HIGH);
139
Rusty Russell13816c72013-03-20 15:37:09 +1030140 desc = kmalloc(total_sg * sizeof(struct vring_desc), gfp);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100141 if (!desc)
Michael S. Tsirkin686d3632010-06-10 18:16:11 +0300142 return -ENOMEM;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100143
Rusty Russell13816c72013-03-20 15:37:09 +1030144 /* Transfer entries from the sg lists into the indirect page */
145 i = 0;
146 for (n = 0; n < out_sgs; n++) {
147 for (sg = sgs[n]; sg; sg = next(sg, &total_out)) {
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 i++;
153 }
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100154 }
Rusty Russell13816c72013-03-20 15:37:09 +1030155 for (; n < (out_sgs + in_sgs); n++) {
156 for (sg = sgs[n]; sg; sg = next(sg, &total_in)) {
157 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
158 desc[i].addr = sg_phys(sg);
159 desc[i].len = sg->length;
160 desc[i].next = i+1;
161 i++;
162 }
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100163 }
Rusty Russell13816c72013-03-20 15:37:09 +1030164 BUG_ON(i != total_sg);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100165
166 /* Last one doesn't continue. */
167 desc[i-1].flags &= ~VRING_DESC_F_NEXT;
168 desc[i-1].next = 0;
169
170 /* We're about to use a buffer */
Rusty Russell06ca2872012-10-16 23:56:14 +1030171 vq->vq.num_free--;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100172
173 /* Use a single buffer which doesn't continue */
174 head = vq->free_head;
175 vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
176 vq->vring.desc[head].addr = virt_to_phys(desc);
Rusty Russellbb478d82013-10-14 18:08:45 +1030177 /* kmemleak gives a false positive, as it's hidden by virt_to_phys */
178 kmemleak_ignore(desc);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100179 vq->vring.desc[head].len = i * sizeof(struct vring_desc);
180
181 /* Update free pointer */
182 vq->free_head = vq->vring.desc[head].next;
183
184 return head;
185}
186
Rusty Russell13816c72013-03-20 15:37:09 +1030187static inline int virtqueue_add(struct virtqueue *_vq,
188 struct scatterlist *sgs[],
189 struct scatterlist *(*next)
190 (struct scatterlist *, unsigned int *),
191 unsigned int total_out,
192 unsigned int total_in,
193 unsigned int out_sgs,
194 unsigned int in_sgs,
195 void *data,
196 gfp_t gfp)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000197{
198 struct vring_virtqueue *vq = to_vvq(_vq);
Rusty Russell13816c72013-03-20 15:37:09 +1030199 struct scatterlist *sg;
200 unsigned int i, n, avail, uninitialized_var(prev), total_sg;
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930201 int head;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000202
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100203 START_USE(vq);
204
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000205 BUG_ON(data == NULL);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100206
Rusty Russelle93300b2012-01-12 15:44:43 +1030207#ifdef DEBUG
208 {
209 ktime_t now = ktime_get();
210
211 /* No kick or get, with .1 second between? Warn. */
212 if (vq->last_add_time_valid)
213 WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time))
214 > 100);
215 vq->last_add_time = now;
216 vq->last_add_time_valid = true;
217 }
218#endif
219
Rusty Russell13816c72013-03-20 15:37:09 +1030220 total_sg = total_in + total_out;
221
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100222 /* If the host supports indirect descriptor tables, and we have multiple
223 * buffers, then go indirect. FIXME: tune this threshold */
Rusty Russell13816c72013-03-20 15:37:09 +1030224 if (vq->indirect && total_sg > 1 && vq->vq.num_free) {
225 head = vring_add_indirect(vq, sgs, next, total_sg, total_out,
226 total_in,
227 out_sgs, in_sgs, gfp);
Michael S. Tsirkin1fe9b6f2010-07-26 16:55:30 +0930228 if (likely(head >= 0))
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100229 goto add_head;
230 }
231
Rusty Russell13816c72013-03-20 15:37:09 +1030232 BUG_ON(total_sg > vq->vring.num);
233 BUG_ON(total_sg == 0);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000234
Rusty Russell13816c72013-03-20 15:37:09 +1030235 if (vq->vq.num_free < total_sg) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000236 pr_debug("Can't add buf len %i - avail = %i\n",
Rusty Russell13816c72013-03-20 15:37:09 +1030237 total_sg, vq->vq.num_free);
Rusty Russell44653ea2008-07-25 12:06:04 -0500238 /* FIXME: for historical reasons, we force a notify here if
239 * there are outgoing parts to the buffer. Presumably the
240 * host should service the ring ASAP. */
Rusty Russell13816c72013-03-20 15:37:09 +1030241 if (out_sgs)
Rusty Russell44653ea2008-07-25 12:06:04 -0500242 vq->notify(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000243 END_USE(vq);
244 return -ENOSPC;
245 }
246
247 /* We're about to use some buffers from the free list. */
Rusty Russell13816c72013-03-20 15:37:09 +1030248 vq->vq.num_free -= total_sg;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000249
Rusty Russell13816c72013-03-20 15:37:09 +1030250 head = i = vq->free_head;
251 for (n = 0; n < out_sgs; n++) {
252 for (sg = sgs[n]; sg; sg = next(sg, &total_out)) {
253 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
254 vq->vring.desc[i].addr = sg_phys(sg);
255 vq->vring.desc[i].len = sg->length;
256 prev = i;
257 i = vq->vring.desc[i].next;
258 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000259 }
Rusty Russell13816c72013-03-20 15:37:09 +1030260 for (; n < (out_sgs + in_sgs); n++) {
261 for (sg = sgs[n]; sg; sg = next(sg, &total_in)) {
262 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
263 vq->vring.desc[i].addr = sg_phys(sg);
264 vq->vring.desc[i].len = sg->length;
265 prev = i;
266 i = vq->vring.desc[i].next;
267 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000268 }
269 /* Last one doesn't continue. */
270 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
271
272 /* Update free pointer */
273 vq->free_head = i;
274
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100275add_head:
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000276 /* Set token. */
277 vq->data[head] = data;
278
279 /* Put entry in available array (but don't update avail->idx until they
Rusty Russell3b720b82012-01-12 15:44:43 +1030280 * do sync). */
Rusty Russellee7cd892012-01-12 15:44:43 +1030281 avail = (vq->vring.avail->idx & (vq->vring.num-1));
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000282 vq->vring.avail->ring[avail] = head;
283
Rusty Russellee7cd892012-01-12 15:44:43 +1030284 /* Descriptors and available array need to be set before we expose the
285 * new available array entries. */
Rusty Russella9a0fef2013-03-18 13:22:19 +1030286 virtio_wmb(vq->weak_barriers);
Rusty Russellee7cd892012-01-12 15:44:43 +1030287 vq->vring.avail->idx++;
288 vq->num_added++;
289
290 /* This is very unlikely, but theoretically possible. Kick
291 * just in case. */
292 if (unlikely(vq->num_added == (1 << 16) - 1))
293 virtqueue_kick(_vq);
294
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000295 pr_debug("Added buffer head %i to %p\n", head, vq);
296 END_USE(vq);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600297
Rusty Russell98e8c6b2012-10-16 23:56:15 +1030298 return 0;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000299}
Rusty Russell13816c72013-03-20 15:37:09 +1030300
301/**
Rusty Russell13816c72013-03-20 15:37:09 +1030302 * virtqueue_add_sgs - expose buffers to other end
303 * @vq: the struct virtqueue we're talking about.
304 * @sgs: array of terminated scatterlists.
305 * @out_num: the number of scatterlists readable by other side
306 * @in_num: the number of scatterlists which are writable (after readable ones)
307 * @data: the token identifying the buffer.
308 * @gfp: how to do memory allocations (if necessary).
309 *
310 * Caller must ensure we don't call this with other virtqueue operations
311 * at the same time (except where noted).
312 *
313 * Returns zero or a negative error (ie. ENOSPC, ENOMEM).
314 */
315int virtqueue_add_sgs(struct virtqueue *_vq,
316 struct scatterlist *sgs[],
317 unsigned int out_sgs,
318 unsigned int in_sgs,
319 void *data,
320 gfp_t gfp)
321{
322 unsigned int i, total_out, total_in;
323
324 /* Count them first. */
325 for (i = total_out = total_in = 0; i < out_sgs; i++) {
326 struct scatterlist *sg;
327 for (sg = sgs[i]; sg; sg = sg_next(sg))
328 total_out++;
329 }
330 for (; i < out_sgs + in_sgs; i++) {
331 struct scatterlist *sg;
332 for (sg = sgs[i]; sg; sg = sg_next(sg))
333 total_in++;
334 }
335 return virtqueue_add(_vq, sgs, sg_next_chained,
336 total_out, total_in, out_sgs, in_sgs, data, gfp);
337}
338EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
339
340/**
Rusty Russell282edb32013-03-20 15:44:26 +1030341 * virtqueue_add_outbuf - expose output buffers to other end
342 * @vq: the struct virtqueue we're talking about.
343 * @sgs: array of scatterlists (need not be terminated!)
344 * @num: the number of scatterlists readable by other side
345 * @data: the token identifying the buffer.
346 * @gfp: how to do memory allocations (if necessary).
347 *
348 * Caller must ensure we don't call this with other virtqueue operations
349 * at the same time (except where noted).
350 *
351 * Returns zero or a negative error (ie. ENOSPC, ENOMEM).
352 */
353int virtqueue_add_outbuf(struct virtqueue *vq,
354 struct scatterlist sg[], unsigned int num,
355 void *data,
356 gfp_t gfp)
357{
358 return virtqueue_add(vq, &sg, sg_next_arr, num, 0, 1, 0, data, gfp);
359}
360EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
361
362/**
363 * virtqueue_add_inbuf - expose input buffers to other end
364 * @vq: the struct virtqueue we're talking about.
365 * @sgs: array of scatterlists (need not be terminated!)
366 * @num: the number of scatterlists writable by other side
367 * @data: the token identifying the buffer.
368 * @gfp: how to do memory allocations (if necessary).
369 *
370 * Caller must ensure we don't call this with other virtqueue operations
371 * at the same time (except where noted).
372 *
373 * Returns zero or a negative error (ie. ENOSPC, ENOMEM).
374 */
375int virtqueue_add_inbuf(struct virtqueue *vq,
376 struct scatterlist sg[], unsigned int num,
377 void *data,
378 gfp_t gfp)
379{
380 return virtqueue_add(vq, &sg, sg_next_arr, 0, num, 0, 1, data, gfp);
381}
382EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
383
384/**
Rusty Russell41f03772012-01-12 15:44:43 +1030385 * virtqueue_kick_prepare - first half of split virtqueue_kick call.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030386 * @vq: the struct virtqueue
387 *
Rusty Russell41f03772012-01-12 15:44:43 +1030388 * Instead of virtqueue_kick(), you can do:
389 * if (virtqueue_kick_prepare(vq))
390 * virtqueue_notify(vq);
Rusty Russell5dfc1762012-01-12 15:44:42 +1030391 *
Rusty Russell41f03772012-01-12 15:44:43 +1030392 * This is sometimes useful because the virtqueue_kick_prepare() needs
393 * to be serialized, but the actual virtqueue_notify() call does not.
Rusty Russell5dfc1762012-01-12 15:44:42 +1030394 */
Rusty Russell41f03772012-01-12 15:44:43 +1030395bool virtqueue_kick_prepare(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000396{
397 struct vring_virtqueue *vq = to_vvq(_vq);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300398 u16 new, old;
Rusty Russell41f03772012-01-12 15:44:43 +1030399 bool needs_kick;
400
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000401 START_USE(vq);
Jason Wanga72caae2012-01-20 16:17:08 +0800402 /* We need to expose available array entries before checking avail
403 * event. */
Rusty Russella9a0fef2013-03-18 13:22:19 +1030404 virtio_mb(vq->weak_barriers);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000405
Rusty Russellee7cd892012-01-12 15:44:43 +1030406 old = vq->vring.avail->idx - vq->num_added;
407 new = vq->vring.avail->idx;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000408 vq->num_added = 0;
409
Rusty Russelle93300b2012-01-12 15:44:43 +1030410#ifdef DEBUG
411 if (vq->last_add_time_valid) {
412 WARN_ON(ktime_to_ms(ktime_sub(ktime_get(),
413 vq->last_add_time)) > 100);
414 }
415 vq->last_add_time_valid = false;
416#endif
417
Rusty Russell41f03772012-01-12 15:44:43 +1030418 if (vq->event) {
419 needs_kick = vring_need_event(vring_avail_event(&vq->vring),
420 new, old);
421 } else {
422 needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
423 }
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000424 END_USE(vq);
Rusty Russell41f03772012-01-12 15:44:43 +1030425 return needs_kick;
426}
427EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
428
429/**
430 * virtqueue_notify - second half of split virtqueue_kick call.
431 * @vq: the struct virtqueue
432 *
433 * This does not need to be serialized.
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030434 *
435 * Returns false if host notify failed or queue is broken, otherwise true.
Rusty Russell41f03772012-01-12 15:44:43 +1030436 */
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030437bool virtqueue_notify(struct virtqueue *_vq)
Rusty Russell41f03772012-01-12 15:44:43 +1030438{
439 struct vring_virtqueue *vq = to_vvq(_vq);
440
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030441 if (unlikely(vq->broken))
442 return false;
443
Rusty Russell41f03772012-01-12 15:44:43 +1030444 /* Prod other side to tell it about changes. */
Heinz Graalfs2342d6a2013-11-05 21:20:27 +1030445 if (!vq->notify(_vq)) {
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030446 vq->broken = true;
447 return false;
448 }
449 return true;
Rusty Russell41f03772012-01-12 15:44:43 +1030450}
451EXPORT_SYMBOL_GPL(virtqueue_notify);
452
453/**
454 * virtqueue_kick - update after add_buf
455 * @vq: the struct virtqueue
456 *
Rusty Russellb3087e42013-05-20 12:15:44 +0930457 * After one or more virtqueue_add_* calls, invoke this to kick
Rusty Russell41f03772012-01-12 15:44:43 +1030458 * the other side.
459 *
460 * Caller must ensure we don't call this with other virtqueue
461 * operations at the same time (except where noted).
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030462 *
463 * Returns false if kick failed, otherwise true.
Rusty Russell41f03772012-01-12 15:44:43 +1030464 */
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030465bool virtqueue_kick(struct virtqueue *vq)
Rusty Russell41f03772012-01-12 15:44:43 +1030466{
467 if (virtqueue_kick_prepare(vq))
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +1030468 return virtqueue_notify(vq);
469 return true;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000470}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300471EXPORT_SYMBOL_GPL(virtqueue_kick);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000472
473static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
474{
475 unsigned int i;
476
477 /* Clear data ptr. */
478 vq->data[head] = NULL;
479
480 /* Put back on free list: find end */
481 i = head;
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100482
483 /* Free the indirect table */
484 if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
485 kfree(phys_to_virt(vq->vring.desc[i].addr));
486
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000487 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
488 i = vq->vring.desc[i].next;
Rusty Russell06ca2872012-10-16 23:56:14 +1030489 vq->vq.num_free++;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000490 }
491
492 vq->vring.desc[i].next = vq->free_head;
493 vq->free_head = head;
494 /* Plus final descriptor */
Rusty Russell06ca2872012-10-16 23:56:14 +1030495 vq->vq.num_free++;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000496}
497
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000498static inline bool more_used(const struct vring_virtqueue *vq)
499{
500 return vq->last_used_idx != vq->vring.used->idx;
501}
502
Rusty Russell5dfc1762012-01-12 15:44:42 +1030503/**
504 * virtqueue_get_buf - get the next used buffer
505 * @vq: the struct virtqueue we're talking about.
506 * @len: the length written into the buffer
507 *
508 * If the driver wrote data into the buffer, @len will be set to the
509 * amount written. This means you don't need to clear the buffer
510 * beforehand to ensure there's no data leakage in the case of short
511 * writes.
512 *
513 * Caller must ensure we don't call this with other virtqueue
514 * operations at the same time (except where noted).
515 *
516 * Returns NULL if there are no used buffers, or the "data" token
Rusty Russellb3087e42013-05-20 12:15:44 +0930517 * handed to virtqueue_add_*().
Rusty Russell5dfc1762012-01-12 15:44:42 +1030518 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300519void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000520{
521 struct vring_virtqueue *vq = to_vvq(_vq);
522 void *ret;
523 unsigned int i;
Rusty Russell3b720b82012-01-12 15:44:43 +1030524 u16 last_used;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000525
526 START_USE(vq);
527
Rusty Russell5ef82752008-05-02 21:50:43 -0500528 if (unlikely(vq->broken)) {
529 END_USE(vq);
530 return NULL;
531 }
532
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000533 if (!more_used(vq)) {
534 pr_debug("No more buffers in queue\n");
535 END_USE(vq);
536 return NULL;
537 }
538
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200539 /* Only get used array entries after they have been exposed by host. */
Rusty Russella9a0fef2013-03-18 13:22:19 +1030540 virtio_rmb(vq->weak_barriers);
Michael S. Tsirkin2d61ba92009-10-25 15:28:53 +0200541
Rusty Russell3b720b82012-01-12 15:44:43 +1030542 last_used = (vq->last_used_idx & (vq->vring.num - 1));
543 i = vq->vring.used->ring[last_used].id;
544 *len = vq->vring.used->ring[last_used].len;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000545
546 if (unlikely(i >= vq->vring.num)) {
547 BAD_RING(vq, "id %u out of range\n", i);
548 return NULL;
549 }
550 if (unlikely(!vq->data[i])) {
551 BAD_RING(vq, "id %u is not a head!\n", i);
552 return NULL;
553 }
554
555 /* detach_buf clears data, so grab it now. */
556 ret = vq->data[i];
557 detach_buf(vq, i);
558 vq->last_used_idx++;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300559 /* If we expect an interrupt for the next entry, tell host
560 * by writing event index and flush out the write before
561 * the read in the next get_buf call. */
562 if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
563 vring_used_event(&vq->vring) = vq->last_used_idx;
Rusty Russella9a0fef2013-03-18 13:22:19 +1030564 virtio_mb(vq->weak_barriers);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300565 }
566
Rusty Russelle93300b2012-01-12 15:44:43 +1030567#ifdef DEBUG
568 vq->last_add_time_valid = false;
569#endif
570
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000571 END_USE(vq);
572 return ret;
573}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300574EXPORT_SYMBOL_GPL(virtqueue_get_buf);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000575
Rusty Russell5dfc1762012-01-12 15:44:42 +1030576/**
577 * virtqueue_disable_cb - disable callbacks
578 * @vq: the struct virtqueue we're talking about.
579 *
580 * Note that this is not necessarily synchronous, hence unreliable and only
581 * useful as an optimization.
582 *
583 * Unlike other operations, this need not be serialized.
584 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300585void virtqueue_disable_cb(struct virtqueue *_vq)
Rusty Russell18445c42008-02-04 23:49:57 -0500586{
587 struct vring_virtqueue *vq = to_vvq(_vq);
588
Rusty Russell18445c42008-02-04 23:49:57 -0500589 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
Rusty Russell18445c42008-02-04 23:49:57 -0500590}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300591EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
Rusty Russell18445c42008-02-04 23:49:57 -0500592
Rusty Russell5dfc1762012-01-12 15:44:42 +1030593/**
Michael S. Tsirkincc229882013-07-09 13:19:18 +0300594 * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
595 * @vq: the struct virtqueue we're talking about.
596 *
597 * This re-enables callbacks; it returns current queue state
598 * in an opaque unsigned value. This value should be later tested by
599 * virtqueue_poll, to detect a possible race between the driver checking for
600 * more work, and enabling callbacks.
601 *
602 * Caller must ensure we don't call this with other virtqueue
603 * operations at the same time (except where noted).
604 */
605unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
606{
607 struct vring_virtqueue *vq = to_vvq(_vq);
608 u16 last_used_idx;
609
610 START_USE(vq);
611
612 /* We optimistically turn back on interrupts, then check if there was
613 * more to do. */
614 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
615 * either clear the flags bit or point the event index at the next
616 * entry. Always do both to keep code simple. */
617 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
618 vring_used_event(&vq->vring) = last_used_idx = vq->last_used_idx;
619 END_USE(vq);
620 return last_used_idx;
621}
622EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
623
624/**
625 * virtqueue_poll - query pending used buffers
626 * @vq: the struct virtqueue we're talking about.
627 * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
628 *
629 * Returns "true" if there are pending used buffers in the queue.
630 *
631 * This does not need to be serialized.
632 */
633bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
634{
635 struct vring_virtqueue *vq = to_vvq(_vq);
636
637 virtio_mb(vq->weak_barriers);
638 return (u16)last_used_idx != vq->vring.used->idx;
639}
640EXPORT_SYMBOL_GPL(virtqueue_poll);
641
642/**
Rusty Russell5dfc1762012-01-12 15:44:42 +1030643 * virtqueue_enable_cb - restart callbacks after disable_cb.
644 * @vq: the struct virtqueue we're talking about.
645 *
646 * This re-enables callbacks; it returns "false" if there are pending
647 * buffers in the queue, to detect a possible race between the driver
648 * checking for more work, and enabling callbacks.
649 *
650 * Caller must ensure we don't call this with other virtqueue
651 * operations at the same time (except where noted).
652 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300653bool virtqueue_enable_cb(struct virtqueue *_vq)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000654{
Michael S. Tsirkincc229882013-07-09 13:19:18 +0300655 unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
656 return !virtqueue_poll(_vq, last_used_idx);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000657}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300658EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000659
Rusty Russell5dfc1762012-01-12 15:44:42 +1030660/**
661 * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
662 * @vq: the struct virtqueue we're talking about.
663 *
664 * This re-enables callbacks but hints to the other side to delay
665 * interrupts until most of the available buffers have been processed;
666 * it returns "false" if there are many pending buffers in the queue,
667 * to detect a possible race between the driver checking for more work,
668 * and enabling callbacks.
669 *
670 * Caller must ensure we don't call this with other virtqueue
671 * operations at the same time (except where noted).
672 */
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300673bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
674{
675 struct vring_virtqueue *vq = to_vvq(_vq);
676 u16 bufs;
677
678 START_USE(vq);
679
680 /* We optimistically turn back on interrupts, then check if there was
681 * more to do. */
682 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
683 * either clear the flags bit or point the event index at the next
684 * entry. Always do both to keep code simple. */
685 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
686 /* TODO: tune this threshold */
687 bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
688 vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
Rusty Russella9a0fef2013-03-18 13:22:19 +1030689 virtio_mb(vq->weak_barriers);
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +0300690 if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
691 END_USE(vq);
692 return false;
693 }
694
695 END_USE(vq);
696 return true;
697}
698EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
699
Rusty Russell5dfc1762012-01-12 15:44:42 +1030700/**
701 * virtqueue_detach_unused_buf - detach first unused buffer
702 * @vq: the struct virtqueue we're talking about.
703 *
Rusty Russellb3087e42013-05-20 12:15:44 +0930704 * Returns NULL or the "data" token handed to virtqueue_add_*().
Rusty Russell5dfc1762012-01-12 15:44:42 +1030705 * This is not valid on an active queue; it is useful only for device
706 * shutdown.
707 */
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300708void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
Shirley Mac021eac2010-01-18 19:15:23 +0530709{
710 struct vring_virtqueue *vq = to_vvq(_vq);
711 unsigned int i;
712 void *buf;
713
714 START_USE(vq);
715
716 for (i = 0; i < vq->vring.num; i++) {
717 if (!vq->data[i])
718 continue;
719 /* detach_buf clears data, so grab it now. */
720 buf = vq->data[i];
721 detach_buf(vq, i);
Amit Shahb3258ff2011-03-16 19:12:10 +0530722 vq->vring.avail->idx--;
Shirley Mac021eac2010-01-18 19:15:23 +0530723 END_USE(vq);
724 return buf;
725 }
726 /* That should have freed everything. */
Rusty Russell06ca2872012-10-16 23:56:14 +1030727 BUG_ON(vq->vq.num_free != vq->vring.num);
Shirley Mac021eac2010-01-18 19:15:23 +0530728
729 END_USE(vq);
730 return NULL;
731}
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +0300732EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
Shirley Mac021eac2010-01-18 19:15:23 +0530733
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000734irqreturn_t vring_interrupt(int irq, void *_vq)
735{
736 struct vring_virtqueue *vq = to_vvq(_vq);
737
738 if (!more_used(vq)) {
739 pr_debug("virtqueue interrupt with no work for %p\n", vq);
740 return IRQ_NONE;
741 }
742
743 if (unlikely(vq->broken))
744 return IRQ_HANDLED;
745
746 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
Rusty Russell18445c42008-02-04 23:49:57 -0500747 if (vq->vq.callback)
748 vq->vq.callback(&vq->vq);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000749
750 return IRQ_HANDLED;
751}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500752EXPORT_SYMBOL_GPL(vring_interrupt);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000753
Jason Wang17bb6d42012-08-28 13:54:13 +0200754struct virtqueue *vring_new_virtqueue(unsigned int index,
755 unsigned int num,
Rusty Russell87c7d572008-12-30 09:26:03 -0600756 unsigned int vring_align,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000757 struct virtio_device *vdev,
Rusty Russell7b21e342012-01-12 15:44:42 +1030758 bool weak_barriers,
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000759 void *pages,
Heinz Graalfs46f9c2b2013-10-29 09:38:50 +1030760 bool (*notify)(struct virtqueue *),
Rusty Russell9499f5e2009-06-12 22:16:35 -0600761 void (*callback)(struct virtqueue *),
762 const char *name)
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000763{
764 struct vring_virtqueue *vq;
765 unsigned int i;
766
Rusty Russell42b36cc2007-11-12 13:39:18 +1100767 /* We assume num is a power of 2. */
768 if (num & (num - 1)) {
769 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
770 return NULL;
771 }
772
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000773 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
774 if (!vq)
775 return NULL;
776
Rusty Russell87c7d572008-12-30 09:26:03 -0600777 vring_init(&vq->vring, num, pages, vring_align);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000778 vq->vq.callback = callback;
779 vq->vq.vdev = vdev;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600780 vq->vq.name = name;
Rusty Russell06ca2872012-10-16 23:56:14 +1030781 vq->vq.num_free = num;
782 vq->vq.index = index;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000783 vq->notify = notify;
Rusty Russell7b21e342012-01-12 15:44:42 +1030784 vq->weak_barriers = weak_barriers;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000785 vq->broken = false;
786 vq->last_used_idx = 0;
787 vq->num_added = 0;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600788 list_add_tail(&vq->vq.list, &vdev->vqs);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000789#ifdef DEBUG
790 vq->in_use = false;
Rusty Russelle93300b2012-01-12 15:44:43 +1030791 vq->last_add_time_valid = false;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000792#endif
793
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100794 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300795 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100796
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000797 /* No callback? Tell other side not to bother us. */
798 if (!callback)
799 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
800
801 /* Put everything in free lists. */
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000802 vq->free_head = 0;
Amit Shah3b870622010-02-12 10:32:14 +0530803 for (i = 0; i < num-1; i++) {
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000804 vq->vring.desc[i].next = i+1;
Amit Shah3b870622010-02-12 10:32:14 +0530805 vq->data[i] = NULL;
806 }
807 vq->data[i] = NULL;
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000808
809 return &vq->vq;
810}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500811EXPORT_SYMBOL_GPL(vring_new_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000812
813void vring_del_virtqueue(struct virtqueue *vq)
814{
Rusty Russell9499f5e2009-06-12 22:16:35 -0600815 list_del(&vq->list);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000816 kfree(to_vvq(vq));
817}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500818EXPORT_SYMBOL_GPL(vring_del_virtqueue);
Rusty Russell0a8a69d2007-10-22 11:03:40 +1000819
Rusty Russelle34f8722008-07-25 12:06:13 -0500820/* Manipulates transport-specific feature bits. */
821void vring_transport_features(struct virtio_device *vdev)
822{
823 unsigned int i;
824
825 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
826 switch (i) {
Mark McLoughlin9fa29b9d2009-05-11 18:11:45 +0100827 case VIRTIO_RING_F_INDIRECT_DESC:
828 break;
Michael S. Tsirkina5c262c2011-05-20 02:10:44 +0300829 case VIRTIO_RING_F_EVENT_IDX:
830 break;
Rusty Russelle34f8722008-07-25 12:06:13 -0500831 default:
832 /* We don't understand this bit. */
833 clear_bit(i, vdev->features);
834 }
835 }
836}
837EXPORT_SYMBOL_GPL(vring_transport_features);
838
Rusty Russell5dfc1762012-01-12 15:44:42 +1030839/**
840 * virtqueue_get_vring_size - return the size of the virtqueue's vring
841 * @vq: the struct virtqueue containing the vring of interest.
842 *
843 * Returns the size of the vring. This is mainly used for boasting to
844 * userspace. Unlike other operations, this need not be serialized.
845 */
Rick Jones8f9f4662011-10-19 08:10:59 +0000846unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
847{
848
849 struct vring_virtqueue *vq = to_vvq(_vq);
850
851 return vq->vring.num;
852}
853EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
854
Heinz Graalfsb3b32c92013-10-29 09:40:19 +1030855bool virtqueue_is_broken(struct virtqueue *_vq)
856{
857 struct vring_virtqueue *vq = to_vvq(_vq);
858
859 return vq->broken;
860}
861EXPORT_SYMBOL_GPL(virtqueue_is_broken);
862
Rusty Russellc6fd4702008-02-04 23:50:05 -0500863MODULE_LICENSE("GPL");