virtio-rng.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 virtio_device *vdev;
  29. struct hwrng hwrng;
  30. struct virtqueue *vq;
  31. unsigned int data_avail;
  32. struct completion have_data;
  33. bool busy;
  34. char name[25];
  35. int index;
  36. };
  37. static bool probe_done;
  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. /*
  60. * Don't ask host for data till we're setup. This call can
  61. * happen during hwrng_register(), after commit d9e7972619.
  62. */
  63. if (unlikely(!probe_done))
  64. return 0;
  65. if (!vi->busy) {
  66. vi->busy = true;
  67. init_completion(&vi->have_data);
  68. register_buffer(vi, buf, size);
  69. }
  70. if (!wait)
  71. return 0;
  72. ret = wait_for_completion_killable(&vi->have_data);
  73. if (ret < 0)
  74. return ret;
  75. vi->busy = false;
  76. return vi->data_avail;
  77. }
  78. static void virtio_cleanup(struct hwrng *rng)
  79. {
  80. struct virtrng_info *vi = (struct virtrng_info *)rng->priv;
  81. if (vi->busy)
  82. wait_for_completion(&vi->have_data);
  83. }
  84. static int probe_common(struct virtio_device *vdev)
  85. {
  86. int err, index;
  87. struct virtrng_info *vi = NULL;
  88. vi = kzalloc(sizeof(struct virtrng_info), GFP_KERNEL);
  89. if (!vi)
  90. return -ENOMEM;
  91. vi->index = index = ida_simple_get(&rng_index_ida, 0, 0, GFP_KERNEL);
  92. if (index < 0) {
  93. kfree(vi);
  94. return index;
  95. }
  96. sprintf(vi->name, "virtio_rng.%d", index);
  97. init_completion(&vi->have_data);
  98. vi->hwrng = (struct hwrng) {
  99. .read = virtio_read,
  100. .cleanup = virtio_cleanup,
  101. .priv = (unsigned long)vi,
  102. .name = vi->name,
  103. };
  104. vdev->priv = vi;
  105. /* We expect a single virtqueue. */
  106. vi->vq = virtio_find_single_vq(vdev, random_recv_done, "input");
  107. if (IS_ERR(vi->vq)) {
  108. err = PTR_ERR(vi->vq);
  109. vi->vq = NULL;
  110. kfree(vi);
  111. ida_simple_remove(&rng_index_ida, index);
  112. return err;
  113. }
  114. err = hwrng_register(&vi->hwrng);
  115. if (err) {
  116. vdev->config->del_vqs(vdev);
  117. vi->vq = NULL;
  118. kfree(vi);
  119. ida_simple_remove(&rng_index_ida, index);
  120. return err;
  121. }
  122. probe_done = true;
  123. return 0;
  124. }
  125. static void remove_common(struct virtio_device *vdev)
  126. {
  127. struct virtrng_info *vi = vdev->priv;
  128. vdev->config->reset(vdev);
  129. vi->busy = false;
  130. hwrng_unregister(&vi->hwrng);
  131. vdev->config->del_vqs(vdev);
  132. ida_simple_remove(&rng_index_ida, vi->index);
  133. kfree(vi);
  134. }
  135. static int virtrng_probe(struct virtio_device *vdev)
  136. {
  137. return probe_common(vdev);
  138. }
  139. static void virtrng_remove(struct virtio_device *vdev)
  140. {
  141. remove_common(vdev);
  142. }
  143. #ifdef CONFIG_PM_SLEEP
  144. static int virtrng_freeze(struct virtio_device *vdev)
  145. {
  146. remove_common(vdev);
  147. return 0;
  148. }
  149. static int virtrng_restore(struct virtio_device *vdev)
  150. {
  151. return probe_common(vdev);
  152. }
  153. #endif
  154. static struct virtio_device_id id_table[] = {
  155. { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
  156. { 0 },
  157. };
  158. static struct virtio_driver virtio_rng_driver = {
  159. .driver.name = KBUILD_MODNAME,
  160. .driver.owner = THIS_MODULE,
  161. .id_table = id_table,
  162. .probe = virtrng_probe,
  163. .remove = virtrng_remove,
  164. #ifdef CONFIG_PM_SLEEP
  165. .freeze = virtrng_freeze,
  166. .restore = virtrng_restore,
  167. #endif
  168. };
  169. module_virtio_driver(virtio_rng_driver);
  170. MODULE_DEVICE_TABLE(virtio, id_table);
  171. MODULE_DESCRIPTION("Virtio random number driver");
  172. MODULE_LICENSE("GPL");