blob: 171380bd2256b20398068b7425279688290a98b7 [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{
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
68 wait_for_completion(&have_data);
Rusty Russell594de1d2009-06-12 22:16:39 -060069
Ian Moltonbb347d92009-12-01 15:26:33 +080070 busy = false;
Rusty Russell594de1d2009-06-12 22:16:39 -060071
Ian Moltonbb347d92009-12-01 15:26:33 +080072 return data_avail;
Rusty Russellf7f510e2008-05-30 15:09:44 -050073}
74
Ian Moltonbb347d92009-12-01 15:26:33 +080075static void virtio_cleanup(struct hwrng *rng)
Rusty Russellf7f510e2008-05-30 15:09:44 -050076{
Ian Moltonbb347d92009-12-01 15:26:33 +080077 if (busy)
78 wait_for_completion(&have_data);
Rusty Russellf7f510e2008-05-30 15:09:44 -050079}
80
Ian Moltonbb347d92009-12-01 15:26:33 +080081
Rusty Russellf7f510e2008-05-30 15:09:44 -050082static struct hwrng virtio_hwrng = {
Ian Moltonbb347d92009-12-01 15:26:33 +080083 .name = "virtio",
84 .cleanup = virtio_cleanup,
85 .read = virtio_read,
Rusty Russellf7f510e2008-05-30 15:09:44 -050086};
87
88static int virtrng_probe(struct virtio_device *vdev)
89{
90 int err;
91
Amit Shahc8fba582013-03-08 11:30:18 +110092 if (vq) {
93 /* We only support one device for now */
94 return -EBUSY;
95 }
Rusty Russellf7f510e2008-05-30 15:09:44 -050096 /* We expect a single virtqueue. */
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060097 vq = virtio_find_single_vq(vdev, random_recv_done, "input");
Amit Shahc8fba582013-03-08 11:30:18 +110098 if (IS_ERR(vq)) {
99 err = PTR_ERR(vq);
100 vq = NULL;
101 return err;
102 }
Rusty Russellf7f510e2008-05-30 15:09:44 -0500103
104 err = hwrng_register(&virtio_hwrng);
105 if (err) {
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600106 vdev->config->del_vqs(vdev);
Amit Shahc8fba582013-03-08 11:30:18 +1100107 vq = NULL;
Rusty Russellf7f510e2008-05-30 15:09:44 -0500108 return err;
109 }
110
Rusty Russellf7f510e2008-05-30 15:09:44 -0500111 return 0;
112}
113
Uwe Kleine-Königff07eb82009-10-01 10:28:35 +0200114static void __devexit virtrng_remove(struct virtio_device *vdev)
Rusty Russellf7f510e2008-05-30 15:09:44 -0500115{
116 vdev->config->reset(vdev);
117 hwrng_unregister(&virtio_hwrng);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600118 vdev->config->del_vqs(vdev);
Amit Shahc8fba582013-03-08 11:30:18 +1100119 vq = NULL;
Rusty Russellf7f510e2008-05-30 15:09:44 -0500120}
121
122static struct virtio_device_id id_table[] = {
123 { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
124 { 0 },
125};
126
Jeff Mahoneyd817cd52010-01-15 17:01:26 -0800127static struct virtio_driver virtio_rng_driver = {
Rusty Russellf7f510e2008-05-30 15:09:44 -0500128 .driver.name = KBUILD_MODNAME,
129 .driver.owner = THIS_MODULE,
130 .id_table = id_table,
131 .probe = virtrng_probe,
132 .remove = __devexit_p(virtrng_remove),
133};
134
135static int __init init(void)
136{
Jeff Mahoneyd817cd52010-01-15 17:01:26 -0800137 return register_virtio_driver(&virtio_rng_driver);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500138}
139
140static void __exit fini(void)
141{
Jeff Mahoneyd817cd52010-01-15 17:01:26 -0800142 unregister_virtio_driver(&virtio_rng_driver);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500143}
144module_init(init);
145module_exit(fini);
146
147MODULE_DEVICE_TABLE(virtio, id_table);
148MODULE_DESCRIPTION("Virtio random number driver");
149MODULE_LICENSE("GPL");