pseries-rng.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C) 2010 Michael Neuling IBM Corporation
  3. *
  4. * Driver for the pseries hardware RNG for POWER7+ and above
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/hw_random.h>
  23. #include <asm/vio.h>
  24. static int pseries_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  25. {
  26. u64 buffer[PLPAR_HCALL_BUFSIZE];
  27. size_t size = max < 8 ? max : 8;
  28. int rc;
  29. rc = plpar_hcall(H_RANDOM, (unsigned long *)buffer);
  30. if (rc != H_SUCCESS) {
  31. pr_err_ratelimited("H_RANDOM call failed %d\n", rc);
  32. return -EIO;
  33. }
  34. memcpy(data, buffer, size);
  35. /* The hypervisor interface returns 64 bits */
  36. return size;
  37. }
  38. /**
  39. * pseries_rng_get_desired_dma - Return desired DMA allocate for CMO operations
  40. *
  41. * This is a required function for a driver to operate in a CMO environment
  42. * but this device does not make use of DMA allocations, return 0.
  43. *
  44. * Return value:
  45. * Number of bytes of IO data the driver will need to perform well -> 0
  46. */
  47. static unsigned long pseries_rng_get_desired_dma(struct vio_dev *vdev)
  48. {
  49. return 0;
  50. };
  51. static struct hwrng pseries_rng = {
  52. .name = KBUILD_MODNAME,
  53. .read = pseries_rng_read,
  54. };
  55. static int pseries_rng_probe(struct vio_dev *dev,
  56. const struct vio_device_id *id)
  57. {
  58. return hwrng_register(&pseries_rng);
  59. }
  60. static int pseries_rng_remove(struct vio_dev *dev)
  61. {
  62. hwrng_unregister(&pseries_rng);
  63. return 0;
  64. }
  65. static struct vio_device_id pseries_rng_driver_ids[] = {
  66. { "ibm,random-v1", "ibm,random"},
  67. { "", "" }
  68. };
  69. MODULE_DEVICE_TABLE(vio, pseries_rng_driver_ids);
  70. static struct vio_driver pseries_rng_driver = {
  71. .name = KBUILD_MODNAME,
  72. .probe = pseries_rng_probe,
  73. .remove = pseries_rng_remove,
  74. .get_desired_dma = pseries_rng_get_desired_dma,
  75. .id_table = pseries_rng_driver_ids
  76. };
  77. static int __init rng_init(void)
  78. {
  79. pr_info("Registering IBM pSeries RNG driver\n");
  80. return vio_register_driver(&pseries_rng_driver);
  81. }
  82. module_init(rng_init);
  83. static void __exit rng_exit(void)
  84. {
  85. vio_unregister_driver(&pseries_rng_driver);
  86. }
  87. module_exit(rng_exit);
  88. MODULE_LICENSE("GPL");
  89. MODULE_AUTHOR("Michael Neuling <mikey@neuling.org>");
  90. MODULE_DESCRIPTION("H/W RNG driver for IBM pSeries processors");