Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * ipmi_si_pci.c
0004  *
0005  * Handling for IPMI devices on the PCI bus.
0006  */
0007 
0008 #define pr_fmt(fmt) "ipmi_pci: " fmt
0009 
0010 #include <linux/module.h>
0011 #include <linux/pci.h>
0012 #include "ipmi_si.h"
0013 
0014 static bool pci_registered;
0015 
0016 static bool si_trypci = true;
0017 
0018 module_param_named(trypci, si_trypci, bool, 0);
0019 MODULE_PARM_DESC(trypci,
0020          "Setting this to zero will disable the default scan of the interfaces identified via pci");
0021 
0022 #define PCI_DEVICE_ID_HP_MMC 0x121A
0023 
0024 static int ipmi_pci_probe_regspacing(struct si_sm_io *io)
0025 {
0026     if (io->si_type == SI_KCS) {
0027         unsigned char   status;
0028         int     regspacing;
0029 
0030         io->regsize = DEFAULT_REGSIZE;
0031         io->regshift = 0;
0032 
0033         /* detect 1, 4, 16byte spacing */
0034         for (regspacing = DEFAULT_REGSPACING; regspacing <= 16;) {
0035             io->regspacing = regspacing;
0036             if (io->io_setup(io)) {
0037                 dev_err(io->dev, "Could not setup I/O space\n");
0038                 return DEFAULT_REGSPACING;
0039             }
0040             /* write invalid cmd */
0041             io->outputb(io, 1, 0x10);
0042             /* read status back */
0043             status = io->inputb(io, 1);
0044             io->io_cleanup(io);
0045             if (status)
0046                 return regspacing;
0047             regspacing *= 4;
0048         }
0049     }
0050     return DEFAULT_REGSPACING;
0051 }
0052 
0053 static struct pci_device_id ipmi_pci_blacklist[] = {
0054     /*
0055      * This is a "Virtual IPMI device", whatever that is.  It appears
0056      * as a KCS device by the class, but it is not one.
0057      */
0058     { PCI_VDEVICE(REALTEK, 0x816c) },
0059     { 0, }
0060 };
0061 
0062 static int ipmi_pci_probe(struct pci_dev *pdev,
0063                     const struct pci_device_id *ent)
0064 {
0065     int rv;
0066     struct si_sm_io io;
0067 
0068     if (pci_match_id(ipmi_pci_blacklist, pdev))
0069         return -ENODEV;
0070 
0071     memset(&io, 0, sizeof(io));
0072     io.addr_source = SI_PCI;
0073     dev_info(&pdev->dev, "probing via PCI");
0074 
0075     switch (pdev->class) {
0076     case PCI_CLASS_SERIAL_IPMI_SMIC:
0077         io.si_type = SI_SMIC;
0078         break;
0079 
0080     case PCI_CLASS_SERIAL_IPMI_KCS:
0081         io.si_type = SI_KCS;
0082         break;
0083 
0084     case PCI_CLASS_SERIAL_IPMI_BT:
0085         io.si_type = SI_BT;
0086         break;
0087 
0088     default:
0089         dev_info(&pdev->dev, "Unknown IPMI class: %x\n", pdev->class);
0090         return -ENOMEM;
0091     }
0092 
0093     rv = pcim_enable_device(pdev);
0094     if (rv) {
0095         dev_err(&pdev->dev, "couldn't enable PCI device\n");
0096         return rv;
0097     }
0098 
0099     if (pci_resource_flags(pdev, 0) & IORESOURCE_IO) {
0100         io.addr_space = IPMI_IO_ADDR_SPACE;
0101         io.io_setup = ipmi_si_port_setup;
0102     } else {
0103         io.addr_space = IPMI_MEM_ADDR_SPACE;
0104         io.io_setup = ipmi_si_mem_setup;
0105     }
0106     io.addr_data = pci_resource_start(pdev, 0);
0107 
0108     io.dev = &pdev->dev;
0109 
0110     io.regspacing = ipmi_pci_probe_regspacing(&io);
0111     io.regsize = DEFAULT_REGSIZE;
0112     io.regshift = 0;
0113 
0114     io.irq = pdev->irq;
0115     if (io.irq)
0116         io.irq_setup = ipmi_std_irq_setup;
0117 
0118     dev_info(&pdev->dev, "%pR regsize %d spacing %d irq %d\n",
0119          &pdev->resource[0], io.regsize, io.regspacing, io.irq);
0120 
0121     return ipmi_si_add_smi(&io);
0122 }
0123 
0124 static void ipmi_pci_remove(struct pci_dev *pdev)
0125 {
0126     ipmi_si_remove_by_dev(&pdev->dev);
0127 }
0128 
0129 static const struct pci_device_id ipmi_pci_devices[] = {
0130     { PCI_VDEVICE(HP, PCI_DEVICE_ID_HP_MMC) },
0131     { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_IPMI_SMIC, ~0) },
0132     { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_IPMI_KCS, ~0) },
0133     { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_IPMI_BT, ~0) },
0134     { 0, }
0135 };
0136 MODULE_DEVICE_TABLE(pci, ipmi_pci_devices);
0137 
0138 static struct pci_driver ipmi_pci_driver = {
0139     .name =         SI_DEVICE_NAME,
0140     .id_table =     ipmi_pci_devices,
0141     .probe =        ipmi_pci_probe,
0142     .remove =       ipmi_pci_remove,
0143 };
0144 
0145 void ipmi_si_pci_init(void)
0146 {
0147     if (si_trypci) {
0148         int rv = pci_register_driver(&ipmi_pci_driver);
0149         if (rv)
0150             pr_err("Unable to register PCI driver: %d\n", rv);
0151         else
0152             pci_registered = true;
0153     }
0154 }
0155 
0156 void ipmi_si_pci_shutdown(void)
0157 {
0158     if (pci_registered)
0159         pci_unregister_driver(&ipmi_pci_driver);
0160 }