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 #ifndef _LINUX_INTERRUPT_H_ 32 #define _LINUX_INTERRUPT_H_ 33 34 #include <linux/cpu.h> 35 #include <linux/device.h> 36 #include <linux/pci.h> 37 #include <linux/irqreturn.h> 38 #include <linux/hardirq.h> 39 40 #include <sys/param.h> 41 #include <sys/bus.h> 42 #include <sys/rman.h> 43 #include <sys/interrupt.h> 44 45 typedef irqreturn_t (*irq_handler_t)(int, void *); 46 47 #define IRQF_SHARED RF_SHAREABLE 48 49 struct irq_ent { 50 struct list_head links; 51 struct device *dev; 52 struct resource *res; 53 void *arg; 54 irqreturn_t (*handler)(int, void *); 55 void *tag; 56 unsigned int irq; 57 58 /* XXX All new entries must be after this in stable/13 */ 59 irqreturn_t (*thread_handler)(int, void *); 60 }; 61 62 void linux_irq_handler(void *); 63 void lkpi_devm_irq_release(struct device *, void *); 64 void lkpi_irq_release(struct device *, struct irq_ent *); 65 66 static inline int 67 linux_irq_rid(struct device *dev, unsigned int irq) 68 { 69 /* check for MSI- or MSIX- interrupt */ 70 if (irq >= dev->irq_start && irq < dev->irq_end) 71 return (irq - dev->irq_start + 1); 72 else 73 return (0); 74 } 75 76 static inline struct irq_ent * 77 linux_irq_ent(struct device *dev, unsigned int irq) 78 { 79 struct irq_ent *irqe; 80 81 list_for_each_entry(irqe, &dev->irqents, links) 82 if (irqe->irq == irq) 83 return (irqe); 84 85 return (NULL); 86 } 87 88 static inline int 89 _request_irq(struct device *xdev, unsigned int irq, 90 irq_handler_t handler, irq_handler_t thread_handler, 91 unsigned long flags, const char *name, void *arg) 92 { 93 struct resource *res; 94 struct irq_ent *irqe; 95 struct device *dev; 96 int error; 97 int rid; 98 99 dev = linux_pci_find_irq_dev(irq); 100 if (dev == NULL) 101 return -ENXIO; 102 if (xdev != NULL && xdev != dev) 103 return -ENXIO; 104 rid = linux_irq_rid(dev, irq); 105 res = bus_alloc_resource_any(dev->bsddev, SYS_RES_IRQ, &rid, 106 flags | RF_ACTIVE); 107 if (res == NULL) 108 return (-ENXIO); 109 if (xdev != NULL) 110 irqe = lkpi_devres_alloc(lkpi_devm_irq_release, sizeof(*irqe), 111 GFP_KERNEL | __GFP_ZERO); 112 else 113 irqe = kzalloc(sizeof(*irqe), GFP_KERNEL); 114 irqe->dev = dev; 115 irqe->res = res; 116 irqe->arg = arg; 117 irqe->handler = handler; 118 irqe->thread_handler = thread_handler; 119 irqe->irq = irq; 120 121 error = bus_setup_intr(dev->bsddev, res, INTR_TYPE_NET | INTR_MPSAFE, 122 NULL, linux_irq_handler, irqe, &irqe->tag); 123 if (error) 124 goto errout; 125 list_add(&irqe->links, &dev->irqents); 126 if (xdev != NULL) 127 devres_add(xdev, irqe); 128 129 return 0; 130 131 errout: 132 bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res); 133 if (xdev != NULL) 134 devres_free(irqe); 135 else 136 kfree(irqe); 137 return (-error); 138 } 139 140 static inline int 141 request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags, 142 const char *name, void *arg) 143 { 144 145 return (_request_irq(NULL, irq, handler, NULL, flags, name, arg)); 146 } 147 148 static inline int 149 request_threaded_irq(int irq, irq_handler_t handler, 150 irq_handler_t thread_handler, unsigned long flags, 151 const char *name, void *arg) 152 { 153 154 return (_request_irq(NULL, irq, handler, thread_handler, 155 flags, name, arg)); 156 } 157 158 static inline int 159 devm_request_threaded_irq(struct device *dev, int irq, 160 irq_handler_t handler, irq_handler_t thread_handler, 161 unsigned long flags, const char *name, void *arg) 162 { 163 164 return (_request_irq(dev, irq, handler, thread_handler, 165 flags, name, arg)); 166 } 167 168 static inline int 169 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 = linux_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 static inline void 185 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 = linux_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 static inline int 202 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 = linux_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 static inline void 219 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 = linux_irq_ent(dev, irq); 228 if (irqe == NULL) 229 return; 230 lkpi_irq_release(dev, irqe); 231 kfree(irqe); 232 } 233 234 static inline void 235 devm_free_irq(struct device *xdev, unsigned int irq, void *p) 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 = linux_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 254 static inline int 255 irq_set_affinity_hint(int vector, cpumask_t *mask) 256 { 257 int error; 258 259 if (mask != NULL) 260 error = intr_setaffinity(vector, CPU_WHICH_IRQ, mask); 261 else 262 error = intr_setaffinity(vector, CPU_WHICH_IRQ, cpuset_root); 263 264 return (-error); 265 } 266 267 /* 268 * LinuxKPI tasklet support 269 */ 270 typedef void tasklet_func_t(unsigned long); 271 272 struct tasklet_struct { 273 TAILQ_ENTRY(tasklet_struct) entry; 274 tasklet_func_t *func; 275 /* Our "state" implementation is different. Avoid same name as Linux. */ 276 volatile u_int tasklet_state; 277 atomic_t count; 278 unsigned long data; 279 }; 280 281 #define DECLARE_TASKLET(_name, _func, _data) \ 282 struct tasklet_struct _name = { .func = (_func), .data = (_data) } 283 284 #define tasklet_hi_schedule(t) tasklet_schedule(t) 285 286 extern void tasklet_schedule(struct tasklet_struct *); 287 extern void tasklet_kill(struct tasklet_struct *); 288 extern void tasklet_init(struct tasklet_struct *, tasklet_func_t *, 289 unsigned long data); 290 extern void tasklet_enable(struct tasklet_struct *); 291 extern void tasklet_disable(struct tasklet_struct *); 292 extern void tasklet_disable_nosync(struct tasklet_struct *); 293 extern int tasklet_trylock(struct tasklet_struct *); 294 extern void tasklet_unlock(struct tasklet_struct *); 295 extern void tasklet_unlock_wait(struct tasklet_struct *ts); 296 297 #endif /* _LINUX_INTERRUPT_H_ */ 298