1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Randomness driver for virtio
4 * Copyright (C) 2007, 2008 Rusty Russell IBM Corporation
5 */
6
7 #include <asm/barrier.h>
8 #include <linux/err.h>
9 #include <linux/hw_random.h>
10 #include <linux/scatterlist.h>
11 #include <linux/spinlock.h>
12 #include <linux/virtio.h>
13 #include <linux/virtio_rng.h>
14 #include <linux/module.h>
15
16 static DEFINE_IDA(rng_index_ida);
17
18 struct virtrng_info {
19 struct hwrng hwrng;
20 struct virtqueue *vq;
21 char name[25];
22 int index;
23 bool hwrng_register_done;
24 bool hwrng_removed;
25 /* data transfer */
26 struct completion have_data;
27 unsigned int data_avail;
28 unsigned int data_idx;
29 /* minimal size returned by rng_buffer_size() */
30 #if SMP_CACHE_BYTES < 32
31 u8 data[32];
32 #else
33 u8 data[SMP_CACHE_BYTES];
34 #endif
35 };
36
random_recv_done(struct virtqueue * vq)37 static void random_recv_done(struct virtqueue *vq)
38 {
39 struct virtrng_info *vi = vq->vdev->priv;
40 unsigned int len;
41
42 /* We can get spurious callbacks, e.g. shared IRQs + virtio_pci. */
43 if (!virtqueue_get_buf(vi->vq, &len))
44 return;
45
46 smp_store_release(&vi->data_avail, len);
47 complete(&vi->have_data);
48 }
49
request_entropy(struct virtrng_info * vi)50 static void request_entropy(struct virtrng_info *vi)
51 {
52 struct scatterlist sg;
53
54 reinit_completion(&vi->have_data);
55 vi->data_idx = 0;
56
57 sg_init_one(&sg, vi->data, sizeof(vi->data));
58
59 /* There should always be room for one buffer. */
60 virtqueue_add_inbuf(vi->vq, &sg, 1, vi->data, GFP_KERNEL);
61
62 virtqueue_kick(vi->vq);
63 }
64
copy_data(struct virtrng_info * vi,void * buf,unsigned int size)65 static unsigned int copy_data(struct virtrng_info *vi, void *buf,
66 unsigned int size)
67 {
68 size = min_t(unsigned int, size, vi->data_avail);
69 memcpy(buf, vi->data + vi->data_idx, size);
70 vi->data_idx += size;
71 vi->data_avail -= size;
72 if (vi->data_avail == 0)
73 request_entropy(vi);
74 return size;
75 }
76
virtio_read(struct hwrng * rng,void * buf,size_t size,bool wait)77 static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait)
78 {
79 int ret;
80 struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
81 unsigned int chunk;
82 size_t read;
83
84 if (vi->hwrng_removed)
85 return -ENODEV;
86
87 read = 0;
88
89 /* copy available data */
90 if (smp_load_acquire(&vi->data_avail)) {
91 chunk = copy_data(vi, buf, size);
92 size -= chunk;
93 read += chunk;
94 }
95
96 if (!wait)
97 return read;
98
99 /* We have already copied available entropy,
100 * so either size is 0 or data_avail is 0
101 */
102 while (size != 0) {
103 /* data_avail is 0 but a request is pending */
104 ret = wait_for_completion_killable(&vi->have_data);
105 if (ret < 0)
106 return ret;
107 /* if vi->data_avail is 0, we have been interrupted
108 * by a cleanup, but buffer stays in the queue
109 */
110 if (vi->data_avail == 0)
111 return read;
112
113 chunk = copy_data(vi, buf + read, size);
114 size -= chunk;
115 read += chunk;
116 }
117
118 return read;
119 }
120
virtio_cleanup(struct hwrng * rng)121 static void virtio_cleanup(struct hwrng *rng)
122 {
123 struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
124
125 complete(&vi->have_data);
126 }
127
probe_common(struct virtio_device * vdev)128 static int probe_common(struct virtio_device *vdev)
129 {
130 int err, index;
131 struct virtrng_info *vi = NULL;
132
133 vi = kzalloc(sizeof(struct virtrng_info), GFP_KERNEL);
134 if (!vi)
135 return -ENOMEM;
136
137 vi->index = index = ida_simple_get(&rng_index_ida, 0, 0, GFP_KERNEL);
138 if (index < 0) {
139 err = index;
140 goto err_ida;
141 }
142 sprintf(vi->name, "virtio_rng.%d", index);
143 init_completion(&vi->have_data);
144
145 vi->hwrng = (struct hwrng) {
146 .read = virtio_read,
147 .cleanup = virtio_cleanup,
148 .priv = (unsigned long)vi,
149 .name = vi->name,
150 .quality = 1000,
151 };
152 vdev->priv = vi;
153
154 /* We expect a single virtqueue. */
155 vi->vq = virtio_find_single_vq(vdev, random_recv_done, "input");
156 if (IS_ERR(vi->vq)) {
157 err = PTR_ERR(vi->vq);
158 goto err_find;
159 }
160
161 /* we always have a pending entropy request */
162 request_entropy(vi);
163
164 return 0;
165
166 err_find:
167 ida_simple_remove(&rng_index_ida, index);
168 err_ida:
169 kfree(vi);
170 return err;
171 }
172
remove_common(struct virtio_device * vdev)173 static void remove_common(struct virtio_device *vdev)
174 {
175 struct virtrng_info *vi = vdev->priv;
176
177 vi->hwrng_removed = true;
178 vi->data_avail = 0;
179 vi->data_idx = 0;
180 complete(&vi->have_data);
181 vdev->config->reset(vdev);
182 if (vi->hwrng_register_done)
183 hwrng_unregister(&vi->hwrng);
184 vdev->config->del_vqs(vdev);
185 ida_simple_remove(&rng_index_ida, vi->index);
186 kfree(vi);
187 }
188
virtrng_probe(struct virtio_device * vdev)189 static int virtrng_probe(struct virtio_device *vdev)
190 {
191 return probe_common(vdev);
192 }
193
virtrng_remove(struct virtio_device * vdev)194 static void virtrng_remove(struct virtio_device *vdev)
195 {
196 remove_common(vdev);
197 }
198
virtrng_scan(struct virtio_device * vdev)199 static void virtrng_scan(struct virtio_device *vdev)
200 {
201 struct virtrng_info *vi = vdev->priv;
202 int err;
203
204 err = hwrng_register(&vi->hwrng);
205 if (!err)
206 vi->hwrng_register_done = true;
207 }
208
209 #ifdef CONFIG_PM_SLEEP
virtrng_freeze(struct virtio_device * vdev)210 static int virtrng_freeze(struct virtio_device *vdev)
211 {
212 remove_common(vdev);
213 return 0;
214 }
215
virtrng_restore(struct virtio_device * vdev)216 static int virtrng_restore(struct virtio_device *vdev)
217 {
218 int err;
219
220 err = probe_common(vdev);
221 if (!err) {
222 struct virtrng_info *vi = vdev->priv;
223
224 /*
225 * Set hwrng_removed to ensure that virtio_read()
226 * does not block waiting for data before the
227 * registration is complete.
228 */
229 vi->hwrng_removed = true;
230 err = hwrng_register(&vi->hwrng);
231 if (!err) {
232 vi->hwrng_register_done = true;
233 vi->hwrng_removed = false;
234 }
235 }
236
237 return err;
238 }
239 #endif
240
241 static struct virtio_device_id id_table[] = {
242 { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
243 { 0 },
244 };
245
246 static struct virtio_driver virtio_rng_driver = {
247 .driver.name = KBUILD_MODNAME,
248 .driver.owner = THIS_MODULE,
249 .id_table = id_table,
250 .probe = virtrng_probe,
251 .remove = virtrng_remove,
252 .scan = virtrng_scan,
253 #ifdef CONFIG_PM_SLEEP
254 .freeze = virtrng_freeze,
255 .restore = virtrng_restore,
256 #endif
257 };
258
259 module_virtio_driver(virtio_rng_driver);
260 MODULE_DEVICE_TABLE(virtio, id_table);
261 MODULE_DESCRIPTION("Virtio random number driver");
262 MODULE_LICENSE("GPL");
263