git: 8af3223a5042 - stable/14 - nvme: honor FLBAS Format Index Upper when selecting the LBA format

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

URL: https://cgit.FreeBSD.org/src/commit/?id=8af3223a50422581e65a5d91c706cea467a33f07

commit 8af3223a50422581e65a5d91c706cea467a33f07
Author:     Abdelkader Boudih <seuros@FreeBSD.org>
AuthorDate: 2026-09-24 14:19:51 +0000
Commit:     Kenneth D. Merry <ken@FreeBSD.org>
CommitDate: 2026-11-16 21:00:04 +0000

    nvme: honor FLBAS Format Index Upper when selecting the LBA format
    
    Added nvme_ns_data_format_index() in the nvme, nda, and nvmf
    host paths as well as nvmecontrol and camdd.
    
    Reviewed by:    imp, adrian
    Differential Revision:  https://reviews.freebsd.org/D59627
    
    (cherry picked from commit 2c41f7d8a034ee64a0aaa69a1aa5cb07748a1a32)
---
 sbin/nvmecontrol/devlist.c  |  2 +-
 sbin/nvmecontrol/format.c   |  2 +-
 sbin/nvmecontrol/identify.c |  2 +-
 sys/cam/nvme/nvme_da.c      |  2 +-
 sys/dev/nvme/nvme.h         | 18 ++++++++++++++++++
 sys/dev/nvme/nvme_ns.c      |  5 ++---
 usr.sbin/camdd/camdd.c      |  2 +-
 7 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/sbin/nvmecontrol/devlist.c b/sbin/nvmecontrol/devlist.c
index b5e95b38937e..701f489dcc4d 100644
--- a/sbin/nvmecontrol/devlist.c
+++ b/sbin/nvmecontrol/devlist.c
@@ -83,7 +83,7 @@ ns_get_sector_size(struct nvme_namespace_data *nsdata)
 {
 	uint8_t flbas_fmt, lbads;
 
-	flbas_fmt = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, nsdata->flbas);
+	flbas_fmt = nvme_ns_data_format_index(nsdata);
 	lbads = NVMEV(NVME_NS_DATA_LBAF_LBADS, nsdata->lbaf[flbas_fmt]);
 
 	return (1 << lbads);
diff --git a/sbin/nvmecontrol/format.c b/sbin/nvmecontrol/format.c
index ea1e7877dc34..a507ddd78ffb 100644
--- a/sbin/nvmecontrol/format.c
+++ b/sbin/nvmecontrol/format.c
@@ -178,7 +178,7 @@ format(const struct cmd *f, int argc, char *argv[])
 		if (read_namespace_data(fd, nsid, &nsd))
 			errx(EX_IOERR, "Identify request failed");
 		if (lbaf < 0)
-			lbaf = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, nsd.flbas);
+			lbaf = nvme_ns_data_format_index(&nsd);
 		if (lbaf > nsd.nlbaf)
 			errx(EX_USAGE, "LBA format is out of range");
 		if (ms < 0)
diff --git a/sbin/nvmecontrol/identify.c b/sbin/nvmecontrol/identify.c
index d02ae51d72ee..509cb2be3f9f 100644
--- a/sbin/nvmecontrol/identify.c
+++ b/sbin/nvmecontrol/identify.c
@@ -69,7 +69,7 @@ print_namespace(struct nvme_namespace_data *nsdata)
 
 	thin_prov = NVMEV(NVME_NS_DATA_NSFEAT_THIN_PROV, nsdata->nsfeat);
 
-	flbas_fmt = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, nsdata->flbas);
+	flbas_fmt = nvme_ns_data_format_index(nsdata);
 
 	printf("Size:                        %lld blocks\n",
 	    (long long)nsdata->nsze);
diff --git a/sys/cam/nvme/nvme_da.c b/sys/cam/nvme/nvme_da.c
index 33efbc66773c..0e55501fc7ab 100644
--- a/sys/cam/nvme/nvme_da.c
+++ b/sys/cam/nvme/nvme_da.c
@@ -912,7 +912,7 @@ ndaregister(struct cam_periph *periph, void *arg)
 	else if (maxio > maxphys)
 		maxio = maxphys;	/* for safety */
 	disk->d_maxsize = maxio;
-	flbas_fmt = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, nsd->flbas);
+	flbas_fmt = nvme_ns_data_format_index(nsd);
 	lbads = NVMEV(NVME_NS_DATA_LBAF_LBADS, nsd->lbaf[flbas_fmt]);
 	disk->d_sectorsize = 1 << lbads;
 	disk->d_mediasize = (off_t)(disk->d_sectorsize * nsd->nsze);
diff --git a/sys/dev/nvme/nvme.h b/sys/dev/nvme/nvme.h
index 17a59d70d9fa..64b48da191e7 100644
--- a/sys/dev/nvme/nvme.h
+++ b/sys/dev/nvme/nvme.h
@@ -523,6 +523,11 @@ enum nvme_psdt {
 #define NVME_NS_DATA_FLBAS_FORMAT_MASK			(0xF)
 #define NVME_NS_DATA_FLBAS_EXTENDED_SHIFT		(4)
 #define NVME_NS_DATA_FLBAS_EXTENDED_MASK		(0x1)
+#define NVME_NS_DATA_FLBAS_FORMAT_MSB_SHIFT		(5)
+#define NVME_NS_DATA_FLBAS_FORMAT_MSB_MASK		(0x3)
+/* FIDXL width, and the format count below which FIDXU is reserved. */
+#define NVME_NS_DATA_FLBAS_FIDXL_BITS			(4)
+#define NVME_NS_DATA_LBAF_BASE_COUNT			(16)
 
 /** metadata capabilities */
 /* metadata can be transferred as part of data prp list */
@@ -2002,6 +2007,19 @@ extern int nvme_use_nvd;
 
 #endif /* _KERNEL */
 
+static inline uint8_t
+nvme_ns_data_format_index(const struct nvme_namespace_data *nsdata)
+{
+	uint8_t fmt;
+
+	fmt = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, nsdata->flbas);
+	/* FIDXU is valid only above the base count; NLBAF is 0's based. */
+	if (nsdata->nlbaf + 1 > NVME_NS_DATA_LBAF_BASE_COUNT)
+		fmt |= NVMEV(NVME_NS_DATA_FLBAS_FORMAT_MSB, nsdata->flbas) <<
+		    NVME_NS_DATA_FLBAS_FIDXL_BITS;
+	return (fmt);
+}
+
 /* Endianess conversion functions for NVMe structs */
 static inline
 void	nvme_completion_swapbytes(struct nvme_completion *s __unused)
diff --git a/sys/dev/nvme/nvme_ns.c b/sys/dev/nvme/nvme_ns.c
index 2f5170a59994..3ea956211dca 100644
--- a/sys/dev/nvme/nvme_ns.c
+++ b/sys/dev/nvme/nvme_ns.c
@@ -186,7 +186,7 @@ nvme_ns_get_sector_size(struct nvme_namespace *ns)
 {
 	uint8_t flbas_fmt, lbads;
 
-	flbas_fmt = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, ns->data.flbas);
+	flbas_fmt = nvme_ns_data_format_index(&ns->data);
 	lbads = NVMEV(NVME_NS_DATA_LBAF_LBADS, ns->data.lbaf[flbas_fmt]);
 
 	return (1 << lbads);
@@ -555,12 +555,11 @@ nvme_ns_construct(struct nvme_namespace *ns, uint32_t id,
 	if (ns->data.nsze == 0)
 		return (ENXIO);
 
-	flbas_fmt = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, ns->data.flbas);
-
 	/*
 	 * Note: format is a 0-based value, so > is appropriate here,
 	 *  not >=.
 	 */
+	flbas_fmt = nvme_ns_data_format_index(&ns->data);
 	if (flbas_fmt > ns->data.nlbaf) {
 		nvme_printf(ctrlr,
 		    "lba format %d exceeds number supported (%d)\n",
diff --git a/usr.sbin/camdd/camdd.c b/usr.sbin/camdd/camdd.c
index 1f067113c562..3f36704646e3 100644
--- a/usr.sbin/camdd/camdd.c
+++ b/usr.sbin/camdd/camdd.c
@@ -1430,7 +1430,7 @@ camdd_probe_pass_nvme(struct cam_device *cam_dev, union ccb *ccb,
 
 	*maxsector = nsdata.nsze;
 	/* The LBA Data Size (LBADS) is reported as a power of 2 */
-	format = NVMEV(NVME_NS_DATA_FLBAS_FORMAT, nsdata.flbas);
+	format = nvme_ns_data_format_index(&nsdata);
 	lbads = NVMEV(NVME_NS_DATA_LBAF_LBADS, nsdata.lbaf[format]);
 	*block_len = 1 << lbads;