19 #include <linux/virtio.h>
20 #include <linux/virtio_ring.h>
21 #include <linux/virtio_config.h>
22 #include <linux/device.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/hrtimer.h>
33 #define virtio_mb(vq) \
34 do { if ((vq)->weak_barriers) smp_mb(); else mb(); } while(0)
35 #define virtio_rmb(vq) \
36 do { if ((vq)->weak_barriers) smp_rmb(); else rmb(); } while(0)
37 #define virtio_wmb(vq) \
38 do { if ((vq)->weak_barriers) smp_wmb(); else wmb(); } while(0)
43 #define virtio_mb(vq) mb()
44 #define virtio_rmb(vq) rmb()
45 #define virtio_wmb(vq) wmb()
50 #define BAD_RING(_vq, fmt, args...) \
52 dev_err(&(_vq)->vq.vdev->dev, \
53 "%s:"fmt, (_vq)->vq.name, ##args); \
57 #define START_USE(_vq) \
60 panic("%s:in_use = %i\n", \
61 (_vq)->vq.name, (_vq)->in_use); \
62 (_vq)->in_use = __LINE__; \
64 #define END_USE(_vq) \
65 do { BUG_ON(!(_vq)->in_use); (_vq)->in_use = 0; } while(0)
67 #define BAD_RING(_vq, fmt, args...) \
69 dev_err(&_vq->vq.vdev->dev, \
70 "%s:"fmt, (_vq)->vq.name, ##args); \
71 (_vq)->broken = true; \
117 bool last_add_time_valid;
125 #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
143 for (i = 0; i <
out; i++) {
150 for (; i < (out +
in); i++) {
221 if (vq->last_add_time_valid)
222 WARN_ON(ktime_to_ms(ktime_sub(now, vq->last_add_time))
224 vq->last_add_time = now;
225 vq->last_add_time_valid =
true;
232 head = vring_add_indirect(vq, sg, out, in, gfp);
241 pr_debug(
"Can't add buf len %i - avail = %i\n",
263 for (;
in; i = vq->
vring.desc[
i].next, in--) {
282 avail = (vq->
vring.avail->idx & (vq->
vring.num-1));
288 vq->
vring.avail->idx++;
296 pr_debug(
"Added buffer head %i to %p\n", head, vq);
326 new = vq->
vring.avail->idx;
330 if (vq->last_add_time_valid) {
332 vq->last_add_time)) > 100);
334 vq->last_add_time_valid =
false;
395 i = vq->
vring.desc[
i].next;
440 if (!more_used(vq)) {
441 pr_debug(
"No more buffers in queue\n");
450 i = vq->
vring.used->ring[last_used].id;
451 *len = vq->
vring.used->ring[last_used].len;
454 BAD_RING(vq,
"id %u out of range\n", i);
458 BAD_RING(vq,
"id %u is not a head!\n", i);
475 vq->last_add_time_valid =
false;
591 for (i = 0; i < vq->
vring.num; i++) {
597 vq->
vring.avail->idx--;
613 if (!more_used(vq)) {
614 pr_debug(
"virtqueue interrupt with no work for %p\n", vq);
621 pr_debug(
"virtqueue callback for %p (%p)\n", vq, vq->
vq.callback);
623 vq->
vq.callback(&vq->
vq);
631 unsigned int vring_align,
643 if (num & (num - 1)) {
644 dev_warn(&vdev->
dev,
"Bad virtqueue length %u\n", num);
652 vring_init(&vq->
vring, num, pages, vring_align);
665 vq->last_add_time_valid =
false;
678 for (i = 0; i < num-1; i++) {
679 vq->
vring.desc[
i].next = i+1;
726 return vq->
vring.num;