git: 9fa9eea4896d - stable/15 - camcontrol: Fix extended host identifier retrieval

From: Kenneth D. Merry <ken_at_FreeBSD.org>
Date: Mon, 16 Nov 2026 21:00:45 UTC
The branch stable/15 has been updated by ken:

URL: https://cgit.FreeBSD.org/src/commit/?id=9fa9eea4896d55b88e48cddac110c2ca2a274b10

commit 9fa9eea4896d55b88e48cddac110c2ca2a274b10
Author:     Kenneth D. Merry <ken@FreeBSD.org>
AuthorDate: 2026-11-09 18:47:01 +0000
Commit:     Kenneth D. Merry <ken@FreeBSD.org>
CommitDate: 2026-11-16 16:12:47 +0000

    camcontrol: Fix extended host identifier retrieval
    
    The Get Features command for the host identifier always requested
    the 64-bit form (cdw11 EXHID=0), but the decoding logic sized the
    identifier from the controller's CTRATT extended host identifier
    capability bit, or always treated it as 128 bits when -U was
    given.  On a controller supporting the extended host identifier,
    the printed value did not match what was requested from the
    controller.
    
    Fetch the controller data before issuing the command, request the
    extended form when the controller supports it, and decode the form
    that was requested.  Reject -U when the controller only supports
    64-bit host identifiers, since an 8 byte value cannot be shown as
    a UUID.
    
    sys/cam/nvme/nvme_all.c,
    sys/cam/nvme/nvme_all.h:
            Add an exhid argument to nvme_get_feature_hostid(),
            matching nvme_set_feature_hostid(), and set the EXHID bit
            in cdw11 from it.
    
    sbin/camcontrol/nvfeature.c:
            Fetch the controller data and determine the host
            identifier size before issuing the Get Features command,
            request only as many bytes as the identifier holds, and
            decode the identifier in the form it was requested.
            Reject -U on controllers without extended host identifier
            support.
    
    Reported by:    llm-postcr
    Fixes:          9814633f631b
    Fixes:          6eae951cd51b
    MFC after:      1 week
    Sponsored by:   Spectra Logic
    Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
    
    (cherry picked from commit 2dd34affca4d1041b8f6f58bb24c45f29b720430)
---
 sbin/camcontrol/nvfeature.c | 55 ++++++++++++++++++++++++++++-----------------
 sys/cam/nvme/nvme_all.c     |  9 ++++++--
 sys/cam/nvme/nvme_all.h     |  5 +++--
 3 files changed, 44 insertions(+), 25 deletions(-)

diff --git a/sbin/camcontrol/nvfeature.c b/sbin/camcontrol/nvfeature.c
index f288365657f9..ee018f277d60 100644
--- a/sbin/camcontrol/nvfeature.c
+++ b/sbin/camcontrol/nvfeature.c
@@ -208,8 +208,7 @@ nvfeature_print_caps(union ccb *ccb, const char *msg)
 }
 
 static int
-nvfeature_print_hostid(struct cam_device *device, uint8_t *hostid,
-	int sel, int output_uuid)
+nvfeature_print_hostid(uint8_t *hostid, int sel, int exhid, int output_uuid)
 {
 	int error = 0;
 	char *hostid_str = NULL;
@@ -226,16 +225,7 @@ nvfeature_print_hostid(struct cam_device *device, uint8_t *hostid,
 			goto bailout;
 		}
 	} else {
-		int exhid;
 		struct sbuf *hostid_sbuf;
-		struct nvme_controller_data cdata;
-
-		error = nvme_get_data(device, CDAI_TYPE_NVME_CNTRL,
-				&cdata, sizeof(cdata));
-		if (error != 0) {
-			goto bailout;
-		}
-		exhid = cdata.ctratt & NVME_CD_CTRATT_EXHID;
 
 		hostid_sbuf = sbuf_new(NULL, NULL, 64, SBUF_FIXEDLEN);
 		if (hostid_sbuf == NULL) {
@@ -265,9 +255,11 @@ nvmefeature_get_hostid(struct cam_device *device, int sel, int verbosemode,
 		       int timeout)
 {
 	union ccb *ccb = NULL;
+	struct nvme_controller_data cdata;
 	uint8_t hostid[16];
 	uint8_t *data = NULL;
 	uint32_t datalen = 0;
+	int exhid = 0;
 	int error = 0;
 
 	if (! nvfeature_validate_select(sel)) {
@@ -275,28 +267,49 @@ nvmefeature_get_hostid(struct cam_device *device, int sel, int verbosemode,
 		goto bailout;
 	}
 
-	ccb = cam_getccb(device);
-	if (ccb == NULL) {
-		warnx("%s: error allocating CCB", __func__);
-		error = 1;
-		goto bailout;
-	}
-	CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
-
 	/* Getting capabilities does not output as uuid nor
 	 * is the data buffer (hostid) used/needed
 	 */
 	if (sel == NVME_FEAT_SEL_CAPABILITIES) {
 		output_uuid = 0;
 	} else {
+		/*
+		 * Request and decode the form of the host identifier
+		 * (64-bit or extended 128-bit) that the controller
+		 * supports.
+		 */
+		error = nvme_get_data(device, CDAI_TYPE_NVME_CNTRL, &cdata,
+		    sizeof(cdata));
+		if (error != 0)
+			goto bailout;
+		exhid = (cdata.ctratt & NVME_CD_CTRATT_EXHID) ? 1 : 0;
+
+		if (output_uuid == 1 && exhid == 0) {
+			warnx("%s: -U requires a 128-bit host identifier, "
+			    "which this controller does not support",
+			    __func__);
+			error = 1;
+			goto bailout;
+		}
+
 		bzero(hostid, sizeof(hostid));
 		data = hostid;
-		datalen = sizeof(hostid);
+		datalen = exhid ? 16 : 8;
+	}
+
+	ccb = cam_getccb(device);
+	if (ccb == NULL) {
+		warnx("%s: error allocating CCB", __func__);
+		error = 1;
+		goto bailout;
 	}
+	CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
+
 	nvme_get_feature_hostid(/*nvmeio*/ &ccb->nvmeio,
 				/*retries*/ retry_count,
 				/*cbfcnp*/ NULL,
 				/*sel*/ sel,
+				/*exhid*/ exhid,
 				/*uuid_idx*/ 0,
 				/*hostid*/ data,
 				/*dxfer_len*/ datalen,
@@ -322,7 +335,7 @@ nvmefeature_get_hostid(struct cam_device *device, int sel, int verbosemode,
 	if (sel == NVME_FEAT_SEL_CAPABILITIES) {
 		error = nvfeature_print_caps(ccb, "Host ID capabilities:");
 	} else {
-		error = nvfeature_print_hostid(device, hostid, sel,
+		error = nvfeature_print_hostid(hostid, sel, exhid,
 				output_uuid);
 	}
 
diff --git a/sys/cam/nvme/nvme_all.c b/sys/cam/nvme/nvme_all.c
index a239498901d1..1bad4702b027 100644
--- a/sys/cam/nvme/nvme_all.c
+++ b/sys/cam/nvme/nvme_all.c
@@ -319,16 +319,21 @@ nvme_set_feature(struct ccb_nvmeio *nvmeio, uint32_t retries,
 void
 nvme_get_feature_hostid(struct ccb_nvmeio *nvmeio, uint32_t retries,
 			void (*cbfcnp)(struct cam_periph *, union ccb *),
-			int sel, int uuid_idx, uint8_t *data_ptr,
+			int sel, int exhid, int uuid_idx, uint8_t *data_ptr,
 			uint32_t dxfer_len, uint32_t timeout)
 {
+	uint32_t cdw11 = 0;
+
+	if (exhid)
+		cdw11 = 0x01;
+
 	nvme_get_feature(/*nvmeio*/ nvmeio,
 			 /*retries*/ retries,
 			 /*cbfcnp*/ cbfcnp,
 			 /*nsid*/ 0,
 			 /*sel*/ sel,
 			 /*feature*/ NVME_FEAT_HOST_IDENTIFIER,
-			 /*cdw11*/ 0,
+			 /*cdw11*/ cdw11,
 			 /*uuid_idx*/ uuid_idx,
 			 /*data_ptr*/ data_ptr,
 			 /*dxfer_len*/ dxfer_len,
diff --git a/sys/cam/nvme/nvme_all.h b/sys/cam/nvme/nvme_all.h
index fede596d4b74..58115b69fcdd 100644
--- a/sys/cam/nvme/nvme_all.h
+++ b/sys/cam/nvme/nvme_all.h
@@ -78,8 +78,9 @@ void nvme_set_feature(struct ccb_nvmeio *nvmeio, uint32_t retries,
 		      uint32_t timeout);
 void nvme_get_feature_hostid(struct ccb_nvmeio *nvmeio, uint32_t retries,
 			     void (*cbfcnp)(struct cam_periph *, union ccb *),
-			     int sel, int uuid_idx, uint8_t *data_ptr,
-			     uint32_t dxfer_len, uint32_t timeout);
+			     int sel, int exhid, int uuid_idx,
+			     uint8_t *data_ptr, uint32_t dxfer_len,
+			     uint32_t timeout);
 void nvme_set_feature_hostid(struct ccb_nvmeio *nvmeio, uint32_t retries,
 			     void (*cbfcnp)(struct cam_periph *, union ccb *),
 			     int sv, int exhid, uint8_t *data_ptr,