^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Hardware Random Number Generator support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Cavium Thunder, Marvell OcteonTx/Tx2 processor families.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2016 Cavium, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/hw_random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/pci_ids.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define THUNDERX_RNM_ENT_EN 0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define THUNDERX_RNM_RNG_EN 0x2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct cavium_rng_pf {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) void __iomem *control_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /* Enable the RNG hardware and activate the VF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static int cavium_rng_probe(struct pci_dev *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) const struct pci_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct cavium_rng_pf *rng;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int iov_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (!rng)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /*Map the RNG control */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) rng->control_status = pcim_iomap(pdev, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (!rng->control_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) "Error iomap failed retrieving control_status.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* Enable the RNG hardware and entropy source */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) writeq(THUNDERX_RNM_RNG_EN | THUNDERX_RNM_ENT_EN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) rng->control_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) pci_set_drvdata(pdev, rng);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* Enable the Cavium RNG as a VF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) iov_err = pci_enable_sriov(pdev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (iov_err != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* Disable the RNG hardware and entropy source */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) writeq(0, rng->control_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) "Error initializing RNG virtual function,(%i).\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) iov_err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return iov_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /* Disable VF and RNG Hardware */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static void cavium_rng_remove(struct pci_dev *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct cavium_rng_pf *rng;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) rng = pci_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /* Remove the VF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) pci_disable_sriov(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /* Disable the RNG hardware and entropy source */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) writeq(0, rng->control_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static const struct pci_device_id cavium_rng_pf_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, 0xa018), 0, 0, 0}, /* Thunder RNM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) {0,},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) MODULE_DEVICE_TABLE(pci, cavium_rng_pf_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static struct pci_driver cavium_rng_pf_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .name = "cavium_rng_pf",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .id_table = cavium_rng_pf_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .probe = cavium_rng_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .remove = cavium_rng_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) module_pci_driver(cavium_rng_pf_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) MODULE_AUTHOR("Omer Khaliq <okhaliq@caviumnetworks.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) MODULE_LICENSE("GPL v2");