Blame SOURCES/0096-cxl-list-Add-filter-by-serial-support.patch

e0018b
From d7854adcd1e517d2372ec51f4a1ede2d549975e8 Mon Sep 17 00:00:00 2001
e0018b
From: Dan Williams <dan.j.williams@intel.com>
e0018b
Date: Sun, 23 Jan 2022 16:52:41 -0800
e0018b
Subject: [PATCH 096/217] cxl/list: Add filter by serial support
e0018b
e0018b
Given that serial numbers are intended to be unique device identifiers,
e0018b
enable them as a memdev filter option.
e0018b
e0018b
Link: https://lore.kernel.org/r/164298556167.3021641.5470955268978068465.stgit@dwillia2-desk3.amr.corp.intel.com
e0018b
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
e0018b
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
e0018b
---
e0018b
 Documentation/cxl/cxl-list.txt |  4 ++++
e0018b
 cxl/filter.c                   | 38 ++++++++++++++++++++++++++++++----
e0018b
 cxl/filter.h                   |  4 +++-
e0018b
 cxl/list.c                     |  4 +++-
e0018b
 cxl/memdev.c                   |  2 +-
e0018b
 5 files changed, 45 insertions(+), 7 deletions(-)
e0018b
e0018b
diff --git a/Documentation/cxl/cxl-list.txt b/Documentation/cxl/cxl-list.txt
e0018b
index bd0207e..224c972 100644
e0018b
--- a/Documentation/cxl/cxl-list.txt
e0018b
+++ b/Documentation/cxl/cxl-list.txt
e0018b
@@ -64,6 +64,10 @@ OPTIONS
e0018b
 ]
e0018b
 ----
e0018b
 
e0018b
+-s::
e0018b
+--serial=::
e0018b
+	Specify CXL memory device serial number(s) to filter the listing
e0018b
+
e0018b
 -M::
e0018b
 --memdevs::
e0018b
 	Include CXL memory devices in the listing
e0018b
diff --git a/cxl/filter.c b/cxl/filter.c
e0018b
index d1ff4b6..26efc65 100644
e0018b
--- a/cxl/filter.c
e0018b
+++ b/cxl/filter.c
e0018b
@@ -21,15 +21,45 @@ static const char *which_sep(const char *filter)
e0018b
 	return " ";
e0018b
 }
e0018b
 
e0018b
+static struct cxl_memdev *
e0018b
+util_cxl_memdev_serial_filter(struct cxl_memdev *memdev, const char *__serials)
e0018b
+{
e0018b
+	unsigned long long serial = 0;
e0018b
+	char *serials, *save, *end;
e0018b
+	const char *arg;
e0018b
+
e0018b
+	if (!__serials)
e0018b
+		return memdev;
e0018b
+
e0018b
+	serials = strdup(__serials);
e0018b
+	if (!serials)
e0018b
+		return NULL;
e0018b
+
e0018b
+	for (arg = strtok_r(serials, which_sep(__serials), &save); arg;
e0018b
+	     arg = strtok_r(NULL, which_sep(__serials), &save)) {
e0018b
+		serial = strtoull(arg, &end, 0);
e0018b
+		if (!arg[0] || end[0] != 0)
e0018b
+			continue;
e0018b
+		if (cxl_memdev_get_serial(memdev) == serial)
e0018b
+			break;
e0018b
+	}
e0018b
+
e0018b
+	free(serials);
e0018b
+	if (arg)
e0018b
+		return memdev;
e0018b
+	return NULL;
e0018b
+}
e0018b
+
e0018b
 struct cxl_memdev *util_cxl_memdev_filter(struct cxl_memdev *memdev,
e0018b
-					  const char *__ident)
e0018b
+					  const char *__ident,
e0018b
+					  const char *serials)
e0018b
 {
e0018b
 	char *ident, *save;
e0018b
 	const char *name;
e0018b
 	int memdev_id;
e0018b
 
e0018b
 	if (!__ident)
e0018b
-		return memdev;
e0018b
+		return util_cxl_memdev_serial_filter(memdev, serials);
e0018b
 
e0018b
 	ident = strdup(__ident);
e0018b
 	if (!ident)
e0018b
@@ -51,7 +81,7 @@ struct cxl_memdev *util_cxl_memdev_filter(struct cxl_memdev *memdev,
e0018b
 
e0018b
 	free(ident);
e0018b
 	if (name)
e0018b
-		return memdev;
e0018b
+		return util_cxl_memdev_serial_filter(memdev, serials);
e0018b
 	return NULL;
e0018b
 }
e0018b
 
e0018b
@@ -82,7 +112,7 @@ int cxl_filter_walk(struct cxl_ctx *ctx, struct cxl_filter_params *p)
e0018b
 	cxl_memdev_foreach(ctx, memdev) {
e0018b
 		struct json_object *jdev;
e0018b
 
e0018b
-		if (!util_cxl_memdev_filter(memdev, p->memdev_filter))
e0018b
+		if (!util_cxl_memdev_filter(memdev, p->memdev_filter, p->serial_filter))
e0018b
 			continue;
e0018b
 		if (p->memdevs) {
e0018b
 			jdev = util_cxl_memdev_to_json(memdev, flags);
e0018b
diff --git a/cxl/filter.h b/cxl/filter.h
e0018b
index 664b74b..12d9344 100644
e0018b
--- a/cxl/filter.h
e0018b
+++ b/cxl/filter.h
e0018b
@@ -8,6 +8,7 @@
e0018b
 
e0018b
 struct cxl_filter_params {
e0018b
 	const char *memdev_filter;
e0018b
+	const char *serial_filter;
e0018b
 	bool memdevs;
e0018b
 	bool idle;
e0018b
 	bool human;
e0018b
@@ -16,6 +17,7 @@ struct cxl_filter_params {
e0018b
 };
e0018b
 
e0018b
 struct cxl_memdev *util_cxl_memdev_filter(struct cxl_memdev *memdev,
e0018b
-					  const char *ident);
e0018b
+					  const char *__ident,
e0018b
+					  const char *serials);
e0018b
 int cxl_filter_walk(struct cxl_ctx *ctx, struct cxl_filter_params *param);
e0018b
 #endif /* _CXL_UTIL_FILTER_H_ */
e0018b
diff --git a/cxl/list.c b/cxl/list.c
e0018b
index 1730307..6bc48df 100644
e0018b
--- a/cxl/list.c
e0018b
+++ b/cxl/list.c
e0018b
@@ -24,6 +24,8 @@ int cmd_list(int argc, const char **argv, struct cxl_ctx *ctx)
e0018b
 	const struct option options[] = {
e0018b
 		OPT_STRING('m', "memdev", &param.memdev_filter, "memory device name",
e0018b
 			   "filter by CXL memory device name"),
e0018b
+		OPT_STRING('s', "serial", &param.serial_filter, "memory device serial",
e0018b
+			   "filter by CXL memory device serial number"),
e0018b
 		OPT_BOOLEAN('M', "memdevs", &param.memdevs,
e0018b
 			    "include CXL memory device info"),
e0018b
 		OPT_BOOLEAN('i', "idle", &param.idle, "include disabled devices"),
e0018b
@@ -47,7 +49,7 @@ int cmd_list(int argc, const char **argv, struct cxl_ctx *ctx)
e0018b
 		usage_with_options(u, options);
e0018b
 
e0018b
 	if (num_list_flags() == 0) {
e0018b
-		if (param.memdev_filter)
e0018b
+		if (param.memdev_filter || param.serial_filter)
e0018b
 			param.memdevs = true;
e0018b
 		else {
e0018b
 			/*
e0018b
diff --git a/cxl/memdev.c b/cxl/memdev.c
e0018b
index d063d51..b9141be 100644
e0018b
--- a/cxl/memdev.c
e0018b
+++ b/cxl/memdev.c
e0018b
@@ -248,7 +248,7 @@ static int memdev_action(int argc, const char **argv, struct cxl_ctx *ctx,
e0018b
 			continue;
e0018b
 
e0018b
 		cxl_memdev_foreach (ctx, memdev) {
e0018b
-			if (!util_cxl_memdev_filter(memdev, argv[i]))
e0018b
+			if (!util_cxl_memdev_filter(memdev, argv[i], NULL))
e0018b
 				continue;
e0018b
 
e0018b
 			if (action == action_write) {
e0018b
-- 
e0018b
2.27.0
e0018b