1 /*- 2 * Copyright (c) 2015-2016 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 #include <linux/compat.h> 60 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; 67 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), 75 DEVMETHOD_END 76 }; 77 78 static struct pci_driver * 79 linux_pci_find(device_t dev, const struct pci_device_id **idp) 80 { 81 const struct pci_device_id *id; 82 struct pci_driver *pdrv; 83 uint16_t vendor; 84 uint16_t device; 85 86 vendor = pci_get_vendor(dev); 87 device = pci_get_device(dev); 88 89 spin_lock(&pci_lock); 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) { 93 *idp = id; 94 spin_unlock(&pci_lock); 95 return (pdrv); 96 } 97 } 98 } 99 spin_unlock(&pci_lock); 100 return (NULL); 101 } 102 103 static int 104 linux_pci_probe(device_t dev) 105 { 106 const struct pci_device_id *id; 107 struct pci_driver *pdrv; 108 109 if ((pdrv = linux_pci_find(dev, &id)) == NULL) 110 return (ENXIO); 111 if (device_get_driver(dev) != &pdrv->bsddriver) 112 return (ENXIO); 113 device_set_desc(dev, pdrv->name); 114 return (0); 115 } 116 117 static int 118 linux_pci_attach(device_t dev) 119 { 120 struct resource_list_entry *rle; 121 struct pci_dev *pdev; 122 struct pci_devinfo *dinfo; 123 struct pci_driver *pdrv; 124 const struct pci_device_id *id; 125 int error; 126 127 dinfo = device_get_ivars(dev); 128 129 linux_set_current(curthread); 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->devfn = PCI_DEVFN(pci_get_slot(dev), pci_get_function(dev)); 136 pdev->device = id->device; 137 pdev->vendor = id->vendor; 138 pdev->subsystem_vendor = dinfo->cfg.subvendor; 139 pdev->subsystem_device = dinfo->cfg.subdevice; 140 pdev->class = pci_get_class(dev); 141 pdev->revision = pci_get_revid(dev); 142 pdev->dev.dma_mask = &pdev->dma_mask; 143 pdev->pdrv = pdrv; 144 kobject_init(&pdev->dev.kobj, &linux_dev_ktype); 145 kobject_set_name(&pdev->dev.kobj, device_get_nameunit(dev)); 146 kobject_add(&pdev->dev.kobj, &linux_root_device.kobj, 147 kobject_name(&pdev->dev.kobj)); 148 rle = linux_pci_get_rle(pdev, SYS_RES_IRQ, 0); 149 if (rle != NULL) 150 pdev->dev.irq = rle->start; 151 else 152 pdev->dev.irq = LINUX_IRQ_INVALID; 153 pdev->irq = pdev->dev.irq; 154 DROP_GIANT(); 155 spin_lock(&pci_lock); 156 list_add(&pdev->links, &pci_devices); 157 spin_unlock(&pci_lock); 158 error = pdrv->probe(pdev, id); 159 PICKUP_GIANT(); 160 if (error) { 161 spin_lock(&pci_lock); 162 list_del(&pdev->links); 163 spin_unlock(&pci_lock); 164 put_device(&pdev->dev); 165 error = -error; 166 } 167 return (error); 168 } 169 170 static int 171 linux_pci_detach(device_t dev) 172 { 173 struct pci_dev *pdev; 174 175 linux_set_current(curthread); 176 pdev = device_get_softc(dev); 177 DROP_GIANT(); 178 pdev->pdrv->remove(pdev); 179 PICKUP_GIANT(); 180 spin_lock(&pci_lock); 181 list_del(&pdev->links); 182 spin_unlock(&pci_lock); 183 put_device(&pdev->dev); 184 185 return (0); 186 } 187 188 static int 189 linux_pci_suspend(device_t dev) 190 { 191 const struct dev_pm_ops *pmops; 192 struct pm_message pm = { }; 193 struct pci_dev *pdev; 194 int error; 195 196 error = 0; 197 linux_set_current(curthread); 198 pdev = device_get_softc(dev); 199 pmops = pdev->pdrv->driver.pm; 200 201 if (pdev->pdrv->suspend != NULL) 202 error = -pdev->pdrv->suspend(pdev, pm); 203 else if (pmops != NULL && pmops->suspend != NULL) { 204 error = -pmops->suspend(&pdev->dev); 205 if (error == 0 && pmops->suspend_late != NULL) 206 error = -pmops->suspend_late(&pdev->dev); 207 } 208 return (error); 209 } 210 211 static int 212 linux_pci_resume(device_t dev) 213 { 214 const struct dev_pm_ops *pmops; 215 struct pci_dev *pdev; 216 int error; 217 218 error = 0; 219 linux_set_current(curthread); 220 pdev = device_get_softc(dev); 221 pmops = pdev->pdrv->driver.pm; 222 223 if (pdev->pdrv->resume != NULL) 224 error = -pdev->pdrv->resume(pdev); 225 else if (pmops != NULL && pmops->resume != NULL) { 226 if (pmops->resume_early != NULL) 227 error = -pmops->resume_early(&pdev->dev); 228 if (error == 0 && pmops->resume != NULL) 229 error = -pmops->resume(&pdev->dev); 230 } 231 return (error); 232 } 233 234 static int 235 linux_pci_shutdown(device_t dev) 236 { 237 struct pci_dev *pdev; 238 239 linux_set_current(curthread); 240 pdev = device_get_softc(dev); 241 if (pdev->pdrv->shutdown != NULL) { 242 DROP_GIANT(); 243 pdev->pdrv->shutdown(pdev); 244 PICKUP_GIANT(); 245 } 246 return (0); 247 } 248 249 int 250 pci_register_driver(struct pci_driver *pdrv) 251 { 252 devclass_t bus; 253 int error = 0; 254 255 bus = devclass_find("pci"); 256 257 linux_set_current(curthread); 258 spin_lock(&pci_lock); 259 list_add(&pdrv->links, &pci_drivers); 260 spin_unlock(&pci_lock); 261 pdrv->bsddriver.name = pdrv->name; 262 pdrv->bsddriver.methods = pci_methods; 263 pdrv->bsddriver.size = sizeof(struct pci_dev); 264 265 mtx_lock(&Giant); 266 if (bus != NULL) { 267 error = devclass_add_driver(bus, &pdrv->bsddriver, 268 BUS_PASS_DEFAULT, &pdrv->bsdclass); 269 } 270 mtx_unlock(&Giant); 271 return (-error); 272 } 273 274 void 275 pci_unregister_driver(struct pci_driver *pdrv) 276 { 277 devclass_t bus; 278 279 bus = devclass_find("pci"); 280 281 spin_lock(&pci_lock); 282 list_del(&pdrv->links); 283 spin_unlock(&pci_lock); 284 mtx_lock(&Giant); 285 if (bus != NULL) 286 devclass_delete_driver(bus, &pdrv->bsddriver); 287 mtx_unlock(&Giant); 288 } 289