Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Hardware Random Number Generator support.
0004  * Cavium Thunder, Marvell OcteonTx/Tx2 processor families.
0005  *
0006  * Copyright (C) 2016 Cavium, Inc.
0007  */
0008 
0009 #include <linux/hw_random.h>
0010 #include <linux/io.h>
0011 #include <linux/module.h>
0012 #include <linux/pci.h>
0013 #include <linux/pci_ids.h>
0014 
0015 #define THUNDERX_RNM_ENT_EN     0x1
0016 #define THUNDERX_RNM_RNG_EN     0x2
0017 
0018 struct cavium_rng_pf {
0019     void __iomem *control_status;
0020 };
0021 
0022 /* Enable the RNG hardware and activate the VF */
0023 static int cavium_rng_probe(struct pci_dev *pdev,
0024             const struct pci_device_id *id)
0025 {
0026     struct  cavium_rng_pf *rng;
0027     int iov_err;
0028 
0029     rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
0030     if (!rng)
0031         return -ENOMEM;
0032 
0033     /*Map the RNG control */
0034     rng->control_status = pcim_iomap(pdev, 0, 0);
0035     if (!rng->control_status) {
0036         dev_err(&pdev->dev,
0037             "Error iomap failed retrieving control_status.\n");
0038         return -ENOMEM;
0039     }
0040 
0041     /* Enable the RNG hardware and entropy source */
0042     writeq(THUNDERX_RNM_RNG_EN | THUNDERX_RNM_ENT_EN,
0043         rng->control_status);
0044 
0045     pci_set_drvdata(pdev, rng);
0046 
0047     /* Enable the Cavium RNG as a VF */
0048     iov_err = pci_enable_sriov(pdev, 1);
0049     if (iov_err != 0) {
0050         /* Disable the RNG hardware and entropy source */
0051         writeq(0, rng->control_status);
0052         dev_err(&pdev->dev,
0053             "Error initializing RNG virtual function,(%i).\n",
0054             iov_err);
0055         return iov_err;
0056     }
0057 
0058     return 0;
0059 }
0060 
0061 /* Disable VF and RNG Hardware */
0062 static void cavium_rng_remove(struct pci_dev *pdev)
0063 {
0064     struct cavium_rng_pf *rng;
0065 
0066     rng = pci_get_drvdata(pdev);
0067 
0068     /* Remove the VF */
0069     pci_disable_sriov(pdev);
0070 
0071     /* Disable the RNG hardware and entropy source */
0072     writeq(0, rng->control_status);
0073 }
0074 
0075 static const struct pci_device_id cavium_rng_pf_id_table[] = {
0076     { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, 0xa018), 0, 0, 0}, /* Thunder RNM */
0077     {0,},
0078 };
0079 
0080 MODULE_DEVICE_TABLE(pci, cavium_rng_pf_id_table);
0081 
0082 static struct pci_driver cavium_rng_pf_driver = {
0083     .name       = "cavium_rng_pf",
0084     .id_table   = cavium_rng_pf_id_table,
0085     .probe      = cavium_rng_probe,
0086     .remove     = cavium_rng_remove,
0087 };
0088 
0089 module_pci_driver(cavium_rng_pf_driver);
0090 MODULE_AUTHOR("Omer Khaliq <okhaliq@caviumnetworks.com>");
0091 MODULE_LICENSE("GPL v2");