]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/virtio/virtio_ring.c
virtio_ring: change host notification API
[mirror_ubuntu-hirsute-kernel.git] / drivers / virtio / virtio_ring.c
CommitLineData
0a8a69dd
RR
1/* 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>
e34f8725 21#include <linux/virtio_config.h>
0a8a69dd 22#include <linux/device.h>
5a0e3ad6 23#include <linux/slab.h>
b5a2c4f1 24#include <linux/module.h>
e93300b1 25#include <linux/hrtimer.h>
0a8a69dd
RR
26
27#ifdef DEBUG
28/* For development, we want to crash whenever the ring is screwed. */
9499f5e7
RR
29#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)
c5f841f1
RR
35/* Caller is supposed to guarantee no reentry. */
36#define START_USE(_vq) \
37 do { \
38 if ((_vq)->in_use) \
9499f5e7
RR
39 panic("%s:in_use = %i\n", \
40 (_vq)->vq.name, (_vq)->in_use); \
c5f841f1 41 (_vq)->in_use = __LINE__; \
9499f5e7 42 } while (0)
3a35ce7d 43#define END_USE(_vq) \
97a545ab 44 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
0a8a69dd 45#else
9499f5e7
RR
46#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)
0a8a69dd
RR
52#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
7b21e34f
RR
63 /* Can we use weak barriers? */
64 bool weak_barriers;
65
0a8a69dd
RR
66 /* Other side has made a mess, don't try any more. */
67 bool broken;
68
9fa29b9d
MM
69 /* Host supports indirect buffers */
70 bool indirect;
71
a5c262c5
MT
72 /* Host publishes avail event idx */
73 bool event;
74
0a8a69dd
RR
75 /* 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. */
1bc4953e 81 u16 last_used_idx;
0a8a69dd
RR
82
83 /* How to notify other side. FIXME: commonalize hcalls! */
46f9c2b9 84 bool (*notify)(struct virtqueue *vq);
0a8a69dd
RR
85
86#ifdef DEBUG
87 /* They're supposed to lock for us. */
88 unsigned int in_use;
e93300b1
RR
89
90 /* Figure out if their kicks are too delayed. */
91 bool last_add_time_valid;
92 ktime_t last_add_time;
0a8a69dd
RR
93#endif
94
95 /* Tokens for callbacks. */
96 void *data[];
97};
98
99#define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
100
13816c76
RR
101static 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
9fa29b9d 115/* Set up an indirect table of descriptors and add it to the queue. */
13816c76
RR
116static 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)
9fa29b9d
MM
126{
127 struct vring_desc *desc;
128 unsigned head;
13816c76
RR
129 struct scatterlist *sg;
130 int i, n;
9fa29b9d 131
b92b1b89
WD
132 /*
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
13816c76 139 desc = kmalloc(total_sg * sizeof(struct vring_desc), gfp);
9fa29b9d 140 if (!desc)
686d3637 141 return -ENOMEM;
9fa29b9d 142
13816c76
RR
143 /* 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 }
9fa29b9d 153 }
13816c76
RR
154 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 }
9fa29b9d 162 }
13816c76 163 BUG_ON(i != total_sg);
9fa29b9d
MM
164
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 */
06ca287d 170 vq->vq.num_free--;
9fa29b9d
MM
171
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);
bb478d8b
RR
176 /* kmemleak gives a false positive, as it's hidden by virt_to_phys */
177 kmemleak_ignore(desc);
9fa29b9d
MM
178 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
13816c76
RR
186static 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)
0a8a69dd
RR
196{
197 struct vring_virtqueue *vq = to_vvq(_vq);
13816c76
RR
198 struct scatterlist *sg;
199 unsigned int i, n, avail, uninitialized_var(prev), total_sg;
1fe9b6fe 200 int head;
0a8a69dd 201
9fa29b9d
MM
202 START_USE(vq);
203
0a8a69dd 204 BUG_ON(data == NULL);
9fa29b9d 205
e93300b1
RR
206#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
13816c76
RR
219 total_sg = total_in + total_out;
220
9fa29b9d
MM
221 /* If the host supports indirect descriptor tables, and we have multiple
222 * buffers, then go indirect. FIXME: tune this threshold */
13816c76
RR
223 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);
1fe9b6fe 227 if (likely(head >= 0))
9fa29b9d
MM
228 goto add_head;
229 }
230
13816c76
RR
231 BUG_ON(total_sg > vq->vring.num);
232 BUG_ON(total_sg == 0);
0a8a69dd 233
13816c76 234 if (vq->vq.num_free < total_sg) {
0a8a69dd 235 pr_debug("Can't add buf len %i - avail = %i\n",
13816c76 236 total_sg, vq->vq.num_free);
44653eae
RR
237 /* 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. */
13816c76 240 if (out_sgs)
44653eae 241 vq->notify(&vq->vq);
0a8a69dd
RR
242 END_USE(vq);
243 return -ENOSPC;
244 }
245
246 /* We're about to use some buffers from the free list. */
13816c76
RR
247 vq->vq.num_free -= total_sg;
248
249 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 }
0a8a69dd 258 }
13816c76
RR
259 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 }
0a8a69dd
RR
267 }
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
9fa29b9d 274add_head:
0a8a69dd
RR
275 /* Set token. */
276 vq->data[head] = data;
277
278 /* Put entry in available array (but don't update avail->idx until they
3b720b8c 279 * do sync). */
ee7cd898 280 avail = (vq->vring.avail->idx & (vq->vring.num-1));
0a8a69dd
RR
281 vq->vring.avail->ring[avail] = head;
282
ee7cd898
RR
283 /* Descriptors and available array need to be set before we expose the
284 * new available array entries. */
a9a0fef7 285 virtio_wmb(vq->weak_barriers);
ee7cd898
RR
286 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
0a8a69dd
RR
294 pr_debug("Added buffer head %i to %p\n", head, vq);
295 END_USE(vq);
3c1b27d5 296
98e8c6bc 297 return 0;
0a8a69dd 298}
13816c76 299
13816c76
RR
300/**
301 * 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
282edb36
RR
339/**
340 * 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
5dfc1762 383/**
41f0377f 384 * virtqueue_kick_prepare - first half of split virtqueue_kick call.
5dfc1762
RR
385 * @vq: the struct virtqueue
386 *
41f0377f
RR
387 * Instead of virtqueue_kick(), you can do:
388 * if (virtqueue_kick_prepare(vq))
389 * virtqueue_notify(vq);
5dfc1762 390 *
41f0377f
RR
391 * This is sometimes useful because the virtqueue_kick_prepare() needs
392 * to be serialized, but the actual virtqueue_notify() call does not.
5dfc1762 393 */
41f0377f 394bool virtqueue_kick_prepare(struct virtqueue *_vq)
0a8a69dd
RR
395{
396 struct vring_virtqueue *vq = to_vvq(_vq);
a5c262c5 397 u16 new, old;
41f0377f
RR
398 bool needs_kick;
399
0a8a69dd 400 START_USE(vq);
a72caae2
JW
401 /* We need to expose available array entries before checking avail
402 * event. */
a9a0fef7 403 virtio_mb(vq->weak_barriers);
0a8a69dd 404
ee7cd898
RR
405 old = vq->vring.avail->idx - vq->num_added;
406 new = vq->vring.avail->idx;
0a8a69dd
RR
407 vq->num_added = 0;
408
e93300b1
RR
409#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
41f0377f
RR
417 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 }
0a8a69dd 423 END_USE(vq);
41f0377f
RR
424 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.
433 */
434void virtqueue_notify(struct virtqueue *_vq)
435{
436 struct vring_virtqueue *vq = to_vvq(_vq);
437
438 /* Prod other side to tell it about changes. */
439 vq->notify(_vq);
440}
441EXPORT_SYMBOL_GPL(virtqueue_notify);
442
443/**
444 * virtqueue_kick - update after add_buf
445 * @vq: the struct virtqueue
446 *
b3087e48 447 * After one or more virtqueue_add_* calls, invoke this to kick
41f0377f
RR
448 * the other side.
449 *
450 * Caller must ensure we don't call this with other virtqueue
451 * operations at the same time (except where noted).
452 */
453void virtqueue_kick(struct virtqueue *vq)
454{
455 if (virtqueue_kick_prepare(vq))
456 virtqueue_notify(vq);
0a8a69dd 457}
7c5e9ed0 458EXPORT_SYMBOL_GPL(virtqueue_kick);
0a8a69dd
RR
459
460static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
461{
462 unsigned int i;
463
464 /* Clear data ptr. */
465 vq->data[head] = NULL;
466
467 /* Put back on free list: find end */
468 i = head;
9fa29b9d
MM
469
470 /* Free the indirect table */
471 if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
472 kfree(phys_to_virt(vq->vring.desc[i].addr));
473
0a8a69dd
RR
474 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
475 i = vq->vring.desc[i].next;
06ca287d 476 vq->vq.num_free++;
0a8a69dd
RR
477 }
478
479 vq->vring.desc[i].next = vq->free_head;
480 vq->free_head = head;
481 /* Plus final descriptor */
06ca287d 482 vq->vq.num_free++;
0a8a69dd
RR
483}
484
0a8a69dd
RR
485static inline bool more_used(const struct vring_virtqueue *vq)
486{
487 return vq->last_used_idx != vq->vring.used->idx;
488}
489
5dfc1762
RR
490/**
491 * virtqueue_get_buf - get the next used buffer
492 * @vq: the struct virtqueue we're talking about.
493 * @len: the length written into the buffer
494 *
495 * If the driver wrote data into the buffer, @len will be set to the
496 * amount written. This means you don't need to clear the buffer
497 * beforehand to ensure there's no data leakage in the case of short
498 * writes.
499 *
500 * Caller must ensure we don't call this with other virtqueue
501 * operations at the same time (except where noted).
502 *
503 * Returns NULL if there are no used buffers, or the "data" token
b3087e48 504 * handed to virtqueue_add_*().
5dfc1762 505 */
7c5e9ed0 506void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
0a8a69dd
RR
507{
508 struct vring_virtqueue *vq = to_vvq(_vq);
509 void *ret;
510 unsigned int i;
3b720b8c 511 u16 last_used;
0a8a69dd
RR
512
513 START_USE(vq);
514
5ef82752
RR
515 if (unlikely(vq->broken)) {
516 END_USE(vq);
517 return NULL;
518 }
519
0a8a69dd
RR
520 if (!more_used(vq)) {
521 pr_debug("No more buffers in queue\n");
522 END_USE(vq);
523 return NULL;
524 }
525
2d61ba95 526 /* Only get used array entries after they have been exposed by host. */
a9a0fef7 527 virtio_rmb(vq->weak_barriers);
2d61ba95 528
3b720b8c
RR
529 last_used = (vq->last_used_idx & (vq->vring.num - 1));
530 i = vq->vring.used->ring[last_used].id;
531 *len = vq->vring.used->ring[last_used].len;
0a8a69dd
RR
532
533 if (unlikely(i >= vq->vring.num)) {
534 BAD_RING(vq, "id %u out of range\n", i);
535 return NULL;
536 }
537 if (unlikely(!vq->data[i])) {
538 BAD_RING(vq, "id %u is not a head!\n", i);
539 return NULL;
540 }
541
542 /* detach_buf clears data, so grab it now. */
543 ret = vq->data[i];
544 detach_buf(vq, i);
545 vq->last_used_idx++;
a5c262c5
MT
546 /* If we expect an interrupt for the next entry, tell host
547 * by writing event index and flush out the write before
548 * the read in the next get_buf call. */
549 if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
550 vring_used_event(&vq->vring) = vq->last_used_idx;
a9a0fef7 551 virtio_mb(vq->weak_barriers);
a5c262c5
MT
552 }
553
e93300b1
RR
554#ifdef DEBUG
555 vq->last_add_time_valid = false;
556#endif
557
0a8a69dd
RR
558 END_USE(vq);
559 return ret;
560}
7c5e9ed0 561EXPORT_SYMBOL_GPL(virtqueue_get_buf);
0a8a69dd 562
5dfc1762
RR
563/**
564 * virtqueue_disable_cb - disable callbacks
565 * @vq: the struct virtqueue we're talking about.
566 *
567 * Note that this is not necessarily synchronous, hence unreliable and only
568 * useful as an optimization.
569 *
570 * Unlike other operations, this need not be serialized.
571 */
7c5e9ed0 572void virtqueue_disable_cb(struct virtqueue *_vq)
18445c4d
RR
573{
574 struct vring_virtqueue *vq = to_vvq(_vq);
575
18445c4d 576 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
18445c4d 577}
7c5e9ed0 578EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
18445c4d 579
5dfc1762 580/**
cc229884 581 * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
5dfc1762
RR
582 * @vq: the struct virtqueue we're talking about.
583 *
cc229884
MT
584 * This re-enables callbacks; it returns current queue state
585 * in an opaque unsigned value. This value should be later tested by
586 * virtqueue_poll, to detect a possible race between the driver checking for
587 * more work, and enabling callbacks.
5dfc1762
RR
588 *
589 * Caller must ensure we don't call this with other virtqueue
590 * operations at the same time (except where noted).
591 */
cc229884 592unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
0a8a69dd
RR
593{
594 struct vring_virtqueue *vq = to_vvq(_vq);
cc229884 595 u16 last_used_idx;
0a8a69dd
RR
596
597 START_USE(vq);
0a8a69dd
RR
598
599 /* We optimistically turn back on interrupts, then check if there was
600 * more to do. */
a5c262c5
MT
601 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
602 * either clear the flags bit or point the event index at the next
603 * entry. Always do both to keep code simple. */
0a8a69dd 604 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
cc229884
MT
605 vring_used_event(&vq->vring) = last_used_idx = vq->last_used_idx;
606 END_USE(vq);
607 return last_used_idx;
608}
609EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
610
611/**
612 * virtqueue_poll - query pending used buffers
613 * @vq: the struct virtqueue we're talking about.
614 * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
615 *
616 * Returns "true" if there are pending used buffers in the queue.
617 *
618 * This does not need to be serialized.
619 */
620bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
621{
622 struct vring_virtqueue *vq = to_vvq(_vq);
623
a9a0fef7 624 virtio_mb(vq->weak_barriers);
cc229884
MT
625 return (u16)last_used_idx != vq->vring.used->idx;
626}
627EXPORT_SYMBOL_GPL(virtqueue_poll);
0a8a69dd 628
cc229884
MT
629/**
630 * virtqueue_enable_cb - restart callbacks after disable_cb.
631 * @vq: the struct virtqueue we're talking about.
632 *
633 * This re-enables callbacks; it returns "false" if there are pending
634 * buffers in the queue, to detect a possible race between the driver
635 * checking for more work, and enabling callbacks.
636 *
637 * Caller must ensure we don't call this with other virtqueue
638 * operations at the same time (except where noted).
639 */
640bool virtqueue_enable_cb(struct virtqueue *_vq)
641{
642 unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
643 return !virtqueue_poll(_vq, last_used_idx);
0a8a69dd 644}
7c5e9ed0 645EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
0a8a69dd 646
5dfc1762
RR
647/**
648 * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
649 * @vq: the struct virtqueue we're talking about.
650 *
651 * This re-enables callbacks but hints to the other side to delay
652 * interrupts until most of the available buffers have been processed;
653 * it returns "false" if there are many pending buffers in the queue,
654 * to detect a possible race between the driver checking for more work,
655 * and enabling callbacks.
656 *
657 * Caller must ensure we don't call this with other virtqueue
658 * operations at the same time (except where noted).
659 */
7ab358c2
MT
660bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
661{
662 struct vring_virtqueue *vq = to_vvq(_vq);
663 u16 bufs;
664
665 START_USE(vq);
666
667 /* We optimistically turn back on interrupts, then check if there was
668 * more to do. */
669 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
670 * either clear the flags bit or point the event index at the next
671 * entry. Always do both to keep code simple. */
672 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
673 /* TODO: tune this threshold */
674 bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
675 vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
a9a0fef7 676 virtio_mb(vq->weak_barriers);
7ab358c2
MT
677 if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
678 END_USE(vq);
679 return false;
680 }
681
682 END_USE(vq);
683 return true;
684}
685EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
686
5dfc1762
RR
687/**
688 * virtqueue_detach_unused_buf - detach first unused buffer
689 * @vq: the struct virtqueue we're talking about.
690 *
b3087e48 691 * Returns NULL or the "data" token handed to virtqueue_add_*().
5dfc1762
RR
692 * This is not valid on an active queue; it is useful only for device
693 * shutdown.
694 */
7c5e9ed0 695void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
c021eac4
SM
696{
697 struct vring_virtqueue *vq = to_vvq(_vq);
698 unsigned int i;
699 void *buf;
700
701 START_USE(vq);
702
703 for (i = 0; i < vq->vring.num; i++) {
704 if (!vq->data[i])
705 continue;
706 /* detach_buf clears data, so grab it now. */
707 buf = vq->data[i];
708 detach_buf(vq, i);
b3258ff1 709 vq->vring.avail->idx--;
c021eac4
SM
710 END_USE(vq);
711 return buf;
712 }
713 /* That should have freed everything. */
06ca287d 714 BUG_ON(vq->vq.num_free != vq->vring.num);
c021eac4
SM
715
716 END_USE(vq);
717 return NULL;
718}
7c5e9ed0 719EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
c021eac4 720
0a8a69dd
RR
721irqreturn_t vring_interrupt(int irq, void *_vq)
722{
723 struct vring_virtqueue *vq = to_vvq(_vq);
724
725 if (!more_used(vq)) {
726 pr_debug("virtqueue interrupt with no work for %p\n", vq);
727 return IRQ_NONE;
728 }
729
730 if (unlikely(vq->broken))
731 return IRQ_HANDLED;
732
733 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
18445c4d
RR
734 if (vq->vq.callback)
735 vq->vq.callback(&vq->vq);
0a8a69dd
RR
736
737 return IRQ_HANDLED;
738}
c6fd4701 739EXPORT_SYMBOL_GPL(vring_interrupt);
0a8a69dd 740
17bb6d40
JW
741struct virtqueue *vring_new_virtqueue(unsigned int index,
742 unsigned int num,
87c7d57c 743 unsigned int vring_align,
0a8a69dd 744 struct virtio_device *vdev,
7b21e34f 745 bool weak_barriers,
0a8a69dd 746 void *pages,
46f9c2b9 747 bool (*notify)(struct virtqueue *),
9499f5e7
RR
748 void (*callback)(struct virtqueue *),
749 const char *name)
0a8a69dd
RR
750{
751 struct vring_virtqueue *vq;
752 unsigned int i;
753
42b36cc0
RR
754 /* We assume num is a power of 2. */
755 if (num & (num - 1)) {
756 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
757 return NULL;
758 }
759
0a8a69dd
RR
760 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
761 if (!vq)
762 return NULL;
763
87c7d57c 764 vring_init(&vq->vring, num, pages, vring_align);
0a8a69dd
RR
765 vq->vq.callback = callback;
766 vq->vq.vdev = vdev;
9499f5e7 767 vq->vq.name = name;
06ca287d
RR
768 vq->vq.num_free = num;
769 vq->vq.index = index;
0a8a69dd 770 vq->notify = notify;
7b21e34f 771 vq->weak_barriers = weak_barriers;
0a8a69dd
RR
772 vq->broken = false;
773 vq->last_used_idx = 0;
774 vq->num_added = 0;
9499f5e7 775 list_add_tail(&vq->vq.list, &vdev->vqs);
0a8a69dd
RR
776#ifdef DEBUG
777 vq->in_use = false;
e93300b1 778 vq->last_add_time_valid = false;
0a8a69dd
RR
779#endif
780
9fa29b9d 781 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
a5c262c5 782 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
9fa29b9d 783
0a8a69dd
RR
784 /* No callback? Tell other side not to bother us. */
785 if (!callback)
786 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
787
788 /* Put everything in free lists. */
0a8a69dd 789 vq->free_head = 0;
3b870624 790 for (i = 0; i < num-1; i++) {
0a8a69dd 791 vq->vring.desc[i].next = i+1;
3b870624
AS
792 vq->data[i] = NULL;
793 }
794 vq->data[i] = NULL;
0a8a69dd
RR
795
796 return &vq->vq;
797}
c6fd4701 798EXPORT_SYMBOL_GPL(vring_new_virtqueue);
0a8a69dd
RR
799
800void vring_del_virtqueue(struct virtqueue *vq)
801{
9499f5e7 802 list_del(&vq->list);
0a8a69dd
RR
803 kfree(to_vvq(vq));
804}
c6fd4701 805EXPORT_SYMBOL_GPL(vring_del_virtqueue);
0a8a69dd 806
e34f8725
RR
807/* Manipulates transport-specific feature bits. */
808void vring_transport_features(struct virtio_device *vdev)
809{
810 unsigned int i;
811
812 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
813 switch (i) {
9fa29b9d
MM
814 case VIRTIO_RING_F_INDIRECT_DESC:
815 break;
a5c262c5
MT
816 case VIRTIO_RING_F_EVENT_IDX:
817 break;
e34f8725
RR
818 default:
819 /* We don't understand this bit. */
820 clear_bit(i, vdev->features);
821 }
822 }
823}
824EXPORT_SYMBOL_GPL(vring_transport_features);
825
5dfc1762
RR
826/**
827 * virtqueue_get_vring_size - return the size of the virtqueue's vring
828 * @vq: the struct virtqueue containing the vring of interest.
829 *
830 * Returns the size of the vring. This is mainly used for boasting to
831 * userspace. Unlike other operations, this need not be serialized.
832 */
8f9f4668
RJ
833unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
834{
835
836 struct vring_virtqueue *vq = to_vvq(_vq);
837
838 return vq->vring.num;
839}
840EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
841
c6fd4701 842MODULE_LICENSE("GPL");