virtio-rng: support multiple virtio-rng devices
[deliverable/linux.git] / drivers / char / hw_random / virtio-rng.c
CommitLineData
f7f510ec
RR
1/*
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 */
bb347d98 19
f7f510ec
RR
20#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>
c22405c9 26#include <linux/module.h>
f7f510ec 27
08e53fbd
AK
28
29struct virtrng_info {
30 struct virtio_device *vdev;
31 struct hwrng hwrng;
32 struct virtqueue *vq;
33 unsigned int data_avail;
34 struct completion have_data;
35 bool busy;
36};
f7f510ec
RR
37
38static void random_recv_done(struct virtqueue *vq)
39{
08e53fbd
AK
40 struct virtrng_info *vi = vq->vdev->priv;
41
e5b89542 42 /* We can get spurious callbacks, e.g. shared IRQs + virtio_pci. */
08e53fbd 43 if (!virtqueue_get_buf(vi->vq, &vi->data_avail))
e5b89542 44 return;
f7f510ec 45
08e53fbd 46 complete(&vi->have_data);
f7f510ec
RR
47}
48
bb347d98 49/* The host will fill any buffer we give it with sweet, sweet randomness. */
08e53fbd 50static void register_buffer(struct virtrng_info *vi, u8 *buf, size_t size)
f7f510ec
RR
51{
52 struct scatterlist sg;
53
bb347d98
IM
54 sg_init_one(&sg, buf, size);
55
f7f510ec 56 /* There should always be room for one buffer. */
08e53fbd 57 virtqueue_add_inbuf(vi->vq, &sg, 1, buf, GFP_KERNEL);
bb347d98 58
08e53fbd 59 virtqueue_kick(vi->vq);
f7f510ec
RR
60}
61
bb347d98 62static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait)
f7f510ec 63{
cc8744e1 64 int ret;
08e53fbd 65 struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
f7f510ec 66
08e53fbd
AK
67 if (!vi->busy) {
68 vi->busy = true;
69 init_completion(&vi->have_data);
70 register_buffer(vi, buf, size);
bb347d98
IM
71 }
72
f7f510ec
RR
73 if (!wait)
74 return 0;
75
08e53fbd 76 ret = wait_for_completion_killable(&vi->have_data);
cc8744e1
AS
77 if (ret < 0)
78 return ret;
594de1dd 79
08e53fbd 80 vi->busy = false;
594de1dd 81
08e53fbd 82 return vi->data_avail;
f7f510ec
RR
83}
84
bb347d98 85static void virtio_cleanup(struct hwrng *rng)
f7f510ec 86{
08e53fbd 87 struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
bb347d98 88
08e53fbd
AK
89 if (vi->busy)
90 wait_for_completion(&vi->have_data);
91}
f7f510ec 92
178d855e 93static int probe_common(struct virtio_device *vdev)
f7f510ec 94{
08e53fbd
AK
95 int err, i;
96 struct virtrng_info *vi = NULL;
97
98 vi = kmalloc(sizeof(struct virtrng_info), GFP_KERNEL);
99 vi->hwrng.name = kmalloc(40, GFP_KERNEL);
100 init_completion(&vi->have_data);
101
102 vi->hwrng.read = virtio_read;
103 vi->hwrng.cleanup = virtio_cleanup;
104 vi->hwrng.priv = (unsigned long)vi;
105 vdev->priv = vi;
f7f510ec
RR
106
107 /* We expect a single virtqueue. */
08e53fbd
AK
108 vi->vq = virtio_find_single_vq(vdev, random_recv_done, "input");
109 if (IS_ERR(vi->vq)) {
110 err = PTR_ERR(vi->vq);
111 kfree(vi->hwrng.name);
112 vi->vq = NULL;
113 kfree(vi);
114 vi = NULL;
e84e7a56
AS
115 return err;
116 }
f7f510ec 117
08e53fbd
AK
118 i = 0;
119 do {
120 sprintf(vi->hwrng.name, "virtio_rng.%d", i++);
121 err = hwrng_register(&vi->hwrng);
122 } while (err == -EEXIST);
123
f7f510ec 124 if (err) {
d2a7ddda 125 vdev->config->del_vqs(vdev);
08e53fbd
AK
126 kfree(vi->hwrng.name);
127 vi->vq = NULL;
128 kfree(vi);
129 vi = NULL;
f7f510ec
RR
130 return err;
131 }
132
f7f510ec
RR
133 return 0;
134}
135
178d855e 136static void remove_common(struct virtio_device *vdev)
f7f510ec 137{
08e53fbd 138 struct virtrng_info *vi = vdev->priv;
f7f510ec 139 vdev->config->reset(vdev);
08e53fbd
AK
140 vi->busy = false;
141 hwrng_unregister(&vi->hwrng);
d2a7ddda 142 vdev->config->del_vqs(vdev);
08e53fbd
AK
143 kfree(vi->hwrng.name);
144 vi->vq = NULL;
145 kfree(vi);
146 vi = NULL;
f7f510ec
RR
147}
148
178d855e
AS
149static int virtrng_probe(struct virtio_device *vdev)
150{
151 return probe_common(vdev);
152}
153
39af33fc 154static void virtrng_remove(struct virtio_device *vdev)
178d855e
AS
155{
156 remove_common(vdev);
157}
158
89107000 159#ifdef CONFIG_PM_SLEEP
0bc1a2ef
AS
160static int virtrng_freeze(struct virtio_device *vdev)
161{
162 remove_common(vdev);
163 return 0;
164}
165
166static int virtrng_restore(struct virtio_device *vdev)
167{
168 return probe_common(vdev);
169}
170#endif
171
f7f510ec
RR
172static struct virtio_device_id id_table[] = {
173 { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
174 { 0 },
175};
176
d817cd52 177static struct virtio_driver virtio_rng_driver = {
f7f510ec
RR
178 .driver.name = KBUILD_MODNAME,
179 .driver.owner = THIS_MODULE,
180 .id_table = id_table,
181 .probe = virtrng_probe,
bcd2982a 182 .remove = virtrng_remove,
89107000 183#ifdef CONFIG_PM_SLEEP
0bc1a2ef
AS
184 .freeze = virtrng_freeze,
185 .restore = virtrng_restore,
186#endif
f7f510ec
RR
187};
188
b2a17029 189module_virtio_driver(virtio_rng_driver);
f7f510ec
RR
190MODULE_DEVICE_TABLE(virtio, id_table);
191MODULE_DESCRIPTION("Virtio random number driver");
192MODULE_LICENSE("GPL");
This page took 0.39285 seconds and 5 git commands to generate.