env_dpdk: Add spdk_pci_device_[attach|detach]

These functions will attach or detach from a PCI device. Attaching
typically means mapping the BAR.

Change-Id: Iaaf59010b8a0366d32ec80bb90c1c277ada7cfe7
Signed-off-by: Cunyin Chang <cunyin.chang@intel.com>
This commit is contained in:
Cunyin Chang 2016-11-15 09:53:14 +08:00 committed by Ben Walker
parent 1c6cd96ee4
commit 56765aa39f
3 changed files with 62 additions and 7 deletions

View File

@ -212,6 +212,10 @@ struct spdk_pci_id spdk_pci_device_get_id(struct spdk_pci_device *dev);
int spdk_pci_device_get_serial_number(struct spdk_pci_device *dev, char *sn, size_t len);
int spdk_pci_device_claim(const struct spdk_pci_addr *pci_addr);
void spdk_pci_device_detach(struct spdk_pci_device *device);
int spdk_pci_device_attach(enum spdk_pci_device_type type,
spdk_pci_enum_cb enum_cb,
void *enum_ctx, struct spdk_pci_addr *pci_addres);
int spdk_pci_device_cfg_read8(struct spdk_pci_device *dev, uint8_t *value, uint32_t offset);
int spdk_pci_device_cfg_write8(struct spdk_pci_device *dev, uint8_t value, uint32_t offset);

View File

@ -246,6 +246,61 @@ static struct rte_driver g_ioat_drv = {
PMD_REGISTER_DRIVER(g_ioat_drv, spdk_ioat);
#endif
static struct spdk_pci_enum_ctx *
spdk_pci_find_driver(enum spdk_pci_device_type type)
{
if (type == SPDK_PCI_DEVICE_NVME) {
return &g_nvme_pci_drv;
} else if (type == SPDK_PCI_DEVICE_IOAT) {
return &g_ioat_pci_drv;
} else {
return NULL;
}
}
void
spdk_pci_device_detach(struct spdk_pci_device *device)
{
rte_eal_pci_detach(&device->addr);
}
int
spdk_pci_device_attach(enum spdk_pci_device_type type,
spdk_pci_enum_cb enum_cb,
void *enum_ctx, struct spdk_pci_addr *pci_address)
{
struct rte_pci_addr addr;
struct spdk_pci_enum_ctx *ctx;
ctx = spdk_pci_find_driver(type);
if (ctx == NULL) {
return -1;
}
addr.domain = pci_address->domain;
addr.bus = pci_address->bus;
addr.devid = pci_address->dev;
addr.function = pci_address->func;
pthread_mutex_lock(&ctx->mtx);
ctx->cb_fn = enum_cb;
ctx->cb_arg = enum_ctx;
if (rte_eal_pci_probe_one(&addr) != 0) {
ctx->cb_arg = NULL;
ctx->cb_fn = NULL;
pthread_mutex_unlock(&ctx->mtx);
return -1;
}
ctx->cb_arg = NULL;
ctx->cb_fn = NULL;
pthread_mutex_unlock(&ctx->mtx);
return 0;
}
/* Note: You can call spdk_pci_enumerate from more than one thread
* simultaneously safely, but you cannot call spdk_pci_enumerate
* and rte_eal_pci_probe simultaneously.
@ -255,17 +310,12 @@ spdk_pci_enumerate(enum spdk_pci_device_type type,
spdk_pci_enum_cb enum_cb,
void *enum_ctx)
{
struct spdk_pci_enum_ctx *ctx;
struct spdk_pci_enum_ctx *ctx = spdk_pci_find_driver(type);
if (type == SPDK_PCI_DEVICE_NVME) {
ctx = &g_nvme_pci_drv;
} else if (type == SPDK_PCI_DEVICE_IOAT) {
ctx = &g_ioat_pci_drv;
} else {
if (ctx == NULL) {
return -1;
}
pthread_mutex_lock(&ctx->mtx);
ctx->cb_fn = enum_cb;

View File

@ -578,6 +578,7 @@ nvme_pcie_ctrlr_destruct(struct spdk_nvme_ctrlr *ctrlr)
}
nvme_pcie_ctrlr_free_bars(pctrlr);
spdk_pci_device_detach(pctrlr->devhandle);
spdk_free(pctrlr);
return 0;