env: clean up PCI address comparison function

- Split the part that gets a PCI device's address into its own function,
  spdk_pci_device_get_addr(). This is useful outside of the comparison
  function and is orthogonal to comparing addresses.
- Make the comparison function take two addresses instead of a device
  and an address.  The more general form will be useful with addresses
  that are not directly associated with a device.  Because of this, also
  rename the function from spdk_pci_device_compare_addr() to
  spdk_pci_addr_compare().
- Return a signed value similar to strcmp() so that addresses can be
  ordered, not just compared for equality.

Change-Id: Idf304454af09ea57f1e1d5dc3a39b077378cecad
Signed-off-by: Daniel Verkamp <daniel.verkamp@intel.com>
This commit is contained in:
Daniel Verkamp 2016-10-28 11:00:18 -07:00
parent 7d30f5aa1e
commit c0527befc5
6 changed files with 72 additions and 13 deletions

View File

@ -173,6 +173,9 @@ uint16_t spdk_pci_device_get_domain(struct spdk_pci_device *dev);
uint8_t spdk_pci_device_get_bus(struct spdk_pci_device *dev);
uint8_t spdk_pci_device_get_dev(struct spdk_pci_device *dev);
uint8_t spdk_pci_device_get_func(struct spdk_pci_device *dev);
struct spdk_pci_addr spdk_pci_device_get_addr(struct spdk_pci_device *dev);
uint16_t spdk_pci_device_get_vendor_id(struct spdk_pci_device *dev);
uint16_t spdk_pci_device_get_device_id(struct spdk_pci_device *dev);
uint16_t spdk_pci_device_get_subvendor_id(struct spdk_pci_device *dev);
@ -189,7 +192,12 @@ int spdk_pci_device_cfg_write16(struct spdk_pci_device *dev, uint16_t value, uin
int spdk_pci_device_cfg_read32(struct spdk_pci_device *dev, uint32_t *value, uint32_t offset);
int spdk_pci_device_cfg_write32(struct spdk_pci_device *dev, uint32_t value, uint32_t offset);
bool spdk_pci_device_compare_addr(struct spdk_pci_device *dev, struct spdk_pci_addr *addr);
/**
* Compare two PCI addresses.
*
* \return 0 if a1 == a2, less than 0 if a1 < a2, greater than 0 if a1 > a2
*/
int spdk_pci_addr_compare(const struct spdk_pci_addr *a1, const struct spdk_pci_addr *a2);
#ifdef __cplusplus
}

View File

@ -414,10 +414,12 @@ blockdev_nvme_exist(struct nvme_probe_ctx *ctx)
{
int i;
struct nvme_device *nvme_dev;
struct spdk_pci_addr dev_addr;
for (i = 0; i < ctx->num_whitelist_controllers; i++) {
TAILQ_FOREACH(nvme_dev, &g_nvme_devices, tailq) {
if (spdk_pci_device_compare_addr(nvme_dev->pci_dev, &ctx->whitelist[i])) {
dev_addr = spdk_pci_device_get_addr(nvme_dev->pci_dev);
if (spdk_pci_addr_compare(&dev_addr, &ctx->whitelist[i]) == 0) {
return true;
}
}

40
lib/env/pci.c vendored
View File

@ -394,13 +394,41 @@ spdk_pci_device_get_serial_number(struct spdk_pci_device *dev, char *sn, size_t
return -1;
}
bool
spdk_pci_device_compare_addr(struct spdk_pci_device *dev, struct spdk_pci_addr *addr)
struct spdk_pci_addr
spdk_pci_device_get_addr(struct spdk_pci_device *pci_dev)
{
return ((spdk_pci_device_get_domain(dev) == addr->domain) &&
(spdk_pci_device_get_bus(dev) == addr->bus) &&
(spdk_pci_device_get_dev(dev) == addr->dev) &&
(spdk_pci_device_get_func(dev) == addr->func));
struct spdk_pci_addr pci_addr;
pci_addr.domain = spdk_pci_device_get_domain(pci_dev);
pci_addr.bus = spdk_pci_device_get_bus(pci_dev);
pci_addr.dev = spdk_pci_device_get_dev(pci_dev);
pci_addr.func = spdk_pci_device_get_func(pci_dev);
return pci_addr;
}
int
spdk_pci_addr_compare(const struct spdk_pci_addr *a1, const struct spdk_pci_addr *a2)
{
if (a1->domain > a2->domain) {
return 1;
} else if (a1->domain < a2->domain) {
return -1;
} else if (a1->bus > a2->bus) {
return 1;
} else if (a1->bus < a2->bus) {
return -1;
} else if (a1->dev > a2->dev) {
return 1;
} else if (a1->dev < a2->dev) {
return -1;
} else if (a1->func > a2->func) {
return 1;
} else if (a1->func < a2->func) {
return -1;
}
return 0;
}
#ifdef __linux__

View File

@ -229,6 +229,9 @@ nvme_enum_cb(void *ctx, struct spdk_pci_device *pci_dev)
struct nvme_enum_ctx *enum_ctx = ctx;
struct spdk_nvme_ctrlr *ctrlr;
struct spdk_nvme_ctrlr_opts opts;
struct spdk_pci_addr dev_addr;
dev_addr = spdk_pci_device_get_addr(pci_dev);
/* Verify that this controller is not already attached */
TAILQ_FOREACH(ctrlr, &g_spdk_nvme_driver->attached_ctrlrs, tailq) {
@ -236,7 +239,7 @@ nvme_enum_cb(void *ctx, struct spdk_pci_device *pci_dev)
* different per each process, we compare by BDF to determine whether it is the
* same controller.
*/
if (spdk_pci_device_compare_addr(pci_dev, &ctrlr->pci_addr)) {
if (spdk_pci_addr_compare(&dev_addr, &ctrlr->pci_addr) == 0) {
return 0;
}
}

View File

@ -73,8 +73,17 @@ spdk_nvme_ctrlr_opts_set_defaults(struct spdk_nvme_ctrlr_opts *opts)
memset(opts, 0, sizeof(*opts));
}
bool
spdk_pci_device_compare_addr(struct spdk_pci_device *dev, struct spdk_pci_addr *addr)
struct spdk_pci_addr
spdk_pci_device_get_addr(struct spdk_pci_device *pci_dev)
{
struct spdk_pci_addr pci_addr;
memset(&pci_addr, 0, sizeof(pci_addr));
return pci_addr;
}
int
spdk_pci_addr_compare(const struct spdk_pci_addr *a1, const struct spdk_pci_addr *a2)
{
return true;
}

View File

@ -77,8 +77,17 @@ nvme_ctrlr_start(struct spdk_nvme_ctrlr *ctrlr)
return 0;
}
bool
spdk_pci_device_compare_addr(struct spdk_pci_device *dev, struct spdk_pci_addr *addr)
struct spdk_pci_addr
spdk_pci_device_get_addr(struct spdk_pci_device *pci_dev)
{
struct spdk_pci_addr pci_addr;
memset(&pci_addr, 0, sizeof(pci_addr));
return pci_addr;
}
int
spdk_pci_addr_compare(const struct spdk_pci_addr *a1, const struct spdk_pci_addr *a2)
{
return true;
}