2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright 2016 Nexenta Systems, Inc.
16 #include <sys/types.h>
22 #include <libdevinfo.h>
30 nvme_ioctl(int fd
, int ioc
, size_t *bufsize
, void **buf
, uint64_t arg
,
33 nvme_ioctl_t nioc
= { 0 };
41 if (bufsize
!= NULL
&& *bufsize
!= 0) {
44 if ((nioc
.n_buf
= (uintptr_t)calloc(*bufsize
, 1)) == 0)
45 err(-1, "nvme_ioctl()");
47 nioc
.n_len
= *bufsize
;
52 if (ioctl(fd
, ioc
, &nioc
) != 0) {
56 free((void *)nioc
.n_buf
);
65 *bufsize
= nioc
.n_len
;
68 *buf
= (void *)nioc
.n_buf
;
74 nvme_capabilities(int fd
)
77 size_t bufsize
= sizeof (nvme_capabilities_t
);
79 (void) nvme_ioctl(fd
, NVME_IOC_CAPABILITIES
, &bufsize
, &cap
, 0, NULL
);
88 size_t bufsize
= sizeof (nvme_version_t
);
90 (void) nvme_ioctl(fd
, NVME_IOC_VERSION
, &bufsize
, &vs
, 0, NULL
);
95 nvme_identify_ctrl_t
*
96 nvme_identify_ctrl(int fd
)
99 size_t bufsize
= NVME_IDENTIFY_BUFSIZE
;
101 (void) nvme_ioctl(fd
, NVME_IOC_IDENTIFY_CTRL
, &bufsize
, &idctl
, 0,
107 nvme_identify_nsid_t
*
108 nvme_identify_nsid(int fd
)
111 size_t bufsize
= NVME_IDENTIFY_BUFSIZE
;
113 (void) nvme_ioctl(fd
, NVME_IOC_IDENTIFY_NSID
, &bufsize
, &idns
, 0, NULL
);
119 nvme_get_logpage(int fd
, uint8_t logpage
, size_t *bufsize
)
123 (void) nvme_ioctl(fd
, NVME_IOC_GET_LOGPAGE
, bufsize
, &buf
, logpage
,
130 nvme_get_feature(int fd
, uint8_t feature
, uint32_t arg
, uint64_t *res
,
131 size_t *bufsize
, void **buf
)
133 return (nvme_ioctl(fd
, NVME_IOC_GET_FEATURES
, bufsize
, buf
,
134 (uint64_t)feature
<< 32 | arg
, res
));
138 nvme_intr_cnt(int fd
)
142 (void) nvme_ioctl(fd
, NVME_IOC_INTR_CNT
, NULL
, NULL
, 0, &res
);
147 nvme_format_nvm(int fd
, uint8_t lbaf
, uint8_t ses
)
149 nvme_format_nvm_t frmt
= { 0 };
151 frmt
.b
.fm_lbaf
= lbaf
& 0xf;
152 frmt
.b
.fm_ses
= ses
& 0x7;
154 return (nvme_ioctl(fd
, NVME_IOC_FORMAT
, NULL
, NULL
, frmt
.r
, NULL
));
160 return (nvme_ioctl(fd
, NVME_IOC_DETACH
, NULL
, NULL
, 0, NULL
));
166 return (nvme_ioctl(fd
, NVME_IOC_ATTACH
, NULL
, NULL
, 0, NULL
));
170 nvme_open(di_minor_t minor
)
172 char *devpath
, *path
;
175 if ((devpath
= di_devfs_minor_path(minor
)) == NULL
)
176 err(-1, "nvme_open()");
178 if (asprintf(&path
, "/devices%s", devpath
) < 0) {
179 di_devfs_path_free(devpath
);
180 err(-1, "nvme_open()");
183 di_devfs_path_free(devpath
);
185 fd
= open(path
, O_RDWR
);
190 warn("nvme_open(%s)", path
);