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 <[email protected]>
Signed-off-by: Vishal Verma <[email protected]>
---
 cxl/libcxl.h | 13 +++++++++++++
 cxl/memdev.c | 11 ++---------
 2 files changed, 15 insertions(+), 9 deletions(-)

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;
-- 
2.37.1


Reply via email to