interrupt.h revision 219820
1219820Sjeff/*- 2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc. 3219820Sjeff * Copyright (c) 2010 iX Systems, Inc. 4219820Sjeff * Copyright (c) 2010 Panasas, Inc. 5219820Sjeff * All rights reserved. 6219820Sjeff * 7219820Sjeff * Redistribution and use in source and binary forms, with or without 8219820Sjeff * modification, are permitted provided that the following conditions 9219820Sjeff * are met: 10219820Sjeff * 1. Redistributions of source code must retain the above copyright 11219820Sjeff * notice unmodified, this list of conditions, and the following 12219820Sjeff * disclaimer. 13219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 14219820Sjeff * notice, this list of conditions and the following disclaimer in the 15219820Sjeff * documentation and/or other materials provided with the distribution. 16219820Sjeff * 17219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27219820Sjeff */ 28219820Sjeff 29219820Sjeff#ifndef _LINUX_INTERRUPT_H_ 30219820Sjeff#define _LINUX_INTERRUPT_H_ 31219820Sjeff 32219820Sjeff#include <linux/device.h> 33219820Sjeff#include <linux/pci.h> 34219820Sjeff 35219820Sjeff#include <sys/bus.h> 36219820Sjeff#include <sys/rman.h> 37219820Sjeff 38219820Sjefftypedef irqreturn_t (*irq_handler_t)(int, void *); 39219820Sjeff 40219820Sjeff#define IRQ_RETVAL(x) ((x) != IRQ_NONE) 41219820Sjeff 42219820Sjeff#define IRQF_SHARED RF_SHAREABLE 43219820Sjeff 44219820Sjeffstruct irq_ent { 45219820Sjeff struct list_head links; 46219820Sjeff struct device *dev; 47219820Sjeff struct resource *res; 48219820Sjeff void *arg; 49219820Sjeff irqreturn_t (*handler)(int, void *); 50219820Sjeff void *tag; 51219820Sjeff int irq; 52219820Sjeff}; 53219820Sjeff 54219820Sjeffstatic inline int 55219820Sjeff_irq_rid(struct device *dev, int irq) 56219820Sjeff{ 57219820Sjeff if (irq == dev->irq) 58219820Sjeff return (0); 59219820Sjeff return irq - dev->msix + 1; 60219820Sjeff} 61219820Sjeff 62219820Sjeffstatic void 63219820Sjeff_irq_handler(void *ent) 64219820Sjeff{ 65219820Sjeff struct irq_ent *irqe; 66219820Sjeff 67219820Sjeff irqe = ent; 68219820Sjeff irqe->handler(irqe->irq, irqe->arg); 69219820Sjeff} 70219820Sjeff 71219820Sjeffstatic inline struct irq_ent * 72219820Sjeff_irq_ent(struct device *dev, int irq) 73219820Sjeff{ 74219820Sjeff struct irq_ent *irqe; 75219820Sjeff 76219820Sjeff list_for_each_entry(irqe, &dev->irqents, links) 77219820Sjeff if (irqe->irq == irq) 78219820Sjeff return (irqe); 79219820Sjeff 80219820Sjeff return (NULL); 81219820Sjeff} 82219820Sjeff 83219820Sjeffstatic inline int 84219820Sjeffrequest_irq(unsigned int irq, irq_handler_t handler, unsigned long flags, 85219820Sjeff const char *name, void *arg) 86219820Sjeff{ 87219820Sjeff struct resource *res; 88219820Sjeff struct irq_ent *irqe; 89219820Sjeff struct device *dev; 90219820Sjeff int error; 91219820Sjeff int rid; 92219820Sjeff 93219820Sjeff dev = _pci_find_irq_dev(irq); 94219820Sjeff if (dev == NULL) 95219820Sjeff return -ENXIO; 96219820Sjeff rid = _irq_rid(dev, irq); 97219820Sjeff res = bus_alloc_resource_any(dev->bsddev, SYS_RES_IRQ, &rid, 98219820Sjeff flags | RF_ACTIVE); 99219820Sjeff if (res == NULL) 100219820Sjeff return (-ENXIO); 101219820Sjeff irqe = kmalloc(sizeof(*irqe), GFP_KERNEL); 102219820Sjeff irqe->dev = dev; 103219820Sjeff irqe->res = res; 104219820Sjeff irqe->arg = arg; 105219820Sjeff irqe->handler = handler; 106219820Sjeff irqe->irq = irq; 107219820Sjeff error = bus_setup_intr(dev->bsddev, res, INTR_TYPE_NET | INTR_MPSAFE, 108219820Sjeff NULL, _irq_handler, irqe, &irqe->tag); 109219820Sjeff if (error) { 110219820Sjeff bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res); 111219820Sjeff kfree(irqe); 112219820Sjeff return (-error); 113219820Sjeff } 114219820Sjeff list_add(&irqe->links, &dev->irqents); 115219820Sjeff 116219820Sjeff return 0; 117219820Sjeff} 118219820Sjeff 119219820Sjeffstatic inline void 120219820Sjefffree_irq(unsigned int irq, void *device) 121219820Sjeff{ 122219820Sjeff struct irq_ent *irqe; 123219820Sjeff struct device *dev; 124219820Sjeff int rid; 125219820Sjeff 126219820Sjeff dev = _pci_find_irq_dev(irq); 127219820Sjeff if (dev == NULL) 128219820Sjeff return; 129219820Sjeff rid = _irq_rid(dev, irq); 130219820Sjeff irqe = _irq_ent(dev, irq); 131219820Sjeff if (irqe == NULL) 132219820Sjeff return; 133219820Sjeff bus_teardown_intr(dev->bsddev, irqe->res, irqe->tag); 134219820Sjeff bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res); 135219820Sjeff list_del(&irqe->links); 136219820Sjeff kfree(irqe); 137219820Sjeff} 138219820Sjeff 139219820Sjeff#endif /* _LINUX_INTERRUPT_H_ */ 140