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