1a7bf63beSAlexander Motin /*- 2a7bf63beSAlexander Motin * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3a7bf63beSAlexander Motin * 4a7bf63beSAlexander Motin * Copyright (C) 2019 Alexander Motin <mav@FreeBSD.org> 5a7bf63beSAlexander Motin * 6a7bf63beSAlexander Motin * Redistribution and use in source and binary forms, with or without 7a7bf63beSAlexander Motin * modification, are permitted provided that the following conditions 8a7bf63beSAlexander Motin * are met: 9a7bf63beSAlexander Motin * 1. Redistributions of source code must retain the above copyright 10a7bf63beSAlexander Motin * notice, this list of conditions and the following disclaimer. 11a7bf63beSAlexander Motin * 2. Redistributions in binary form must reproduce the above copyright 12a7bf63beSAlexander Motin * notice, this list of conditions and the following disclaimer in the 13a7bf63beSAlexander Motin * documentation and/or other materials provided with the distribution. 14a7bf63beSAlexander Motin * 15a7bf63beSAlexander Motin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16a7bf63beSAlexander Motin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17a7bf63beSAlexander Motin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18a7bf63beSAlexander Motin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19a7bf63beSAlexander Motin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20a7bf63beSAlexander Motin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21a7bf63beSAlexander Motin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22a7bf63beSAlexander Motin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23a7bf63beSAlexander Motin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24a7bf63beSAlexander Motin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25a7bf63beSAlexander Motin * SUCH DAMAGE. 26a7bf63beSAlexander Motin */ 27a7bf63beSAlexander Motin 28a7bf63beSAlexander Motin #include <sys/cdefs.h> 29a7bf63beSAlexander Motin __FBSDID("$FreeBSD$"); 30a7bf63beSAlexander Motin 31a7bf63beSAlexander Motin #include <sys/param.h> 32a7bf63beSAlexander Motin 33a7bf63beSAlexander Motin #include <stdio.h> 34a7bf63beSAlexander Motin #include <stdlib.h> 35a7bf63beSAlexander Motin #include <unistd.h> 36a7bf63beSAlexander Motin 37a7bf63beSAlexander Motin #include "nvmecontrol.h" 38a7bf63beSAlexander Motin #include "comnd.h" 39a7bf63beSAlexander Motin 40a7bf63beSAlexander Motin /* Tables for command line parsing */ 41a7bf63beSAlexander Motin 4248ec75f0SAlexander Motin static cmd_fn_t gnsid; 43a7bf63beSAlexander Motin 44a7bf63beSAlexander Motin static struct nsid_options { 45a7bf63beSAlexander Motin const char *dev; 46a7bf63beSAlexander Motin } nsid_opt = { 47a7bf63beSAlexander Motin .dev = NULL, 48a7bf63beSAlexander Motin }; 49a7bf63beSAlexander Motin 50a7bf63beSAlexander Motin static const struct args nsid_args[] = { 51a7bf63beSAlexander Motin { arg_string, &nsid_opt.dev, "namespace-id" }, 52a7bf63beSAlexander Motin { arg_none, NULL, NULL }, 53a7bf63beSAlexander Motin }; 54a7bf63beSAlexander Motin 55a7bf63beSAlexander Motin static struct cmd nsid_cmd = { 56a7bf63beSAlexander Motin .name = "nsid", 5748ec75f0SAlexander Motin .fn = gnsid, 58a7bf63beSAlexander Motin .descr = "Get controller and NSID for namespace", 59a7bf63beSAlexander Motin .ctx_size = sizeof(nsid_opt), 60a7bf63beSAlexander Motin .opts = NULL, 61a7bf63beSAlexander Motin .args = nsid_args, 62a7bf63beSAlexander Motin }; 63a7bf63beSAlexander Motin 64a7bf63beSAlexander Motin CMD_COMMAND(nsid_cmd); 65a7bf63beSAlexander Motin 66a7bf63beSAlexander Motin static void 6748ec75f0SAlexander Motin gnsid(const struct cmd *f, int argc, char *argv[]) 68a7bf63beSAlexander Motin { 69a7bf63beSAlexander Motin char *path; 70a7bf63beSAlexander Motin int fd; 71a7bf63beSAlexander Motin uint32_t nsid; 72a7bf63beSAlexander Motin 736995fb5eSDavid Bright if (arg_parse(argc, argv, f)) 746995fb5eSDavid Bright return; 75a7bf63beSAlexander Motin 76*1f15d49eSAlexander Motin open_dev(nsid_opt.dev, &fd, 0, 1); 77a7bf63beSAlexander Motin get_nsid(fd, &path, &nsid); 78a7bf63beSAlexander Motin close(fd); 79a7bf63beSAlexander Motin printf("%s\t%u\n", path, nsid); 80a7bf63beSAlexander Motin free(path); 81a7bf63beSAlexander Motin } 82