0

I am trying to use the sed-opal libraries added in the linux-kernel 4.11

To do that, i have written a basic program like this:

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <linux/nvme_ioctl.h>
#include <uapi/linux/sed-opal.h>
#include <fcntl.h>
#include <linux/types.h>

int main()
{
    int f_desc;

    printf("Trying to open device /dev/nvme0...\n");

    f_desc = open("/dev/nvme0", O_RDWR);
    if(f_desc == 0)
    {
            printf("Could not open the file..\n");
    }
    else
    {
            printf("Open call returns : %d\n", f_desc);
    }


    struct nvme_admin_cmd cmd = {
    .opcode = 0x81,                 //0x81 Security Send. 0x82 Security Receive
    .nsid   = 0xffffffff,
    .cdw10  = 0x02000000,
    };

    int ret_ioctl_value;

    ret_ioctl_value = ioctl(f_desc, IOC_OPAL_REVERT_TPR, &cmd);

    printf("ioctl returned value : %d \n", ret_ioctl_value);
    perror("Flush");
    close(f_desc);

}

Problem comes when i want to use the sed-opal library ioctl commands as mentioned in the code above.

Error is like:-
Trying to open device /dev/nvme0...
Open call returns : 3
ioctl returned value : -1
Flush: Inappropriate ioctl for device

Is this the correct way to send the Security SEND/RECV commands?

If yes, what could be the possible cause of the error?

Any help is appreciated. :)

Thanks.

Note:
- With the above code the basic NVMe protocol specific ADMIN commands works and i am able to receive response of a SMART health log page from the device.
- I have built /usr/src/linux/block/ directory by Enabling the  "Logic for interfacing with OPAL enabled SEDs" from the menuconfig.
- Using 4.13.9 kernel level right now.

======================================================

Edit:
So it is supposed to be done as follows:
open(/dev/nvme0n1");
set up opal_structures here from /uapi/linux/sed-opal.h
ioctl(fd, IOC_OPAL_*, &struct_from_above);

1 Answers1

0

Try using sed_ioctl instead of ioctl. which is defined in 'sed-opal.h'

syntax is simmilar.

int sed_ioctl(struct opal_dev *dev, unsigned int cmd, void __user *ioctl_ptr);

where as ioctl is.

int ioctl(int fd, unsigned long request, ...);
Devidas
  • 2,479
  • 9
  • 24
  • can we use sed_ioctl directly in userspace program?? – user3453931 Oct 30 '17 at 18:10
  • yes, we can use it in userspace. definition is in sed-opal.h which you have included. give it a try and reply. – Devidas Nov 02 '17 at 04:57
  • I have added the sed-opal from uapi/linux/sed-opal.h. The sed_ioctl you are mentioning is from /linux/sed-opal.h and exposed to be able to use from other kernel modules. Please could you suggest with an example? – user3453931 Nov 02 '17 at 10:22
  • My mistake. Above answer is completely wrong. I think root cause for this error is received -ENOTTY which is default return value for sed_ioctl if no command is found. check (this)[http://elixir.free-electrons.com/linux/v4.13.9/source/block/sed-opal.c#L2354]. Main cause is is command `IOC_OPAL_REVERT_TPR` not a switch case member. so dig in some try to check strings on driver to check whether it has this string. – Devidas Nov 03 '17 at 07:35
  • could you please post your solution ? It may help some one in future. – Devidas Nov 05 '17 at 17:32