Changeset View
Changeset View
Standalone View
Standalone View
head/sbin/nvmecontrol/passthru.c
Show First 20 Lines • Show All 152 Lines • ▼ Show 20 Lines | static const struct args args[] = { | ||||
{ arg_string, &opt.dev, "controller-id|namespace-id" }, | { arg_string, &opt.dev, "controller-id|namespace-id" }, | ||||
{ arg_none, NULL, NULL }, | { arg_none, NULL, NULL }, | ||||
}; | }; | ||||
static void | static void | ||||
passthru(const struct cmd *f, int argc, char *argv[]) | passthru(const struct cmd *f, int argc, char *argv[]) | ||||
{ | { | ||||
int fd = -1, ifd = -1; | int fd = -1, ifd = -1; | ||||
size_t bytes_read; | |||||
void *data = NULL, *metadata = NULL; | void *data = NULL, *metadata = NULL; | ||||
struct nvme_pt_command pt; | struct nvme_pt_command pt; | ||||
arg_parse(argc, argv, f); | if (arg_parse(argc, argv, f)) | ||||
return; | |||||
open_dev(argv[optind], &fd, 1, 1); | open_dev(argv[optind], &fd, 1, 1); | ||||
if (opt.read && opt.write) | if (opt.read && opt.write) | ||||
errx(1, "need exactly one of --read or --write"); | errx(1, "need exactly one of --read or --write"); | ||||
if (opt.data_len != 0 && !opt.read && !opt.write) | if (opt.data_len != 0 && !opt.read && !opt.write) | ||||
errx(1, "need exactly one of --read or --write"); | errx(1, "need exactly one of --read or --write"); | ||||
if (*opt.ifn && (ifd = open(opt.ifn, O_RDONLY)) == -1) { | if (*opt.ifn && (ifd = open(opt.ifn, O_RDONLY)) == -1) { | ||||
warn("open %s", opt.ifn); | warn("open %s", opt.ifn); | ||||
Show All 11 Lines | if (opt.metadata_len != 0) | ||||
errx(1, "metadata not supported on FreeBSD"); | errx(1, "metadata not supported on FreeBSD"); | ||||
#endif | #endif | ||||
if (opt.data_len) { | if (opt.data_len) { | ||||
if (posix_memalign(&data, getpagesize(), opt.data_len)) { | if (posix_memalign(&data, getpagesize(), opt.data_len)) { | ||||
warn("can't allocate %d bytes for data", opt.data_len); | warn("can't allocate %d bytes for data", opt.data_len); | ||||
goto cleanup; | goto cleanup; | ||||
} | } | ||||
memset(data, opt.prefill, opt.data_len); | memset(data, opt.prefill, opt.data_len); | ||||
if (opt.write && read(ifd, data, opt.data_len) < 0) { | if (opt.write && | ||||
warn("read %s", *opt.ifn ? opt.ifn : "stdin"); | (bytes_read = read(ifd, data, opt.data_len)) != | ||||
opt.data_len) { | |||||
warn("read %s; expected %u bytes; got %zd", | |||||
*opt.ifn ? opt.ifn : "stdin", | |||||
opt.data_len, bytes_read); | |||||
goto cleanup; | goto cleanup; | ||||
} | } | ||||
} | } | ||||
if (opt.show_command) { | if (opt.show_command) { | ||||
fprintf(stderr, "opcode : %#02x\n", opt.opcode); | fprintf(stderr, "opcode : %#02x\n", opt.opcode); | ||||
fprintf(stderr, "flags : %#02x\n", opt.flags); | fprintf(stderr, "flags : %#02x\n", opt.flags); | ||||
fprintf(stderr, "rsvd1 : %#04x\n", opt.rsvd); | fprintf(stderr, "rsvd1 : %#04x\n", opt.rsvd); | ||||
fprintf(stderr, "nsid : %#04x\n", opt.nsid); | fprintf(stderr, "nsid : %#04x\n", opt.nsid); | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | if (opt.read) { | ||||
if (opt.binary) | if (opt.binary) | ||||
write(STDOUT_FILENO, data, opt.data_len); | write(STDOUT_FILENO, data, opt.data_len); | ||||
else { | else { | ||||
/* print status here */ | /* print status here */ | ||||
print_hex(data, opt.data_len); | print_hex(data, opt.data_len); | ||||
} | } | ||||
} | } | ||||
cleanup: | cleanup: | ||||
free(data); | |||||
close(fd); | |||||
if (ifd > -1) | |||||
close(ifd); | |||||
if (errno) | if (errno) | ||||
exit(1); | exit(1); | ||||
} | } | ||||
static void | static void | ||||
admin_passthru(const struct cmd *nf, int argc, char *argv[]) | admin_passthru(const struct cmd *nf, int argc, char *argv[]) | ||||
{ | { | ||||
Show All 30 Lines |