Home | History | Annotate | Line # | Download | only in nvmectl
nvmectl.c revision 1.1
      1 /*	$NetBSD: nvmectl.c,v 1.1 2016/06/04 16:29:35 nonaka Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 2012-2013 Intel Corporation
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #ifndef lint
     31 __RCSID("$NetBSD: nvmectl.c,v 1.1 2016/06/04 16:29:35 nonaka Exp $");
     32 #if 0
     33 __FBSDID("$FreeBSD: head/sbin/nvmecontrol/nvmecontrol.c 295087 2016-01-30 22:48:06Z imp $");
     34 #endif
     35 #endif
     36 
     37 #include <sys/param.h>
     38 #include <sys/ioccom.h>
     39 #include <sys/stat.h>
     40 
     41 #include <ctype.h>
     42 #include <err.h>
     43 #include <errno.h>
     44 #include <fcntl.h>
     45 #include <paths.h>
     46 #include <stdbool.h>
     47 #include <stddef.h>
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <string.h>
     51 #include <unistd.h>
     52 
     53 #include "nvmectl.h"
     54 
     55 typedef void (*nvme_fn_t)(int argc, char *argv[]);
     56 
     57 static struct nvme_function {
     58 	const char	*name;
     59 	nvme_fn_t	fn;
     60 	const char	*usage;
     61 } funcs[] = {
     62 	{"devlist",	devlist,	DEVLIST_USAGE},
     63 	{"identify",	identify,	IDENTIFY_USAGE},
     64 #ifdef PERFTEST_USAGE
     65 	{"perftest",	perftest,	PERFTEST_USAGE},
     66 #endif
     67 #ifdef RESET_USAGE
     68 	{"reset",	reset,		RESET_USAGE},
     69 #endif
     70 	{"logpage",	logpage,	LOGPAGE_USAGE},
     71 #ifdef FIRMWARE_USAGE
     72 	{"firmware",	firmware,	FIRMWARE_USAGE},
     73 #endif
     74 	{"power",	power,		POWER_USAGE},
     75 	{NULL,		NULL,		NULL},
     76 };
     77 
     78 static void
     79 usage(void)
     80 {
     81 	struct nvme_function *f;
     82 
     83 	f = funcs;
     84 	fprintf(stderr, "usage:\n");
     85 	while (f->name != NULL) {
     86 		fprintf(stderr, "%s", f->usage);
     87 		f++;
     88 	}
     89 	exit(1);
     90 }
     91 
     92 static void
     93 print_bytes(void *data, uint32_t length)
     94 {
     95 	uint32_t	i, j;
     96 	uint8_t		*p, *end;
     97 
     98 	end = (uint8_t *)data + length;
     99 
    100 	for (i = 0; i < length; i++) {
    101 		p = (uint8_t *)data + (i*16);
    102 		printf("%03x: ", i*16);
    103 		for (j = 0; j < 16 && p < end; j++)
    104 			printf("%02x ", *p++);
    105 		if (p >= end)
    106 			break;
    107 		printf("\n");
    108 	}
    109 	printf("\n");
    110 }
    111 
    112 static void
    113 print_dwords(void *data, uint32_t length)
    114 {
    115 	uint32_t	*p;
    116 	uint32_t	i, j;
    117 
    118 	p = (uint32_t *)data;
    119 	length /= sizeof(uint32_t);
    120 
    121 	for (i = 0; i < length; i+=8) {
    122 		printf("%03x: ", i*4);
    123 		for (j = 0; j < 8; j++)
    124 			printf("%08x ", p[i+j]);
    125 		printf("\n");
    126 	}
    127 
    128 	printf("\n");
    129 }
    130 
    131 void
    132 print_hex(void *data, uint32_t length)
    133 {
    134 	if (length >= sizeof(uint32_t) || length % sizeof(uint32_t) == 0)
    135 		print_dwords(data, length);
    136 	else
    137 		print_bytes(data, length);
    138 }
    139 
    140 void
    141 read_controller_data(int fd, struct nvm_identify_controller *cdata)
    142 {
    143 	struct nvme_pt_command	pt;
    144 
    145 	memset(&pt, 0, sizeof(pt));
    146 	pt.cmd.opcode = NVM_ADMIN_IDENTIFY;
    147 	pt.cmd.cdw10 = 1;
    148 	pt.buf = cdata;
    149 	pt.len = sizeof(*cdata);
    150 	pt.is_read = 1;
    151 
    152 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
    153 		err(1, "identify request failed");
    154 
    155 	if (nvme_completion_is_error(&pt.cpl))
    156 		errx(1, "identify request returned error");
    157 }
    158 
    159 void
    160 read_namespace_data(int fd, int nsid, struct nvm_identify_namespace *nsdata)
    161 {
    162 	struct nvme_pt_command	pt;
    163 
    164 	memset(&pt, 0, sizeof(pt));
    165 	pt.cmd.opcode = NVM_ADMIN_IDENTIFY;
    166 	pt.cmd.nsid = nsid;
    167 	pt.buf = nsdata;
    168 	pt.len = sizeof(*nsdata);
    169 	pt.is_read = 1;
    170 
    171 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
    172 		err(1, "identify request failed");
    173 
    174 	if (nvme_completion_is_error(&pt.cpl))
    175 		errx(1, "identify request returned error");
    176 }
    177 
    178 int
    179 open_dev(const char *str, int *fd, int show_error, int exit_on_error)
    180 {
    181 	char		full_path[64];
    182 
    183 	if (!strnstr(str, NVME_CTRLR_PREFIX, strlen(NVME_CTRLR_PREFIX))) {
    184 		if (show_error)
    185 			warnx("controller/namespace ids must begin with '%s'",
    186 			    NVME_CTRLR_PREFIX);
    187 		if (exit_on_error)
    188 			exit(1);
    189 		else
    190 			return (EINVAL);
    191 	}
    192 
    193 	snprintf(full_path, sizeof(full_path), _PATH_DEV"%s", str);
    194 	*fd = open(full_path, O_RDWR);
    195 	if (*fd < 0) {
    196 		if (show_error)
    197 			warn("could not open %s", full_path);
    198 		if (exit_on_error)
    199 			exit(1);
    200 		else
    201 			return (errno);
    202 	}
    203 
    204 	return (0);
    205 }
    206 
    207 void
    208 parse_ns_str(const char *ns_str, char *ctrlr_str, int *nsid)
    209 {
    210 	char	*nsloc;
    211 
    212 	/*
    213 	 * Pull the namespace id from the string. +2 skips past the "ns" part
    214 	 *  of the string.  Don't search past 10 characters into the string,
    215 	 *  otherwise we know it is malformed.
    216 	 */
    217 	nsloc = strnstr(ns_str, NVME_NS_PREFIX, 10);
    218 	if (nsloc != NULL)
    219 		*nsid = strtol(nsloc + 2, NULL, 10);
    220 	if (nsloc == NULL || (*nsid == 0 && errno != 0))
    221 		errx(1, "invalid namespace ID '%s'", ns_str);
    222 
    223 	/*
    224 	 * The controller string will include only the nvmX part of the
    225 	 *  nvmeXnsY string.
    226 	 */
    227 	snprintf(ctrlr_str, nsloc - ns_str + 1, "%s", ns_str);
    228 }
    229 
    230 void
    231 nvme_strvis(u_char *dst, int dlen, const u_char *src, int slen)
    232 {
    233 #define STRVIS_ISWHITE(x) ((x) == ' ' || (x) == '\0' || (x) == (u_char)'\377')
    234 	/* Trim leading and trailing blanks and NULs. */
    235 	while (slen > 0 && STRVIS_ISWHITE(src[0]))
    236 		++src, --slen;
    237 	while (slen > 0 && STRVIS_ISWHITE(src[slen - 1]))
    238 		--slen;
    239 
    240 	while (slen > 0) {
    241 		if (*src < 0x20 || *src >= 0x80) {
    242 			/* non-printable characters */
    243 			dlen -= 4;
    244 			if (dlen < 1)
    245 				break;
    246 			*dst++ = '\\';
    247 			*dst++ = ((*src & 0300) >> 6) + '0';
    248 			*dst++ = ((*src & 0070) >> 3) + '0';
    249 			*dst++ = ((*src & 0007) >> 0) + '0';
    250 		} else if (*src == '\\') {
    251 			/* quote characters */
    252 			dlen -= 2;
    253 			if (dlen < 1)
    254 				break;
    255 			*dst++ = '\\';
    256 			*dst++ = '\\';
    257 		} else {
    258 			/* normal characters */
    259 			if (--dlen < 1)
    260 				break;
    261 			*dst++ = *src;
    262 		}
    263 		++src, --slen;
    264 	}
    265 
    266 	*dst++ = 0;
    267 }
    268 
    269 int
    270 main(int argc, char *argv[])
    271 {
    272 	struct nvme_function *f;
    273 
    274 	if (argc < 2)
    275 		usage();
    276 
    277 	f = funcs;
    278 	while (f->name != NULL) {
    279 		if (strcmp(argv[1], f->name) == 0)
    280 			f->fn(argc-1, &argv[1]);
    281 		f++;
    282 	}
    283 
    284 	usage();
    285 
    286 	return (0);
    287 }
    288