interrupt.h revision 329957
1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc.
3219820Sjeff * Copyright (c) 2010 iX Systems, Inc.
4219820Sjeff * Copyright (c) 2010 Panasas, Inc.
5290003Shselasky * Copyright (c) 2013-2015 Mellanox Technologies, Ltd.
6219820Sjeff * All rights reserved.
7219820Sjeff *
8219820Sjeff * Redistribution and use in source and binary forms, with or without
9219820Sjeff * modification, are permitted provided that the following conditions
10219820Sjeff * are met:
11219820Sjeff * 1. Redistributions of source code must retain the above copyright
12219820Sjeff *    notice unmodified, this list of conditions, and the following
13219820Sjeff *    disclaimer.
14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
15219820Sjeff *    notice, this list of conditions and the following disclaimer in the
16219820Sjeff *    documentation and/or other materials provided with the distribution.
17219820Sjeff *
18219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28289644Shselasky *
29289644Shselasky * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/interrupt.h 329957 2018-02-25 10:22:27Z hselasky $
30219820Sjeff */
31219820Sjeff#ifndef	_LINUX_INTERRUPT_H_
32219820Sjeff#define	_LINUX_INTERRUPT_H_
33219820Sjeff
34219820Sjeff#include <linux/device.h>
35219820Sjeff#include <linux/pci.h>
36219820Sjeff
37219820Sjeff#include <sys/bus.h>
38219820Sjeff#include <sys/rman.h>
39219820Sjeff
40219820Sjefftypedef	irqreturn_t	(*irq_handler_t)(int, void *);
41219820Sjeff
42219820Sjeff#define	IRQ_RETVAL(x)	((x) != IRQ_NONE)
43219820Sjeff
44219820Sjeff#define	IRQF_SHARED	RF_SHAREABLE
45219820Sjeff
46219820Sjeffstruct irq_ent {
47219820Sjeff	struct list_head	links;
48219820Sjeff	struct device	*dev;
49219820Sjeff	struct resource	*res;
50219820Sjeff	void		*arg;
51219820Sjeff	irqreturn_t	(*handler)(int, void *);
52219820Sjeff	void		*tag;
53310250Shselasky	unsigned int	irq;
54219820Sjeff};
55219820Sjeff
56219820Sjeffstatic inline int
57310250Shselaskylinux_irq_rid(struct device *dev, unsigned int irq)
58219820Sjeff{
59219820Sjeff	if (irq == dev->irq)
60219820Sjeff		return (0);
61219820Sjeff	return irq - dev->msix + 1;
62219820Sjeff}
63219820Sjeff
64293419Shselaskyextern void linux_irq_handler(void *);
65219820Sjeff
66219820Sjeffstatic inline struct irq_ent *
67310250Shselaskylinux_irq_ent(struct device *dev, unsigned int irq)
68219820Sjeff{
69219820Sjeff	struct irq_ent *irqe;
70219820Sjeff
71219820Sjeff	list_for_each_entry(irqe, &dev->irqents, links)
72219820Sjeff		if (irqe->irq == irq)
73219820Sjeff			return (irqe);
74219820Sjeff
75219820Sjeff	return (NULL);
76219820Sjeff}
77219820Sjeff
78219820Sjeffstatic inline int
79219820Sjeffrequest_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
80219820Sjeff    const char *name, void *arg)
81219820Sjeff{
82219820Sjeff	struct resource *res;
83219820Sjeff	struct irq_ent *irqe;
84219820Sjeff	struct device *dev;
85219820Sjeff	int error;
86219820Sjeff	int rid;
87219820Sjeff
88310250Shselasky	dev = linux_pci_find_irq_dev(irq);
89219820Sjeff	if (dev == NULL)
90219820Sjeff		return -ENXIO;
91293419Shselasky	rid = linux_irq_rid(dev, irq);
92219820Sjeff	res = bus_alloc_resource_any(dev->bsddev, SYS_RES_IRQ, &rid,
93219820Sjeff	    flags | RF_ACTIVE);
94219820Sjeff	if (res == NULL)
95219820Sjeff		return (-ENXIO);
96219820Sjeff	irqe = kmalloc(sizeof(*irqe), GFP_KERNEL);
97219820Sjeff	irqe->dev = dev;
98219820Sjeff	irqe->res = res;
99219820Sjeff	irqe->arg = arg;
100219820Sjeff	irqe->handler = handler;
101219820Sjeff	irqe->irq = irq;
102219820Sjeff	error = bus_setup_intr(dev->bsddev, res, INTR_TYPE_NET | INTR_MPSAFE,
103293419Shselasky	    NULL, linux_irq_handler, irqe, &irqe->tag);
104219820Sjeff	if (error) {
105219820Sjeff		bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res);
106219820Sjeff		kfree(irqe);
107219820Sjeff		return (-error);
108219820Sjeff	}
109219820Sjeff	list_add(&irqe->links, &dev->irqents);
110219820Sjeff
111219820Sjeff	return 0;
112219820Sjeff}
113219820Sjeff
114290003Shselaskystatic inline int
115329952Shselaskyenable_irq(unsigned int irq)
116329952Shselasky{
117329952Shselasky	struct irq_ent *irqe;
118329952Shselasky	struct device *dev;
119329952Shselasky
120329952Shselasky	dev = linux_pci_find_irq_dev(irq);
121329952Shselasky	if (dev == NULL)
122329952Shselasky		return -EINVAL;
123329952Shselasky	irqe = linux_irq_ent(dev, irq);
124329957Shselasky	if (irqe == NULL || irqe->tag != NULL)
125329952Shselasky		return -EINVAL;
126329952Shselasky	return -bus_setup_intr(dev->bsddev, irqe->res, INTR_TYPE_NET | INTR_MPSAFE,
127329952Shselasky	    NULL, linux_irq_handler, irqe, &irqe->tag);
128329952Shselasky}
129329952Shselasky
130329952Shselaskystatic inline void
131329952Shselaskydisable_irq(unsigned int irq)
132329952Shselasky{
133329952Shselasky	struct irq_ent *irqe;
134329952Shselasky	struct device *dev;
135329952Shselasky
136329952Shselasky	dev = linux_pci_find_irq_dev(irq);
137329952Shselasky	if (dev == NULL)
138329952Shselasky		return;
139329952Shselasky	irqe = linux_irq_ent(dev, irq);
140329952Shselasky	if (irqe == NULL)
141329952Shselasky		return;
142329957Shselasky	if (irqe->tag != NULL)
143329957Shselasky		bus_teardown_intr(dev->bsddev, irqe->res, irqe->tag);
144329952Shselasky	irqe->tag = NULL;
145329952Shselasky}
146329952Shselasky
147329952Shselaskystatic inline int
148290003Shselaskybind_irq_to_cpu(unsigned int irq, int cpu_id)
149290003Shselasky{
150290003Shselasky	struct irq_ent *irqe;
151290003Shselasky	struct device *dev;
152290003Shselasky
153310250Shselasky	dev = linux_pci_find_irq_dev(irq);
154290003Shselasky	if (dev == NULL)
155290003Shselasky		return (-ENOENT);
156290003Shselasky
157293419Shselasky	irqe = linux_irq_ent(dev, irq);
158290003Shselasky	if (irqe == NULL)
159290003Shselasky		return (-ENOENT);
160290003Shselasky
161290003Shselasky	return (-bus_bind_intr(dev->bsddev, irqe->res, cpu_id));
162290003Shselasky}
163290003Shselasky
164219820Sjeffstatic inline void
165219820Sjefffree_irq(unsigned int irq, void *device)
166219820Sjeff{
167219820Sjeff	struct irq_ent *irqe;
168219820Sjeff	struct device *dev;
169219820Sjeff	int rid;
170219820Sjeff
171310250Shselasky	dev = linux_pci_find_irq_dev(irq);
172219820Sjeff	if (dev == NULL)
173219820Sjeff		return;
174293419Shselasky	rid = linux_irq_rid(dev, irq);
175293419Shselasky	irqe = linux_irq_ent(dev, irq);
176219820Sjeff	if (irqe == NULL)
177219820Sjeff		return;
178329957Shselasky	if (irqe->tag != NULL)
179329957Shselasky		bus_teardown_intr(dev->bsddev, irqe->res, irqe->tag);
180219820Sjeff	bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res);
181219820Sjeff	list_del(&irqe->links);
182219820Sjeff	kfree(irqe);
183219820Sjeff}
184219820Sjeff
185328653Shselasky/*
186328653Shselasky * LinuxKPI tasklet support
187328653Shselasky */
188328653Shselaskytypedef void tasklet_func_t(unsigned long);
189328653Shselasky
190328653Shselaskystruct tasklet_struct {
191328653Shselasky	TAILQ_ENTRY(tasklet_struct) entry;
192328653Shselasky	tasklet_func_t *func;
193328653Shselasky	unsigned long data;
194328653Shselasky};
195328653Shselasky
196328653Shselasky#define	DECLARE_TASKLET(name, func, data)	\
197328653Shselaskystruct tasklet_struct name = { { NULL, NULL }, func, data }
198328653Shselasky
199328653Shselasky#define	tasklet_hi_schedule(t)	tasklet_schedule(t)
200328653Shselasky
201328653Shselaskyextern void tasklet_schedule(struct tasklet_struct *);
202328653Shselaskyextern void tasklet_kill(struct tasklet_struct *);
203328653Shselaskyextern void tasklet_init(struct tasklet_struct *, tasklet_func_t *,
204328653Shselasky    unsigned long data);
205329953Shselaskyextern void tasklet_enable(struct tasklet_struct *);
206329953Shselaskyextern void tasklet_disable(struct tasklet_struct *);
207328653Shselasky
208219820Sjeff#endif	/* _LINUX_INTERRUPT_H_ */
209