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/device.h>
35 #include <linux/pci.h>
36 #include <linux/irqreturn.h>
37 
38 #include <sys/bus.h>
39 #include <sys/rman.h>
40 
41 typedef	irqreturn_t	(*irq_handler_t)(int, void *);
42 
43 #define	IRQF_SHARED	RF_SHAREABLE
44 
45 struct irq_ent {
46 	struct list_head	links;
47 	struct device	*dev;
48 	struct resource	*res;
49 	void		*arg;
50 	irqreturn_t	(*handler)(int, void *);
51 	void		*tag;
52 	unsigned int	irq;
53 };
54 
55 static inline int
56 linux_irq_rid(struct device *dev, unsigned int irq)
57 {
58 	if (irq == dev->irq)
59 		return (0);
60 	return irq - dev->msix + 1;
61 }
62 
63 extern void linux_irq_handler(void *);
64 
65 static inline struct irq_ent *
66 linux_irq_ent(struct device *dev, unsigned int irq)
67 {
68 	struct irq_ent *irqe;
69 
70 	list_for_each_entry(irqe, &dev->irqents, links)
71 		if (irqe->irq == irq)
72 			return (irqe);
73 
74 	return (NULL);
75 }
76 
77 static inline int
78 request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
79     const char *name, void *arg)
80 {
81 	struct resource *res;
82 	struct irq_ent *irqe;
83 	struct device *dev;
84 	int error;
85 	int rid;
86 
87 	dev = linux_pci_find_irq_dev(irq);
88 	if (dev == NULL)
89 		return -ENXIO;
90 	rid = linux_irq_rid(dev, irq);
91 	res = bus_alloc_resource_any(dev->bsddev, SYS_RES_IRQ, &rid,
92 	    flags | RF_ACTIVE);
93 	if (res == NULL)
94 		return (-ENXIO);
95 	irqe = kmalloc(sizeof(*irqe), GFP_KERNEL);
96 	irqe->dev = dev;
97 	irqe->res = res;
98 	irqe->arg = arg;
99 	irqe->handler = handler;
100 	irqe->irq = irq;
101 	error = bus_setup_intr(dev->bsddev, res, INTR_TYPE_NET | INTR_MPSAFE,
102 	    NULL, linux_irq_handler, irqe, &irqe->tag);
103 	if (error) {
104 		bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res);
105 		kfree(irqe);
106 		return (-error);
107 	}
108 	list_add(&irqe->links, &dev->irqents);
109 
110 	return 0;
111 }
112 
113 static inline int
114 enable_irq(unsigned int irq)
115 {
116 	struct irq_ent *irqe;
117 	struct device *dev;
118 
119 	dev = linux_pci_find_irq_dev(irq);
120 	if (dev == NULL)
121 		return -EINVAL;
122 	irqe = linux_irq_ent(dev, irq);
123 	if (irqe == NULL || irqe->tag != NULL)
124 		return -EINVAL;
125 	return -bus_setup_intr(dev->bsddev, irqe->res, INTR_TYPE_NET | INTR_MPSAFE,
126 	    NULL, linux_irq_handler, irqe, &irqe->tag);
127 }
128 
129 static inline void
130 disable_irq(unsigned int irq)
131 {
132 	struct irq_ent *irqe;
133 	struct device *dev;
134 
135 	dev = linux_pci_find_irq_dev(irq);
136 	if (dev == NULL)
137 		return;
138 	irqe = linux_irq_ent(dev, irq);
139 	if (irqe == NULL)
140 		return;
141 	if (irqe->tag != NULL)
142 		bus_teardown_intr(dev->bsddev, irqe->res, irqe->tag);
143 	irqe->tag = NULL;
144 }
145 
146 static inline int
147 bind_irq_to_cpu(unsigned int irq, int cpu_id)
148 {
149 	struct irq_ent *irqe;
150 	struct device *dev;
151 
152 	dev = linux_pci_find_irq_dev(irq);
153 	if (dev == NULL)
154 		return (-ENOENT);
155 
156 	irqe = linux_irq_ent(dev, irq);
157 	if (irqe == NULL)
158 		return (-ENOENT);
159 
160 	return (-bus_bind_intr(dev->bsddev, irqe->res, cpu_id));
161 }
162 
163 static inline void
164 free_irq(unsigned int irq, void *device)
165 {
166 	struct irq_ent *irqe;
167 	struct device *dev;
168 	int rid;
169 
170 	dev = linux_pci_find_irq_dev(irq);
171 	if (dev == NULL)
172 		return;
173 	rid = linux_irq_rid(dev, irq);
174 	irqe = linux_irq_ent(dev, irq);
175 	if (irqe == NULL)
176 		return;
177 	if (irqe->tag != NULL)
178 		bus_teardown_intr(dev->bsddev, irqe->res, irqe->tag);
179 	bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res);
180 	list_del(&irqe->links);
181 	kfree(irqe);
182 }
183 
184 /*
185  * LinuxKPI tasklet support
186  */
187 typedef void tasklet_func_t(unsigned long);
188 
189 struct tasklet_struct {
190 	TAILQ_ENTRY(tasklet_struct) entry;
191 	tasklet_func_t *func;
192 	unsigned long data;
193 };
194 
195 #define	DECLARE_TASKLET(name, func, data)	\
196 struct tasklet_struct name = { { NULL, NULL }, func, data }
197 
198 #define	tasklet_hi_schedule(t)	tasklet_schedule(t)
199 
200 extern void tasklet_schedule(struct tasklet_struct *);
201 extern void tasklet_kill(struct tasklet_struct *);
202 extern void tasklet_init(struct tasklet_struct *, tasklet_func_t *,
203     unsigned long data);
204 extern void tasklet_enable(struct tasklet_struct *);
205 extern void tasklet_disable(struct tasklet_struct *);
206 
207 #endif	/* _LINUX_INTERRUPT_H_ */
208