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/bus.h> 41 #include <sys/rman.h> 42 #include <sys/interrupt.h> 43 44 typedef irqreturn_t (*irq_handler_t)(int, void *); 45 46 #define IRQF_SHARED RF_SHAREABLE 47 48 struct irq_ent { 49 struct list_head links; 50 struct device *dev; 51 struct resource *res; 52 void *arg; 53 irqreturn_t (*handler)(int, void *); 54 void *tag; 55 unsigned int irq; 56 }; 57 58 static inline int 59 linux_irq_rid(struct device *dev, unsigned int irq) 60 { 61 /* check for MSI- or MSIX- interrupt */ 62 if (irq >= dev->irq_start && irq < dev->irq_end) 63 return (irq - dev->irq_start + 1); 64 else 65 return (0); 66 } 67 68 extern void linux_irq_handler(void *); 69 70 static inline struct irq_ent * 71 linux_irq_ent(struct device *dev, unsigned int irq) 72 { 73 struct irq_ent *irqe; 74 75 list_for_each_entry(irqe, &dev->irqents, links) 76 if (irqe->irq == irq) 77 return (irqe); 78 79 return (NULL); 80 } 81 82 static inline int 83 request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags, 84 const char *name, void *arg) 85 { 86 struct resource *res; 87 struct irq_ent *irqe; 88 struct device *dev; 89 int error; 90 int rid; 91 92 dev = linux_pci_find_irq_dev(irq); 93 if (dev == NULL) 94 return -ENXIO; 95 rid = linux_irq_rid(dev, irq); 96 res = bus_alloc_resource_any(dev->bsddev, SYS_RES_IRQ, &rid, 97 flags | RF_ACTIVE); 98 if (res == NULL) 99 return (-ENXIO); 100 irqe = kmalloc(sizeof(*irqe), GFP_KERNEL); 101 irqe->dev = dev; 102 irqe->res = res; 103 irqe->arg = arg; 104 irqe->handler = handler; 105 irqe->irq = irq; 106 error = bus_setup_intr(dev->bsddev, res, INTR_TYPE_NET | INTR_MPSAFE, 107 NULL, linux_irq_handler, irqe, &irqe->tag); 108 if (error) { 109 bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res); 110 kfree(irqe); 111 return (-error); 112 } 113 list_add(&irqe->links, &dev->irqents); 114 115 return 0; 116 } 117 118 static inline int 119 enable_irq(unsigned int irq) 120 { 121 struct irq_ent *irqe; 122 struct device *dev; 123 124 dev = linux_pci_find_irq_dev(irq); 125 if (dev == NULL) 126 return -EINVAL; 127 irqe = linux_irq_ent(dev, irq); 128 if (irqe == NULL || irqe->tag != NULL) 129 return -EINVAL; 130 return -bus_setup_intr(dev->bsddev, irqe->res, INTR_TYPE_NET | INTR_MPSAFE, 131 NULL, linux_irq_handler, irqe, &irqe->tag); 132 } 133 134 static inline void 135 disable_irq(unsigned int irq) 136 { 137 struct irq_ent *irqe; 138 struct device *dev; 139 140 dev = linux_pci_find_irq_dev(irq); 141 if (dev == NULL) 142 return; 143 irqe = linux_irq_ent(dev, irq); 144 if (irqe == NULL) 145 return; 146 if (irqe->tag != NULL) 147 bus_teardown_intr(dev->bsddev, irqe->res, irqe->tag); 148 irqe->tag = NULL; 149 } 150 151 static inline int 152 bind_irq_to_cpu(unsigned int irq, int cpu_id) 153 { 154 struct irq_ent *irqe; 155 struct device *dev; 156 157 dev = linux_pci_find_irq_dev(irq); 158 if (dev == NULL) 159 return (-ENOENT); 160 161 irqe = linux_irq_ent(dev, irq); 162 if (irqe == NULL) 163 return (-ENOENT); 164 165 return (-bus_bind_intr(dev->bsddev, irqe->res, cpu_id)); 166 } 167 168 static inline void 169 free_irq(unsigned int irq, void *device) 170 { 171 struct irq_ent *irqe; 172 struct device *dev; 173 int rid; 174 175 dev = linux_pci_find_irq_dev(irq); 176 if (dev == NULL) 177 return; 178 rid = linux_irq_rid(dev, irq); 179 irqe = linux_irq_ent(dev, irq); 180 if (irqe == NULL) 181 return; 182 if (irqe->tag != NULL) 183 bus_teardown_intr(dev->bsddev, irqe->res, irqe->tag); 184 bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res); 185 list_del(&irqe->links); 186 kfree(irqe); 187 } 188 189 static inline int 190 irq_set_affinity_hint(int vector, cpumask_t *mask) 191 { 192 int error; 193 194 if (mask != NULL) 195 error = intr_setaffinity(vector, CPU_WHICH_IRQ, mask); 196 else 197 error = intr_setaffinity(vector, CPU_WHICH_IRQ, cpuset_root); 198 199 return (-error); 200 } 201 202 /* 203 * LinuxKPI tasklet support 204 */ 205 typedef void tasklet_func_t(unsigned long); 206 207 struct tasklet_struct { 208 TAILQ_ENTRY(tasklet_struct) entry; 209 tasklet_func_t *func; 210 /* Our "state" implementation is different. Avoid same name as Linux. */ 211 volatile u_int tasklet_state; 212 atomic_t count; 213 unsigned long data; 214 }; 215 216 #define DECLARE_TASKLET(_name, _func, _data) \ 217 struct tasklet_struct _name = { .func = (_func), .data = (_data) } 218 219 #define tasklet_hi_schedule(t) tasklet_schedule(t) 220 221 extern void tasklet_schedule(struct tasklet_struct *); 222 extern void tasklet_kill(struct tasklet_struct *); 223 extern void tasklet_init(struct tasklet_struct *, tasklet_func_t *, 224 unsigned long data); 225 extern void tasklet_enable(struct tasklet_struct *); 226 extern void tasklet_disable(struct tasklet_struct *); 227 extern void tasklet_disable_nosync(struct tasklet_struct *); 228 extern int tasklet_trylock(struct tasklet_struct *); 229 extern void tasklet_unlock(struct tasklet_struct *); 230 extern void tasklet_unlock_wait(struct tasklet_struct *ts); 231 232 #endif /* _LINUX_INTERRUPT_H_ */ 233