From patchwork Sat Apr 16 01:35:50 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 8860871 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id C90B6BF29F for ; Sat, 16 Apr 2016 01:36:58 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DBF232017D for ; Sat, 16 Apr 2016 01:36:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D25592021A for ; Sat, 16 Apr 2016 01:36:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752622AbcDPBgE (ORCPT ); Fri, 15 Apr 2016 21:36:04 -0400 Received: from bombadil.infradead.org ([198.137.202.9]:48878 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751546AbcDPBf5 (ORCPT ); Fri, 15 Apr 2016 21:35:57 -0400 Received: from [107.17.164.132] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.80.1 #2 (Red Hat Linux)) id 1arF9Q-00055u-8d; Sat, 16 Apr 2016 01:35:56 +0000 From: Christoph Hellwig To: tglx@linutronix.de, linux-block@vger.kernel.org, linux-pci@vger.kernel.org Cc: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH 6/8] pci: provide sensible irq vector alloc/free routines Date: Fri, 15 Apr 2016 18:35:50 -0700 Message-Id: <1460770552-31260-7-git-send-email-hch@lst.de> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1460770552-31260-1-git-send-email-hch@lst.de> References: <1460770552-31260-1-git-send-email-hch@lst.de> X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Spam-Status: No, score=-5.4 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, SUSPICIOUS_RECIPS, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Hide all the MSI-X vs MSI vs legacy bullshit, and provide an array of interrupt vectors in the pci_dev structure, and ensure we get proper interrupt affinity by default. Signed-off-by: Christoph Hellwig --- drivers/pci/irq.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++- drivers/pci/msi.c | 2 +- drivers/pci/pci.h | 5 +++ include/linux/pci.h | 5 +++ 4 files changed, 99 insertions(+), 2 deletions(-) diff --git a/drivers/pci/irq.c b/drivers/pci/irq.c index 6684f15..b683465 100644 --- a/drivers/pci/irq.c +++ b/drivers/pci/irq.c @@ -1,7 +1,8 @@ /* - * PCI IRQ failure handing code + * PCI IRQ handing code * * Copyright (c) 2008 James Bottomley + * Copyright (c) 2016 Christoph Hellwig. */ #include @@ -9,6 +10,92 @@ #include #include #include +#include +#include "pci.h" + +static int pci_nr_irq_vectors(struct pci_dev *pdev) +{ + int nr_entries; + + nr_entries = pci_msix_vec_count(pdev); + if (nr_entries <= 0 && pci_msi_supported(pdev, 1)) + nr_entries = pci_msi_vec_count(pdev); + if (nr_entries <= 0) + nr_entries = 1; + return nr_entries; +} + +static int pci_enable_msix_range_wrapper(struct pci_dev *pdev, u32 *irqs, + int nr_vecs) +{ + struct msix_entry *msix_entries; + int vecs, i; + + msix_entries = kcalloc(nr_vecs, sizeof(struct msix_entry), GFP_KERNEL); + if (!msix_entries) + return -ENOMEM; + + for (i = 0; i < nr_vecs; i++) + msix_entries[i].entry = i; + + vecs = pci_enable_msix_range(pdev, msix_entries, 1, nr_vecs); + if (vecs > 0) { + for (i = 0; i < vecs; i++) + irqs[i] = msix_entries[i].vector; + } + + kfree(msix_entries); + return vecs; +} + +int pci_alloc_irq_vectors(struct pci_dev *pdev, int nr_vecs) +{ + int vecs, ret, i; + u32 *irqs; + + nr_vecs = min(nr_vecs, pci_nr_irq_vectors(pdev)); + + irqs = kcalloc(nr_vecs, sizeof(u32), GFP_KERNEL); + if (!irqs) + return -ENOMEM; + + vecs = pci_enable_msix_range_wrapper(pdev, irqs, nr_vecs); + if (vecs <= 0) { + vecs = pci_enable_msi_range(pdev, 1, min(nr_vecs, 32)); + if (vecs <= 0) { + ret = -EIO; + if (!pdev->irq) + goto out_free_irqs; + + /* use legacy irq */ + vecs = 1; + } + + for (i = 0; i < vecs; i++) + irqs[i] = pdev->irq + i; + } + + pdev->irqs = irqs; + return vecs; + +out_free_irqs: + kfree(irqs); + return ret; +} +EXPORT_SYMBOL(pci_alloc_irq_vectors); + +void pci_free_irq_vectors(struct pci_dev *pdev) +{ + if (pdev->msi_enabled) + pci_disable_msi(pdev); + else if (pdev->msix_enabled) + pci_disable_msix(pdev); + + kfree(pdev->dev.irq_affinity); + pdev->dev.irq_affinity = NULL; + kfree(pdev->irqs); +} +EXPORT_SYMBOL(pci_free_irq_vectors); static void pci_note_irq_problem(struct pci_dev *pdev, const char *reason) { diff --git a/drivers/pci/msi.c b/drivers/pci/msi.c index a080f44..544d306 100644 --- a/drivers/pci/msi.c +++ b/drivers/pci/msi.c @@ -815,7 +815,7 @@ out_free: * to determine if MSI/-X are supported for the device. If MSI/-X is * supported return 1, else return 0. **/ -static int pci_msi_supported(struct pci_dev *dev, int nvec) +int pci_msi_supported(struct pci_dev *dev, int nvec) { struct pci_bus *bus; diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index d0fb934..263422c 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -144,8 +144,13 @@ extern unsigned int pci_pm_d3_delay; #ifdef CONFIG_PCI_MSI void pci_no_msi(void); +int pci_msi_supported(struct pci_dev *dev, int nvec); #else static inline void pci_no_msi(void) { } +static int pci_msi_supported(struct pci_dev *dev, int nvec) +{ + return 0; +} #endif static inline void pci_msi_set_enable(struct pci_dev *dev, int enable) diff --git a/include/linux/pci.h b/include/linux/pci.h index 004b813..4fbc14f 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -322,6 +322,7 @@ struct pci_dev { * directly, use the values stored here. They might be different! */ unsigned int irq; + unsigned int *irqs; struct resource resource[DEVICE_COUNT_RESOURCE]; /* I/O and memory regions + expansion ROMs */ bool match_driver; /* Skip attaching driver */ @@ -1235,6 +1236,9 @@ resource_size_t pcibios_iov_resource_alignment(struct pci_dev *dev, int resno); int pci_set_vga_state(struct pci_dev *pdev, bool decode, unsigned int command_bits, u32 flags); +int pci_alloc_irq_vectors(struct pci_dev *dev, int nr_vecs); +void pci_free_irq_vectors(struct pci_dev *pdev); + /* kmem_cache style wrapper around pci_alloc_consistent() */ #include @@ -1282,6 +1286,7 @@ static inline int pci_enable_msix_exact(struct pci_dev *dev, return rc; return 0; } + #else static inline int pci_msi_vec_count(struct pci_dev *dev) { return -ENOSYS; } static inline void pci_msi_shutdown(struct pci_dev *dev) { }