virtio_ring: assume sgs are always well-formed.
[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
9fa29b9d 102/* Set up an indirect table of descriptors and add it to the queue. */
13816c76
RR
103static inline int vring_add_indirect(struct vring_virtqueue *vq,
104 struct scatterlist *sgs[],
13816c76 105 unsigned int total_sg,
13816c76
RR
106 unsigned int out_sgs,
107 unsigned int in_sgs,
108 gfp_t gfp)
9fa29b9d
MM
109{
110 struct vring_desc *desc;
111 unsigned head;
13816c76
RR
112 struct scatterlist *sg;
113 int i, n;
9fa29b9d 114
b92b1b89
WD
115 /*
116 * We require lowmem mappings for the descriptors because
117 * otherwise virt_to_phys will give us bogus addresses in the
118 * virtqueue.
119 */
120 gfp &= ~(__GFP_HIGHMEM | __GFP_HIGH);
121
13816c76 122 desc = kmalloc(total_sg * sizeof(struct vring_desc), gfp);
9fa29b9d 123 if (!desc)
686d3637 124 return -ENOMEM;
9fa29b9d 125
13816c76
RR
126 /* Transfer entries from the sg lists into the indirect page */
127 i = 0;
128 for (n = 0; n < out_sgs; n++) {
eeebf9b1 129 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
13816c76
RR
130 desc[i].flags = VRING_DESC_F_NEXT;
131 desc[i].addr = sg_phys(sg);
132 desc[i].len = sg->length;
133 desc[i].next = i+1;
134 i++;
135 }
9fa29b9d 136 }
13816c76 137 for (; n < (out_sgs + in_sgs); n++) {
eeebf9b1 138 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
13816c76
RR
139 desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
140 desc[i].addr = sg_phys(sg);
141 desc[i].len = sg->length;
142 desc[i].next = i+1;
143 i++;
144 }
9fa29b9d 145 }
13816c76 146 BUG_ON(i != total_sg);
9fa29b9d
MM
147
148 /* Last one doesn't continue. */
149 desc[i-1].flags &= ~VRING_DESC_F_NEXT;
150 desc[i-1].next = 0;
151
152 /* We're about to use a buffer */
06ca287d 153 vq->vq.num_free--;
9fa29b9d
MM
154
155 /* Use a single buffer which doesn't continue */
156 head = vq->free_head;
157 vq->vring.desc[head].flags = VRING_DESC_F_INDIRECT;
158 vq->vring.desc[head].addr = virt_to_phys(desc);
bb478d8b
RR
159 /* kmemleak gives a false positive, as it's hidden by virt_to_phys */
160 kmemleak_ignore(desc);
9fa29b9d
MM
161 vq->vring.desc[head].len = i * sizeof(struct vring_desc);
162
163 /* Update free pointer */
164 vq->free_head = vq->vring.desc[head].next;
165
166 return head;
167}
168
13816c76
RR
169static inline int virtqueue_add(struct virtqueue *_vq,
170 struct scatterlist *sgs[],
eeebf9b1 171 unsigned int total_sg,
13816c76
RR
172 unsigned int out_sgs,
173 unsigned int in_sgs,
174 void *data,
175 gfp_t gfp)
0a8a69dd
RR
176{
177 struct vring_virtqueue *vq = to_vvq(_vq);
13816c76 178 struct scatterlist *sg;
eeebf9b1 179 unsigned int i, n, avail, uninitialized_var(prev);
1fe9b6fe 180 int head;
0a8a69dd 181
9fa29b9d
MM
182 START_USE(vq);
183
0a8a69dd 184 BUG_ON(data == NULL);
9fa29b9d 185
70670444
RR
186 if (unlikely(vq->broken)) {
187 END_USE(vq);
188 return -EIO;
189 }
190
e93300b1
RR
191#ifdef DEBUG
192 {
193 ktime_t now = ktime_get();
194
195 /* No kick or get, with .1 second between? Warn. */
196 if (vq->last_add_time_valid)
197 WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time))
198 > 100);
199 vq->last_add_time = now;
200 vq->last_add_time_valid = true;
201 }
202#endif
203
9fa29b9d
MM
204 /* If the host supports indirect descriptor tables, and we have multiple
205 * buffers, then go indirect. FIXME: tune this threshold */
13816c76 206 if (vq->indirect && total_sg > 1 && vq->vq.num_free) {
eeebf9b1 207 head = vring_add_indirect(vq, sgs, total_sg,
13816c76 208 out_sgs, in_sgs, gfp);
1fe9b6fe 209 if (likely(head >= 0))
9fa29b9d
MM
210 goto add_head;
211 }
212
13816c76
RR
213 BUG_ON(total_sg > vq->vring.num);
214 BUG_ON(total_sg == 0);
0a8a69dd 215
13816c76 216 if (vq->vq.num_free < total_sg) {
0a8a69dd 217 pr_debug("Can't add buf len %i - avail = %i\n",
13816c76 218 total_sg, vq->vq.num_free);
44653eae
RR
219 /* FIXME: for historical reasons, we force a notify here if
220 * there are outgoing parts to the buffer. Presumably the
221 * host should service the ring ASAP. */
13816c76 222 if (out_sgs)
44653eae 223 vq->notify(&vq->vq);
0a8a69dd
RR
224 END_USE(vq);
225 return -ENOSPC;
226 }
227
228 /* We're about to use some buffers from the free list. */
13816c76
RR
229 vq->vq.num_free -= total_sg;
230
231 head = i = vq->free_head;
232 for (n = 0; n < out_sgs; n++) {
eeebf9b1 233 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
13816c76
RR
234 vq->vring.desc[i].flags = VRING_DESC_F_NEXT;
235 vq->vring.desc[i].addr = sg_phys(sg);
236 vq->vring.desc[i].len = sg->length;
237 prev = i;
238 i = vq->vring.desc[i].next;
239 }
0a8a69dd 240 }
13816c76 241 for (; n < (out_sgs + in_sgs); n++) {
eeebf9b1 242 for (sg = sgs[n]; sg; sg = sg_next(sg)) {
13816c76
RR
243 vq->vring.desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
244 vq->vring.desc[i].addr = sg_phys(sg);
245 vq->vring.desc[i].len = sg->length;
246 prev = i;
247 i = vq->vring.desc[i].next;
248 }
0a8a69dd
RR
249 }
250 /* Last one doesn't continue. */
251 vq->vring.desc[prev].flags &= ~VRING_DESC_F_NEXT;
252
253 /* Update free pointer */
254 vq->free_head = i;
255
9fa29b9d 256add_head:
0a8a69dd
RR
257 /* Set token. */
258 vq->data[head] = data;
259
260 /* Put entry in available array (but don't update avail->idx until they
3b720b8c 261 * do sync). */
ee7cd898 262 avail = (vq->vring.avail->idx & (vq->vring.num-1));
0a8a69dd
RR
263 vq->vring.avail->ring[avail] = head;
264
ee7cd898
RR
265 /* Descriptors and available array need to be set before we expose the
266 * new available array entries. */
a9a0fef7 267 virtio_wmb(vq->weak_barriers);
ee7cd898
RR
268 vq->vring.avail->idx++;
269 vq->num_added++;
270
271 /* This is very unlikely, but theoretically possible. Kick
272 * just in case. */
273 if (unlikely(vq->num_added == (1 << 16) - 1))
274 virtqueue_kick(_vq);
275
0a8a69dd
RR
276 pr_debug("Added buffer head %i to %p\n", head, vq);
277 END_USE(vq);
3c1b27d5 278
98e8c6bc 279 return 0;
0a8a69dd 280}
13816c76 281
13816c76
RR
282/**
283 * virtqueue_add_sgs - expose buffers to other end
284 * @vq: the struct virtqueue we're talking about.
285 * @sgs: array of terminated scatterlists.
286 * @out_num: the number of scatterlists readable by other side
287 * @in_num: the number of scatterlists which are writable (after readable ones)
288 * @data: the token identifying the buffer.
289 * @gfp: how to do memory allocations (if necessary).
290 *
291 * Caller must ensure we don't call this with other virtqueue operations
292 * at the same time (except where noted).
293 *
70670444 294 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
13816c76
RR
295 */
296int virtqueue_add_sgs(struct virtqueue *_vq,
297 struct scatterlist *sgs[],
298 unsigned int out_sgs,
299 unsigned int in_sgs,
300 void *data,
301 gfp_t gfp)
302{
eeebf9b1 303 unsigned int i, total_sg = 0;
13816c76
RR
304
305 /* Count them first. */
eeebf9b1 306 for (i = 0; i < out_sgs + in_sgs; i++) {
13816c76
RR
307 struct scatterlist *sg;
308 for (sg = sgs[i]; sg; sg = sg_next(sg))
eeebf9b1 309 total_sg++;
13816c76 310 }
eeebf9b1 311 return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs, data, gfp);
13816c76
RR
312}
313EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
314
282edb36
RR
315/**
316 * virtqueue_add_outbuf - expose output buffers to other end
317 * @vq: the struct virtqueue we're talking about.
eeebf9b1
RR
318 * @sg: scatterlist (must be well-formed and terminated!)
319 * @num: the number of entries in @sg readable by other side
282edb36
RR
320 * @data: the token identifying the buffer.
321 * @gfp: how to do memory allocations (if necessary).
322 *
323 * Caller must ensure we don't call this with other virtqueue operations
324 * at the same time (except where noted).
325 *
70670444 326 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
282edb36
RR
327 */
328int virtqueue_add_outbuf(struct virtqueue *vq,
eeebf9b1 329 struct scatterlist *sg, unsigned int num,
282edb36
RR
330 void *data,
331 gfp_t gfp)
332{
eeebf9b1 333 return virtqueue_add(vq, &sg, num, 1, 0, data, gfp);
282edb36
RR
334}
335EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
336
337/**
338 * virtqueue_add_inbuf - expose input buffers to other end
339 * @vq: the struct virtqueue we're talking about.
eeebf9b1
RR
340 * @sg: scatterlist (must be well-formed and terminated!)
341 * @num: the number of entries in @sg writable by other side
282edb36
RR
342 * @data: the token identifying the buffer.
343 * @gfp: how to do memory allocations (if necessary).
344 *
345 * Caller must ensure we don't call this with other virtqueue operations
346 * at the same time (except where noted).
347 *
70670444 348 * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
282edb36
RR
349 */
350int virtqueue_add_inbuf(struct virtqueue *vq,
eeebf9b1 351 struct scatterlist *sg, unsigned int num,
282edb36
RR
352 void *data,
353 gfp_t gfp)
354{
eeebf9b1 355 return virtqueue_add(vq, &sg, num, 0, 1, data, gfp);
282edb36
RR
356}
357EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
358
5dfc1762 359/**
41f0377f 360 * virtqueue_kick_prepare - first half of split virtqueue_kick call.
5dfc1762
RR
361 * @vq: the struct virtqueue
362 *
41f0377f
RR
363 * Instead of virtqueue_kick(), you can do:
364 * if (virtqueue_kick_prepare(vq))
365 * virtqueue_notify(vq);
5dfc1762 366 *
41f0377f
RR
367 * This is sometimes useful because the virtqueue_kick_prepare() needs
368 * to be serialized, but the actual virtqueue_notify() call does not.
5dfc1762 369 */
41f0377f 370bool virtqueue_kick_prepare(struct virtqueue *_vq)
0a8a69dd
RR
371{
372 struct vring_virtqueue *vq = to_vvq(_vq);
a5c262c5 373 u16 new, old;
41f0377f
RR
374 bool needs_kick;
375
0a8a69dd 376 START_USE(vq);
a72caae2
JW
377 /* We need to expose available array entries before checking avail
378 * event. */
a9a0fef7 379 virtio_mb(vq->weak_barriers);
0a8a69dd 380
ee7cd898
RR
381 old = vq->vring.avail->idx - vq->num_added;
382 new = vq->vring.avail->idx;
0a8a69dd
RR
383 vq->num_added = 0;
384
e93300b1
RR
385#ifdef DEBUG
386 if (vq->last_add_time_valid) {
387 WARN_ON(ktime_to_ms(ktime_sub(ktime_get(),
388 vq->last_add_time)) > 100);
389 }
390 vq->last_add_time_valid = false;
391#endif
392
41f0377f
RR
393 if (vq->event) {
394 needs_kick = vring_need_event(vring_avail_event(&vq->vring),
395 new, old);
396 } else {
397 needs_kick = !(vq->vring.used->flags & VRING_USED_F_NO_NOTIFY);
398 }
0a8a69dd 399 END_USE(vq);
41f0377f
RR
400 return needs_kick;
401}
402EXPORT_SYMBOL_GPL(virtqueue_kick_prepare);
403
404/**
405 * virtqueue_notify - second half of split virtqueue_kick call.
406 * @vq: the struct virtqueue
407 *
408 * This does not need to be serialized.
5b1bf7cb
HG
409 *
410 * Returns false if host notify failed or queue is broken, otherwise true.
41f0377f 411 */
5b1bf7cb 412bool virtqueue_notify(struct virtqueue *_vq)
41f0377f
RR
413{
414 struct vring_virtqueue *vq = to_vvq(_vq);
415
5b1bf7cb
HG
416 if (unlikely(vq->broken))
417 return false;
418
41f0377f 419 /* Prod other side to tell it about changes. */
2342d6a6 420 if (!vq->notify(_vq)) {
5b1bf7cb
HG
421 vq->broken = true;
422 return false;
423 }
424 return true;
41f0377f
RR
425}
426EXPORT_SYMBOL_GPL(virtqueue_notify);
427
428/**
429 * virtqueue_kick - update after add_buf
430 * @vq: the struct virtqueue
431 *
b3087e48 432 * After one or more virtqueue_add_* calls, invoke this to kick
41f0377f
RR
433 * the other side.
434 *
435 * Caller must ensure we don't call this with other virtqueue
436 * operations at the same time (except where noted).
5b1bf7cb
HG
437 *
438 * Returns false if kick failed, otherwise true.
41f0377f 439 */
5b1bf7cb 440bool virtqueue_kick(struct virtqueue *vq)
41f0377f
RR
441{
442 if (virtqueue_kick_prepare(vq))
5b1bf7cb
HG
443 return virtqueue_notify(vq);
444 return true;
0a8a69dd 445}
7c5e9ed0 446EXPORT_SYMBOL_GPL(virtqueue_kick);
0a8a69dd
RR
447
448static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
449{
450 unsigned int i;
451
452 /* Clear data ptr. */
453 vq->data[head] = NULL;
454
455 /* Put back on free list: find end */
456 i = head;
9fa29b9d
MM
457
458 /* Free the indirect table */
459 if (vq->vring.desc[i].flags & VRING_DESC_F_INDIRECT)
460 kfree(phys_to_virt(vq->vring.desc[i].addr));
461
0a8a69dd
RR
462 while (vq->vring.desc[i].flags & VRING_DESC_F_NEXT) {
463 i = vq->vring.desc[i].next;
06ca287d 464 vq->vq.num_free++;
0a8a69dd
RR
465 }
466
467 vq->vring.desc[i].next = vq->free_head;
468 vq->free_head = head;
469 /* Plus final descriptor */
06ca287d 470 vq->vq.num_free++;
0a8a69dd
RR
471}
472
0a8a69dd
RR
473static inline bool more_used(const struct vring_virtqueue *vq)
474{
475 return vq->last_used_idx != vq->vring.used->idx;
476}
477
5dfc1762
RR
478/**
479 * virtqueue_get_buf - get the next used buffer
480 * @vq: the struct virtqueue we're talking about.
481 * @len: the length written into the buffer
482 *
483 * If the driver wrote data into the buffer, @len will be set to the
484 * amount written. This means you don't need to clear the buffer
485 * beforehand to ensure there's no data leakage in the case of short
486 * writes.
487 *
488 * Caller must ensure we don't call this with other virtqueue
489 * operations at the same time (except where noted).
490 *
491 * Returns NULL if there are no used buffers, or the "data" token
b3087e48 492 * handed to virtqueue_add_*().
5dfc1762 493 */
7c5e9ed0 494void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len)
0a8a69dd
RR
495{
496 struct vring_virtqueue *vq = to_vvq(_vq);
497 void *ret;
498 unsigned int i;
3b720b8c 499 u16 last_used;
0a8a69dd
RR
500
501 START_USE(vq);
502
5ef82752
RR
503 if (unlikely(vq->broken)) {
504 END_USE(vq);
505 return NULL;
506 }
507
0a8a69dd
RR
508 if (!more_used(vq)) {
509 pr_debug("No more buffers in queue\n");
510 END_USE(vq);
511 return NULL;
512 }
513
2d61ba95 514 /* Only get used array entries after they have been exposed by host. */
a9a0fef7 515 virtio_rmb(vq->weak_barriers);
2d61ba95 516
3b720b8c
RR
517 last_used = (vq->last_used_idx & (vq->vring.num - 1));
518 i = vq->vring.used->ring[last_used].id;
519 *len = vq->vring.used->ring[last_used].len;
0a8a69dd
RR
520
521 if (unlikely(i >= vq->vring.num)) {
522 BAD_RING(vq, "id %u out of range\n", i);
523 return NULL;
524 }
525 if (unlikely(!vq->data[i])) {
526 BAD_RING(vq, "id %u is not a head!\n", i);
527 return NULL;
528 }
529
530 /* detach_buf clears data, so grab it now. */
531 ret = vq->data[i];
532 detach_buf(vq, i);
533 vq->last_used_idx++;
a5c262c5
MT
534 /* If we expect an interrupt for the next entry, tell host
535 * by writing event index and flush out the write before
536 * the read in the next get_buf call. */
537 if (!(vq->vring.avail->flags & VRING_AVAIL_F_NO_INTERRUPT)) {
538 vring_used_event(&vq->vring) = vq->last_used_idx;
a9a0fef7 539 virtio_mb(vq->weak_barriers);
a5c262c5
MT
540 }
541
e93300b1
RR
542#ifdef DEBUG
543 vq->last_add_time_valid = false;
544#endif
545
0a8a69dd
RR
546 END_USE(vq);
547 return ret;
548}
7c5e9ed0 549EXPORT_SYMBOL_GPL(virtqueue_get_buf);
0a8a69dd 550
5dfc1762
RR
551/**
552 * virtqueue_disable_cb - disable callbacks
553 * @vq: the struct virtqueue we're talking about.
554 *
555 * Note that this is not necessarily synchronous, hence unreliable and only
556 * useful as an optimization.
557 *
558 * Unlike other operations, this need not be serialized.
559 */
7c5e9ed0 560void virtqueue_disable_cb(struct virtqueue *_vq)
18445c4d
RR
561{
562 struct vring_virtqueue *vq = to_vvq(_vq);
563
18445c4d 564 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
18445c4d 565}
7c5e9ed0 566EXPORT_SYMBOL_GPL(virtqueue_disable_cb);
18445c4d 567
5dfc1762 568/**
cc229884 569 * virtqueue_enable_cb_prepare - restart callbacks after disable_cb
5dfc1762
RR
570 * @vq: the struct virtqueue we're talking about.
571 *
cc229884
MT
572 * This re-enables callbacks; it returns current queue state
573 * in an opaque unsigned value. This value should be later tested by
574 * virtqueue_poll, to detect a possible race between the driver checking for
575 * more work, and enabling callbacks.
5dfc1762
RR
576 *
577 * Caller must ensure we don't call this with other virtqueue
578 * operations at the same time (except where noted).
579 */
cc229884 580unsigned virtqueue_enable_cb_prepare(struct virtqueue *_vq)
0a8a69dd
RR
581{
582 struct vring_virtqueue *vq = to_vvq(_vq);
cc229884 583 u16 last_used_idx;
0a8a69dd
RR
584
585 START_USE(vq);
0a8a69dd
RR
586
587 /* We optimistically turn back on interrupts, then check if there was
588 * more to do. */
a5c262c5
MT
589 /* Depending on the VIRTIO_RING_F_EVENT_IDX feature, we need to
590 * either clear the flags bit or point the event index at the next
591 * entry. Always do both to keep code simple. */
0a8a69dd 592 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
cc229884
MT
593 vring_used_event(&vq->vring) = last_used_idx = vq->last_used_idx;
594 END_USE(vq);
595 return last_used_idx;
596}
597EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare);
598
599/**
600 * virtqueue_poll - query pending used buffers
601 * @vq: the struct virtqueue we're talking about.
602 * @last_used_idx: virtqueue state (from call to virtqueue_enable_cb_prepare).
603 *
604 * Returns "true" if there are pending used buffers in the queue.
605 *
606 * This does not need to be serialized.
607 */
608bool virtqueue_poll(struct virtqueue *_vq, unsigned last_used_idx)
609{
610 struct vring_virtqueue *vq = to_vvq(_vq);
611
a9a0fef7 612 virtio_mb(vq->weak_barriers);
cc229884
MT
613 return (u16)last_used_idx != vq->vring.used->idx;
614}
615EXPORT_SYMBOL_GPL(virtqueue_poll);
0a8a69dd 616
cc229884
MT
617/**
618 * virtqueue_enable_cb - restart callbacks after disable_cb.
619 * @vq: the struct virtqueue we're talking about.
620 *
621 * This re-enables callbacks; it returns "false" if there are pending
622 * buffers in the queue, to detect a possible race between the driver
623 * checking for more work, and enabling callbacks.
624 *
625 * Caller must ensure we don't call this with other virtqueue
626 * operations at the same time (except where noted).
627 */
628bool virtqueue_enable_cb(struct virtqueue *_vq)
629{
630 unsigned last_used_idx = virtqueue_enable_cb_prepare(_vq);
631 return !virtqueue_poll(_vq, last_used_idx);
0a8a69dd 632}
7c5e9ed0 633EXPORT_SYMBOL_GPL(virtqueue_enable_cb);
0a8a69dd 634
5dfc1762
RR
635/**
636 * virtqueue_enable_cb_delayed - restart callbacks after disable_cb.
637 * @vq: the struct virtqueue we're talking about.
638 *
639 * This re-enables callbacks but hints to the other side to delay
640 * interrupts until most of the available buffers have been processed;
641 * it returns "false" if there are many pending buffers in the queue,
642 * to detect a possible race between the driver checking for more work,
643 * and enabling callbacks.
644 *
645 * Caller must ensure we don't call this with other virtqueue
646 * operations at the same time (except where noted).
647 */
7ab358c2
MT
648bool virtqueue_enable_cb_delayed(struct virtqueue *_vq)
649{
650 struct vring_virtqueue *vq = to_vvq(_vq);
651 u16 bufs;
652
653 START_USE(vq);
654
655 /* We optimistically turn back on interrupts, then check if there was
656 * more to do. */
657 /* Depending on the VIRTIO_RING_F_USED_EVENT_IDX feature, we need to
658 * either clear the flags bit or point the event index at the next
659 * entry. Always do both to keep code simple. */
660 vq->vring.avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
661 /* TODO: tune this threshold */
662 bufs = (u16)(vq->vring.avail->idx - vq->last_used_idx) * 3 / 4;
663 vring_used_event(&vq->vring) = vq->last_used_idx + bufs;
a9a0fef7 664 virtio_mb(vq->weak_barriers);
7ab358c2
MT
665 if (unlikely((u16)(vq->vring.used->idx - vq->last_used_idx) > bufs)) {
666 END_USE(vq);
667 return false;
668 }
669
670 END_USE(vq);
671 return true;
672}
673EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed);
674
5dfc1762
RR
675/**
676 * virtqueue_detach_unused_buf - detach first unused buffer
677 * @vq: the struct virtqueue we're talking about.
678 *
b3087e48 679 * Returns NULL or the "data" token handed to virtqueue_add_*().
5dfc1762
RR
680 * This is not valid on an active queue; it is useful only for device
681 * shutdown.
682 */
7c5e9ed0 683void *virtqueue_detach_unused_buf(struct virtqueue *_vq)
c021eac4
SM
684{
685 struct vring_virtqueue *vq = to_vvq(_vq);
686 unsigned int i;
687 void *buf;
688
689 START_USE(vq);
690
691 for (i = 0; i < vq->vring.num; i++) {
692 if (!vq->data[i])
693 continue;
694 /* detach_buf clears data, so grab it now. */
695 buf = vq->data[i];
696 detach_buf(vq, i);
b3258ff1 697 vq->vring.avail->idx--;
c021eac4
SM
698 END_USE(vq);
699 return buf;
700 }
701 /* That should have freed everything. */
06ca287d 702 BUG_ON(vq->vq.num_free != vq->vring.num);
c021eac4
SM
703
704 END_USE(vq);
705 return NULL;
706}
7c5e9ed0 707EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf);
c021eac4 708
0a8a69dd
RR
709irqreturn_t vring_interrupt(int irq, void *_vq)
710{
711 struct vring_virtqueue *vq = to_vvq(_vq);
712
713 if (!more_used(vq)) {
714 pr_debug("virtqueue interrupt with no work for %p\n", vq);
715 return IRQ_NONE;
716 }
717
718 if (unlikely(vq->broken))
719 return IRQ_HANDLED;
720
721 pr_debug("virtqueue callback for %p (%p)\n", vq, vq->vq.callback);
18445c4d
RR
722 if (vq->vq.callback)
723 vq->vq.callback(&vq->vq);
0a8a69dd
RR
724
725 return IRQ_HANDLED;
726}
c6fd4701 727EXPORT_SYMBOL_GPL(vring_interrupt);
0a8a69dd 728
17bb6d40
JW
729struct virtqueue *vring_new_virtqueue(unsigned int index,
730 unsigned int num,
87c7d57c 731 unsigned int vring_align,
0a8a69dd 732 struct virtio_device *vdev,
7b21e34f 733 bool weak_barriers,
0a8a69dd 734 void *pages,
46f9c2b9 735 bool (*notify)(struct virtqueue *),
9499f5e7
RR
736 void (*callback)(struct virtqueue *),
737 const char *name)
0a8a69dd
RR
738{
739 struct vring_virtqueue *vq;
740 unsigned int i;
741
42b36cc0
RR
742 /* We assume num is a power of 2. */
743 if (num & (num - 1)) {
744 dev_warn(&vdev->dev, "Bad virtqueue length %u\n", num);
745 return NULL;
746 }
747
0a8a69dd
RR
748 vq = kmalloc(sizeof(*vq) + sizeof(void *)*num, GFP_KERNEL);
749 if (!vq)
750 return NULL;
751
87c7d57c 752 vring_init(&vq->vring, num, pages, vring_align);
0a8a69dd
RR
753 vq->vq.callback = callback;
754 vq->vq.vdev = vdev;
9499f5e7 755 vq->vq.name = name;
06ca287d
RR
756 vq->vq.num_free = num;
757 vq->vq.index = index;
0a8a69dd 758 vq->notify = notify;
7b21e34f 759 vq->weak_barriers = weak_barriers;
0a8a69dd
RR
760 vq->broken = false;
761 vq->last_used_idx = 0;
762 vq->num_added = 0;
9499f5e7 763 list_add_tail(&vq->vq.list, &vdev->vqs);
0a8a69dd
RR
764#ifdef DEBUG
765 vq->in_use = false;
e93300b1 766 vq->last_add_time_valid = false;
0a8a69dd
RR
767#endif
768
9fa29b9d 769 vq->indirect = virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC);
a5c262c5 770 vq->event = virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX);
9fa29b9d 771
0a8a69dd
RR
772 /* No callback? Tell other side not to bother us. */
773 if (!callback)
774 vq->vring.avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
775
776 /* Put everything in free lists. */
0a8a69dd 777 vq->free_head = 0;
3b870624 778 for (i = 0; i < num-1; i++) {
0a8a69dd 779 vq->vring.desc[i].next = i+1;
3b870624
AS
780 vq->data[i] = NULL;
781 }
782 vq->data[i] = NULL;
0a8a69dd
RR
783
784 return &vq->vq;
785}
c6fd4701 786EXPORT_SYMBOL_GPL(vring_new_virtqueue);
0a8a69dd
RR
787
788void vring_del_virtqueue(struct virtqueue *vq)
789{
9499f5e7 790 list_del(&vq->list);
0a8a69dd
RR
791 kfree(to_vvq(vq));
792}
c6fd4701 793EXPORT_SYMBOL_GPL(vring_del_virtqueue);
0a8a69dd 794
e34f8725
RR
795/* Manipulates transport-specific feature bits. */
796void vring_transport_features(struct virtio_device *vdev)
797{
798 unsigned int i;
799
800 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++) {
801 switch (i) {
9fa29b9d
MM
802 case VIRTIO_RING_F_INDIRECT_DESC:
803 break;
a5c262c5
MT
804 case VIRTIO_RING_F_EVENT_IDX:
805 break;
e34f8725
RR
806 default:
807 /* We don't understand this bit. */
808 clear_bit(i, vdev->features);
809 }
810 }
811}
812EXPORT_SYMBOL_GPL(vring_transport_features);
813
5dfc1762
RR
814/**
815 * virtqueue_get_vring_size - return the size of the virtqueue's vring
816 * @vq: the struct virtqueue containing the vring of interest.
817 *
818 * Returns the size of the vring. This is mainly used for boasting to
819 * userspace. Unlike other operations, this need not be serialized.
820 */
8f9f4668
RJ
821unsigned int virtqueue_get_vring_size(struct virtqueue *_vq)
822{
823
824 struct vring_virtqueue *vq = to_vvq(_vq);
825
826 return vq->vring.num;
827}
828EXPORT_SYMBOL_GPL(virtqueue_get_vring_size);
829
b3b32c94
HG
830bool virtqueue_is_broken(struct virtqueue *_vq)
831{
832 struct vring_virtqueue *vq = to_vvq(_vq);
833
834 return vq->broken;
835}
836EXPORT_SYMBOL_GPL(virtqueue_is_broken);
837
e2dcdfe9
RR
838/*
839 * This should prevent the device from being used, allowing drivers to
840 * recover. You may need to grab appropriate locks to flush.
841 */
842void virtio_break_device(struct virtio_device *dev)
843{
844 struct virtqueue *_vq;
845
846 list_for_each_entry(_vq, &dev->vqs, list) {
847 struct vring_virtqueue *vq = to_vvq(_vq);
848 vq->broken = true;
849 }
850}
851EXPORT_SYMBOL_GPL(virtio_break_device);
852
c6fd4701 853MODULE_LICENSE("GPL");
This page took 0.528184 seconds and 5 git commands to generate.