1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2013-2015 Mellanox Technologies, Ltd. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #include <linux/device.h> 33 #include <linux/interrupt.h> 34 #include <linux/pci.h> 35 36 #include <sys/param.h> 37 #include <sys/bus.h> 38 #include <sys/rman.h> 39 #include <sys/interrupt.h> 40 41 struct irq_ent { 42 struct list_head links; 43 struct device *dev; 44 struct resource *res; 45 void *arg; 46 irqreturn_t (*handler)(int, void *); 47 void *tag; 48 unsigned int irq; 49 50 /* XXX All new entries must be after this in stable/13 */ 51 irqreturn_t (*thread_handler)(int, void *); 52 }; 53 54 static inline int 55 lkpi_irq_rid(struct device *dev, unsigned int irq) 56 { 57 /* check for MSI- or MSIX- interrupt */ 58 if (irq >= dev->irq_start && irq < dev->irq_end) 59 return (irq - dev->irq_start + 1); 60 else 61 return (0); 62 } 63 64 static inline struct irq_ent * 65 lkpi_irq_ent(struct device *dev, unsigned int irq) 66 { 67 struct irq_ent *irqe; 68 69 list_for_each_entry(irqe, &dev->irqents, links) 70 if (irqe->irq == irq) 71 return (irqe); 72 73 return (NULL); 74 } 75 76 void 77 linux_irq_handler(void *ent) 78 { 79 struct irq_ent *irqe; 80 81 if (linux_set_current_flags(curthread, M_NOWAIT)) 82 return; 83 84 irqe = ent; 85 if (irqe->handler(irqe->irq, irqe->arg) == IRQ_WAKE_THREAD && 86 irqe->thread_handler != NULL) { 87 THREAD_SLEEPING_OK(); 88 irqe->thread_handler(irqe->irq, irqe->arg); 89 THREAD_NO_SLEEPING(); 90 } 91 } 92 93 void 94 lkpi_irq_release(struct device *dev, struct irq_ent *irqe) 95 { 96 if (irqe->tag != NULL) 97 bus_teardown_intr(dev->bsddev, irqe->res, irqe->tag); 98 if (irqe->res != NULL) 99 bus_release_resource(dev->bsddev, SYS_RES_IRQ, 100 rman_get_rid(irqe->res), irqe->res); 101 list_del(&irqe->links); 102 } 103 104 void 105 lkpi_devm_irq_release(struct device *dev, void *p) 106 { 107 struct irq_ent *irqe; 108 109 if (dev == NULL || p == NULL) 110 return; 111 112 irqe = p; 113 lkpi_irq_release(dev, irqe); 114 } 115 116 int 117 lkpi_request_irq(struct device *xdev, unsigned int irq, 118 irq_handler_t handler, irq_handler_t thread_handler, 119 unsigned long flags, const char *name, void *arg) 120 { 121 struct resource *res; 122 struct irq_ent *irqe; 123 struct device *dev; 124 int error; 125 int rid; 126 127 dev = linux_pci_find_irq_dev(irq); 128 if (dev == NULL) 129 return -ENXIO; 130 if (xdev != NULL && xdev != dev) 131 return -ENXIO; 132 rid = lkpi_irq_rid(dev, irq); 133 res = bus_alloc_resource_any(dev->bsddev, SYS_RES_IRQ, &rid, 134 flags | RF_ACTIVE); 135 if (res == NULL) 136 return (-ENXIO); 137 if (xdev != NULL) 138 irqe = lkpi_devres_alloc(lkpi_devm_irq_release, sizeof(*irqe), 139 GFP_KERNEL | __GFP_ZERO); 140 else 141 irqe = kzalloc(sizeof(*irqe), GFP_KERNEL); 142 irqe->dev = dev; 143 irqe->res = res; 144 irqe->arg = arg; 145 irqe->handler = handler; 146 irqe->thread_handler = thread_handler; 147 irqe->irq = irq; 148 149 error = bus_setup_intr(dev->bsddev, res, INTR_TYPE_NET | INTR_MPSAFE, 150 NULL, linux_irq_handler, irqe, &irqe->tag); 151 if (error) 152 goto errout; 153 list_add(&irqe->links, &dev->irqents); 154 if (xdev != NULL) 155 devres_add(xdev, irqe); 156 157 return 0; 158 159 errout: 160 bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res); 161 if (xdev != NULL) 162 devres_free(irqe); 163 else 164 kfree(irqe); 165 return (-error); 166 } 167 168 int 169 lkpi_enable_irq(unsigned int irq) 170 { 171 struct irq_ent *irqe; 172 struct device *dev; 173 174 dev = linux_pci_find_irq_dev(irq); 175 if (dev == NULL) 176 return -EINVAL; 177 irqe = lkpi_irq_ent(dev, irq); 178 if (irqe == NULL || irqe->tag != NULL) 179 return -EINVAL; 180 return -bus_setup_intr(dev->bsddev, irqe->res, INTR_TYPE_NET | INTR_MPSAFE, 181 NULL, linux_irq_handler, irqe, &irqe->tag); 182 } 183 184 void 185 lkpi_disable_irq(unsigned int irq) 186 { 187 struct irq_ent *irqe; 188 struct device *dev; 189 190 dev = linux_pci_find_irq_dev(irq); 191 if (dev == NULL) 192 return; 193 irqe = lkpi_irq_ent(dev, irq); 194 if (irqe == NULL) 195 return; 196 if (irqe->tag != NULL) 197 bus_teardown_intr(dev->bsddev, irqe->res, irqe->tag); 198 irqe->tag = NULL; 199 } 200 201 int 202 lkpi_bind_irq_to_cpu(unsigned int irq, int cpu_id) 203 { 204 struct irq_ent *irqe; 205 struct device *dev; 206 207 dev = linux_pci_find_irq_dev(irq); 208 if (dev == NULL) 209 return (-ENOENT); 210 211 irqe = lkpi_irq_ent(dev, irq); 212 if (irqe == NULL) 213 return (-ENOENT); 214 215 return (-bus_bind_intr(dev->bsddev, irqe->res, cpu_id)); 216 } 217 218 void 219 lkpi_free_irq(unsigned int irq, void *device __unused) 220 { 221 struct irq_ent *irqe; 222 struct device *dev; 223 224 dev = linux_pci_find_irq_dev(irq); 225 if (dev == NULL) 226 return; 227 irqe = lkpi_irq_ent(dev, irq); 228 if (irqe == NULL) 229 return; 230 lkpi_irq_release(dev, irqe); 231 kfree(irqe); 232 } 233 234 void 235 lkpi_devm_free_irq(struct device *xdev, unsigned int irq, void *p __unused) 236 { 237 struct device *dev; 238 struct irq_ent *irqe; 239 240 dev = linux_pci_find_irq_dev(irq); 241 if (dev == NULL) 242 return; 243 if (xdev != dev) 244 return; 245 irqe = lkpi_irq_ent(dev, irq); 246 if (irqe == NULL) 247 return; 248 lkpi_irq_release(dev, irqe); 249 lkpi_devres_unlink(dev, irqe); 250 lkpi_devres_free(irqe); 251 return; 252 } 253