drm/tidss: Fix typos
[drm/drm-misc.git] / drivers / char / ipmi / ipmi_si_pci.c
blobb83d55685b221c3af0b9d5d31d94f38a77cf3b80
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * ipmi_si_pci.c
5 * Handling for IPMI devices on the PCI bus.
6 */
8 #define pr_fmt(fmt) "ipmi_pci: " fmt
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include "ipmi_si.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,
20 "Setting this to zero will disable the default scan of the interfaces identified via pci");
22 #define PCI_DEVICE_ID_HP_MMC 0x121A
24 static int ipmi_pci_probe_regspacing(struct si_sm_io *io)
26 if (io->si_type == SI_KCS) {
27 unsigned char status;
28 int regspacing;
30 io->regsize = DEFAULT_REGSIZE;
31 io->regshift = 0;
33 /* detect 1, 4, 16byte spacing */
34 for (regspacing = DEFAULT_REGSPACING; regspacing <= 16;) {
35 io->regspacing = regspacing;
36 if (io->io_setup(io)) {
37 dev_err(io->dev, "Could not setup I/O space\n");
38 return DEFAULT_REGSPACING;
40 /* write invalid cmd */
41 io->outputb(io, 1, 0x10);
42 /* read status back */
43 status = io->inputb(io, 1);
44 io->io_cleanup(io);
45 if (status)
46 return regspacing;
47 regspacing *= 4;
50 return DEFAULT_REGSPACING;
53 static struct pci_device_id ipmi_pci_blacklist[] = {
55 * This is a "Virtual IPMI device", whatever that is. It appears
56 * as a KCS device by the class, but it is not one.
58 { PCI_VDEVICE(REALTEK, 0x816c) },
59 { 0, }
62 static int ipmi_pci_probe(struct pci_dev *pdev,
63 const struct pci_device_id *ent)
65 int rv;
66 struct si_sm_io io;
68 if (pci_match_id(ipmi_pci_blacklist, pdev))
69 return -ENODEV;
71 memset(&io, 0, sizeof(io));
72 io.addr_source = SI_PCI;
73 dev_info(&pdev->dev, "probing via PCI");
75 switch (pdev->class) {
76 case PCI_CLASS_SERIAL_IPMI_SMIC:
77 io.si_type = SI_SMIC;
78 break;
80 case PCI_CLASS_SERIAL_IPMI_KCS:
81 io.si_type = SI_KCS;
82 break;
84 case PCI_CLASS_SERIAL_IPMI_BT:
85 io.si_type = SI_BT;
86 break;
88 default:
89 dev_info(&pdev->dev, "Unknown IPMI class: %x\n", pdev->class);
90 return -ENOMEM;
93 rv = pcim_enable_device(pdev);
94 if (rv) {
95 dev_err(&pdev->dev, "couldn't enable PCI device\n");
96 return rv;
99 if (pci_resource_flags(pdev, 0) & IORESOURCE_IO) {
100 if (!IS_ENABLED(CONFIG_HAS_IOPORT))
101 return -ENXIO;
103 io.addr_space = IPMI_IO_ADDR_SPACE;
104 io.io_setup = ipmi_si_port_setup;
105 } else {
106 io.addr_space = IPMI_MEM_ADDR_SPACE;
107 io.io_setup = ipmi_si_mem_setup;
109 io.addr_data = pci_resource_start(pdev, 0);
111 io.dev = &pdev->dev;
113 io.regspacing = ipmi_pci_probe_regspacing(&io);
114 io.regsize = DEFAULT_REGSIZE;
115 io.regshift = 0;
117 io.irq = pdev->irq;
118 if (io.irq)
119 io.irq_setup = ipmi_std_irq_setup;
121 dev_info(&pdev->dev, "%pR regsize %d spacing %d irq %d\n",
122 &pdev->resource[0], io.regsize, io.regspacing, io.irq);
124 return ipmi_si_add_smi(&io);
127 static void ipmi_pci_remove(struct pci_dev *pdev)
129 ipmi_si_remove_by_dev(&pdev->dev);
132 static const struct pci_device_id ipmi_pci_devices[] = {
133 { PCI_VDEVICE(HP, PCI_DEVICE_ID_HP_MMC) },
134 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_IPMI_SMIC, ~0) },
135 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_IPMI_KCS, ~0) },
136 { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_IPMI_BT, ~0) },
137 { 0, }
139 MODULE_DEVICE_TABLE(pci, ipmi_pci_devices);
141 static struct pci_driver ipmi_pci_driver = {
142 .name = SI_DEVICE_NAME,
143 .id_table = ipmi_pci_devices,
144 .probe = ipmi_pci_probe,
145 .remove = ipmi_pci_remove,
148 void ipmi_si_pci_init(void)
150 if (si_trypci) {
151 int rv = pci_register_driver(&ipmi_pci_driver);
152 if (rv)
153 pr_err("Unable to register PCI driver: %d\n", rv);
154 else
155 pci_registered = true;
159 void ipmi_si_pci_shutdown(void)
161 if (pci_registered)
162 pci_unregister_driver(&ipmi_pci_driver);