virtio-rng.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. int ret;
  50. if (!busy) {
  51. busy = true;
  52. init_completion(&have_data);
  53. register_buffer(buf, size);
  54. }
  55. if (!wait)
  56. return 0;
  57. ret = wait_for_completion_killable(&have_data);
  58. if (ret < 0)
  59. return ret;
  60. busy = false;
  61. return data_avail;
  62. }
  63. static void virtio_cleanup(struct hwrng *rng)
  64. {
  65. if (busy)
  66. wait_for_completion(&have_data);
  67. }
  68. static struct hwrng virtio_hwrng = {
  69. .name = "virtio",
  70. .cleanup = virtio_cleanup,
  71. .read = virtio_read,
  72. };
  73. static int probe_common(struct virtio_device *vdev)
  74. {
  75. int err;
  76. /* We expect a single virtqueue. */
  77. vq = virtio_find_single_vq(vdev, random_recv_done, "input");
  78. if (IS_ERR(vq))
  79. return PTR_ERR(vq);
  80. err = hwrng_register(&virtio_hwrng);
  81. if (err) {
  82. vdev->config->del_vqs(vdev);
  83. return err;
  84. }
  85. return 0;
  86. }
  87. static void remove_common(struct virtio_device *vdev)
  88. {
  89. vdev->config->reset(vdev);
  90. busy = false;
  91. hwrng_unregister(&virtio_hwrng);
  92. vdev->config->del_vqs(vdev);
  93. }
  94. static int virtrng_probe(struct virtio_device *vdev)
  95. {
  96. return probe_common(vdev);
  97. }
  98. static void virtrng_remove(struct virtio_device *vdev)
  99. {
  100. remove_common(vdev);
  101. }
  102. #ifdef CONFIG_PM
  103. static int virtrng_freeze(struct virtio_device *vdev)
  104. {
  105. remove_common(vdev);
  106. return 0;
  107. }
  108. static int virtrng_restore(struct virtio_device *vdev)
  109. {
  110. return probe_common(vdev);
  111. }
  112. #endif
  113. static struct virtio_device_id id_table[] = {
  114. { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
  115. { 0 },
  116. };
  117. static struct virtio_driver virtio_rng_driver = {
  118. .driver.name = KBUILD_MODNAME,
  119. .driver.owner = THIS_MODULE,
  120. .id_table = id_table,
  121. .probe = virtrng_probe,
  122. .remove = virtrng_remove,
  123. #ifdef CONFIG_PM
  124. .freeze = virtrng_freeze,
  125. .restore = virtrng_restore,
  126. #endif
  127. };
  128. static int __init init(void)
  129. {
  130. return register_virtio_driver(&virtio_rng_driver);
  131. }
  132. static void __exit fini(void)
  133. {
  134. unregister_virtio_driver(&virtio_rng_driver);
  135. }
  136. module_init(init);
  137. module_exit(fini);
  138. MODULE_DEVICE_TABLE(virtio, id_table);
  139. MODULE_DESCRIPTION("Virtio random number driver");
  140. MODULE_LICENSE("GPL");