1 // SPDX-License-Identifier: GPL-2.0+
5 * Handling for IPMI devices on the PCI bus.
8 #define pr_fmt(fmt) "ipmi_pci: " fmt
10 #include <linux/module.h>
11 #include <linux/pci.h>
14 static bool pci_registered;
16 static bool si_trypci = true;
18 module_param_named(trypci, si_trypci, bool, 0);
19 MODULE_PARM_DESC(trypci, "Setting this to zero will disable the"
20 " default scan of the interfaces identified via pci");
22 #define PCI_DEVICE_ID_HP_MMC 0x121A
24 static void ipmi_pci_cleanup(struct si_sm_io *io)
26 struct pci_dev *pdev = io->addr_source_data;
28 pci_disable_device(pdev);
31 static int ipmi_pci_probe_regspacing(struct si_sm_io *io)
33 if (io->si_type == SI_KCS) {
37 io->regsize = DEFAULT_REGSIZE;
40 /* detect 1, 4, 16byte spacing */
41 for (regspacing = DEFAULT_REGSPACING; regspacing <= 16;) {
42 io->regspacing = regspacing;
43 if (io->io_setup(io)) {
44 dev_err(io->dev, "Could not setup I/O space\n");
45 return DEFAULT_REGSPACING;
47 /* write invalid cmd */
48 io->outputb(io, 1, 0x10);
49 /* read status back */
50 status = io->inputb(io, 1);
57 return DEFAULT_REGSPACING;
60 static struct pci_device_id ipmi_pci_blacklist[] = {
62 * This is a "Virtual IPMI device", whatever that is. It appears
63 * as a KCS device by the class, but it is not one.
65 { PCI_VDEVICE(REALTEK, 0x816c) },
69 static int ipmi_pci_probe(struct pci_dev *pdev,
70 const struct pci_device_id *ent)
75 if (pci_match_id(ipmi_pci_blacklist, pdev))
78 memset(&io, 0, sizeof(io));
79 io.addr_source = SI_PCI;
80 dev_info(&pdev->dev, "probing via PCI");
82 switch (pdev->class) {
83 case PCI_CLASS_SERIAL_IPMI_SMIC:
87 case PCI_CLASS_SERIAL_IPMI_KCS:
91 case PCI_CLASS_SERIAL_IPMI_BT:
96 dev_info(&pdev->dev, "Unknown IPMI class: %x\n", pdev->class);
100 rv = pci_enable_device(pdev);
102 dev_err(&pdev->dev, "couldn't enable PCI device\n");
106 io.addr_source_cleanup = ipmi_pci_cleanup;
107 io.addr_source_data = pdev;
109 if (pci_resource_flags(pdev, 0) & IORESOURCE_IO) {
110 io.addr_space = IPMI_IO_ADDR_SPACE;
111 io.io_setup = ipmi_si_port_setup;
113 io.addr_space = IPMI_MEM_ADDR_SPACE;
114 io.io_setup = ipmi_si_mem_setup;
116 io.addr_data = pci_resource_start(pdev, 0);
120 io.regspacing = ipmi_pci_probe_regspacing(&io);
121 io.regsize = DEFAULT_REGSIZE;
126 io.irq_setup = ipmi_std_irq_setup;
128 dev_info(&pdev->dev, "%pR regsize %d spacing %d irq %d\n",
129 &pdev->resource[0], io.regsize, io.regspacing, io.irq);
131 rv = ipmi_si_add_smi(&io);
133 pci_disable_device(pdev);
138 static void ipmi_pci_remove(struct pci_dev *pdev)
140 ipmi_si_remove_by_dev(&pdev->dev);
143 static const struct pci_device_id ipmi_pci_devices[] = {
144 { PCI_VDEVICE(HP, PCI_DEVICE_ID_HP_MMC) },
145 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_IPMI_SMIC, ~0) },
146 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_IPMI_KCS, ~0) },
147 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_IPMI_BT, ~0) },
150 MODULE_DEVICE_TABLE(pci, ipmi_pci_devices);
152 static struct pci_driver ipmi_pci_driver = {
153 .name = SI_DEVICE_NAME,
154 .id_table = ipmi_pci_devices,
155 .probe = ipmi_pci_probe,
156 .remove = ipmi_pci_remove,
159 void ipmi_si_pci_init(void)
162 int rv = pci_register_driver(&ipmi_pci_driver);
164 pr_err("Unable to register PCI driver: %d\n", rv);
166 pci_registered = true;
170 void ipmi_si_pci_shutdown(void)
173 pci_unregister_driver(&ipmi_pci_driver);