xref: /freebsd/sbin/nvmecontrol/nvmecontrol.h (revision f39bffc62c1395bde25d152c7f68fdf7cbaab414)
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  * $FreeBSD$
29  */
30 
31 #ifndef __NVMECONTROL_H__
32 #define __NVMECONTROL_H__
33 
34 #include <dev/nvme/nvme.h>
35 
36 typedef void (*nvme_fn_t)(int argc, char *argv[]);
37 
38 struct nvme_function {
39 	const char	*name;
40 	nvme_fn_t	fn;
41 	const char	*usage;
42 };
43 
44 #define NVME_CTRLR_PREFIX	"nvme"
45 #define NVME_NS_PREFIX		"ns"
46 
47 #define DEVLIST_USAGE							       \
48 "       nvmecontrol devlist\n"
49 
50 #define IDENTIFY_USAGE							       \
51 "       nvmecontrol identify [-x [-v]] <controller id|namespace id>\n"
52 
53 #define PERFTEST_USAGE							       \
54 "       nvmecontrol perftest <-n num_threads> <-o read|write>\n"	       \
55 "                            <-s size_in_bytes> <-t time_in_seconds>\n"	       \
56 "                            <-i intr|wait> [-f refthread] [-p]\n"	       \
57 "                            <namespace id>\n"
58 
59 #define RESET_USAGE							       \
60 "       nvmecontrol reset <controller id>\n"
61 
62 #define LOGPAGE_USAGE							       \
63 "       nvmecontrol logpage <-p page_id> [-b] [-v vendor] [-x] <controller id|namespace id>\n"  \
64 
65 #define FIRMWARE_USAGE							       \
66 "       nvmecontrol firmware [-s slot] [-f path_to_firmware] [-a] <controller id>\n"
67 
68 #define FORMAT_USAGE							       \
69 "       nvmecontrol format [-f fmt] [-m mset] [-p pi] [-l pil] [-E] [-C] <controller id|namespace id>\n"
70 
71 #define POWER_USAGE							       \
72 "       nvmecontrol power [-l] [-p new-state [-w workload-hint]] <controller id>\n"
73 
74 #define WDC_USAGE							       \
75 "       nvmecontrol wdc (cap-diag|drive-log|get-crash-dump|purge|purge-montior)\n"
76 
77 #define NS_USAGE								\
78 "       nvmecontrol ns (create|delete|attach|detach)\n"
79 
80 void devlist(int argc, char *argv[]);
81 void identify(int argc, char *argv[]);
82 void perftest(int argc, char *argv[]);
83 void reset(int argc, char *argv[]);
84 void logpage(int argc, char *argv[]);
85 void firmware(int argc, char *argv[]);
86 void format(int argc, char *argv[]);
87 void power(int argc, char *argv[]);
88 void wdc(int argc, char *argv[]);
89 void ns(int argc, char *argv[]);
90 
91 int open_dev(const char *str, int *fd, int show_error, int exit_on_error);
92 void parse_ns_str(const char *ns_str, char *ctrlr_str, uint32_t *nsid);
93 void read_controller_data(int fd, struct nvme_controller_data *cdata);
94 void read_namespace_data(int fd, uint32_t nsid, struct nvme_namespace_data *nsdata);
95 void print_hex(void *data, uint32_t length);
96 void read_logpage(int fd, uint8_t log_page, uint32_t nsid, void *payload,
97     uint32_t payload_size);
98 void gen_usage(struct nvme_function *);
99 void dispatch(int argc, char *argv[], struct nvme_function *f);
100 
101 /* Utility Routines */
102 /*
103  * 128-bit integer augments to standard values. On i386 this
104  * doesn't exist, so we use 64-bit values. So, on 32-bit i386,
105  * you'll get truncated values until someone implement 128bit
106  * ints in sofware.
107  */
108 #define UINT128_DIG	39
109 #ifdef __i386__
110 typedef uint64_t uint128_t;
111 #else
112 typedef __uint128_t uint128_t;
113 #endif
114 
115 static __inline uint128_t
116 to128(void *p)
117 {
118 	return *(uint128_t *)p;
119 }
120 
121 uint64_t le48dec(const void *pp);
122 char * uint128_to_str(uint128_t u, char *buf, size_t buflen);
123 
124 #endif
125