blob: 2ce0e225e58c3edbceeafa290c8db065eb4e6e72 [file] [log] [blame]
Rusty Russellf7f510e2008-05-30 15:09:44 -05001/*
2 * Randomness driver for virtio
3 * Copyright (C) 2007, 2008 Rusty Russell IBM Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
Ian Moltonbb347d92009-12-01 15:26:33 +080019
Rusty Russellf7f510e2008-05-30 15:09:44 -050020#include <linux/err.h>
21#include <linux/hw_random.h>
22#include <linux/scatterlist.h>
23#include <linux/spinlock.h>
24#include <linux/virtio.h>
25#include <linux/virtio_rng.h>
Paul Gortmakerc22405c2011-07-03 13:35:48 -040026#include <linux/module.h>
Rusty Russellf7f510e2008-05-30 15:09:44 -050027
Rusty Russellf7f510e2008-05-30 15:09:44 -050028static struct virtqueue *vq;
Ian Moltonbb347d92009-12-01 15:26:33 +080029static unsigned int data_avail;
Rusty Russellf7f510e2008-05-30 15:09:44 -050030static DECLARE_COMPLETION(have_data);
Ian Moltonbb347d92009-12-01 15:26:33 +080031static bool busy;
Rusty Russellf7f510e2008-05-30 15:09:44 -050032
33static void random_recv_done(struct virtqueue *vq)
34{
Christian Borntraegere5b89542009-04-23 16:42:59 +093035 /* We can get spurious callbacks, e.g. shared IRQs + virtio_pci. */
Michael S. Tsirkin28cfc822010-04-13 16:11:42 +030036 if (!virtqueue_get_buf(vq, &data_avail))
Christian Borntraegere5b89542009-04-23 16:42:59 +093037 return;
Rusty Russellf7f510e2008-05-30 15:09:44 -050038
Rusty Russellf7f510e2008-05-30 15:09:44 -050039 complete(&have_data);
40}
41
Ian Moltonbb347d92009-12-01 15:26:33 +080042/* The host will fill any buffer we give it with sweet, sweet randomness. */
43static void register_buffer(u8 *buf, size_t size)
Rusty Russellf7f510e2008-05-30 15:09:44 -050044{
45 struct scatterlist sg;
46
Ian Moltonbb347d92009-12-01 15:26:33 +080047 sg_init_one(&sg, buf, size);
48
Rusty Russellf7f510e2008-05-30 15:09:44 -050049 /* There should always be room for one buffer. */
Rusty Russell9914a762014-03-13 11:23:40 +103050 virtqueue_add_inbuf(vq, &sg, 1, buf, GFP_KERNEL);
Ian Moltonbb347d92009-12-01 15:26:33 +080051
Michael S. Tsirkin28cfc822010-04-13 16:11:42 +030052 virtqueue_kick(vq);
Rusty Russellf7f510e2008-05-30 15:09:44 -050053}
54
Ian Moltonbb347d92009-12-01 15:26:33 +080055static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait)
Rusty Russellf7f510e2008-05-30 15:09:44 -050056{
Amit Shahcc8744e2012-05-28 12:18:40 +053057 int ret;
Rusty Russellf7f510e2008-05-30 15:09:44 -050058
Ian Moltonbb347d92009-12-01 15:26:33 +080059 if (!busy) {
60 busy = true;
61 init_completion(&have_data);
62 register_buffer(buf, size);
63 }
64
Rusty Russellf7f510e2008-05-30 15:09:44 -050065 if (!wait)
66 return 0;
67
Amit Shahcc8744e2012-05-28 12:18:40 +053068 ret = wait_for_completion_killable(&have_data);
69 if (ret < 0)
70 return ret;
Rusty Russell594de1d2009-06-12 22:16:39 -060071
Ian Moltonbb347d92009-12-01 15:26:33 +080072 busy = false;
Rusty Russell594de1d2009-06-12 22:16:39 -060073
Ian Moltonbb347d92009-12-01 15:26:33 +080074 return data_avail;
Rusty Russellf7f510e2008-05-30 15:09:44 -050075}
76
Ian Moltonbb347d92009-12-01 15:26:33 +080077static void virtio_cleanup(struct hwrng *rng)
Rusty Russellf7f510e2008-05-30 15:09:44 -050078{
Ian Moltonbb347d92009-12-01 15:26:33 +080079 if (busy)
80 wait_for_completion(&have_data);
Rusty Russellf7f510e2008-05-30 15:09:44 -050081}
82
Ian Moltonbb347d92009-12-01 15:26:33 +080083
Rusty Russellf7f510e2008-05-30 15:09:44 -050084static struct hwrng virtio_hwrng = {
Ian Moltonbb347d92009-12-01 15:26:33 +080085 .name = "virtio",
86 .cleanup = virtio_cleanup,
87 .read = virtio_read,
Rusty Russellf7f510e2008-05-30 15:09:44 -050088};
89
Amit Shah178d8552012-05-28 12:18:42 +053090static int probe_common(struct virtio_device *vdev)
Rusty Russellf7f510e2008-05-30 15:09:44 -050091{
92 int err;
93
Amit Shahe84e7a52013-03-08 11:30:18 +110094 if (vq) {
95 /* We only support one device for now */
96 return -EBUSY;
97 }
Rusty Russellf7f510e2008-05-30 15:09:44 -050098 /* We expect a single virtqueue. */
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060099 vq = virtio_find_single_vq(vdev, random_recv_done, "input");
Amit Shahe84e7a52013-03-08 11:30:18 +1100100 if (IS_ERR(vq)) {
101 err = PTR_ERR(vq);
102 vq = NULL;
103 return err;
104 }
Rusty Russellf7f510e2008-05-30 15:09:44 -0500105
106 err = hwrng_register(&virtio_hwrng);
107 if (err) {
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600108 vdev->config->del_vqs(vdev);
Amit Shahe84e7a52013-03-08 11:30:18 +1100109 vq = NULL;
Rusty Russellf7f510e2008-05-30 15:09:44 -0500110 return err;
111 }
112
Rusty Russellf7f510e2008-05-30 15:09:44 -0500113 return 0;
114}
115
Amit Shah178d8552012-05-28 12:18:42 +0530116static void remove_common(struct virtio_device *vdev)
Rusty Russellf7f510e2008-05-30 15:09:44 -0500117{
118 vdev->config->reset(vdev);
Amit Shah44769872012-05-28 12:18:41 +0530119 busy = false;
Rusty Russellf7f510e2008-05-30 15:09:44 -0500120 hwrng_unregister(&virtio_hwrng);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600121 vdev->config->del_vqs(vdev);
Amit Shahe84e7a52013-03-08 11:30:18 +1100122 vq = NULL;
Rusty Russellf7f510e2008-05-30 15:09:44 -0500123}
124
Amit Shah178d8552012-05-28 12:18:42 +0530125static int virtrng_probe(struct virtio_device *vdev)
126{
127 return probe_common(vdev);
128}
129
Bill Pemberton39af33f2012-11-19 13:26:26 -0500130static void virtrng_remove(struct virtio_device *vdev)
Amit Shah178d8552012-05-28 12:18:42 +0530131{
132 remove_common(vdev);
133}
134
Aaron Lu89107002013-09-17 09:25:23 +0930135#ifdef CONFIG_PM_SLEEP
Amit Shah0bc1a2e2012-05-28 12:18:43 +0530136static int virtrng_freeze(struct virtio_device *vdev)
137{
138 remove_common(vdev);
139 return 0;
140}
141
142static int virtrng_restore(struct virtio_device *vdev)
143{
144 return probe_common(vdev);
145}
146#endif
147
Rusty Russellf7f510e2008-05-30 15:09:44 -0500148static struct virtio_device_id id_table[] = {
149 { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
150 { 0 },
151};
152
Jeff Mahoneyd817cd52010-01-15 17:01:26 -0800153static struct virtio_driver virtio_rng_driver = {
Rusty Russellf7f510e2008-05-30 15:09:44 -0500154 .driver.name = KBUILD_MODNAME,
155 .driver.owner = THIS_MODULE,
156 .id_table = id_table,
157 .probe = virtrng_probe,
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -0800158 .remove = virtrng_remove,
Aaron Lu89107002013-09-17 09:25:23 +0930159#ifdef CONFIG_PM_SLEEP
Amit Shah0bc1a2e2012-05-28 12:18:43 +0530160 .freeze = virtrng_freeze,
161 .restore = virtrng_restore,
162#endif
Rusty Russellf7f510e2008-05-30 15:09:44 -0500163};
164
Rusty Russellb2a17022013-02-13 16:59:28 +1030165module_virtio_driver(virtio_rng_driver);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500166MODULE_DEVICE_TABLE(virtio, id_table);
167MODULE_DESCRIPTION("Virtio random number driver");
168MODULE_LICENSE("GPL");