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 #include <machine/pmap.h> 48 49 #include <linux/kobject.h> 50 #include <linux/device.h> 51 #include <linux/slab.h> 52 #include <linux/module.h> 53 #include <linux/cdev.h> 54 #include <linux/file.h> 55 #include <linux/sysfs.h> 56 #include <linux/mm.h> 57 #include <linux/io.h> 58 #include <linux/vmalloc.h> 59 #include <linux/pci.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 65 static device_method_t pci_methods[] = { 66 DEVMETHOD(device_probe, linux_pci_probe), 67 DEVMETHOD(device_attach, linux_pci_attach), 68 DEVMETHOD(device_detach, linux_pci_detach), 69 DEVMETHOD_END 70 }; 71 72 static struct pci_driver * 73 linux_pci_find(device_t dev, const struct pci_device_id **idp) 74 { 75 const struct pci_device_id *id; 76 struct pci_driver *pdrv; 77 uint16_t vendor; 78 uint16_t device; 79 80 vendor = pci_get_vendor(dev); 81 device = pci_get_device(dev); 82 83 spin_lock(&pci_lock); 84 list_for_each_entry(pdrv, &pci_drivers, links) { 85 for (id = pdrv->id_table; id->vendor != 0; id++) { 86 if (vendor == id->vendor && device == id->device) { 87 *idp = id; 88 spin_unlock(&pci_lock); 89 return (pdrv); 90 } 91 } 92 } 93 spin_unlock(&pci_lock); 94 return (NULL); 95 } 96 97 static int 98 linux_pci_probe(device_t dev) 99 { 100 const struct pci_device_id *id; 101 struct pci_driver *pdrv; 102 103 if ((pdrv = linux_pci_find(dev, &id)) == NULL) 104 return (ENXIO); 105 if (device_get_driver(dev) != &pdrv->driver) 106 return (ENXIO); 107 device_set_desc(dev, pdrv->name); 108 return (0); 109 } 110 111 static int 112 linux_pci_attach(device_t dev) 113 { 114 struct resource_list_entry *rle; 115 struct pci_dev *pdev; 116 struct pci_driver *pdrv; 117 const struct pci_device_id *id; 118 int error; 119 120 pdrv = linux_pci_find(dev, &id); 121 pdev = device_get_softc(dev); 122 pdev->dev.parent = &linux_rootdev; 123 pdev->dev.bsddev = dev; 124 INIT_LIST_HEAD(&pdev->dev.irqents); 125 pdev->device = id->device; 126 pdev->vendor = id->vendor; 127 pdev->dev.dma_mask = &pdev->dma_mask; 128 pdev->pdrv = pdrv; 129 kobject_init(&pdev->dev.kobj, &dev_ktype); 130 kobject_set_name(&pdev->dev.kobj, device_get_nameunit(dev)); 131 kobject_add(&pdev->dev.kobj, &linux_rootdev.kobj, 132 kobject_name(&pdev->dev.kobj)); 133 rle = _pci_get_rle(pdev, SYS_RES_IRQ, 0); 134 if (rle) 135 pdev->dev.irq = rle->start; 136 else 137 pdev->dev.irq = 0; 138 pdev->irq = pdev->dev.irq; 139 mtx_unlock(&Giant); 140 spin_lock(&pci_lock); 141 list_add(&pdev->links, &pci_devices); 142 spin_unlock(&pci_lock); 143 error = pdrv->probe(pdev, id); 144 mtx_lock(&Giant); 145 if (error) { 146 spin_lock(&pci_lock); 147 list_del(&pdev->links); 148 spin_unlock(&pci_lock); 149 put_device(&pdev->dev); 150 return (-error); 151 } 152 return (0); 153 } 154 155 static int 156 linux_pci_detach(device_t dev) 157 { 158 struct pci_dev *pdev; 159 160 pdev = device_get_softc(dev); 161 mtx_unlock(&Giant); 162 pdev->pdrv->remove(pdev); 163 mtx_lock(&Giant); 164 spin_lock(&pci_lock); 165 list_del(&pdev->links); 166 spin_unlock(&pci_lock); 167 put_device(&pdev->dev); 168 169 return (0); 170 } 171 172 int 173 pci_register_driver(struct pci_driver *pdrv) 174 { 175 devclass_t bus; 176 int error = 0; 177 178 bus = devclass_find("pci"); 179 180 spin_lock(&pci_lock); 181 list_add(&pdrv->links, &pci_drivers); 182 spin_unlock(&pci_lock); 183 pdrv->driver.name = pdrv->name; 184 pdrv->driver.methods = pci_methods; 185 pdrv->driver.size = sizeof(struct pci_dev); 186 mtx_lock(&Giant); 187 if (bus != NULL) { 188 error = devclass_add_driver(bus, &pdrv->driver, BUS_PASS_DEFAULT, 189 &pdrv->bsdclass); 190 } 191 mtx_unlock(&Giant); 192 return (-error); 193 } 194 195 void 196 pci_unregister_driver(struct pci_driver *pdrv) 197 { 198 devclass_t bus; 199 200 bus = devclass_find("pci"); 201 202 list_del(&pdrv->links); 203 mtx_lock(&Giant); 204 if (bus != NULL) 205 devclass_delete_driver(bus, &pdrv->driver); 206 mtx_unlock(&Giant); 207 } 208 209