]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/nvmecontrol/nvmecontrol.h
MFC r341409 (by imp): Move common logpage routines into nvmecontrol.h
[FreeBSD/FreeBSD.git] / sbin / nvmecontrol / nvmecontrol.h
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 <sys/linker_set.h>
35 #include <dev/nvme/nvme.h>
36
37 typedef void (*nvme_fn_t)(int argc, char *argv[]);
38
39 struct nvme_function {
40         const char      *name;
41         nvme_fn_t       fn;
42         const char      *usage;
43 };
44
45 #define NVME_CMDSET(set, sym)   DATA_SET(set, sym)
46 #define NVME_COMMAND(set, nam, function, usage_str)                     \
47         static struct nvme_function function ## _nvme_cmd =             \
48         { .name = #nam, .fn = function, .usage = usage_str };           \
49         NVME_CMDSET(set, function ## _nvme_cmd)
50
51 typedef void (*print_fn_t)(const struct nvme_controller_data *cdata, void *buf, uint32_t size);
52
53 struct logpage_function {
54         uint8_t         log_page;
55         const char     *vendor;
56         const char     *name;
57         print_fn_t      print_fn;
58         size_t          size;
59 };
60
61 #define NVME_LOGPAGESET(sym)            DATA_SET(logpage, sym)
62 #define NVME_LOGPAGE(unique, lp, vend, nam, fn, sz)                     \
63         static struct logpage_function unique ## _lpf = {               \
64                 .log_page = lp,                                         \
65                 .vendor = vend,                                         \
66                 .name = nam,                                            \
67                 .print_fn = fn,                                         \
68                 .size = sz,                                             \
69         } ;                                                             \
70         NVME_LOGPAGESET(unique ## _lpf)
71
72 #define DEFAULT_SIZE    (4096)
73 struct kv_name {
74         uint32_t key;
75         const char *name;
76 };
77
78 const char *kv_lookup(const struct kv_name *kv, size_t kv_count, uint32_t key);
79
80 #define NVME_CTRLR_PREFIX       "nvme"
81 #define NVME_NS_PREFIX          "ns"
82
83 int open_dev(const char *str, int *fd, int show_error, int exit_on_error);
84 void parse_ns_str(const char *ns_str, char *ctrlr_str, uint32_t *nsid);
85 void read_controller_data(int fd, struct nvme_controller_data *cdata);
86 void read_namespace_data(int fd, uint32_t nsid, struct nvme_namespace_data *nsdata);
87 void print_hex(void *data, uint32_t length);
88 void read_logpage(int fd, uint8_t log_page, uint32_t nsid, void *payload,
89     uint32_t payload_size);
90
91 void dispatch_set(int argc, char *argv[], struct nvme_function **tbl,
92     struct nvme_function **tbl_limit);
93
94 #define DISPATCH(argc, argv, set)       \
95         dispatch_set(argc, argv, SET_BEGIN(set), SET_LIMIT(set))
96
97 /* Utility Routines */
98 /*
99  * 128-bit integer augments to standard values. On i386 this
100  * doesn't exist, so we use 64-bit values. So, on 32-bit i386,
101  * you'll get truncated values until someone implement 128bit
102  * ints in sofware.
103  */
104 #define UINT128_DIG     39
105 #ifdef __i386__
106 typedef uint64_t uint128_t;
107 #else
108 typedef __uint128_t uint128_t;
109 #endif
110
111 static __inline uint128_t
112 to128(void *p)
113 {
114         return *(uint128_t *)p;
115 }
116
117 uint64_t le48dec(const void *pp);
118 char * uint128_to_str(uint128_t u, char *buf, size_t buflen);
119
120 #endif