virtio-rng.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_ids.h>
  25. #include <linux/virtio_rng.h>
  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. static struct virtqueue *vq;
  31. static u32 *random_data;
  32. static unsigned int data_left;
  33. static DECLARE_COMPLETION(have_data);
  34. static void random_recv_done(struct virtqueue *vq)
  35. {
  36. unsigned int len;
  37. /* We can get spurious callbacks, e.g. shared IRQs + virtio_pci. */
  38. if (!vq->vq_ops->get_buf(vq, &len))
  39. return;
  40. data_left += len;
  41. complete(&have_data);
  42. }
  43. static void register_buffer(void)
  44. {
  45. struct scatterlist sg;
  46. sg_init_one(&sg, random_data+data_left, RANDOM_DATA_SIZE-data_left);
  47. /* There should always be room for one buffer. */
  48. if (vq->vq_ops->add_buf(vq, &sg, 0, 1, random_data) < 0)
  49. BUG();
  50. vq->vq_ops->kick(vq);
  51. }
  52. /* At least we don't udelay() in a loop like some other drivers. */
  53. static int virtio_data_present(struct hwrng *rng, int wait)
  54. {
  55. if (data_left >= sizeof(u32))
  56. return 1;
  57. again:
  58. if (!wait)
  59. return 0;
  60. wait_for_completion(&have_data);
  61. /* Not enough? Re-register. */
  62. if (unlikely(data_left < sizeof(u32))) {
  63. register_buffer();
  64. goto again;
  65. }
  66. return 1;
  67. }
  68. /* virtio_data_present() must have succeeded before this is called. */
  69. static int virtio_data_read(struct hwrng *rng, u32 *data)
  70. {
  71. BUG_ON(data_left < sizeof(u32));
  72. data_left -= sizeof(u32);
  73. *data = random_data[data_left / 4];
  74. if (data_left < sizeof(u32)) {
  75. init_completion(&have_data);
  76. register_buffer();
  77. }
  78. return sizeof(*data);
  79. }
  80. static struct hwrng virtio_hwrng = {
  81. .name = "virtio",
  82. .data_present = virtio_data_present,
  83. .data_read = virtio_data_read,
  84. };
  85. static int virtrng_probe(struct virtio_device *vdev)
  86. {
  87. int err;
  88. /* We expect a single virtqueue. */
  89. vq = virtio_find_single_vq(vdev, random_recv_done, "input");
  90. if (IS_ERR(vq))
  91. return PTR_ERR(vq);
  92. err = hwrng_register(&virtio_hwrng);
  93. if (err) {
  94. vdev->config->del_vqs(vdev);
  95. return err;
  96. }
  97. register_buffer();
  98. return 0;
  99. }
  100. static void virtrng_remove(struct virtio_device *vdev)
  101. {
  102. vdev->config->reset(vdev);
  103. hwrng_unregister(&virtio_hwrng);
  104. vdev->config->del_vqs(vdev);
  105. }
  106. static struct virtio_device_id id_table[] = {
  107. { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
  108. { 0 },
  109. };
  110. static struct virtio_driver virtio_rng = {
  111. .driver.name = KBUILD_MODNAME,
  112. .driver.owner = THIS_MODULE,
  113. .id_table = id_table,
  114. .probe = virtrng_probe,
  115. .remove = __devexit_p(virtrng_remove),
  116. };
  117. static int __init init(void)
  118. {
  119. int err;
  120. random_data = kmalloc(RANDOM_DATA_SIZE, GFP_KERNEL);
  121. if (!random_data)
  122. return -ENOMEM;
  123. err = register_virtio_driver(&virtio_rng);
  124. if (err)
  125. kfree(random_data);
  126. return err;
  127. }
  128. static void __exit fini(void)
  129. {
  130. kfree(random_data);
  131. unregister_virtio_driver(&virtio_rng);
  132. }
  133. module_init(init);
  134. module_exit(fini);
  135. MODULE_DEVICE_TABLE(virtio, id_table);
  136. MODULE_DESCRIPTION("Virtio random number driver");
  137. MODULE_LICENSE("GPL");