diff mbox series

[ndctl,v3,03/11] cxl/memdev: refactor decoder mode string parsing

Message ID 20220815192214.545800-4-vishal.l.verma@intel.com (mailing list archive)
State Accepted
Commit 14565442634cfab0aac8823129a175be572fb11e
Headers show
Series cxl: add region management | expand

Commit Message

Verma, Vishal L Aug. 15, 2022, 7:22 p.m. UTC
In preparation for create-region to use a similar decoder mode string
to enum operation, break out the mode string parsing into its own inline
helper in libcxl.h, and call it from memdev.c:__reserve_dpa().

Cc: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
---
 cxl/libcxl.h | 13 +++++++++++++
 cxl/memdev.c | 11 ++---------
 2 files changed, 15 insertions(+), 9 deletions(-)

Comments

Dan Williams Aug. 15, 2022, 10:57 p.m. UTC | #1
Vishal Verma wrote:
> In preparation for create-region to use a similar decoder mode string
> to enum operation, break out the mode string parsing into its own inline
> helper in libcxl.h, and call it from memdev.c:__reserve_dpa().
> 
> Cc: Dan Williams <dan.j.williams@intel.com>
> Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>

LGTM

Reviewed-by: Dan Williams <dan.j.williams@intel.com>
diff mbox series

Patch

diff --git a/cxl/libcxl.h b/cxl/libcxl.h
index 33a216e..c1f8d14 100644
--- a/cxl/libcxl.h
+++ b/cxl/libcxl.h
@@ -4,6 +4,7 @@ 
 #define _LIBCXL_H_
 
 #include <stdarg.h>
+#include <string.h>
 #include <unistd.h>
 #include <stdbool.h>
 
@@ -154,6 +155,18 @@  static inline const char *cxl_decoder_mode_name(enum cxl_decoder_mode mode)
 	return names[mode];
 }
 
+static inline enum cxl_decoder_mode
+cxl_decoder_mode_from_ident(const char *ident)
+{
+	if (strcmp(ident, "ram") == 0)
+		return CXL_DECODER_MODE_RAM;
+	else if (strcmp(ident, "volatile") == 0)
+		return CXL_DECODER_MODE_RAM;
+	else if (strcmp(ident, "pmem") == 0)
+		return CXL_DECODER_MODE_PMEM;
+	return CXL_DECODER_MODE_NONE;
+}
+
 enum cxl_decoder_mode cxl_decoder_get_mode(struct cxl_decoder *decoder);
 int cxl_decoder_set_mode(struct cxl_decoder *decoder,
 			 enum cxl_decoder_mode mode);
diff --git a/cxl/memdev.c b/cxl/memdev.c
index e42f554..0b3ad02 100644
--- a/cxl/memdev.c
+++ b/cxl/memdev.c
@@ -154,15 +154,8 @@  static int __reserve_dpa(struct cxl_memdev *memdev,
 	int rc;
 
 	if (param.type) {
-		if (strcmp(param.type, "ram") == 0)
-			mode = CXL_DECODER_MODE_RAM;
-		else if (strcmp(param.type, "volatile") == 0)
-			mode = CXL_DECODER_MODE_RAM;
-		else if (strcmp(param.type, "ram") == 0)
-			mode = CXL_DECODER_MODE_RAM;
-		else if (strcmp(param.type, "pmem") == 0)
-			mode = CXL_DECODER_MODE_PMEM;
-		else {
+		mode = cxl_decoder_mode_from_ident(param.type);
+		if (mode == CXL_DECODER_MODE_NONE) {
 			log_err(&ml, "%s: unsupported type: %s\n", devname,
 				param.type);
 			return -EINVAL;