Blame 0005-nvme-Fix-namespace-identifiers.patch

Vojtech Trefny 97cfd5
From b25fd9caca9b2fb34e5a4d7d4bee0031e4758d0a Mon Sep 17 00:00:00 2001
Vojtech Trefny 97cfd5
From: Tomas Bzatek <tbzatek@redhat.com>
Vojtech Trefny 97cfd5
Date: Thu, 22 Sep 2022 16:31:28 +0200
Vojtech Trefny 97cfd5
Subject: [PATCH 1/5] nvme: Avoid sending NVME_IDENTIFY_CNS_NS_DESC_LIST on
Vojtech Trefny 97cfd5
 older devices
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
Turned out this Identify feature was introduced only with the NVMe 1.3
Vojtech Trefny 97cfd5
specification. To find out device supported NVMe revision an extra
Vojtech Trefny 97cfd5
Identify Controller call is needed.
Vojtech Trefny 97cfd5
---
Vojtech Trefny 97cfd5
 src/plugins/nvme/nvme-info.c | 14 +++++++++++---
Vojtech Trefny 97cfd5
 1 file changed, 11 insertions(+), 3 deletions(-)
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
diff --git a/src/plugins/nvme/nvme-info.c b/src/plugins/nvme/nvme-info.c
Vojtech Trefny 97cfd5
index fdd90459..112b4054 100644
Vojtech Trefny 97cfd5
--- a/src/plugins/nvme/nvme-info.c
Vojtech Trefny 97cfd5
+++ b/src/plugins/nvme/nvme-info.c
Vojtech Trefny 97cfd5
@@ -431,7 +431,7 @@ BDNVMEControllerInfo * bd_nvme_get_controller_info (const gchar *device, GError
Vojtech Trefny 97cfd5
     if (fd < 0)
Vojtech Trefny 97cfd5
         return NULL;
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
-    /* send the NVME_IDENTIFY_CNS_NS + NVME_IDENTIFY_CNS_CTRL ioctl */
Vojtech Trefny 97cfd5
+    /* send the NVME_IDENTIFY_CNS_CTRL ioctl */
Vojtech Trefny 97cfd5
     ret = nvme_identify_ctrl (fd, &ctrl_id);
Vojtech Trefny 97cfd5
     if (ret != 0) {
Vojtech Trefny 97cfd5
         _nvme_status_to_error (ret, FALSE, error);
Vojtech Trefny 97cfd5
@@ -539,9 +539,11 @@ BDNVMEControllerInfo * bd_nvme_get_controller_info (const gchar *device, GError
Vojtech Trefny 97cfd5
  */
Vojtech Trefny 97cfd5
 BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **error) {
Vojtech Trefny 97cfd5
     int ret;
Vojtech Trefny 97cfd5
-    int ret_desc;
Vojtech Trefny 97cfd5
+    int ret_ctrl;
Vojtech Trefny 97cfd5
+    int ret_desc = -1;
Vojtech Trefny 97cfd5
     int fd;
Vojtech Trefny 97cfd5
     __u32 nsid = 0;
Vojtech Trefny 97cfd5
+    struct nvme_id_ctrl ctrl_id = ZERO_INIT;
Vojtech Trefny 97cfd5
     struct nvme_id_ns ns_info = ZERO_INIT;
Vojtech Trefny 97cfd5
     uint8_t desc[NVME_IDENTIFY_DATA_SIZE] = ZERO_INIT;
Vojtech Trefny 97cfd5
     guint8 flbas;
Vojtech Trefny 97cfd5
@@ -565,7 +567,6 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
Vojtech Trefny 97cfd5
     }
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
     /* send the NVME_IDENTIFY_CNS_NS ioctl */
Vojtech Trefny 97cfd5
-    ret_desc = nvme_identify_ns_descs (fd, nsid, (struct nvme_ns_id_desc *) &desc);
Vojtech Trefny 97cfd5
     ret = nvme_identify_ns (fd, nsid, &ns_info);
Vojtech Trefny 97cfd5
     if (ret != 0) {
Vojtech Trefny 97cfd5
         _nvme_status_to_error (ret, FALSE, error);
Vojtech Trefny 97cfd5
@@ -573,6 +574,13 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
Vojtech Trefny 97cfd5
         close (fd);
Vojtech Trefny 97cfd5
         return NULL;
Vojtech Trefny 97cfd5
     }
Vojtech Trefny 97cfd5
+
Vojtech Trefny 97cfd5
+    /* send the NVME_IDENTIFY_CNS_CTRL ioctl */
Vojtech Trefny 97cfd5
+    ret_ctrl = nvme_identify_ctrl (fd, &ctrl_id);
Vojtech Trefny 97cfd5
+
Vojtech Trefny 97cfd5
+    /* send the NVME_IDENTIFY_CNS_NS_DESC_LIST ioctl, NVMe 1.3 */
Vojtech Trefny 97cfd5
+    if (ret_ctrl == 0 && GUINT32_FROM_LE (ctrl_id.ver) >= 0x10300)
Vojtech Trefny 97cfd5
+        ret_desc = nvme_identify_ns_descs (fd, nsid, (struct nvme_ns_id_desc *) &desc);
Vojtech Trefny 97cfd5
     close (fd);
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
     info = g_new0 (BDNVMENamespaceInfo, 1);
Vojtech Trefny 97cfd5
-- 
Vojtech Trefny 97cfd5
2.39.0
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
From e6f7d0c4562623b03df96dc6b89ab00d8e4d6b90 Mon Sep 17 00:00:00 2001
Vojtech Trefny 97cfd5
From: Tomas Bzatek <tbzatek@redhat.com>
Vojtech Trefny 97cfd5
Date: Thu, 22 Sep 2022 16:56:26 +0200
Vojtech Trefny 97cfd5
Subject: [PATCH 2/5] nvme: Add BD_NVME_NS_FEAT_ROTATIONAL
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
A NVMe 2.0 feature indicating rotational medium on a namespace level.
Vojtech Trefny 97cfd5
Further information can be found in the Rotational Media Information Log
Vojtech Trefny 97cfd5
page (Log Identifier 16h) that is not implemented in libblockdev yet.
Vojtech Trefny 97cfd5
---
Vojtech Trefny 97cfd5
 src/lib/plugin_apis/nvme.api |  2 ++
Vojtech Trefny 97cfd5
 src/plugins/nvme/nvme-info.c | 10 ++++++++++
Vojtech Trefny 97cfd5
 src/plugins/nvme/nvme.h      |  2 ++
Vojtech Trefny 97cfd5
 tests/nvme_test.py           |  1 +
Vojtech Trefny 97cfd5
 4 files changed, 15 insertions(+)
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
diff --git a/src/lib/plugin_apis/nvme.api b/src/lib/plugin_apis/nvme.api
Vojtech Trefny 97cfd5
index 79247a01..7bc2cf9e 100644
Vojtech Trefny 97cfd5
--- a/src/lib/plugin_apis/nvme.api
Vojtech Trefny 97cfd5
+++ b/src/lib/plugin_apis/nvme.api
Vojtech Trefny 97cfd5
@@ -317,12 +317,14 @@ GType bd_nvme_lba_format_get_type () {
Vojtech Trefny 97cfd5
  *                                    in the NVM subsystem concurrently.
Vojtech Trefny 97cfd5
  * @BD_NVME_NS_FEAT_FORMAT_PROGRESS: indicates the capability to report the percentage of the namespace
Vojtech Trefny 97cfd5
  *                                   that remains to be formatted.
Vojtech Trefny 97cfd5
+ * @BD_NVME_NS_FEAT_ROTATIONAL: indicates a rotational medium.
Vojtech Trefny 97cfd5
  */
Vojtech Trefny 97cfd5
 /* BpG-skip-end */
Vojtech Trefny 97cfd5
 typedef enum {
Vojtech Trefny 97cfd5
     BD_NVME_NS_FEAT_THIN             = 1 << 0,
Vojtech Trefny 97cfd5
     BD_NVME_NS_FEAT_MULTIPATH_SHARED = 1 << 1,
Vojtech Trefny 97cfd5
     BD_NVME_NS_FEAT_FORMAT_PROGRESS  = 1 << 2,
Vojtech Trefny 97cfd5
+    BD_NVME_NS_FEAT_ROTATIONAL       = 1 << 3,
Vojtech Trefny 97cfd5
 } BDNVMENamespaceFeature;
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
 #define BD_NVME_TYPE_NAMESPACE_INFO (bd_nvme_namespace_info_get_type ())
Vojtech Trefny 97cfd5
diff --git a/src/plugins/nvme/nvme-info.c b/src/plugins/nvme/nvme-info.c
Vojtech Trefny 97cfd5
index 112b4054..c574a6f3 100644
Vojtech Trefny 97cfd5
--- a/src/plugins/nvme/nvme-info.c
Vojtech Trefny 97cfd5
+++ b/src/plugins/nvme/nvme-info.c
Vojtech Trefny 97cfd5
@@ -541,10 +541,12 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
Vojtech Trefny 97cfd5
     int ret;
Vojtech Trefny 97cfd5
     int ret_ctrl;
Vojtech Trefny 97cfd5
     int ret_desc = -1;
Vojtech Trefny 97cfd5
+    int ret_ns_ind = -1;
Vojtech Trefny 97cfd5
     int fd;
Vojtech Trefny 97cfd5
     __u32 nsid = 0;
Vojtech Trefny 97cfd5
     struct nvme_id_ctrl ctrl_id = ZERO_INIT;
Vojtech Trefny 97cfd5
     struct nvme_id_ns ns_info = ZERO_INIT;
Vojtech Trefny 97cfd5
+    struct nvme_id_independent_id_ns ns_info_ind = ZERO_INIT;
Vojtech Trefny 97cfd5
     uint8_t desc[NVME_IDENTIFY_DATA_SIZE] = ZERO_INIT;
Vojtech Trefny 97cfd5
     guint8 flbas;
Vojtech Trefny 97cfd5
     guint i;
Vojtech Trefny 97cfd5
@@ -581,6 +583,10 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
Vojtech Trefny 97cfd5
     /* send the NVME_IDENTIFY_CNS_NS_DESC_LIST ioctl, NVMe 1.3 */
Vojtech Trefny 97cfd5
     if (ret_ctrl == 0 && GUINT32_FROM_LE (ctrl_id.ver) >= 0x10300)
Vojtech Trefny 97cfd5
         ret_desc = nvme_identify_ns_descs (fd, nsid, (struct nvme_ns_id_desc *) &desc);
Vojtech Trefny 97cfd5
+
Vojtech Trefny 97cfd5
+    /* send the NVME_IDENTIFY_CNS_CSI_INDEPENDENT_ID_NS ioctl, NVMe 2.0 */
Vojtech Trefny 97cfd5
+    if (ret_ctrl == 0 && GUINT32_FROM_LE (ctrl_id.ver) >= 0x20000)
Vojtech Trefny 97cfd5
+        ret_ns_ind = nvme_identify_independent_identify_ns (fd, nsid, &ns_info_ind);
Vojtech Trefny 97cfd5
     close (fd);
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
     info = g_new0 (BDNVMENamespaceInfo, 1);
Vojtech Trefny 97cfd5
@@ -627,6 +633,10 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
Vojtech Trefny 97cfd5
             }
Vojtech Trefny 97cfd5
         }
Vojtech Trefny 97cfd5
     }
Vojtech Trefny 97cfd5
+    if (ret_ns_ind == 0) {
Vojtech Trefny 97cfd5
+        if ((ns_info_ind.nsfeat & 1 << 4) == 1 << 4)
Vojtech Trefny 97cfd5
+            info->features |= BD_NVME_NS_FEAT_ROTATIONAL;
Vojtech Trefny 97cfd5
+    }
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
     /* translate the LBA Format array */
Vojtech Trefny 97cfd5
     ptr_array = g_ptr_array_new ();
Vojtech Trefny 97cfd5
diff --git a/src/plugins/nvme/nvme.h b/src/plugins/nvme/nvme.h
Vojtech Trefny 97cfd5
index a7d30d79..ad456a82 100644
Vojtech Trefny 97cfd5
--- a/src/plugins/nvme/nvme.h
Vojtech Trefny 97cfd5
+++ b/src/plugins/nvme/nvme.h
Vojtech Trefny 97cfd5
@@ -202,11 +202,13 @@ typedef struct BDNVMELBAFormat {
Vojtech Trefny 97cfd5
  *                                    in the NVM subsystem concurrently.
Vojtech Trefny 97cfd5
  * @BD_NVME_NS_FEAT_FORMAT_PROGRESS: indicates the capability to report the percentage of the namespace
Vojtech Trefny 97cfd5
  *                                   that remains to be formatted.
Vojtech Trefny 97cfd5
+ * @BD_NVME_NS_FEAT_ROTATIONAL: indicates a rotational medium.
Vojtech Trefny 97cfd5
  */
Vojtech Trefny 97cfd5
 typedef enum {
Vojtech Trefny 97cfd5
     BD_NVME_NS_FEAT_THIN             = 1 << 0,
Vojtech Trefny 97cfd5
     BD_NVME_NS_FEAT_MULTIPATH_SHARED = 1 << 1,
Vojtech Trefny 97cfd5
     BD_NVME_NS_FEAT_FORMAT_PROGRESS  = 1 << 2,
Vojtech Trefny 97cfd5
+    BD_NVME_NS_FEAT_ROTATIONAL       = 1 << 3,
Vojtech Trefny 97cfd5
 } BDNVMENamespaceFeature;
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
 /**
Vojtech Trefny 97cfd5
diff --git a/tests/nvme_test.py b/tests/nvme_test.py
Vojtech Trefny 97cfd5
index a46f7422..f205e539 100644
Vojtech Trefny 97cfd5
--- a/tests/nvme_test.py
Vojtech Trefny 97cfd5
+++ b/tests/nvme_test.py
Vojtech Trefny 97cfd5
@@ -73,6 +73,7 @@ class NVMeTestCase(NVMeTest):
Vojtech Trefny 97cfd5
         self.assertFalse(info.features & BlockDev.NVMENamespaceFeature.THIN)
Vojtech Trefny 97cfd5
         self.assertTrue (info.features & BlockDev.NVMENamespaceFeature.MULTIPATH_SHARED)
Vojtech Trefny 97cfd5
         self.assertFalse(info.features & BlockDev.NVMENamespaceFeature.FORMAT_PROGRESS)
Vojtech Trefny 97cfd5
+        self.assertFalse(info.features & BlockDev.NVMENamespaceFeature.ROTATIONAL)
Vojtech Trefny 97cfd5
         self.assertEqual(info.eui64, "0000000000000000")
Vojtech Trefny 97cfd5
         self.assertEqual(info.format_progress_remaining, 0)
Vojtech Trefny 97cfd5
         self.assertEqual(len(info.lba_formats), 1)
Vojtech Trefny 97cfd5
-- 
Vojtech Trefny 97cfd5
2.39.0
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
From 4ff0df937dcd357623e7b7d960c08c476b1deffb Mon Sep 17 00:00:00 2001
Vojtech Trefny 97cfd5
From: Tomas Bzatek <tbzatek@redhat.com>
Vojtech Trefny 97cfd5
Date: Fri, 9 Dec 2022 16:13:43 +0100
Vojtech Trefny 97cfd5
Subject: [PATCH 3/5] nvme: Fix namespace identifiers
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
Use Namespace Identification Descriptor list (CNS 03h) data when available
Vojtech Trefny 97cfd5
and NVM Command Set Identify Namespace Data Structure (CNS 00h) as a fallback.
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
Also, if the CNS 00h EUI64 or NGUID fields equal to zero, return NULL
Vojtech Trefny 97cfd5
instead of zeroes:
Vojtech Trefny 97cfd5
  "If the controller is not able to provide a ... identifier in this field,
Vojtech Trefny 97cfd5
   then this field shall be cleared to 0h."
Vojtech Trefny 97cfd5
---
Vojtech Trefny 97cfd5
 src/plugins/nvme/nvme-info.c | 26 +++++++++++++++++++-------
Vojtech Trefny 97cfd5
 tests/nvme_test.py           |  2 +-
Vojtech Trefny 97cfd5
 2 files changed, 20 insertions(+), 8 deletions(-)
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
diff --git a/src/plugins/nvme/nvme-info.c b/src/plugins/nvme/nvme-info.c
Vojtech Trefny 97cfd5
index c574a6f3..ac189abe 100644
Vojtech Trefny 97cfd5
--- a/src/plugins/nvme/nvme-info.c
Vojtech Trefny 97cfd5
+++ b/src/plugins/nvme/nvme-info.c
Vojtech Trefny 97cfd5
@@ -603,12 +603,7 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
Vojtech Trefny 97cfd5
     info->format_progress_remaining = ns_info.fpi & NVME_NS_FPI_REMAINING;
Vojtech Trefny 97cfd5
     /* TODO: what the ns_info.nvmcap really stands for? */
Vojtech Trefny 97cfd5
     info->write_protected = (ns_info.nsattr & NVME_NS_NSATTR_WRITE_PROTECTED) == NVME_NS_NSATTR_WRITE_PROTECTED;
Vojtech Trefny 97cfd5
-    info->nguid = g_malloc0 (sizeof (ns_info.nguid) * 2 + 1);
Vojtech Trefny 97cfd5
-    for (i = 0; i < G_N_ELEMENTS (ns_info.nguid); i++)
Vojtech Trefny 97cfd5
-        snprintf (info->nguid + i * 2, 3, "%02x", ns_info.nguid[i]);
Vojtech Trefny 97cfd5
-    info->eui64 = g_malloc0 (sizeof (ns_info.eui64) * 2 + 1);
Vojtech Trefny 97cfd5
-    for (i = 0; i < G_N_ELEMENTS (ns_info.eui64); i++)
Vojtech Trefny 97cfd5
-        snprintf (info->eui64 + i * 2, 3, "%02x", ns_info.eui64[i]);
Vojtech Trefny 97cfd5
+
Vojtech Trefny 97cfd5
     if (ret_desc == 0) {
Vojtech Trefny 97cfd5
         for (i = 0; i < NVME_IDENTIFY_DATA_SIZE; i += len) {
Vojtech Trefny 97cfd5
             struct nvme_ns_id_desc *d = (void *) desc + i;
Vojtech Trefny 97cfd5
@@ -620,8 +615,14 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
             switch (d->nidt) {
Vojtech Trefny 97cfd5
                 case NVME_NIDT_EUI64:
Vojtech Trefny 97cfd5
+                    info->eui64 = g_malloc0 (d->nidl * 2 + 1);
Vojtech Trefny 97cfd5
+                    for (i = 0; i < d->nidl; i++)
Vojtech Trefny 97cfd5
+                        snprintf (info->eui64 + i * 2, 3, "%02x", d->nid[i]);
Vojtech Trefny 97cfd5
+                    break;
Vojtech Trefny 97cfd5
                 case NVME_NIDT_NGUID:
Vojtech Trefny 97cfd5
-                    /* already have these from nvme_identify_ns() */
Vojtech Trefny 97cfd5
+                    info->nguid = g_malloc0 (d->nidl * 2 + 1);
Vojtech Trefny 97cfd5
+                    for (i = 0; i < d->nidl; i++)
Vojtech Trefny 97cfd5
+                        snprintf (info->nguid + i * 2, 3, "%02x", d->nid[i]);
Vojtech Trefny 97cfd5
                     break;
Vojtech Trefny 97cfd5
                 case NVME_NIDT_UUID:
Vojtech Trefny 97cfd5
                     uuid_unparse (d->nid, uuid_buf);
Vojtech Trefny 97cfd5
@@ -633,6 +634,17 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
Vojtech Trefny 97cfd5
             }
Vojtech Trefny 97cfd5
         }
Vojtech Trefny 97cfd5
     }
Vojtech Trefny 97cfd5
+
Vojtech Trefny 97cfd5
+    if (info->nguid == NULL && ns_info.nguid[G_N_ELEMENTS (ns_info.nguid) - 1] > 0) {
Vojtech Trefny 97cfd5
+        info->nguid = g_malloc0 (sizeof (ns_info.nguid) * 2 + 1);
Vojtech Trefny 97cfd5
+        for (i = 0; i < G_N_ELEMENTS (ns_info.nguid); i++)
Vojtech Trefny 97cfd5
+            snprintf (info->nguid + i * 2, 3, "%02x", ns_info.nguid[i]);
Vojtech Trefny 97cfd5
+    }
Vojtech Trefny 97cfd5
+    if (info->eui64 == NULL && ns_info.eui64[G_N_ELEMENTS (ns_info.eui64) - 1] > 0) {
Vojtech Trefny 97cfd5
+        info->eui64 = g_malloc0 (sizeof (ns_info.eui64) * 2 + 1);
Vojtech Trefny 97cfd5
+        for (i = 0; i < G_N_ELEMENTS (ns_info.eui64); i++)
Vojtech Trefny 97cfd5
+            snprintf (info->eui64 + i * 2, 3, "%02x", ns_info.eui64[i]);
Vojtech Trefny 97cfd5
+    }
Vojtech Trefny 97cfd5
     if (ret_ns_ind == 0) {
Vojtech Trefny 97cfd5
         if ((ns_info_ind.nsfeat & 1 << 4) == 1 << 4)
Vojtech Trefny 97cfd5
             info->features |= BD_NVME_NS_FEAT_ROTATIONAL;
Vojtech Trefny 97cfd5
diff --git a/tests/nvme_test.py b/tests/nvme_test.py
Vojtech Trefny 97cfd5
index f205e539..a1822be6 100644
Vojtech Trefny 97cfd5
--- a/tests/nvme_test.py
Vojtech Trefny 97cfd5
+++ b/tests/nvme_test.py
Vojtech Trefny 97cfd5
@@ -74,7 +74,7 @@ class NVMeTestCase(NVMeTest):
Vojtech Trefny 97cfd5
         self.assertTrue (info.features & BlockDev.NVMENamespaceFeature.MULTIPATH_SHARED)
Vojtech Trefny 97cfd5
         self.assertFalse(info.features & BlockDev.NVMENamespaceFeature.FORMAT_PROGRESS)
Vojtech Trefny 97cfd5
         self.assertFalse(info.features & BlockDev.NVMENamespaceFeature.ROTATIONAL)
Vojtech Trefny 97cfd5
-        self.assertEqual(info.eui64, "0000000000000000")
Vojtech Trefny 97cfd5
+        self.assertIsNone(info.eui64)
Vojtech Trefny 97cfd5
         self.assertEqual(info.format_progress_remaining, 0)
Vojtech Trefny 97cfd5
         self.assertEqual(len(info.lba_formats), 1)
Vojtech Trefny 97cfd5
         self.assertGreater(len(info.nguid), 0)
Vojtech Trefny 97cfd5
-- 
Vojtech Trefny 97cfd5
2.39.0
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
From cdbb9a37a19d3f388910f68c4c384bafae8901ae Mon Sep 17 00:00:00 2001
Vojtech Trefny 97cfd5
From: Tomas Bzatek <tbzatek@redhat.com>
Vojtech Trefny 97cfd5
Date: Wed, 11 Jan 2023 18:19:36 +0100
Vojtech Trefny 97cfd5
Subject: [PATCH 4/5] nvme: Use libnvme-1.2's nvme_uuid_to_string()
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
This also bumps libnvme dependency to 1.2
Vojtech Trefny 97cfd5
---
Vojtech Trefny 97cfd5
 configure.ac                    |  5 +----
Vojtech Trefny 97cfd5
 src/plugins/nvme/Makefile.am    |  4 ++--
Vojtech Trefny 97cfd5
 src/plugins/nvme/nvme-error.c   |  3 ---
Vojtech Trefny 97cfd5
 src/plugins/nvme/nvme-fabrics.c |  1 -
Vojtech Trefny 97cfd5
 src/plugins/nvme/nvme-info.c    | 17 ++++++++++-------
Vojtech Trefny 97cfd5
 src/plugins/nvme/nvme-op.c      |  1 -
Vojtech Trefny 97cfd5
 src/plugins/nvme/nvme.c         |  1 -
Vojtech Trefny 97cfd5
 7 files changed, 13 insertions(+), 19 deletions(-)
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
diff --git a/configure.ac b/configure.ac
Vojtech Trefny 97cfd5
index ec789c91..fbd70473 100644
Vojtech Trefny 97cfd5
--- a/configure.ac
Vojtech Trefny 97cfd5
+++ b/configure.ac
Vojtech Trefny 97cfd5
@@ -281,10 +281,7 @@ AS_IF([test "x$with_nvdimm" != "xno"],
Vojtech Trefny 97cfd5
       [])
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
 AS_IF([test "x$with_nvme" != "xno"],
Vojtech Trefny 97cfd5
-      [LIBBLOCKDEV_PKG_CHECK_MODULES([NVME], [libnvme >= 1.0])
Vojtech Trefny 97cfd5
-      AS_IF([$PKG_CONFIG --atleast-version=1.1 libnvme],
Vojtech Trefny 97cfd5
-            [AC_DEFINE([HAVE_LIBNVME_1_1])], [])
Vojtech Trefny 97cfd5
-      ],
Vojtech Trefny 97cfd5
+      [LIBBLOCKDEV_PKG_CHECK_MODULES([NVME], [libnvme >= 1.2])],
Vojtech Trefny 97cfd5
       [])
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
 AS_IF([test "x$with_vdo" != "xno"],
Vojtech Trefny 97cfd5
diff --git a/src/plugins/nvme/Makefile.am b/src/plugins/nvme/Makefile.am
Vojtech Trefny 97cfd5
index b4a10ce0..a8a856d4 100644
Vojtech Trefny 97cfd5
--- a/src/plugins/nvme/Makefile.am
Vojtech Trefny 97cfd5
+++ b/src/plugins/nvme/Makefile.am
Vojtech Trefny 97cfd5
@@ -2,8 +2,8 @@ AUTOMAKE_OPTIONS = subdir-objects
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
 lib_LTLIBRARIES = libbd_nvme.la
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
-libbd_nvme_la_CFLAGS = $(GLIB_CFLAGS) $(GIO_CFLAGS) $(UUID_CFLAGS) $(NVME_CFLAGS) -Wall -Wextra -Werror
Vojtech Trefny 97cfd5
-libbd_nvme_la_LIBADD = ${builddir}/../../utils/libbd_utils.la $(GLIB_LIBS) $(GIO_LIBS) $(UUID_LIBS) $(NVME_LIBS)
Vojtech Trefny 97cfd5
+libbd_nvme_la_CFLAGS = $(GLIB_CFLAGS) $(GIO_CFLAGS) $(NVME_CFLAGS) -Wall -Wextra -Werror
Vojtech Trefny 97cfd5
+libbd_nvme_la_LIBADD = ${builddir}/../../utils/libbd_utils.la $(GLIB_LIBS) $(GIO_LIBS) $(NVME_LIBS)
Vojtech Trefny 97cfd5
 libbd_nvme_la_LDFLAGS = -L${srcdir}/../../utils/ -version-info 2:0:0 -Wl,--no-undefined
Vojtech Trefny 97cfd5
 libbd_nvme_la_CPPFLAGS = -I${builddir}/../../../include/ -I${srcdir}/../ -I. -DPACKAGE_SYSCONF_DIR=\""$(sysconfdir)"\"
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
diff --git a/src/plugins/nvme/nvme-error.c b/src/plugins/nvme/nvme-error.c
Vojtech Trefny 97cfd5
index 86f0d6a3..cb95a46d 100644
Vojtech Trefny 97cfd5
--- a/src/plugins/nvme/nvme-error.c
Vojtech Trefny 97cfd5
+++ b/src/plugins/nvme/nvme-error.c
Vojtech Trefny 97cfd5
@@ -28,7 +28,6 @@
Vojtech Trefny 97cfd5
 #include <malloc.h>
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
 #include <libnvme.h>
Vojtech Trefny 97cfd5
-#include <uuid/uuid.h>
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
 #include <blockdev/utils.h>
Vojtech Trefny 97cfd5
 #include <check_deps.h>
Vojtech Trefny 97cfd5
@@ -123,7 +122,6 @@ void _nvme_fabrics_errno_to_gerror (int result, int _errno, GError **error)
Vojtech Trefny 97cfd5
             case ENVME_CONNECT_LOOKUP_SUBSYS:
Vojtech Trefny 97cfd5
                 code = BD_NVME_ERROR_CONNECT;
Vojtech Trefny 97cfd5
                 break;
Vojtech Trefny 97cfd5
-#ifdef HAVE_LIBNVME_1_1
Vojtech Trefny 97cfd5
             case ENVME_CONNECT_ALREADY:
Vojtech Trefny 97cfd5
                 code = BD_NVME_ERROR_CONNECT_ALREADY;
Vojtech Trefny 97cfd5
                 break;
Vojtech Trefny 97cfd5
@@ -139,7 +137,6 @@ void _nvme_fabrics_errno_to_gerror (int result, int _errno, GError **error)
Vojtech Trefny 97cfd5
             case ENVME_CONNECT_OPNOTSUPP:
Vojtech Trefny 97cfd5
                 code = BD_NVME_ERROR_CONNECT_OPNOTSUPP;
Vojtech Trefny 97cfd5
                 break;
Vojtech Trefny 97cfd5
-#endif
Vojtech Trefny 97cfd5
             default:
Vojtech Trefny 97cfd5
                 code = BD_NVME_ERROR_CONNECT;
Vojtech Trefny 97cfd5
         }
Vojtech Trefny 97cfd5
diff --git a/src/plugins/nvme/nvme-fabrics.c b/src/plugins/nvme/nvme-fabrics.c
Vojtech Trefny 97cfd5
index 20ed57f5..bba7392d 100644
Vojtech Trefny 97cfd5
--- a/src/plugins/nvme/nvme-fabrics.c
Vojtech Trefny 97cfd5
+++ b/src/plugins/nvme/nvme-fabrics.c
Vojtech Trefny 97cfd5
@@ -30,7 +30,6 @@
Vojtech Trefny 97cfd5
 #include <glib/gstdio.h>
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
 #include <libnvme.h>
Vojtech Trefny 97cfd5
-#include <uuid/uuid.h>
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
 #include <blockdev/utils.h>
Vojtech Trefny 97cfd5
 #include <check_deps.h>
Vojtech Trefny 97cfd5
diff --git a/src/plugins/nvme/nvme-info.c b/src/plugins/nvme/nvme-info.c
Vojtech Trefny 97cfd5
index ac189abe..18719d51 100644
Vojtech Trefny 97cfd5
--- a/src/plugins/nvme/nvme-info.c
Vojtech Trefny 97cfd5
+++ b/src/plugins/nvme/nvme-info.c
Vojtech Trefny 97cfd5
@@ -28,7 +28,6 @@
Vojtech Trefny 97cfd5
 #include <malloc.h>
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
 #include <libnvme.h>
Vojtech Trefny 97cfd5
-#include <uuid/uuid.h>
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
 #include <blockdev/utils.h>
Vojtech Trefny 97cfd5
 #include <check_deps.h>
Vojtech Trefny 97cfd5
@@ -408,6 +407,14 @@ static gchar *decode_nvme_rev (guint32 ver) {
Vojtech Trefny 97cfd5
         return g_strdup_printf ("%u.%u.%u", mjr, mnr, ter);
Vojtech Trefny 97cfd5
 }
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
+static gchar *_uuid_to_str (unsigned char uuid[NVME_UUID_LEN]) {
Vojtech Trefny 97cfd5
+    gchar uuid_buf[NVME_UUID_LEN_STRING] = ZERO_INIT;
Vojtech Trefny 97cfd5
+
Vojtech Trefny 97cfd5
+    if (nvme_uuid_to_string (uuid, uuid_buf) == 0)
Vojtech Trefny 97cfd5
+        return g_strdup (uuid_buf);
Vojtech Trefny 97cfd5
+    return NULL;
Vojtech Trefny 97cfd5
+}
Vojtech Trefny 97cfd5
+
Vojtech Trefny 97cfd5
 /**
Vojtech Trefny 97cfd5
  * bd_nvme_get_controller_info:
Vojtech Trefny 97cfd5
  * @device: a NVMe controller device (e.g. `/dev/nvme0`)
Vojtech Trefny 97cfd5
@@ -461,9 +468,7 @@ BDNVMEControllerInfo * bd_nvme_get_controller_info (const gchar *device, GError
Vojtech Trefny 97cfd5
     info->pci_vendor_id = GUINT16_FROM_LE (ctrl_id.vid);
Vojtech Trefny 97cfd5
     info->pci_subsys_vendor_id = GUINT16_FROM_LE (ctrl_id.ssvid);
Vojtech Trefny 97cfd5
     info->ctrl_id = GUINT16_FROM_LE (ctrl_id.cntlid);
Vojtech Trefny 97cfd5
-    /* TODO: decode fguid as 128-bit hex string? */
Vojtech Trefny 97cfd5
-    info->fguid = g_strdup_printf ("%-.*s", (int) sizeof (ctrl_id.fguid), ctrl_id.fguid);
Vojtech Trefny 97cfd5
-    g_strstrip (info->fguid);
Vojtech Trefny 97cfd5
+    info->fguid = _uuid_to_str (ctrl_id.fguid);
Vojtech Trefny 97cfd5
     info->model_number = g_strndup (ctrl_id.mn, sizeof (ctrl_id.mn));
Vojtech Trefny 97cfd5
     g_strstrip (info->model_number);
Vojtech Trefny 97cfd5
     info->serial_number = g_strndup (ctrl_id.sn, sizeof (ctrl_id.sn));
Vojtech Trefny 97cfd5
@@ -607,7 +612,6 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
Vojtech Trefny 97cfd5
     if (ret_desc == 0) {
Vojtech Trefny 97cfd5
         for (i = 0; i < NVME_IDENTIFY_DATA_SIZE; i += len) {
Vojtech Trefny 97cfd5
             struct nvme_ns_id_desc *d = (void *) desc + i;
Vojtech Trefny 97cfd5
-            gchar uuid_buf[37] = ZERO_INIT;
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
             if (!d->nidl)
Vojtech Trefny 97cfd5
                 break;
Vojtech Trefny 97cfd5
@@ -625,8 +629,7 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
Vojtech Trefny 97cfd5
                         snprintf (info->nguid + i * 2, 3, "%02x", d->nid[i]);
Vojtech Trefny 97cfd5
                     break;
Vojtech Trefny 97cfd5
                 case NVME_NIDT_UUID:
Vojtech Trefny 97cfd5
-                    uuid_unparse (d->nid, uuid_buf);
Vojtech Trefny 97cfd5
-                    info->uuid = g_strdup (uuid_buf);
Vojtech Trefny 97cfd5
+                    info->uuid = _uuid_to_str (d->nid);
Vojtech Trefny 97cfd5
                     break;
Vojtech Trefny 97cfd5
                 case NVME_NIDT_CSI:
Vojtech Trefny 97cfd5
                     /* unused */
Vojtech Trefny 97cfd5
diff --git a/src/plugins/nvme/nvme-op.c b/src/plugins/nvme/nvme-op.c
Vojtech Trefny 97cfd5
index 4568c453..c9e92697 100644
Vojtech Trefny 97cfd5
--- a/src/plugins/nvme/nvme-op.c
Vojtech Trefny 97cfd5
+++ b/src/plugins/nvme/nvme-op.c
Vojtech Trefny 97cfd5
@@ -29,7 +29,6 @@
Vojtech Trefny 97cfd5
 #include <linux/fs.h>
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
 #include <libnvme.h>
Vojtech Trefny 97cfd5
-#include <uuid/uuid.h>
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
 #include <blockdev/utils.h>
Vojtech Trefny 97cfd5
 #include <check_deps.h>
Vojtech Trefny 97cfd5
diff --git a/src/plugins/nvme/nvme.c b/src/plugins/nvme/nvme.c
Vojtech Trefny 97cfd5
index 00f2f76e..4a32ac4e 100644
Vojtech Trefny 97cfd5
--- a/src/plugins/nvme/nvme.c
Vojtech Trefny 97cfd5
+++ b/src/plugins/nvme/nvme.c
Vojtech Trefny 97cfd5
@@ -28,7 +28,6 @@
Vojtech Trefny 97cfd5
 #include <malloc.h>
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
 #include <libnvme.h>
Vojtech Trefny 97cfd5
-#include <uuid/uuid.h>
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
 #include <blockdev/utils.h>
Vojtech Trefny 97cfd5
 #include <check_deps.h>
Vojtech Trefny 97cfd5
-- 
Vojtech Trefny 97cfd5
2.39.0
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
From 64263599ec39b6b0f20d8e16c1169afcf66f5d9a Mon Sep 17 00:00:00 2001
Vojtech Trefny 97cfd5
From: Tomas Bzatek <tbzatek@redhat.com>
Vojtech Trefny 97cfd5
Date: Thu, 12 Jan 2023 16:01:28 +0100
Vojtech Trefny 97cfd5
Subject: [PATCH 5/5] nvme: Fix zeroed struct fields detection
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
As often stated in the NVMe specification, fields of features that
Vojtech Trefny 97cfd5
are either not implemented or not valid are typically cleared to zero (0h).
Vojtech Trefny 97cfd5
---
Vojtech Trefny 97cfd5
 src/plugins/nvme/nvme-info.c | 20 +++++++++++++++-----
Vojtech Trefny 97cfd5
 tests/nvme_test.py           |  2 +-
Vojtech Trefny 97cfd5
 2 files changed, 16 insertions(+), 6 deletions(-)
Vojtech Trefny 97cfd5
Vojtech Trefny 97cfd5
diff --git a/src/plugins/nvme/nvme-info.c b/src/plugins/nvme/nvme-info.c
Vojtech Trefny 97cfd5
index 18719d51..85f94a32 100644
Vojtech Trefny 97cfd5
--- a/src/plugins/nvme/nvme-info.c
Vojtech Trefny 97cfd5
+++ b/src/plugins/nvme/nvme-info.c
Vojtech Trefny 97cfd5
@@ -415,6 +415,15 @@ static gchar *_uuid_to_str (unsigned char uuid[NVME_UUID_LEN]) {
Vojtech Trefny 97cfd5
     return NULL;
Vojtech Trefny 97cfd5
 }
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
+static gboolean _nvme_a_is_zero (const __u8 a[], int len) {
Vojtech Trefny 97cfd5
+    int i;
Vojtech Trefny 97cfd5
+
Vojtech Trefny 97cfd5
+    for (i = 0; i < len; i++)
Vojtech Trefny 97cfd5
+        if (a[i] > 0)
Vojtech Trefny 97cfd5
+            return FALSE;
Vojtech Trefny 97cfd5
+    return TRUE;
Vojtech Trefny 97cfd5
+}
Vojtech Trefny 97cfd5
+
Vojtech Trefny 97cfd5
 /**
Vojtech Trefny 97cfd5
  * bd_nvme_get_controller_info:
Vojtech Trefny 97cfd5
  * @device: a NVMe controller device (e.g. `/dev/nvme0`)
Vojtech Trefny 97cfd5
@@ -468,7 +477,8 @@ BDNVMEControllerInfo * bd_nvme_get_controller_info (const gchar *device, GError
Vojtech Trefny 97cfd5
     info->pci_vendor_id = GUINT16_FROM_LE (ctrl_id.vid);
Vojtech Trefny 97cfd5
     info->pci_subsys_vendor_id = GUINT16_FROM_LE (ctrl_id.ssvid);
Vojtech Trefny 97cfd5
     info->ctrl_id = GUINT16_FROM_LE (ctrl_id.cntlid);
Vojtech Trefny 97cfd5
-    info->fguid = _uuid_to_str (ctrl_id.fguid);
Vojtech Trefny 97cfd5
+    if (!_nvme_a_is_zero (ctrl_id.fguid, sizeof (ctrl_id.fguid)))
Vojtech Trefny 97cfd5
+        info->fguid = _uuid_to_str (ctrl_id.fguid);
Vojtech Trefny 97cfd5
     info->model_number = g_strndup (ctrl_id.mn, sizeof (ctrl_id.mn));
Vojtech Trefny 97cfd5
     g_strstrip (info->model_number);
Vojtech Trefny 97cfd5
     info->serial_number = g_strndup (ctrl_id.sn, sizeof (ctrl_id.sn));
Vojtech Trefny 97cfd5
@@ -638,14 +648,14 @@ BDNVMENamespaceInfo *bd_nvme_get_namespace_info (const gchar *device, GError **e
Vojtech Trefny 97cfd5
         }
Vojtech Trefny 97cfd5
     }
Vojtech Trefny 97cfd5
 
Vojtech Trefny 97cfd5
-    if (info->nguid == NULL && ns_info.nguid[G_N_ELEMENTS (ns_info.nguid) - 1] > 0) {
Vojtech Trefny 97cfd5
+    if (info->nguid == NULL && !_nvme_a_is_zero (ns_info.nguid, sizeof (ns_info.nguid))) {
Vojtech Trefny 97cfd5
         info->nguid = g_malloc0 (sizeof (ns_info.nguid) * 2 + 1);
Vojtech Trefny 97cfd5
-        for (i = 0; i < G_N_ELEMENTS (ns_info.nguid); i++)
Vojtech Trefny 97cfd5
+        for (i = 0; i < sizeof (ns_info.nguid); i++)
Vojtech Trefny 97cfd5
             snprintf (info->nguid + i * 2, 3, "%02x", ns_info.nguid[i]);
Vojtech Trefny 97cfd5
     }
Vojtech Trefny 97cfd5
-    if (info->eui64 == NULL && ns_info.eui64[G_N_ELEMENTS (ns_info.eui64) - 1] > 0) {
Vojtech Trefny 97cfd5
+    if (info->eui64 == NULL && !_nvme_a_is_zero (ns_info.eui64, sizeof (ns_info.eui64))) {
Vojtech Trefny 97cfd5
         info->eui64 = g_malloc0 (sizeof (ns_info.eui64) * 2 + 1);
Vojtech Trefny 97cfd5
-        for (i = 0; i < G_N_ELEMENTS (ns_info.eui64); i++)
Vojtech Trefny 97cfd5
+        for (i = 0; i < sizeof (ns_info.eui64); i++)
Vojtech Trefny 97cfd5
             snprintf (info->eui64 + i * 2, 3, "%02x", ns_info.eui64[i]);
Vojtech Trefny 97cfd5
     }
Vojtech Trefny 97cfd5
     if (ret_ns_ind == 0) {
Vojtech Trefny 97cfd5
diff --git a/tests/nvme_test.py b/tests/nvme_test.py
Vojtech Trefny 97cfd5
index a1822be6..a1494d9a 100644
Vojtech Trefny 97cfd5
--- a/tests/nvme_test.py
Vojtech Trefny 97cfd5
+++ b/tests/nvme_test.py
Vojtech Trefny 97cfd5
@@ -116,7 +116,7 @@ class NVMeTestCase(NVMeTest):
Vojtech Trefny 97cfd5
         self.assertFalse(info.features & BlockDev.NVMEControllerFeature.ENCLOSURE)
Vojtech Trefny 97cfd5
         self.assertFalse(info.features & BlockDev.NVMEControllerFeature.MGMT_PCIE)
Vojtech Trefny 97cfd5
         self.assertFalse(info.features & BlockDev.NVMEControllerFeature.MGMT_SMBUS)
Vojtech Trefny 97cfd5
-        self.assertEqual(info.fguid, "")
Vojtech Trefny 97cfd5
+        self.assertIsNone(info.fguid)
Vojtech Trefny 97cfd5
         self.assertEqual(info.pci_vendor_id, 0)
Vojtech Trefny 97cfd5
         self.assertEqual(info.pci_subsys_vendor_id, 0)
Vojtech Trefny 97cfd5
         self.assertIn("Linux", info.model_number)
Vojtech Trefny 97cfd5
-- 
Vojtech Trefny 97cfd5
2.39.0
Vojtech Trefny 97cfd5