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	_LINUXKPI_LINUX_INTERRUPT_H_
32 #define	_LINUXKPI_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/interrupt.h>
42 
43 typedef	irqreturn_t	(*irq_handler_t)(int, void *);
44 
45 #define	IRQF_SHARED	RF_SHAREABLE
46 
47 int  lkpi_request_irq(struct device *, unsigned int, irq_handler_t,
48 	irq_handler_t, unsigned long, const char *, void *);
49 int  lkpi_enable_irq(unsigned int);
50 void lkpi_disable_irq(unsigned int);
51 int  lkpi_bind_irq_to_cpu(unsigned int, int);
52 void lkpi_free_irq(unsigned int, void *);
53 void lkpi_devm_free_irq(struct device *, unsigned int, void *);
54 
55 static inline int
56 request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
57     const char *name, void *arg)
58 {
59 
60 	return (lkpi_request_irq(NULL, irq, handler, NULL, flags, name, arg));
61 }
62 
63 static inline int
64 request_threaded_irq(int irq, irq_handler_t handler,
65     irq_handler_t thread_handler, unsigned long flags,
66     const char *name, void *arg)
67 {
68 
69 	return (lkpi_request_irq(NULL, irq, handler, thread_handler,
70 	    flags, name, arg));
71 }
72 
73 static inline int
74 devm_request_irq(struct device *dev, int irq,
75     irq_handler_t handler, unsigned long flags, const char *name, void *arg)
76 {
77 
78 	return (lkpi_request_irq(dev, irq, handler, NULL, flags, name, arg));
79 }
80 
81 static inline int
82 devm_request_threaded_irq(struct device *dev, int irq,
83     irq_handler_t handler, irq_handler_t thread_handler,
84     unsigned long flags, const char *name, void *arg)
85 {
86 
87 	return (lkpi_request_irq(dev, irq, handler, thread_handler,
88 	    flags, name, arg));
89 }
90 
91 static inline int
92 enable_irq(unsigned int irq)
93 {
94 	return (lkpi_enable_irq(irq));
95 }
96 
97 static inline void
98 disable_irq(unsigned int irq)
99 {
100 	lkpi_disable_irq(irq);
101 }
102 
103 static inline int
104 bind_irq_to_cpu(unsigned int irq, int cpu_id)
105 {
106 	return (lkpi_bind_irq_to_cpu(irq, cpu_id));
107 }
108 
109 static inline void
110 free_irq(unsigned int irq, void *device)
111 {
112 	lkpi_free_irq(irq, device);
113 }
114 
115 static inline void
116 devm_free_irq(struct device *xdev, unsigned int irq, void *p)
117 {
118 	lkpi_devm_free_irq(xdev, irq, p);
119 }
120 
121 static inline int
122 irq_set_affinity_hint(int vector, cpumask_t *mask)
123 {
124 	int error;
125 
126 	if (mask != NULL)
127 		error = intr_setaffinity(vector, CPU_WHICH_IRQ, mask);
128 	else
129 		error = intr_setaffinity(vector, CPU_WHICH_IRQ, cpuset_root);
130 
131 	return (-error);
132 }
133 
134 static inline struct msi_desc *
135 irq_get_msi_desc(unsigned int irq)
136 {
137 
138 	return (lkpi_pci_msi_desc_alloc(irq));
139 }
140 
141 /*
142  * LinuxKPI tasklet support
143  */
144 struct tasklet_struct;
145 typedef void tasklet_func_t(unsigned long);
146 typedef void tasklet_callback_t(struct tasklet_struct *);
147 
148 struct tasklet_struct {
149 	TAILQ_ENTRY(tasklet_struct) entry;
150 	tasklet_func_t *func;
151 	/* Our "state" implementation is different. Avoid same name as Linux. */
152 	volatile u_int tasklet_state;
153 	atomic_t count;
154 	unsigned long data;
155 	tasklet_callback_t *callback;
156 	bool use_callback;
157 };
158 
159 #define	DECLARE_TASKLET(_name, _func, _data)	\
160 struct tasklet_struct _name = { .func = (_func), .data = (_data) }
161 
162 #define	tasklet_hi_schedule(t)	tasklet_schedule(t)
163 
164 /* Some other compat code in the tree has this defined as well. */
165 #define	from_tasklet(_dev, _t, _field)		\
166     container_of(_t, typeof(*(_dev)), _field)
167 
168 void tasklet_setup(struct tasklet_struct *, tasklet_callback_t *);
169 extern void tasklet_schedule(struct tasklet_struct *);
170 extern void tasklet_kill(struct tasklet_struct *);
171 extern void tasklet_init(struct tasklet_struct *, tasklet_func_t *,
172     unsigned long data);
173 extern void tasklet_enable(struct tasklet_struct *);
174 extern void tasklet_disable(struct tasklet_struct *);
175 extern void tasklet_disable_nosync(struct tasklet_struct *);
176 extern int tasklet_trylock(struct tasklet_struct *);
177 extern void tasklet_unlock(struct tasklet_struct *);
178 extern void tasklet_unlock_wait(struct tasklet_struct *ts);
179 
180 #endif	/* _LINUXKPI_LINUX_INTERRUPT_H_ */
181