Skip to content

Commit

Permalink
tree: Add PCI physical slot number for controller
Browse files Browse the repository at this point in the history
This commit introduces a physical slot field for controller, that
contains the PCI physcial slot number for controller device.

In case, there are multiple NVME drives present on the platform,
it's hard to identify which NVME drive is present in which slot.
The slot number is usually helpful in determining the location.
It is cross reference-able from lspci, but it would be nice to
have a direct option.

Signed-off-by: Umer Saleem <[email protected]>
  • Loading branch information
usaleem-ix authored and igaw committed Jun 14, 2023
1 parent 110339e commit 42ac453
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/libnvme.map
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

LIBNVME_1_5 {
global:
nvme_ctrl_get_phy_slot;
nvme_ipaddrs_eq;
nvme_nbft_free;
nvme_nbft_read;
Expand Down
1 change: 1 addition & 0 deletions src/nvme/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ struct nvme_ctrl {
char *dhchap_ctrl_key;
char *cntrltype;
char *dctype;
char *phy_slot;
bool discovery_ctrl;
bool unique_discovery_ctrl;
bool discovered;
Expand Down
53 changes: 53 additions & 0 deletions src/nvme/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "log.h"
#include "private.h"

const char *nvme_slots_sysfs_dir = "/sys/bus/pci/slots";

static struct nvme_host *default_host;

static void __nvme_free_host(nvme_host_t h);
Expand Down Expand Up @@ -822,6 +824,11 @@ const char *nvme_ctrl_get_address(nvme_ctrl_t c)
return c->address ? c->address : "";
}

const char *nvme_ctrl_get_phy_slot(nvme_ctrl_t c)
{
return c->phy_slot ? c->phy_slot : "";
}

const char *nvme_ctrl_get_firmware(nvme_ctrl_t c)
{
return c->firmware;
Expand Down Expand Up @@ -1009,6 +1016,7 @@ void nvme_deconfigure_ctrl(nvme_ctrl_t c)
FREE_CTRL_ATTR(c->address);
FREE_CTRL_ATTR(c->dctype);
FREE_CTRL_ATTR(c->cntrltype);
FREE_CTRL_ATTR(c->phy_slot);
}

int nvme_disconnect_ctrl(nvme_ctrl_t c)
Expand Down Expand Up @@ -1256,6 +1264,50 @@ static char *nvme_ctrl_lookup_subsystem_name(nvme_root_t r,
return subsys_name;
}

static char *nvme_ctrl_lookup_phy_slot(nvme_root_t r, const char *address)
{
char *target_addr;
char *addr;
char *path;
int found = 0;
int ret;
DIR *slots_dir;
struct dirent *entry;

slots_dir = opendir(nvme_slots_sysfs_dir);
if (!slots_dir) {
nvme_msg(r, LOG_WARNING, "failed to open slots dir %s\n",
nvme_slots_sysfs_dir);
return NULL;
}

target_addr = strndup(address, 10);
while (!(entry = readdir(slots_dir))) {
if (entry->d_type == DT_DIR &&
strncmp(entry->d_name, ".", 1) != 0 &&
strncmp(entry->d_name, "..", 2) != 0) {
ret = asprintf(&path, "/sys/bus/pci/slots/%s", entry->d_name);
if (ret < 0) {
errno = ENOMEM;
return NULL;
}
addr = nvme_get_attr(path, "address");
if (strcmp(addr, target_addr) == 0) {
found = 1;
free(path);
free(addr);
break;
}
free(path);
free(addr);
}
}
free(target_addr);
if (found)
return strdup(entry->d_name);
return NULL;
}

static int nvme_configure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path,
const char *name)
{
Expand Down Expand Up @@ -1297,6 +1349,7 @@ static int nvme_configure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path,
}
c->cntrltype = nvme_get_ctrl_attr(c, "cntrltype");
c->dctype = nvme_get_ctrl_attr(c, "dctype");
c->phy_slot = nvme_ctrl_lookup_phy_slot(r, c->address);

errno = 0; /* cleanup after nvme_get_ctrl_attr() */
return 0;
Expand Down
9 changes: 9 additions & 0 deletions src/nvme/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,15 @@ const char *nvme_ctrl_get_sysfs_dir(nvme_ctrl_t c);
*/
const char *nvme_ctrl_get_address(nvme_ctrl_t c);

/**
* nvme_ctrl_get_phy_slot() - PCI physical slot number of a controller
* @c: Controller instance
*
* Return: PCI physical slot number of @c or empty string if slot
* number is not present.
*/
const char *nvme_ctrl_get_phy_slot(nvme_ctrl_t c);

/**
* nvme_ctrl_get_firmware() - Firmware string of a controller
* @c: Controller instance
Expand Down

0 comments on commit 42ac453

Please sign in to comment.