Commit c0527bef authored by Daniel Verkamp's avatar Daniel Verkamp
Browse files

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: default avatarDaniel Verkamp <daniel.verkamp@intel.com>
parent 7d30f5aa
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -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
}
+3 −1
Original line number Diff line number Diff line
@@ -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;
			}
		}
+34 −6
Original line number Diff line number Diff line
@@ -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__
+4 −1
Original line number Diff line number Diff line
@@ -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;
		}
	}
+11 −2
Original line number Diff line number Diff line
@@ -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;
}
Loading