0

I am trying to write a kernel module that will handle MSI interrupts for a PCIe device. I have written a simple skeleton outline for my driver currently and whenever I try to call 'pci_disable_msi(dev)' I get an unable to handle kernel NULL pointer dereference error. I am following along exactly as described from the /Documentation/PCI/MSI-HOWTO.txt and it seems to me that I should not be getting this error. Is this a bug or is my setup incorrect? Judging by the last print that occures, I am pretty sure that it is happening at the fpga_remove() when I call pci_disable_msi(). (Clearly this occures when I am removing the module)

static struct pci_driver fpga_driver = {
        .name       = "PCIe_test",
        .id_table   = fpga_dev_table,
        .probe      = fpga_probe,
        .remove     = fpga_remove,
        .suspend    = fpga_suspend,
        .resume     = fpga_resume,
};

static irqreturn_t fpga_isr(int irq, struct pci_dev *dev)
{

    printk(KERN_NOTICE "THIS is the ISR\n");

    return IRQ_HANDLED;
}


static int setup_MSI_interrupt(struct pci_dev *dev, int num_msi)
{

    int result;
    result = pci_enable_msi(dev);
    if(result)
    {
        printk(KERN_WARNING "Could not enable MSI\n");
        return result;

    }
    printk(KERN_NOTICE "MSI has been enabled\n");
    printk(KERN_NOTICE "dev->irq line is %d", dev->irq);

    result = request_irq(dev->irq, fpga_isr, IRQF_SHARED, fpga_driver.name, dev);
    printk(KERN_NOTICE "Using IRQ num %d\n", dev->irq);

    if (result) {
        dev_err(&dev->dev, "Failed to allocate irq %d: %d\n", dev->irq, result);
        goto exit1;
    }
        dev_info(&dev->dev, "FPGA using PCIe Interrupt\n");

    return 0;
exit1:
    return -1;
}

static int fpga_probe(struct pci_dev *dev, const struct pci_device_id *id)
{
    printk(KERN_NOTICE "Probing driver\n");
    switch(dev->vendor) {
    case VENDOR://0x1708:
        printk(KERN_NOTICE "Xilinx device found\n");
        break;
    default:
        printk(KERN_NOTICE "Device found that does not match id: id = 0x%04X\n", dev->device);
    };

    int err = pci_enable_device(dev);
    if (err) {
        dev_err(&dev->dev, "Failed to enable FPGA PCI device (%d)\n", err);
        goto exit;
    }

    err = setup_MSI_interrupt(dev, NUM_MSI);

    if(err)
        goto exit;

    return 0;

exit:
    return -1;
}

static void fpga_remove(struct pci_dev *dev)
{
    printk(KERN_NOTICE "REMOVING IRQ # %d\n", dev->irq);

    free_irq(dev->irq, dev);
    printk(KERN_NOTICE "IRQ has been freed\n");
    pci_disable_msi(dev);                                               // This causes a NUll Pointer to be dereferenced but needs to be added
    printk(KERN_NOTICE "MSI has been disabled\n");
}

static int __init fpga_init(void)
{
    printk(KERN_NOTICE "Registering Driver\n");
    return pci_register_driver(&fpga_driver);
    return 0;
}
4

1 回答 1

1

您可能不应该在和函数中使用struct pci_devasvoid *dev_id参数。使用正确且相同的中断唯一参数调用它们很重要。这样做可能会导致内核在删除模块时出现恐慌。request_irq(...)free_irq(...)dev_idpci_disable_msi()

在此处阅读更多信息: request_irq 中的 dev_id 参数是什么?

于 2015-04-14T12:02:34.313 回答