xref: /freebsd/sbin/nvmecontrol/nvmecontrol.c (revision 1e4896b176ff664dc9c2fce5426bf2fdf8017a7d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/ioccom.h>
34 #include <sys/stat.h>
35 
36 #include <ctype.h>
37 #include <dlfcn.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <paths.h>
42 #include <stdbool.h>
43 #include <stddef.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sysexits.h>
48 #include <unistd.h>
49 
50 #include "nvmecontrol.h"
51 
52 static void
53 print_bytes(void *data, uint32_t length)
54 {
55 	uint32_t	i, j;
56 	uint8_t		*p, *end;
57 
58 	end = (uint8_t *)data + length;
59 
60 	for (i = 0; i < length; i++) {
61 		p = (uint8_t *)data + (i*16);
62 		printf("%03x: ", i*16);
63 		for (j = 0; j < 16 && p < end; j++)
64 			printf("%02x ", *p++);
65 		if (p >= end)
66 			break;
67 		printf("\n");
68 	}
69 	printf("\n");
70 }
71 
72 static void
73 print_dwords(void *data, uint32_t length)
74 {
75 	uint32_t	*p;
76 	uint32_t	i, j;
77 
78 	p = (uint32_t *)data;
79 	length /= sizeof(uint32_t);
80 
81 	for (i = 0; i < length; i+=8) {
82 		printf("%03x: ", i*4);
83 		for (j = 0; j < 8; j++)
84 			printf("%08x ", p[i+j]);
85 		printf("\n");
86 	}
87 
88 	printf("\n");
89 }
90 
91 void
92 print_hex(void *data, uint32_t length)
93 {
94 	if (length >= sizeof(uint32_t) || length % sizeof(uint32_t) == 0)
95 		print_dwords(data, length);
96 	else
97 		print_bytes(data, length);
98 }
99 
100 int
101 read_controller_data(int fd, struct nvme_controller_data *cdata)
102 {
103 	struct nvme_pt_command	pt;
104 
105 	memset(&pt, 0, sizeof(pt));
106 	pt.cmd.opc = NVME_OPC_IDENTIFY;
107 	pt.cmd.cdw10 = htole32(1);
108 	pt.buf = cdata;
109 	pt.len = sizeof(*cdata);
110 	pt.is_read = 1;
111 
112 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
113 		return (errno);
114 
115 	/* Convert data to host endian */
116 	nvme_controller_data_swapbytes(cdata);
117 
118 	if (nvme_completion_is_error(&pt.cpl))
119 		return (EIO);
120 	return (0);
121 }
122 
123 int
124 read_namespace_data(int fd, uint32_t nsid, struct nvme_namespace_data *nsdata)
125 {
126 	struct nvme_pt_command	pt;
127 
128 	memset(&pt, 0, sizeof(pt));
129 	pt.cmd.opc = NVME_OPC_IDENTIFY;
130 	pt.cmd.nsid = htole32(nsid);
131 	pt.cmd.cdw10 = htole32(0);
132 	pt.buf = nsdata;
133 	pt.len = sizeof(*nsdata);
134 	pt.is_read = 1;
135 
136 	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
137 		return (errno);
138 
139 	/* Convert data to host endian */
140 	nvme_namespace_data_swapbytes(nsdata);
141 
142 	if (nvme_completion_is_error(&pt.cpl))
143 		return (EIO);
144 	return (0);
145 }
146 
147 int
148 open_dev(const char *str, int *fd, int write, int exit_on_error)
149 {
150 	char		full_path[64];
151 
152 	snprintf(full_path, sizeof(full_path), _PATH_DEV"%s", str);
153 	*fd = open(full_path, write ? O_RDWR : O_RDONLY);
154 	if (*fd < 0) {
155 		if (exit_on_error) {
156 			err(EX_OSFILE, "could not open %s%s", full_path,
157 			    write ? " for write" : "");
158 		} else
159 			return (errno);
160 	}
161 
162 	return (0);
163 }
164 
165 void
166 get_nsid(int fd, char **ctrlr_str, uint32_t *nsid)
167 {
168 	struct nvme_get_nsid gnsid;
169 
170 	if (ioctl(fd, NVME_GET_NSID, &gnsid) < 0)
171 		err(EX_OSERR, "NVME_GET_NSID ioctl failed");
172 	if (ctrlr_str != NULL)
173 		*ctrlr_str = strndup(gnsid.cdev, sizeof(gnsid.cdev));
174 	if (nsid != NULL)
175 		*nsid = gnsid.nsid;
176 }
177 
178 int
179 main(int argc, char *argv[])
180 {
181 
182 	cmd_init();
183 
184 	cmd_load_dir("/lib/nvmecontrol", NULL, NULL);
185 	cmd_load_dir(_PATH_LOCALBASE "/lib/nvmecontrol", NULL, NULL);
186 
187 	cmd_dispatch(argc, argv, NULL);
188 
189 	return (0);
190 }
191