1219820Sjeff/*- 2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc. 3219820Sjeff * Copyright (c) 2010 iX Systems, Inc. 4219820Sjeff * Copyright (c) 2010 Panasas, Inc. 5292192Shselasky * 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. 28219820Sjeff */ 29219820Sjeff 30219820Sjeff#ifndef _LINUX_INTERRUPT_H_ 31219820Sjeff#define _LINUX_INTERRUPT_H_ 32219820Sjeff 33219820Sjeff#include <linux/device.h> 34219820Sjeff#include <linux/pci.h> 35219820Sjeff 36219820Sjeff#include <sys/bus.h> 37219820Sjeff#include <sys/rman.h> 38219820Sjeff 39219820Sjefftypedef irqreturn_t (*irq_handler_t)(int, void *); 40219820Sjeff 41219820Sjeff#define IRQ_RETVAL(x) ((x) != IRQ_NONE) 42219820Sjeff 43219820Sjeff#define IRQF_SHARED RF_SHAREABLE 44219820Sjeff 45219820Sjeffstruct irq_ent { 46219820Sjeff struct list_head links; 47219820Sjeff struct device *dev; 48219820Sjeff struct resource *res; 49219820Sjeff void *arg; 50219820Sjeff irqreturn_t (*handler)(int, void *); 51219820Sjeff void *tag; 52219820Sjeff int irq; 53219820Sjeff}; 54219820Sjeff 55219820Sjeffstatic inline int 56219820Sjeff_irq_rid(struct device *dev, int irq) 57219820Sjeff{ 58219820Sjeff if (irq == dev->irq) 59219820Sjeff return (0); 60219820Sjeff return irq - dev->msix + 1; 61219820Sjeff} 62219820Sjeff 63219820Sjeffstatic void 64219820Sjeff_irq_handler(void *ent) 65219820Sjeff{ 66219820Sjeff struct irq_ent *irqe; 67219820Sjeff 68219820Sjeff irqe = ent; 69219820Sjeff irqe->handler(irqe->irq, irqe->arg); 70219820Sjeff} 71219820Sjeff 72219820Sjeffstatic inline struct irq_ent * 73219820Sjeff_irq_ent(struct device *dev, int irq) 74219820Sjeff{ 75219820Sjeff struct irq_ent *irqe; 76219820Sjeff 77219820Sjeff list_for_each_entry(irqe, &dev->irqents, links) 78219820Sjeff if (irqe->irq == irq) 79219820Sjeff return (irqe); 80219820Sjeff 81219820Sjeff return (NULL); 82219820Sjeff} 83219820Sjeff 84219820Sjeffstatic inline int 85219820Sjeffrequest_irq(unsigned int irq, irq_handler_t handler, unsigned long flags, 86219820Sjeff const char *name, void *arg) 87219820Sjeff{ 88219820Sjeff struct resource *res; 89219820Sjeff struct irq_ent *irqe; 90219820Sjeff struct device *dev; 91219820Sjeff int error; 92219820Sjeff int rid; 93219820Sjeff 94219820Sjeff dev = _pci_find_irq_dev(irq); 95219820Sjeff if (dev == NULL) 96219820Sjeff return -ENXIO; 97219820Sjeff rid = _irq_rid(dev, irq); 98219820Sjeff res = bus_alloc_resource_any(dev->bsddev, SYS_RES_IRQ, &rid, 99219820Sjeff flags | RF_ACTIVE); 100219820Sjeff if (res == NULL) 101219820Sjeff return (-ENXIO); 102219820Sjeff irqe = kmalloc(sizeof(*irqe), GFP_KERNEL); 103219820Sjeff irqe->dev = dev; 104219820Sjeff irqe->res = res; 105219820Sjeff irqe->arg = arg; 106219820Sjeff irqe->handler = handler; 107219820Sjeff irqe->irq = irq; 108219820Sjeff error = bus_setup_intr(dev->bsddev, res, INTR_TYPE_NET | INTR_MPSAFE, 109219820Sjeff NULL, _irq_handler, irqe, &irqe->tag); 110219820Sjeff if (error) { 111219820Sjeff bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res); 112219820Sjeff kfree(irqe); 113219820Sjeff return (-error); 114219820Sjeff } 115219820Sjeff list_add(&irqe->links, &dev->irqents); 116219820Sjeff 117219820Sjeff return 0; 118219820Sjeff} 119219820Sjeff 120292192Shselaskystatic inline int 121292192Shselaskybind_irq_to_cpu(unsigned int irq, int cpu_id) 122292192Shselasky{ 123292192Shselasky struct irq_ent *irqe; 124292192Shselasky struct device *dev; 125292192Shselasky 126292192Shselasky dev = _pci_find_irq_dev(irq); 127292192Shselasky if (dev == NULL) 128292192Shselasky return (-ENOENT); 129292192Shselasky 130292192Shselasky irqe = _irq_ent(dev, irq); 131292192Shselasky if (irqe == NULL) 132292192Shselasky return (-ENOENT); 133292192Shselasky 134292192Shselasky return (-bus_bind_intr(dev->bsddev, irqe->res, cpu_id)); 135292192Shselasky} 136292192Shselasky 137219820Sjeffstatic inline void 138219820Sjefffree_irq(unsigned int irq, void *device) 139219820Sjeff{ 140219820Sjeff struct irq_ent *irqe; 141219820Sjeff struct device *dev; 142219820Sjeff int rid; 143219820Sjeff 144219820Sjeff dev = _pci_find_irq_dev(irq); 145219820Sjeff if (dev == NULL) 146219820Sjeff return; 147219820Sjeff rid = _irq_rid(dev, irq); 148219820Sjeff irqe = _irq_ent(dev, irq); 149219820Sjeff if (irqe == NULL) 150219820Sjeff return; 151219820Sjeff bus_teardown_intr(dev->bsddev, irqe->res, irqe->tag); 152219820Sjeff bus_release_resource(dev->bsddev, SYS_RES_IRQ, rid, irqe->res); 153219820Sjeff list_del(&irqe->links); 154219820Sjeff kfree(irqe); 155219820Sjeff} 156219820Sjeff 157219820Sjeff#endif /* _LINUX_INTERRUPT_H_ */ 158