blob: 718d8c0876506c59e4d05b7ccc5ef950e431ed19 [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>
Rusty Russellf7f510e2008-05-30 15:09:44 -050014
Sasha Levina17597d2014-05-16 11:42:43 +093015static DEFINE_IDA(rng_index_ida);
Amos Kong08e53fb2014-05-14 10:33:46 +093016
17struct virtrng_info {
Amos Kong08e53fb2014-05-14 10:33:46 +093018 struct hwrng hwrng;
19 struct virtqueue *vq;
Amos Kong08e53fb2014-05-14 10:33:46 +093020 struct completion have_data;
Sasha Levina17597d2014-05-16 11:42:43 +093021 char name[25];
Amit Shah60628292014-07-27 07:33:01 +093022 unsigned int data_avail;
Sasha Levina17597d2014-05-16 11:42:43 +093023 int index;
Amit Shah60628292014-07-27 07:33:01 +093024 bool busy;
Amit Shah5c062732014-07-27 07:34:01 +093025 bool hwrng_register_done;
Amos Kongf4981952014-09-11 22:21:53 +093026 bool hwrng_removed;
Amos Kong08e53fb2014-05-14 10:33:46 +093027};
Rusty Russellf7f510e2008-05-30 15:09:44 -050028
29static void random_recv_done(struct virtqueue *vq)
30{
Amos Kong08e53fb2014-05-14 10:33:46 +093031 struct virtrng_info *vi = vq->vdev->priv;
32
Christian Borntraegere5b89542009-04-23 16:42:59 +093033 /* We can get spurious callbacks, e.g. shared IRQs + virtio_pci. */
Amos Kong08e53fb2014-05-14 10:33:46 +093034 if (!virtqueue_get_buf(vi->vq, &vi->data_avail))
Christian Borntraegere5b89542009-04-23 16:42:59 +093035 return;
Rusty Russellf7f510e2008-05-30 15:09:44 -050036
Amos Kong08e53fb2014-05-14 10:33:46 +093037 complete(&vi->have_data);
Rusty Russellf7f510e2008-05-30 15:09:44 -050038}
39
Ian Moltonbb347d92009-12-01 15:26:33 +080040/* The host will fill any buffer we give it with sweet, sweet randomness. */
Amos Kong08e53fb2014-05-14 10:33:46 +093041static void register_buffer(struct virtrng_info *vi, u8 *buf, size_t size)
Rusty Russellf7f510e2008-05-30 15:09:44 -050042{
43 struct scatterlist sg;
44
Ian Moltonbb347d92009-12-01 15:26:33 +080045 sg_init_one(&sg, buf, size);
46
Rusty Russellf7f510e2008-05-30 15:09:44 -050047 /* There should always be room for one buffer. */
Amos Kong08e53fb2014-05-14 10:33:46 +093048 virtqueue_add_inbuf(vi->vq, &sg, 1, buf, GFP_KERNEL);
Ian Moltonbb347d92009-12-01 15:26:33 +080049
Amos Kong08e53fb2014-05-14 10:33:46 +093050 virtqueue_kick(vi->vq);
Rusty Russellf7f510e2008-05-30 15:09:44 -050051}
52
Ian Moltonbb347d92009-12-01 15:26:33 +080053static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait)
Rusty Russellf7f510e2008-05-30 15:09:44 -050054{
Amit Shahcc8744e2012-05-28 12:18:40 +053055 int ret;
Amos Kong08e53fb2014-05-14 10:33:46 +093056 struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
Rusty Russellf7f510e2008-05-30 15:09:44 -050057
Amos Kongf4981952014-09-11 22:21:53 +093058 if (vi->hwrng_removed)
59 return -ENODEV;
60
Amos Kong08e53fb2014-05-14 10:33:46 +093061 if (!vi->busy) {
62 vi->busy = true;
David Tolnayaef027d2019-01-07 14:36:11 -080063 reinit_completion(&vi->have_data);
Amos Kong08e53fb2014-05-14 10:33:46 +093064 register_buffer(vi, buf, size);
Ian Moltonbb347d92009-12-01 15:26:33 +080065 }
66
Rusty Russellf7f510e2008-05-30 15:09:44 -050067 if (!wait)
68 return 0;
69
Amos Kong08e53fb2014-05-14 10:33:46 +093070 ret = wait_for_completion_killable(&vi->have_data);
Amit Shahcc8744e2012-05-28 12:18:40 +053071 if (ret < 0)
72 return ret;
Rusty Russell594de1d2009-06-12 22:16:39 -060073
Amos Kong08e53fb2014-05-14 10:33:46 +093074 vi->busy = false;
Rusty Russell594de1d2009-06-12 22:16:39 -060075
Amos Kong08e53fb2014-05-14 10:33:46 +093076 return vi->data_avail;
Rusty Russellf7f510e2008-05-30 15:09:44 -050077}
78
Ian Moltonbb347d92009-12-01 15:26:33 +080079static void virtio_cleanup(struct hwrng *rng)
Rusty Russellf7f510e2008-05-30 15:09:44 -050080{
Amos Kong08e53fb2014-05-14 10:33:46 +093081 struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
82
83 if (vi->busy)
84 wait_for_completion(&vi->have_data);
Rusty Russellf7f510e2008-05-30 15:09:44 -050085}
86
Amit Shah178d8552012-05-28 12:18:42 +053087static int probe_common(struct virtio_device *vdev)
Rusty Russellf7f510e2008-05-30 15:09:44 -050088{
Sasha Levina17597d2014-05-16 11:42:43 +093089 int err, index;
Amos Kong08e53fb2014-05-14 10:33:46 +093090 struct virtrng_info *vi = NULL;
Rusty Russellf7f510e2008-05-30 15:09:44 -050091
Sasha Levine5d23a82014-05-16 11:41:57 +093092 vi = kzalloc(sizeof(struct virtrng_info), GFP_KERNEL);
Sasha Levina17597d2014-05-16 11:42:43 +093093 if (!vi)
94 return -ENOMEM;
95
96 vi->index = index = ida_simple_get(&rng_index_ida, 0, 0, GFP_KERNEL);
97 if (index < 0) {
Michael S. Tsirkin1bbc2602014-10-15 10:22:33 +103098 err = index;
99 goto err_ida;
Sasha Levina17597d2014-05-16 11:42:43 +0930100 }
101 sprintf(vi->name, "virtio_rng.%d", index);
Amos Kong08e53fb2014-05-14 10:33:46 +0930102 init_completion(&vi->have_data);
103
Sasha Levina17597d2014-05-16 11:42:43 +0930104 vi->hwrng = (struct hwrng) {
105 .read = virtio_read,
106 .cleanup = virtio_cleanup,
107 .priv = (unsigned long)vi,
108 .name = vi->name,
Amit Shah34679ec2014-08-12 00:05:41 +0530109 .quality = 1000,
Sasha Levina17597d2014-05-16 11:42:43 +0930110 };
Amos Kong08e53fb2014-05-14 10:33:46 +0930111 vdev->priv = vi;
112
Rusty Russellf7f510e2008-05-30 15:09:44 -0500113 /* We expect a single virtqueue. */
Amos Kong08e53fb2014-05-14 10:33:46 +0930114 vi->vq = virtio_find_single_vq(vdev, random_recv_done, "input");
115 if (IS_ERR(vi->vq)) {
116 err = PTR_ERR(vi->vq);
Michael S. Tsirkin1bbc2602014-10-15 10:22:33 +1030117 goto err_find;
Amit Shahe84e7a52013-03-08 11:30:18 +1100118 }
Rusty Russellf7f510e2008-05-30 15:09:44 -0500119
Rusty Russellf7f510e2008-05-30 15:09:44 -0500120 return 0;
Michael S. Tsirkin1bbc2602014-10-15 10:22:33 +1030121
122err_find:
123 ida_simple_remove(&rng_index_ida, index);
124err_ida:
125 kfree(vi);
126 return err;
Rusty Russellf7f510e2008-05-30 15:09:44 -0500127}
128
Amit Shah178d8552012-05-28 12:18:42 +0530129static void remove_common(struct virtio_device *vdev)
Rusty Russellf7f510e2008-05-30 15:09:44 -0500130{
Amos Kong08e53fb2014-05-14 10:33:46 +0930131 struct virtrng_info *vi = vdev->priv;
Amit Shah5c062732014-07-27 07:34:01 +0930132
Amos Kongf4981952014-09-11 22:21:53 +0930133 vi->hwrng_removed = true;
Amos Kong3856e542014-09-11 22:20:39 +0930134 vi->data_avail = 0;
135 complete(&vi->have_data);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500136 vdev->config->reset(vdev);
Amos Kong08e53fb2014-05-14 10:33:46 +0930137 vi->busy = false;
Amit Shah5c062732014-07-27 07:34:01 +0930138 if (vi->hwrng_register_done)
139 hwrng_unregister(&vi->hwrng);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600140 vdev->config->del_vqs(vdev);
Sasha Levina17597d2014-05-16 11:42:43 +0930141 ida_simple_remove(&rng_index_ida, vi->index);
Amos Kong08e53fb2014-05-14 10:33:46 +0930142 kfree(vi);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500143}
144
Amit Shah178d8552012-05-28 12:18:42 +0530145static int virtrng_probe(struct virtio_device *vdev)
146{
147 return probe_common(vdev);
148}
149
Bill Pemberton39af33f2012-11-19 13:26:26 -0500150static void virtrng_remove(struct virtio_device *vdev)
Amit Shah178d8552012-05-28 12:18:42 +0530151{
152 remove_common(vdev);
153}
154
Amit Shah5c062732014-07-27 07:34:01 +0930155static void virtrng_scan(struct virtio_device *vdev)
156{
157 struct virtrng_info *vi = vdev->priv;
158 int err;
159
160 err = hwrng_register(&vi->hwrng);
161 if (!err)
162 vi->hwrng_register_done = true;
163}
164
Aaron Lu89107002013-09-17 09:25:23 +0930165#ifdef CONFIG_PM_SLEEP
Amit Shah0bc1a2e2012-05-28 12:18:43 +0530166static int virtrng_freeze(struct virtio_device *vdev)
167{
168 remove_common(vdev);
169 return 0;
170}
171
172static int virtrng_restore(struct virtio_device *vdev)
173{
Jim Quigleye5cc6e72017-11-03 09:57:21 +0000174 int err;
175
176 err = probe_common(vdev);
177 if (!err) {
178 struct virtrng_info *vi = vdev->priv;
179
180 /*
181 * Set hwrng_removed to ensure that virtio_read()
182 * does not block waiting for data before the
183 * registration is complete.
184 */
185 vi->hwrng_removed = true;
186 err = hwrng_register(&vi->hwrng);
187 if (!err) {
188 vi->hwrng_register_done = true;
189 vi->hwrng_removed = false;
190 }
191 }
192
193 return err;
Amit Shah0bc1a2e2012-05-28 12:18:43 +0530194}
195#endif
196
Rusty Russellf7f510e2008-05-30 15:09:44 -0500197static struct virtio_device_id id_table[] = {
198 { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
199 { 0 },
200};
201
Jeff Mahoneyd817cd52010-01-15 17:01:26 -0800202static struct virtio_driver virtio_rng_driver = {
Rusty Russellf7f510e2008-05-30 15:09:44 -0500203 .driver.name = KBUILD_MODNAME,
204 .driver.owner = THIS_MODULE,
205 .id_table = id_table,
206 .probe = virtrng_probe,
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -0800207 .remove = virtrng_remove,
Amit Shah5c062732014-07-27 07:34:01 +0930208 .scan = virtrng_scan,
Aaron Lu89107002013-09-17 09:25:23 +0930209#ifdef CONFIG_PM_SLEEP
Amit Shah0bc1a2e2012-05-28 12:18:43 +0530210 .freeze = virtrng_freeze,
211 .restore = virtrng_restore,
212#endif
Rusty Russellf7f510e2008-05-30 15:09:44 -0500213};
214
Rusty Russellb2a17022013-02-13 16:59:28 +1030215module_virtio_driver(virtio_rng_driver);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500216MODULE_DEVICE_TABLE(virtio, id_table);
217MODULE_DESCRIPTION("Virtio random number driver");
218MODULE_LICENSE("GPL");