virtio-rng.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #include <linux/module.h>
  26. static struct virtqueue *vq;
  27. static unsigned int data_avail;
  28. static DECLARE_COMPLETION(have_data);
  29. static bool busy;
  30. static void random_recv_done(struct virtqueue *vq)
  31. {
  32. /* We can get spurious callbacks, e.g. shared IRQs + virtio_pci. */
  33. if (!virtqueue_get_buf(vq, &data_avail))
  34. return;
  35. complete(&have_data);
  36. }
  37. /* The host will fill any buffer we give it with sweet, sweet randomness. */
  38. static void register_buffer(u8 *buf, size_t size)
  39. {
  40. struct scatterlist sg;
  41. sg_init_one(&sg, buf, size);
  42. /* There should always be room for one buffer. */
  43. if (virtqueue_add_buf(vq, &sg, 0, 1, buf, GFP_KERNEL) < 0)
  44. BUG();
  45. virtqueue_kick(vq);
  46. }
  47. static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait)
  48. {
  49. if (!busy) {
  50. busy = true;
  51. init_completion(&have_data);
  52. register_buffer(buf, size);
  53. }
  54. if (!wait)
  55. return 0;
  56. wait_for_completion(&have_data);
  57. busy = false;
  58. return data_avail;
  59. }
  60. static void virtio_cleanup(struct hwrng *rng)
  61. {
  62. if (busy)
  63. wait_for_completion(&have_data);
  64. }
  65. static struct hwrng virtio_hwrng = {
  66. .name = "virtio",
  67. .cleanup = virtio_cleanup,
  68. .read = virtio_read,
  69. };
  70. static int virtrng_probe(struct virtio_device *vdev)
  71. {
  72. int err;
  73. if (vq) {
  74. /* We only support one device for now */
  75. return -EBUSY;
  76. }
  77. /* We expect a single virtqueue. */
  78. vq = virtio_find_single_vq(vdev, random_recv_done, "input");
  79. if (IS_ERR(vq)) {
  80. err = PTR_ERR(vq);
  81. vq = NULL;
  82. return err;
  83. }
  84. err = hwrng_register(&virtio_hwrng);
  85. if (err) {
  86. vdev->config->del_vqs(vdev);
  87. vq = NULL;
  88. return err;
  89. }
  90. return 0;
  91. }
  92. static void __devexit virtrng_remove(struct virtio_device *vdev)
  93. {
  94. vdev->config->reset(vdev);
  95. hwrng_unregister(&virtio_hwrng);
  96. vdev->config->del_vqs(vdev);
  97. vq = NULL;
  98. }
  99. static struct virtio_device_id id_table[] = {
  100. { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
  101. { 0 },
  102. };
  103. static struct virtio_driver virtio_rng_driver = {
  104. .driver.name = KBUILD_MODNAME,
  105. .driver.owner = THIS_MODULE,
  106. .id_table = id_table,
  107. .probe = virtrng_probe,
  108. .remove = __devexit_p(virtrng_remove),
  109. };
  110. static int __init init(void)
  111. {
  112. return register_virtio_driver(&virtio_rng_driver);
  113. }
  114. static void __exit fini(void)
  115. {
  116. unregister_virtio_driver(&virtio_rng_driver);
  117. }
  118. module_init(init);
  119. module_exit(fini);
  120. MODULE_DEVICE_TABLE(virtio, id_table);
  121. MODULE_DESCRIPTION("Virtio random number driver");
  122. MODULE_LICENSE("GPL");