.. | .. |
---|
4 | 4 | * Copyright (C) 2007, 2008 Rusty Russell IBM Corporation |
---|
5 | 5 | */ |
---|
6 | 6 | |
---|
| 7 | +#include <asm/barrier.h> |
---|
7 | 8 | #include <linux/err.h> |
---|
8 | 9 | #include <linux/hw_random.h> |
---|
9 | 10 | #include <linux/scatterlist.h> |
---|
.. | .. |
---|
18 | 19 | struct virtrng_info { |
---|
19 | 20 | struct hwrng hwrng; |
---|
20 | 21 | struct virtqueue *vq; |
---|
21 | | - struct completion have_data; |
---|
22 | 22 | char name[25]; |
---|
23 | | - unsigned int data_avail; |
---|
24 | 23 | int index; |
---|
25 | | - bool busy; |
---|
26 | 24 | bool hwrng_register_done; |
---|
27 | 25 | bool hwrng_removed; |
---|
| 26 | + /* data transfer */ |
---|
| 27 | + struct completion have_data; |
---|
| 28 | + unsigned int data_avail; |
---|
| 29 | + unsigned int data_idx; |
---|
| 30 | + /* minimal size returned by rng_buffer_size() */ |
---|
| 31 | +#if SMP_CACHE_BYTES < 32 |
---|
| 32 | + u8 data[32]; |
---|
| 33 | +#else |
---|
| 34 | + u8 data[SMP_CACHE_BYTES]; |
---|
| 35 | +#endif |
---|
28 | 36 | }; |
---|
29 | 37 | |
---|
30 | 38 | static void random_recv_done(struct virtqueue *vq) |
---|
31 | 39 | { |
---|
32 | 40 | struct virtrng_info *vi = vq->vdev->priv; |
---|
| 41 | + unsigned int len; |
---|
33 | 42 | |
---|
34 | 43 | /* We can get spurious callbacks, e.g. shared IRQs + virtio_pci. */ |
---|
35 | | - if (!virtqueue_get_buf(vi->vq, &vi->data_avail)) |
---|
| 44 | + if (!virtqueue_get_buf(vi->vq, &len)) |
---|
36 | 45 | return; |
---|
37 | 46 | |
---|
| 47 | + smp_store_release(&vi->data_avail, len); |
---|
38 | 48 | complete(&vi->have_data); |
---|
39 | 49 | } |
---|
40 | 50 | |
---|
41 | | -/* The host will fill any buffer we give it with sweet, sweet randomness. */ |
---|
42 | | -static void register_buffer(struct virtrng_info *vi, u8 *buf, size_t size) |
---|
| 51 | +static void request_entropy(struct virtrng_info *vi) |
---|
43 | 52 | { |
---|
44 | 53 | struct scatterlist sg; |
---|
45 | 54 | |
---|
46 | | - sg_init_one(&sg, buf, size); |
---|
| 55 | + reinit_completion(&vi->have_data); |
---|
| 56 | + vi->data_idx = 0; |
---|
| 57 | + |
---|
| 58 | + sg_init_one(&sg, vi->data, sizeof(vi->data)); |
---|
47 | 59 | |
---|
48 | 60 | /* There should always be room for one buffer. */ |
---|
49 | | - virtqueue_add_inbuf(vi->vq, &sg, 1, buf, GFP_KERNEL); |
---|
| 61 | + virtqueue_add_inbuf(vi->vq, &sg, 1, vi->data, GFP_KERNEL); |
---|
50 | 62 | |
---|
51 | 63 | virtqueue_kick(vi->vq); |
---|
| 64 | +} |
---|
| 65 | + |
---|
| 66 | +static unsigned int copy_data(struct virtrng_info *vi, void *buf, |
---|
| 67 | + unsigned int size) |
---|
| 68 | +{ |
---|
| 69 | + size = min_t(unsigned int, size, vi->data_avail); |
---|
| 70 | + memcpy(buf, vi->data + vi->data_idx, size); |
---|
| 71 | + vi->data_idx += size; |
---|
| 72 | + vi->data_avail -= size; |
---|
| 73 | + if (vi->data_avail == 0) |
---|
| 74 | + request_entropy(vi); |
---|
| 75 | + return size; |
---|
52 | 76 | } |
---|
53 | 77 | |
---|
54 | 78 | static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait) |
---|
55 | 79 | { |
---|
56 | 80 | int ret; |
---|
57 | 81 | struct virtrng_info *vi = (struct virtrng_info *)rng->priv; |
---|
| 82 | + unsigned int chunk; |
---|
| 83 | + size_t read; |
---|
58 | 84 | |
---|
59 | 85 | if (vi->hwrng_removed) |
---|
60 | 86 | return -ENODEV; |
---|
61 | 87 | |
---|
62 | | - if (!vi->busy) { |
---|
63 | | - vi->busy = true; |
---|
64 | | - reinit_completion(&vi->have_data); |
---|
65 | | - register_buffer(vi, buf, size); |
---|
| 88 | + read = 0; |
---|
| 89 | + |
---|
| 90 | + /* copy available data */ |
---|
| 91 | + if (smp_load_acquire(&vi->data_avail)) { |
---|
| 92 | + chunk = copy_data(vi, buf, size); |
---|
| 93 | + size -= chunk; |
---|
| 94 | + read += chunk; |
---|
66 | 95 | } |
---|
67 | 96 | |
---|
68 | 97 | if (!wait) |
---|
69 | | - return 0; |
---|
| 98 | + return read; |
---|
70 | 99 | |
---|
71 | | - ret = wait_for_completion_killable(&vi->have_data); |
---|
72 | | - if (ret < 0) |
---|
73 | | - return ret; |
---|
| 100 | + /* We have already copied available entropy, |
---|
| 101 | + * so either size is 0 or data_avail is 0 |
---|
| 102 | + */ |
---|
| 103 | + while (size != 0) { |
---|
| 104 | + /* data_avail is 0 but a request is pending */ |
---|
| 105 | + ret = wait_for_completion_killable(&vi->have_data); |
---|
| 106 | + if (ret < 0) |
---|
| 107 | + return ret; |
---|
| 108 | + /* if vi->data_avail is 0, we have been interrupted |
---|
| 109 | + * by a cleanup, but buffer stays in the queue |
---|
| 110 | + */ |
---|
| 111 | + if (vi->data_avail == 0) |
---|
| 112 | + return read; |
---|
74 | 113 | |
---|
75 | | - vi->busy = false; |
---|
| 114 | + chunk = copy_data(vi, buf + read, size); |
---|
| 115 | + size -= chunk; |
---|
| 116 | + read += chunk; |
---|
| 117 | + } |
---|
76 | 118 | |
---|
77 | | - return vi->data_avail; |
---|
| 119 | + return read; |
---|
78 | 120 | } |
---|
79 | 121 | |
---|
80 | 122 | static void virtio_cleanup(struct hwrng *rng) |
---|
81 | 123 | { |
---|
82 | 124 | struct virtrng_info *vi = (struct virtrng_info *)rng->priv; |
---|
83 | 125 | |
---|
84 | | - if (vi->busy) |
---|
85 | | - wait_for_completion(&vi->have_data); |
---|
| 126 | + complete(&vi->have_data); |
---|
86 | 127 | } |
---|
87 | 128 | |
---|
88 | 129 | static int probe_common(struct virtio_device *vdev) |
---|
.. | .. |
---|
118 | 159 | goto err_find; |
---|
119 | 160 | } |
---|
120 | 161 | |
---|
| 162 | + /* we always have a pending entropy request */ |
---|
| 163 | + request_entropy(vi); |
---|
| 164 | + |
---|
121 | 165 | return 0; |
---|
122 | 166 | |
---|
123 | 167 | err_find: |
---|
.. | .. |
---|
133 | 177 | |
---|
134 | 178 | vi->hwrng_removed = true; |
---|
135 | 179 | vi->data_avail = 0; |
---|
| 180 | + vi->data_idx = 0; |
---|
136 | 181 | complete(&vi->have_data); |
---|
137 | 182 | vdev->config->reset(vdev); |
---|
138 | | - vi->busy = false; |
---|
139 | 183 | if (vi->hwrng_register_done) |
---|
140 | 184 | hwrng_unregister(&vi->hwrng); |
---|
141 | 185 | vdev->config->del_vqs(vdev); |
---|