]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/char/hw_random/virtio-rng.c
random: ensure early RDSEED goes through mixer on init
[mirror_ubuntu-jammy-kernel.git] / drivers / char / hw_random / virtio-rng.c
CommitLineData
fd534e9b 1// SPDX-License-Identifier: GPL-2.0-or-later
f7f510ec
RR
2/*
3 * Randomness driver for virtio
4 * Copyright (C) 2007, 2008 Rusty Russell IBM Corporation
f7f510ec 5 */
bb347d98 6
f7f510ec
RR
7#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>
c22405c9 13#include <linux/module.h>
b69df6cd 14#include <linux/slab.h>
f7f510ec 15
a17597d3 16static DEFINE_IDA(rng_index_ida);
08e53fbd
AK
17
18struct virtrng_info {
08e53fbd
AK
19 struct hwrng hwrng;
20 struct virtqueue *vq;
08e53fbd 21 struct completion have_data;
a17597d3 22 char name[25];
6062829f 23 unsigned int data_avail;
a17597d3 24 int index;
6062829f 25 bool busy;
5c062734 26 bool hwrng_register_done;
f4981956 27 bool hwrng_removed;
08e53fbd 28};
f7f510ec
RR
29
30static void random_recv_done(struct virtqueue *vq)
31{
08e53fbd
AK
32 struct virtrng_info *vi = vq->vdev->priv;
33
e5b89542 34 /* We can get spurious callbacks, e.g. shared IRQs + virtio_pci. */
08e53fbd 35 if (!virtqueue_get_buf(vi->vq, &vi->data_avail))
e5b89542 36 return;
f7f510ec 37
08e53fbd 38 complete(&vi->have_data);
f7f510ec
RR
39}
40
bb347d98 41/* The host will fill any buffer we give it with sweet, sweet randomness. */
08e53fbd 42static void register_buffer(struct virtrng_info *vi, u8 *buf, size_t size)
f7f510ec
RR
43{
44 struct scatterlist sg;
45
bb347d98
IM
46 sg_init_one(&sg, buf, size);
47
f7f510ec 48 /* There should always be room for one buffer. */
08e53fbd 49 virtqueue_add_inbuf(vi->vq, &sg, 1, buf, GFP_KERNEL);
bb347d98 50
08e53fbd 51 virtqueue_kick(vi->vq);
f7f510ec
RR
52}
53
bb347d98 54static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait)
f7f510ec 55{
cc8744e1 56 int ret;
08e53fbd 57 struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
f7f510ec 58
f4981956
AK
59 if (vi->hwrng_removed)
60 return -ENODEV;
61
08e53fbd
AK
62 if (!vi->busy) {
63 vi->busy = true;
aef027db 64 reinit_completion(&vi->have_data);
08e53fbd 65 register_buffer(vi, buf, size);
bb347d98
IM
66 }
67
f7f510ec
RR
68 if (!wait)
69 return 0;
70
08e53fbd 71 ret = wait_for_completion_killable(&vi->have_data);
cc8744e1
AS
72 if (ret < 0)
73 return ret;
594de1dd 74
08e53fbd 75 vi->busy = false;
594de1dd 76
08e53fbd 77 return vi->data_avail;
f7f510ec
RR
78}
79
bb347d98 80static void virtio_cleanup(struct hwrng *rng)
f7f510ec 81{
08e53fbd 82 struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
bb347d98 83
08e53fbd
AK
84 if (vi->busy)
85 wait_for_completion(&vi->have_data);
86}
f7f510ec 87
178d855e 88static int probe_common(struct virtio_device *vdev)
f7f510ec 89{
a17597d3 90 int err, index;
08e53fbd
AK
91 struct virtrng_info *vi = NULL;
92
e5d23a8c 93 vi = kzalloc(sizeof(struct virtrng_info), GFP_KERNEL);
a17597d3
SL
94 if (!vi)
95 return -ENOMEM;
96
97 vi->index = index = ida_simple_get(&rng_index_ida, 0, 0, GFP_KERNEL);
98 if (index < 0) {
1bbc2606
MT
99 err = index;
100 goto err_ida;
a17597d3
SL
101 }
102 sprintf(vi->name, "virtio_rng.%d", index);
08e53fbd
AK
103 init_completion(&vi->have_data);
104
a17597d3
SL
105 vi->hwrng = (struct hwrng) {
106 .read = virtio_read,
107 .cleanup = virtio_cleanup,
108 .priv = (unsigned long)vi,
109 .name = vi->name,
34679ec7 110 .quality = 1000,
a17597d3 111 };
08e53fbd 112 vdev->priv = vi;
f7f510ec
RR
113
114 /* We expect a single virtqueue. */
08e53fbd
AK
115 vi->vq = virtio_find_single_vq(vdev, random_recv_done, "input");
116 if (IS_ERR(vi->vq)) {
117 err = PTR_ERR(vi->vq);
1bbc2606 118 goto err_find;
e84e7a56 119 }
f7f510ec 120
f7f510ec 121 return 0;
1bbc2606
MT
122
123err_find:
124 ida_simple_remove(&rng_index_ida, index);
125err_ida:
126 kfree(vi);
127 return err;
f7f510ec
RR
128}
129
178d855e 130static void remove_common(struct virtio_device *vdev)
f7f510ec 131{
08e53fbd 132 struct virtrng_info *vi = vdev->priv;
5c062734 133
f4981956 134 vi->hwrng_removed = true;
3856e548
AK
135 vi->data_avail = 0;
136 complete(&vi->have_data);
f7f510ec 137 vdev->config->reset(vdev);
08e53fbd 138 vi->busy = false;
5c062734
AS
139 if (vi->hwrng_register_done)
140 hwrng_unregister(&vi->hwrng);
d2a7ddda 141 vdev->config->del_vqs(vdev);
a17597d3 142 ida_simple_remove(&rng_index_ida, vi->index);
08e53fbd 143 kfree(vi);
f7f510ec
RR
144}
145
178d855e
AS
146static int virtrng_probe(struct virtio_device *vdev)
147{
148 return probe_common(vdev);
149}
150
39af33fc 151static void virtrng_remove(struct virtio_device *vdev)
178d855e
AS
152{
153 remove_common(vdev);
154}
155
5c062734
AS
156static void virtrng_scan(struct virtio_device *vdev)
157{
158 struct virtrng_info *vi = vdev->priv;
159 int err;
160
161 err = hwrng_register(&vi->hwrng);
162 if (!err)
163 vi->hwrng_register_done = true;
164}
165
89107000 166#ifdef CONFIG_PM_SLEEP
0bc1a2ef
AS
167static int virtrng_freeze(struct virtio_device *vdev)
168{
169 remove_common(vdev);
170 return 0;
171}
172
173static int virtrng_restore(struct virtio_device *vdev)
174{
e5cc6e79
JQ
175 int err;
176
177 err = probe_common(vdev);
178 if (!err) {
179 struct virtrng_info *vi = vdev->priv;
180
181 /*
182 * Set hwrng_removed to ensure that virtio_read()
183 * does not block waiting for data before the
184 * registration is complete.
185 */
186 vi->hwrng_removed = true;
187 err = hwrng_register(&vi->hwrng);
188 if (!err) {
189 vi->hwrng_register_done = true;
190 vi->hwrng_removed = false;
191 }
192 }
193
194 return err;
0bc1a2ef
AS
195}
196#endif
197
a024d70a 198static const struct virtio_device_id id_table[] = {
f7f510ec
RR
199 { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
200 { 0 },
201};
202
d817cd52 203static struct virtio_driver virtio_rng_driver = {
f7f510ec
RR
204 .driver.name = KBUILD_MODNAME,
205 .driver.owner = THIS_MODULE,
206 .id_table = id_table,
207 .probe = virtrng_probe,
bcd2982a 208 .remove = virtrng_remove,
5c062734 209 .scan = virtrng_scan,
89107000 210#ifdef CONFIG_PM_SLEEP
0bc1a2ef
AS
211 .freeze = virtrng_freeze,
212 .restore = virtrng_restore,
213#endif
f7f510ec
RR
214};
215
b2a17029 216module_virtio_driver(virtio_rng_driver);
f7f510ec
RR
217MODULE_DEVICE_TABLE(virtio, id_table);
218MODULE_DESCRIPTION("Virtio random number driver");
219MODULE_LICENSE("GPL");