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 30 #include <linux/device.h> 31 #include <linux/interrupt.h> 32 #include <linux/pci.h> 33 34 #include <sys/param.h> 35 #include <sys/bus.h> 36 #include <sys/rman.h> 37 #include <sys/interrupt.h> 38 39 struct irq_ent { 40 struct list_head links; 41 struct device *dev; 42 struct resource *res; 43 void *arg; 44 irqreturn_t (*handler)(int, void *); 45 irqreturn_t (*thread_handler)(int, void *); 46 void *tag; 47 unsigned int irq; 48 }; 49 50 static inline int 51 lkpi_irq_rid(struct device *dev, unsigned int irq) 52 { 53 /* check for MSI- or MSIX- interrupt */ 54 if (irq >= dev->irq_start && irq < dev->irq_end) 55 return (irq - dev->irq_start + 1); 56 else 57 return (0); 58 } 59 60 static inline struct irq_ent * 61 lkpi_irq_ent(struct device *dev, unsigned int irq) 62 { 63 struct irq_ent *irqe; 64 65 list_for_each_entry(irqe, &dev->irqents, links) 66 if (irqe->irq == irq) 67 return (irqe); 68 69 return (NULL); 70 } 71 72 static void 73 lkpi_irq_handler(void *ent) 74 { 75 struct irq_ent *irqe; 76 77 if (linux_set_current_flags(curthread, M_NOWAIT)) 78 return; 79 80 irqe = ent; 81 if (irqe->handler(irqe->irq, irqe->arg) == IRQ_WAKE_THREAD && 82 irqe->thread_handler != NULL) { 83 THREAD_SLEEPING_OK(); 84 irqe->thread_handler(irqe->irq, irqe->arg); 85 THREAD_NO_SLEEPING(); 86 } 87 } 88 89 static inline void 90 lkpi_irq_release(struct device *dev, struct irq_ent *irqe) 91 { 92 if (irqe->tag != NULL) 93 bus_teardown_intr(dev->bsddev, irqe->res, irqe->tag); 94 if (irqe->res != NULL) 95 bus_release_resource(dev->bsddev, SYS_RES_IRQ, 96 rman_get_rid(irqe->res), irqe->res); 97 list_del(&irqe->links); 98 } 99 100 static void 101 lkpi_devm_irq_release(struct device *dev, void *p) 102 { 103 struct irq_ent *irqe; 104 105 if (dev == NULL || p == NULL) 106 return; 107 108 irqe = p; 109 lkpi_irq_release(dev, irqe); 110 } 111 112 int 113 lkpi_request_irq(struct device *xdev, unsigned int irq, 114 irq_handler_t handler, irq_handler_t thread_handler, 115 unsigned long flags, const char *name, void *arg) 116 { 117 struct resource *res; 118 struct irq_ent *irqe; 119 struct device *dev; 120 int error; 121 int rid; 122 123 dev = linux_pci_find_irq_dev(irq); 124 if (dev == NULL) 125 return -ENXIO; 126 if (xdev != NULL && xdev != dev) 127 return -ENXIO; 128 rid = lkpi_irq_rid(dev, irq); 129 res = bus_alloc_resource_any(dev->bsddev, SYS_RES_IRQ, &rid, 130 flags | RF_ACTIVE); 131 if (res == NULL) 132 return (-ENXIO); 133 if (xdev != NULL) 134 irqe = lkpi_devres_alloc(lkpi_devm_irq_release, sizeof(*irqe), 135 GFP_KERNEL | __GFP_ZERO); 136 else 137 irqe = kzalloc(sizeof(*irqe), GFP_KERNEL); 138 irqe->dev = dev; 139 irqe->res = res; 140 irqe->arg = arg; 141 irqe->handler = handler; 142 irqe->thread_handler = thread_handler; 143 irqe->irq = irq; 144 145 error = bus_setup_intr(dev->bsddev, res, INTR_TYPE_NET | INTR_MPSAFE, 146 NULL, lkpi_irq_handler, irqe, &irqe->tag); 147 if (error) 148 goto errout; 149 list_add(&irqe->links, &dev->irqents); 150 if (xdev != NULL) 151 devres_add(xdev, irqe); 152 153 return 0; 154 155 errout: 156 bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res); 157 if (xdev != NULL) 158 devres_free(irqe); 159 else 160 kfree(irqe); 161 return (-error); 162 } 163 164 int 165 lkpi_enable_irq(unsigned int irq) 166 { 167 struct irq_ent *irqe; 168 struct device *dev; 169 170 dev = linux_pci_find_irq_dev(irq); 171 if (dev == NULL) 172 return -EINVAL; 173 irqe = lkpi_irq_ent(dev, irq); 174 if (irqe == NULL || irqe->tag != NULL) 175 return -EINVAL; 176 return -bus_setup_intr(dev->bsddev, irqe->res, INTR_TYPE_NET | INTR_MPSAFE, 177 NULL, lkpi_irq_handler, irqe, &irqe->tag); 178 } 179 180 void 181 lkpi_disable_irq(unsigned int irq) 182 { 183 struct irq_ent *irqe; 184 struct device *dev; 185 186 dev = linux_pci_find_irq_dev(irq); 187 if (dev == NULL) 188 return; 189 irqe = lkpi_irq_ent(dev, irq); 190 if (irqe == NULL) 191 return; 192 if (irqe->tag != NULL) 193 bus_teardown_intr(dev->bsddev, irqe->res, irqe->tag); 194 irqe->tag = NULL; 195 } 196 197 int 198 lkpi_bind_irq_to_cpu(unsigned int irq, int cpu_id) 199 { 200 struct irq_ent *irqe; 201 struct device *dev; 202 203 dev = linux_pci_find_irq_dev(irq); 204 if (dev == NULL) 205 return (-ENOENT); 206 207 irqe = lkpi_irq_ent(dev, irq); 208 if (irqe == NULL) 209 return (-ENOENT); 210 211 return (-bus_bind_intr(dev->bsddev, irqe->res, cpu_id)); 212 } 213 214 void 215 lkpi_free_irq(unsigned int irq, void *device __unused) 216 { 217 struct irq_ent *irqe; 218 struct device *dev; 219 220 dev = linux_pci_find_irq_dev(irq); 221 if (dev == NULL) 222 return; 223 irqe = lkpi_irq_ent(dev, irq); 224 if (irqe == NULL) 225 return; 226 lkpi_irq_release(dev, irqe); 227 kfree(irqe); 228 } 229 230 void 231 lkpi_devm_free_irq(struct device *xdev, unsigned int irq, void *p __unused) 232 { 233 struct device *dev; 234 struct irq_ent *irqe; 235 236 dev = linux_pci_find_irq_dev(irq); 237 if (dev == NULL) 238 return; 239 if (xdev != dev) 240 return; 241 irqe = lkpi_irq_ent(dev, irq); 242 if (irqe == NULL) 243 return; 244 lkpi_irq_release(dev, irqe); 245 lkpi_devres_unlink(dev, irqe); 246 lkpi_devres_free(irqe); 247 return; 248 } 249