interrupt.h revision 290003
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: head/sys/ofed/include/linux/interrupt.h 290003 2015-10-26 13:28:34Z 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; 53219820Sjeff int irq; 54219820Sjeff}; 55219820Sjeff 56219820Sjeffstatic inline int 57219820Sjeff_irq_rid(struct device *dev, int irq) 58219820Sjeff{ 59219820Sjeff if (irq == dev->irq) 60219820Sjeff return (0); 61219820Sjeff return irq - dev->msix + 1; 62219820Sjeff} 63219820Sjeff 64219820Sjeffstatic void 65219820Sjeff_irq_handler(void *ent) 66219820Sjeff{ 67219820Sjeff struct irq_ent *irqe; 68219820Sjeff 69219820Sjeff irqe = ent; 70219820Sjeff irqe->handler(irqe->irq, irqe->arg); 71219820Sjeff} 72219820Sjeff 73219820Sjeffstatic inline struct irq_ent * 74219820Sjeff_irq_ent(struct device *dev, int irq) 75219820Sjeff{ 76219820Sjeff struct irq_ent *irqe; 77219820Sjeff 78219820Sjeff list_for_each_entry(irqe, &dev->irqents, links) 79219820Sjeff if (irqe->irq == irq) 80219820Sjeff return (irqe); 81219820Sjeff 82219820Sjeff return (NULL); 83219820Sjeff} 84219820Sjeff 85219820Sjeffstatic inline int 86219820Sjeffrequest_irq(unsigned int irq, irq_handler_t handler, unsigned long flags, 87219820Sjeff const char *name, void *arg) 88219820Sjeff{ 89219820Sjeff struct resource *res; 90219820Sjeff struct irq_ent *irqe; 91219820Sjeff struct device *dev; 92219820Sjeff int error; 93219820Sjeff int rid; 94219820Sjeff 95219820Sjeff dev = _pci_find_irq_dev(irq); 96219820Sjeff if (dev == NULL) 97219820Sjeff return -ENXIO; 98219820Sjeff rid = _irq_rid(dev, irq); 99219820Sjeff res = bus_alloc_resource_any(dev->bsddev, SYS_RES_IRQ, &rid, 100219820Sjeff flags | RF_ACTIVE); 101219820Sjeff if (res == NULL) 102219820Sjeff return (-ENXIO); 103219820Sjeff irqe = kmalloc(sizeof(*irqe), GFP_KERNEL); 104219820Sjeff irqe->dev = dev; 105219820Sjeff irqe->res = res; 106219820Sjeff irqe->arg = arg; 107219820Sjeff irqe->handler = handler; 108219820Sjeff irqe->irq = irq; 109219820Sjeff error = bus_setup_intr(dev->bsddev, res, INTR_TYPE_NET | INTR_MPSAFE, 110219820Sjeff NULL, _irq_handler, irqe, &irqe->tag); 111219820Sjeff if (error) { 112219820Sjeff bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res); 113219820Sjeff kfree(irqe); 114219820Sjeff return (-error); 115219820Sjeff } 116219820Sjeff list_add(&irqe->links, &dev->irqents); 117219820Sjeff 118219820Sjeff return 0; 119219820Sjeff} 120219820Sjeff 121290003Shselaskystatic inline int 122290003Shselaskybind_irq_to_cpu(unsigned int irq, int cpu_id) 123290003Shselasky{ 124290003Shselasky struct irq_ent *irqe; 125290003Shselasky struct device *dev; 126290003Shselasky 127290003Shselasky dev = _pci_find_irq_dev(irq); 128290003Shselasky if (dev == NULL) 129290003Shselasky return (-ENOENT); 130290003Shselasky 131290003Shselasky irqe = _irq_ent(dev, irq); 132290003Shselasky if (irqe == NULL) 133290003Shselasky return (-ENOENT); 134290003Shselasky 135290003Shselasky return (-bus_bind_intr(dev->bsddev, irqe->res, cpu_id)); 136290003Shselasky} 137290003Shselasky 138219820Sjeffstatic inline void 139219820Sjefffree_irq(unsigned int irq, void *device) 140219820Sjeff{ 141219820Sjeff struct irq_ent *irqe; 142219820Sjeff struct device *dev; 143219820Sjeff int rid; 144219820Sjeff 145219820Sjeff dev = _pci_find_irq_dev(irq); 146219820Sjeff if (dev == NULL) 147219820Sjeff return; 148219820Sjeff rid = _irq_rid(dev, irq); 149219820Sjeff irqe = _irq_ent(dev, irq); 150219820Sjeff if (irqe == NULL) 151219820Sjeff return; 152219820Sjeff bus_teardown_intr(dev->bsddev, irqe->res, irqe->tag); 153219820Sjeff bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res); 154219820Sjeff list_del(&irqe->links); 155219820Sjeff kfree(irqe); 156219820Sjeff} 157219820Sjeff 158219820Sjeff#endif /* _LINUX_INTERRUPT_H_ */ 159