From 0ea4bfea9bd0c6d0c580bd8aa0f809f86aef0bc1 Mon Sep 17 00:00:00 2001
From: chrfranke <authors@smartmontools.org>
Date: Wed, 23 Oct 2024 16:49:02 +0000
Subject: [PATCH] nvmecmds.h: Add 'nvme_broadcast_nsid' and use it where
 applicable.

diff --git smartmontools/nvmecmds.cpp smartmontools/nvmecmds.cpp
index e7cecd07..897448c1 100644
--- smartmontools/nvmecmds.cpp
+++ smartmontools/nvmecmds.cpp
@@ -231,7 +231,7 @@ unsigned nvme_read_log_page(nvme_device * device, unsigned nsid, unsigned char l
 unsigned nvme_read_error_log(nvme_device * device, nvme_error_log_page * error_log,
   unsigned num_entries, bool lpo_sup)
 {
-  unsigned n = nvme_read_log_page(device, 0xffffffff, 0x01, error_log,
+  unsigned n = nvme_read_log_page(device, nvme_broadcast_nsid, 0x01, error_log,
                                   num_entries * sizeof(*error_log), lpo_sup);
 
   unsigned read_entries = n / sizeof(*error_log);
@@ -253,7 +253,8 @@ unsigned nvme_read_error_log(nvme_device * device, nvme_error_log_page * error_l
 // Read NVMe SMART/Health Information log.
 bool nvme_read_smart_log(nvme_device * device, nvme_smart_log & smart_log)
 {
-  if (!nvme_read_log_page_1(device, 0xffffffff, 0x02, &smart_log, sizeof(smart_log)))
+  if (!nvme_read_log_page_1(device, nvme_broadcast_nsid, 0x02, &smart_log,
+                            sizeof(smart_log)))
     return false;
 
   if (isbigendian()) {
diff --git smartmontools/nvmecmds.h smartmontools/nvmecmds.h
index c48b6efd..1638b5c4 100644
--- smartmontools/nvmecmds.h
+++ smartmontools/nvmecmds.h
@@ -253,6 +253,9 @@ STATIC_ASSERT(sizeof(nvme_self_test_log) == 564);
 
 class nvme_device;
 
+// Broadcast namespace ID.
+constexpr uint32_t nvme_broadcast_nsid = 0xffffffffU;
+
 // Print NVMe debug messages?
 extern unsigned char nvme_debugmode;
 
diff --git smartmontools/nvmeprint.cpp smartmontools/nvmeprint.cpp
index 95c0fdf5..9ceb8daa 100644
--- smartmontools/nvmeprint.cpp
+++ smartmontools/nvmeprint.cpp
@@ -523,7 +523,7 @@ static void print_error_log(const nvme_error_log_page * error_log,
       snprintf(lb, sizeof(lb), "%" PRIu64, e.lba);
       jrefi["lba"]["value"].set_unsafe_uint64(e.lba);
     }
-    if (e.nsid != 0xffffffffU) {
+    if (e.nsid != nvme_broadcast_nsid) {
       snprintf(ns, sizeof(ns), "%u", e.nsid);
       jrefi["nsid"] = e.nsid;
     }
@@ -548,7 +548,7 @@ static void print_self_test_log(const nvme_self_test_log & self_test_log, unsign
   // Figure 203 of NVM Express Base Specification Revision 1.4c, March 9, 2021
   json::ref jref = jglb["nvme_self_test_log"];
   jout("Self-test Log (NVMe Log 0x06, NSID 0x%x)\n", nsid);
-  jref["nsid"] = (nsid != 0xffffffff ? (int64_t)nsid : -1);
+  jref["nsid"] = (nsid != nvme_broadcast_nsid ? (int64_t)nsid : -1);
 
   const char * s; char buf[32];
   switch (self_test_log.current_operation & 0xf) {
@@ -618,12 +618,12 @@ static void print_self_test_log(const nvme_self_test_log & self_test_log, unsign
       jrefi["segment"] = r.segment;
     }
     if (r.valid & 0x01) {
-      if (r.nsid == 0xffffffff)
+      if (r.nsid == nvme_broadcast_nsid)
         ns[0] = '*', ns[1] = 0;
       else
         snprintf(ns, sizeof(ns), "%u", r.nsid);
       // Broadcast = -1
-      jrefi["nsid"] = (r.nsid != 0xffffffff ? (int64_t)r.nsid : -1);
+      jrefi["nsid"] = (r.nsid != nvme_broadcast_nsid ? (int64_t)r.nsid : -1);
     }
     if (r.valid & 0x02) {
       uint64_t lba = sg_get_unaligned_le64(r.lba);
@@ -676,7 +676,7 @@ int nvmePrintMain(nvme_device * device, const nvme_print_options & options)
     nvme_id_ns id_ns; memset(&id_ns, 0, sizeof(id_ns));
 
     unsigned nsid = device->get_nsid();
-    if (nsid == 0xffffffffU) {
+    if (nsid == nvme_broadcast_nsid) {
       // Broadcast namespace
       if (id_ctrl.nn == 1) {
         // No namespace management, get size from single namespace
@@ -760,7 +760,7 @@ int nvmePrintMain(nvme_device * device, const nvme_print_options & options)
       pout("Self-tests not supported\n\n");
     else {
       nvme_self_test_log self_test_log;
-      unsigned self_test_log_nsid = 0xffffffff;
+      unsigned self_test_log_nsid = nvme_broadcast_nsid;
       if (!nvme_read_self_test_log(device, self_test_log_nsid, self_test_log)) {
         jerr("Read Self-test Log failed: %s\n\n", device->get_errmsg());
         return retval | FAILSMART;
@@ -785,7 +785,7 @@ int nvmePrintMain(nvme_device * device, const nvme_print_options & options)
     case 1:
     case 2:
     case 3:
-      nsid = 0xffffffff;
+      nsid = nvme_broadcast_nsid;
       break;
     default:
       nsid = device->get_nsid();
diff --git smartmontools/scsinvme.cpp smartmontools/scsinvme.cpp
index 10f8eec3..a8f195cd 100644
--- smartmontools/scsinvme.cpp
+++ smartmontools/scsinvme.cpp
@@ -70,7 +70,7 @@ bool sntasmedia_device::nvme_pass_through(const nvme_cmd_in & in, nvme_cmd_out &
       }
       return set_err(ENOSYS, "NVMe Identify with CDW10=0x%08x not supported", in.cdw10);
     case smartmontools::nvme_admin_get_log_page:
-      if (!(in.nsid == 0xffffffff || !in.nsid))
+      if (!(in.nsid == nvme_broadcast_nsid || !in.nsid))
         return set_err(ENOSYS, "NVMe Get Log Page with NSID=0x%x not supported", in.nsid);
       break;
     default:
@@ -165,7 +165,7 @@ bool sntjmicron_device::open()
   // cannot detect e.g. /dev/sdX is NSID 2.
   // Set to broadcast if not available
   if (!get_nsid()) {
-    set_nsid(0xFFFFFFFF);
+    set_nsid(nvme_broadcast_nsid);
   }
 
   return true;
@@ -353,7 +353,7 @@ bool sntrealtek_device::nvme_pass_through(const nvme_cmd_in & in, nvme_cmd_out &
       }
       return set_err(ENOSYS, "NVMe Identify with CDW10=0x%08x not supported", in.cdw10);
     case smartmontools::nvme_admin_get_log_page:
-      if (!(in.nsid == 0xffffffff || !in.nsid))
+      if (!(in.nsid == nvme_broadcast_nsid || !in.nsid))
         return set_err(ENOSYS, "NVMe Get Log Page with NSID=0x%x not supported", in.nsid);
       if (size > 0x200) { // Reading more apparently returns old data from previous command
         // TODO: Add ability to return short reads to caller
@@ -413,7 +413,7 @@ nvme_device * smart_interface::get_snt_device(const char * type, scsi_device * s
 
   if (!strcmp(type, "sntasmedia")) {
     // No namespace supported
-    sntdev = new sntasmedia_device(this, scsidev, type, 0xffffffff);
+    sntdev = new sntasmedia_device(this, scsidev, type, nvme_broadcast_nsid);
   }
 
   else if (!strncmp(type, "sntjmicron", 10)) {
@@ -429,7 +429,7 @@ nvme_device * smart_interface::get_snt_device(const char * type, scsi_device * s
 
   else if (!strcmp(type, "sntrealtek")) {
     // No namespace supported
-    sntdev = new sntrealtek_device(this, scsidev, type, 0xffffffff);
+    sntdev = new sntrealtek_device(this, scsidev, type, nvme_broadcast_nsid);
   }
 
   else {
diff --git smartmontools/smartd.cpp smartmontools/smartd.cpp
index 34f2cde2..21d9a2d1 100644
--- smartmontools/smartd.cpp
+++ smartmontools/smartd.cpp
@@ -2784,7 +2784,7 @@ static int NVMeDeviceScan(dev_config & cfg, dev_state & state, nvme_device * nvm
   // Format device id string for warning emails
   char nsstr[32] = "", capstr[32] = "";
   unsigned nsid = nvmedev->get_nsid();
-  if (nsid != 0xffffffff)
+  if (nsid != nvme_broadcast_nsid)
     snprintf(nsstr, sizeof(nsstr), ", NSID:%u", nsid);
   uint64_t capacity = le128_to_uint64(id_ctrl.tnvmcap);
   if (capacity)
@@ -2851,7 +2851,7 @@ static int NVMeDeviceScan(dev_config & cfg, dev_state & state, nvme_device * nvm
     std::replace_if(model, model+strlen(model), not_allowed_in_filename, '_');
     std::replace_if(serial, serial+strlen(serial), not_allowed_in_filename, '_');
     nsstr[0] = 0;
-    if (nsid != 0xffffffff)
+    if (nsid != nvme_broadcast_nsid)
       snprintf(nsstr, sizeof(nsstr), "-n%u", nsid);
     cfg.state_file = strprintf("%s%s-%s%s.nvme.state", state_path_prefix.c_str(), model, serial, nsstr);
     // Read previous state
-- 
2.35.3

