virtio-rng.c 3.6 KB

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