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 <ziye.yang@intel.com>
Signed-off-by: Daniel Verkamp <daniel.verkamp@intel.com>
This commit is contained in:
Daniel Verkamp 2016-02-03 15:00:39 -07:00
parent ff7e2122c7
commit 9a924a066e
4 changed files with 78 additions and 53 deletions

View File

@ -36,6 +36,8 @@
#ifdef USE_PCIACCESS
#include <pciaccess.h>
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 <rte_pci.h>

View File

@ -10,6 +10,7 @@
#include <rte_atomic.h>
#include <rte_cycles.h>
#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);
}
/**

View File

@ -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);
}
/**

View File

@ -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)
{