virtio-rng.c 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Randomness driver for virtio
 *  Copyright (C) 2007, 2008 Rusty Russell IBM Corporation
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */
19

20 21 22 23 24 25
#include <linux/err.h>
#include <linux/hw_random.h>
#include <linux/scatterlist.h>
#include <linux/spinlock.h>
#include <linux/virtio.h>
#include <linux/virtio_rng.h>
26
#include <linux/module.h>
27

28 29 30 31 32 33 34 35 36

struct virtrng_info {
	struct virtio_device *vdev;
	struct hwrng hwrng;
	struct virtqueue *vq;
	unsigned int data_avail;
	struct completion have_data;
	bool busy;
};
37 38 39

static void random_recv_done(struct virtqueue *vq)
{
40 41
	struct virtrng_info *vi = vq->vdev->priv;

42
	/* We can get spurious callbacks, e.g. shared IRQs + virtio_pci. */
43
	if (!virtqueue_get_buf(vi->vq, &vi->data_avail))
44
		return;
45

46
	complete(&vi->have_data);
47 48
}

49
/* The host will fill any buffer we give it with sweet, sweet randomness. */
50
static void register_buffer(struct virtrng_info *vi, u8 *buf, size_t size)
51 52 53
{
	struct scatterlist sg;

54 55
	sg_init_one(&sg, buf, size);

56
	/* There should always be room for one buffer. */
57
	virtqueue_add_inbuf(vi->vq, &sg, 1, buf, GFP_KERNEL);
58

59
	virtqueue_kick(vi->vq);
60 61
}

62
static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait)
63
{
64
	int ret;
65
	struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
66

67 68 69 70
	if (!vi->busy) {
		vi->busy = true;
		init_completion(&vi->have_data);
		register_buffer(vi, buf, size);
71 72
	}

73 74 75
	if (!wait)
		return 0;

76
	ret = wait_for_completion_killable(&vi->have_data);
77 78
	if (ret < 0)
		return ret;
79

80
	vi->busy = false;
81

82
	return vi->data_avail;
83 84
}

85
static void virtio_cleanup(struct hwrng *rng)
86
{
87
	struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
88

89 90 91
	if (vi->busy)
		wait_for_completion(&vi->have_data);
}
92

93
static int probe_common(struct virtio_device *vdev)
94
{
95 96 97 98 99 100 101 102 103 104 105
	int err, i;
	struct virtrng_info *vi = NULL;

	vi = kmalloc(sizeof(struct virtrng_info), GFP_KERNEL);
	vi->hwrng.name = kmalloc(40, GFP_KERNEL);
	init_completion(&vi->have_data);

	vi->hwrng.read = virtio_read;
	vi->hwrng.cleanup = virtio_cleanup;
	vi->hwrng.priv = (unsigned long)vi;
	vdev->priv = vi;
106 107

	/* We expect a single virtqueue. */
108 109 110 111 112 113 114
	vi->vq = virtio_find_single_vq(vdev, random_recv_done, "input");
	if (IS_ERR(vi->vq)) {
		err = PTR_ERR(vi->vq);
		kfree(vi->hwrng.name);
		vi->vq = NULL;
		kfree(vi);
		vi = NULL;
115 116
		return err;
	}
117

118 119 120 121 122 123
	i = 0;
	do {
		sprintf(vi->hwrng.name, "virtio_rng.%d", i++);
		err = hwrng_register(&vi->hwrng);
	} while (err == -EEXIST);

124
	if (err) {
125
		vdev->config->del_vqs(vdev);
126 127 128 129
		kfree(vi->hwrng.name);
		vi->vq = NULL;
		kfree(vi);
		vi = NULL;
130 131 132 133 134 135
		return err;
	}

	return 0;
}

136
static void remove_common(struct virtio_device *vdev)
137
{
138
	struct virtrng_info *vi = vdev->priv;
139
	vdev->config->reset(vdev);
140 141
	vi->busy = false;
	hwrng_unregister(&vi->hwrng);
142
	vdev->config->del_vqs(vdev);
143 144 145 146
	kfree(vi->hwrng.name);
	vi->vq = NULL;
	kfree(vi);
	vi = NULL;
147 148
}

149 150 151 152 153
static int virtrng_probe(struct virtio_device *vdev)
{
	return probe_common(vdev);
}

B
Bill Pemberton 已提交
154
static void virtrng_remove(struct virtio_device *vdev)
155 156 157 158
{
	remove_common(vdev);
}

159
#ifdef CONFIG_PM_SLEEP
A
Amit Shah 已提交
160 161 162 163 164 165 166 167 168 169 170 171
static int virtrng_freeze(struct virtio_device *vdev)
{
	remove_common(vdev);
	return 0;
}

static int virtrng_restore(struct virtio_device *vdev)
{
	return probe_common(vdev);
}
#endif

172 173 174 175 176
static struct virtio_device_id id_table[] = {
	{ VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
	{ 0 },
};

177
static struct virtio_driver virtio_rng_driver = {
178 179 180 181
	.driver.name =	KBUILD_MODNAME,
	.driver.owner =	THIS_MODULE,
	.id_table =	id_table,
	.probe =	virtrng_probe,
182
	.remove =	virtrng_remove,
183
#ifdef CONFIG_PM_SLEEP
A
Amit Shah 已提交
184 185 186
	.freeze =	virtrng_freeze,
	.restore =	virtrng_restore,
#endif
187 188
};

189
module_virtio_driver(virtio_rng_driver);
190 191 192
MODULE_DEVICE_TABLE(virtio, id_table);
MODULE_DESCRIPTION("Virtio random number driver");
MODULE_LICENSE("GPL");