blob: 6bf4d47324eb884e7f462678acf493fec4c518b1 [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
Amit Shahe84e7a52013-03-08 11:30:18 +110095 if (vq) {
96 /* We only support one device for now */
97 return -EBUSY;
98 }
Rusty Russellf7f510e2008-05-30 15:09:44 -050099 /* We expect a single virtqueue. */
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600100 vq = virtio_find_single_vq(vdev, random_recv_done, "input");
Amit Shahe84e7a52013-03-08 11:30:18 +1100101 if (IS_ERR(vq)) {
102 err = PTR_ERR(vq);
103 vq = NULL;
104 return err;
105 }
Rusty Russellf7f510e2008-05-30 15:09:44 -0500106
107 err = hwrng_register(&virtio_hwrng);
108 if (err) {
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600109 vdev->config->del_vqs(vdev);
Amit Shahe84e7a52013-03-08 11:30:18 +1100110 vq = NULL;
Rusty Russellf7f510e2008-05-30 15:09:44 -0500111 return err;
112 }
113
Rusty Russellf7f510e2008-05-30 15:09:44 -0500114 return 0;
115}
116
Amit Shah178d8552012-05-28 12:18:42 +0530117static void remove_common(struct virtio_device *vdev)
Rusty Russellf7f510e2008-05-30 15:09:44 -0500118{
119 vdev->config->reset(vdev);
Amit Shah44769872012-05-28 12:18:41 +0530120 busy = false;
Rusty Russellf7f510e2008-05-30 15:09:44 -0500121 hwrng_unregister(&virtio_hwrng);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600122 vdev->config->del_vqs(vdev);
Amit Shahe84e7a52013-03-08 11:30:18 +1100123 vq = NULL;
Rusty Russellf7f510e2008-05-30 15:09:44 -0500124}
125
Amit Shah178d8552012-05-28 12:18:42 +0530126static int virtrng_probe(struct virtio_device *vdev)
127{
128 return probe_common(vdev);
129}
130
Bill Pemberton39af33f2012-11-19 13:26:26 -0500131static void virtrng_remove(struct virtio_device *vdev)
Amit Shah178d8552012-05-28 12:18:42 +0530132{
133 remove_common(vdev);
134}
135
Amit Shah0bc1a2e2012-05-28 12:18:43 +0530136#ifdef CONFIG_PM
137static int virtrng_freeze(struct virtio_device *vdev)
138{
139 remove_common(vdev);
140 return 0;
141}
142
143static int virtrng_restore(struct virtio_device *vdev)
144{
145 return probe_common(vdev);
146}
147#endif
148
Rusty Russellf7f510e2008-05-30 15:09:44 -0500149static struct virtio_device_id id_table[] = {
150 { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
151 { 0 },
152};
153
Jeff Mahoneyd817cd52010-01-15 17:01:26 -0800154static struct virtio_driver virtio_rng_driver = {
Rusty Russellf7f510e2008-05-30 15:09:44 -0500155 .driver.name = KBUILD_MODNAME,
156 .driver.owner = THIS_MODULE,
157 .id_table = id_table,
158 .probe = virtrng_probe,
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -0800159 .remove = virtrng_remove,
Amit Shah0bc1a2e2012-05-28 12:18:43 +0530160#ifdef CONFIG_PM
161 .freeze = virtrng_freeze,
162 .restore = virtrng_restore,
163#endif
Rusty Russellf7f510e2008-05-30 15:09:44 -0500164};
165
Rusty Russellb2a17022013-02-13 16:59:28 +1030166module_virtio_driver(virtio_rng_driver);
Rusty Russellf7f510e2008-05-30 15:09:44 -0500167MODULE_DEVICE_TABLE(virtio, id_table);
168MODULE_DESCRIPTION("Virtio random number driver");
169MODULE_LICENSE("GPL");