imsm: retrieve nvme serial from sysfs
PATCH 1/1 imsm: retrieve nvme serial from sysfs
Don't rely on SCSI ioctl for reading NVMe serials - SCSI emulation for NVMe devices can be disabled in the kernel config. Instead, try to get a serial from /sys/block/nvme*/device/serial. If that fails for whatever reason (i.e. no such attribute in old kernels) - fall back to the SCSI method.
This also moves some SCSI-specific code from imsm_read_serial() to scsi_get_serial().
--- sg_io.c | 23 +++++++++++++++++++---- super-intel.c | 46 +++++++++++++++++++++++++++------------------- 2 files changed, 46 insertions(+), 23 deletions(-)
sg_io.c +19 -4
@@ -23,20 +23,35 @@int scsi_get_serial (int fd,void *buf,size_t buf_len) { -unsigned char inq_cmd[] = {INQUIRY ,1 ,0x80 ,0 , buf_len,0 }; +unsigned char rsp_buf[255 ]; +unsigned char inq_cmd[] = {INQUIRY ,1 ,0x80 ,0 ,sizeof (rsp_buf),0 };unsigned char sense[32 ];struct sg_io_hdr io_hdr; +int rv; +unsigned int rsp_len;memset (&io_hdr,0 ,sizeof (io_hdr)); io_hdr.interface_id ='S' ; io_hdr.cmdp = inq_cmd; io_hdr.cmd_len =sizeof (inq_cmd); - io_hdr.dxferp = buf; - io_hdr.dxfer_len = buf_len; + io_hdr.dxferp = rsp_buf; + io_hdr.dxfer_len =sizeof (rsp_buf); io_hdr.dxfer_direction =SG_DXFER_FROM_DEV ; io_hdr.sbp = sense; io_hdr.mx_sb_len =sizeof (sense); io_hdr.timeout =5000 ; -return ioctl (fd,SG_IO , &io_hdr); + rv =ioctl (fd,SG_IO , &io_hdr); + +if (rv) +return rv; + + rsp_len = rsp_buf[3 ]; + +if (!rsp_len || buf_len < rsp_len) +return -1 ; + +memcpy (buf, &rsp_buf[4 ], rsp_len); + +return 0 ; }
super-intel.c +27 -19
78 lines not on this page · raw message
Signature · 1 line
2.10.0
Quoted 9 lines · Artur Paszkiewicz
Don't rely on SCSI ioctl for reading NVMe serials - SCSI emulation for NVMe devices can be disabled in the kernel config. Instead, try to get a serial from
/sys/block/nvme*/device/serial. If that fails for whatever reason (i.e. no such attribute in old kernels) - fall back to the SCSI method.This also moves some SCSI-specific code from
imsm_read_serial()toscsi_get_serial().
Quoted 7 lines
Signed-off-by Artur Paszkiewicz <[email protected]>Reviewed-by Tomasz Majchrzak <[email protected]>Reviewed-by Alexey Obitotskiy <[email protected]>--- sg_io.c | 23 +++++++++++++++++++---- super-intel.c | 46 +++++++++++++++++++++++++++------------------- 2 files changed, 46 insertions(+), 23 deletions(-)
Hi Artur,
Looks quite reasonable - applied!
Thanks,
Jes