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;
86 vendor = pci_get_vendor(dev);
87 device = pci_get_device(dev);
90 list_for_each_entry(pdrv, &pci_drivers, links) {
91 for (id = pdrv->id_table; id->vendor != 0; id++) {
92 if (vendor == id->vendor && device == id->device) {
94 spin_unlock(&pci_lock);
99 spin_unlock(&pci_lock);
104 linux_pci_probe(device_t dev)
106 const struct pci_device_id *id;
107 struct pci_driver *pdrv;
109 if ((pdrv = linux_pci_find(dev, &id)) == NULL)
111 if (device_get_driver(dev) != &pdrv->driver)
113 device_set_desc(dev, pdrv->name);
118 linux_pci_attach(device_t dev)
120 struct resource_list_entry *rle;
121 struct pci_dev *pdev;
122 struct pci_driver *pdrv;
123 const struct pci_device_id *id;
124 struct task_struct t;
129 linux_set_current(td, &t);
130 pdrv = linux_pci_find(dev, &id);
131 pdev = device_get_softc(dev);
132 pdev->dev.parent = &linux_root_device;
133 pdev->dev.bsddev = dev;
134 INIT_LIST_HEAD(&pdev->dev.irqents);
135 pdev->device = id->device;
136 pdev->vendor = id->vendor;
137 pdev->dev.dma_mask = &pdev->dma_mask;
139 kobject_init(&pdev->dev.kobj, &linux_dev_ktype);
140 kobject_set_name(&pdev->dev.kobj, device_get_nameunit(dev));
141 kobject_add(&pdev->dev.kobj, &linux_root_device.kobj,
142 kobject_name(&pdev->dev.kobj));
143 rle = _pci_get_rle(pdev, SYS_RES_IRQ, 0);
145 pdev->dev.irq = rle->start;
148 pdev->irq = pdev->dev.irq;
150 spin_lock(&pci_lock);
151 list_add(&pdev->links, &pci_devices);
152 spin_unlock(&pci_lock);
153 error = pdrv->probe(pdev, id);
156 spin_lock(&pci_lock);
157 list_del(&pdev->links);
158 spin_unlock(&pci_lock);
159 put_device(&pdev->dev);
162 linux_clear_current(td);
167 linux_pci_detach(device_t dev)
169 struct pci_dev *pdev;
170 struct task_struct t;
174 linux_set_current(td, &t);
175 pdev = device_get_softc(dev);
177 pdev->pdrv->remove(pdev);
179 spin_lock(&pci_lock);
180 list_del(&pdev->links);
181 spin_unlock(&pci_lock);
182 put_device(&pdev->dev);
183 linux_clear_current(td);
189 linux_pci_suspend(device_t dev)
191 struct pm_message pm = { };
192 struct pci_dev *pdev;
193 struct task_struct t;
198 linux_set_current(td, &t);
199 pdev = device_get_softc(dev);
200 if (pdev->pdrv->suspend != NULL)
201 err = -pdev->pdrv->suspend(pdev, pm);
204 linux_clear_current(td);
209 linux_pci_resume(device_t dev)
211 struct pci_dev *pdev;
212 struct task_struct t;
217 linux_set_current(td, &t);
218 pdev = device_get_softc(dev);
219 if (pdev->pdrv->resume != NULL)
220 err = -pdev->pdrv->resume(pdev);
223 linux_clear_current(td);
228 linux_pci_shutdown(device_t dev)
230 struct pci_dev *pdev;
231 struct task_struct t;
235 linux_set_current(td, &t);
236 pdev = device_get_softc(dev);
237 if (pdev->pdrv->shutdown != NULL) {
239 pdev->pdrv->shutdown(pdev);
242 linux_clear_current(td);
247 pci_register_driver(struct pci_driver *pdrv)
252 bus = devclass_find("pci");
254 spin_lock(&pci_lock);
255 list_add(&pdrv->links, &pci_drivers);
256 spin_unlock(&pci_lock);
257 pdrv->driver.name = pdrv->name;
258 pdrv->driver.methods = pci_methods;
259 pdrv->driver.size = sizeof(struct pci_dev);
262 error = devclass_add_driver(bus, &pdrv->driver, BUS_PASS_DEFAULT,
270 pci_unregister_driver(struct pci_driver *pdrv)
274 bus = devclass_find("pci");
276 list_del(&pdrv->links);
279 devclass_delete_driver(bus, &pdrv->driver);