virtio-rng.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 DEFINE_IDA(rng_index_ida);
  27. struct virtrng_info {
  28. struct hwrng hwrng;
  29. struct virtqueue *vq;
  30. struct completion have_data;
  31. char name[25];
  32. unsigned int data_avail;
  33. int index;
  34. bool busy;
  35. bool hwrng_register_done;
  36. bool hwrng_removed;
  37. };
  38. static void random_recv_done(struct virtqueue *vq)
  39. {
  40. struct virtrng_info *vi = vq->vdev->priv;
  41. /* We can get spurious callbacks, e.g. shared IRQs + virtio_pci. */
  42. if (!virtqueue_get_buf(vi->vq, &vi->data_avail))
  43. return;
  44. complete(&vi->have_data);
  45. }
  46. /* The host will fill any buffer we give it with sweet, sweet randomness. */
  47. static void register_buffer(struct virtrng_info *vi, u8 *buf, size_t size)
  48. {
  49. struct scatterlist sg;
  50. sg_init_one(&sg, buf, size);
  51. /* There should always be room for one buffer. */
  52. virtqueue_add_inbuf(vi->vq, &sg, 1, buf, GFP_KERNEL);
  53. virtqueue_kick(vi->vq);
  54. }
  55. static int virtio_read(struct hwrng *rng, void *buf, size_t size, bool wait)
  56. {
  57. int ret;
  58. struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
  59. if (vi->hwrng_removed)
  60. return -ENODEV;
  61. if (!vi->busy) {
  62. vi->busy = true;
  63. reinit_completion(&vi->have_data);
  64. register_buffer(vi, buf, size);
  65. }
  66. if (!wait)
  67. return 0;
  68. ret = wait_for_completion_killable(&vi->have_data);
  69. if (ret < 0)
  70. return ret;
  71. vi->busy = false;
  72. return vi->data_avail;
  73. }
  74. static void virtio_cleanup(struct hwrng *rng)
  75. {
  76. struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
  77. if (vi->busy)
  78. wait_for_completion(&vi->have_data);
  79. }
  80. static int probe_common(struct virtio_device *vdev)
  81. {
  82. int err, index;
  83. struct virtrng_info *vi = NULL;
  84. vi = kzalloc(sizeof(struct virtrng_info), GFP_KERNEL);
  85. if (!vi)
  86. return -ENOMEM;
  87. vi->index = index = ida_simple_get(&rng_index_ida, 0, 0, GFP_KERNEL);
  88. if (index < 0) {
  89. err = index;
  90. goto err_ida;
  91. }
  92. sprintf(vi->name, "virtio_rng.%d", index);
  93. init_completion(&vi->have_data);
  94. vi->hwrng = (struct hwrng) {
  95. .read = virtio_read,
  96. .cleanup = virtio_cleanup,
  97. .priv = (unsigned long)vi,
  98. .name = vi->name,
  99. .quality = 1000,
  100. };
  101. vdev->priv = vi;
  102. /* We expect a single virtqueue. */
  103. vi->vq = virtio_find_single_vq(vdev, random_recv_done, "input");
  104. if (IS_ERR(vi->vq)) {
  105. err = PTR_ERR(vi->vq);
  106. goto err_find;
  107. }
  108. return 0;
  109. err_find:
  110. ida_simple_remove(&rng_index_ida, index);
  111. err_ida:
  112. kfree(vi);
  113. return err;
  114. }
  115. static void remove_common(struct virtio_device *vdev)
  116. {
  117. struct virtrng_info *vi = vdev->priv;
  118. vi->hwrng_removed = true;
  119. vi->data_avail = 0;
  120. complete(&vi->have_data);
  121. vdev->config->reset(vdev);
  122. vi->busy = false;
  123. if (vi->hwrng_register_done)
  124. hwrng_unregister(&vi->hwrng);
  125. vdev->config->del_vqs(vdev);
  126. ida_simple_remove(&rng_index_ida, vi->index);
  127. kfree(vi);
  128. }
  129. static int virtrng_probe(struct virtio_device *vdev)
  130. {
  131. return probe_common(vdev);
  132. }
  133. static void virtrng_remove(struct virtio_device *vdev)
  134. {
  135. remove_common(vdev);
  136. }
  137. static void virtrng_scan(struct virtio_device *vdev)
  138. {
  139. struct virtrng_info *vi = vdev->priv;
  140. int err;
  141. err = hwrng_register(&vi->hwrng);
  142. if (!err)
  143. vi->hwrng_register_done = true;
  144. }
  145. #ifdef CONFIG_PM_SLEEP
  146. static int virtrng_freeze(struct virtio_device *vdev)
  147. {
  148. remove_common(vdev);
  149. return 0;
  150. }
  151. static int virtrng_restore(struct virtio_device *vdev)
  152. {
  153. return probe_common(vdev);
  154. }
  155. #endif
  156. static struct virtio_device_id id_table[] = {
  157. { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
  158. { 0 },
  159. };
  160. static struct virtio_driver virtio_rng_driver = {
  161. .driver.name = KBUILD_MODNAME,
  162. .driver.owner = THIS_MODULE,
  163. .id_table = id_table,
  164. .probe = virtrng_probe,
  165. .remove = virtrng_remove,
  166. .scan = virtrng_scan,
  167. #ifdef CONFIG_PM_SLEEP
  168. .freeze = virtrng_freeze,
  169. .restore = virtrng_restore,
  170. #endif
  171. };
  172. module_virtio_driver(virtio_rng_driver);
  173. MODULE_DEVICE_TABLE(virtio, id_table);
  174. MODULE_DESCRIPTION("Virtio random number driver");
  175. MODULE_LICENSE("GPL");