Linux 4.18.10
[linux/fpc-iii.git] / drivers / char / ipmi / ipmi_si_pci.c
blobf54ca6869ed2c3f62d3c2d31daea3c5044642b9f
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * ipmi_si_pci.c
5 * Handling for IPMI devices on the PCI bus.
6 */
7 #include <linux/module.h>
8 #include <linux/pci.h>
9 #include "ipmi_si.h"
11 #define PFX "ipmi_pci: "
13 static bool pci_registered;
15 static bool si_trypci = true;
17 module_param_named(trypci, si_trypci, bool, 0);
18 MODULE_PARM_DESC(trypci, "Setting this to zero will disable the"
19 " default scan of the interfaces identified via pci");
21 #define PCI_CLASS_SERIAL_IPMI 0x0c07
22 #define PCI_CLASS_SERIAL_IPMI_SMIC 0x0c0700
23 #define PCI_CLASS_SERIAL_IPMI_KCS 0x0c0701
24 #define PCI_CLASS_SERIAL_IPMI_BT 0x0c0702
26 #define PCI_DEVICE_ID_HP_MMC 0x121A
28 static void ipmi_pci_cleanup(struct si_sm_io *io)
30 struct pci_dev *pdev = io->addr_source_data;
32 pci_disable_device(pdev);
35 static int ipmi_pci_probe_regspacing(struct si_sm_io *io)
37 if (io->si_type == SI_KCS) {
38 unsigned char status;
39 int regspacing;
41 io->regsize = DEFAULT_REGSIZE;
42 io->regshift = 0;
44 /* detect 1, 4, 16byte spacing */
45 for (regspacing = DEFAULT_REGSPACING; regspacing <= 16;) {
46 io->regspacing = regspacing;
47 if (io->io_setup(io)) {
48 dev_err(io->dev,
49 "Could not setup I/O space\n");
50 return DEFAULT_REGSPACING;
52 /* write invalid cmd */
53 io->outputb(io, 1, 0x10);
54 /* read status back */
55 status = io->inputb(io, 1);
56 io->io_cleanup(io);
57 if (status)
58 return regspacing;
59 regspacing *= 4;
62 return DEFAULT_REGSPACING;
65 static struct pci_device_id ipmi_pci_blacklist[] = {
67 * This is a "Virtual IPMI device", whatever that is. It appears
68 * as a KCS device by the class, but it is not one.
70 { PCI_VDEVICE(REALTEK, 0x816c) },
71 { 0, }
74 static int ipmi_pci_probe(struct pci_dev *pdev,
75 const struct pci_device_id *ent)
77 int rv;
78 struct si_sm_io io;
80 if (pci_match_id(ipmi_pci_blacklist, pdev))
81 return -ENODEV;
83 memset(&io, 0, sizeof(io));
84 io.addr_source = SI_PCI;
85 dev_info(&pdev->dev, "probing via PCI");
87 switch (pdev->class) {
88 case PCI_CLASS_SERIAL_IPMI_SMIC:
89 io.si_type = SI_SMIC;
90 break;
92 case PCI_CLASS_SERIAL_IPMI_KCS:
93 io.si_type = SI_KCS;
94 break;
96 case PCI_CLASS_SERIAL_IPMI_BT:
97 io.si_type = SI_BT;
98 break;
100 default:
101 dev_info(&pdev->dev, "Unknown IPMI class: %x\n", pdev->class);
102 return -ENOMEM;
105 rv = pci_enable_device(pdev);
106 if (rv) {
107 dev_err(&pdev->dev, "couldn't enable PCI device\n");
108 return rv;
111 io.addr_source_cleanup = ipmi_pci_cleanup;
112 io.addr_source_data = pdev;
114 if (pci_resource_flags(pdev, 0) & IORESOURCE_IO) {
115 io.addr_type = IPMI_IO_ADDR_SPACE;
116 io.io_setup = ipmi_si_port_setup;
117 } else {
118 io.addr_type = IPMI_MEM_ADDR_SPACE;
119 io.io_setup = ipmi_si_mem_setup;
121 io.addr_data = pci_resource_start(pdev, 0);
123 io.regspacing = ipmi_pci_probe_regspacing(&io);
124 io.regsize = DEFAULT_REGSIZE;
125 io.regshift = 0;
127 io.irq = pdev->irq;
128 if (io.irq)
129 io.irq_setup = ipmi_std_irq_setup;
131 io.dev = &pdev->dev;
133 dev_info(&pdev->dev, "%pR regsize %d spacing %d irq %d\n",
134 &pdev->resource[0], io.regsize, io.regspacing, io.irq);
136 rv = ipmi_si_add_smi(&io);
137 if (rv)
138 pci_disable_device(pdev);
140 return rv;
143 static void ipmi_pci_remove(struct pci_dev *pdev)
145 ipmi_si_remove_by_dev(&pdev->dev);
148 static const struct pci_device_id ipmi_pci_devices[] = {
149 { PCI_VDEVICE(HP, PCI_DEVICE_ID_HP_MMC) },
150 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_IPMI_SMIC, ~0) },
151 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_IPMI_KCS, ~0) },
152 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_IPMI_BT, ~0) },
153 { 0, }
155 MODULE_DEVICE_TABLE(pci, ipmi_pci_devices);
157 static struct pci_driver ipmi_pci_driver = {
158 .name = DEVICE_NAME,
159 .id_table = ipmi_pci_devices,
160 .probe = ipmi_pci_probe,
161 .remove = ipmi_pci_remove,
164 void ipmi_si_pci_init(void)
166 if (si_trypci) {
167 int rv = pci_register_driver(&ipmi_pci_driver);
168 if (rv)
169 pr_err(PFX "Unable to register PCI driver: %d\n", rv);
170 else
171 pci_registered = true;
175 void ipmi_si_pci_shutdown(void)
177 if (pci_registered)
178 pci_unregister_driver(&ipmi_pci_driver);