Rusty Russell | f7f510e | 2008-05-30 15:09:44 -0500 | [diff] [blame] | 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 | */ |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/hw_random.h> |
| 21 | #include <linux/scatterlist.h> |
| 22 | #include <linux/spinlock.h> |
| 23 | #include <linux/virtio.h> |
| 24 | #include <linux/virtio_rng.h> |
| 25 | |
| 26 | /* The host will fill any buffer we give it with sweet, sweet randomness. We |
| 27 | * give it 64 bytes at a time, and the hwrng framework takes it 4 bytes at a |
| 28 | * time. */ |
| 29 | #define RANDOM_DATA_SIZE 64 |
| 30 | |
| 31 | static struct virtqueue *vq; |
| 32 | static u32 *random_data; |
| 33 | static unsigned int data_left; |
| 34 | static DECLARE_COMPLETION(have_data); |
| 35 | |
| 36 | static void random_recv_done(struct virtqueue *vq) |
| 37 | { |
Rusty Russell | 594de1d | 2009-06-12 22:16:39 -0600 | [diff] [blame] | 38 | unsigned int len; |
Rusty Russell | f7f510e | 2008-05-30 15:09:44 -0500 | [diff] [blame] | 39 | |
Christian Borntraeger | e5b8954 | 2009-04-23 16:42:59 +0930 | [diff] [blame] | 40 | /* We can get spurious callbacks, e.g. shared IRQs + virtio_pci. */ |
Rusty Russell | f7f510e | 2008-05-30 15:09:44 -0500 | [diff] [blame] | 41 | if (!vq->vq_ops->get_buf(vq, &len)) |
Christian Borntraeger | e5b8954 | 2009-04-23 16:42:59 +0930 | [diff] [blame] | 42 | return; |
Rusty Russell | f7f510e | 2008-05-30 15:09:44 -0500 | [diff] [blame] | 43 | |
Rusty Russell | 594de1d | 2009-06-12 22:16:39 -0600 | [diff] [blame] | 44 | data_left += len; |
Rusty Russell | f7f510e | 2008-05-30 15:09:44 -0500 | [diff] [blame] | 45 | complete(&have_data); |
| 46 | } |
| 47 | |
| 48 | static void register_buffer(void) |
| 49 | { |
| 50 | struct scatterlist sg; |
| 51 | |
Rusty Russell | 594de1d | 2009-06-12 22:16:39 -0600 | [diff] [blame] | 52 | sg_init_one(&sg, random_data+data_left, RANDOM_DATA_SIZE-data_left); |
Rusty Russell | f7f510e | 2008-05-30 15:09:44 -0500 | [diff] [blame] | 53 | /* There should always be room for one buffer. */ |
Rusty Russell | 3c1b27d | 2009-09-23 22:26:31 -0600 | [diff] [blame] | 54 | if (vq->vq_ops->add_buf(vq, &sg, 0, 1, random_data) < 0) |
Rusty Russell | f7f510e | 2008-05-30 15:09:44 -0500 | [diff] [blame] | 55 | BUG(); |
| 56 | vq->vq_ops->kick(vq); |
| 57 | } |
| 58 | |
| 59 | /* At least we don't udelay() in a loop like some other drivers. */ |
| 60 | static int virtio_data_present(struct hwrng *rng, int wait) |
| 61 | { |
Rusty Russell | 594de1d | 2009-06-12 22:16:39 -0600 | [diff] [blame] | 62 | if (data_left >= sizeof(u32)) |
Rusty Russell | f7f510e | 2008-05-30 15:09:44 -0500 | [diff] [blame] | 63 | return 1; |
| 64 | |
Rusty Russell | 594de1d | 2009-06-12 22:16:39 -0600 | [diff] [blame] | 65 | again: |
Rusty Russell | f7f510e | 2008-05-30 15:09:44 -0500 | [diff] [blame] | 66 | if (!wait) |
| 67 | return 0; |
| 68 | |
| 69 | wait_for_completion(&have_data); |
Rusty Russell | 594de1d | 2009-06-12 22:16:39 -0600 | [diff] [blame] | 70 | |
| 71 | /* Not enough? Re-register. */ |
| 72 | if (unlikely(data_left < sizeof(u32))) { |
| 73 | register_buffer(); |
| 74 | goto again; |
| 75 | } |
| 76 | |
Rusty Russell | f7f510e | 2008-05-30 15:09:44 -0500 | [diff] [blame] | 77 | return 1; |
| 78 | } |
| 79 | |
| 80 | /* virtio_data_present() must have succeeded before this is called. */ |
| 81 | static int virtio_data_read(struct hwrng *rng, u32 *data) |
| 82 | { |
Rusty Russell | 594de1d | 2009-06-12 22:16:39 -0600 | [diff] [blame] | 83 | BUG_ON(data_left < sizeof(u32)); |
| 84 | data_left -= sizeof(u32); |
| 85 | *data = random_data[data_left / 4]; |
Rusty Russell | f7f510e | 2008-05-30 15:09:44 -0500 | [diff] [blame] | 86 | |
Rusty Russell | 594de1d | 2009-06-12 22:16:39 -0600 | [diff] [blame] | 87 | if (data_left < sizeof(u32)) { |
Rusty Russell | f7f510e | 2008-05-30 15:09:44 -0500 | [diff] [blame] | 88 | init_completion(&have_data); |
| 89 | register_buffer(); |
| 90 | } |
| 91 | return sizeof(*data); |
| 92 | } |
| 93 | |
| 94 | static struct hwrng virtio_hwrng = { |
| 95 | .name = "virtio", |
| 96 | .data_present = virtio_data_present, |
| 97 | .data_read = virtio_data_read, |
| 98 | }; |
| 99 | |
| 100 | static int virtrng_probe(struct virtio_device *vdev) |
| 101 | { |
| 102 | int err; |
| 103 | |
| 104 | /* We expect a single virtqueue. */ |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 105 | vq = virtio_find_single_vq(vdev, random_recv_done, "input"); |
Rusty Russell | f7f510e | 2008-05-30 15:09:44 -0500 | [diff] [blame] | 106 | if (IS_ERR(vq)) |
| 107 | return PTR_ERR(vq); |
| 108 | |
| 109 | err = hwrng_register(&virtio_hwrng); |
| 110 | if (err) { |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 111 | vdev->config->del_vqs(vdev); |
Rusty Russell | f7f510e | 2008-05-30 15:09:44 -0500 | [diff] [blame] | 112 | return err; |
| 113 | } |
| 114 | |
| 115 | register_buffer(); |
| 116 | return 0; |
| 117 | } |
| 118 | |
Uwe Kleine-König | ff07eb8 | 2009-10-01 10:28:35 +0200 | [diff] [blame^] | 119 | static void __devexit virtrng_remove(struct virtio_device *vdev) |
Rusty Russell | f7f510e | 2008-05-30 15:09:44 -0500 | [diff] [blame] | 120 | { |
| 121 | vdev->config->reset(vdev); |
| 122 | hwrng_unregister(&virtio_hwrng); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 123 | vdev->config->del_vqs(vdev); |
Rusty Russell | f7f510e | 2008-05-30 15:09:44 -0500 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static struct virtio_device_id id_table[] = { |
| 127 | { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID }, |
| 128 | { 0 }, |
| 129 | }; |
| 130 | |
| 131 | static struct virtio_driver virtio_rng = { |
| 132 | .driver.name = KBUILD_MODNAME, |
| 133 | .driver.owner = THIS_MODULE, |
| 134 | .id_table = id_table, |
| 135 | .probe = virtrng_probe, |
| 136 | .remove = __devexit_p(virtrng_remove), |
| 137 | }; |
| 138 | |
| 139 | static int __init init(void) |
| 140 | { |
| 141 | int err; |
| 142 | |
| 143 | random_data = kmalloc(RANDOM_DATA_SIZE, GFP_KERNEL); |
| 144 | if (!random_data) |
| 145 | return -ENOMEM; |
| 146 | |
| 147 | err = register_virtio_driver(&virtio_rng); |
| 148 | if (err) |
| 149 | kfree(random_data); |
| 150 | return err; |
| 151 | } |
| 152 | |
| 153 | static void __exit fini(void) |
| 154 | { |
| 155 | kfree(random_data); |
| 156 | unregister_virtio_driver(&virtio_rng); |
| 157 | } |
| 158 | module_init(init); |
| 159 | module_exit(fini); |
| 160 | |
| 161 | MODULE_DEVICE_TABLE(virtio, id_table); |
| 162 | MODULE_DESCRIPTION("Virtio random number driver"); |
| 163 | MODULE_LICENSE("GPL"); |