11556Srgrimes/*-
21556Srgrimes * SPDX-License-Identifier: BSD-2-Clause
31556Srgrimes *
41556Srgrimes * Copyright (C) 2012-2013 Intel Corporation
51556Srgrimes * All rights reserved.
61556Srgrimes *
71556Srgrimes * Redistribution and use in source and binary forms, with or without
81556Srgrimes * modification, are permitted provided that the following conditions
91556Srgrimes * are met:
101556Srgrimes * 1. Redistributions of source code must retain the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer.
121556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
131556Srgrimes *    notice, this list of conditions and the following disclaimer in the
141556Srgrimes *    documentation and/or other materials provided with the distribution.
151556Srgrimes *
161556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
171556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
201556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
231556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261556Srgrimes * SUCH DAMAGE.
271556Srgrimes */
281556Srgrimes
291556Srgrimes#include <sys/param.h>
301556Srgrimes#include <sys/ioccom.h>
311556Srgrimes#include <sys/stat.h>
321556Srgrimes
331556Srgrimes#include <ctype.h>
341556Srgrimes#include <dlfcn.h>
351556Srgrimes#include <err.h>
361556Srgrimes#include <errno.h>
371556Srgrimes#include <fcntl.h>
381556Srgrimes#include <libutil.h>
391556Srgrimes#include <paths.h>
401556Srgrimes#include <stdbool.h>
41#include <stddef.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <sysexits.h>
46#include <unistd.h>
47
48#include "nvmecontrol.h"
49
50static void
51print_bytes(void *data, uint32_t length)
52{
53	uint32_t	i, j;
54	uint8_t		*p, *end;
55
56	end = (uint8_t *)data + length;
57
58	for (i = 0; i < length; i++) {
59		p = (uint8_t *)data + (i*16);
60		printf("%03x: ", i*16);
61		for (j = 0; j < 16 && p < end; j++)
62			printf("%02x ", *p++);
63		if (p >= end)
64			break;
65		printf("\n");
66	}
67	printf("\n");
68}
69
70static void
71print_dwords(void *data, uint32_t length)
72{
73	uint32_t	*p;
74	uint32_t	i, j;
75
76	p = (uint32_t *)data;
77	length /= sizeof(uint32_t);
78
79	for (i = 0; i < length; i+=8) {
80		printf("%03x: ", i*4);
81		for (j = 0; j < 8; j++)
82			printf("%08x ", p[i+j]);
83		printf("\n");
84	}
85
86	printf("\n");
87}
88
89void
90print_hex(void *data, uint32_t length)
91{
92	if (length >= sizeof(uint32_t) || length % sizeof(uint32_t) == 0)
93		print_dwords(data, length);
94	else
95		print_bytes(data, length);
96}
97
98int
99read_controller_data(int fd, struct nvme_controller_data *cdata)
100{
101	struct nvme_pt_command	pt;
102
103	memset(&pt, 0, sizeof(pt));
104	pt.cmd.opc = NVME_OPC_IDENTIFY;
105	pt.cmd.cdw10 = htole32(1);
106	pt.buf = cdata;
107	pt.len = sizeof(*cdata);
108	pt.is_read = 1;
109
110	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
111		return (errno);
112
113	/* Convert data to host endian */
114	nvme_controller_data_swapbytes(cdata);
115
116	if (nvme_completion_is_error(&pt.cpl))
117		return (EIO);
118	return (0);
119}
120
121int
122read_namespace_data(int fd, uint32_t nsid, struct nvme_namespace_data *nsdata)
123{
124	struct nvme_pt_command	pt;
125
126	memset(&pt, 0, sizeof(pt));
127	pt.cmd.opc = NVME_OPC_IDENTIFY;
128	pt.cmd.nsid = htole32(nsid);
129	pt.cmd.cdw10 = htole32(0);
130	pt.buf = nsdata;
131	pt.len = sizeof(*nsdata);
132	pt.is_read = 1;
133
134	if (ioctl(fd, NVME_PASSTHROUGH_CMD, &pt) < 0)
135		return (errno);
136
137	/* Convert data to host endian */
138	nvme_namespace_data_swapbytes(nsdata);
139
140	if (nvme_completion_is_error(&pt.cpl))
141		return (EIO);
142	return (0);
143}
144
145int
146open_dev(const char *str, int *fd, int write, int exit_on_error)
147{
148	char		full_path[MAXPATHLEN];
149
150	if (str[0] == '/')	/* Full path */
151		strlcpy(full_path, str, sizeof(full_path));
152	else			/* Add /dev/ */
153		snprintf(full_path, sizeof(full_path), _PATH_DEV"%s", str);
154	*fd = open(full_path, write ? O_RDWR : O_RDONLY);
155	if (*fd < 0) {
156		if (exit_on_error) {
157			err(EX_OSFILE, "could not open %s%s", full_path,
158			    write ? " for write" : "");
159		} else
160			return (errno);
161	}
162
163	return (0);
164}
165
166void
167get_nsid(int fd, char **ctrlr_str, uint32_t *nsid)
168{
169	struct nvme_get_nsid gnsid;
170
171	if (ioctl(fd, NVME_GET_NSID, &gnsid) < 0)
172		err(EX_OSERR, "NVME_GET_NSID ioctl failed");
173	if (ctrlr_str != NULL)
174		*ctrlr_str = strndup(gnsid.cdev, sizeof(gnsid.cdev));
175	if (nsid != NULL)
176		*nsid = gnsid.nsid;
177}
178
179int
180main(int argc, char *argv[])
181{
182	static char dir[MAXPATHLEN];
183
184	cmd_init();
185
186	cmd_load_dir("/lib/nvmecontrol", NULL, NULL);
187	snprintf(dir, MAXPATHLEN, "%s/lib/nvmecontrol", getlocalbase());
188	cmd_load_dir(dir, NULL, NULL);
189
190	cmd_dispatch(argc, argv, NULL);
191
192	return (0);
193}
194