blob: 723725bbb96b774036f9fee05562b207d1cecfc0 [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
92 /* We expect a single virtqueue. */
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060093 vq = virtio_find_single_vq(vdev, random_recv_done, "input");
Rusty Russellf7f510e2008-05-30 15:09:44 -050094 if (IS_ERR(vq))
95 return PTR_ERR(vq);
96
97 err = hwrng_register(&virtio_hwrng);
98 if (err) {
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -060099 vdev->config->del_vqs(vdev);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500100 return err;
101 }
102
Rusty Russellf7f510e2008-05-30 15:09:44 -0500103 return 0;
104}
105
Uwe Kleine-Königff07eb82009-10-01 10:28:35 +0200106static void __devexit virtrng_remove(struct virtio_device *vdev)
Rusty Russellf7f510e2008-05-30 15:09:44 -0500107{
108 vdev->config->reset(vdev);
109 hwrng_unregister(&virtio_hwrng);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600110 vdev->config->del_vqs(vdev);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500111}
112
113static struct virtio_device_id id_table[] = {
114 { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
115 { 0 },
116};
117
Jeff Mahoneyd817cd52010-01-15 17:01:26 -0800118static struct virtio_driver virtio_rng_driver = {
Rusty Russellf7f510e2008-05-30 15:09:44 -0500119 .driver.name = KBUILD_MODNAME,
120 .driver.owner = THIS_MODULE,
121 .id_table = id_table,
122 .probe = virtrng_probe,
123 .remove = __devexit_p(virtrng_remove),
124};
125
126static int __init init(void)
127{
Jeff Mahoneyd817cd52010-01-15 17:01:26 -0800128 return register_virtio_driver(&virtio_rng_driver);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500129}
130
131static void __exit fini(void)
132{
Jeff Mahoneyd817cd52010-01-15 17:01:26 -0800133 unregister_virtio_driver(&virtio_rng_driver);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500134}
135module_init(init);
136module_exit(fini);
137
138MODULE_DEVICE_TABLE(virtio, id_table);
139MODULE_DESCRIPTION("Virtio random number driver");
140MODULE_LICENSE("GPL");