blob: 5708299507d0e5b59ad401d495f1b0baa80dbe65 [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 Russellf96fde42012-01-12 15:44:42 +103050 if (virtqueue_add_buf(vq, &sg, 0, 1, buf, GFP_KERNEL) < 0)
Rusty Russellf7f510e2008-05-30 15:09:44 -050051 BUG();
Ian Moltonbb347d92009-12-01 15:26:33 +080052
Michael S. Tsirkin28cfc822010-04-13 16:11:42 +030053 virtqueue_kick(vq);
Rusty Russellf7f510e2008-05-30 15:09:44 -050054}
55
Ian Moltonbb347d92009-12-01 15:26:33 +080056static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait)
Rusty Russellf7f510e2008-05-30 15:09:44 -050057{
Amit Shahcc8744e2012-05-28 12:18:40 +053058 int ret;
Rusty Russellf7f510e2008-05-30 15:09:44 -050059
Ian Moltonbb347d92009-12-01 15:26:33 +080060 if (!busy) {
61 busy = true;
62 init_completion(&have_data);
63 register_buffer(buf, size);
64 }
65
Rusty Russellf7f510e2008-05-30 15:09:44 -050066 if (!wait)
67 return 0;
68
Amit Shahcc8744e2012-05-28 12:18:40 +053069 ret = wait_for_completion_killable(&have_data);
70 if (ret < 0)
71 return ret;
Rusty Russell594de1d2009-06-12 22:16:39 -060072
Ian Moltonbb347d92009-12-01 15:26:33 +080073 busy = false;
Rusty Russell594de1d2009-06-12 22:16:39 -060074
Ian Moltonbb347d92009-12-01 15:26:33 +080075 return data_avail;
Rusty Russellf7f510e2008-05-30 15:09:44 -050076}
77
Ian Moltonbb347d92009-12-01 15:26:33 +080078static void virtio_cleanup(struct hwrng *rng)
Rusty Russellf7f510e2008-05-30 15:09:44 -050079{
Ian Moltonbb347d92009-12-01 15:26:33 +080080 if (busy)
81 wait_for_completion(&have_data);
Rusty Russellf7f510e2008-05-30 15:09:44 -050082}
83
Ian Moltonbb347d92009-12-01 15:26:33 +080084
Rusty Russellf7f510e2008-05-30 15:09:44 -050085static struct hwrng virtio_hwrng = {
Ian Moltonbb347d92009-12-01 15:26:33 +080086 .name = "virtio",
87 .cleanup = virtio_cleanup,
88 .read = virtio_read,
Rusty Russellf7f510e2008-05-30 15:09:44 -050089};
90
Amit Shah178d8552012-05-28 12:18:42 +053091static int probe_common(struct virtio_device *vdev)
Rusty Russellf7f510e2008-05-30 15:09:44 -050092{
93 int err;
94
95 /* We expect a single virtqueue. */
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060096 vq = virtio_find_single_vq(vdev, random_recv_done, "input");
Rusty Russellf7f510e2008-05-30 15:09:44 -050097 if (IS_ERR(vq))
98 return PTR_ERR(vq);
99
100 err = hwrng_register(&virtio_hwrng);
101 if (err) {
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600102 vdev->config->del_vqs(vdev);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500103 return err;
104 }
105
Rusty Russellf7f510e2008-05-30 15:09:44 -0500106 return 0;
107}
108
Amit Shah178d8552012-05-28 12:18:42 +0530109static void remove_common(struct virtio_device *vdev)
Rusty Russellf7f510e2008-05-30 15:09:44 -0500110{
111 vdev->config->reset(vdev);
Amit Shah44769872012-05-28 12:18:41 +0530112 busy = false;
Rusty Russellf7f510e2008-05-30 15:09:44 -0500113 hwrng_unregister(&virtio_hwrng);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600114 vdev->config->del_vqs(vdev);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500115}
116
Amit Shah178d8552012-05-28 12:18:42 +0530117static int virtrng_probe(struct virtio_device *vdev)
118{
119 return probe_common(vdev);
120}
121
122static void __devexit virtrng_remove(struct virtio_device *vdev)
123{
124 remove_common(vdev);
125}
126
Amit Shah0bc1a2e2012-05-28 12:18:43 +0530127#ifdef CONFIG_PM
128static int virtrng_freeze(struct virtio_device *vdev)
129{
130 remove_common(vdev);
131 return 0;
132}
133
134static int virtrng_restore(struct virtio_device *vdev)
135{
136 return probe_common(vdev);
137}
138#endif
139
Rusty Russellf7f510e2008-05-30 15:09:44 -0500140static struct virtio_device_id id_table[] = {
141 { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
142 { 0 },
143};
144
Jeff Mahoneyd817cd52010-01-15 17:01:26 -0800145static struct virtio_driver virtio_rng_driver = {
Rusty Russellf7f510e2008-05-30 15:09:44 -0500146 .driver.name = KBUILD_MODNAME,
147 .driver.owner = THIS_MODULE,
148 .id_table = id_table,
149 .probe = virtrng_probe,
150 .remove = __devexit_p(virtrng_remove),
Amit Shah0bc1a2e2012-05-28 12:18:43 +0530151#ifdef CONFIG_PM
152 .freeze = virtrng_freeze,
153 .restore = virtrng_restore,
154#endif
Rusty Russellf7f510e2008-05-30 15:09:44 -0500155};
156
157static int __init init(void)
158{
Jeff Mahoneyd817cd52010-01-15 17:01:26 -0800159 return register_virtio_driver(&virtio_rng_driver);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500160}
161
162static void __exit fini(void)
163{
Jeff Mahoneyd817cd52010-01-15 17:01:26 -0800164 unregister_virtio_driver(&virtio_rng_driver);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500165}
166module_init(init);
167module_exit(fini);
168
169MODULE_DEVICE_TABLE(virtio, id_table);
170MODULE_DESCRIPTION("Virtio random number driver");
171MODULE_LICENSE("GPL");