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