EFiore
EFiore

Reputation: 115

NVMe drive model data through ioctl

Is there a way to retrieve the model number of a NVME drive through a ioctl function call? This is possible for a IDE drive by using the hd_driveid struct defined in /include/linux/hdreg.h.

hdreg.h

struct hd_driveid{
...
        unsigned short  ecc_bytes;      
        unsigned char   fw_rev[8];      
        unsigned char   model[40];      /*see here*/
        unsigned char   max_multsect;   
...
}

I do not see a similar method for retrieving the model of a NVMe drive in /include/linux/nvme_ioctl.h

I fear

Upvotes: 2

Views: 1377

Answers (1)

James Risner
James Risner

Reputation: 6094

Looking at the section 5.15.2.2 of the 1.4 specification. This should work. I didn't have an NVMe drive to test.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <linux/nvme_ioctl.h>

int main(int argc, char** argv) {
    int fd = open(argv[1], O_RDWR);
    if (fd < 0) {
        perror("open: ");
        exit(1);
    }

    char buf[4096] = {0};
    struct nvme_admin_cmd mib = {0};
    mib.opcode = 0x06; // identify
    mib.nsid = 0;
    mib.addr = (__u64) buf;
    mib.data_len = sizeof(buf);
    mib.cdw10 = 1; // controller

    int ret = ioctl(fd, NVME_IOCTL_ADMIN_CMD, &mib);
    if (ret) {
        perror("ioctl: ");
        exit(1);
    }

    printf("SN: %.20s\n", &buf[4]);
    printf("SN: %.40s\n", &buf[24]);
    printf("FW: %.8s\n", &buf[64]);
    return 0;
}

Upvotes: 1

Related Questions