blob: 0a7dde135db19a84df0c809370bce039ada2be59 [file] [log] [blame]
Thomas Gleixnerfd534e92019-05-23 11:14:39 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Rusty Russellf7f510e2008-05-30 15:09:44 -05002/*
3 * Randomness driver for virtio
4 * Copyright (C) 2007, 2008 Rusty Russell IBM Corporation
Rusty Russellf7f510e2008-05-30 15:09:44 -05005 */
Ian Moltonbb347d92009-12-01 15:26:33 +08006
Rusty Russellf7f510e2008-05-30 15:09:44 -05007#include <linux/err.h>
8#include <linux/hw_random.h>
9#include <linux/scatterlist.h>
10#include <linux/spinlock.h>
11#include <linux/virtio.h>
12#include <linux/virtio_rng.h>
Paul Gortmakerc22405c2011-07-03 13:35:48 -040013#include <linux/module.h>
Michael S. Tsirkinb69df6c2020-04-06 20:37:55 -040014#include <linux/slab.h>
Rusty Russellf7f510e2008-05-30 15:09:44 -050015
Sasha Levina17597d2014-05-16 11:42:43 +093016static DEFINE_IDA(rng_index_ida);
Amos Kong08e53fb2014-05-14 10:33:46 +093017
18struct virtrng_info {
Amos Kong08e53fb2014-05-14 10:33:46 +093019 struct hwrng hwrng;
20 struct virtqueue *vq;
Sasha Levina17597d2014-05-16 11:42:43 +093021 char name[25];
22 int index;
Amit Shah5c062732014-07-27 07:34:01 +093023 bool hwrng_register_done;
Amos Kongf4981952014-09-11 22:21:53 +093024 bool hwrng_removed;
Laurent Vivierbf3175b2021-10-28 12:11:08 +020025 /* data transfer */
26 struct completion have_data;
27 unsigned int data_avail;
Laurent Vivier5c8e9332021-10-28 12:11:10 +020028 unsigned int data_idx;
Laurent Vivierbf3175b2021-10-28 12:11:08 +020029 /* 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
Amos Kong08e53fb2014-05-14 10:33:46 +093035};
Rusty Russellf7f510e2008-05-30 15:09:44 -050036
37static void random_recv_done(struct virtqueue *vq)
38{
Amos Kong08e53fb2014-05-14 10:33:46 +093039 struct virtrng_info *vi = vq->vdev->priv;
40
Christian Borntraegere5b89542009-04-23 16:42:59 +093041 /* We can get spurious callbacks, e.g. shared IRQs + virtio_pci. */
Amos Kong08e53fb2014-05-14 10:33:46 +093042 if (!virtqueue_get_buf(vi->vq, &vi->data_avail))
Christian Borntraegere5b89542009-04-23 16:42:59 +093043 return;
Rusty Russellf7f510e2008-05-30 15:09:44 -050044
Laurent Vivier5c8e9332021-10-28 12:11:10 +020045 vi->data_idx = 0;
Laurent Vivier5c8e9332021-10-28 12:11:10 +020046
Amos Kong08e53fb2014-05-14 10:33:46 +093047 complete(&vi->have_data);
Rusty Russellf7f510e2008-05-30 15:09:44 -050048}
49
Laurent Vivier9a4b6122021-10-28 12:11:11 +020050static void request_entropy(struct virtrng_info *vi)
Rusty Russellf7f510e2008-05-30 15:09:44 -050051{
52 struct scatterlist sg;
53
Laurent Vivier9a4b6122021-10-28 12:11:11 +020054 reinit_completion(&vi->have_data);
55 vi->data_avail = 0;
56 vi->data_idx = 0;
57
Laurent Vivierbf3175b2021-10-28 12:11:08 +020058 sg_init_one(&sg, vi->data, sizeof(vi->data));
Ian Moltonbb347d92009-12-01 15:26:33 +080059
Rusty Russellf7f510e2008-05-30 15:09:44 -050060 /* There should always be room for one buffer. */
Laurent Vivierbf3175b2021-10-28 12:11:08 +020061 virtqueue_add_inbuf(vi->vq, &sg, 1, vi->data, GFP_KERNEL);
Ian Moltonbb347d92009-12-01 15:26:33 +080062
Amos Kong08e53fb2014-05-14 10:33:46 +093063 virtqueue_kick(vi->vq);
Rusty Russellf7f510e2008-05-30 15:09:44 -050064}
65
Laurent Vivier5c8e9332021-10-28 12:11:10 +020066static 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;
Laurent Vivier9a4b6122021-10-28 12:11:11 +020073 if (vi->data_avail == 0)
74 request_entropy(vi);
Laurent Vivier5c8e9332021-10-28 12:11:10 +020075 return size;
76}
77
Ian Moltonbb347d92009-12-01 15:26:33 +080078static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait)
Rusty Russellf7f510e2008-05-30 15:09:44 -050079{
Amit Shahcc8744e2012-05-28 12:18:40 +053080 int ret;
Amos Kong08e53fb2014-05-14 10:33:46 +093081 struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
Laurent Vivierbf3175b2021-10-28 12:11:08 +020082 unsigned int chunk;
83 size_t read;
Rusty Russellf7f510e2008-05-30 15:09:44 -050084
Amos Kongf4981952014-09-11 22:21:53 +093085 if (vi->hwrng_removed)
86 return -ENODEV;
87
Laurent Vivier5c8e9332021-10-28 12:11:10 +020088 read = 0;
89
90 /* copy available data */
91 if (vi->data_avail) {
92 chunk = copy_data(vi, buf, size);
93 size -= chunk;
94 read += chunk;
Ian Moltonbb347d92009-12-01 15:26:33 +080095 }
96
Rusty Russellf7f510e2008-05-30 15:09:44 -050097 if (!wait)
Laurent Vivier5c8e9332021-10-28 12:11:10 +020098 return read;
Rusty Russellf7f510e2008-05-30 15:09:44 -050099
Laurent Vivier5c8e9332021-10-28 12:11:10 +0200100 /* We have already copied available entropy,
101 * so either size is 0 or data_avail is 0
102 */
Laurent Vivierbf3175b2021-10-28 12:11:08 +0200103 while (size != 0) {
Laurent Vivier9a4b6122021-10-28 12:11:11 +0200104 /* data_avail is 0 but a request is pending */
Laurent Vivierbf3175b2021-10-28 12:11:08 +0200105 ret = wait_for_completion_killable(&vi->have_data);
106 if (ret < 0)
107 return ret;
Laurent Vivier2bb31ab2021-10-28 12:11:09 +0200108 /* 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;
Laurent Vivierbf3175b2021-10-28 12:11:08 +0200113
Laurent Vivier5c8e9332021-10-28 12:11:10 +0200114 chunk = copy_data(vi, buf + read, size);
Laurent Vivierbf3175b2021-10-28 12:11:08 +0200115 size -= chunk;
Laurent Vivier5c8e9332021-10-28 12:11:10 +0200116 read += chunk;
Laurent Vivierbf3175b2021-10-28 12:11:08 +0200117 }
Rusty Russell594de1d2009-06-12 22:16:39 -0600118
Laurent Vivierbf3175b2021-10-28 12:11:08 +0200119 return read;
Rusty Russellf7f510e2008-05-30 15:09:44 -0500120}
121
Ian Moltonbb347d92009-12-01 15:26:33 +0800122static void virtio_cleanup(struct hwrng *rng)
Rusty Russellf7f510e2008-05-30 15:09:44 -0500123{
Amos Kong08e53fb2014-05-14 10:33:46 +0930124 struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
125
Laurent Vivier9a4b6122021-10-28 12:11:11 +0200126 complete(&vi->have_data);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500127}
128
Amit Shah178d8552012-05-28 12:18:42 +0530129static int probe_common(struct virtio_device *vdev)
Rusty Russellf7f510e2008-05-30 15:09:44 -0500130{
Sasha Levina17597d2014-05-16 11:42:43 +0930131 int err, index;
Amos Kong08e53fb2014-05-14 10:33:46 +0930132 struct virtrng_info *vi = NULL;
Rusty Russellf7f510e2008-05-30 15:09:44 -0500133
Sasha Levine5d23a82014-05-16 11:41:57 +0930134 vi = kzalloc(sizeof(struct virtrng_info), GFP_KERNEL);
Sasha Levina17597d2014-05-16 11:42:43 +0930135 if (!vi)
136 return -ENOMEM;
137
138 vi->index = index = ida_simple_get(&rng_index_ida, 0, 0, GFP_KERNEL);
139 if (index < 0) {
Michael S. Tsirkin1bbc2602014-10-15 10:22:33 +1030140 err = index;
141 goto err_ida;
Sasha Levina17597d2014-05-16 11:42:43 +0930142 }
143 sprintf(vi->name, "virtio_rng.%d", index);
Amos Kong08e53fb2014-05-14 10:33:46 +0930144 init_completion(&vi->have_data);
145
Sasha Levina17597d2014-05-16 11:42:43 +0930146 vi->hwrng = (struct hwrng) {
147 .read = virtio_read,
148 .cleanup = virtio_cleanup,
149 .priv = (unsigned long)vi,
150 .name = vi->name,
Amit Shah34679ec2014-08-12 00:05:41 +0530151 .quality = 1000,
Sasha Levina17597d2014-05-16 11:42:43 +0930152 };
Amos Kong08e53fb2014-05-14 10:33:46 +0930153 vdev->priv = vi;
154
Rusty Russellf7f510e2008-05-30 15:09:44 -0500155 /* We expect a single virtqueue. */
Amos Kong08e53fb2014-05-14 10:33:46 +0930156 vi->vq = virtio_find_single_vq(vdev, random_recv_done, "input");
157 if (IS_ERR(vi->vq)) {
158 err = PTR_ERR(vi->vq);
Michael S. Tsirkin1bbc2602014-10-15 10:22:33 +1030159 goto err_find;
Amit Shahe84e7a52013-03-08 11:30:18 +1100160 }
Rusty Russellf7f510e2008-05-30 15:09:44 -0500161
Laurent Vivier9a4b6122021-10-28 12:11:11 +0200162 /* we always have a pending entropy request */
163 request_entropy(vi);
164
Rusty Russellf7f510e2008-05-30 15:09:44 -0500165 return 0;
Michael S. Tsirkin1bbc2602014-10-15 10:22:33 +1030166
167err_find:
168 ida_simple_remove(&rng_index_ida, index);
169err_ida:
170 kfree(vi);
171 return err;
Rusty Russellf7f510e2008-05-30 15:09:44 -0500172}
173
Amit Shah178d8552012-05-28 12:18:42 +0530174static void remove_common(struct virtio_device *vdev)
Rusty Russellf7f510e2008-05-30 15:09:44 -0500175{
Amos Kong08e53fb2014-05-14 10:33:46 +0930176 struct virtrng_info *vi = vdev->priv;
Amit Shah5c062732014-07-27 07:34:01 +0930177
Amos Kongf4981952014-09-11 22:21:53 +0930178 vi->hwrng_removed = true;
Amos Kong3856e542014-09-11 22:20:39 +0930179 vi->data_avail = 0;
Laurent Vivier5c8e9332021-10-28 12:11:10 +0200180 vi->data_idx = 0;
Amos Kong3856e542014-09-11 22:20:39 +0930181 complete(&vi->have_data);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500182 vdev->config->reset(vdev);
Amit Shah5c062732014-07-27 07:34:01 +0930183 if (vi->hwrng_register_done)
184 hwrng_unregister(&vi->hwrng);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600185 vdev->config->del_vqs(vdev);
Sasha Levina17597d2014-05-16 11:42:43 +0930186 ida_simple_remove(&rng_index_ida, vi->index);
Amos Kong08e53fb2014-05-14 10:33:46 +0930187 kfree(vi);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500188}
189
Amit Shah178d8552012-05-28 12:18:42 +0530190static int virtrng_probe(struct virtio_device *vdev)
191{
192 return probe_common(vdev);
193}
194
Bill Pemberton39af33f2012-11-19 13:26:26 -0500195static void virtrng_remove(struct virtio_device *vdev)
Amit Shah178d8552012-05-28 12:18:42 +0530196{
197 remove_common(vdev);
198}
199
Amit Shah5c062732014-07-27 07:34:01 +0930200static void virtrng_scan(struct virtio_device *vdev)
201{
202 struct virtrng_info *vi = vdev->priv;
203 int err;
204
205 err = hwrng_register(&vi->hwrng);
206 if (!err)
207 vi->hwrng_register_done = true;
208}
209
Aaron Lu89107002013-09-17 09:25:23 +0930210#ifdef CONFIG_PM_SLEEP
Amit Shah0bc1a2e2012-05-28 12:18:43 +0530211static int virtrng_freeze(struct virtio_device *vdev)
212{
213 remove_common(vdev);
214 return 0;
215}
216
217static int virtrng_restore(struct virtio_device *vdev)
218{
Jim Quigleye5cc6e72017-11-03 09:57:21 +0000219 int err;
220
221 err = probe_common(vdev);
222 if (!err) {
223 struct virtrng_info *vi = vdev->priv;
224
225 /*
226 * Set hwrng_removed to ensure that virtio_read()
227 * does not block waiting for data before the
228 * registration is complete.
229 */
230 vi->hwrng_removed = true;
231 err = hwrng_register(&vi->hwrng);
232 if (!err) {
233 vi->hwrng_register_done = true;
234 vi->hwrng_removed = false;
235 }
236 }
237
238 return err;
Amit Shah0bc1a2e2012-05-28 12:18:43 +0530239}
240#endif
241
Rikard Falkeborna024d702020-07-01 22:09:48 +0200242static const struct virtio_device_id id_table[] = {
Rusty Russellf7f510e2008-05-30 15:09:44 -0500243 { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
244 { 0 },
245};
246
Jeff Mahoneyd817cd52010-01-15 17:01:26 -0800247static struct virtio_driver virtio_rng_driver = {
Rusty Russellf7f510e2008-05-30 15:09:44 -0500248 .driver.name = KBUILD_MODNAME,
249 .driver.owner = THIS_MODULE,
250 .id_table = id_table,
251 .probe = virtrng_probe,
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -0800252 .remove = virtrng_remove,
Amit Shah5c062732014-07-27 07:34:01 +0930253 .scan = virtrng_scan,
Aaron Lu89107002013-09-17 09:25:23 +0930254#ifdef CONFIG_PM_SLEEP
Amit Shah0bc1a2e2012-05-28 12:18:43 +0530255 .freeze = virtrng_freeze,
256 .restore = virtrng_restore,
257#endif
Rusty Russellf7f510e2008-05-30 15:09:44 -0500258};
259
Rusty Russellb2a17022013-02-13 16:59:28 +1030260module_virtio_driver(virtio_rng_driver);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500261MODULE_DEVICE_TABLE(virtio, id_table);
262MODULE_DESCRIPTION("Virtio random number driver");
263MODULE_LICENSE("GPL");