xref: /freebsd/sys/compat/linuxkpi/common/src/linux_pci.c (revision f5e9c916afed4a948fe5c03bfaee038d165e12ab)
1 /*-
2  * Copyright (c) 2015 Mellanox Technologies, Ltd.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
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.
14  *
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.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
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>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/bus.h>
38 #include <sys/fcntl.h>
39 #include <sys/file.h>
40 #include <sys/filio.h>
41 #include <sys/rwlock.h>
42 
43 #include <vm/vm.h>
44 #include <vm/pmap.h>
45 
46 #include <machine/stdarg.h>
47 
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>
55 #include <linux/mm.h>
56 #include <linux/io.h>
57 #include <linux/vmalloc.h>
58 #include <linux/pci.h>
59 
60 static device_probe_t linux_pci_probe;
61 static device_attach_t linux_pci_attach;
62 static device_detach_t linux_pci_detach;
63 static device_suspend_t linux_pci_suspend;
64 static device_resume_t linux_pci_resume;
65 static device_shutdown_t linux_pci_shutdown;
66 
67 static device_method_t pci_methods[] = {
68 	DEVMETHOD(device_probe, linux_pci_probe),
69 	DEVMETHOD(device_attach, linux_pci_attach),
70 	DEVMETHOD(device_detach, linux_pci_detach),
71 	DEVMETHOD(device_suspend, linux_pci_suspend),
72 	DEVMETHOD(device_resume, linux_pci_resume),
73 	DEVMETHOD(device_shutdown, linux_pci_shutdown),
74 	DEVMETHOD_END
75 };
76 
77 static struct pci_driver *
78 linux_pci_find(device_t dev, const struct pci_device_id **idp)
79 {
80 	const struct pci_device_id *id;
81 	struct pci_driver *pdrv;
82 	uint16_t vendor;
83 	uint16_t device;
84 
85 	vendor = pci_get_vendor(dev);
86 	device = pci_get_device(dev);
87 
88 	spin_lock(&pci_lock);
89 	list_for_each_entry(pdrv, &pci_drivers, links) {
90 		for (id = pdrv->id_table; id->vendor != 0; id++) {
91 			if (vendor == id->vendor && device == id->device) {
92 				*idp = id;
93 				spin_unlock(&pci_lock);
94 				return (pdrv);
95 			}
96 		}
97 	}
98 	spin_unlock(&pci_lock);
99 	return (NULL);
100 }
101 
102 static int
103 linux_pci_probe(device_t dev)
104 {
105 	const struct pci_device_id *id;
106 	struct pci_driver *pdrv;
107 
108 	if ((pdrv = linux_pci_find(dev, &id)) == NULL)
109 		return (ENXIO);
110 	if (device_get_driver(dev) != &pdrv->driver)
111 		return (ENXIO);
112 	device_set_desc(dev, pdrv->name);
113 	return (0);
114 }
115 
116 static int
117 linux_pci_attach(device_t dev)
118 {
119 	struct resource_list_entry *rle;
120 	struct pci_dev *pdev;
121 	struct pci_driver *pdrv;
122 	const struct pci_device_id *id;
123 	int error;
124 
125 	pdrv = linux_pci_find(dev, &id);
126 	pdev = device_get_softc(dev);
127 	pdev->dev.parent = &linux_root_device;
128 	pdev->dev.bsddev = dev;
129 	INIT_LIST_HEAD(&pdev->dev.irqents);
130 	pdev->device = id->device;
131 	pdev->vendor = id->vendor;
132 	pdev->dev.dma_mask = &pdev->dma_mask;
133 	pdev->pdrv = pdrv;
134 	kobject_init(&pdev->dev.kobj, &linux_dev_ktype);
135 	kobject_set_name(&pdev->dev.kobj, device_get_nameunit(dev));
136 	kobject_add(&pdev->dev.kobj, &linux_root_device.kobj,
137 	    kobject_name(&pdev->dev.kobj));
138 	rle = _pci_get_rle(pdev, SYS_RES_IRQ, 0);
139 	if (rle)
140 		pdev->dev.irq = rle->start;
141 	else
142 		pdev->dev.irq = 0;
143 	pdev->irq = pdev->dev.irq;
144 	mtx_unlock(&Giant);
145 	spin_lock(&pci_lock);
146 	list_add(&pdev->links, &pci_devices);
147 	spin_unlock(&pci_lock);
148 	error = pdrv->probe(pdev, id);
149 	mtx_lock(&Giant);
150 	if (error) {
151 		spin_lock(&pci_lock);
152 		list_del(&pdev->links);
153 		spin_unlock(&pci_lock);
154 		put_device(&pdev->dev);
155 		return (-error);
156 	}
157 	return (0);
158 }
159 
160 static int
161 linux_pci_detach(device_t dev)
162 {
163 	struct pci_dev *pdev;
164 
165 	pdev = device_get_softc(dev);
166 	mtx_unlock(&Giant);
167 	pdev->pdrv->remove(pdev);
168 	mtx_lock(&Giant);
169 	spin_lock(&pci_lock);
170 	list_del(&pdev->links);
171 	spin_unlock(&pci_lock);
172 	put_device(&pdev->dev);
173 
174 	return (0);
175 }
176 
177 static int
178 linux_pci_suspend(device_t dev)
179 {
180 	struct pm_message pm = { };
181 	struct pci_dev *pdev;
182 	int err;
183 
184 	pdev = device_get_softc(dev);
185 	if (pdev->pdrv->suspend != NULL)
186 		err = -pdev->pdrv->suspend(pdev, pm);
187 	else
188 		err = 0;
189 	return (err);
190 }
191 
192 static int
193 linux_pci_resume(device_t dev)
194 {
195 	struct pci_dev *pdev;
196 	int err;
197 
198 	pdev = device_get_softc(dev);
199 	if (pdev->pdrv->resume != NULL)
200 		err = -pdev->pdrv->resume(pdev);
201 	else
202 		err = 0;
203 	return (err);
204 }
205 
206 static int
207 linux_pci_shutdown(device_t dev)
208 {
209 	struct pci_dev *pdev;
210 
211 	pdev = device_get_softc(dev);
212 	if (pdev->pdrv->shutdown != NULL)
213 		pdev->pdrv->shutdown(pdev);
214 	return (0);
215 }
216 
217 int
218 pci_register_driver(struct pci_driver *pdrv)
219 {
220 	devclass_t bus;
221 	int error = 0;
222 
223 	bus = devclass_find("pci");
224 
225 	spin_lock(&pci_lock);
226 	list_add(&pdrv->links, &pci_drivers);
227 	spin_unlock(&pci_lock);
228 	pdrv->driver.name = pdrv->name;
229 	pdrv->driver.methods = pci_methods;
230 	pdrv->driver.size = sizeof(struct pci_dev);
231 	mtx_lock(&Giant);
232 	if (bus != NULL) {
233 		error = devclass_add_driver(bus, &pdrv->driver, BUS_PASS_DEFAULT,
234 		    &pdrv->bsdclass);
235 	}
236 	mtx_unlock(&Giant);
237 	return (-error);
238 }
239 
240 void
241 pci_unregister_driver(struct pci_driver *pdrv)
242 {
243 	devclass_t bus;
244 
245 	bus = devclass_find("pci");
246 
247 	list_del(&pdrv->links);
248 	mtx_lock(&Giant);
249 	if (bus != NULL)
250 		devclass_delete_driver(bus, &pdrv->driver);
251 	mtx_unlock(&Giant);
252 }
253 
254