diff options
| -rw-r--r-- | drivers/char/hw_random/virtio-rng.c | 78 |
1 files changed, 27 insertions, 51 deletions
diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c index 915157fcff98..bdaef8e94021 100644 --- a/drivers/char/hw_random/virtio-rng.c +++ b/drivers/char/hw_random/virtio-rng.c | |||
| @@ -16,6 +16,7 @@ | |||
| 16 | * along with this program; if not, write to the Free Software | 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 | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ | 18 | */ |
| 19 | |||
| 19 | #include <linux/err.h> | 20 | #include <linux/err.h> |
| 20 | #include <linux/hw_random.h> | 21 | #include <linux/hw_random.h> |
| 21 | #include <linux/scatterlist.h> | 22 | #include <linux/scatterlist.h> |
| @@ -23,78 +24,64 @@ | |||
| 23 | #include <linux/virtio.h> | 24 | #include <linux/virtio.h> |
| 24 | #include <linux/virtio_rng.h> | 25 | #include <linux/virtio_rng.h> |
| 25 | 26 | ||
| 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; | 27 | static struct virtqueue *vq; |
| 32 | static u32 *random_data; | 28 | static unsigned int data_avail; |
| 33 | static unsigned int data_left; | ||
| 34 | static DECLARE_COMPLETION(have_data); | 29 | static DECLARE_COMPLETION(have_data); |
| 30 | static bool busy; | ||
| 35 | 31 | ||
| 36 | static void random_recv_done(struct virtqueue *vq) | 32 | static void random_recv_done(struct virtqueue *vq) |
| 37 | { | 33 | { |
| 38 | unsigned int len; | ||
| 39 | |||
| 40 | /* We can get spurious callbacks, e.g. shared IRQs + virtio_pci. */ | 34 | /* We can get spurious callbacks, e.g. shared IRQs + virtio_pci. */ |
| 41 | if (!vq->vq_ops->get_buf(vq, &len)) | 35 | if (!vq->vq_ops->get_buf(vq, &data_avail)) |
| 42 | return; | 36 | return; |
| 43 | 37 | ||
| 44 | data_left += len; | ||
| 45 | complete(&have_data); | 38 | complete(&have_data); |
| 46 | } | 39 | } |
| 47 | 40 | ||
| 48 | static void register_buffer(void) | 41 | /* The host will fill any buffer we give it with sweet, sweet randomness. */ |
| 42 | static void register_buffer(u8 *buf, size_t size) | ||
| 49 | { | 43 | { |
| 50 | struct scatterlist sg; | 44 | struct scatterlist sg; |
| 51 | 45 | ||
| 52 | sg_init_one(&sg, random_data+data_left, RANDOM_DATA_SIZE-data_left); | 46 | sg_init_one(&sg, buf, size); |
| 47 | |||
| 53 | /* There should always be room for one buffer. */ | 48 | /* There should always be room for one buffer. */ |
| 54 | if (vq->vq_ops->add_buf(vq, &sg, 0, 1, random_data) < 0) | 49 | if (vq->vq_ops->add_buf(vq, &sg, 0, 1, buf) < 0) |
| 55 | BUG(); | 50 | BUG(); |
| 51 | |||
| 56 | vq->vq_ops->kick(vq); | 52 | vq->vq_ops->kick(vq); |
| 57 | } | 53 | } |
| 58 | 54 | ||
| 59 | /* At least we don't udelay() in a loop like some other drivers. */ | 55 | static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait) |
| 60 | static int virtio_data_present(struct hwrng *rng, int wait) | ||
| 61 | { | 56 | { |
| 62 | if (data_left >= sizeof(u32)) | ||
| 63 | return 1; | ||
| 64 | 57 | ||
| 65 | again: | 58 | if (!busy) { |
| 59 | busy = true; | ||
| 60 | init_completion(&have_data); | ||
| 61 | register_buffer(buf, size); | ||
| 62 | } | ||
| 63 | |||
| 66 | if (!wait) | 64 | if (!wait) |
| 67 | return 0; | 65 | return 0; |
| 68 | 66 | ||
| 69 | wait_for_completion(&have_data); | 67 | wait_for_completion(&have_data); |
| 70 | 68 | ||
| 71 | /* Not enough? Re-register. */ | 69 | busy = false; |
| 72 | if (unlikely(data_left < sizeof(u32))) { | ||
| 73 | register_buffer(); | ||
| 74 | goto again; | ||
| 75 | } | ||
| 76 | 70 | ||
| 77 | return 1; | 71 | return data_avail; |
| 78 | } | 72 | } |
| 79 | 73 | ||
| 80 | /* virtio_data_present() must have succeeded before this is called. */ | 74 | static void virtio_cleanup(struct hwrng *rng) |
| 81 | static int virtio_data_read(struct hwrng *rng, u32 *data) | ||
| 82 | { | 75 | { |
| 83 | BUG_ON(data_left < sizeof(u32)); | 76 | if (busy) |
| 84 | data_left -= sizeof(u32); | 77 | wait_for_completion(&have_data); |
| 85 | *data = random_data[data_left / 4]; | ||
| 86 | |||
| 87 | if (data_left < sizeof(u32)) { | ||
| 88 | init_completion(&have_data); | ||
| 89 | register_buffer(); | ||
| 90 | } | ||
| 91 | return sizeof(*data); | ||
| 92 | } | 78 | } |
| 93 | 79 | ||
| 80 | |||
| 94 | static struct hwrng virtio_hwrng = { | 81 | static struct hwrng virtio_hwrng = { |
| 95 | .name = "virtio", | 82 | .name = "virtio", |
| 96 | .data_present = virtio_data_present, | 83 | .cleanup = virtio_cleanup, |
| 97 | .data_read = virtio_data_read, | 84 | .read = virtio_read, |
| 98 | }; | 85 | }; |
| 99 | 86 | ||
| 100 | static int virtrng_probe(struct virtio_device *vdev) | 87 | static int virtrng_probe(struct virtio_device *vdev) |
| @@ -112,7 +99,6 @@ static int virtrng_probe(struct virtio_device *vdev) | |||
| 112 | return err; | 99 | return err; |
| 113 | } | 100 | } |
| 114 | 101 | ||
| 115 | register_buffer(); | ||
| 116 | return 0; | 102 | return 0; |
| 117 | } | 103 | } |
| 118 | 104 | ||
| @@ -138,21 +124,11 @@ static struct virtio_driver virtio_rng = { | |||
| 138 | 124 | ||
| 139 | static int __init init(void) | 125 | static int __init init(void) |
| 140 | { | 126 | { |
| 141 | int err; | 127 | return register_virtio_driver(&virtio_rng); |
| 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 | } | 128 | } |
| 152 | 129 | ||
| 153 | static void __exit fini(void) | 130 | static void __exit fini(void) |
| 154 | { | 131 | { |
| 155 | kfree(random_data); | ||
| 156 | unregister_virtio_driver(&virtio_rng); | 132 | unregister_virtio_driver(&virtio_rng); |
| 157 | } | 133 | } |
| 158 | module_init(init); | 134 | module_init(init); |
