| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/* |
| 3 |
* This file is part of libnvme. |
| 4 |
* Copyright (c) 2020 Western Digital Corporation or its affiliates. |
| 5 |
* |
| 6 |
* Authors: Keith Busch <[email protected]> |
| 7 |
* Chaitanya Kulkarni <[email protected]> |
| 8 |
*/ |
| 9 |
#include <errno.h> |
| 10 |
#include <fcntl.h> |
| 11 |
#ifdef CONFIG_LIBURING |
| 12 |
#include <liburing.h> |
| 13 |
#endif |
| 14 |
#include <stdint.h> |
| 15 |
#include <stdio.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
#include <unistd.h> |
| 19 |
#include <inttypes.h> |
| 20 |
|
| 21 |
#include <sys/ioctl.h> |
| 22 |
#include <sys/stat.h> |
| 23 |
#include <sys/time.h> |
| 24 |
|
| 25 |
#include <ccan/build_assert/build_assert.h> |
| 26 |
#include <ccan/ccan/minmax/minmax.h> |
| 27 |
#include <ccan/endian/endian.h> |
| 28 |
|
| 29 |
#include "ioctl.h" |
| 30 |
#include "util.h" |
| 31 |
#include "log.h" |
| 32 |
|
| 33 |
static int nvme_verify_chr(int fd) |
| 34 |
{ |
| 35 |
static struct stat nvme_stat; |
| 36 |
int err = fstat(fd, &nvme_stat); |
| 37 |
|
| 38 |
if (err < 0) |
| 39 |
return errno; |
| 40 |
|
| 41 |
if (!S_ISCHR(nvme_stat.st_mode)) { |
| 42 |
errno = ENOTBLK; |
| 43 |
return -1; |
| 44 |
} |
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
int nvme_subsystem_reset(int fd) |
| 49 |
{ |
| 50 |
int ret; |
| 51 |
|
| 52 |
ret = nvme_verify_chr(fd); |
| 53 |
if (ret) |
| 54 |
return ret; |
| 55 |
return ioctl(fd, NVME_IOCTL_SUBSYS_RESET); |
| 56 |
} |
| 57 |
|
| 58 |
int nvme_ctrl_reset(int fd) |
| 59 |
{ |
| 60 |
int ret; |
| 61 |
|
| 62 |
ret = nvme_verify_chr(fd); |
| 63 |
if (ret) |
| 64 |
return ret; |
| 65 |
return ioctl(fd, NVME_IOCTL_RESET); |
| 66 |
} |
| 67 |
|
| 68 |
int nvme_ns_rescan(int fd) |
| 69 |
{ |
| 70 |
int ret; |
| 71 |
|
| 72 |
ret = nvme_verify_chr(fd); |
| 73 |
if (ret) |
| 74 |
return ret; |
| 75 |
return ioctl(fd, NVME_IOCTL_RESCAN); |
| 76 |
} |
| 77 |
|
| 78 |
int nvme_get_nsid(int fd, __u32 *nsid) |
| 79 |
{ |
| 80 |
errno = 0; |
| 81 |
*nsid = ioctl(fd, NVME_IOCTL_ID); |
| 82 |
return -1 * (errno != 0); |
| 83 |
} |
| 84 |
|
| 85 |
__attribute__((weak)) |
| 86 |
int nvme_submit_passthru64(int fd, unsigned long ioctl_cmd, |
| 87 |
struct nvme_passthru_cmd64 *cmd, |
| 88 |
__u64 *result) |
| 89 |
{ |
| 90 |
int err = ioctl(fd, ioctl_cmd, cmd); |
| 91 |
|
| 92 |
if (err >= 0 && result) |
| 93 |
*result = cmd->result; |
| 94 |
return err; |
| 95 |
} |
| 96 |
|
| 97 |
__attribute__((weak)) |
| 98 |
int nvme_submit_passthru(int fd, unsigned long ioctl_cmd, |
| 99 |
struct nvme_passthru_cmd *cmd, __u32 *result) |
| 100 |
{ |
| 101 |
int err = ioctl(fd, ioctl_cmd, cmd); |
| 102 |
|
| 103 |
if (err >= 0 && result) |
| 104 |
*result = cmd->result; |
| 105 |
return err; |
| 106 |
} |
| 107 |
|
| 108 |
static int nvme_passthru64(int fd, unsigned long ioctl_cmd, __u8 opcode, |
| 109 |
__u8 flags, __u16 rsvd, __u32 nsid, __u32 cdw2, |
| 110 |
__u32 cdw3, __u32 cdw10, __u32 cdw11, __u32 cdw12, |
| 111 |
__u32 cdw13, __u32 cdw14, __u32 cdw15, |
| 112 |
__u32 data_len, void *data, __u32 metadata_len, |
| 113 |
void *metadata, __u32 timeout_ms, __u64 *result) |
| 114 |
{ |
| 115 |
struct nvme_passthru_cmd64 cmd = { |
| 116 |
.opcode = opcode, |
| 117 |
.flags = flags, |
| 118 |
.rsvd1 = rsvd, |
| 119 |
.nsid = nsid, |
| 120 |
.cdw2 = cdw2, |
| 121 |
.cdw3 = cdw3, |
| 122 |
.metadata = (__u64)(uintptr_t)metadata, |
| 123 |
.addr = (__u64)(uintptr_t)data, |
| 124 |
.metadata_len = metadata_len, |
| 125 |
.data_len = data_len, |
| 126 |
.cdw10 = cdw10, |
| 127 |
.cdw11 = cdw11, |
| 128 |
.cdw12 = cdw12, |
| 129 |
.cdw13 = cdw13, |
| 130 |
.cdw14 = cdw14, |
| 131 |
.cdw15 = cdw15, |
| 132 |
.timeout_ms = timeout_ms, |
| 133 |
}; |
| 134 |
|
| 135 |
return nvme_submit_passthru64(fd, ioctl_cmd, &cmd, result); |
| 136 |
} |
| 137 |
|
| 138 |
static int nvme_passthru(int fd, unsigned long ioctl_cmd, __u8 opcode, |
| 139 |
__u8 flags, __u16 rsvd, __u32 nsid, __u32 cdw2, |
| 140 |
__u32 cdw3, __u32 cdw10, __u32 cdw11, __u32 cdw12, |
| 141 |
__u32 cdw13, __u32 cdw14, __u32 cdw15, __u32 data_len, |
| 142 |
void *data, __u32 metadata_len, void *metadata, |
| 143 |
__u32 timeout_ms, __u32 *result) |
| 144 |
{ |
| 145 |
struct nvme_passthru_cmd cmd = { |
| 146 |
.opcode = opcode, |
| 147 |
.flags = flags, |
| 148 |
.rsvd1 = rsvd, |
| 149 |
.nsid = nsid, |
| 150 |
.cdw2 = cdw2, |
| 151 |
.cdw3 = cdw3, |
| 152 |
.metadata = (__u64)(uintptr_t)metadata, |
| 153 |
.addr = (__u64)(uintptr_t)data, |
| 154 |
.metadata_len = metadata_len, |
| 155 |
.data_len = data_len, |
| 156 |
.cdw10 = cdw10, |
| 157 |
.cdw11 = cdw11, |
| 158 |
.cdw12 = cdw12, |
| 159 |
.cdw13 = cdw13, |
| 160 |
.cdw14 = cdw14, |
| 161 |
.cdw15 = cdw15, |
| 162 |
.timeout_ms = timeout_ms, |
| 163 |
}; |
| 164 |
|
| 165 |
return nvme_submit_passthru(fd, ioctl_cmd, &cmd, result); |
| 166 |
} |
| 167 |
|
| 168 |
int nvme_submit_admin_passthru64(int fd, struct nvme_passthru_cmd64 *cmd, |
| 169 |
__u64 *result) |
| 170 |
{ |
| 171 |
return nvme_submit_passthru64(fd, NVME_IOCTL_ADMIN64_CMD, cmd, result); |
| 172 |
} |
| 173 |
|
| 174 |
int nvme_admin_passthru64(int fd, __u8 opcode, __u8 flags, __u16 rsvd, |
| 175 |
__u32 nsid, __u32 cdw2, __u32 cdw3, __u32 cdw10, |
| 176 |
__u32 cdw11, __u32 cdw12, __u32 cdw13, __u32 cdw14, |
| 177 |
__u32 cdw15, __u32 data_len, void *data, |
| 178 |
__u32 metadata_len, void *metadata, __u32 timeout_ms, |
| 179 |
__u64 *result) |
| 180 |
{ |
| 181 |
return nvme_passthru64(fd, NVME_IOCTL_ADMIN64_CMD, opcode, flags, rsvd, |
| 182 |
nsid, cdw2, cdw3, cdw10, cdw11, cdw12, cdw13, |
| 183 |
cdw14, cdw15, data_len, data, metadata_len, |
| 184 |
metadata, timeout_ms, result); |
| 185 |
} |
| 186 |
|
| 187 |
int nvme_submit_admin_passthru(int fd, struct nvme_passthru_cmd *cmd, __u32 *result) |
| 188 |
{ |
| 189 |
return nvme_submit_passthru(fd, NVME_IOCTL_ADMIN_CMD, cmd, result); |
| 190 |
} |
| 191 |
|
| 192 |
int nvme_admin_passthru(int fd, __u8 opcode, __u8 flags, __u16 rsvd, |
| 193 |
__u32 nsid, __u32 cdw2, __u32 cdw3, __u32 cdw10, |
| 194 |
__u32 cdw11, __u32 cdw12, __u32 cdw13, __u32 cdw14, |
| 195 |
__u32 cdw15, __u32 data_len, void *data, |
| 196 |
__u32 metadata_len, void *metadata, __u32 timeout_ms, |
| 197 |
__u32 *result) |
| 198 |
{ |
| 199 |
return nvme_passthru(fd, NVME_IOCTL_ADMIN_CMD, opcode, flags, rsvd, |
| 200 |
nsid, cdw2, cdw3, cdw10, cdw11, cdw12, cdw13, |
| 201 |
cdw14, cdw15, data_len, data, metadata_len, |
| 202 |
metadata, timeout_ms, result); |
| 203 |
} |
| 204 |
|
| 205 |
enum features { |
| 206 |
NVME_FEATURES_ARBITRATION_BURST_SHIFT = 0, |
| 207 |
NVME_FEATURES_ARBITRATION_LPW_SHIFT = 8, |
| 208 |
NVME_FEATURES_ARBITRATION_MPW_SHIFT = 16, |
| 209 |
NVME_FEATURES_ARBITRATION_HPW_SHIFT = 24, |
| 210 |
NVME_FEATURES_ARBITRATION_BURST_MASK = 0x7, |
| 211 |
NVME_FEATURES_ARBITRATION_LPW_MASK = 0xff, |
| 212 |
NVME_FEATURES_ARBITRATION_MPW_MASK = 0xff, |
| 213 |
NVME_FEATURES_ARBITRATION_HPW_MASK = 0xff, |
| 214 |
NVME_FEATURES_PWRMGMT_PS_SHIFT = 0, |
| 215 |
NVME_FEATURES_PWRMGMT_WH_SHIFT = 5, |
| 216 |
NVME_FEATURES_PWRMGMT_PS_MASK = 0x1f, |
| 217 |
NVME_FEATURES_PWRMGMT_WH_MASK = 0x7, |
| 218 |
NVME_FEATURES_TMPTH_SHIFT = 0, |
| 219 |
NVME_FEATURES_TMPSEL_SHIFT = 16, |
| 220 |
NVME_FEATURES_THSEL_SHIFT = 20, |
| 221 |
NVME_FEATURES_TMPTH_MASK = 0xff, |
| 222 |
NVME_FEATURES_TMPSEL_MASK = 0xf, |
| 223 |
NVME_FEATURES_THSEL_MASK = 0x3, |
| 224 |
NVME_FEATURES_ERROR_RECOVERY_TLER_SHIFT = 0, |
| 225 |
NVME_FEATURES_ERROR_RECOVERY_DULBE_SHIFT = 16, |
| 226 |
NVME_FEATURES_ERROR_RECOVERY_TLER_MASK = 0xff, |
| 227 |
NVME_FEATURES_ERROR_RECOVERY_DULBE_MASK = 0x1, |
| 228 |
NVME_FEATURES_VWC_WCE_SHIFT = 0, |
| 229 |
NVME_FEATURES_VWC_WCE_MASK = 0x1, |
| 230 |
NVME_FEATURES_IRQC_THR_SHIFT = 0, |
| 231 |
NVME_FEATURES_IRQC_TIME_SHIFT = 8, |
| 232 |
NVME_FEATURES_IRQC_THR_MASK = 0xff, |
| 233 |
NVME_FEATURES_IRQC_TIME_MASK = 0xff, |
| 234 |
NVME_FEATURES_IVC_IV_SHIFT = 0, |
| 235 |
NVME_FEATURES_IVC_CD_SHIFT = 16, |
| 236 |
NVME_FEATURES_IVC_IV_MASK = 0xffff, |
| 237 |
NVME_FEATURES_IVC_CD_MASK = 0x1, |
| 238 |
NVME_FEATURES_WAN_DN_SHIFT = 0, |
| 239 |
NVME_FEATURES_WAN_DN_MASK = 0x1, |
| 240 |
NVME_FEATURES_APST_APSTE_SHIFT = 0, |
| 241 |
NVME_FEATURES_APST_APSTE_MASK = 0x1, |
| 242 |
NVME_FEATURES_HCTM_TMT2_SHIFT = 0, |
| 243 |
NVME_FEATURES_HCTM_TMT1_SHIFT = 16, |
| 244 |
NVME_FEATURES_HCTM_TMT2_MASK = 0xffff, |
| 245 |
NVME_FEATURES_HCTM_TMT1_MASK = 0xffff, |
| 246 |
NVME_FEATURES_NOPS_NOPPME_SHIFT = 0, |
| 247 |
NVME_FEATURES_NOPS_NOPPME_MASK = 0x1, |
| 248 |
NVME_FEATURES_PLM_PLE_SHIFT = 0, |
| 249 |
NVME_FEATURES_PLM_PLE_MASK = 0x1, |
| 250 |
NVME_FEATURES_PLM_WINDOW_SELECT_SHIFT = 0, |
| 251 |
NVME_FEATURES_PLM_WINDOW_SELECT_MASK = 0xf, |
| 252 |
NVME_FEATURES_LBAS_LSIRI_SHIFT = 0, |
| 253 |
NVME_FEATURES_LBAS_LSIPI_SHIFT = 16, |
| 254 |
NVME_FEATURES_LBAS_LSIRI_MASK = 0xffff, |
| 255 |
NVME_FEATURES_LBAS_LSIPI_MASK = 0xffff, |
| 256 |
NVME_FEATURES_IOCSP_IOCSCI_SHIFT = 0, |
| 257 |
NVME_FEATURES_IOCSP_IOCSCI_MASK = 0xff, |
| 258 |
}; |
| 259 |
|
| 260 |
int nvme_identify(struct nvme_identify_args *args) |
| 261 |
{ |
| 262 |
__u32 cdw10 = NVME_SET(args->cntid, IDENTIFY_CDW10_CNTID) | |
| 263 |
NVME_SET(args->cns, IDENTIFY_CDW10_CNS); |
| 264 |
__u32 cdw11 = NVME_SET(args->cns_specific_id, IDENTIFY_CDW11_CNSSPECID) | |
| 265 |
NVME_SET(args->csi, IDENTIFY_CDW11_CSI); |
| 266 |
__u32 cdw14 = NVME_SET(args->uuidx, IDENTIFY_CDW14_UUID); |
| 267 |
|
| 268 |
struct nvme_passthru_cmd cmd = { |
| 269 |
.opcode = nvme_admin_identify, |
| 270 |
.nsid = args->nsid, |
| 271 |
.addr = (__u64)(uintptr_t)args->data, |
| 272 |
.data_len = NVME_IDENTIFY_DATA_SIZE, |
| 273 |
.cdw10 = cdw10, |
| 274 |
.cdw11 = cdw11, |
| 275 |
.cdw14 = cdw14, |
| 276 |
.timeout_ms = args->timeout, |
| 277 |
}; |
| 278 |
|
| 279 |
if (args->args_size < sizeof(*args)) { |
| 280 |
errno = EINVAL; |
| 281 |
return -1; |
| 282 |
} |
| 283 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 284 |
} |
| 285 |
|
| 286 |
int nvme_get_log(struct nvme_get_log_args *args) |
| 287 |
{ |
| 288 |
__u32 numd = (args->len >> 2) - 1; |
| 289 |
__u16 numdu = numd >> 16, numdl = numd & 0xffff; |
| 290 |
|
| 291 |
__u32 cdw10 = NVME_SET(args->lid, LOG_CDW10_LID) | |
| 292 |
NVME_SET(args->lsp, LOG_CDW10_LSP) | |
| 293 |
NVME_SET(!!args->rae, LOG_CDW10_RAE) | |
| 294 |
NVME_SET(numdl, LOG_CDW10_NUMDL); |
| 295 |
__u32 cdw11 = NVME_SET(numdu, LOG_CDW11_NUMDU) | |
| 296 |
NVME_SET(args->lsi, LOG_CDW11_LSI); |
| 297 |
__u32 cdw12 = args->lpo & 0xffffffff; |
| 298 |
__u32 cdw13 = args->lpo >> 32; |
| 299 |
__u32 cdw14 = NVME_SET(args->uuidx, LOG_CDW14_UUID) | |
| 300 |
NVME_SET(!!args->ot, LOG_CDW14_OT) | |
| 301 |
NVME_SET(args->csi, LOG_CDW14_CSI); |
| 302 |
|
| 303 |
struct nvme_passthru_cmd cmd = { |
| 304 |
.opcode = nvme_admin_get_log_page, |
| 305 |
.nsid = args->nsid, |
| 306 |
.addr = (__u64)(uintptr_t)args->log, |
| 307 |
.data_len = args->len, |
| 308 |
.cdw10 = cdw10, |
| 309 |
.cdw11 = cdw11, |
| 310 |
.cdw12 = cdw12, |
| 311 |
.cdw13 = cdw13, |
| 312 |
.cdw14 = cdw14, |
| 313 |
.timeout_ms = args->timeout, |
| 314 |
}; |
| 315 |
|
| 316 |
if (args->args_size < sizeof(struct nvme_get_log_args)) { |
| 317 |
errno = EINVAL; |
| 318 |
return -1; |
| 319 |
} |
| 320 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 321 |
} |
| 322 |
|
| 323 |
static bool force_4k; |
| 324 |
|
| 325 |
__attribute__((constructor)) |
| 326 |
static void nvme_init_env(void) |
| 327 |
{ |
| 328 |
char *val; |
| 329 |
|
| 330 |
val = getenv("LIBNVME_FORCE_4K"); |
| 331 |
if (!val) |
| 332 |
return; |
| 333 |
if (!strcmp(val, "1") || |
| 334 |
!strcasecmp(val, "true") || |
| 335 |
!strncasecmp(val, "enable", 6)) |
| 336 |
force_4k = true; |
| 337 |
} |
| 338 |
|
| 339 |
#ifdef CONFIG_LIBURING |
| 340 |
enum { |
| 341 |
IO_URING_NOT_AVAILABLE, |
| 342 |
IO_URING_AVAILABLE, |
| 343 |
} io_uring_kernel_support = IO_URING_NOT_AVAILABLE; |
| 344 |
|
| 345 |
/* |
| 346 |
* gcc specific attribute, call automatically on the library loading. |
| 347 |
* if IORING_OP_URING_CMD is not supported, fallback to ioctl interface. |
| 348 |
*/ |
| 349 |
__attribute__((constructor)) |
| 350 |
static void nvme_uring_cmd_probe() |
| 351 |
{ |
| 352 |
struct io_uring_probe *probe = io_uring_get_probe(); |
| 353 |
|
| 354 |
if (!probe) |
| 355 |
return; |
| 356 |
|
| 357 |
if (!io_uring_opcode_supported(probe, IORING_OP_URING_CMD)) |
| 358 |
return; |
| 359 |
|
| 360 |
io_uring_kernel_support = IO_URING_AVAILABLE; |
| 361 |
} |
| 362 |
|
| 363 |
static int nvme_uring_cmd_setup(struct io_uring *ring) |
| 364 |
{ |
| 365 |
return io_uring_queue_init(NVME_URING_ENTRIES, ring, |
| 366 |
IORING_SETUP_SQE128 | IORING_SETUP_CQE32); |
| 367 |
} |
| 368 |
|
| 369 |
static void nvme_uring_cmd_exit(struct io_uring *ring) |
| 370 |
{ |
| 371 |
io_uring_queue_exit(ring); |
| 372 |
} |
| 373 |
|
| 374 |
static int nvme_uring_cmd_admin_passthru_async(struct io_uring *ring, struct nvme_get_log_args *args) |
| 375 |
{ |
| 376 |
struct io_uring_sqe *sqe; |
| 377 |
struct nvme_uring_cmd *cmd; |
| 378 |
int ret; |
| 379 |
|
| 380 |
__u32 numd = (args->len >> 2) - 1; |
| 381 |
__u16 numdu = numd >> 16, numdl = numd & 0xffff; |
| 382 |
|
| 383 |
__u32 cdw10 = NVME_SET(args->lid, LOG_CDW10_LID) | |
| 384 |
NVME_SET(args->lsp, LOG_CDW10_LSP) | |
| 385 |
NVME_SET(!!args->rae, LOG_CDW10_RAE) | |
| 386 |
NVME_SET(numdl, LOG_CDW10_NUMDL); |
| 387 |
__u32 cdw11 = NVME_SET(numdu, LOG_CDW11_NUMDU) | |
| 388 |
NVME_SET(args->lsi, LOG_CDW11_LSI); |
| 389 |
__u32 cdw12 = args->lpo & 0xffffffff; |
| 390 |
__u32 cdw13 = args->lpo >> 32; |
| 391 |
__u32 cdw14 = NVME_SET(args->uuidx, LOG_CDW14_UUID) | |
| 392 |
NVME_SET(!!args->ot, LOG_CDW14_OT) | |
| 393 |
NVME_SET(args->csi, LOG_CDW14_CSI); |
| 394 |
|
| 395 |
if (args->args_size < sizeof(struct nvme_get_log_args)) { |
| 396 |
errno = EINVAL; |
| 397 |
return -1; |
| 398 |
} |
| 399 |
|
| 400 |
sqe = io_uring_get_sqe(ring); |
| 401 |
if (!sqe) |
| 402 |
return -1; |
| 403 |
|
| 404 |
cmd = (void *)&sqe->cmd; |
| 405 |
cmd->opcode = nvme_admin_get_log_page, |
| 406 |
cmd->nsid = args->nsid, |
| 407 |
cmd->addr = (__u64)(uintptr_t)args->log, |
| 408 |
cmd->data_len = args->len, |
| 409 |
cmd->cdw10 = cdw10, |
| 410 |
cmd->cdw11 = cdw11, |
| 411 |
cmd->cdw12 = cdw12, |
| 412 |
cmd->cdw13 = cdw13, |
| 413 |
cmd->cdw14 = cdw14, |
| 414 |
cmd->timeout_ms = args->timeout, |
| 415 |
|
| 416 |
sqe->fd = args->fd; |
| 417 |
sqe->opcode = IORING_OP_URING_CMD; |
| 418 |
sqe->cmd_op = NVME_URING_CMD_ADMIN; |
| 419 |
sqe->user_data = (__u64)(uintptr_t)args; |
| 420 |
|
| 421 |
ret = io_uring_submit(ring); |
| 422 |
if (ret < 0) { |
| 423 |
errno = -ret; |
| 424 |
return -1; |
| 425 |
} |
| 426 |
|
| 427 |
return 0; |
| 428 |
} |
| 429 |
|
| 430 |
static int nvme_uring_cmd_wait_complete(struct io_uring *ring, int n) |
| 431 |
{ |
| 432 |
struct nvme_get_log_args *args; |
| 433 |
struct io_uring_cqe *cqe; |
| 434 |
int i, ret = 0; |
| 435 |
|
| 436 |
for (i = 0; i < n; i++) { |
| 437 |
ret = io_uring_wait_cqe(ring, &cqe); |
| 438 |
if (ret) { |
| 439 |
errno = -ret; |
| 440 |
return -1; |
| 441 |
} |
| 442 |
|
| 443 |
if (cqe->res) { |
| 444 |
args = (struct nvme_get_log_args *)cqe->user_data; |
| 445 |
if (args->result) |
| 446 |
*args->result = cqe->res; |
| 447 |
ret = cqe->res; |
| 448 |
break; |
| 449 |
} |
| 450 |
|
| 451 |
io_uring_cqe_seen(ring, cqe); |
| 452 |
} |
| 453 |
|
| 454 |
return ret; |
| 455 |
} |
| 456 |
#endif |
| 457 |
|
| 458 |
int nvme_get_log_page(int fd, __u32 xfer_len, struct nvme_get_log_args *args) |
| 459 |
{ |
| 460 |
__u64 offset = 0, xfer, data_len = args->len; |
| 461 |
__u64 start = args->lpo; |
| 462 |
bool retain = args->rae; |
| 463 |
void *ptr = args->log; |
| 464 |
int ret; |
| 465 |
|
| 466 |
args->fd = fd; |
| 467 |
|
| 468 |
if (force_4k) |
| 469 |
xfer_len = NVME_LOG_PAGE_PDU_SIZE; |
| 470 |
|
| 471 |
#ifdef CONFIG_LIBURING |
| 472 |
int n = 0; |
| 473 |
struct io_uring ring; |
| 474 |
struct stat st; |
| 475 |
bool use_uring = false; |
| 476 |
|
| 477 |
if (io_uring_kernel_support == IO_URING_AVAILABLE) { |
| 478 |
if (fstat(fd, &st) == 0 && S_ISCHR(st.st_mode)) { |
| 479 |
use_uring = true; |
| 480 |
|
| 481 |
if (nvme_uring_cmd_setup(&ring)) |
| 482 |
return -1; |
| 483 |
} |
| 484 |
} |
| 485 |
#endif |
| 486 |
/* |
| 487 |
* 4k is the smallest possible transfer unit, so restricting to 4k |
| 488 |
* avoids having to check the MDTS value of the controller. |
| 489 |
*/ |
| 490 |
do { |
| 491 |
if (!force_4k) { |
| 492 |
xfer = data_len - offset; |
| 493 |
if (xfer > xfer_len) |
| 494 |
xfer = xfer_len; |
| 495 |
} else { |
| 496 |
xfer = NVME_LOG_PAGE_PDU_SIZE; |
| 497 |
} |
| 498 |
|
| 499 |
/* |
| 500 |
* Always retain regardless of the RAE parameter until the very |
| 501 |
* last portion of this log page so the data remains latched |
| 502 |
* during the fetch sequence. |
| 503 |
*/ |
| 504 |
args->lpo = start + offset; |
| 505 |
args->len = xfer; |
| 506 |
args->log = ptr; |
| 507 |
args->rae = offset + xfer < data_len || retain; |
| 508 |
#ifdef CONFIG_LIBURING |
| 509 |
if (io_uring_kernel_support == IO_URING_AVAILABLE && use_uring) { |
| 510 |
if (n >= NVME_URING_ENTRIES) { |
| 511 |
ret = nvme_uring_cmd_wait_complete(&ring, n); |
| 512 |
n = 0; |
| 513 |
} |
| 514 |
n += 1; |
| 515 |
ret = nvme_uring_cmd_admin_passthru_async(&ring, args); |
| 516 |
|
| 517 |
if (ret) |
| 518 |
nvme_uring_cmd_exit(&ring); |
| 519 |
} else |
| 520 |
#endif |
| 521 |
ret = nvme_get_log(args); |
| 522 |
if (ret) |
| 523 |
return ret; |
| 524 |
|
| 525 |
offset += xfer; |
| 526 |
ptr += xfer; |
| 527 |
} while (offset < data_len); |
| 528 |
|
| 529 |
#ifdef CONFIG_LIBURING |
| 530 |
if (io_uring_kernel_support == IO_URING_AVAILABLE && use_uring) { |
| 531 |
ret = nvme_uring_cmd_wait_complete(&ring, n); |
| 532 |
nvme_uring_cmd_exit(&ring); |
| 533 |
if (ret) |
| 534 |
return ret; |
| 535 |
} |
| 536 |
#endif |
| 537 |
return 0; |
| 538 |
} |
| 539 |
|
| 540 |
static int read_ana_chunk(int fd, enum nvme_log_ana_lsp lsp, bool rae, |
| 541 |
__u8 *log, __u8 **read, __u8 *to_read, __u8 *log_end) |
| 542 |
{ |
| 543 |
if (to_read > log_end) { |
| 544 |
errno = ENOSPC; |
| 545 |
return -1; |
| 546 |
} |
| 547 |
|
| 548 |
while (*read < to_read) { |
| 549 |
__u32 len = min_t(__u32, log_end - *read, NVME_LOG_PAGE_PDU_SIZE); |
| 550 |
int ret; |
| 551 |
|
| 552 |
ret = nvme_get_log_ana(fd, lsp, rae, *read - log, len, *read); |
| 553 |
if (ret) |
| 554 |
return ret; |
| 555 |
|
| 556 |
*read += len; |
| 557 |
} |
| 558 |
return 0; |
| 559 |
} |
| 560 |
|
| 561 |
static int try_read_ana(int fd, enum nvme_log_ana_lsp lsp, bool rae, |
| 562 |
struct nvme_ana_log *log, __u8 *log_end, |
| 563 |
__u8 *read, __u8 **to_read, bool *may_retry) |
| 564 |
{ |
| 565 |
__u16 ngrps = le16_to_cpu(log->ngrps); |
| 566 |
|
| 567 |
while (ngrps--) { |
| 568 |
__u8 *group = *to_read; |
| 569 |
int ret; |
| 570 |
__le32 nnsids; |
| 571 |
|
| 572 |
*to_read += sizeof(*log->descs); |
| 573 |
ret = read_ana_chunk(fd, lsp, rae, |
| 574 |
(__u8 *)log, &read, *to_read, log_end); |
| 575 |
if (ret) { |
| 576 |
/* |
| 577 |
* If the provided buffer isn't long enough, |
| 578 |
* the log page may have changed while reading it |
| 579 |
* and the computed length was inaccurate. |
| 580 |
* Have the caller check chgcnt and retry. |
| 581 |
*/ |
| 582 |
*may_retry = errno == ENOSPC; |
| 583 |
return ret; |
| 584 |
} |
| 585 |
|
| 586 |
/* |
| 587 |
* struct nvme_ana_group_desc has 8-byte alignment |
| 588 |
* but the group pointer is only 4-byte aligned. |
| 589 |
* Don't dereference the misaligned pointer. |
| 590 |
*/ |
| 591 |
memcpy(&nnsids, |
| 592 |
group + offsetof(struct nvme_ana_group_desc, nnsids), |
| 593 |
sizeof(nnsids)); |
| 594 |
*to_read += le32_to_cpu(nnsids) * sizeof(__le32); |
| 595 |
ret = read_ana_chunk(fd, lsp, rae, |
| 596 |
(__u8 *)log, &read, *to_read, log_end); |
| 597 |
if (ret) { |
| 598 |
*may_retry = errno == ENOSPC; |
| 599 |
return ret; |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
*may_retry = true; |
| 604 |
return 0; |
| 605 |
} |
| 606 |
|
| 607 |
int nvme_get_ana_log_atomic(int fd, bool rgo, bool rae, unsigned int retries, |
| 608 |
struct nvme_ana_log *log, __u32 *len) |
| 609 |
{ |
| 610 |
const enum nvme_log_ana_lsp lsp = |
| 611 |
rgo ? NVME_LOG_ANA_LSP_RGO_GROUPS_ONLY : 0; |
| 612 |
/* Get Log Page can only fetch multiples of dwords */ |
| 613 |
__u8 * const log_end = (__u8 *)log + (*len & -4); |
| 614 |
__u8 *read = (__u8 *)log; |
| 615 |
__u8 *to_read; |
| 616 |
int ret; |
| 617 |
|
| 618 |
if (!retries) { |
| 619 |
errno = EINVAL; |
| 620 |
return -1; |
| 621 |
} |
| 622 |
|
| 623 |
to_read = (__u8 *)log->descs; |
| 624 |
ret = read_ana_chunk(fd, lsp, rae, |
| 625 |
(__u8 *)log, &read, to_read, log_end); |
| 626 |
if (ret) |
| 627 |
return ret; |
| 628 |
|
| 629 |
do { |
| 630 |
bool may_retry = false; |
| 631 |
int saved_ret; |
| 632 |
int saved_errno; |
| 633 |
__le64 chgcnt; |
| 634 |
|
| 635 |
saved_ret = try_read_ana(fd, lsp, rae, log, log_end, |
| 636 |
read, &to_read, &may_retry); |
| 637 |
/* |
| 638 |
* If the log page was read with multiple Get Log Page commands, |
| 639 |
* chgcnt must be checked afterwards to ensure atomicity |
| 640 |
*/ |
| 641 |
*len = to_read - (__u8 *)log; |
| 642 |
if (*len <= NVME_LOG_PAGE_PDU_SIZE || !may_retry) |
| 643 |
return saved_ret; |
| 644 |
|
| 645 |
saved_errno = errno; |
| 646 |
chgcnt = log->chgcnt; |
| 647 |
read = (__u8 *)log; |
| 648 |
to_read = (__u8 *)log->descs; |
| 649 |
ret = read_ana_chunk(fd, lsp, rae, |
| 650 |
(__u8 *)log, &read, to_read, log_end); |
| 651 |
if (ret) |
| 652 |
return ret; |
| 653 |
|
| 654 |
if (log->chgcnt == chgcnt) { |
| 655 |
/* Log hasn't changed; return try_read_ana() result */ |
| 656 |
errno = saved_errno; |
| 657 |
return saved_ret; |
| 658 |
} |
| 659 |
} while (--retries); |
| 660 |
|
| 661 |
errno = EAGAIN; |
| 662 |
return -1; |
| 663 |
} |
| 664 |
|
| 665 |
int nvme_set_features(struct nvme_set_features_args *args) |
| 666 |
{ |
| 667 |
__u32 cdw10 = NVME_SET(args->fid, FEATURES_CDW10_FID) | |
| 668 |
NVME_SET(!!args->save, SET_FEATURES_CDW10_SAVE); |
| 669 |
__u32 cdw14 = NVME_SET(args->uuidx, FEATURES_CDW14_UUID); |
| 670 |
|
| 671 |
struct nvme_passthru_cmd cmd = { |
| 672 |
.opcode = nvme_admin_set_features, |
| 673 |
.nsid = args->nsid, |
| 674 |
.addr = (__u64)(uintptr_t)args->data, |
| 675 |
.data_len = args->data_len, |
| 676 |
.cdw10 = cdw10, |
| 677 |
.cdw11 = args->cdw11, |
| 678 |
.cdw12 = args->cdw12, |
| 679 |
.cdw13 = args->cdw13, |
| 680 |
.cdw14 = cdw14, |
| 681 |
.cdw15 = args->cdw15, |
| 682 |
.timeout_ms = args->timeout, |
| 683 |
}; |
| 684 |
if (args->args_size < sizeof(*args)) { |
| 685 |
errno = EINVAL; |
| 686 |
return -1; |
| 687 |
} |
| 688 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 689 |
} |
| 690 |
|
| 691 |
static int __nvme_set_features(int fd, __u8 fid, __u32 cdw11, bool save, |
| 692 |
__u32 *result) |
| 693 |
{ |
| 694 |
struct nvme_set_features_args args = { |
| 695 |
.args_size = sizeof(args), |
| 696 |
.fd = fd, |
| 697 |
.fid = fid, |
| 698 |
.nsid = NVME_NSID_NONE, |
| 699 |
.cdw11 = cdw11, |
| 700 |
.cdw12 = 0, |
| 701 |
.save = save, |
| 702 |
.uuidx = NVME_UUID_NONE, |
| 703 |
.cdw15 = 0, |
| 704 |
.data_len = 0, |
| 705 |
.data = NULL, |
| 706 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 707 |
.result = result, |
| 708 |
}; |
| 709 |
return nvme_set_features(&args); |
| 710 |
} |
| 711 |
|
| 712 |
int nvme_set_features_arbitration(int fd, __u8 ab, __u8 lpw, __u8 mpw, |
| 713 |
__u8 hpw, bool save, __u32 *result) |
| 714 |
{ |
| 715 |
__u32 value = NVME_SET(ab, FEAT_ARBITRATION_BURST) | |
| 716 |
NVME_SET(lpw, FEAT_ARBITRATION_LPW) | |
| 717 |
NVME_SET(mpw, FEAT_ARBITRATION_MPW) | |
| 718 |
NVME_SET(hpw, FEAT_ARBITRATION_HPW); |
| 719 |
|
| 720 |
return __nvme_set_features(fd, NVME_FEAT_FID_ARBITRATION, value, save, |
| 721 |
result); |
| 722 |
} |
| 723 |
|
| 724 |
int nvme_set_features_power_mgmt(int fd, __u8 ps, __u8 wh, bool save, |
| 725 |
__u32 *result) |
| 726 |
{ |
| 727 |
__u32 value = NVME_SET(ps, FEAT_PWRMGMT_PS) | |
| 728 |
NVME_SET(wh, FEAT_PWRMGMT_WH); |
| 729 |
|
| 730 |
return __nvme_set_features(fd, NVME_FEAT_FID_POWER_MGMT, value, save, |
| 731 |
result); |
| 732 |
} |
| 733 |
|
| 734 |
int nvme_set_features_lba_range(int fd, __u32 nsid, __u8 nr_ranges, bool save, |
| 735 |
struct nvme_lba_range_type *data, __u32 *result) |
| 736 |
{ |
| 737 |
return nvme_set_features_data( |
| 738 |
fd, NVME_FEAT_FID_LBA_RANGE, nsid, nr_ranges - 1, save, |
| 739 |
sizeof(*data), data, result); |
| 740 |
} |
| 741 |
|
| 742 |
int nvme_set_features_temp_thresh(int fd, __u16 tmpth, __u8 tmpsel, |
| 743 |
enum nvme_feat_tmpthresh_thsel thsel, |
| 744 |
bool save, __u32 *result) |
| 745 |
{ |
| 746 |
__u32 value = NVME_SET(tmpth, FEAT_TT_TMPTH) | |
| 747 |
NVME_SET(tmpsel, FEAT_TT_TMPSEL) | |
| 748 |
NVME_SET(thsel, FEAT_TT_THSEL); |
| 749 |
|
| 750 |
return __nvme_set_features(fd, NVME_FEAT_FID_TEMP_THRESH, value, save, |
| 751 |
result); |
| 752 |
} |
| 753 |
|
| 754 |
int nvme_set_features_temp_thresh2(int fd, __u16 tmpth, __u8 tmpsel, |
| 755 |
enum nvme_feat_tmpthresh_thsel thsel, __u8 tmpthh, |
| 756 |
bool save, __u32 *result) |
| 757 |
{ |
| 758 |
__u32 value = NVME_SET(tmpth, FEAT_TT_TMPTH) | |
| 759 |
NVME_SET(tmpsel, FEAT_TT_TMPSEL) | |
| 760 |
NVME_SET(thsel, FEAT_TT_THSEL) | |
| 761 |
NVME_SET(tmpthh, FEAT_TT_TMPTHH); |
| 762 |
|
| 763 |
return __nvme_set_features(fd, NVME_FEAT_FID_TEMP_THRESH, value, save, |
| 764 |
result); |
| 765 |
} |
| 766 |
|
| 767 |
int nvme_set_features_err_recovery(int fd, __u32 nsid, __u16 tler, bool dulbe, |
| 768 |
bool save, __u32 *result) |
| 769 |
{ |
| 770 |
__u32 value = NVME_SET(tler, FEAT_ERROR_RECOVERY_TLER) | |
| 771 |
NVME_SET(!!dulbe, FEAT_ERROR_RECOVERY_DULBE); |
| 772 |
|
| 773 |
return nvme_set_features_simple( |
| 774 |
fd, NVME_FEAT_FID_ERR_RECOVERY, nsid, value, save, result); |
| 775 |
} |
| 776 |
|
| 777 |
int nvme_set_features_volatile_wc(int fd, bool wce, bool save, __u32 *result) |
| 778 |
{ |
| 779 |
__u32 value = NVME_SET(!!wce, FEAT_VWC_WCE); |
| 780 |
|
| 781 |
return __nvme_set_features(fd, NVME_FEAT_FID_VOLATILE_WC, value, save, |
| 782 |
result); |
| 783 |
} |
| 784 |
|
| 785 |
int nvme_set_features_irq_coalesce(int fd, __u8 thr, __u8 time, bool save, |
| 786 |
__u32 *result) |
| 787 |
{ |
| 788 |
__u32 value = NVME_SET(thr, FEAT_IRQC_THR) | |
| 789 |
NVME_SET(time, FEAT_IRQC_TIME); |
| 790 |
|
| 791 |
return __nvme_set_features(fd, NVME_FEAT_FID_IRQ_COALESCE, value, save, |
| 792 |
result); |
| 793 |
} |
| 794 |
|
| 795 |
int nvme_set_features_irq_config(int fd, __u16 iv, bool cd, bool save, |
| 796 |
__u32 *result) |
| 797 |
{ |
| 798 |
__u32 value = NVME_SET(iv, FEAT_ICFG_IV) | |
| 799 |
NVME_SET(!!cd, FEAT_ICFG_CD); |
| 800 |
|
| 801 |
return __nvme_set_features(fd, NVME_FEAT_FID_IRQ_CONFIG, value, save, |
| 802 |
result); |
| 803 |
} |
| 804 |
|
| 805 |
int nvme_set_features_write_atomic(int fd, bool dn, bool save, __u32 *result) |
| 806 |
{ |
| 807 |
__u32 value = NVME_SET(!!dn, FEAT_WA_DN); |
| 808 |
|
| 809 |
return __nvme_set_features(fd, NVME_FEAT_FID_WRITE_ATOMIC, value, save, |
| 810 |
result); |
| 811 |
} |
| 812 |
|
| 813 |
int nvme_set_features_async_event(int fd, __u32 events, |
| 814 |
bool save, __u32 *result) |
| 815 |
{ |
| 816 |
return __nvme_set_features(fd, NVME_FEAT_FID_ASYNC_EVENT, events, save, |
| 817 |
result); |
| 818 |
} |
| 819 |
|
| 820 |
int nvme_set_features_auto_pst(int fd, bool apste, bool save, |
| 821 |
struct nvme_feat_auto_pst *apst, __u32 *result) |
| 822 |
{ |
| 823 |
return nvme_set_features_data(fd, NVME_FEAT_FID_AUTO_PST, |
| 824 |
NVME_NSID_NONE, NVME_SET(!!apste, FEAT_APST_APSTE), save, |
| 825 |
sizeof(*apst), apst, result); |
| 826 |
} |
| 827 |
|
| 828 |
int nvme_set_features_timestamp(int fd, bool save, __u64 timestamp) |
| 829 |
{ |
| 830 |
__le64 t = cpu_to_le64(timestamp); |
| 831 |
struct nvme_timestamp ts = {}; |
| 832 |
memcpy(ts.timestamp, &t, sizeof(ts.timestamp)); |
| 833 |
|
| 834 |
return nvme_set_features_data(fd, NVME_FEAT_FID_TIMESTAMP, |
| 835 |
NVME_NSID_NONE, 0, save, sizeof(ts), &ts, NULL); |
| 836 |
} |
| 837 |
|
| 838 |
int nvme_set_features_hctm(int fd, __u16 tmt2, __u16 tmt1, |
| 839 |
bool save, __u32 *result) |
| 840 |
{ |
| 841 |
__u32 value = NVME_SET(tmt2, FEAT_HCTM_TMT2) | |
| 842 |
NVME_SET(tmt1, FEAT_HCTM_TMT1); |
| 843 |
|
| 844 |
return __nvme_set_features(fd, NVME_FEAT_FID_HCTM, value, save, |
| 845 |
result); |
| 846 |
} |
| 847 |
|
| 848 |
int nvme_set_features_nopsc(int fd, bool noppme, bool save, __u32 *result) |
| 849 |
{ |
| 850 |
__u32 value = NVME_SET(noppme, FEAT_NOPS_NOPPME); |
| 851 |
|
| 852 |
return __nvme_set_features(fd, NVME_FEAT_FID_NOPSC, value, save, |
| 853 |
result); |
| 854 |
} |
| 855 |
|
| 856 |
int nvme_set_features_rrl(int fd, __u8 rrl, __u16 nvmsetid, |
| 857 |
bool save, __u32 *result) |
| 858 |
{ |
| 859 |
struct nvme_set_features_args args = { |
| 860 |
.args_size = sizeof(args), |
| 861 |
.fd = fd, |
| 862 |
.fid = NVME_FEAT_FID_RRL, |
| 863 |
.nsid = NVME_NSID_NONE, |
| 864 |
.cdw11 = nvmsetid, |
| 865 |
.cdw12 = rrl, |
| 866 |
.save = save, |
| 867 |
.uuidx = NVME_UUID_NONE, |
| 868 |
.cdw15 = 0, |
| 869 |
.data_len = 0, |
| 870 |
.data = NULL, |
| 871 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 872 |
.result = result, |
| 873 |
}; |
| 874 |
|
| 875 |
return nvme_set_features(&args); |
| 876 |
} |
| 877 |
|
| 878 |
int nvme_set_features_plm_config(int fd, bool plm, __u16 nvmsetid, bool save, |
| 879 |
struct nvme_plm_config *data, __u32 *result) |
| 880 |
{ |
| 881 |
struct nvme_set_features_args args = { |
| 882 |
.args_size = sizeof(args), |
| 883 |
.fd = fd, |
| 884 |
.fid = NVME_FEAT_FID_PLM_CONFIG, |
| 885 |
.nsid = NVME_NSID_NONE, |
| 886 |
.cdw11 = nvmsetid, |
| 887 |
.cdw12 = !!plm, |
| 888 |
.save = save, |
| 889 |
.uuidx = NVME_UUID_NONE, |
| 890 |
.cdw15 = 0, |
| 891 |
.data_len = sizeof(*data), |
| 892 |
.data = data, |
| 893 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 894 |
.result = result, |
| 895 |
}; |
| 896 |
|
| 897 |
return nvme_set_features(&args); |
| 898 |
} |
| 899 |
|
| 900 |
int nvme_set_features_plm_window(int fd, enum nvme_feat_plm_window_select sel, |
| 901 |
__u16 nvmsetid, bool save, __u32 *result) |
| 902 |
{ |
| 903 |
__u32 cdw12 = NVME_SET(sel, FEAT_PLMW_WS); |
| 904 |
struct nvme_set_features_args args = { |
| 905 |
.args_size = sizeof(args), |
| 906 |
.fd = fd, |
| 907 |
.fid = NVME_FEAT_FID_PLM_WINDOW, |
| 908 |
.nsid = NVME_NSID_NONE, |
| 909 |
.cdw11 = nvmsetid, |
| 910 |
.cdw12 = cdw12, |
| 911 |
.save = save, |
| 912 |
.uuidx = NVME_UUID_NONE, |
| 913 |
.cdw15 = 0, |
| 914 |
.data_len = 0, |
| 915 |
.data = NULL, |
| 916 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 917 |
.result = result, |
| 918 |
}; |
| 919 |
|
| 920 |
return nvme_set_features(&args); |
| 921 |
} |
| 922 |
|
| 923 |
int nvme_set_features_lba_sts_interval(int fd, __u16 lsiri, __u16 lsipi, |
| 924 |
bool save, __u32 *result) |
| 925 |
{ |
| 926 |
__u32 value = NVME_SET(lsiri, FEAT_LBAS_LSIRI) | |
| 927 |
NVME_SET(lsipi, FEAT_LBAS_LSIPI); |
| 928 |
|
| 929 |
return __nvme_set_features(fd, NVME_FEAT_FID_LBA_STS_INTERVAL, value, |
| 930 |
save, result); |
| 931 |
} |
| 932 |
|
| 933 |
int nvme_set_features_host_behavior(int fd, bool save, |
| 934 |
struct nvme_feat_host_behavior *data) |
| 935 |
{ |
| 936 |
return nvme_set_features_data(fd, NVME_FEAT_FID_HOST_BEHAVIOR, |
| 937 |
NVME_NSID_NONE, 0, false, sizeof(*data), data, NULL); |
| 938 |
} |
| 939 |
|
| 940 |
int nvme_set_features_sanitize(int fd, bool nodrm, bool save, __u32 *result) |
| 941 |
{ |
| 942 |
return __nvme_set_features(fd, NVME_FEAT_FID_SANITIZE, !!nodrm, save, |
| 943 |
result); |
| 944 |
} |
| 945 |
|
| 946 |
int nvme_set_features_endurance_evt_cfg(int fd, __u16 endgid, __u8 egwarn, |
| 947 |
bool save, __u32 *result) |
| 948 |
{ |
| 949 |
__u32 value = endgid | egwarn << 16; |
| 950 |
|
| 951 |
return __nvme_set_features(fd, NVME_FEAT_FID_ENDURANCE_EVT_CFG, value, |
| 952 |
save, result); |
| 953 |
} |
| 954 |
|
| 955 |
int nvme_set_features_sw_progress(int fd, __u8 pbslc, bool save, |
| 956 |
__u32 *result) |
| 957 |
{ |
| 958 |
return __nvme_set_features(fd, NVME_FEAT_FID_SW_PROGRESS, pbslc, save, |
| 959 |
result); |
| 960 |
} |
| 961 |
|
| 962 |
int nvme_set_features_host_id(int fd, bool exhid, bool save, __u8 *hostid) |
| 963 |
{ |
| 964 |
__u32 len = exhid ? 16 : 8; |
| 965 |
__u32 value = !!exhid; |
| 966 |
|
| 967 |
return nvme_set_features_data(fd, NVME_FEAT_FID_HOST_ID, |
| 968 |
NVME_NSID_NONE, value, save, len, hostid, NULL); |
| 969 |
} |
| 970 |
|
| 971 |
int nvme_set_features_resv_mask(int fd, __u32 mask, bool save, __u32 *result) |
| 972 |
{ |
| 973 |
return __nvme_set_features(fd, NVME_FEAT_FID_RESV_MASK, mask, save, |
| 974 |
result); |
| 975 |
} |
| 976 |
|
| 977 |
int nvme_set_features_resv_mask2(int fd, __u32 nsid, __u32 mask, bool save, |
| 978 |
__u32 *result) |
| 979 |
{ |
| 980 |
return nvme_set_features_simple( |
| 981 |
fd, NVME_FEAT_FID_RESV_MASK, nsid, mask, save, result); |
| 982 |
} |
| 983 |
|
| 984 |
int nvme_set_features_resv_persist(int fd, bool ptpl, bool save, __u32 *result) |
| 985 |
{ |
| 986 |
return __nvme_set_features(fd, NVME_FEAT_FID_RESV_PERSIST, !!ptpl, save, |
| 987 |
result); |
| 988 |
} |
| 989 |
|
| 990 |
int nvme_set_features_resv_persist2(int fd, __u32 nsid, bool ptpl, bool save, |
| 991 |
__u32 *result) |
| 992 |
{ |
| 993 |
return nvme_set_features_simple( |
| 994 |
fd, NVME_FEAT_FID_RESV_PERSIST, nsid, !!ptpl, save, result); |
| 995 |
} |
| 996 |
|
| 997 |
int nvme_set_features_write_protect(int fd, enum nvme_feat_nswpcfg_state state, |
| 998 |
bool save, __u32 *result) |
| 999 |
{ |
| 1000 |
return __nvme_set_features(fd, NVME_FEAT_FID_WRITE_PROTECT, state, |
| 1001 |
false, result); |
| 1002 |
} |
| 1003 |
|
| 1004 |
int nvme_set_features_write_protect2(int fd, __u32 nsid, |
| 1005 |
enum nvme_feat_nswpcfg_state state, |
| 1006 |
bool save, __u32 *result) |
| 1007 |
{ |
| 1008 |
return nvme_set_features_simple( |
| 1009 |
fd, NVME_FEAT_FID_WRITE_PROTECT, nsid, state, false, result); |
| 1010 |
} |
| 1011 |
|
| 1012 |
int nvme_set_features_iocs_profile(int fd, __u16 iocsi, bool save) |
| 1013 |
{ |
| 1014 |
__u32 value = NVME_SET(iocsi, FEAT_IOCSP_IOCSCI); |
| 1015 |
|
| 1016 |
return __nvme_set_features(fd, NVME_FEAT_FID_IOCS_PROFILE, value, |
| 1017 |
save, NULL); |
| 1018 |
} |
| 1019 |
|
| 1020 |
int nvme_get_features(struct nvme_get_features_args *args) |
| 1021 |
{ |
| 1022 |
__u32 cdw10 = NVME_SET(args->fid, FEATURES_CDW10_FID) | |
| 1023 |
NVME_SET(args->sel, GET_FEATURES_CDW10_SEL); |
| 1024 |
__u32 cdw14 = NVME_SET(args->uuidx, FEATURES_CDW14_UUID); |
| 1025 |
|
| 1026 |
struct nvme_passthru_cmd cmd = { |
| 1027 |
.opcode = nvme_admin_get_features, |
| 1028 |
.nsid = args->nsid, |
| 1029 |
.addr = (__u64)(uintptr_t)args->data, |
| 1030 |
.data_len = args->data_len, |
| 1031 |
.cdw10 = cdw10, |
| 1032 |
.cdw11 = args->cdw11, |
| 1033 |
.cdw14 = cdw14, |
| 1034 |
.timeout_ms = args->timeout, |
| 1035 |
}; |
| 1036 |
|
| 1037 |
if (args->args_size < sizeof(*args)) { |
| 1038 |
errno = EINVAL; |
| 1039 |
return -1; |
| 1040 |
} |
| 1041 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 1042 |
} |
| 1043 |
|
| 1044 |
static int __nvme_get_features(int fd, enum nvme_features_id fid, |
| 1045 |
enum nvme_get_features_sel sel, __u32 *result) |
| 1046 |
{ |
| 1047 |
struct nvme_get_features_args args = { |
| 1048 |
.args_size = sizeof(args), |
| 1049 |
.fd = fd, |
| 1050 |
.fid = fid, |
| 1051 |
.nsid = NVME_NSID_NONE, |
| 1052 |
.sel = sel, |
| 1053 |
.cdw11 = 0, |
| 1054 |
.uuidx = NVME_UUID_NONE, |
| 1055 |
.data_len = 0, |
| 1056 |
.data = NULL, |
| 1057 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 1058 |
.result = result, |
| 1059 |
}; |
| 1060 |
|
| 1061 |
return nvme_get_features(&args); |
| 1062 |
} |
| 1063 |
|
| 1064 |
int nvme_get_features_arbitration(int fd, enum nvme_get_features_sel sel, |
| 1065 |
__u32 *result) |
| 1066 |
{ |
| 1067 |
return __nvme_get_features(fd, NVME_FEAT_FID_ARBITRATION, sel, result); |
| 1068 |
} |
| 1069 |
|
| 1070 |
int nvme_get_features_power_mgmt(int fd, enum nvme_get_features_sel sel, |
| 1071 |
__u32 *result) |
| 1072 |
{ |
| 1073 |
return __nvme_get_features(fd, NVME_FEAT_FID_POWER_MGMT, sel, result); |
| 1074 |
} |
| 1075 |
|
| 1076 |
int nvme_get_features_lba_range(int fd, enum nvme_get_features_sel sel, |
| 1077 |
struct nvme_lba_range_type *data, |
| 1078 |
__u32 *result) |
| 1079 |
{ |
| 1080 |
struct nvme_get_features_args args = { |
| 1081 |
.args_size = sizeof(args), |
| 1082 |
.fd = fd, |
| 1083 |
.fid = NVME_FEAT_FID_LBA_RANGE, |
| 1084 |
.nsid = NVME_NSID_NONE, |
| 1085 |
.sel = sel, |
| 1086 |
.cdw11 = 0, |
| 1087 |
.uuidx = NVME_UUID_NONE, |
| 1088 |
.data_len = sizeof(*data), |
| 1089 |
.data = data, |
| 1090 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 1091 |
.result = result, |
| 1092 |
}; |
| 1093 |
|
| 1094 |
return nvme_get_features(&args); |
| 1095 |
} |
| 1096 |
|
| 1097 |
int nvme_get_features_lba_range2(int fd, enum nvme_get_features_sel sel, |
| 1098 |
__u32 nsid, struct nvme_lba_range_type *data, |
| 1099 |
__u32 *result) |
| 1100 |
{ |
| 1101 |
struct nvme_get_features_args args = { |
| 1102 |
.args_size = sizeof(args), |
| 1103 |
.fd = fd, |
| 1104 |
.fid = NVME_FEAT_FID_LBA_RANGE, |
| 1105 |
.nsid = nsid, |
| 1106 |
.sel = sel, |
| 1107 |
.uuidx = NVME_UUID_NONE, |
| 1108 |
.data = data, |
| 1109 |
.data_len = sizeof(*data), |
| 1110 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 1111 |
.result = result, |
| 1112 |
}; |
| 1113 |
|
| 1114 |
return nvme_get_features(&args); |
| 1115 |
} |
| 1116 |
|
| 1117 |
int nvme_get_features_temp_thresh(int fd, enum nvme_get_features_sel sel, |
| 1118 |
__u32 *result) |
| 1119 |
{ |
| 1120 |
return __nvme_get_features(fd, NVME_FEAT_FID_TEMP_THRESH, sel, result); |
| 1121 |
} |
| 1122 |
|
| 1123 |
int nvme_get_features_temp_thresh2(int fd, enum nvme_get_features_sel sel, __u8 tmpsel, |
| 1124 |
enum nvme_feat_tmpthresh_thsel thsel, __u32 *result) |
| 1125 |
{ |
| 1126 |
struct nvme_get_features_args args = { |
| 1127 |
.args_size = sizeof(args), |
| 1128 |
.fd = fd, |
| 1129 |
.fid = NVME_FEAT_FID_TEMP_THRESH, |
| 1130 |
.nsid = NVME_NSID_NONE, |
| 1131 |
.sel = sel, |
| 1132 |
.cdw11 = NVME_SET(tmpsel, FEAT_TT_TMPSEL) | NVME_SET(thsel, FEAT_TT_THSEL), |
| 1133 |
.uuidx = NVME_UUID_NONE, |
| 1134 |
.data_len = 0, |
| 1135 |
.data = NULL, |
| 1136 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 1137 |
.result = result, |
| 1138 |
}; |
| 1139 |
|
| 1140 |
return nvme_get_features(&args); |
| 1141 |
} |
| 1142 |
|
| 1143 |
int nvme_get_features_err_recovery(int fd, enum nvme_get_features_sel sel, |
| 1144 |
__u32 *result) |
| 1145 |
{ |
| 1146 |
return __nvme_get_features(fd, NVME_FEAT_FID_ERR_RECOVERY, sel, |
| 1147 |
result); |
| 1148 |
} |
| 1149 |
|
| 1150 |
int nvme_get_features_err_recovery2(int fd, enum nvme_get_features_sel sel, |
| 1151 |
__u32 nsid, __u32 *result) |
| 1152 |
{ |
| 1153 |
|
| 1154 |
struct nvme_get_features_args args = { |
| 1155 |
.args_size = sizeof(args), |
| 1156 |
.fd = fd, |
| 1157 |
.fid = NVME_FEAT_FID_ERR_RECOVERY, |
| 1158 |
.nsid = nsid, |
| 1159 |
.sel = sel, |
| 1160 |
.uuidx = NVME_UUID_NONE, |
| 1161 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 1162 |
.result = result, |
| 1163 |
}; |
| 1164 |
|
| 1165 |
return nvme_get_features(&args); |
| 1166 |
} |
| 1167 |
|
| 1168 |
int nvme_get_features_volatile_wc(int fd, enum nvme_get_features_sel sel, |
| 1169 |
__u32 *result) |
| 1170 |
{ |
| 1171 |
return __nvme_get_features(fd, NVME_FEAT_FID_VOLATILE_WC, sel, result); |
| 1172 |
} |
| 1173 |
|
| 1174 |
int nvme_get_features_num_queues(int fd, enum nvme_get_features_sel sel, |
| 1175 |
__u32 *result) |
| 1176 |
{ |
| 1177 |
return __nvme_get_features(fd, NVME_FEAT_FID_NUM_QUEUES, sel, result); |
| 1178 |
} |
| 1179 |
|
| 1180 |
int nvme_get_features_irq_coalesce(int fd, enum nvme_get_features_sel sel, |
| 1181 |
__u32 *result) |
| 1182 |
{ |
| 1183 |
return __nvme_get_features(fd, NVME_FEAT_FID_IRQ_COALESCE, sel, |
| 1184 |
result); |
| 1185 |
} |
| 1186 |
|
| 1187 |
int nvme_get_features_irq_config(int fd, enum nvme_get_features_sel sel, |
| 1188 |
__u16 iv, __u32 *result) |
| 1189 |
{ |
| 1190 |
struct nvme_get_features_args args = { |
| 1191 |
.args_size = sizeof(args), |
| 1192 |
.fd = fd, |
| 1193 |
.fid = NVME_FEAT_FID_IRQ_CONFIG, |
| 1194 |
.nsid = NVME_NSID_NONE, |
| 1195 |
.sel = sel, |
| 1196 |
.cdw11 = iv, |
| 1197 |
.uuidx = NVME_UUID_NONE, |
| 1198 |
.data_len = 0, |
| 1199 |
.data = NULL, |
| 1200 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 1201 |
.result = result, |
| 1202 |
}; |
| 1203 |
|
| 1204 |
return nvme_get_features(&args); |
| 1205 |
} |
| 1206 |
|
| 1207 |
int nvme_get_features_write_atomic(int fd, enum nvme_get_features_sel sel, |
| 1208 |
__u32 *result) |
| 1209 |
{ |
| 1210 |
return __nvme_get_features(fd, NVME_FEAT_FID_WRITE_ATOMIC, sel, |
| 1211 |
result); |
| 1212 |
} |
| 1213 |
|
| 1214 |
int nvme_get_features_async_event(int fd, enum nvme_get_features_sel sel, |
| 1215 |
__u32 *result) |
| 1216 |
{ |
| 1217 |
return __nvme_get_features(fd, NVME_FEAT_FID_ASYNC_EVENT, sel, result); |
| 1218 |
} |
| 1219 |
|
| 1220 |
int nvme_get_features_auto_pst(int fd, enum nvme_get_features_sel sel, |
| 1221 |
struct nvme_feat_auto_pst *apst, __u32 *result) |
| 1222 |
{ |
| 1223 |
struct nvme_get_features_args args = { |
| 1224 |
.args_size = sizeof(args), |
| 1225 |
.fd = fd, |
| 1226 |
.fid = NVME_FEAT_FID_AUTO_PST, |
| 1227 |
.nsid = NVME_NSID_NONE, |
| 1228 |
.sel = sel, |
| 1229 |
.cdw11 = 0, |
| 1230 |
.uuidx = NVME_UUID_NONE, |
| 1231 |
.data_len = sizeof(*apst), |
| 1232 |
.data = apst, |
| 1233 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 1234 |
.result = result, |
| 1235 |
}; |
| 1236 |
|
| 1237 |
return nvme_get_features(&args); |
| 1238 |
} |
| 1239 |
|
| 1240 |
int nvme_get_features_host_mem_buf(int fd, enum nvme_get_features_sel sel, |
| 1241 |
__u32 *result) |
| 1242 |
{ |
| 1243 |
return __nvme_get_features(fd, NVME_FEAT_FID_HOST_MEM_BUF, sel, result); |
| 1244 |
} |
| 1245 |
|
| 1246 |
int nvme_get_features_host_mem_buf2(int fd, enum nvme_get_features_sel sel, |
| 1247 |
struct nvme_host_mem_buf_attrs *attrs, |
| 1248 |
__u32 *result) |
| 1249 |
{ |
| 1250 |
struct nvme_get_features_args args = { |
| 1251 |
.args_size = sizeof(args), |
| 1252 |
.fd = fd, |
| 1253 |
.fid = NVME_FEAT_FID_HOST_MEM_BUF, |
| 1254 |
.nsid = NVME_NSID_NONE, |
| 1255 |
.sel = sel, |
| 1256 |
.uuidx = NVME_UUID_NONE, |
| 1257 |
.data = attrs, |
| 1258 |
.data_len = sizeof(*attrs), |
| 1259 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 1260 |
.result = result, |
| 1261 |
}; |
| 1262 |
|
| 1263 |
return nvme_get_features(&args); |
| 1264 |
} |
| 1265 |
|
| 1266 |
int nvme_get_features_timestamp(int fd, enum nvme_get_features_sel sel, |
| 1267 |
struct nvme_timestamp *ts) |
| 1268 |
{ |
| 1269 |
struct nvme_get_features_args args = { |
| 1270 |
.args_size = sizeof(args), |
| 1271 |
.fd = fd, |
| 1272 |
.fid = NVME_FEAT_FID_TIMESTAMP, |
| 1273 |
.nsid = NVME_NSID_NONE, |
| 1274 |
.sel = sel, |
| 1275 |
.cdw11 = 0, |
| 1276 |
.uuidx = NVME_UUID_NONE, |
| 1277 |
.data_len = sizeof(*ts), |
| 1278 |
.data = ts, |
| 1279 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 1280 |
.result = NULL, |
| 1281 |
}; |
| 1282 |
|
| 1283 |
return nvme_get_features(&args); |
| 1284 |
} |
| 1285 |
|
| 1286 |
int nvme_get_features_kato(int fd, enum nvme_get_features_sel sel, __u32 *result) |
| 1287 |
{ |
| 1288 |
return __nvme_get_features(fd, NVME_FEAT_FID_KATO, sel, result); |
| 1289 |
} |
| 1290 |
|
| 1291 |
int nvme_get_features_hctm(int fd, enum nvme_get_features_sel sel, __u32 *result) |
| 1292 |
{ |
| 1293 |
return __nvme_get_features(fd, NVME_FEAT_FID_HCTM, sel, result); |
| 1294 |
} |
| 1295 |
|
| 1296 |
int nvme_get_features_nopsc(int fd, enum nvme_get_features_sel sel, __u32 *result) |
| 1297 |
{ |
| 1298 |
return __nvme_get_features(fd, NVME_FEAT_FID_NOPSC, sel, result); |
| 1299 |
} |
| 1300 |
|
| 1301 |
int nvme_get_features_rrl(int fd, enum nvme_get_features_sel sel, __u32 *result) |
| 1302 |
{ |
| 1303 |
return __nvme_get_features(fd, NVME_FEAT_FID_RRL, sel, result); |
| 1304 |
} |
| 1305 |
|
| 1306 |
int nvme_get_features_plm_config(int fd, enum nvme_get_features_sel sel, |
| 1307 |
__u16 nvmsetid, struct nvme_plm_config *data, |
| 1308 |
__u32 *result) |
| 1309 |
{ |
| 1310 |
struct nvme_get_features_args args = { |
| 1311 |
.args_size = sizeof(args), |
| 1312 |
.fd = fd, |
| 1313 |
.fid = NVME_FEAT_FID_PLM_CONFIG, |
| 1314 |
.nsid = NVME_NSID_NONE, |
| 1315 |
.sel = sel, |
| 1316 |
.cdw11 = nvmsetid, |
| 1317 |
.uuidx = NVME_UUID_NONE, |
| 1318 |
.data_len = sizeof(*data), |
| 1319 |
.data = data, |
| 1320 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 1321 |
.result = result, |
| 1322 |
}; |
| 1323 |
|
| 1324 |
return nvme_get_features(&args); |
| 1325 |
} |
| 1326 |
|
| 1327 |
int nvme_get_features_plm_window(int fd, enum nvme_get_features_sel sel, |
| 1328 |
__u16 nvmsetid, __u32 *result) |
| 1329 |
{ |
| 1330 |
struct nvme_get_features_args args = { |
| 1331 |
.args_size = sizeof(args), |
| 1332 |
.fd = fd, |
| 1333 |
.fid = NVME_FEAT_FID_PLM_WINDOW, |
| 1334 |
.nsid = NVME_NSID_NONE, |
| 1335 |
.sel = sel, |
| 1336 |
.cdw11 = nvmsetid, |
| 1337 |
.uuidx = NVME_UUID_NONE, |
| 1338 |
.data_len = 0, |
| 1339 |
.data = NULL, |
| 1340 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 1341 |
.result = result, |
| 1342 |
}; |
| 1343 |
|
| 1344 |
return nvme_get_features(&args); |
| 1345 |
} |
| 1346 |
|
| 1347 |
int nvme_get_features_lba_sts_interval(int fd, enum nvme_get_features_sel sel, |
| 1348 |
__u32 *result) |
| 1349 |
{ |
| 1350 |
return __nvme_get_features(fd, NVME_FEAT_FID_LBA_STS_INTERVAL, sel, |
| 1351 |
result); |
| 1352 |
} |
| 1353 |
|
| 1354 |
int nvme_get_features_host_behavior(int fd, enum nvme_get_features_sel sel, |
| 1355 |
struct nvme_feat_host_behavior *data, |
| 1356 |
__u32 *result) |
| 1357 |
{ |
| 1358 |
struct nvme_get_features_args args = { |
| 1359 |
.args_size = sizeof(args), |
| 1360 |
.fd = fd, |
| 1361 |
.fid = NVME_FEAT_FID_HOST_BEHAVIOR, |
| 1362 |
.nsid = NVME_NSID_NONE, |
| 1363 |
.sel = sel, |
| 1364 |
.cdw11 = 0, |
| 1365 |
.uuidx = NVME_UUID_NONE, |
| 1366 |
.data_len = sizeof(*data), |
| 1367 |
.data = data, |
| 1368 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 1369 |
.result = result, |
| 1370 |
}; |
| 1371 |
|
| 1372 |
return nvme_get_features(&args); |
| 1373 |
} |
| 1374 |
|
| 1375 |
int nvme_get_features_sanitize(int fd, enum nvme_get_features_sel sel, |
| 1376 |
__u32 *result) |
| 1377 |
{ |
| 1378 |
return __nvme_get_features(fd, NVME_FEAT_FID_SANITIZE, sel, result); |
| 1379 |
} |
| 1380 |
|
| 1381 |
int nvme_get_features_endurance_event_cfg(int fd, enum nvme_get_features_sel sel, |
| 1382 |
__u16 endgid, __u32 *result) |
| 1383 |
{ |
| 1384 |
struct nvme_get_features_args args = { |
| 1385 |
.args_size = sizeof(args), |
| 1386 |
.fd = fd, |
| 1387 |
.fid = NVME_FEAT_FID_ENDURANCE_EVT_CFG, |
| 1388 |
.nsid = NVME_NSID_NONE, |
| 1389 |
.sel = sel, |
| 1390 |
.cdw11 = endgid, |
| 1391 |
.uuidx = NVME_UUID_NONE, |
| 1392 |
.data_len = 0, |
| 1393 |
.data = NULL, |
| 1394 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 1395 |
.result = result, |
| 1396 |
}; |
| 1397 |
|
| 1398 |
return nvme_get_features(&args); |
| 1399 |
} |
| 1400 |
|
| 1401 |
int nvme_get_features_sw_progress(int fd, enum nvme_get_features_sel sel, |
| 1402 |
__u32 *result) |
| 1403 |
{ |
| 1404 |
return __nvme_get_features(fd, NVME_FEAT_FID_SW_PROGRESS, sel, result); |
| 1405 |
} |
| 1406 |
|
| 1407 |
int nvme_get_features_host_id(int fd, enum nvme_get_features_sel sel, |
| 1408 |
bool exhid, __u32 len, __u8 *hostid) |
| 1409 |
{ |
| 1410 |
struct nvme_get_features_args args = { |
| 1411 |
.args_size = sizeof(args), |
| 1412 |
.fd = fd, |
| 1413 |
.fid = NVME_FEAT_FID_HOST_ID, |
| 1414 |
.nsid = NVME_NSID_NONE, |
| 1415 |
.sel = sel, |
| 1416 |
.cdw11 = !!exhid, |
| 1417 |
.uuidx = NVME_UUID_NONE, |
| 1418 |
.data_len = len, |
| 1419 |
.data = hostid, |
| 1420 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 1421 |
.result = NULL, |
| 1422 |
}; |
| 1423 |
|
| 1424 |
return nvme_get_features(&args); |
| 1425 |
} |
| 1426 |
|
| 1427 |
int nvme_get_features_resv_mask(int fd, enum nvme_get_features_sel sel, |
| 1428 |
__u32 *result) |
| 1429 |
{ |
| 1430 |
return __nvme_get_features(fd, NVME_FEAT_FID_RESV_MASK, sel, result); |
| 1431 |
} |
| 1432 |
|
| 1433 |
int nvme_get_features_resv_mask2(int fd, enum nvme_get_features_sel sel, |
| 1434 |
__u32 nsid, __u32 *result) |
| 1435 |
{ |
| 1436 |
struct nvme_get_features_args args = { |
| 1437 |
.args_size = sizeof(args), |
| 1438 |
.fd = fd, |
| 1439 |
.fid = NVME_FEAT_FID_RESV_MASK, |
| 1440 |
.nsid = nsid, |
| 1441 |
.sel = sel, |
| 1442 |
.uuidx = NVME_UUID_NONE, |
| 1443 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 1444 |
.result = result, |
| 1445 |
}; |
| 1446 |
|
| 1447 |
return nvme_get_features(&args); |
| 1448 |
} |
| 1449 |
|
| 1450 |
int nvme_get_features_resv_persist(int fd, enum nvme_get_features_sel sel, |
| 1451 |
__u32 *result) |
| 1452 |
{ |
| 1453 |
return __nvme_get_features(fd, NVME_FEAT_FID_RESV_PERSIST, sel, result); |
| 1454 |
} |
| 1455 |
|
| 1456 |
int nvme_get_features_resv_persist2(int fd, enum nvme_get_features_sel sel, |
| 1457 |
__u32 nsid, __u32 *result) |
| 1458 |
{ |
| 1459 |
struct nvme_get_features_args args = { |
| 1460 |
.args_size = sizeof(args), |
| 1461 |
.fd = fd, |
| 1462 |
.fid = NVME_FEAT_FID_RESV_PERSIST, |
| 1463 |
.nsid = nsid, |
| 1464 |
.sel = sel, |
| 1465 |
.uuidx = NVME_UUID_NONE, |
| 1466 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 1467 |
.result = result, |
| 1468 |
}; |
| 1469 |
|
| 1470 |
return nvme_get_features(&args); |
| 1471 |
} |
| 1472 |
|
| 1473 |
int nvme_get_features_write_protect(int fd, __u32 nsid, |
| 1474 |
enum nvme_get_features_sel sel, |
| 1475 |
__u32 *result) |
| 1476 |
{ |
| 1477 |
struct nvme_get_features_args args = { |
| 1478 |
.args_size = sizeof(args), |
| 1479 |
.fd = fd, |
| 1480 |
.fid = NVME_FEAT_FID_WRITE_PROTECT, |
| 1481 |
.nsid = nsid, |
| 1482 |
.sel = sel, |
| 1483 |
.cdw11 = 0, |
| 1484 |
.uuidx = NVME_UUID_NONE, |
| 1485 |
.data_len = 0, |
| 1486 |
.data = NULL, |
| 1487 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 1488 |
.result = result, |
| 1489 |
}; |
| 1490 |
|
| 1491 |
return nvme_get_features(&args); |
| 1492 |
} |
| 1493 |
|
| 1494 |
int nvme_get_features_iocs_profile(int fd, enum nvme_get_features_sel sel, |
| 1495 |
__u32 *result) |
| 1496 |
{ |
| 1497 |
return __nvme_get_features(fd, NVME_FEAT_FID_IOCS_PROFILE, sel, result); |
| 1498 |
} |
| 1499 |
|
| 1500 |
int nvme_format_nvm(struct nvme_format_nvm_args *args) |
| 1501 |
{ |
| 1502 |
const size_t size_v1 = sizeof_args(struct nvme_format_nvm_args, lbaf, __u64); |
| 1503 |
const size_t size_v2 = sizeof_args(struct nvme_format_nvm_args, lbafu, __u64); |
| 1504 |
__u32 cdw10; |
| 1505 |
|
| 1506 |
if (args->args_size < size_v1 || args->args_size > size_v2) { |
| 1507 |
errno = EINVAL; |
| 1508 |
return -1; |
| 1509 |
} |
| 1510 |
|
| 1511 |
cdw10 = NVME_SET(args->lbaf, FORMAT_CDW10_LBAF) | |
| 1512 |
NVME_SET(args->mset, FORMAT_CDW10_MSET) | |
| 1513 |
NVME_SET(args->pi, FORMAT_CDW10_PI) | |
| 1514 |
NVME_SET(args->pil, FORMAT_CDW10_PIL) | |
| 1515 |
NVME_SET(args->ses, FORMAT_CDW10_SES); |
| 1516 |
|
| 1517 |
if (args->args_size == size_v2) { |
| 1518 |
/* set lbafu extension */ |
| 1519 |
cdw10 |= NVME_SET(args->lbafu, FORMAT_CDW10_LBAFU); |
| 1520 |
} |
| 1521 |
|
| 1522 |
struct nvme_passthru_cmd cmd = { |
| 1523 |
.opcode = nvme_admin_format_nvm, |
| 1524 |
.nsid = args->nsid, |
| 1525 |
.cdw10 = cdw10, |
| 1526 |
.timeout_ms = args->timeout, |
| 1527 |
}; |
| 1528 |
|
| 1529 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 1530 |
} |
| 1531 |
|
| 1532 |
int nvme_ns_mgmt(struct nvme_ns_mgmt_args *args) |
| 1533 |
{ |
| 1534 |
const size_t size_v1 = sizeof_args(struct nvme_ns_mgmt_args, csi, __u64); |
| 1535 |
const size_t size_v2 = sizeof_args(struct nvme_ns_mgmt_args, data, __u64); |
| 1536 |
__u32 cdw10 = NVME_SET(args->sel, NAMESPACE_MGMT_CDW10_SEL); |
| 1537 |
__u32 cdw11 = NVME_SET(args->csi, NAMESPACE_MGMT_CDW11_CSI); |
| 1538 |
|
| 1539 |
if (args->args_size < size_v1 || args->args_size > size_v2) { |
| 1540 |
errno = EINVAL; |
| 1541 |
return -1; |
| 1542 |
} |
| 1543 |
|
| 1544 |
struct nvme_passthru_cmd cmd = { |
| 1545 |
.nsid = args->nsid, |
| 1546 |
.opcode = nvme_admin_ns_mgmt, |
| 1547 |
.cdw10 = cdw10, |
| 1548 |
.cdw11 = cdw11, |
| 1549 |
.timeout_ms = args->timeout, |
| 1550 |
}; |
| 1551 |
|
| 1552 |
if (args->args_size == size_v2) { |
| 1553 |
if (args->data) { |
| 1554 |
cmd.data_len = sizeof(*args->data); |
| 1555 |
cmd.addr = (__u64)(uintptr_t)args->data; |
| 1556 |
} |
| 1557 |
} |
| 1558 |
else { |
| 1559 |
if (args->ns) { |
| 1560 |
cmd.data_len = sizeof(*args->ns); |
| 1561 |
cmd.addr = (__u64)(uintptr_t)args->ns; |
| 1562 |
} |
| 1563 |
} |
| 1564 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 1565 |
} |
| 1566 |
|
| 1567 |
int nvme_ns_attach(struct nvme_ns_attach_args *args) |
| 1568 |
{ |
| 1569 |
__u32 cdw10 = NVME_SET(args->sel, NAMESPACE_ATTACH_CDW10_SEL); |
| 1570 |
|
| 1571 |
struct nvme_passthru_cmd cmd = { |
| 1572 |
.opcode = nvme_admin_ns_attach, |
| 1573 |
.nsid = args->nsid, |
| 1574 |
.cdw10 = cdw10, |
| 1575 |
.data_len = sizeof(*args->ctrlist), |
| 1576 |
.addr = (__u64)(uintptr_t)args->ctrlist, |
| 1577 |
.timeout_ms = args->timeout, |
| 1578 |
}; |
| 1579 |
|
| 1580 |
if (args->args_size < sizeof(*args)) { |
| 1581 |
errno = EINVAL; |
| 1582 |
return -1; |
| 1583 |
} |
| 1584 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 1585 |
} |
| 1586 |
|
| 1587 |
int nvme_fw_download(struct nvme_fw_download_args *args) |
| 1588 |
{ |
| 1589 |
__u32 cdw10 = (args->data_len >> 2) - 1; |
| 1590 |
__u32 cdw11 = args->offset >> 2; |
| 1591 |
|
| 1592 |
struct nvme_passthru_cmd cmd = { |
| 1593 |
.opcode = nvme_admin_fw_download, |
| 1594 |
.cdw10 = cdw10, |
| 1595 |
.cdw11 = cdw11, |
| 1596 |
.data_len = args->data_len, |
| 1597 |
.addr = (__u64)(uintptr_t)args->data, |
| 1598 |
.timeout_ms = args->timeout, |
| 1599 |
}; |
| 1600 |
|
| 1601 |
if (args->args_size < sizeof(*args)) { |
| 1602 |
errno = EINVAL; |
| 1603 |
return -1; |
| 1604 |
} |
| 1605 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 1606 |
} |
| 1607 |
|
| 1608 |
int nvme_fw_commit(struct nvme_fw_commit_args *args) |
| 1609 |
{ |
| 1610 |
__u32 cdw10 = NVME_SET(args->slot, FW_COMMIT_CDW10_FS) | |
| 1611 |
NVME_SET(args->action, FW_COMMIT_CDW10_CA) | |
| 1612 |
NVME_SET(args->bpid, FW_COMMIT_CDW10_BPID); |
| 1613 |
|
| 1614 |
struct nvme_passthru_cmd cmd = { |
| 1615 |
.opcode = nvme_admin_fw_commit, |
| 1616 |
.cdw10 = cdw10, |
| 1617 |
.timeout_ms = args->timeout, |
| 1618 |
}; |
| 1619 |
|
| 1620 |
if (args->args_size < sizeof(*args)) { |
| 1621 |
errno = EINVAL; |
| 1622 |
return -1; |
| 1623 |
} |
| 1624 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 1625 |
} |
| 1626 |
|
| 1627 |
int nvme_security_send(struct nvme_security_send_args *args) |
| 1628 |
{ |
| 1629 |
__u32 cdw10 = NVME_SET(args->secp, SECURITY_SECP) | |
| 1630 |
NVME_SET(args->spsp0, SECURITY_SPSP0) | |
| 1631 |
NVME_SET(args->spsp1, SECURITY_SPSP1) | |
| 1632 |
NVME_SET(args->nssf, SECURITY_NSSF); |
| 1633 |
__u32 cdw11 = args->tl; |
| 1634 |
|
| 1635 |
struct nvme_passthru_cmd cmd = { |
| 1636 |
.opcode = nvme_admin_security_send, |
| 1637 |
.nsid = args->nsid, |
| 1638 |
.cdw10 = cdw10, |
| 1639 |
.cdw11 = cdw11, |
| 1640 |
.data_len = args->data_len, |
| 1641 |
.addr = (__u64)(uintptr_t)args->data, |
| 1642 |
.timeout_ms = args->timeout, |
| 1643 |
}; |
| 1644 |
|
| 1645 |
if (args->args_size < sizeof(*args)) { |
| 1646 |
errno = EINVAL; |
| 1647 |
return -1; |
| 1648 |
} |
| 1649 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 1650 |
} |
| 1651 |
|
| 1652 |
int nvme_security_receive(struct nvme_security_receive_args *args) |
| 1653 |
{ |
| 1654 |
__u32 cdw10 = NVME_SET(args->secp, SECURITY_SECP) | |
| 1655 |
NVME_SET(args->spsp0, SECURITY_SPSP0) | |
| 1656 |
NVME_SET(args->spsp1, SECURITY_SPSP1) | |
| 1657 |
NVME_SET(args->nssf, SECURITY_NSSF); |
| 1658 |
__u32 cdw11 = args->al; |
| 1659 |
|
| 1660 |
struct nvme_passthru_cmd cmd = { |
| 1661 |
.opcode = nvme_admin_security_recv, |
| 1662 |
.nsid = args->nsid, |
| 1663 |
.cdw10 = cdw10, |
| 1664 |
.cdw11 = cdw11, |
| 1665 |
.data_len = args->data_len, |
| 1666 |
.addr = (__u64)(uintptr_t)args->data, |
| 1667 |
.timeout_ms = args->timeout, |
| 1668 |
}; |
| 1669 |
|
| 1670 |
if (args->args_size < sizeof(*args)) { |
| 1671 |
errno = EINVAL; |
| 1672 |
return -1; |
| 1673 |
} |
| 1674 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 1675 |
} |
| 1676 |
|
| 1677 |
int nvme_get_lba_status(struct nvme_get_lba_status_args *args) |
| 1678 |
{ |
| 1679 |
__u32 cdw10 = args->slba & 0xffffffff; |
| 1680 |
__u32 cdw11 = args->slba >> 32; |
| 1681 |
__u32 cdw12 = args->mndw; |
| 1682 |
__u32 cdw13 = NVME_SET(args->rl, GET_LBA_STATUS_CDW13_RL) | |
| 1683 |
NVME_SET(args->atype, GET_LBA_STATUS_CDW13_ATYPE); |
| 1684 |
|
| 1685 |
struct nvme_passthru_cmd cmd = { |
| 1686 |
.opcode = nvme_admin_get_lba_status, |
| 1687 |
.nsid = args->nsid, |
| 1688 |
.addr = (__u64)(uintptr_t)args->lbas, |
| 1689 |
.data_len = (args->mndw + 1) << 2, |
| 1690 |
.cdw10 = cdw10, |
| 1691 |
.cdw11 = cdw11, |
| 1692 |
.cdw12 = cdw12, |
| 1693 |
.cdw13 = cdw13, |
| 1694 |
.timeout_ms = args->timeout, |
| 1695 |
}; |
| 1696 |
|
| 1697 |
if (args->args_size < sizeof(*args)) { |
| 1698 |
errno = EINVAL; |
| 1699 |
return -1; |
| 1700 |
} |
| 1701 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 1702 |
} |
| 1703 |
|
| 1704 |
int nvme_directive_send(struct nvme_directive_send_args *args) |
| 1705 |
{ |
| 1706 |
__u32 cdw10 = args->data_len ? (args->data_len >> 2) - 1 : 0; |
| 1707 |
__u32 cdw11 = NVME_SET(args->doper, DIRECTIVE_CDW11_DOPER) | |
| 1708 |
NVME_SET(args->dtype, DIRECTIVE_CDW11_DTYPE) | |
| 1709 |
NVME_SET(args->dspec, DIRECTIVE_CDW11_DPSEC); |
| 1710 |
|
| 1711 |
struct nvme_passthru_cmd cmd = { |
| 1712 |
.opcode = nvme_admin_directive_send, |
| 1713 |
.nsid = args->nsid, |
| 1714 |
.cdw10 = cdw10, |
| 1715 |
.cdw11 = cdw11, |
| 1716 |
.cdw12 = args->cdw12, |
| 1717 |
.data_len = args->data_len, |
| 1718 |
.addr = (__u64)(uintptr_t)args->data, |
| 1719 |
.timeout_ms = args->timeout, |
| 1720 |
}; |
| 1721 |
|
| 1722 |
if (args->args_size < sizeof(*args)) { |
| 1723 |
errno = EINVAL; |
| 1724 |
return -1; |
| 1725 |
} |
| 1726 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 1727 |
} |
| 1728 |
|
| 1729 |
int nvme_directive_send_id_endir(int fd, __u32 nsid, bool endir, |
| 1730 |
enum nvme_directive_dtype dtype, |
| 1731 |
struct nvme_id_directives *id) |
| 1732 |
{ |
| 1733 |
__u32 cdw12 = NVME_SET(dtype, DIRECTIVE_SEND_IDENTIFY_CDW12_DTYPE) | |
| 1734 |
NVME_SET(endir, DIRECTIVE_SEND_IDENTIFY_CDW12_ENDIR); |
| 1735 |
struct nvme_directive_send_args args = { |
| 1736 |
.args_size = sizeof(args), |
| 1737 |
.fd = fd, |
| 1738 |
.nsid = nsid, |
| 1739 |
.dspec = 0, |
| 1740 |
.dtype = NVME_DIRECTIVE_DTYPE_IDENTIFY, |
| 1741 |
.doper = NVME_DIRECTIVE_SEND_IDENTIFY_DOPER_ENDIR, |
| 1742 |
.cdw12 = cdw12, |
| 1743 |
.data_len = sizeof(*id), |
| 1744 |
.data = id, |
| 1745 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 1746 |
.result = NULL, |
| 1747 |
}; |
| 1748 |
|
| 1749 |
return nvme_directive_send(&args); |
| 1750 |
} |
| 1751 |
|
| 1752 |
int nvme_directive_recv(struct nvme_directive_recv_args *args) |
| 1753 |
{ |
| 1754 |
__u32 cdw10 = args->data_len ? (args->data_len >> 2) - 1 : 0; |
| 1755 |
__u32 cdw11 = NVME_SET(args->doper, DIRECTIVE_CDW11_DOPER) | |
| 1756 |
NVME_SET(args->dtype, DIRECTIVE_CDW11_DTYPE) | |
| 1757 |
NVME_SET(args->dspec, DIRECTIVE_CDW11_DPSEC); |
| 1758 |
|
| 1759 |
struct nvme_passthru_cmd cmd = { |
| 1760 |
.opcode = nvme_admin_directive_recv, |
| 1761 |
.nsid = args->nsid, |
| 1762 |
.cdw10 = cdw10, |
| 1763 |
.cdw11 = cdw11, |
| 1764 |
.cdw12 = args->cdw12, |
| 1765 |
.data_len = args->data_len, |
| 1766 |
.addr = (__u64)(uintptr_t)args->data, |
| 1767 |
.timeout_ms = args->timeout, |
| 1768 |
}; |
| 1769 |
|
| 1770 |
if (args->args_size < sizeof(*args)) { |
| 1771 |
errno = EINVAL; |
| 1772 |
return -1; |
| 1773 |
} |
| 1774 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 1775 |
} |
| 1776 |
|
| 1777 |
int nvme_capacity_mgmt(struct nvme_capacity_mgmt_args *args) |
| 1778 |
{ |
| 1779 |
__u32 cdw10 = args->op | args->element_id << 16; |
| 1780 |
|
| 1781 |
struct nvme_passthru_cmd cmd = { |
| 1782 |
.opcode = nvme_admin_capacity_mgmt, |
| 1783 |
.cdw10 = cdw10, |
| 1784 |
.cdw11 = args->cdw11, |
| 1785 |
.cdw12 = args->cdw12, |
| 1786 |
.timeout_ms = args->timeout, |
| 1787 |
}; |
| 1788 |
|
| 1789 |
if (args->args_size < sizeof(*args)) { |
| 1790 |
errno = EINVAL; |
| 1791 |
return -1; |
| 1792 |
} |
| 1793 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 1794 |
} |
| 1795 |
|
| 1796 |
int nvme_lockdown(struct nvme_lockdown_args *args) |
| 1797 |
{ |
| 1798 |
__u32 cdw10 = args->ofi << 8 | |
| 1799 |
(args->ifc & 0x3) << 5 | |
| 1800 |
(args->prhbt & 0x1) << 4 | |
| 1801 |
(args->scp & 0xF); |
| 1802 |
|
| 1803 |
struct nvme_passthru_cmd cmd = { |
| 1804 |
.opcode = nvme_admin_lockdown, |
| 1805 |
.cdw10 = cdw10, |
| 1806 |
.cdw14 = args->uuidx & 0x3F, |
| 1807 |
.timeout_ms = args->timeout, |
| 1808 |
}; |
| 1809 |
|
| 1810 |
if (args->args_size < sizeof(*args)) { |
| 1811 |
errno = EINVAL; |
| 1812 |
return -1; |
| 1813 |
} |
| 1814 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 1815 |
} |
| 1816 |
|
| 1817 |
int nvme_set_property(struct nvme_set_property_args *args) |
| 1818 |
{ |
| 1819 |
__u32 cdw10 = nvme_is_64bit_reg(args->offset); |
| 1820 |
|
| 1821 |
struct nvme_passthru_cmd cmd = { |
| 1822 |
.opcode = nvme_admin_fabrics, |
| 1823 |
.nsid = nvme_fabrics_type_property_set, |
| 1824 |
.cdw10 = cdw10, |
| 1825 |
.cdw11 = args->offset, |
| 1826 |
.cdw12 = args->value & 0xffffffff, |
| 1827 |
.cdw13 = args->value >> 32, |
| 1828 |
.timeout_ms = args->timeout, |
| 1829 |
}; |
| 1830 |
|
| 1831 |
if (args->args_size < sizeof(*args)) { |
| 1832 |
errno = EINVAL; |
| 1833 |
return -1; |
| 1834 |
} |
| 1835 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 1836 |
} |
| 1837 |
|
| 1838 |
int nvme_get_property(struct nvme_get_property_args *args) |
| 1839 |
{ |
| 1840 |
__u32 cdw10 = nvme_is_64bit_reg(args->offset); |
| 1841 |
|
| 1842 |
struct nvme_passthru_cmd64 cmd = { |
| 1843 |
.opcode = nvme_admin_fabrics, |
| 1844 |
.nsid = nvme_fabrics_type_property_get, |
| 1845 |
.cdw10 = cdw10, |
| 1846 |
.cdw11 = args->offset, |
| 1847 |
.timeout_ms = args->timeout, |
| 1848 |
}; |
| 1849 |
|
| 1850 |
if (args->args_size < sizeof(*args)) { |
| 1851 |
errno = EINVAL; |
| 1852 |
return -1; |
| 1853 |
} |
| 1854 |
return nvme_submit_admin_passthru64(args->fd, &cmd, args->value); |
| 1855 |
} |
| 1856 |
|
| 1857 |
int nvme_sanitize_nvm(struct nvme_sanitize_nvm_args *args) |
| 1858 |
{ |
| 1859 |
const size_t size_v1 = sizeof_args(struct nvme_sanitize_nvm_args, nodas, __u64); |
| 1860 |
const size_t size_v2 = sizeof_args(struct nvme_sanitize_nvm_args, emvs, __u64); |
| 1861 |
__u32 cdw10, cdw11; |
| 1862 |
|
| 1863 |
if (args->args_size < size_v1 || args->args_size > size_v2) { |
| 1864 |
errno = EINVAL; |
| 1865 |
return -1; |
| 1866 |
} |
| 1867 |
|
| 1868 |
cdw10 = NVME_SET(args->sanact, SANITIZE_CDW10_SANACT) | |
| 1869 |
NVME_SET(!!args->ause, SANITIZE_CDW10_AUSE) | |
| 1870 |
NVME_SET(args->owpass, SANITIZE_CDW10_OWPASS) | |
| 1871 |
NVME_SET(!!args->oipbp, SANITIZE_CDW10_OIPBP) | |
| 1872 |
NVME_SET(!!args->nodas, SANITIZE_CDW10_NODAS); |
| 1873 |
|
| 1874 |
if (args->args_size == size_v2) |
| 1875 |
cdw10 |= NVME_SET(!!args->emvs, SANITIZE_CDW10_EMVS); |
| 1876 |
|
| 1877 |
cdw11 = args->ovrpat; |
| 1878 |
|
| 1879 |
struct nvme_passthru_cmd cmd = { |
| 1880 |
.opcode = nvme_admin_sanitize_nvm, |
| 1881 |
.cdw10 = cdw10, |
| 1882 |
.cdw11 = cdw11, |
| 1883 |
.timeout_ms = args->timeout, |
| 1884 |
}; |
| 1885 |
|
| 1886 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 1887 |
} |
| 1888 |
|
| 1889 |
int nvme_dev_self_test(struct nvme_dev_self_test_args *args) |
| 1890 |
{ |
| 1891 |
__u32 cdw10 = NVME_SET(args->stc, DEVICE_SELF_TEST_CDW10_STC); |
| 1892 |
|
| 1893 |
struct nvme_passthru_cmd cmd = { |
| 1894 |
.opcode = nvme_admin_dev_self_test, |
| 1895 |
.nsid = args->nsid, |
| 1896 |
.cdw10 = cdw10, |
| 1897 |
.timeout_ms = args->timeout, |
| 1898 |
}; |
| 1899 |
|
| 1900 |
if (args->args_size < sizeof(*args)) { |
| 1901 |
errno = EINVAL; |
| 1902 |
return -1; |
| 1903 |
} |
| 1904 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 1905 |
} |
| 1906 |
|
| 1907 |
int nvme_virtual_mgmt(struct nvme_virtual_mgmt_args *args) |
| 1908 |
{ |
| 1909 |
__u32 cdw10 = NVME_SET(args->act, VIRT_MGMT_CDW10_ACT) | |
| 1910 |
NVME_SET(args->rt, VIRT_MGMT_CDW10_RT) | |
| 1911 |
NVME_SET(args->cntlid, VIRT_MGMT_CDW10_CNTLID); |
| 1912 |
__u32 cdw11 = NVME_SET(args->nr, VIRT_MGMT_CDW11_NR); |
| 1913 |
|
| 1914 |
struct nvme_passthru_cmd cmd = { |
| 1915 |
.opcode = nvme_admin_virtual_mgmt, |
| 1916 |
.cdw10 = cdw10, |
| 1917 |
.cdw11 = cdw11, |
| 1918 |
.timeout_ms = args->timeout, |
| 1919 |
}; |
| 1920 |
|
| 1921 |
if (args->args_size < sizeof(*args)) { |
| 1922 |
errno = EINVAL; |
| 1923 |
return -1; |
| 1924 |
} |
| 1925 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 1926 |
} |
| 1927 |
|
| 1928 |
int nvme_submit_io_passthru64(int fd, struct nvme_passthru_cmd64 *cmd, |
| 1929 |
__u64 *result) |
| 1930 |
{ |
| 1931 |
return nvme_submit_passthru64(fd, NVME_IOCTL_IO64_CMD, cmd, result); |
| 1932 |
} |
| 1933 |
|
| 1934 |
int nvme_io_passthru64(int fd, __u8 opcode, __u8 flags, __u16 rsvd, |
| 1935 |
__u32 nsid, __u32 cdw2, __u32 cdw3, __u32 cdw10, |
| 1936 |
__u32 cdw11, __u32 cdw12, __u32 cdw13, __u32 cdw14, |
| 1937 |
__u32 cdw15, __u32 data_len, void *data, __u32 metadata_len, |
| 1938 |
void *metadata, __u32 timeout_ms, __u64 *result) |
| 1939 |
{ |
| 1940 |
return nvme_passthru64(fd, NVME_IOCTL_IO64_CMD, opcode, flags, rsvd, |
| 1941 |
nsid, cdw2, cdw3, cdw10, cdw11, cdw12, cdw13, |
| 1942 |
cdw14, cdw15, data_len, data, metadata_len, metadata, |
| 1943 |
timeout_ms, result); |
| 1944 |
} |
| 1945 |
|
| 1946 |
int nvme_submit_io_passthru(int fd, struct nvme_passthru_cmd *cmd, __u32 *result) |
| 1947 |
{ |
| 1948 |
return nvme_submit_passthru(fd, NVME_IOCTL_IO_CMD, cmd, result); |
| 1949 |
} |
| 1950 |
|
| 1951 |
int nvme_io_passthru(int fd, __u8 opcode, __u8 flags, __u16 rsvd, |
| 1952 |
__u32 nsid, __u32 cdw2, __u32 cdw3, __u32 cdw10, |
| 1953 |
__u32 cdw11, __u32 cdw12, __u32 cdw13, __u32 cdw14, |
| 1954 |
__u32 cdw15, __u32 data_len, void *data, __u32 metadata_len, |
| 1955 |
void *metadata, __u32 timeout_ms, __u32 *result) |
| 1956 |
{ |
| 1957 |
return nvme_passthru(fd, NVME_IOCTL_IO_CMD, opcode, flags, rsvd, nsid, |
| 1958 |
cdw2, cdw3, cdw10, cdw11, cdw12, cdw13, cdw14, |
| 1959 |
cdw15, data_len, data, metadata_len, metadata, |
| 1960 |
timeout_ms, result); |
| 1961 |
} |
| 1962 |
|
| 1963 |
static int nvme_set_var_size_tags(__u32 *cmd_dw2, __u32 *cmd_dw3, __u32 *cmd_dw14, |
| 1964 |
__u8 pif, __u8 sts, __u64 reftag, __u64 storage_tag) |
| 1965 |
{ |
| 1966 |
__u32 cdw2 = 0, cdw3 = 0, cdw14; |
| 1967 |
|
| 1968 |
switch (pif) { |
| 1969 |
case NVME_NVM_PIF_16B_GUARD: |
| 1970 |
cdw14 = reftag & 0xffffffff; |
| 1971 |
cdw14 |= ((storage_tag << (32 - sts)) & 0xffffffff); |
| 1972 |
break; |
| 1973 |
case NVME_NVM_PIF_32B_GUARD: |
| 1974 |
cdw14 = reftag & 0xffffffff; |
| 1975 |
cdw3 = reftag >> 32; |
| 1976 |
cdw14 |= ((storage_tag << (80 - sts)) & 0xffff0000); |
| 1977 |
if (sts >= 48) |
| 1978 |
cdw3 |= ((storage_tag >> (sts - 48)) & 0xffffffff); |
| 1979 |
else |
| 1980 |
cdw3 |= ((storage_tag << (48 - sts)) & 0xffffffff); |
| 1981 |
cdw2 = (storage_tag >> (sts - 16)) & 0xffff; |
| 1982 |
break; |
| 1983 |
case NVME_NVM_PIF_64B_GUARD: |
| 1984 |
cdw14 = reftag & 0xffffffff; |
| 1985 |
cdw3 = (reftag >> 32) & 0xffff; |
| 1986 |
cdw14 |= ((storage_tag << (48 - sts)) & 0xffffffff); |
| 1987 |
if (sts >= 16) |
| 1988 |
cdw3 |= ((storage_tag >> (sts - 16)) & 0xffff); |
| 1989 |
else |
| 1990 |
cdw3 |= ((storage_tag << (16 - sts)) & 0xffff); |
| 1991 |
break; |
| 1992 |
default: |
| 1993 |
perror("Unsupported Protection Information Format"); |
| 1994 |
errno = EINVAL; |
| 1995 |
return -1; |
| 1996 |
} |
| 1997 |
|
| 1998 |
*cmd_dw2 = cdw2; |
| 1999 |
*cmd_dw3 = cdw3; |
| 2000 |
*cmd_dw14 = cdw14; |
| 2001 |
return 0; |
| 2002 |
} |
| 2003 |
|
| 2004 |
int nvme_io(struct nvme_io_args *args, __u8 opcode) |
| 2005 |
{ |
| 2006 |
const size_t size_v1 = sizeof_args(struct nvme_io_args, dsm, __u64); |
| 2007 |
const size_t size_v2 = sizeof_args(struct nvme_io_args, pif, __u64); |
| 2008 |
__u32 cdw2, cdw3, cdw10, cdw11, cdw12, cdw13, cdw14, cdw15; |
| 2009 |
|
| 2010 |
if (args->args_size < size_v1 || args->args_size > size_v2) { |
| 2011 |
errno = EINVAL; |
| 2012 |
return -1; |
| 2013 |
} |
| 2014 |
|
| 2015 |
cdw10 = args->slba & 0xffffffff; |
| 2016 |
cdw11 = args->slba >> 32; |
| 2017 |
cdw12 = args->nlb | (args->control << 16); |
| 2018 |
cdw13 = args->dsm | (args->dspec << 16); |
| 2019 |
cdw15 = args->apptag | (args->appmask << 16); |
| 2020 |
|
| 2021 |
if (args->args_size == size_v1) { |
| 2022 |
cdw2 = (args->storage_tag >> 32) & 0xffff; |
| 2023 |
cdw3 = args->storage_tag & 0xffffffff; |
| 2024 |
cdw14 = args->reftag; |
| 2025 |
} else { |
| 2026 |
if (nvme_set_var_size_tags(&cdw2, &cdw3, &cdw14, |
| 2027 |
args->pif, |
| 2028 |
args->sts, |
| 2029 |
args->reftag_u64, |
| 2030 |
args->storage_tag)) { |
| 2031 |
errno = EINVAL; |
| 2032 |
return -1; |
| 2033 |
} |
| 2034 |
} |
| 2035 |
|
| 2036 |
struct nvme_passthru_cmd cmd = { |
| 2037 |
.opcode = opcode, |
| 2038 |
.nsid = args->nsid, |
| 2039 |
.cdw2 = cdw2, |
| 2040 |
.cdw3 = cdw3, |
| 2041 |
.cdw10 = cdw10, |
| 2042 |
.cdw11 = cdw11, |
| 2043 |
.cdw12 = cdw12, |
| 2044 |
.cdw13 = cdw13, |
| 2045 |
.cdw14 = cdw14, |
| 2046 |
.cdw15 = cdw15, |
| 2047 |
.data_len = args->data_len, |
| 2048 |
.metadata_len = args->metadata_len, |
| 2049 |
.addr = (__u64)(uintptr_t)args->data, |
| 2050 |
.metadata = (__u64)(uintptr_t)args->metadata, |
| 2051 |
.timeout_ms = args->timeout, |
| 2052 |
}; |
| 2053 |
|
| 2054 |
return nvme_submit_io_passthru(args->fd, &cmd, args->result); |
| 2055 |
} |
| 2056 |
|
| 2057 |
int nvme_dsm(struct nvme_dsm_args *args) |
| 2058 |
{ |
| 2059 |
struct nvme_passthru_cmd cmd = { |
| 2060 |
.opcode = nvme_cmd_dsm, |
| 2061 |
.nsid = args->nsid, |
| 2062 |
.addr = (__u64)(uintptr_t)args->dsm, |
| 2063 |
.data_len = args->nr_ranges * sizeof(*args->dsm), |
| 2064 |
.cdw10 = args->nr_ranges - 1, |
| 2065 |
.cdw11 = args->attrs, |
| 2066 |
.timeout_ms = args->timeout, |
| 2067 |
}; |
| 2068 |
|
| 2069 |
if (args->args_size < sizeof(*args)) { |
| 2070 |
errno = EINVAL; |
| 2071 |
return -1; |
| 2072 |
} |
| 2073 |
return nvme_submit_io_passthru(args->fd, &cmd, args->result); |
| 2074 |
} |
| 2075 |
|
| 2076 |
int nvme_copy(struct nvme_copy_args *args) |
| 2077 |
{ |
| 2078 |
const size_t size_v1 = sizeof_args(struct nvme_copy_args, format, __u64); |
| 2079 |
const size_t size_v2 = sizeof_args(struct nvme_copy_args, ilbrt_u64, __u64); |
| 2080 |
__u32 cdw3, cdw12, cdw14, data_len; |
| 2081 |
|
| 2082 |
if (args->args_size < size_v1 || args->args_size > size_v2) { |
| 2083 |
errno = EINVAL; |
| 2084 |
return -1; |
| 2085 |
} |
| 2086 |
|
| 2087 |
cdw12 = ((args->nr - 1) & 0xff) | ((args->format & 0xf) << 8) | |
| 2088 |
((args->prinfor & 0xf) << 12) | ((args->dtype & 0xf) << 20) | |
| 2089 |
((args->prinfow & 0xf) << 26) | ((args->fua & 0x1) << 30) | |
| 2090 |
((args->lr & 0x1) << 31); |
| 2091 |
|
| 2092 |
if (args->args_size == size_v1) { |
| 2093 |
cdw3 = 0; |
| 2094 |
cdw14 = args->ilbrt; |
| 2095 |
} else { |
| 2096 |
cdw3 = (args->ilbrt_u64 >> 32) & 0xffffffff; |
| 2097 |
cdw14 = args->ilbrt_u64 & 0xffffffff; |
| 2098 |
} |
| 2099 |
|
| 2100 |
if (args->format == 1) |
| 2101 |
data_len = args->nr * sizeof(struct nvme_copy_range_f1); |
| 2102 |
else if (args->format == 2) |
| 2103 |
data_len = args->nr * sizeof(struct nvme_copy_range_f2); |
| 2104 |
else if (args->format == 3) |
| 2105 |
data_len = args->nr * sizeof(struct nvme_copy_range_f3); |
| 2106 |
else |
| 2107 |
data_len = args->nr * sizeof(struct nvme_copy_range); |
| 2108 |
|
| 2109 |
struct nvme_passthru_cmd cmd = { |
| 2110 |
.opcode = nvme_cmd_copy, |
| 2111 |
.nsid = args->nsid, |
| 2112 |
.addr = (__u64)(uintptr_t)args->copy, |
| 2113 |
.data_len = data_len, |
| 2114 |
.cdw3 = cdw3, |
| 2115 |
.cdw10 = args->sdlba & 0xffffffff, |
| 2116 |
.cdw11 = args->sdlba >> 32, |
| 2117 |
.cdw12 = cdw12, |
| 2118 |
.cdw13 = (args->dspec & 0xffff) << 16, |
| 2119 |
.cdw14 = cdw14, |
| 2120 |
.cdw15 = (args->lbatm << 16) | args->lbat, |
| 2121 |
.timeout_ms = args->timeout, |
| 2122 |
}; |
| 2123 |
|
| 2124 |
return nvme_submit_io_passthru(args->fd, &cmd, args->result); |
| 2125 |
} |
| 2126 |
|
| 2127 |
int nvme_resv_acquire(struct nvme_resv_acquire_args *args) |
| 2128 |
{ |
| 2129 |
__le64 payload[2] = { |
| 2130 |
cpu_to_le64(args->crkey), |
| 2131 |
cpu_to_le64(args->nrkey) |
| 2132 |
}; |
| 2133 |
__u32 cdw10 = (args->racqa & 0x7) | |
| 2134 |
(args->iekey ? 1 << 3 : 0) | |
| 2135 |
(args->rtype << 8); |
| 2136 |
|
| 2137 |
struct nvme_passthru_cmd cmd = { |
| 2138 |
.opcode = nvme_cmd_resv_acquire, |
| 2139 |
.nsid = args->nsid, |
| 2140 |
.cdw10 = cdw10, |
| 2141 |
.data_len = sizeof(payload), |
| 2142 |
.addr = (__u64)(uintptr_t)(payload), |
| 2143 |
.timeout_ms = args->timeout, |
| 2144 |
}; |
| 2145 |
|
| 2146 |
if (args->args_size < sizeof(*args)) { |
| 2147 |
errno = EINVAL; |
| 2148 |
return -1; |
| 2149 |
} |
| 2150 |
return nvme_submit_io_passthru(args->fd, &cmd, args->result); |
| 2151 |
} |
| 2152 |
|
| 2153 |
int nvme_resv_register(struct nvme_resv_register_args *args) |
| 2154 |
{ |
| 2155 |
__le64 payload[2] = { |
| 2156 |
cpu_to_le64(args->crkey), |
| 2157 |
cpu_to_le64(args->nrkey) |
| 2158 |
}; |
| 2159 |
__u32 cdw10 = (args->rrega & 0x7) | |
| 2160 |
(args->iekey ? 1 << 3 : 0) | |
| 2161 |
(args->cptpl << 30); |
| 2162 |
|
| 2163 |
struct nvme_passthru_cmd cmd = { |
| 2164 |
.opcode = nvme_cmd_resv_register, |
| 2165 |
.nsid = args->nsid, |
| 2166 |
.cdw10 = cdw10, |
| 2167 |
.data_len = sizeof(payload), |
| 2168 |
.addr = (__u64)(uintptr_t)(payload), |
| 2169 |
.timeout_ms = args->timeout, |
| 2170 |
}; |
| 2171 |
|
| 2172 |
if (args->args_size < sizeof(*args)) { |
| 2173 |
errno = EINVAL; |
| 2174 |
return -1; |
| 2175 |
} |
| 2176 |
return nvme_submit_io_passthru(args->fd, &cmd, args->result); |
| 2177 |
} |
| 2178 |
|
| 2179 |
int nvme_resv_release(struct nvme_resv_release_args *args) |
| 2180 |
{ |
| 2181 |
__le64 payload[1] = { cpu_to_le64(args->crkey) }; |
| 2182 |
__u32 cdw10 = (args->rrela & 0x7) | |
| 2183 |
(args->iekey ? 1 << 3 : 0) | |
| 2184 |
(args->rtype << 8); |
| 2185 |
|
| 2186 |
struct nvme_passthru_cmd cmd = { |
| 2187 |
.opcode = nvme_cmd_resv_release, |
| 2188 |
.nsid = args->nsid, |
| 2189 |
.cdw10 = cdw10, |
| 2190 |
.addr = (__u64)(uintptr_t)(payload), |
| 2191 |
.data_len = sizeof(payload), |
| 2192 |
.timeout_ms = args->timeout, |
| 2193 |
}; |
| 2194 |
|
| 2195 |
if (args->args_size < sizeof(*args)) { |
| 2196 |
errno = EINVAL; |
| 2197 |
return -1; |
| 2198 |
} |
| 2199 |
return nvme_submit_io_passthru(args->fd, &cmd, args->result); |
| 2200 |
} |
| 2201 |
|
| 2202 |
int nvme_resv_report(struct nvme_resv_report_args *args) |
| 2203 |
{ |
| 2204 |
struct nvme_passthru_cmd cmd = { |
| 2205 |
.opcode = nvme_cmd_resv_report, |
| 2206 |
.nsid = args->nsid, |
| 2207 |
.cdw10 = (args->len >> 2) - 1, |
| 2208 |
.cdw11 = args->eds ? 1 : 0, |
| 2209 |
.addr = (__u64)(uintptr_t)args->report, |
| 2210 |
.data_len = args->len, |
| 2211 |
.timeout_ms = args->timeout, |
| 2212 |
}; |
| 2213 |
|
| 2214 |
if (args->args_size < sizeof(*args)) { |
| 2215 |
errno = EINVAL; |
| 2216 |
return -1; |
| 2217 |
} |
| 2218 |
return nvme_submit_io_passthru(args->fd, &cmd, args->result); |
| 2219 |
} |
| 2220 |
|
| 2221 |
int nvme_io_mgmt_recv(struct nvme_io_mgmt_recv_args *args) |
| 2222 |
{ |
| 2223 |
__u32 cdw10 = args->mo | (args->mos << 16); |
| 2224 |
__u32 cdw11 = (args->data_len >> 2) - 1; |
| 2225 |
|
| 2226 |
struct nvme_passthru_cmd cmd = { |
| 2227 |
.opcode = nvme_cmd_io_mgmt_recv, |
| 2228 |
.nsid = args->nsid, |
| 2229 |
.cdw10 = cdw10, |
| 2230 |
.cdw11 = cdw11, |
| 2231 |
.addr = (__u64)(uintptr_t)args->data, |
| 2232 |
.data_len = args->data_len, |
| 2233 |
.timeout_ms = args->timeout, |
| 2234 |
}; |
| 2235 |
|
| 2236 |
if (args->args_size < sizeof(*args)) { |
| 2237 |
errno = EINVAL; |
| 2238 |
return -1; |
| 2239 |
} |
| 2240 |
|
| 2241 |
return nvme_submit_io_passthru(args->fd, &cmd, NULL); |
| 2242 |
} |
| 2243 |
|
| 2244 |
int nvme_io_mgmt_send(struct nvme_io_mgmt_send_args *args) |
| 2245 |
{ |
| 2246 |
__u32 cdw10 = args->mo | (args->mos << 16); |
| 2247 |
|
| 2248 |
struct nvme_passthru_cmd cmd = { |
| 2249 |
.opcode = nvme_cmd_io_mgmt_send, |
| 2250 |
.nsid = args->nsid, |
| 2251 |
.cdw10 = cdw10, |
| 2252 |
.addr = (__u64)(uintptr_t)args->data, |
| 2253 |
.data_len = args->data_len, |
| 2254 |
.timeout_ms = args->timeout, |
| 2255 |
}; |
| 2256 |
|
| 2257 |
if (args->args_size < sizeof(*args)) { |
| 2258 |
errno = EINVAL; |
| 2259 |
return -1; |
| 2260 |
} |
| 2261 |
|
| 2262 |
return nvme_submit_io_passthru(args->fd, &cmd, NULL); |
| 2263 |
} |
| 2264 |
|
| 2265 |
int nvme_zns_mgmt_send(struct nvme_zns_mgmt_send_args *args) |
| 2266 |
{ |
| 2267 |
__u32 cdw10 = args->slba & 0xffffffff; |
| 2268 |
__u32 cdw11 = args->slba >> 32; |
| 2269 |
__u32 cdw13 = NVME_SET(args->zsaso, ZNS_MGMT_SEND_ZSASO) | |
| 2270 |
NVME_SET(!!args->select_all, ZNS_MGMT_SEND_SEL) | |
| 2271 |
NVME_SET(args->zsa, ZNS_MGMT_SEND_ZSA); |
| 2272 |
|
| 2273 |
struct nvme_passthru_cmd cmd = { |
| 2274 |
.opcode = nvme_zns_cmd_mgmt_send, |
| 2275 |
.nsid = args->nsid, |
| 2276 |
.cdw10 = cdw10, |
| 2277 |
.cdw11 = cdw11, |
| 2278 |
.cdw13 = cdw13, |
| 2279 |
.addr = (__u64)(uintptr_t)args->data, |
| 2280 |
.data_len = args->data_len, |
| 2281 |
.timeout_ms = args->timeout, |
| 2282 |
}; |
| 2283 |
|
| 2284 |
if (args->args_size < sizeof(*args)) { |
| 2285 |
errno = EINVAL; |
| 2286 |
return -1; |
| 2287 |
} |
| 2288 |
return nvme_submit_io_passthru(args->fd, &cmd, args->result); |
| 2289 |
} |
| 2290 |
|
| 2291 |
int nvme_zns_mgmt_recv(struct nvme_zns_mgmt_recv_args *args) |
| 2292 |
{ |
| 2293 |
__u32 cdw10 = args->slba & 0xffffffff; |
| 2294 |
__u32 cdw11 = args->slba >> 32; |
| 2295 |
__u32 cdw12 = (args->data_len >> 2) - 1; |
| 2296 |
__u32 cdw13 = NVME_SET(args->zra, ZNS_MGMT_RECV_ZRA) | |
| 2297 |
NVME_SET(args->zrasf, ZNS_MGMT_RECV_ZRASF) | |
| 2298 |
NVME_SET(args->zras_feat, ZNS_MGMT_RECV_ZRAS_FEAT); |
| 2299 |
|
| 2300 |
struct nvme_passthru_cmd cmd = { |
| 2301 |
.opcode = nvme_zns_cmd_mgmt_recv, |
| 2302 |
.nsid = args->nsid, |
| 2303 |
.cdw10 = cdw10, |
| 2304 |
.cdw11 = cdw11, |
| 2305 |
.cdw12 = cdw12, |
| 2306 |
.cdw13 = cdw13, |
| 2307 |
.addr = (__u64)(uintptr_t)args->data, |
| 2308 |
.data_len = args->data_len, |
| 2309 |
.timeout_ms = args->timeout, |
| 2310 |
}; |
| 2311 |
|
| 2312 |
if (args->args_size < sizeof(*args)) { |
| 2313 |
errno = EINVAL; |
| 2314 |
return -1; |
| 2315 |
} |
| 2316 |
return nvme_submit_io_passthru(args->fd, &cmd, args->result); |
| 2317 |
} |
| 2318 |
|
| 2319 |
int nvme_zns_append(struct nvme_zns_append_args *args) |
| 2320 |
{ |
| 2321 |
const size_t size_v1 = sizeof_args(struct nvme_zns_append_args, lbatm, __u64); |
| 2322 |
const size_t size_v2 = sizeof_args(struct nvme_zns_append_args, ilbrt_u64, __u64); |
| 2323 |
__u32 cdw3, cdw10, cdw11, cdw12, cdw14, cdw15; |
| 2324 |
|
| 2325 |
if (args->args_size < size_v1 || args->args_size > size_v2) { |
| 2326 |
errno = EINVAL; |
| 2327 |
return -1; |
| 2328 |
} |
| 2329 |
|
| 2330 |
cdw10 = args->zslba & 0xffffffff; |
| 2331 |
cdw11 = args->zslba >> 32; |
| 2332 |
cdw12 = args->nlb | (args->control << 16); |
| 2333 |
cdw15 = args->lbat | (args->lbatm << 16); |
| 2334 |
|
| 2335 |
if (args->args_size == size_v1) { |
| 2336 |
cdw3 = 0; |
| 2337 |
cdw14 = args->ilbrt; |
| 2338 |
} else { |
| 2339 |
cdw3 = (args->ilbrt_u64 >> 32) & 0xffffffff; |
| 2340 |
cdw14 = args->ilbrt_u64 & 0xffffffff; |
| 2341 |
} |
| 2342 |
|
| 2343 |
struct nvme_passthru_cmd64 cmd = { |
| 2344 |
.opcode = nvme_zns_cmd_append, |
| 2345 |
.nsid = args->nsid, |
| 2346 |
.cdw3 = cdw3, |
| 2347 |
.cdw10 = cdw10, |
| 2348 |
.cdw11 = cdw11, |
| 2349 |
.cdw12 = cdw12, |
| 2350 |
.cdw14 = cdw14, |
| 2351 |
.cdw15 = cdw15, |
| 2352 |
.data_len = args->data_len, |
| 2353 |
.addr = (__u64)(uintptr_t)args->data, |
| 2354 |
.metadata_len = args->metadata_len, |
| 2355 |
.metadata = (__u64)(uintptr_t)args->metadata, |
| 2356 |
.timeout_ms = args->timeout, |
| 2357 |
}; |
| 2358 |
|
| 2359 |
return nvme_submit_io_passthru64(args->fd, &cmd, args->result); |
| 2360 |
} |
| 2361 |
|
| 2362 |
int nvme_dim_send(struct nvme_dim_args *args) |
| 2363 |
{ |
| 2364 |
__u32 cdw10 = NVME_SET(args->tas, DIM_TAS); |
| 2365 |
|
| 2366 |
struct nvme_passthru_cmd cmd = { |
| 2367 |
.opcode = nvme_admin_discovery_info_mgmt, |
| 2368 |
.cdw10 = cdw10, |
| 2369 |
.addr = (__u64)(uintptr_t)args->data, |
| 2370 |
.data_len = args->data_len, |
| 2371 |
.timeout_ms = args->timeout, |
| 2372 |
}; |
| 2373 |
|
| 2374 |
if (args->args_size < sizeof(*args)) { |
| 2375 |
errno = EINVAL; |
| 2376 |
return -1; |
| 2377 |
} |
| 2378 |
|
| 2379 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 2380 |
} |
| 2381 |
|
| 2382 |
|
| 2383 |
int nvme_lm_cdq(struct nvme_lm_cdq_args *args) |
| 2384 |
{ |
| 2385 |
const size_t size_v1 = sizeof_args(struct nvme_lm_cdq_args, sz_u8, __u64); |
| 2386 |
const size_t size_v2 = sizeof_args(struct nvme_lm_cdq_args, sz, __u64); |
| 2387 |
__u32 cdw10 = NVME_SET(args->sel, LM_CDQ_SEL) | |
| 2388 |
NVME_SET(args->mos, LM_CDQ_MOS); |
| 2389 |
__u32 cdw11 = 0, data_len = 0, sz = 0; |
| 2390 |
int err; |
| 2391 |
|
| 2392 |
if (args->args_size < size_v1 || args->args_size > size_v2) { |
| 2393 |
errno = EINVAL; |
| 2394 |
return -1; |
| 2395 |
} |
| 2396 |
|
| 2397 |
if (args->args_size == size_v1) |
| 2398 |
sz = args->sz_u8; |
| 2399 |
else |
| 2400 |
sz = args->sz; |
| 2401 |
|
| 2402 |
if (args->sel == NVME_LM_SEL_CREATE_CDQ) { |
| 2403 |
cdw11 = NVME_SET(NVME_SET(args->cntlid, LM_CREATE_CDQ_CNTLID), LM_CQS) | |
| 2404 |
NVME_LM_CREATE_CDQ_PC; |
| 2405 |
data_len = sz << 2; |
| 2406 |
} else if (args->sel == NVME_LM_SEL_DELETE_CDQ) { |
| 2407 |
cdw11 = NVME_SET(args->cdqid, LM_DELETE_CDQ_CDQID); |
| 2408 |
} |
| 2409 |
|
| 2410 |
struct nvme_passthru_cmd cmd = { |
| 2411 |
.opcode = nvme_admin_ctrl_data_queue, |
| 2412 |
.cdw10 = cdw10, |
| 2413 |
.cdw11 = cdw11, |
| 2414 |
.cdw12 = sz, |
| 2415 |
.addr = (__u64)(uintptr_t)args->data, |
| 2416 |
.data_len = data_len, |
| 2417 |
.timeout_ms = args->timeout, |
| 2418 |
}; |
| 2419 |
|
| 2420 |
if (args->args_size < sizeof(*args)) { |
| 2421 |
errno = EINVAL; |
| 2422 |
return -1; |
| 2423 |
} |
| 2424 |
|
| 2425 |
err = nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 2426 |
|
| 2427 |
if (!err) |
| 2428 |
args->cdqid = NVME_GET(cmd.result, LM_CREATE_CDQ_CDQID); |
| 2429 |
|
| 2430 |
return err; |
| 2431 |
} |
| 2432 |
|
| 2433 |
int nvme_lm_track_send(struct nvme_lm_track_send_args *args) |
| 2434 |
{ |
| 2435 |
__u32 cdw10 = NVME_SET(args->sel, LM_TRACK_SEND_SEL) | |
| 2436 |
NVME_SET(args->mos, LM_TRACK_SEND_MOS); |
| 2437 |
|
| 2438 |
struct nvme_passthru_cmd cmd = { |
| 2439 |
.opcode = nvme_admin_track_send, |
| 2440 |
.cdw10 = cdw10, |
| 2441 |
.cdw11 = args->cdqid, |
| 2442 |
.timeout_ms = args->timeout, |
| 2443 |
}; |
| 2444 |
|
| 2445 |
if (args->args_size < sizeof(*args)) { |
| 2446 |
errno = EINVAL; |
| 2447 |
return -1; |
| 2448 |
} |
| 2449 |
|
| 2450 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 2451 |
} |
| 2452 |
|
| 2453 |
int nvme_lm_migration_send(struct nvme_lm_migration_send_args *args) |
| 2454 |
{ |
| 2455 |
__u32 cdw10 = NVME_SET(args->sel, LM_MIGRATION_SEND_SEL) | |
| 2456 |
NVME_SET(args->mos, LM_MIGRATION_SEND_MOS); |
| 2457 |
__u32 cdw11 = 0; |
| 2458 |
|
| 2459 |
if (args->sel == NVME_LM_SEL_SUSPEND) { |
| 2460 |
cdw11 = NVME_SET(args->stype, LM_STYPE) | |
| 2461 |
NVME_SET(args->cntlid, LM_SUSPEND_CNTLID); |
| 2462 |
if (args->dudmq) |
| 2463 |
cdw11 |= NVME_LM_DUDMQ; |
| 2464 |
} else if (args->sel == NVME_LM_SEL_RESUME) { |
| 2465 |
cdw11 = NVME_SET(args->cntlid, LM_RESUME_CNTLID); |
| 2466 |
} else if (args->sel == NVME_LM_SEL_SET_CONTROLLER_STATE) { |
| 2467 |
cdw11 = NVME_SET(args->csuuidi, LM_SET_CONTROLLER_STATE_CSUUIDI) | |
| 2468 |
NVME_SET(args->csvi, LM_SET_CONTROLLER_STATE_CSVI) | |
| 2469 |
NVME_SET(args->cntlid, LM_SET_CONTROLLER_STATE_CNTLID); |
| 2470 |
} |
| 2471 |
|
| 2472 |
struct nvme_passthru_cmd cmd = { |
| 2473 |
.opcode = nvme_admin_migration_send, |
| 2474 |
.cdw10 = cdw10, |
| 2475 |
.cdw11 = cdw11, |
| 2476 |
.cdw12 = (__u32)args->offset, |
| 2477 |
.cdw13 = (__u32)(args->offset >> 32), |
| 2478 |
.cdw14 = NVME_SET(args->uidx, LM_MIGRATION_SEND_UIDX), |
| 2479 |
.cdw15 = args->numd, |
| 2480 |
.addr = (__u64)(uintptr_t)args->data, |
| 2481 |
.data_len = args->numd << 2, |
| 2482 |
.timeout_ms = args->timeout, |
| 2483 |
}; |
| 2484 |
|
| 2485 |
if (args->args_size < sizeof(*args)) { |
| 2486 |
errno = EINVAL; |
| 2487 |
return -1; |
| 2488 |
} |
| 2489 |
|
| 2490 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 2491 |
} |
| 2492 |
|
| 2493 |
int nvme_lm_migration_recv(struct nvme_lm_migration_recv_args *args) |
| 2494 |
{ |
| 2495 |
__u32 cdw10 = NVME_SET(args->sel, LM_MIGRATION_RECV_SEL) | |
| 2496 |
NVME_SET(args->mos, LM_MIGRATION_RECV_MOS); |
| 2497 |
__u32 cdw11 = 0, data_len = 0; |
| 2498 |
|
| 2499 |
if (args->sel == NVME_LM_SEL_GET_CONTROLLER_STATE) { |
| 2500 |
cdw11 = NVME_SET(args->csuidxp, LM_GET_CONTROLLER_STATE_CSUIDXP) | |
| 2501 |
NVME_SET(args->csuuidi, LM_GET_CONTROLLER_STATE_CSUUIDI) | |
| 2502 |
NVME_SET(args->cntlid, LM_GET_CONTROLLER_STATE_CNTLID); |
| 2503 |
data_len = (args->numd + 1 /*0's based*/) << 2; |
| 2504 |
} |
| 2505 |
|
| 2506 |
struct nvme_passthru_cmd cmd = { |
| 2507 |
.opcode = nvme_admin_migration_receive, |
| 2508 |
.cdw10 = cdw10, |
| 2509 |
.cdw11 = cdw11, |
| 2510 |
.cdw12 = (__u32)args->offset, |
| 2511 |
.cdw13 = (__u32)(args->offset >> 32), |
| 2512 |
.cdw14 = NVME_SET(args->uidx, LM_MIGRATION_RECV_UIDX), |
| 2513 |
.cdw15 = args->numd, |
| 2514 |
.addr = (__u64)(uintptr_t)args->data, |
| 2515 |
.data_len = data_len, |
| 2516 |
.timeout_ms = args->timeout, |
| 2517 |
}; |
| 2518 |
|
| 2519 |
if (args->args_size < sizeof(*args)) { |
| 2520 |
errno = EINVAL; |
| 2521 |
return -1; |
| 2522 |
} |
| 2523 |
|
| 2524 |
return nvme_submit_admin_passthru(args->fd, &cmd, args->result); |
| 2525 |
} |
| 2526 |
|
| 2527 |
int nvme_lm_set_features_ctrl_data_queue(int fd, __u16 cdqid, __u32 hp, __u32 tpt, bool etpt, |
| 2528 |
__u32 *result) |
| 2529 |
{ |
| 2530 |
struct nvme_set_features_args args = { |
| 2531 |
.args_size = sizeof(args), |
| 2532 |
.fd = fd, |
| 2533 |
.fid = NVME_FEAT_FID_CTRL_DATA_QUEUE, |
| 2534 |
.nsid = NVME_NSID_NONE, |
| 2535 |
.cdw11 = cdqid | NVME_SET(etpt, LM_CTRL_DATA_QUEUE_ETPT), |
| 2536 |
.cdw12 = hp, |
| 2537 |
.cdw13 = tpt, |
| 2538 |
.save = false, |
| 2539 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 2540 |
.result = result, |
| 2541 |
}; |
| 2542 |
|
| 2543 |
return nvme_set_features(&args); |
| 2544 |
} |
| 2545 |
|
| 2546 |
int nvme_lm_get_features_ctrl_data_queue(int fd, __u16 cdqid, |
| 2547 |
struct nvme_lm_ctrl_data_queue_fid_data *data, |
| 2548 |
__u32 *result) |
| 2549 |
{ |
| 2550 |
struct nvme_get_features_args args = { |
| 2551 |
.args_size = sizeof(args), |
| 2552 |
.fd = fd, |
| 2553 |
.fid = NVME_FEAT_FID_CTRL_DATA_QUEUE, |
| 2554 |
.nsid = NVME_NSID_NONE, |
| 2555 |
.cdw11 = cdqid, |
| 2556 |
.data = data, |
| 2557 |
.data_len = sizeof(*data), |
| 2558 |
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT, |
| 2559 |
.result = result, |
| 2560 |
}; |
| 2561 |
|
| 2562 |
return nvme_get_features(&args); |
| 2563 |
} |