From 9a924a066eb0229b7f1457928532c8b80ed00718 Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Wed, 3 Feb 2016 15:00:39 -0700 Subject: [PATCH] pci: factor out PCI enumeration into util lib This is a step toward abstracting PCI access so that libpciaccess can be swapped out more easily. Change-Id: I5491459460cbfbd0be471f70f9d07a7eb3175234 Signed-off-by: Ziye Yang Signed-off-by: Daniel Verkamp --- include/spdk/pci.h | 3 +++ lib/ioat/ioat_impl.h | 54 ++++++++++++++++++++------------------------ lib/nvme/nvme_impl.h | 46 ++++++++++++++++++------------------- lib/util/pci.c | 28 +++++++++++++++++++++++ 4 files changed, 78 insertions(+), 53 deletions(-) diff --git a/include/spdk/pci.h b/include/spdk/pci.h index 9093679268..ca838177e0 100644 --- a/include/spdk/pci.h +++ b/include/spdk/pci.h @@ -36,6 +36,8 @@ #ifdef USE_PCIACCESS #include +int spdk_pci_enumerate(int (*enum_cb)(void *enum_ctx, void *pci_dev), void *enum_ctx); + #define spdk_pci_device_get_domain(dev) (dev->domain) #define spdk_pci_device_get_bus(dev) (dev->bus) #define spdk_pci_device_get_dev(pdev) (pdev->dev) @@ -44,6 +46,7 @@ #define spdk_pci_device_get_device_id(dev) (dev->device_id) #define spdk_pci_device_get_subvendor_id(dev) (dev->subvendor_id) #define spdk_pci_device_get_subdevice_id(dev) (dev->subdevice_id) +#define spdk_pci_device_get_class(dev) (dev->device_class) #else #include diff --git a/lib/ioat/ioat_impl.h b/lib/ioat/ioat_impl.h index 69ae39b7b5..badf2ab88a 100644 --- a/lib/ioat/ioat_impl.h +++ b/lib/ioat/ioat_impl.h @@ -10,6 +10,7 @@ #include #include +#include "spdk/pci.h" #include "spdk/vtophys.h" #include "spdk/pci.h" #include "spdk/ioat.h" @@ -124,40 +125,35 @@ ioat_pci_device_match_id(uint16_t vendor_id, uint16_t device_id) return false; } +struct ioat_pci_enum_ctx { + int (*user_enum_cb)(void *enum_ctx, void *pci_dev); + void *user_enum_ctx; +}; + +static int +ioat_pci_enum_cb(void *enum_ctx, void *pdev) +{ + struct ioat_pci_enum_ctx *ctx = enum_ctx; + struct pci_device *pci_dev = pdev; + uint16_t vendor_id = spdk_pci_device_get_vendor_id(pci_dev); + uint16_t device_id = spdk_pci_device_get_device_id(pci_dev); + + if (!ioat_pci_device_match_id(vendor_id, device_id)) { + return 0; + } + + return ctx->user_enum_cb(ctx->user_enum_ctx, pci_dev); +} + static inline int ioat_pci_enumerate(int (*enum_cb)(void *enum_ctx, void *pci_dev), void *enum_ctx) { - struct pci_device_iterator *pci_dev_iter; - struct pci_device *pci_dev; - struct pci_id_match match; - int rc; + struct ioat_pci_enum_ctx ioat_enum_ctx; - match.vendor_id = PCI_VENDOR_ID_INTEL; - match.subvendor_id = PCI_MATCH_ANY; - match.subdevice_id = PCI_MATCH_ANY; - match.device_id = PCI_MATCH_ANY; - match.device_class = 0x088000; - match.device_class_mask = 0xFFFFFF; + ioat_enum_ctx.user_enum_cb = enum_cb; + ioat_enum_ctx.user_enum_ctx = enum_ctx; - pci_dev_iter = pci_id_match_iterator_create(&match); - - rc = 0; - while ((pci_dev = pci_device_next(pci_dev_iter))) { - if (!(ioat_pci_device_match_id(pci_dev->vendor_id, - pci_dev->device_id))) { - continue; - } - - pci_device_probe(pci_dev); - - if (enum_cb(enum_ctx, pci_dev)) { - rc = -1; - } - } - - pci_iterator_destroy(pci_dev_iter); - - return rc; + return spdk_pci_enumerate(ioat_pci_enum_cb, &ioat_enum_ctx); } /** diff --git a/lib/nvme/nvme_impl.h b/lib/nvme/nvme_impl.h index d46eb4b8bb..d14e42f5fa 100644 --- a/lib/nvme/nvme_impl.h +++ b/lib/nvme/nvme_impl.h @@ -119,35 +119,33 @@ extern struct rte_mempool *request_mempool; #define nvme_dealloc_request(buf) rte_mempool_put(request_mempool, buf) #ifdef USE_PCIACCESS +struct nvme_pci_enum_ctx { + int (*user_enum_cb)(void *enum_ctx, void *pci_dev); + void *user_enum_ctx; +}; + +static int +nvme_pci_enum_cb(void *enum_ctx, void *pdev) +{ + struct nvme_pci_enum_ctx *ctx = enum_ctx; + struct pci_device *pci_dev = pdev; + + if (spdk_pci_device_get_class(pci_dev) != NVME_CLASS_CODE) { + return 0; + } + + return ctx->user_enum_cb(ctx->user_enum_ctx, pci_dev); +} + static inline int nvme_pci_enumerate(int (*enum_cb)(void *enum_ctx, void *pci_dev), void *enum_ctx) { - struct pci_device_iterator *pci_dev_iter; - struct pci_device *pci_dev; - struct pci_id_match match; - int rc; + struct nvme_pci_enum_ctx nvme_enum_ctx; - match.vendor_id = PCI_MATCH_ANY; - match.subvendor_id = PCI_MATCH_ANY; - match.subdevice_id = PCI_MATCH_ANY; - match.device_id = PCI_MATCH_ANY; - match.device_class = NVME_CLASS_CODE; - match.device_class_mask = 0xFFFFFF; + nvme_enum_ctx.user_enum_cb = enum_cb; + nvme_enum_ctx.user_enum_ctx = enum_ctx; - pci_dev_iter = pci_id_match_iterator_create(&match); - - rc = 0; - while ((pci_dev = pci_device_next(pci_dev_iter))) { - pci_device_probe(pci_dev); - - if (enum_cb(enum_ctx, pci_dev)) { - rc = -1; - } - } - - pci_iterator_destroy(pci_dev_iter); - - return rc; + return spdk_pci_enumerate(nvme_pci_enum_cb, &nvme_enum_ctx); } /** diff --git a/lib/util/pci.c b/lib/util/pci.c index ef63be8e26..d7e5b71bde 100644 --- a/lib/util/pci.c +++ b/lib/util/pci.c @@ -96,6 +96,34 @@ static int pci_device_get_info(struct pci_device *dev, const char *file, uint32_ } #endif +int +spdk_pci_enumerate(int (*enum_cb)(void *enum_ctx, void *pci_dev), void *enum_ctx) +{ + struct pci_device_iterator *pci_dev_iter; + struct pci_device *pci_dev; + struct pci_slot_match match; + int rc; + + match.domain = PCI_MATCH_ANY; + match.bus = PCI_MATCH_ANY; + match.dev = PCI_MATCH_ANY; + match.func = PCI_MATCH_ANY; + + pci_dev_iter = pci_slot_match_iterator_create(&match); + + rc = 0; + while ((pci_dev = pci_device_next(pci_dev_iter))) { + pci_device_probe(pci_dev); + if (enum_cb(enum_ctx, pci_dev)) { + rc = -1; + } + } + + pci_iterator_destroy(pci_dev_iter); + + return rc; +} + int pci_device_get_serial_number(struct pci_device *dev, char *sn, int len) {