2 * Copyright (c) 2015-2016 Mellanox Technologies, Ltd.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/kernel.h>
34 #include <sys/sysctl.h>
36 #include <sys/mutex.h>
38 #include <sys/fcntl.h>
40 #include <sys/filio.h>
41 #include <sys/rwlock.h>
46 #include <machine/stdarg.h>
48 #include <linux/kobject.h>
49 #include <linux/device.h>
50 #include <linux/slab.h>
51 #include <linux/module.h>
52 #include <linux/cdev.h>
53 #include <linux/file.h>
54 #include <linux/sysfs.h>
57 #include <linux/vmalloc.h>
58 #include <linux/pci.h>
59 #include <linux/compat.h>
61 static device_probe_t linux_pci_probe;
62 static device_attach_t linux_pci_attach;
63 static device_detach_t linux_pci_detach;
64 static device_suspend_t linux_pci_suspend;
65 static device_resume_t linux_pci_resume;
66 static device_shutdown_t linux_pci_shutdown;
68 static device_method_t pci_methods[] = {
69 DEVMETHOD(device_probe, linux_pci_probe),
70 DEVMETHOD(device_attach, linux_pci_attach),
71 DEVMETHOD(device_detach, linux_pci_detach),
72 DEVMETHOD(device_suspend, linux_pci_suspend),
73 DEVMETHOD(device_resume, linux_pci_resume),
74 DEVMETHOD(device_shutdown, linux_pci_shutdown),
78 static struct pci_driver *
79 linux_pci_find(device_t dev, const struct pci_device_id **idp)
81 const struct pci_device_id *id;
82 struct pci_driver *pdrv;
88 vendor = pci_get_vendor(dev);
89 device = pci_get_device(dev);
90 subvendor = pci_get_subvendor(dev);
91 subdevice = pci_get_subdevice(dev);
94 list_for_each_entry(pdrv, &pci_drivers, links) {
95 for (id = pdrv->id_table; id->vendor != 0; id++) {
96 if (vendor == id->vendor &&
97 (PCI_ANY_ID == id->device || device == id->device) &&
98 (PCI_ANY_ID == id->subvendor || subvendor == id->subvendor) &&
99 (PCI_ANY_ID == id->subdevice || subdevice == id->subdevice)) {
101 spin_unlock(&pci_lock);
106 spin_unlock(&pci_lock);
111 linux_pci_probe(device_t dev)
113 const struct pci_device_id *id;
114 struct pci_driver *pdrv;
116 if ((pdrv = linux_pci_find(dev, &id)) == NULL)
118 if (device_get_driver(dev) != &pdrv->bsddriver)
120 device_set_desc(dev, pdrv->name);
125 linux_pci_attach(device_t dev)
127 struct resource_list_entry *rle;
128 struct pci_bus *pbus;
129 struct pci_dev *pdev;
130 struct pci_devinfo *dinfo;
131 struct pci_driver *pdrv;
132 const struct pci_device_id *id;
137 linux_set_current(curthread);
139 pdrv = linux_pci_find(dev, &id);
140 pdev = device_get_softc(dev);
142 parent = device_get_parent(dev);
143 devclass = device_get_devclass(parent);
145 dinfo = device_get_ivars(parent);
146 device_set_ivars(dev, dinfo);
148 dinfo = device_get_ivars(dev);
151 pdev->dev.parent = &linux_root_device;
152 pdev->dev.bsddev = dev;
153 INIT_LIST_HEAD(&pdev->dev.irqents);
154 pdev->devfn = PCI_DEVFN(pci_get_slot(dev), pci_get_function(dev));
155 pdev->device = dinfo->cfg.device;
156 pdev->vendor = dinfo->cfg.vendor;
157 pdev->subsystem_vendor = dinfo->cfg.subvendor;
158 pdev->subsystem_device = dinfo->cfg.subdevice;
159 pdev->class = pci_get_class(dev);
160 pdev->revision = pci_get_revid(dev);
161 pdev->dev.dma_mask = &pdev->dma_mask;
163 kobject_init(&pdev->dev.kobj, &linux_dev_ktype);
164 kobject_set_name(&pdev->dev.kobj, device_get_nameunit(dev));
165 kobject_add(&pdev->dev.kobj, &linux_root_device.kobj,
166 kobject_name(&pdev->dev.kobj));
167 rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 0);
169 pdev->dev.irq = rle->start;
171 pdev->dev.irq = LINUX_IRQ_INVALID;
172 pdev->irq = pdev->dev.irq;
174 if (pdev->bus == NULL) {
175 pbus = malloc(sizeof(*pbus), M_DEVBUF, M_WAITOK | M_ZERO);
177 pbus->number = pci_get_bus(dev);
181 spin_lock(&pci_lock);
182 list_add(&pdev->links, &pci_devices);
183 spin_unlock(&pci_lock);
185 error = pdrv->probe(pdev, id);
187 spin_lock(&pci_lock);
188 list_del(&pdev->links);
189 spin_unlock(&pci_lock);
190 put_device(&pdev->dev);
197 linux_pci_detach(device_t dev)
199 struct pci_dev *pdev;
201 linux_set_current(curthread);
202 pdev = device_get_softc(dev);
204 pdev->pdrv->remove(pdev);
206 spin_lock(&pci_lock);
207 list_del(&pdev->links);
208 spin_unlock(&pci_lock);
209 device_set_desc(dev, NULL);
210 put_device(&pdev->dev);
216 linux_pci_suspend(device_t dev)
218 const struct dev_pm_ops *pmops;
219 struct pm_message pm = { };
220 struct pci_dev *pdev;
224 linux_set_current(curthread);
225 pdev = device_get_softc(dev);
226 pmops = pdev->pdrv->driver.pm;
228 if (pdev->pdrv->suspend != NULL)
229 error = -pdev->pdrv->suspend(pdev, pm);
230 else if (pmops != NULL && pmops->suspend != NULL) {
231 error = -pmops->suspend(&pdev->dev);
232 if (error == 0 && pmops->suspend_late != NULL)
233 error = -pmops->suspend_late(&pdev->dev);
239 linux_pci_resume(device_t dev)
241 const struct dev_pm_ops *pmops;
242 struct pci_dev *pdev;
246 linux_set_current(curthread);
247 pdev = device_get_softc(dev);
248 pmops = pdev->pdrv->driver.pm;
250 if (pdev->pdrv->resume != NULL)
251 error = -pdev->pdrv->resume(pdev);
252 else if (pmops != NULL && pmops->resume != NULL) {
253 if (pmops->resume_early != NULL)
254 error = -pmops->resume_early(&pdev->dev);
255 if (error == 0 && pmops->resume != NULL)
256 error = -pmops->resume(&pdev->dev);
262 linux_pci_shutdown(device_t dev)
264 struct pci_dev *pdev;
266 linux_set_current(curthread);
267 pdev = device_get_softc(dev);
268 if (pdev->pdrv->shutdown != NULL)
269 pdev->pdrv->shutdown(pdev);
274 _linux_pci_register_driver(struct pci_driver *pdrv, devclass_t dc)
278 linux_set_current(curthread);
279 spin_lock(&pci_lock);
280 list_add(&pdrv->links, &pci_drivers);
281 spin_unlock(&pci_lock);
282 pdrv->bsddriver.name = pdrv->name;
283 pdrv->bsddriver.methods = pci_methods;
284 pdrv->bsddriver.size = sizeof(struct pci_dev);
287 error = devclass_add_driver(dc, &pdrv->bsddriver,
288 BUS_PASS_DEFAULT, &pdrv->bsdclass);
294 linux_pci_register_driver(struct pci_driver *pdrv)
298 dc = devclass_find("pci");
302 return (_linux_pci_register_driver(pdrv, dc));
306 linux_pci_register_drm_driver(struct pci_driver *pdrv)
310 dc = devclass_create("vgapci");
315 return (_linux_pci_register_driver(pdrv, dc));
319 linux_pci_unregister_driver(struct pci_driver *pdrv)
323 bus = devclass_find("pci");
325 spin_lock(&pci_lock);
326 list_del(&pdrv->links);
327 spin_unlock(&pci_lock);
330 devclass_delete_driver(bus, &pdrv->bsddriver);