cavium-rng-vf.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Hardware Random Number Generator support for Cavium, Inc.
  3. * Thunder processor family.
  4. *
  5. * This file is subject to the terms and conditions of the GNU General Public
  6. * License. See the file "COPYING" in the main directory of this archive
  7. * for more details.
  8. *
  9. * Copyright (C) 2016 Cavium, Inc.
  10. */
  11. #include <linux/hw_random.h>
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/pci.h>
  15. #include <linux/pci_ids.h>
  16. struct cavium_rng {
  17. struct hwrng ops;
  18. void __iomem *result;
  19. };
  20. /* Read data from the RNG unit */
  21. static int cavium_rng_read(struct hwrng *rng, void *dat, size_t max, bool wait)
  22. {
  23. struct cavium_rng *p = container_of(rng, struct cavium_rng, ops);
  24. unsigned int size = max;
  25. while (size >= 8) {
  26. *((u64 *)dat) = readq(p->result);
  27. size -= 8;
  28. dat += 8;
  29. }
  30. while (size > 0) {
  31. *((u8 *)dat) = readb(p->result);
  32. size--;
  33. dat++;
  34. }
  35. return max;
  36. }
  37. /* Map Cavium RNG to an HWRNG object */
  38. static int cavium_rng_probe_vf(struct pci_dev *pdev,
  39. const struct pci_device_id *id)
  40. {
  41. struct cavium_rng *rng;
  42. int ret;
  43. rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
  44. if (!rng)
  45. return -ENOMEM;
  46. /* Map the RNG result */
  47. rng->result = pcim_iomap(pdev, 0, 0);
  48. if (!rng->result) {
  49. dev_err(&pdev->dev, "Error iomap failed retrieving result.\n");
  50. return -ENOMEM;
  51. }
  52. rng->ops.name = "cavium rng";
  53. rng->ops.read = cavium_rng_read;
  54. rng->ops.quality = 1000;
  55. pci_set_drvdata(pdev, rng);
  56. ret = hwrng_register(&rng->ops);
  57. if (ret) {
  58. dev_err(&pdev->dev, "Error registering device as HWRNG.\n");
  59. return ret;
  60. }
  61. return 0;
  62. }
  63. /* Remove the VF */
  64. void cavium_rng_remove_vf(struct pci_dev *pdev)
  65. {
  66. struct cavium_rng *rng;
  67. rng = pci_get_drvdata(pdev);
  68. hwrng_unregister(&rng->ops);
  69. }
  70. static const struct pci_device_id cavium_rng_vf_id_table[] = {
  71. { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, 0xa033), 0, 0, 0},
  72. {0,},
  73. };
  74. MODULE_DEVICE_TABLE(pci, cavium_rng_vf_id_table);
  75. static struct pci_driver cavium_rng_vf_driver = {
  76. .name = "cavium_rng_vf",
  77. .id_table = cavium_rng_vf_id_table,
  78. .probe = cavium_rng_probe_vf,
  79. .remove = cavium_rng_remove_vf,
  80. };
  81. module_pci_driver(cavium_rng_vf_driver);
  82. MODULE_AUTHOR("Omer Khaliq <okhaliq@caviumnetworks.com>");
  83. MODULE_LICENSE("GPL");