virtio-rng.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. if (vq) {
  77. /* We only support one device for now */
  78. return -EBUSY;
  79. }
  80. /* We expect a single virtqueue. */
  81. vq = virtio_find_single_vq(vdev, random_recv_done, "input");
  82. if (IS_ERR(vq)) {
  83. err = PTR_ERR(vq);
  84. vq = NULL;
  85. return err;
  86. }
  87. err = hwrng_register(&virtio_hwrng);
  88. if (err) {
  89. vdev->config->del_vqs(vdev);
  90. vq = NULL;
  91. return err;
  92. }
  93. return 0;
  94. }
  95. static void remove_common(struct virtio_device *vdev)
  96. {
  97. vdev->config->reset(vdev);
  98. busy = false;
  99. hwrng_unregister(&virtio_hwrng);
  100. vdev->config->del_vqs(vdev);
  101. vq = NULL;
  102. }
  103. static int virtrng_probe(struct virtio_device *vdev)
  104. {
  105. return probe_common(vdev);
  106. }
  107. static void virtrng_remove(struct virtio_device *vdev)
  108. {
  109. remove_common(vdev);
  110. }
  111. #ifdef CONFIG_PM
  112. static int virtrng_freeze(struct virtio_device *vdev)
  113. {
  114. remove_common(vdev);
  115. return 0;
  116. }
  117. static int virtrng_restore(struct virtio_device *vdev)
  118. {
  119. return probe_common(vdev);
  120. }
  121. #endif
  122. static struct virtio_device_id id_table[] = {
  123. { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
  124. { 0 },
  125. };
  126. static struct virtio_driver virtio_rng_driver = {
  127. .driver.name = KBUILD_MODNAME,
  128. .driver.owner = THIS_MODULE,
  129. .id_table = id_table,
  130. .probe = virtrng_probe,
  131. .remove = virtrng_remove,
  132. #ifdef CONFIG_PM
  133. .freeze = virtrng_freeze,
  134. .restore = virtrng_restore,
  135. #endif
  136. };
  137. module_virtio_driver(virtio_rng_driver);
  138. MODULE_DEVICE_TABLE(virtio, id_table);
  139. MODULE_DESCRIPTION("Virtio random number driver");
  140. MODULE_LICENSE("GPL");