interrupt.h revision 289624
1/* $FreeBSD: head/sys/ofed/include/linux/interrupt.h 289624 2015-10-20 11:40:04Z hselasky $ */
2/*-
3 * Copyright (c) 2010 Isilon Systems, Inc.
4 * Copyright (c) 2010 iX Systems, Inc.
5 * Copyright (c) 2010 Panasas, Inc.
6 * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice unmodified, this list of conditions, and the following
14 *    disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
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	int		 irq;
54};
55
56static inline int
57_irq_rid(struct device *dev, int irq)
58{
59	if (irq == dev->irq)
60		return (0);
61	return irq - dev->msix + 1;
62}
63
64static void
65_irq_handler(void *ent)
66{
67	struct irq_ent *irqe;
68
69	irqe = ent;
70	irqe->handler(irqe->irq, irqe->arg);
71}
72
73static inline struct irq_ent *
74_irq_ent(struct device *dev, int irq)
75{
76	struct irq_ent *irqe;
77
78	list_for_each_entry(irqe, &dev->irqents, links)
79		if (irqe->irq == irq)
80			return (irqe);
81
82	return (NULL);
83}
84
85static inline int
86request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
87    const char *name, void *arg)
88{
89	struct resource *res;
90	struct irq_ent *irqe;
91	struct device *dev;
92	int error;
93	int rid;
94
95	dev = _pci_find_irq_dev(irq);
96	if (dev == NULL)
97		return -ENXIO;
98	rid = _irq_rid(dev, irq);
99	res = bus_alloc_resource_any(dev->bsddev, SYS_RES_IRQ, &rid,
100	    flags | RF_ACTIVE);
101	if (res == NULL)
102		return (-ENXIO);
103	irqe = kmalloc(sizeof(*irqe), GFP_KERNEL);
104	irqe->dev = dev;
105	irqe->res = res;
106	irqe->arg = arg;
107	irqe->handler = handler;
108	irqe->irq = irq;
109	error = bus_setup_intr(dev->bsddev, res, INTR_TYPE_NET | INTR_MPSAFE,
110	    NULL, _irq_handler, irqe, &irqe->tag);
111	if (error) {
112		bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res);
113		kfree(irqe);
114		return (-error);
115	}
116	list_add(&irqe->links, &dev->irqents);
117
118	return 0;
119}
120
121static inline void
122free_irq(unsigned int irq, void *device)
123{
124	struct irq_ent *irqe;
125	struct device *dev;
126	int rid;
127
128	dev = _pci_find_irq_dev(irq);
129	if (dev == NULL)
130		return;
131	rid = _irq_rid(dev, irq);
132	irqe = _irq_ent(dev, irq);
133	if (irqe == NULL)
134		return;
135	bus_teardown_intr(dev->bsddev, irqe->res, irqe->tag);
136	bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res);
137	list_del(&irqe->links);
138	kfree(irqe);
139}
140
141#endif	/* _LINUX_INTERRUPT_H_ */
142