Skip to content

Commit 42ac453

Browse files
Umer Saleemigaw
authored andcommitted
tree: Add PCI physical slot number for controller
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]>
1 parent 110339e commit 42ac453

4 files changed

Lines changed: 64 additions & 0 deletions

File tree

src/libnvme.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
LIBNVME_1_5 {
44
global:
5+
nvme_ctrl_get_phy_slot;
56
nvme_ipaddrs_eq;
67
nvme_nbft_free;
78
nvme_nbft_read;

src/nvme/private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ struct nvme_ctrl {
8484
char *dhchap_ctrl_key;
8585
char *cntrltype;
8686
char *dctype;
87+
char *phy_slot;
8788
bool discovery_ctrl;
8889
bool unique_discovery_ctrl;
8990
bool discovered;

src/nvme/tree.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include "log.h"
3535
#include "private.h"
3636

37+
const char *nvme_slots_sysfs_dir = "/sys/bus/pci/slots";
38+
3739
static struct nvme_host *default_host;
3840

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

827+
const char *nvme_ctrl_get_phy_slot(nvme_ctrl_t c)
828+
{
829+
return c->phy_slot ? c->phy_slot : "";
830+
}
831+
825832
const char *nvme_ctrl_get_firmware(nvme_ctrl_t c)
826833
{
827834
return c->firmware;
@@ -1009,6 +1016,7 @@ void nvme_deconfigure_ctrl(nvme_ctrl_t c)
10091016
FREE_CTRL_ATTR(c->address);
10101017
FREE_CTRL_ATTR(c->dctype);
10111018
FREE_CTRL_ATTR(c->cntrltype);
1019+
FREE_CTRL_ATTR(c->phy_slot);
10121020
}
10131021

10141022
int nvme_disconnect_ctrl(nvme_ctrl_t c)
@@ -1256,6 +1264,50 @@ static char *nvme_ctrl_lookup_subsystem_name(nvme_root_t r,
12561264
return subsys_name;
12571265
}
12581266

1267+
static char *nvme_ctrl_lookup_phy_slot(nvme_root_t r, const char *address)
1268+
{
1269+
char *target_addr;
1270+
char *addr;
1271+
char *path;
1272+
int found = 0;
1273+
int ret;
1274+
DIR *slots_dir;
1275+
struct dirent *entry;
1276+
1277+
slots_dir = opendir(nvme_slots_sysfs_dir);
1278+
if (!slots_dir) {
1279+
nvme_msg(r, LOG_WARNING, "failed to open slots dir %s\n",
1280+
nvme_slots_sysfs_dir);
1281+
return NULL;
1282+
}
1283+
1284+
target_addr = strndup(address, 10);
1285+
while (!(entry = readdir(slots_dir))) {
1286+
if (entry->d_type == DT_DIR &&
1287+
strncmp(entry->d_name, ".", 1) != 0 &&
1288+
strncmp(entry->d_name, "..", 2) != 0) {
1289+
ret = asprintf(&path, "/sys/bus/pci/slots/%s", entry->d_name);
1290+
if (ret < 0) {
1291+
errno = ENOMEM;
1292+
return NULL;
1293+
}
1294+
addr = nvme_get_attr(path, "address");
1295+
if (strcmp(addr, target_addr) == 0) {
1296+
found = 1;
1297+
free(path);
1298+
free(addr);
1299+
break;
1300+
}
1301+
free(path);
1302+
free(addr);
1303+
}
1304+
}
1305+
free(target_addr);
1306+
if (found)
1307+
return strdup(entry->d_name);
1308+
return NULL;
1309+
}
1310+
12591311
static int nvme_configure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path,
12601312
const char *name)
12611313
{
@@ -1297,6 +1349,7 @@ static int nvme_configure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path,
12971349
}
12981350
c->cntrltype = nvme_get_ctrl_attr(c, "cntrltype");
12991351
c->dctype = nvme_get_ctrl_attr(c, "dctype");
1352+
c->phy_slot = nvme_ctrl_lookup_phy_slot(r, c->address);
13001353

13011354
errno = 0; /* cleanup after nvme_get_ctrl_attr() */
13021355
return 0;

src/nvme/tree.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,15 @@ const char *nvme_ctrl_get_sysfs_dir(nvme_ctrl_t c);
801801
*/
802802
const char *nvme_ctrl_get_address(nvme_ctrl_t c);
803803

804+
/**
805+
* nvme_ctrl_get_phy_slot() - PCI physical slot number of a controller
806+
* @c: Controller instance
807+
*
808+
* Return: PCI physical slot number of @c or empty string if slot
809+
* number is not present.
810+
*/
811+
const char *nvme_ctrl_get_phy_slot(nvme_ctrl_t c);
812+
804813
/**
805814
* nvme_ctrl_get_firmware() - Firmware string of a controller
806815
* @c: Controller instance

0 commit comments

Comments
 (0)