GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / char / ipmi / ipmi_si_pci.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * ipmi_si_pci.c
4  *
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"
10
11 #define PFX "ipmi_pci: "
12
13 static bool pci_registered;
14
15 static bool si_trypci = true;
16
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");
20
21 #define PCI_DEVICE_ID_HP_MMC 0x121A
22
23 static void ipmi_pci_cleanup(struct si_sm_io *io)
24 {
25         struct pci_dev *pdev = io->addr_source_data;
26
27         pci_disable_device(pdev);
28 }
29
30 static int ipmi_pci_probe_regspacing(struct si_sm_io *io)
31 {
32         if (io->si_type == SI_KCS) {
33                 unsigned char   status;
34                 int             regspacing;
35
36                 io->regsize = DEFAULT_REGSIZE;
37                 io->regshift = 0;
38
39                 /* detect 1, 4, 16byte spacing */
40                 for (regspacing = DEFAULT_REGSPACING; regspacing <= 16;) {
41                         io->regspacing = regspacing;
42                         if (io->io_setup(io)) {
43                                 dev_err(io->dev,
44                                         "Could not setup I/O space\n");
45                                 return DEFAULT_REGSPACING;
46                         }
47                         /* write invalid cmd */
48                         io->outputb(io, 1, 0x10);
49                         /* read status back */
50                         status = io->inputb(io, 1);
51                         io->io_cleanup(io);
52                         if (status)
53                                 return regspacing;
54                         regspacing *= 4;
55                 }
56         }
57         return DEFAULT_REGSPACING;
58 }
59
60 static struct pci_device_id ipmi_pci_blacklist[] = {
61         /*
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.
64          */
65         { PCI_VDEVICE(REALTEK, 0x816c) },
66         { 0, }
67 };
68
69 static int ipmi_pci_probe(struct pci_dev *pdev,
70                                     const struct pci_device_id *ent)
71 {
72         int rv;
73         struct si_sm_io io;
74
75         if (pci_match_id(ipmi_pci_blacklist, pdev))
76                 return -ENODEV;
77
78         memset(&io, 0, sizeof(io));
79         io.addr_source = SI_PCI;
80         dev_info(&pdev->dev, "probing via PCI");
81
82         switch (pdev->class) {
83         case PCI_CLASS_SERIAL_IPMI_SMIC:
84                 io.si_type = SI_SMIC;
85                 break;
86
87         case PCI_CLASS_SERIAL_IPMI_KCS:
88                 io.si_type = SI_KCS;
89                 break;
90
91         case PCI_CLASS_SERIAL_IPMI_BT:
92                 io.si_type = SI_BT;
93                 break;
94
95         default:
96                 dev_info(&pdev->dev, "Unknown IPMI class: %x\n", pdev->class);
97                 return -ENOMEM;
98         }
99
100         rv = pci_enable_device(pdev);
101         if (rv) {
102                 dev_err(&pdev->dev, "couldn't enable PCI device\n");
103                 return rv;
104         }
105
106         io.addr_source_cleanup = ipmi_pci_cleanup;
107         io.addr_source_data = pdev;
108
109         if (pci_resource_flags(pdev, 0) & IORESOURCE_IO) {
110                 io.addr_type = IPMI_IO_ADDR_SPACE;
111                 io.io_setup = ipmi_si_port_setup;
112         } else {
113                 io.addr_type = IPMI_MEM_ADDR_SPACE;
114                 io.io_setup = ipmi_si_mem_setup;
115         }
116         io.addr_data = pci_resource_start(pdev, 0);
117
118         io.dev = &pdev->dev;
119
120         io.regspacing = ipmi_pci_probe_regspacing(&io);
121         io.regsize = DEFAULT_REGSIZE;
122         io.regshift = 0;
123
124         io.irq = pdev->irq;
125         if (io.irq)
126                 io.irq_setup = ipmi_std_irq_setup;
127
128         dev_info(&pdev->dev, "%pR regsize %d spacing %d irq %d\n",
129                 &pdev->resource[0], io.regsize, io.regspacing, io.irq);
130
131         rv = ipmi_si_add_smi(&io);
132         if (rv)
133                 pci_disable_device(pdev);
134
135         return rv;
136 }
137
138 static void ipmi_pci_remove(struct pci_dev *pdev)
139 {
140         ipmi_si_remove_by_dev(&pdev->dev);
141 }
142
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) },
148         { 0, }
149 };
150 MODULE_DEVICE_TABLE(pci, ipmi_pci_devices);
151
152 static struct pci_driver ipmi_pci_driver = {
153         .name =         DEVICE_NAME,
154         .id_table =     ipmi_pci_devices,
155         .probe =        ipmi_pci_probe,
156         .remove =       ipmi_pci_remove,
157 };
158
159 void ipmi_si_pci_init(void)
160 {
161         if (si_trypci) {
162                 int rv = pci_register_driver(&ipmi_pci_driver);
163                 if (rv)
164                         pr_err(PFX "Unable to register PCI driver: %d\n", rv);
165                 else
166                         pci_registered = true;
167         }
168 }
169
170 void ipmi_si_pci_shutdown(void)
171 {
172         if (pci_registered)
173                 pci_unregister_driver(&ipmi_pci_driver);
174 }