none
how to send nvme command RRS feed

  • Question

  • code:

    the “PhysicalDrive1” is nvme disk


    HANDLE handle = CreateFileA("\\\\.\\PhysicalDrive1",GENERIC_READ| GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
    if (handle != INVALID_HANDLE_VALUE)
    {
    return GetLastError();
    }

    PSTORAGE_PROTOCOL_COMMAND protocolCommand;
    int bufferLength = FIELD_OFFSET(STORAGE_PROTOCOL_COMMAND, Command) + STORAGE_PROTOCOL_COMMAND_LENGTH_NVME + sizeof(NVME_ERROR_INFO_LOG);
    char *buffer = (char *)malloc(sizeof(char) * bufferLength);

    ZeroMemory(buffer, bufferLength);
    protocolCommand = (PSTORAGE_PROTOCOL_COMMAND)buffer;

    protocolCommand->Version = STORAGE_PROTOCOL_STRUCTURE_VERSION;
    protocolCommand->Length = sizeof(STORAGE_PROTOCOL_COMMAND);
    protocolCommand->ProtocolType = ProtocolTypeNvme;
    protocolCommand->Flags = STORAGE_PROTOCOL_COMMAND_FLAG_ADAPTER_REQUEST;
    protocolCommand->CommandLength = STORAGE_PROTOCOL_COMMAND_LENGTH_NVME;
    protocolCommand->ErrorInfoLength = sizeof(NVME_ERROR_INFO_LOG);
    protocolCommand->DataFromDeviceTransferLength = 4096;
    protocolCommand->TimeOutValue = 10;
    protocolCommand->ErrorInfoOffset = FIELD_OFFSET(STORAGE_PROTOCOL_COMMAND, Command) + STORAGE_PROTOCOL_COMMAND_LENGTH_NVME;
    protocolCommand->DataFromDeviceBufferOffset = protocolCommand->ErrorInfoOffset + protocolCommand->ErrorInfoLength;
    protocolCommand->CommandSpecific = STORAGE_PROTOCOL_SPECIFIC_NVME_NVM_COMMAND;

    PNVME_COMMAND command = (PNVME_COMMAND)protocolCommand->Command;

    command->CDW0.OPC = NVME_NVM_COMMAND_FLUSH;


    //  
    // Send request down.  
    //  
    DWORD returnedLength = 0;
    bool result = DeviceIoControl(handle,
    IOCTL_STORAGE_PROTOCOL_COMMAND,
    buffer,
    bufferLength,
    buffer,
    bufferLength,
    &returnedLength,
    NULL
    );
    return GetLastError();

    the code can not send flush command to nvme disk? thanks ! how to send command?

    Thursday, December 21, 2017 10:53 AM

Answers

  • I can't verify what the Microsoft NVME driver will do, but I can state that various vendors drivers do act differently in this area, so if you are assuming this will generally work you are out of luck.   My general rule for NVME is trying to go into the management capabilities of the drive is not going to work.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    Thursday, January 11, 2018 3:30 PM

All replies

  • Hi - According to MS docs and other sites I've seen, I believe that the IOCTL_STORAGE_PROTOCOL_COMMAND can only be used for Vendor-Specific commands.

    My assumption is that if DeviceIOControl is returning 'invalid command', then this is true. I've seen other questions on this forum using that command and getting the same results.

    I have yet to find code samples of Admin and NVME commands, excluding storage query.

    Can someone from Microsoft verify this?

    Thursday, January 11, 2018 2:33 PM
  • I can't verify what the Microsoft NVME driver will do, but I can state that various vendors drivers do act differently in this area, so if you are assuming this will generally work you are out of luck.   My general rule for NVME is trying to go into the management capabilities of the drive is not going to work.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    Thursday, January 11, 2018 3:30 PM
  • Hi, You might want to refer https://docs.microsoft.com/en-us/windows-hardware/drivers/storage/stornvme-command-set-support

    -Vishal

    Tuesday, February 11, 2020 4:09 AM