All of lore.kernel.org
 help / color / mirror / Atom feed
From: Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
To: linux-nvme@lists.infradead.org
Cc: kbusch@kernel.org, hch@lst.de, sagi@grimberg.me,
	Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
Subject: [PATCH 2/4] nvme-core: add a helper to get the len from nid
Date: Mon, 17 May 2021 22:05:55 -0700	[thread overview]
Message-ID: <20210518050557.14961-3-chaitanya.kulkarni@wdc.com> (raw)
In-Reply-To: <20210518050557.14961-1-chaitanya.kulkarni@wdc.com>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=y, Size: 3583 bytes --]

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@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



[-- Attachment #2: Type: text/plain, Size: 158 bytes --]

_______________________________________________
Linux-nvme mailing list
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

  parent reply	other threads:[~2021-05-18  5:06 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-18  5:05 [PATCH 0/4] nvme: nidt and ns-desc related clanup Chaitanya Kulkarni
2021-05-18  5:05 ` [PATCH 1/4] nvme: calculate data offset once for ns-desc Chaitanya Kulkarni
2021-05-18  5:05 ` Chaitanya Kulkarni [this message]
2021-05-18  5:05 ` [PATCH 3/4] nvmet: use nvme_nidt_len() helper for id-desc Chaitanya Kulkarni
2021-05-18  5:05 ` [PATCH 4/4] nvme-core: remove the NVME_NIDT_XXX_LEN macros Chaitanya Kulkarni
2021-05-18  6:49 ` [PATCH 0/4] nvme: nidt and ns-desc related clanup Christoph Hellwig
2021-05-18 20:29   ` Chaitanya Kulkarni

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210518050557.14961-3-chaitanya.kulkarni@wdc.com \
    --to=chaitanya.kulkarni@wdc.com \
    --cc=hch@lst.de \
    --cc=kbusch@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sagi@grimberg.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.