nvme: Abstract out function to obtain a disk ident string from cdata

This will permit sharing the code with nvmf(4).

Sponsored by:	Chelsio Communications
Differential Revision:	https://reviews.freebsd.org/D53338
This commit is contained in:
John Baldwin
2025-11-17 13:21:39 -05:00
parent 4fcc58afbf
commit 8d2a50bb38
4 changed files with 21 additions and 22 deletions
+19
View File
@@ -1910,6 +1910,7 @@ void nvme_sc_sbuf(const struct nvme_completion *cpl, struct sbuf *sbuf);
void nvme_strvis(uint8_t *dst, const uint8_t *src, int dstlen, int srclen); void nvme_strvis(uint8_t *dst, const uint8_t *src, int dstlen, int srclen);
#ifdef _KERNEL #ifdef _KERNEL
#include <sys/disk.h>
struct bio; struct bio;
struct thread; struct thread;
@@ -1995,6 +1996,24 @@ nvme_ctrlr_has_dataset_mgmt(const struct nvme_controller_data *cd)
return (NVMEV(NVME_CTRLR_DATA_ONCS_DSM, cd->oncs) != 0); return (NVMEV(NVME_CTRLR_DATA_ONCS_DSM, cd->oncs) != 0);
} }
/*
* Copy the NVME device's serial number to the provided buffer, which must be
* at least DISK_IDENT_SIZE bytes large.
*/
static inline void
nvme_cdata_get_disk_ident(const struct nvme_controller_data *cdata, uint8_t *sn)
{
_Static_assert(NVME_SERIAL_NUMBER_LENGTH < DISK_IDENT_SIZE,
"NVME serial number too big for disk ident");
memmove(sn, 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] = ' ';
}
}
/* Namespace helper functions */ /* Namespace helper functions */
uint32_t nvme_ns_get_max_io_xfer_size(struct nvme_namespace *ns); uint32_t nvme_ns_get_max_io_xfer_size(struct nvme_namespace *ns);
uint32_t nvme_ns_get_sector_size(struct nvme_namespace *ns); uint32_t nvme_ns_get_sector_size(struct nvme_namespace *ns);
+1 -20
View File
@@ -33,7 +33,6 @@
#include <sys/buf.h> #include <sys/buf.h>
#include <sys/bus.h> #include <sys/bus.h>
#include <sys/conf.h> #include <sys/conf.h>
#include <sys/disk.h>
#include <sys/ioccom.h> #include <sys/ioccom.h>
#include <sys/proc.h> #include <sys/proc.h>
#include <sys/smp.h> #include <sys/smp.h>
@@ -1254,24 +1253,6 @@ nvme_ctrlr_poll(struct nvme_controller *ctrlr)
nvme_qpair_process_completions(&ctrlr->ioq[i]); nvme_qpair_process_completions(&ctrlr->ioq[i]);
} }
/*
* Copy the NVME device's serial number to the provided buffer, which must be
* at least DISK_IDENT_SIZE bytes large.
*/
void
nvme_ctrlr_get_ident(const struct nvme_controller *ctrlr, uint8_t *sn)
{
_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] = ' ';
}
}
/* /*
* Poll the single-vector interrupt case: num_io_queues will be 1 and * Poll the single-vector interrupt case: num_io_queues will be 1 and
* there's only a single vector. While we're polling, we mask further * there's only a single vector. While we're polling, we mask further
@@ -1516,7 +1497,7 @@ nvme_ctrlr_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int flag,
break; break;
case DIOCGIDENT: { case DIOCGIDENT: {
uint8_t *sn = arg; uint8_t *sn = arg;
nvme_ctrlr_get_ident(ctrlr, sn); nvme_cdata_get_disk_ident(&ctrlr->cdata, sn);
break; break;
} }
/* Linux Compatible (see nvme_linux.h) */ /* Linux Compatible (see nvme_linux.h) */
+1 -1
View File
@@ -90,7 +90,7 @@ nvme_ns_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int flag,
} }
case DIOCGIDENT: { case DIOCGIDENT: {
uint8_t *sn = arg; uint8_t *sn = arg;
nvme_ctrlr_get_ident(ctrlr, sn); nvme_cdata_get_disk_ident(&ctrlr->cdata, sn);
break; break;
} }
case DIOCGMEDIASIZE: case DIOCGMEDIASIZE:
-1
View File
@@ -565,7 +565,6 @@ void nvme_notify_new_controller(struct nvme_controller *ctrlr);
void nvme_notify_ns(struct nvme_controller *ctrlr, int nsid); void nvme_notify_ns(struct nvme_controller *ctrlr, int nsid);
void nvme_ctrlr_shared_handler(void *arg); void nvme_ctrlr_shared_handler(void *arg);
void nvme_ctrlr_get_ident(const struct nvme_controller *ctrlr, uint8_t *sn);
void nvme_ctrlr_poll(struct nvme_controller *ctrlr); void nvme_ctrlr_poll(struct nvme_controller *ctrlr);
int nvme_ctrlr_suspend(struct nvme_controller *ctrlr); int nvme_ctrlr_suspend(struct nvme_controller *ctrlr);