2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2017 Netflix, Inc.
5 * Copyright (C) 2018-2019 Alexander Motin <mav@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/ioccom.h>
44 #include "nvmecontrol.h"
46 /* Tables for command line parsing */
49 static cmd_fn_t nsactive;
50 static cmd_fn_t nsallocated;
51 static cmd_fn_t nscontrollers;
52 static cmd_fn_t nscreate;
53 static cmd_fn_t nsdelete;
54 static cmd_fn_t nsattach;
55 static cmd_fn_t nsdetach;
56 static cmd_fn_t nsattached;
57 static cmd_fn_t nsidentify;
59 #define NONE 0xffffffffu
60 #define NONE64 0xffffffffffffffffull
61 #define OPT(l, s, t, opt, addr, desc) { l, s, t, &opt.addr, desc }
62 #define OPT_END { NULL, 0, arg_none, NULL, NULL }
64 static struct cmd ns_cmd = {
67 .descr = "Namespace management commands",
75 static struct active_options {
81 static const struct args active_args[] = {
82 { arg_string, &active_opt.dev, "controller-id" },
83 { arg_none, NULL, NULL },
86 static struct cmd active_cmd = {
89 .descr = "List active (attached) namespaces",
90 .ctx_size = sizeof(active_opt),
95 CMD_SUBCOMMAND(ns_cmd, active_cmd);
97 static struct cmd allocated_cmd = {
100 .descr = "List allocated (created) namespaces",
101 .ctx_size = sizeof(active_opt),
106 CMD_SUBCOMMAND(ns_cmd, allocated_cmd);
108 static struct controllers_options {
110 } controllers_opt = {
114 static const struct args controllers_args[] = {
115 { arg_string, &controllers_opt.dev, "controller-id" },
116 { arg_none, NULL, NULL },
119 static struct cmd controllers_cmd = {
120 .name = "controllers",
122 .descr = "List all controllers in NVM subsystem",
123 .ctx_size = sizeof(controllers_opt),
125 .args = controllers_args,
128 CMD_SUBCOMMAND(ns_cmd, controllers_cmd);
130 static struct create_options {
140 // uint32_t block_size;
153 // .block_size = NONE,
156 static const struct opts create_opts[] = {
157 OPT("nsze", 's', arg_uint64, create_opt, nsze,
158 "The namespace size"),
159 OPT("ncap", 'c', arg_uint64, create_opt, cap,
160 "The capacity of the namespace (<= ns size)"),
161 OPT("lbaf", 'f', arg_uint32, create_opt, lbaf,
162 "The FMT field of the FLBAS"),
163 OPT("mset", 'm', arg_uint32, create_opt, mset,
164 "The MSET field of the FLBAS"),
165 OPT("nmic", 'n', arg_uint32, create_opt, nmic,
166 "Namespace multipath and sharing capabilities"),
167 OPT("pi", 'p', arg_uint32, create_opt, pi,
168 "PI field of FLBAS"),
169 OPT("pil", 'l', arg_uint32, create_opt, pil,
170 "PIL field of FLBAS"),
171 OPT("flbas", 'L', arg_uint32, create_opt, flbas,
172 "Namespace formatted logical block size setting"),
173 OPT("dps", 'd', arg_uint32, create_opt, dps,
174 "Data protection settings"),
175 // OPT("block-size", 'b', arg_uint32, create_opt, block_size,
176 // "Blocksize of the namespace"),
180 static const struct args create_args[] = {
181 { arg_string, &create_opt.dev, "controller-id" },
182 { arg_none, NULL, NULL },
185 static struct cmd create_cmd = {
188 .descr = "Create a namespace",
189 .ctx_size = sizeof(create_opt),
194 CMD_SUBCOMMAND(ns_cmd, create_cmd);
196 static struct delete_options {
204 static const struct opts delete_opts[] = {
205 OPT("namespace-id", 'n', arg_uint32, delete_opt, nsid,
206 "The namespace ID to delete"),
210 static const struct args delete_args[] = {
211 { arg_string, &delete_opt.dev, "controller-id" },
212 { arg_none, NULL, NULL },
215 static struct cmd delete_cmd = {
218 .descr = "Delete a namespace",
219 .ctx_size = sizeof(delete_opt),
224 CMD_SUBCOMMAND(ns_cmd, delete_cmd);
226 static struct attach_options {
236 static const struct opts attach_opts[] = {
237 OPT("namespace-id", 'n', arg_uint32, attach_opt, nsid,
238 "The namespace ID to attach"),
239 OPT("controller", 'c', arg_uint32, attach_opt, ctrlrid,
240 "The controller ID to attach"),
244 static const struct args attach_args[] = {
245 { arg_string, &attach_opt.dev, "controller-id" },
246 { arg_none, NULL, NULL },
249 static struct cmd attach_cmd = {
252 .descr = "Attach a controller to a namespace",
253 .ctx_size = sizeof(attach_opt),
258 CMD_SUBCOMMAND(ns_cmd, attach_cmd);
260 static struct attached_options {
268 static const struct opts attached_opts[] = {
269 OPT("namespace-id", 'n', arg_uint32, attached_opt, nsid,
270 "The namespace ID to request attached controllers"),
274 static const struct args attached_args[] = {
275 { arg_string, &attached_opt.dev, "controller-id" },
276 { arg_none, NULL, NULL },
279 static struct cmd attached_cmd = {
282 .descr = "List controllers attached to a namespace",
283 .ctx_size = sizeof(attached_opt),
284 .opts = attached_opts,
285 .args = attached_args,
288 CMD_SUBCOMMAND(ns_cmd, attached_cmd);
290 static struct detach_options {
300 static const struct opts detach_opts[] = {
301 OPT("namespace-id", 'n', arg_uint32, detach_opt, nsid,
302 "The namespace ID to detach"),
303 OPT("controller", 'c', arg_uint32, detach_opt, ctrlrid,
304 "The controller ID to detach"),
308 static const struct args detach_args[] = {
309 { arg_string, &detach_opt.dev, "controller-id" },
310 { arg_none, NULL, NULL },
313 static struct cmd detach_cmd = {
316 .descr = "Detach a controller from a namespace",
317 .ctx_size = sizeof(detach_opt),
322 CMD_SUBCOMMAND(ns_cmd, detach_cmd);
324 static struct identify_options {
336 static const struct opts identify_opts[] = {
337 OPT("hex", 'x', arg_none, identify_opt, hex,
338 "Print identiy information in hex"),
339 OPT("verbose", 'v', arg_none, identify_opt, verbose,
340 "More verbosity: print entire identify table"),
341 OPT("nsid", 'n', arg_uint32, identify_opt, nsid,
342 "The namespace ID to print IDENTIFY for"),
343 { NULL, 0, arg_none, NULL, NULL }
346 static const struct args identify_args[] = {
347 { arg_string, &identify_opt.dev, "controller-id" },
348 { arg_none, NULL, NULL },
351 static struct cmd identify_cmd = {
354 .descr = "Print IDENTIFY for allocated namespace",
355 .ctx_size = sizeof(identify_opt),
356 .opts = identify_opts,
357 .args = identify_args,
360 CMD_SUBCOMMAND(ns_cmd, identify_cmd);
362 /* handles NVME_OPC_NAMESPACE_MANAGEMENT and ATTACHMENT admin cmds */
364 struct ns_result_str {
369 static struct ns_result_str ns_result[] = {
370 { 0x2, "Invalid Field"},
371 { 0xa, "Invalid Format"},
372 { 0xb, "Invalid Namespace or format"},
373 { 0x15, "Namespace insufficent capacity"},
374 { 0x16, "Namespace ID unavaliable"},
375 { 0x18, "Namespace already attached"},
376 { 0x19, "Namespace is private"},
377 { 0x1a, "Namespace is not attached"},
378 { 0x1b, "Thin provisioning not supported"},
379 { 0x1c, "Controller list invalid"},
380 { 0x24, "ANA Group Identifier Invalid"},
381 { 0x25, "ANA Attach Failed"},
386 get_res_str(uint16_t res)
388 struct ns_result_str *t = ns_result;
390 while (t->res != 0xFFFF) {
399 nsactive(const struct cmd *f, int argc, char *argv[])
401 struct nvme_pt_command pt;
405 if (arg_parse(argc, argv, f))
407 open_dev(active_opt.dev, &fd, 1, 1);
409 memset(&pt, 0, sizeof(pt));
410 pt.cmd.opc = NVME_OPC_IDENTIFY;
411 pt.cmd.nsid = htole32(0);
412 pt.cmd.cdw10 = htole32(0x02);
414 pt.len = sizeof(list);
416 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
417 err(1, "identify request failed");
418 if (nvme_completion_is_error(&pt.cpl))
419 errx(1, "identify request returned error");
421 printf("Active namespaces:\n");
422 for (i = 0; list[i] != 0; i++)
423 printf("%10d\n", le32toh(list[i]));
429 nsallocated(const struct cmd *f, int argc, char *argv[])
431 struct nvme_pt_command pt;
432 struct nvme_controller_data cd;
436 if (arg_parse(argc, argv, f))
438 open_dev(active_opt.dev, &fd, 1, 1);
439 read_controller_data(fd, &cd);
441 /* Check that controller can execute this command. */
442 if (((cd.oacs >> NVME_CTRLR_DATA_OACS_NSMGMT_SHIFT) &
443 NVME_CTRLR_DATA_OACS_NSMGMT_MASK) == 0)
444 errx(1, "controller does not support namespace management");
446 memset(&pt, 0, sizeof(pt));
447 pt.cmd.opc = NVME_OPC_IDENTIFY;
448 pt.cmd.nsid = htole32(0);
449 pt.cmd.cdw10 = htole32(0x10);
451 pt.len = sizeof(list);
453 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
454 err(1, "identify request failed");
455 if (nvme_completion_is_error(&pt.cpl))
456 errx(1, "identify request returned error");
458 printf("Allocated namespaces:\n");
459 for (i = 0; list[i] != 0; i++)
460 printf("%10d\n", le32toh(list[i]));
466 nscontrollers(const struct cmd *f, int argc, char *argv[])
468 struct nvme_pt_command pt;
469 struct nvme_controller_data cd;
471 uint16_t clist[2048];
473 if (arg_parse(argc, argv, f))
475 open_dev(controllers_opt.dev, &fd, 1, 1);
476 read_controller_data(fd, &cd);
478 /* Check that controller can execute this command. */
479 if (((cd.oacs >> NVME_CTRLR_DATA_OACS_NSMGMT_SHIFT) &
480 NVME_CTRLR_DATA_OACS_NSMGMT_MASK) == 0)
481 errx(1, "controller does not support namespace management");
483 memset(&pt, 0, sizeof(pt));
484 pt.cmd.opc = NVME_OPC_IDENTIFY;
485 pt.cmd.cdw10 = htole32(0x13);
487 pt.len = sizeof(clist);
489 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
490 err(1, "identify request failed");
491 if (nvme_completion_is_error(&pt.cpl))
492 errx(1, "identify request returned error");
494 n = le16toh(clist[0]);
495 printf("NVM subsystem includes %d controller(s):\n", n);
496 for (i = 0; i < n; i++)
497 printf(" 0x%04x\n", le16toh(clist[i + 1]));
503 * NS MGMT Command specific status values:
504 * 0xa = Invalid Format
505 * 0x15 = Namespace Insuffience capacity
506 * 0x16 = Namespace ID unavailable (number namespaces exceeded)
507 * 0xb = Thin Provisioning Not supported
510 nscreate(const struct cmd *f, int argc, char *argv[])
512 struct nvme_pt_command pt;
513 struct nvme_controller_data cd;
514 struct nvme_namespace_data nsdata;
517 if (arg_parse(argc, argv, f))
520 if (create_opt.cap == NONE64)
521 create_opt.cap = create_opt.nsze;
522 if (create_opt.nsze == NONE64) {
524 "Size not specified\n");
525 arg_help(argc, argv, f);
528 open_dev(create_opt.dev, &fd, 1, 1);
529 read_controller_data(fd, &cd);
531 /* Check that controller can execute this command. */
532 if (((cd.oacs >> NVME_CTRLR_DATA_OACS_NSMGMT_SHIFT) &
533 NVME_CTRLR_DATA_OACS_NSMGMT_MASK) == 0)
534 errx(1, "controller does not support namespace management");
536 /* Allow namespaces sharing if Multi-Path I/O is supported. */
537 if (create_opt.nmic == NONE) {
538 create_opt.nmic = cd.mic ? (NVME_NS_DATA_NMIC_MAY_BE_SHARED_MASK <<
539 NVME_NS_DATA_NMIC_MAY_BE_SHARED_SHIFT) : 0;
542 memset(&nsdata, 0, sizeof(nsdata));
543 nsdata.nsze = create_opt.nsze;
544 nsdata.ncap = create_opt.cap;
545 if (create_opt.flbas == NONE)
546 nsdata.flbas = ((create_opt.lbaf & NVME_NS_DATA_FLBAS_FORMAT_MASK)
547 << NVME_NS_DATA_FLBAS_FORMAT_SHIFT) |
548 ((create_opt.mset & NVME_NS_DATA_FLBAS_EXTENDED_MASK)
549 << NVME_NS_DATA_FLBAS_EXTENDED_SHIFT);
551 nsdata.flbas = create_opt.flbas;
552 if (create_opt.dps == NONE)
553 nsdata.dps = ((create_opt.pi & NVME_NS_DATA_DPS_MD_START_MASK)
554 << NVME_NS_DATA_DPS_MD_START_SHIFT) |
555 ((create_opt.pil & NVME_NS_DATA_DPS_PIT_MASK)
556 << NVME_NS_DATA_DPS_PIT_SHIFT);
558 nsdata.dps = create_opt.dps;
559 nsdata.nmic = create_opt.nmic;
560 nvme_namespace_data_swapbytes(&nsdata);
562 memset(&pt, 0, sizeof(pt));
563 pt.cmd.opc = NVME_OPC_NAMESPACE_MANAGEMENT;
564 pt.cmd.cdw10 = htole32(0); /* create */
566 pt.len = sizeof(struct nvme_namespace_data);
567 pt.is_read = 0; /* passthrough writes data to ctrlr */
568 if ((result = ioctl(fd, NVME_PASSTHROUGH_CMD, &pt)) < 0)
569 errx(1, "ioctl request to %s failed: %d", argv[optind], result);
571 if (nvme_completion_is_error(&pt.cpl)) {
572 errx(1, "namespace creation failed: %s",
573 get_res_str((pt.cpl.status >> NVME_STATUS_SC_SHIFT) &
574 NVME_STATUS_SC_MASK));
576 printf("namespace %d created\n", pt.cpl.cdw0);
581 nsdelete(const struct cmd *f, int argc, char *argv[])
583 struct nvme_pt_command pt;
584 struct nvme_controller_data cd;
588 if (arg_parse(argc, argv, f))
590 if (delete_opt.nsid == NONE) {
592 "No NSID specified");
593 arg_help(argc, argv, f);
596 open_dev(delete_opt.dev, &fd, 1, 1);
597 read_controller_data(fd, &cd);
599 /* Check that controller can execute this command. */
600 if (((cd.oacs >> NVME_CTRLR_DATA_OACS_NSMGMT_SHIFT) &
601 NVME_CTRLR_DATA_OACS_NSMGMT_MASK) == 0)
602 errx(1, "controller does not support namespace management");
604 memset(&pt, 0, sizeof(pt));
605 pt.cmd.opc = NVME_OPC_NAMESPACE_MANAGEMENT;
606 pt.cmd.cdw10 = htole32(1); /* delete */
608 pt.len = sizeof(buf);
610 pt.cmd.nsid = delete_opt.nsid;
612 if ((result = ioctl(fd, NVME_PASSTHROUGH_CMD, &pt)) < 0)
613 errx(1, "ioctl request to %s failed: %d", delete_opt.dev, result);
615 if (nvme_completion_is_error(&pt.cpl)) {
616 errx(1, "namespace deletion failed: %s",
617 get_res_str((pt.cpl.status >> NVME_STATUS_SC_SHIFT) &
618 NVME_STATUS_SC_MASK));
620 printf("namespace %d deleted\n", delete_opt.nsid);
625 * Attach and Detach use Dword 10, and a controller list (section 4.9)
626 * This struct is 4096 bytes in size.
630 * Result values for both attach/detach:
632 * Completion 18h = Already attached
633 * 19h = NS is private and already attached to a controller
634 * 1Ah = Not attached, request could not be completed
635 * 1Ch = Controller list invalid.
637 * 0x2 Invalid Field can occur if ctrlrid d.n.e in system.
640 nsattach(const struct cmd *f, int argc, char *argv[])
642 struct nvme_pt_command pt;
643 struct nvme_controller_data cd;
645 uint16_t clist[2048];
647 if (arg_parse(argc, argv, f))
649 if (attach_opt.nsid == NONE) {
650 fprintf(stderr, "No valid NSID specified\n");
651 arg_help(argc, argv, f);
653 open_dev(attach_opt.dev, &fd, 1, 1);
654 read_controller_data(fd, &cd);
656 /* Check that controller can execute this command. */
657 if (((cd.oacs >> NVME_CTRLR_DATA_OACS_NSMGMT_SHIFT) &
658 NVME_CTRLR_DATA_OACS_NSMGMT_MASK) == 0)
659 errx(1, "controller does not support namespace management");
661 if (attach_opt.ctrlrid == NONE) {
662 /* Get full list of controllers to attach to. */
663 memset(&pt, 0, sizeof(pt));
664 pt.cmd.opc = NVME_OPC_IDENTIFY;
665 pt.cmd.cdw10 = htole32(0x13);
667 pt.len = sizeof(clist);
669 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
670 err(1, "identify request failed");
671 if (nvme_completion_is_error(&pt.cpl))
672 errx(1, "identify request returned error");
674 /* By default attach to this controller. */
675 if (attach_opt.ctrlrid == NONE - 1)
676 attach_opt.ctrlrid = cd.ctrlr_id;
677 memset(&clist, 0, sizeof(clist));
678 clist[0] = htole16(1);
679 clist[1] = htole16(attach_opt.ctrlrid);
682 memset(&pt, 0, sizeof(pt));
683 pt.cmd.opc = NVME_OPC_NAMESPACE_ATTACHMENT;
684 pt.cmd.cdw10 = htole32(0); /* attach */
685 pt.cmd.nsid = attach_opt.nsid;
687 pt.len = sizeof(clist);
689 if ((result = ioctl(fd, NVME_PASSTHROUGH_CMD, &pt)) < 0)
690 errx(1, "ioctl request to %s failed: %d", attach_opt.dev, result);
692 if (nvme_completion_is_error(&pt.cpl)) {
693 errx(1, "namespace attach failed: %s",
694 get_res_str((pt.cpl.status >> NVME_STATUS_SC_SHIFT) &
695 NVME_STATUS_SC_MASK));
697 printf("namespace %d attached\n", attach_opt.nsid);
702 nsdetach(const struct cmd *f, int argc, char *argv[])
704 struct nvme_pt_command pt;
705 struct nvme_controller_data cd;
707 uint16_t clist[2048];
709 if (arg_parse(argc, argv, f))
711 if (detach_opt.nsid == NONE) {
712 fprintf(stderr, "No valid NSID specified\n");
713 arg_help(argc, argv, f);
715 open_dev(detach_opt.dev, &fd, 1, 1);
716 read_controller_data(fd, &cd);
718 /* Check that controller can execute this command. */
719 if (((cd.oacs >> NVME_CTRLR_DATA_OACS_NSMGMT_SHIFT) &
720 NVME_CTRLR_DATA_OACS_NSMGMT_MASK) == 0)
721 errx(1, "controller does not support namespace management");
723 if (detach_opt.ctrlrid == NONE) {
724 /* Get list of controllers this namespace attached to. */
725 memset(&pt, 0, sizeof(pt));
726 pt.cmd.opc = NVME_OPC_IDENTIFY;
727 pt.cmd.nsid = htole32(detach_opt.nsid);
728 pt.cmd.cdw10 = htole32(0x12);
730 pt.len = sizeof(clist);
732 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
733 err(1, "identify request failed");
734 if (nvme_completion_is_error(&pt.cpl))
735 errx(1, "identify request returned error");
737 detach_opt.ctrlrid = cd.ctrlr_id;
738 memset(&clist, 0, sizeof(clist));
739 clist[0] = htole16(1);
740 clist[1] = htole16(detach_opt.ctrlrid);
743 /* By default detach from this controller. */
744 if (detach_opt.ctrlrid == NONE - 1)
745 detach_opt.ctrlrid = cd.ctrlr_id;
746 memset(&clist, 0, sizeof(clist));
747 clist[0] = htole16(1);
748 clist[1] = htole16(detach_opt.ctrlrid);
751 memset(&pt, 0, sizeof(pt));
752 pt.cmd.opc = NVME_OPC_NAMESPACE_ATTACHMENT;
753 pt.cmd.cdw10 = htole32(1); /* detach */
754 pt.cmd.nsid = detach_opt.nsid;
756 pt.len = sizeof(clist);
758 if ((result = ioctl(fd, NVME_PASSTHROUGH_CMD, &pt)) < 0)
759 errx(1, "ioctl request to %s failed: %d", argv[optind], result);
761 if (nvme_completion_is_error(&pt.cpl)) {
762 errx(1, "namespace detach failed: %s",
763 get_res_str((pt.cpl.status >> NVME_STATUS_SC_SHIFT) &
764 NVME_STATUS_SC_MASK));
766 printf("namespace %d detached\n", detach_opt.nsid);
771 nsattached(const struct cmd *f, int argc, char *argv[])
773 struct nvme_pt_command pt;
774 struct nvme_controller_data cd;
776 uint16_t clist[2048];
778 if (arg_parse(argc, argv, f))
780 if (attached_opt.nsid == NONE) {
781 fprintf(stderr, "No valid NSID specified\n");
782 arg_help(argc, argv, f);
784 open_dev(attached_opt.dev, &fd, 1, 1);
785 read_controller_data(fd, &cd);
787 /* Check that controller can execute this command. */
788 if (((cd.oacs >> NVME_CTRLR_DATA_OACS_NSMGMT_SHIFT) &
789 NVME_CTRLR_DATA_OACS_NSMGMT_MASK) == 0)
790 errx(1, "controller does not support namespace management");
792 memset(&pt, 0, sizeof(pt));
793 pt.cmd.opc = NVME_OPC_IDENTIFY;
794 pt.cmd.nsid = htole32(attached_opt.nsid);
795 pt.cmd.cdw10 = htole32(0x12);
797 pt.len = sizeof(clist);
799 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
800 err(1, "identify request failed");
801 if (nvme_completion_is_error(&pt.cpl))
802 errx(1, "identify request returned error");
804 n = le16toh(clist[0]);
805 printf("Attached %d controller(s):\n", n);
806 for (i = 0; i < n; i++)
807 printf(" 0x%04x\n", le16toh(clist[i + 1]));
813 nsidentify(const struct cmd *f, int argc, char *argv[])
815 struct nvme_pt_command pt;
816 struct nvme_controller_data cd;
817 struct nvme_namespace_data nsdata;
822 if (arg_parse(argc, argv, f))
824 if (identify_opt.nsid == NONE) {
825 fprintf(stderr, "No valid NSID specified\n");
826 arg_help(argc, argv, f);
828 open_dev(identify_opt.dev, &fd, 1, 1);
829 read_controller_data(fd, &cd);
831 /* Check that controller can execute this command. */
832 if (((cd.oacs >> NVME_CTRLR_DATA_OACS_NSMGMT_SHIFT) &
833 NVME_CTRLR_DATA_OACS_NSMGMT_MASK) == 0)
834 errx(1, "controller does not support namespace management");
836 memset(&pt, 0, sizeof(pt));
837 pt.cmd.opc = NVME_OPC_IDENTIFY;
838 pt.cmd.nsid = htole32(identify_opt.nsid);
839 pt.cmd.cdw10 = htole32(0x11);
841 pt.len = sizeof(nsdata);
844 if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
845 err(1, "identify request failed");
847 if (nvme_completion_is_error(&pt.cpl))
848 errx(1, "identify request returned error");
852 data = (uint8_t *)&nsdata;
853 for (i = 0; i < sizeof(nsdata); i++) {
857 if (i == sizeof(nsdata))
858 errx(1, "namespace %d is not allocated", identify_opt.nsid);
860 /* Convert data to host endian */
861 nvme_namespace_data_swapbytes(&nsdata);
863 if (identify_opt.hex) {
864 i = sizeof(struct nvme_namespace_data);
865 if (!identify_opt.verbose) {
866 for (; i > 384; i--) {
867 if (data[i - 1] != 0)
871 print_hex(&nsdata, i);
875 print_namespace(&nsdata);
880 ns(const struct cmd *nf __unused, int argc, char *argv[])
883 cmd_dispatch(argc, argv, &ns_cmd);