virtio-rng: don't crash if virtqueue is broken.
[deliverable/linux.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>
6abb2dd9 26#include <linux/kmemleak.h>
0a8a69dd
RR
27
28#ifdef DEBUG
29/* For development, we want to crash whenever the ring is screwed. */
9499f5e7
RR
30#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)
c5f841f1
RR
36/* Caller is supposed to guarantee no reentry. */
37#define START_USE(_vq) \
38 do { \
39 if ((_vq)->in_use) \
9499f5e7
RR
40 panic("%s:in_use = %i\n", \
41 (_vq)->vq.name, (_vq)->in_use); \
c5f841f1 42 (_vq)->in_use = __LINE__; \
9499f5e7 43 } while (0)
3a35ce7d 44#define END_USE(_vq) \
97a545ab 45 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
0a8a69dd 46#else
9499f5e7
RR
47#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)
0a8a69dd
RR
53#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
7b21e34f
RR
64 /* Can we use weak barriers? */
65 bool weak_barriers;
66
0a8a69dd
RR
67 /* Other side has made a mess, don't try any more. */
68 bool broken;
69
9fa29b9d
MM
70 /* Host supports indirect buffers */
71 bool indirect;
72
a5c262c5
MT
73 /* Host publishes avail event idx */
74 bool event;
75
0a8a69dd
RR
76 /* 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. */
1bc4953e 82 u16 last_used_idx;
0a8a69dd
RR
83
84 /* How to notify other side. FIXME: commonalize hcalls! */
46f9c2b9 85 bool (*notify)(struct virtqueue *vq);
0a8a69dd
RR
86
87#ifdef DEBUG
88 /* They're supposed to lock for us. */
89 unsigned int in_use;
e93300b1
RR
90
91 /* Figure out if their kicks are too delayed. */
92 bool last_add_time_valid;
93 ktime_t last_add_time;
0a8a69dd
RR
94#endif
95
96 /* Tokens for callbacks. */
97 void *data[];
98};
99
100#define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
101
13816c76
RR
102static 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
9fa29b9d 116/* Set up an indirect table of descriptors and add it to the queue. */
13816c76
RR
117static 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)
9fa29b9d
MM
127{
128 struct vring_desc *desc;
129 unsigned head;
13816c76
RR
130 struct scatterlist *sg;
131 int i, n;
9fa29b9d 132
b92b1b89
WD
133 /*
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
13816c76 140 desc = kmalloc(total_sg * sizeof(struct vring_desc), gfp);
9fa29b9d 141 if (!desc)
686d3637 142 return -ENOMEM;
9fa29b9d 143
13816c76
RR
144 /* 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 }
9fa29b9d 154 }
13816c76
RR
155 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 }
9fa29b9d 163 }
13816c76 164 BUG_ON(i != total_sg);
9fa29b9d
MM
165
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 */
06ca287d 171 vq->vq.num_free--;
9fa29b9d
MM
172
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);
bb478d8b
RR
177 /* kmemleak gives a false positive, as it's hidden by virt_to_phys */
178 kmemleak_ignore(desc);
9fa29b9d
MM
179 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
13816c76
RR
187static 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)
0a8a69dd
RR
197{
198 struct vring_virtqueue *vq = to_vvq(_vq);
13816c76
RR
199 struct scatterlist *sg;
200 unsigned int i, n, avail, uninitialized_var(prev), total_sg;
1fe9b6fe 201 int head;
0a8a69dd 202
9fa29b9d
MM
203 START_USE(vq);
204
0a8a69dd 205 BUG_ON(data == NULL);
9fa29b9d 206
e93300b1
RR
207#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
13816c76
RR
220 total_sg = total_in + total_out;
221
9fa29b9d
MM
222 /* If the host supports indirect descriptor tables, and we have multiple
223 * buffers, then go indirect. FIXME: tune this threshold */
13816c76
RR
224 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);
1fe9b6fe 228 if (likely(head >= 0))
9fa29b9d
MM
229 goto add_head;
230 }
231
13816c76
RR
232 BUG_ON(total_sg > vq->vring.num);
233 BUG_ON(total_sg == 0);
0a8a69dd 234
13816c76 235 if (vq->vq.num_free < total_sg) {
0a8a69dd 236 pr_debug("Can't add buf len %i - avail = %i\n",
13816c76 237 total_sg, vq->vq.num_free);
44653eae
RR
238 /* 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. */
13816c76 241 if (out_sgs)
44653eae 242 vq->notify(&vq->vq);
0a8a69dd
RR
243 END_USE(vq);
244 return -ENOSPC;
245 }
246
247 /* We're about to use some buffers from the free list. */
13816c76
RR
248 vq->vq.num_free -= total_sg;
249
250 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 }
0a8a69dd 259 }
13816c76
RR
260 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 }
0a8a69dd
RR
268 }
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
9fa29b9d 275add_head:
0a8a69dd
RR
276 /* Set token. */
277 vq->data[head] = data;
278
279 /* Put entry in available array (but don't update avail->idx until they
3b720b8c 280 * do sync). */
ee7cd898 281 avail = (vq->vring.avail->idx & (vq->vring.num-1));
0a8a69dd
RR
282 vq->vring.avail->ring[avail] = head;
283
ee7cd898
RR
284 /* Descriptors and available array need to be set before we expose the
285 * new available array entries. */
a9a0fef7 286 virtio_wmb(vq->weak_barriers);
ee7cd898
RR
287 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
0a8a69dd
RR
295 pr_debug("Added buffer head %i to %p\n", head, vq);
296 END_USE(vq);
3c1b27d5 297
98e8c6bc 298 return 0;
0a8a69dd 299}
13816c76 300
13816c76
RR
301/**
302 * 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
282edb36
RR
340/**
341 * 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
5dfc1762 384/**
41f0377f 385 * virtqueue_kick_prepare - first half of split virtqueue_kick call.
5dfc1762
RR
386 * @vq: the struct virtqueue
387 *
41f0377f
RR
388 * Instead of virtqueue_kick(), you can do:
389 * if (virtqueue_kick_prepare(vq))
390 * virtqueue_notify(vq);
5dfc1762 391 *
41f0377f
RR
392 * This is sometimes useful because the virtqueue_kick_prepare() needs
393 * to be serialized, but the actual virtqueue_notify() call does not.
5dfc1762 394 */
41f0377f 395bool virtqueue_kick_prepare(struct virtqueue *_vq)
0a8a69dd
RR
396{
397 struct vring_virtqueue *vq = to_vvq(_vq);
a5c262c5 398 u16 new, old;
41f0377f
RR
399 bool needs_kick;
400
0a8a69dd 401 START_USE(vq);
a72caae2
JW
402 /* We need to expose available array entries before checking avail
403 * event. */
a9a0fef7 404 virtio_mb(vq->weak_barriers);
0a8a69dd 405
ee7cd898
RR
406 old = vq->vring.avail->idx - vq->num_added;
407 new = vq->vring.avail->idx;
0a8a69dd
RR
408 vq->num_added = 0;
409
e93300b1
RR
410#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
41f0377f
RR
418 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 }
0a8a69dd 424 END_USE(vq);
41f0377f
RR
425 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.
5b1bf7cb
HG
434 *
435 * Returns false if host notify failed or queue is broken, otherwise true.
41f0377f 436 */
5b1bf7cb 437bool virtqueue_notify(struct virtqueue *_vq)
41f0377f
RR
438{
439 struct vring_virtqueue *vq = to_vvq(_vq);
440
5b1bf7cb
HG
441 if (unlikely(vq->broken))
442 return false;
443
41f0377f 444 /* Prod other side to tell it about changes. */
2342d6a6 445 if (!vq->notify(_vq)) {
5b1bf7cb
HG
446 vq->broken = true;
447 return false;
448 }
449 return true;
41f0377f
RR
450}
451EXPORT_SYMBOL_GPL(virtqueue_notify);
452
453/**
454 * virtqueue_kick - update after add_buf
455 * @vq: the struct virtqueue
456 *
b3087e48 457 * After one or more virtqueue_add_* calls, invoke this to kick
41f0377f
RR
458 * 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).
5b1bf7cb
HG
462 *
463 * Returns false if kick failed, otherwise true.
41f0377f 464 */
5b1bf7cb 465bool virtqueue_kick(struct virtqueue *vq)
41f0377f
RR
466{
467 if (virtqueue_kick_prepare(vq))
5b1bf7cb
HG
468 return virtqueue_notify(vq);
469 return true;
0a8a69dd 470}
7c5e9ed0 471EXPORT_SYMBOL_GPL(virtqueue_kick);
0a8a69dd
RR
472
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;
9fa29b9d
MM
482
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
0a8a69dd
RR
487 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
488 i = vq->vring.desc[i].next;
06ca287d 489 vq->vq.num_free++;
0a8a69dd
RR
490 }
491
492 vq->vring.desc[i].next = vq->free_head;
493 vq->free_head = head;
494 /* Plus final descriptor */
06ca287d 495 vq->vq.num_free++;
0a8a69dd
RR
496}
497
0a8a69dd
RR
498static inline bool more_used(const struct vring_virtqueue *vq)
499{
500 return vq->last_used_idx != vq->vring.used->idx;
501}
502
5dfc1762
RR
503/**
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
b3087e48 517 * handed to virtqueue_add_*().
5dfc1762 518 */
7c5e9ed0 519void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
0a8a69dd
RR
520{
521 struct vring_virtqueue *vq = to_vvq(_vq);
522 void *ret;
523 unsigned int i;
3b720b8c 524 u16 last_used;
0a8a69dd
RR
525
526 START_USE(vq);
527
5ef82752
RR
528 if (unlikely(vq->broken)) {
529 END_USE(vq);
530 return NULL;
531 }
532
0a8a69dd
RR
533 if (!more_used(vq)) {
534 pr_debug("No more buffers in queue\n");
535 END_USE(vq);
536 return NULL;
537 }
538
2d61ba95 539 /* Only get used array entries after they have been exposed by host. */
a9a0fef7 540 virtio_rmb(vq->weak_barriers);
2d61ba95 541
3b720b8c
RR
542 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;
0a8a69dd
RR
545
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++;
a5c262c5
MT
559 /* 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;
a9a0fef7 564 virtio_mb(vq->weak_barriers);
a5c262c5
MT
565 }
566
e93300b1
RR
567#ifdef DEBUG
568 vq->last_add_time_valid = false;
569#endif
570
0a8a69dd
RR
571 END_USE(vq);
572 return ret;
573}
7c5e9ed0 574EXPORT_SYMBOL_GPL(virtqueue_get_buf);
0a8a69dd 575
5dfc1762
RR
576/**
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 */
7c5e9ed0 585void virtqueue_disable_cb(struct virtqueue *_vq)
18445c4d
RR
586{
587 struct vring_virtqueue *vq = to_vvq(_vq);
588
18445c4d 589 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
18445c4d 590}
7c5e9ed0 591EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
18445c4d 592
5dfc1762 593/**
cc229884 594 * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
5dfc1762
RR
595 * @vq: the struct virtqueue we're talking about.
596 *
cc229884
MT
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.
5dfc1762
RR
601 *
602 * Caller must ensure we don't call this with other virtqueue
603 * operations at the same time (except where noted).
604 */
cc229884 605unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
0a8a69dd
RR
606{
607 struct vring_virtqueue *vq = to_vvq(_vq);
cc229884 608 u16 last_used_idx;
0a8a69dd
RR
609
610 START_USE(vq);
0a8a69dd
RR
611
612 /* We optimistically turn back on interrupts, then check if there was
613 * more to do. */
a5c262c5
MT
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. */
0a8a69dd 617 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
cc229884
MT
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
a9a0fef7 637 virtio_mb(vq->weak_barriers);
cc229884
MT
638 return (u16)last_used_idx != vq->vring.used->idx;
639}
640EXPORT_SYMBOL_GPL(virtqueue_poll);
0a8a69dd 641
cc229884
MT
642/**
643 * 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 */
653bool virtqueue_enable_cb(struct virtqueue *_vq)
654{
655 unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
656 return !virtqueue_poll(_vq, last_used_idx);
0a8a69dd 657}
7c5e9ed0 658EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
0a8a69dd 659
5dfc1762
RR
660/**
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 */
7ab358c2
MT
673bool 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;
a9a0fef7 689 virtio_mb(vq->weak_barriers);
7ab358c2
MT
690 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
5dfc1762
RR
700/**
701 * virtqueue_detach_unused_buf - detach first unused buffer
702 * @vq: the struct virtqueue we're talking about.
703 *
b3087e48 704 * Returns NULL or the "data" token handed to virtqueue_add_*().
5dfc1762
RR
705 * This is not valid on an active queue; it is useful only for device
706 * shutdown.
707 */
7c5e9ed0 708void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
c021eac4
SM
709{
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);
b3258ff1 722 vq->vring.avail->idx--;
c021eac4
SM
723 END_USE(vq);
724 return buf;
725 }
726 /* That should have freed everything. */
06ca287d 727 BUG_ON(vq->vq.num_free != vq->vring.num);
c021eac4
SM
728
729 END_USE(vq);
730 return NULL;
731}
7c5e9ed0 732EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
c021eac4 733
0a8a69dd
RR
734irqreturn_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);
18445c4d
RR
747 if (vq->vq.callback)
748 vq->vq.callback(&vq->vq);
0a8a69dd
RR
749
750 return IRQ_HANDLED;
751}
c6fd4701 752EXPORT_SYMBOL_GPL(vring_interrupt);
0a8a69dd 753
17bb6d40
JW
754struct virtqueue *vring_new_virtqueue(unsigned int index,
755 unsigned int num,
87c7d57c 756 unsigned int vring_align,
0a8a69dd 757 struct virtio_device *vdev,
7b21e34f 758 bool weak_barriers,
0a8a69dd 759 void *pages,
46f9c2b9 760 bool (*notify)(struct virtqueue *),
9499f5e7
RR
761 void (*callback)(struct virtqueue *),
762 const char *name)
0a8a69dd
RR
763{
764 struct vring_virtqueue *vq;
765 unsigned int i;
766
42b36cc0
RR
767 /* 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
0a8a69dd
RR
773 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
774 if (!vq)
775 return NULL;
776
87c7d57c 777 vring_init(&vq->vring, num, pages, vring_align);
0a8a69dd
RR
778 vq->vq.callback = callback;
779 vq->vq.vdev = vdev;
9499f5e7 780 vq->vq.name = name;
06ca287d
RR
781 vq->vq.num_free = num;
782 vq->vq.index = index;
0a8a69dd 783 vq->notify = notify;
7b21e34f 784 vq->weak_barriers = weak_barriers;
0a8a69dd
RR
785 vq->broken = false;
786 vq->last_used_idx = 0;
787 vq->num_added = 0;
9499f5e7 788 list_add_tail(&vq->vq.list, &vdev->vqs);
0a8a69dd
RR
789#ifdef DEBUG
790 vq->in_use = false;
e93300b1 791 vq->last_add_time_valid = false;
0a8a69dd
RR
792#endif
793
9fa29b9d 794 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
a5c262c5 795 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
9fa29b9d 796
0a8a69dd
RR
797 /* 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. */
0a8a69dd 802 vq->free_head = 0;
3b870624 803 for (i = 0; i < num-1; i++) {
0a8a69dd 804 vq->vring.desc[i].next = i+1;
3b870624
AS
805 vq->data[i] = NULL;
806 }
807 vq->data[i] = NULL;
0a8a69dd
RR
808
809 return &vq->vq;
810}
c6fd4701 811EXPORT_SYMBOL_GPL(vring_new_virtqueue);
0a8a69dd
RR
812
813void vring_del_virtqueue(struct virtqueue *vq)
814{
9499f5e7 815 list_del(&vq->list);
0a8a69dd
RR
816 kfree(to_vvq(vq));
817}
c6fd4701 818EXPORT_SYMBOL_GPL(vring_del_virtqueue);
0a8a69dd 819
e34f8725
RR
820/* 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) {
9fa29b9d
MM
827 case VIRTIO_RING_F_INDIRECT_DESC:
828 break;
a5c262c5
MT
829 case VIRTIO_RING_F_EVENT_IDX:
830 break;
e34f8725
RR
831 default:
832 /* We don't understand this bit. */
833 clear_bit(i, vdev->features);
834 }
835 }
836}
837EXPORT_SYMBOL_GPL(vring_transport_features);
838
5dfc1762
RR
839/**
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 */
8f9f4668
RJ
846unsigned 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
b3b32c94
HG
855bool 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
c6fd4701 863MODULE_LICENSE("GPL");
This page took 0.526862 seconds and 5 git commands to generate.