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_driver *pdrv; 123 const struct pci_device_id *id; 124 int error; 125 126 linux_set_current(curthread); 127 pdrv = linux_pci_find(dev, &id); 128 pdev = device_get_softc(dev); 129 pdev->dev.parent = &linux_root_device; 130 pdev->dev.bsddev = dev; 131 INIT_LIST_HEAD(&pdev->dev.irqents); 132 pdev->devfn = PCI_DEVFN(pci_get_slot(dev), pci_get_function(dev)); 133 pdev->device = id->device; 134 pdev->vendor = id->vendor; 135 pdev->class = pci_get_class(dev); 136 pdev->revision = pci_get_revid(dev); 137 pdev->dev.dma_mask = &pdev->dma_mask; 138 pdev->pdrv = pdrv; 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 = linux_pci_get_rle(pdev, SYS_RES_IRQ, 0); 144 if (rle != NULL) 145 pdev->dev.irq = rle->start; 146 else 147 pdev->dev.irq = LINUX_IRQ_INVALID; 148 pdev->irq = pdev->dev.irq; 149 DROP_GIANT(); 150 spin_lock(&pci_lock); 151 list_add(&pdev->links, &pci_devices); 152 spin_unlock(&pci_lock); 153 error = pdrv->probe(pdev, id); 154 PICKUP_GIANT(); 155 if (error) { 156 spin_lock(&pci_lock); 157 list_del(&pdev->links); 158 spin_unlock(&pci_lock); 159 put_device(&pdev->dev); 160 error = -error; 161 } 162 return (error); 163 } 164 165 static int 166 linux_pci_detach(device_t dev) 167 { 168 struct pci_dev *pdev; 169 170 linux_set_current(curthread); 171 pdev = device_get_softc(dev); 172 DROP_GIANT(); 173 pdev->pdrv->remove(pdev); 174 PICKUP_GIANT(); 175 spin_lock(&pci_lock); 176 list_del(&pdev->links); 177 spin_unlock(&pci_lock); 178 put_device(&pdev->dev); 179 180 return (0); 181 } 182 183 static int 184 linux_pci_suspend(device_t dev) 185 { 186 const struct dev_pm_ops *pmops; 187 struct pm_message pm = { }; 188 struct pci_dev *pdev; 189 int error; 190 191 error = 0; 192 linux_set_current(curthread); 193 pdev = device_get_softc(dev); 194 pmops = pdev->pdrv->driver.pm; 195 196 if (pdev->pdrv->suspend != NULL) 197 error = -pdev->pdrv->suspend(pdev, pm); 198 else if (pmops != NULL && pmops->suspend != NULL) { 199 error = -pmops->suspend(&pdev->dev); 200 if (error == 0 && pmops->suspend_late != NULL) 201 error = -pmops->suspend_late(&pdev->dev); 202 } 203 return (error); 204 } 205 206 static int 207 linux_pci_resume(device_t dev) 208 { 209 const struct dev_pm_ops *pmops; 210 struct pci_dev *pdev; 211 int error; 212 213 error = 0; 214 linux_set_current(curthread); 215 pdev = device_get_softc(dev); 216 pmops = pdev->pdrv->driver.pm; 217 218 if (pdev->pdrv->resume != NULL) 219 error = -pdev->pdrv->resume(pdev); 220 else if (pmops != NULL && pmops->resume != NULL) { 221 if (pmops->resume_early != NULL) 222 error = -pmops->resume_early(&pdev->dev); 223 if (error == 0 && pmops->resume != NULL) 224 error = -pmops->resume(&pdev->dev); 225 } 226 return (error); 227 } 228 229 static int 230 linux_pci_shutdown(device_t dev) 231 { 232 struct pci_dev *pdev; 233 234 linux_set_current(curthread); 235 pdev = device_get_softc(dev); 236 if (pdev->pdrv->shutdown != NULL) { 237 DROP_GIANT(); 238 pdev->pdrv->shutdown(pdev); 239 PICKUP_GIANT(); 240 } 241 return (0); 242 } 243 244 int 245 pci_register_driver(struct pci_driver *pdrv) 246 { 247 devclass_t bus; 248 int error = 0; 249 250 bus = devclass_find("pci"); 251 252 linux_set_current(curthread); 253 spin_lock(&pci_lock); 254 list_add(&pdrv->links, &pci_drivers); 255 spin_unlock(&pci_lock); 256 pdrv->bsddriver.name = pdrv->name; 257 pdrv->bsddriver.methods = pci_methods; 258 pdrv->bsddriver.size = sizeof(struct pci_dev); 259 260 mtx_lock(&Giant); 261 if (bus != NULL) { 262 error = devclass_add_driver(bus, &pdrv->bsddriver, 263 BUS_PASS_DEFAULT, &pdrv->bsdclass); 264 } 265 mtx_unlock(&Giant); 266 return (-error); 267 } 268 269 void 270 pci_unregister_driver(struct pci_driver *pdrv) 271 { 272 devclass_t bus; 273 274 bus = devclass_find("pci"); 275 276 spin_lock(&pci_lock); 277 list_del(&pdrv->links); 278 spin_unlock(&pci_lock); 279 mtx_lock(&Giant); 280 if (bus != NULL) 281 devclass_delete_driver(bus, &pdrv->bsddriver); 282 mtx_unlock(&Giant); 283 } 284