[PATCH 2/4] nvme-core: add a helper to get the len from nid

Chaitanya Kulkarni chaitanya.kulkarni at wdc.com
Mon May 17 22:05:55 PDT 2021


In the current code we maintain NVME_NIDT_XXX identifier values and the 
their length NVME_NIDT_XXX_LEN separately which has 1:1 mapping. This
adds four extra macros and open coding of such macros in the host and 
the target.                                                                                            

With this open coding in future we will have to maintain one extra macro
representing length when we add a new NIDT identifier(s).

In this preparation patch add a helper function and use it so that it
can now remove the open coding of the NVME_NIDT_XXX_LEN for host.

Signed-off-by: Chaitanya Kulkarni <chaitanya.kulkarni at wdc.com>
---
 drivers/nvme/host/core.c | 22 +++++++++++-----------
 include/linux/nvme.h     | 16 ++++++++++++++++
 2 files changed, 27 insertions(+), 11 deletions(-)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index f8a3a36eba86..aac33f575f19 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -1306,38 +1306,38 @@ static int nvme_process_ns_desc(struct nvme_ctrl *ctrl, struct nvme_ns_ids *ids,
 
 	switch (cur->nidt) {
 	case NVME_NIDT_EUI64:
-		if (cur->nidl != NVME_NIDT_EUI64_LEN) {
+		if (cur->nidl != nvme_nidt_len(NVME_NIDT_EUI64)) {
 			dev_warn(ctrl->device, "%s %d for NVME_NIDT_EUI64\n",
 				 warn_str, cur->nidl);
 			return -1;
 		}
-		memcpy(ids->eui64, buf, NVME_NIDT_EUI64_LEN);
-		return NVME_NIDT_EUI64_LEN;
+		memcpy(ids->eui64, buf, nvme_nidt_len(NVME_NIDT_EUI64));
+		return nvme_nidt_len(NVME_NIDT_EUI64);
 	case NVME_NIDT_NGUID:
-		if (cur->nidl != NVME_NIDT_NGUID_LEN) {
+		if (cur->nidl != nvme_nidt_len(NVME_NIDT_NGUID)) {
 			dev_warn(ctrl->device, "%s %d for NVME_NIDT_NGUID\n",
 				 warn_str, cur->nidl);
 			return -1;
 		}
-		memcpy(ids->nguid, buf, NVME_NIDT_NGUID_LEN);
-		return NVME_NIDT_NGUID_LEN;
+		memcpy(ids->nguid, buf, nvme_nidt_len(NVME_NIDT_NGUID));
+		return nvme_nidt_len(NVME_NIDT_NGUID);
 	case NVME_NIDT_UUID:
-		if (cur->nidl != NVME_NIDT_UUID_LEN) {
+		if (cur->nidl != nvme_nidt_len(NVME_NIDT_UUID)) {
 			dev_warn(ctrl->device, "%s %d for NVME_NIDT_UUID\n",
 				 warn_str, cur->nidl);
 			return -1;
 		}
 		uuid_copy(&ids->uuid, buf);
-		return NVME_NIDT_UUID_LEN;
+		return nvme_nidt_len(NVME_NIDT_UUID);
 	case NVME_NIDT_CSI:
-		if (cur->nidl != NVME_NIDT_CSI_LEN) {
+		if (cur->nidl != nvme_nidt_len(NVME_NIDT_CSI)) {
 			dev_warn(ctrl->device, "%s %d for NVME_NIDT_CSI\n",
 				 warn_str, cur->nidl);
 			return -1;
 		}
-		memcpy(&ids->csi, buf, NVME_NIDT_CSI_LEN);
+		memcpy(&ids->csi, buf, nvme_nidt_len(NVME_NIDT_CSI));
 		*csi_seen = true;
-		return NVME_NIDT_CSI_LEN;
+		return nvme_nidt_len(NVME_NIDT_CSI);
 	default:
 		/* Skip unknown types */
 		return cur->nidl;
diff --git a/include/linux/nvme.h b/include/linux/nvme.h
index edcbd60b88b9..5d6b0fe64db1 100644
--- a/include/linux/nvme.h
+++ b/include/linux/nvme.h
@@ -9,6 +9,7 @@
 
 #include <linux/types.h>
 #include <linux/uuid.h>
+#include <linux/kernel.h>
 
 /* NQN names in commands fields specified one size */
 #define NVMF_NQN_FIELD_LEN	256
@@ -497,6 +498,21 @@ enum {
 	NVME_NIDT_CSI		= 0x04,
 };
 
+static inline unsigned int nvme_nidt_len(unsigned int id)
+{
+	static int nvme_nidt_len_map[] = {
+		[NVME_NIDT_EUI64]	= 8,
+		[NVME_NIDT_NGUID]	= 16,
+		[NVME_NIDT_UUID]	= 16,
+		[NVME_NIDT_CSI]		= 1,
+	};
+
+	if (id > ARRAY_SIZE(nvme_nidt_len_map) || !nvme_nidt_len_map[id])
+		return 0;
+
+	return nvme_nidt_len_map[id];
+}
+
 struct nvme_smart_log {
 	__u8			critical_warning;
 	__u8			temperature[2];
-- 
2.22.1




More information about the Linux-nvme mailing list