The standard way to get a disk's serial number is with the DIOCGIDENT ioctl. However, /dev/nvmeX devices don't support that, probably because they aren't technically disks. An NVME device's corresponding nda driver _does_. However, it may be difficult to determine which nda device corresponds to a given nvme device, if NVME namespaces are in use. Within the kernel, the nvme driver already knows its serial number. It's recorded in nvme_controller_data.sn . But it simply isn't exposed to user space. I propose that we implement DIOCGIDENT on nvme devices. Or, if that name is objectionable we could use NVME_IOCTL_SERIAL or NVME_IOCTL_IDENT. This would allow a user to lookup an NVME device's serial number without first translating the nvme device node into an nda device node.
I think that somebody within my organization will be able to implement this later this Fall. So I'll self-assign it for now. But if anybody else it champing at the bit, you may steal the bug from me.
So nvme -> nda is one to many, since namespaces are a thing. And nobody deals with nvmeX devices for anything but management... Except, well, lofs of people do management and use the nvmeX device because ndaX didn't used to work. ``` sudo nvmecontrol identify nvme0 | grep Serial Serial Number: S2UDNX0J602857 ``` works today, though. I'm cool with this. In fact, it's super easy to implement. diff --git a/sys/dev/nvme/nvme_ctrlr.c b/sys/dev/nvme/nvme_ctrlr.c index 3a1894bf754d..02aac360febc 100644 --- a/sys/dev/nvme/nvme_ctrlr.c +++ b/sys/dev/nvme/nvme_ctrlr.c @@ -33,6 +33,7 @@ #include <sys/buf.h> #include <sys/bus.h> #include <sys/conf.h> +#include <sys/disk.h> #include <sys/ioccom.h> #include <sys/proc.h> #include <sys/smp.h> @@ -1495,6 +1496,19 @@ nvme_ctrlr_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int flag, case NVME_GET_CONTROLLER_DATA: memcpy(arg, &ctrlr->cdata, sizeof(ctrlr->cdata)); break; + case DIOCGIDENT: { + uint8_t *sn = arg; + _Static_assert(NVME_SERIAL_NUMBER_LENGTH < DISK_IDENT_SIZE, + "NVME serial number too big for disk ident"); + + memmove(sn, ctrlr->cdata.sn, NVME_SERIAL_NUMBER_LENGTH); + sn[NVME_SERIAL_NUMBER_LENGTH] = '\0'; + for (int i = 0; sn[i] != '\0'; i++) { + if (sn[i] < 0x20 || sn[i] >= 0x80) + sn[i] = ' '; + } + break; + } /* Linux Compatible (see nvme_linux.h) */ case NVME_IOCTL_ID: td->td_retval[0] = 0xfffffffful; diff --git a/sys/dev/nvme/nvme_ns.c b/sys/dev/nvme/nvme_ns.c index e84d2066930e..9ce9e3227a56 100644 --- a/sys/dev/nvme/nvme_ns.c +++ b/sys/dev/nvme/nvme_ns.c @@ -94,6 +94,20 @@ nvme_ns_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int flag, case DIOCGSECTORSIZE: *(u_int *)arg = nvme_ns_get_sector_size(ns); break; + case DIOCGIDENT: { + uint8_t *sn = arg; + _Static_assert(NVME_SERIAL_NUMBER_LENGTH < DISK_IDENT_SIZE, + "NVME serial number too big for disk ident"); + + memmove(sn, nvme_ns_get_serial_number(ns), + NVME_SERIAL_NUMBER_LENGTH); + sn[NVME_SERIAL_NUMBER_LENGTH] = '\0'; + for (int i = 0; sn[i] != '\0'; i++) { + if (sn[i] < 0x20 || sn[i] >= 0x80) + sn[i] = ' '; + } + break; + } /* Linux Compatible (see nvme_linux.h) */ case NVME_IOCTL_ID: td->td_retval[0] = ns->id;
(yes, that should be refactored, it's presented here for testing purposes)
I was hoping to save this task as an exercise for a new hire. But since you've already gone and done it, I suppose I'll test and review it now.
Doh... I've been fighting this driver and needed a win to start my day.
A commit in branch main references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=6d0001d44490becdd20d627ce663c72a30b9aac3 commit 6d0001d44490becdd20d627ce663c72a30b9aac3 Author: Alan Somers <asomers@FreeBSD.org> AuthorDate: 2025-10-16 22:00:14 +0000 Commit: Alan Somers <asomers@FreeBSD.org> CommitDate: 2025-10-22 15:39:35 +0000 nvme: add support for DIOCGIDENT Add support for the DIOCGIDENT ioctl to both nvme controller device nodes and namespace device nodes. This information was already available via the nda(4) device node. However, mapping /dev/nvmeX to /dev/ndaY device nodes is not straightforward, so it's better to get it directly from the /dev/nvme device node. PR: 290259 MFC after: 2 weeks Sponsored by: ConnectWise Submitted by: imp (mostly) Pull Request: https://github.com/freebsd/freebsd-src/pull/1875 sys/dev/nvme/nvme_ctrlr.c | 24 ++++++++++++++++++++++++ sys/dev/nvme/nvme_ns.c | 5 +++++ sys/dev/nvme/nvme_private.h | 1 + 3 files changed, 30 insertions(+)
A commit in branch stable/14 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=465c30c4f202b02cd9cb12f12d9ea856c84c5203 commit 465c30c4f202b02cd9cb12f12d9ea856c84c5203 Author: Alan Somers <asomers@FreeBSD.org> AuthorDate: 2025-10-16 22:00:14 +0000 Commit: Alan Somers <asomers@FreeBSD.org> CommitDate: 2025-11-22 14:59:24 +0000 nvme: add support for DIOCGIDENT Add support for the DIOCGIDENT ioctl to both nvme controller device nodes and namespace device nodes. This information was already available via the nda(4) device node. However, mapping /dev/nvmeX to /dev/ndaY device nodes is not straightforward, so it's better to get it directly from the /dev/nvme device node. PR: 290259 Sponsored by: ConnectWise Submitted by: imp (mostly) Pull Request: https://github.com/freebsd/freebsd-src/pull/1875 (cherry picked from commit 6d0001d44490becdd20d627ce663c72a30b9aac3) sys/dev/nvme/nvme_ctrlr.c | 24 ++++++++++++++++++++++++ sys/dev/nvme/nvme_ns.c | 5 +++++ sys/dev/nvme/nvme_private.h | 1 + 3 files changed, 30 insertions(+)
A commit in branch stable/15 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=1b5cc3c48cb8ff80f0b6a75981d5f85e87cf7e8e commit 1b5cc3c48cb8ff80f0b6a75981d5f85e87cf7e8e Author: Alan Somers <asomers@FreeBSD.org> AuthorDate: 2025-10-16 22:00:14 +0000 Commit: Alan Somers <asomers@FreeBSD.org> CommitDate: 2025-11-22 17:54:42 +0000 nvme: add support for DIOCGIDENT Add support for the DIOCGIDENT ioctl to both nvme controller device nodes and namespace device nodes. This information was already available via the nda(4) device node. However, mapping /dev/nvmeX to /dev/ndaY device nodes is not straightforward, so it's better to get it directly from the /dev/nvme device node. PR: 290259 Sponsored by: ConnectWise Submitted by: imp (mostly) Pull Request: https://github.com/freebsd/freebsd-src/pull/1875 (cherry picked from commit 6d0001d44490becdd20d627ce663c72a30b9aac3) sys/dev/nvme/nvme_ctrlr.c | 24 ++++++++++++++++++++++++ sys/dev/nvme/nvme_ns.c | 5 +++++ sys/dev/nvme/nvme_private.h | 1 + 3 files changed, 30 insertions(+)