cavium-rng.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #define THUNDERX_RNM_ENT_EN 0x1
  17. #define THUNDERX_RNM_RNG_EN 0x2
  18. struct cavium_rng_pf {
  19. void __iomem *control_status;
  20. };
  21. /* Enable the RNG hardware and activate the VF */
  22. static int cavium_rng_probe(struct pci_dev *pdev,
  23. const struct pci_device_id *id)
  24. {
  25. struct cavium_rng_pf *rng;
  26. int iov_err;
  27. rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
  28. if (!rng)
  29. return -ENOMEM;
  30. /*Map the RNG control */
  31. rng->control_status = pcim_iomap(pdev, 0, 0);
  32. if (!rng->control_status) {
  33. dev_err(&pdev->dev,
  34. "Error iomap failed retrieving control_status.\n");
  35. return -ENOMEM;
  36. }
  37. /* Enable the RNG hardware and entropy source */
  38. writeq(THUNDERX_RNM_RNG_EN | THUNDERX_RNM_ENT_EN,
  39. rng->control_status);
  40. pci_set_drvdata(pdev, rng);
  41. /* Enable the Cavium RNG as a VF */
  42. iov_err = pci_enable_sriov(pdev, 1);
  43. if (iov_err != 0) {
  44. /* Disable the RNG hardware and entropy source */
  45. writeq(0, rng->control_status);
  46. dev_err(&pdev->dev,
  47. "Error initializing RNG virtual function,(%i).\n",
  48. iov_err);
  49. return iov_err;
  50. }
  51. return 0;
  52. }
  53. /* Disable VF and RNG Hardware */
  54. void cavium_rng_remove(struct pci_dev *pdev)
  55. {
  56. struct cavium_rng_pf *rng;
  57. rng = pci_get_drvdata(pdev);
  58. /* Remove the VF */
  59. pci_disable_sriov(pdev);
  60. /* Disable the RNG hardware and entropy source */
  61. writeq(0, rng->control_status);
  62. }
  63. static const struct pci_device_id cavium_rng_pf_id_table[] = {
  64. { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, 0xa018), 0, 0, 0}, /* Thunder RNM */
  65. {0,},
  66. };
  67. MODULE_DEVICE_TABLE(pci, cavium_rng_pf_id_table);
  68. static struct pci_driver cavium_rng_pf_driver = {
  69. .name = "cavium_rng_pf",
  70. .id_table = cavium_rng_pf_id_table,
  71. .probe = cavium_rng_probe,
  72. .remove = cavium_rng_remove,
  73. };
  74. module_pci_driver(cavium_rng_pf_driver);
  75. MODULE_AUTHOR("Omer Khaliq <okhaliq@caviumnetworks.com>");
  76. MODULE_LICENSE("GPL");