Linux Test Project
 help / color / mirror / Atom feed
From: priyama2@linux.ibm.com
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v2] testcases/nvme: Add NVMe device discovery and identification test
Date: Thu, 30 Apr 2026 15:52:41 +0000	[thread overview]
Message-ID: <20260430155241.897024-1-priyama2@linux.ibm.com> (raw)
In-Reply-To: <20260428075302.7320ef7e3@imap1.dmz-prg2.suse.org>

From: priyama2 <priyama2@linux.ibm.com>

This test verifies NVMe device discovery and identification by:
- Detecting NVMe controllers in /dev
- Enumerating namespaces for detected controllers
- Verifying PCI enumeration (class code 0x010802)
- Checking NVMe driver binding

Changes in v2:
- Use LTP buildsystem instead of standalone Makefile
- Remove README file (use doc comments instead)
- Fix author attribution
- Add proper LTP documentation comment format
- Use SAFE_OPENDIR() and SAFE_CLOSEDIR() macros
- Remove obvious and redundant comments
- Code cleanup and style improvements

Signed-off-by: priyama2 <priyama2@linux.ibm.com>
---
 testcases/kernel/device-drivers/nvme/Makefile |   7 +
 testcases/kernel/device-drivers/nvme/nvme01.c | 208 ++++++++++++++++++
 2 files changed, 215 insertions(+)
 create mode 100644 testcases/kernel/device-drivers/nvme/Makefile
 create mode 100644 testcases/kernel/device-drivers/nvme/nvme01.c

diff --git a/testcases/kernel/device-drivers/nvme/Makefile b/testcases/kernel/device-drivers/nvme/Makefile
new file mode 100644
index 000000000..d47e99e6a
--- /dev/null
+++ b/testcases/kernel/device-drivers/nvme/Makefile
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2024 IBM Corporation
+
+top_srcdir	?= ../../../..
+
+include $(top_srcdir)/include/mk/env_pre.mk
+include $(top_srcdir)/include/mk/generic_trunk_target.mk
diff --git a/testcases/kernel/device-drivers/nvme/nvme01.c b/testcases/kernel/device-drivers/nvme/nvme01.c
new file mode 100644
index 000000000..f448e7e5c
--- /dev/null
+++ b/testcases/kernel/device-drivers/nvme/nvme01.c
@@ -0,0 +1,208 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 IBM Corporation
+ * Author: Priya Gopinath <priyama2@linux.ibm.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify NVMe device discovery and identification:
+ *
+ * - Detect NVMe controllers in /dev
+ * - Enumerate namespaces for detected controllers
+ * - Verify PCI enumeration (class code 0x010802)
+ * - Check NVMe driver binding
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>
+#include <errno.h>
+
+#include "tst_test.h"
+
+#define NVME_DEV_PATH "/dev"
+#define NVME_SYS_PATH "/sys/class/nvme"
+#define PCI_DEVICES_PATH "/sys/bus/pci/devices"
+
+static int nvme_device_found;
+static char nvme_dev_name[256];
+
+static void test_detect_nvme_controllers(void)
+{
+	DIR *dir;
+	struct dirent *entry;
+	int found = 0;
+
+	dir = SAFE_OPENDIR(NVME_DEV_PATH);
+
+	tst_res(TINFO, "Scanning for NVMe devices in %s", NVME_DEV_PATH);
+
+	while ((entry = readdir(dir)) != NULL) {
+		if (strncmp(entry->d_name, "nvme", 4) == 0 &&
+		    strlen(entry->d_name) == 5) {
+			tst_res(TINFO, "Found NVMe controller: %s", entry->d_name);
+			strncpy(nvme_dev_name, entry->d_name, sizeof(nvme_dev_name) - 1);
+			found = 1;
+			nvme_device_found = 1;
+		}
+	}
+
+	SAFE_CLOSEDIR(dir);
+
+	if (found)
+		tst_res(TPASS, "NVMe controller(s) detected successfully");
+	else
+		tst_res(TCONF, "No NVMe controllers found in system");
+}
+
+static void test_enumerate_namespaces(void)
+{
+	DIR *dir;
+	struct dirent *entry;
+	int ns_count = 0;
+	char search_pattern[32];
+
+	if (!nvme_device_found) {
+		tst_res(TCONF, "No NVMe device found, skipping namespace enumeration");
+		return;
+	}
+
+	snprintf(search_pattern, sizeof(search_pattern), "%sn", nvme_dev_name);
+
+	dir = SAFE_OPENDIR(NVME_DEV_PATH);
+
+	tst_res(TINFO, "Enumerating namespaces for %s", nvme_dev_name);
+
+	while ((entry = readdir(dir)) != NULL) {
+		if (strncmp(entry->d_name, search_pattern, strlen(search_pattern)) == 0) {
+			tst_res(TINFO, "Found namespace: %s", entry->d_name);
+			ns_count++;
+		}
+	}
+
+	SAFE_CLOSEDIR(dir);
+
+	if (ns_count > 0)
+		tst_res(TPASS, "Enumerated %d namespace(s) successfully", ns_count);
+	else
+		tst_res(TPASS, "No namespaces found for %s (device may not have namespaces configured)", nvme_dev_name);
+}
+
+static void test_verify_pci_enumeration(void)
+{
+	DIR *dir;
+	struct dirent *entry;
+	int found = 0;
+	char class_path[512];
+	FILE *fp;
+	char class_id[16];
+
+	if (!nvme_device_found) {
+		tst_res(TCONF, "No NVMe device found, skipping PCI enumeration check");
+		return;
+	}
+
+	dir = SAFE_OPENDIR(PCI_DEVICES_PATH);
+
+	tst_res(TINFO, "Checking PCI enumeration for NVMe devices");
+
+	while ((entry = readdir(dir)) != NULL) {
+		if (entry->d_name[0] == '.')
+			continue;
+
+		snprintf(class_path, sizeof(class_path), "%s/%s/class",
+			 PCI_DEVICES_PATH, entry->d_name);
+
+		fp = fopen(class_path, "r");
+		if (!fp)
+			continue;
+
+		if (fgets(class_id, sizeof(class_id), fp)) {
+			if (strncmp(class_id, "0x010802", 8) == 0) {
+				tst_res(TINFO, "Found NVMe PCI device: %s (class: %s)",
+					entry->d_name, class_id);
+				found = 1;
+			}
+		}
+		fclose(fp);
+	}
+
+	SAFE_CLOSEDIR(dir);
+
+	if (found)
+		tst_res(TPASS, "NVMe device properly enumerated on PCI bus");
+	else
+		tst_res(TFAIL, "NVMe device not found on PCI bus");
+}
+
+static void test_check_driver_binding(void)
+{
+	char driver_path[512];
+	char driver_link[512];
+	ssize_t len;
+
+	if (!nvme_device_found) {
+		tst_res(TCONF, "No NVMe device found, skipping driver binding check");
+		return;
+	}
+
+	snprintf(driver_path, sizeof(driver_path), "%s/%s/device/driver",
+		 NVME_SYS_PATH, nvme_dev_name);
+
+	len = readlink(driver_path, driver_link, sizeof(driver_link) - 1);
+	if (len == -1) {
+		tst_res(TFAIL | TERRNO, "Failed to read driver symlink for %s", nvme_dev_name);
+		return;
+	}
+
+	driver_link[len] = '\0';
+
+	tst_res(TINFO, "Driver binding: %s", driver_link);
+
+	if (strstr(driver_link, "nvme") != NULL)
+		tst_res(TPASS, "NVMe driver properly bound to device");
+	else
+		tst_res(TFAIL, "Unexpected driver bound to NVMe device: %s", driver_link);
+}
+
+static void setup(void)
+{
+	/* Check if nvme module is loaded */
+	if (access("/sys/module/nvme", F_OK) != 0)
+		tst_brk(TCONF, "NVMe kernel module not loaded");
+
+	tst_res(TINFO, "NVMe Device Discovery & Identification Test");
+}
+
+static void run(unsigned int n)
+{
+	switch (n) {
+	case 0:
+		test_detect_nvme_controllers();
+		break;
+	case 1:
+		test_enumerate_namespaces();
+		break;
+	case 2:
+		test_verify_pci_enumeration();
+		break;
+	case 3:
+		test_check_driver_binding();
+		break;
+	}
+}
+
+static struct tst_test test = {
+	.test = run,
+	.tcnt = 4,
+	.setup = setup,
+	.needs_root = 1,
+};
-- 
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

       reply	other threads:[~2026-04-29  5:49 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20260428075302.7320ef7e3@imap1.dmz-prg2.suse.org>
2026-04-30 15:52 ` priyama2 [this message]
2026-04-29  7:33   ` [LTP] testcases/nvme: Add NVMe device discovery and identification test linuxtestproject.agent
2026-05-01 15:14 ` [LTP] [PATCH v3] testcases/kernel/device-drivers/nvme: Add NVMe device discovery test priyama2
2026-04-30  6:35   ` [LTP] " linuxtestproject.agent
2026-05-04 10:40   ` [LTP] [PATCH v3] " Andrea Cervesato via ltp
2026-05-05  3:46   ` Sebastian Chlad
2026-05-05  8:45     ` Daniel Wagner
2026-05-05 13:26       ` Daniel Wagner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260430155241.897024-1-priyama2@linux.ibm.com \
    --to=priyama2@linux.ibm.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox