xref: /freebsd/sbin/nvmecontrol/nvmecontrol.h (revision 6580f5c38dd5b01aeeaed16b370f1a12423437f0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #ifndef __NVMECONTROL_H__
30 #define __NVMECONTROL_H__
31 
32 #include <dev/nvme/nvme.h>
33 #include "comnd.h"
34 
35 typedef void (*print_fn_t)(const struct nvme_controller_data *cdata, void *buf, uint32_t size);
36 
37 struct logpage_function {
38         SLIST_ENTRY(logpage_function)   link;
39 	uint8_t		log_page;
40 	const char     *vendor;
41 	const char     *name;
42 	print_fn_t	print_fn;
43 	size_t		size;
44 };
45 
46 #define NVME_LOGPAGE(unique, lp, vend, nam, fn, sz)			\
47 	static struct logpage_function unique ## _lpf = {		\
48 		.log_page = lp,						\
49 		.vendor = vend,						\
50 		.name = nam,						\
51 		.print_fn = fn, 					\
52 		.size = sz,						\
53 	} ;								\
54         static void logpage_reg_##unique(void) __attribute__((constructor)); \
55         static void logpage_reg_##unique(void) { logpage_register(&unique##_lpf); }
56 
57 #define DEFAULT_SIZE	(4096)
58 struct kv_name {
59 	uint32_t key;
60 	const char *name;
61 };
62 
63 /*
64  * Generically convert little endian to host endian, based on the type of the thing
65  * being converted. Use the proposed name for future changes to endian.h.
66  */
67 #define letoh(x)							\
68 	_Generic(x,							\
69 	uint8_t: (x),							\
70 	uint16_t: le16toh(x),						\
71 	uint32_t: le32toh(x),						\
72 	uint64_t: le64toh(x))
73 
74 const char *kv_lookup(const struct kv_name *kv, size_t kv_count, uint32_t key);
75 
76 void logpage_register(struct logpage_function *p);
77 #define NVME_CTRLR_PREFIX	"nvme"
78 #define NVME_NS_PREFIX		"ns"
79 
80 int open_dev(const char *str, int *fd, int write, int exit_on_error);
81 void get_nsid(int fd, char **ctrlr_str, uint32_t *nsid);
82 int read_controller_data(int fd, struct nvme_controller_data *cdata);
83 int read_namespace_data(int fd, uint32_t nsid, struct nvme_namespace_data *nsdata);
84 void print_hex(void *data, uint32_t length);
85 void print_namespace(struct nvme_namespace_data *nsdata);
86 void read_logpage(int fd, uint8_t log_page, uint32_t nsid, uint8_t lsp,
87     uint16_t lsi, uint8_t rae, uint64_t lpo, uint8_t csi, uint8_t ot,
88     uint16_t uuid_index, void *payload, uint32_t payload_size);
89 void print_temp_C(uint16_t t);
90 void print_temp_K(uint16_t t);
91 void print_intel_add_smart(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused);
92 
93 /* Utility Routines */
94 /*
95  * C23 supports 128-bit integers via _BitInt(128). clang 16 and gcc 13 support
96  * this. Older compilers will support 128-bit ints on 64-bit
97  * platforms. Otherwise we truncate this to 64-bit on 32-bit systems with older
98  * compilers. We also check for > C18 instead of >= C23 because clang 17 was
99  * released before the __STDC_VERSION__ was defined.
100  */
101 #define UINT128_DIG	39
102 #if __STDC_VERSION__ >= 202311L
103 typedef unsigned _BitInt(128) uint128_t;
104 #elif defined(__SIZEOF_INT128__)
105 typedef __uint128_t uint128_t;
106 #else
107 typedef uint64_t uint128_t;
108 #endif
109 
110 static __inline uint128_t
111 to128(void *p)
112 {
113 #if __STDC_VERSION__ >= 202311L || defined(__SIZEOF_INT128__)
114 	uint64_t lo, hi;
115 
116 	lo = le64dec(p);
117 	hi = le64dec((const char *)p + 8);
118 	return ((uint128_t)hi << 64 | lo);
119 #else
120 	return (le64dec(p));
121 #endif
122 }
123 
124 uint64_t le48dec(const void *pp);
125 char * uint128_to_str(uint128_t u, char *buf, size_t buflen);
126 #endif
127