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 struct nvme_function; 38 typedef void (*nvme_fn_t)(const struct nvme_function *nf, int argc, char *argv[]); 39 40 struct nvme_function { 41 const char *name; 42 nvme_fn_t fn; 43 const char *usage; 44 }; 45 46 #define NVME_SETNAME(set) set 47 #define NVME_CMDSET(set, sym) DATA_SET(NVME_SETNAME(set), sym) 48 #define NVME_COMMAND(set, nam, function, usage_str) \ 49 static struct nvme_function function ## _nvme_cmd = \ 50 { .name = #nam, .fn = function, .usage = usage_str }; \ 51 NVME_CMDSET(set, function ## _nvme_cmd) 52 #define NVME_CMD_BEGIN(set) SET_BEGIN(NVME_SETNAME(set)) 53 #define NVME_CMD_LIMIT(set) SET_LIMIT(NVME_SETNAME(set)) 54 #define NVME_CMD_DECLARE(set, t) SET_DECLARE(NVME_SETNAME(set), t) 55 56 typedef void (*print_fn_t)(const struct nvme_controller_data *cdata, void *buf, uint32_t size); 57 58 struct logpage_function { 59 uint8_t log_page; 60 const char *vendor; 61 const char *name; 62 print_fn_t print_fn; 63 size_t size; 64 }; 65 66 67 #define NVME_LOGPAGESET(sym) DATA_SET(NVME_SETNAME(logpage), sym) 68 #define NVME_LOGPAGE(unique, lp, vend, nam, fn, sz) \ 69 static struct logpage_function unique ## _lpf = { \ 70 .log_page = lp, \ 71 .vendor = vend, \ 72 .name = nam, \ 73 .print_fn = fn, \ 74 .size = sz, \ 75 } ; \ 76 NVME_LOGPAGESET(unique ## _lpf) 77 #define NVME_LOGPAGE_BEGIN SET_BEGIN(NVME_SETNAME(logpage)) 78 #define NVME_LOGPAGE_LIMIT SET_LIMIT(NVME_SETNAME(logpage)) 79 #define NVME_LOGPAGE_DECLARE(t) SET_DECLARE(NVME_SETNAME(logpage), t) 80 81 #define DEFAULT_SIZE (4096) 82 struct kv_name { 83 uint32_t key; 84 const char *name; 85 }; 86 87 const char *kv_lookup(const struct kv_name *kv, size_t kv_count, uint32_t key); 88 89 NVME_CMD_DECLARE(top, struct nvme_function); 90 NVME_LOGPAGE_DECLARE(struct logpage_function); 91 92 struct set_concat { 93 void **begin; 94 void **limit; 95 }; 96 void set_concat_add(struct set_concat *m, void *begin, void *end); 97 #define SET_CONCAT_DEF(set, t) \ 98 static struct set_concat set ## _concat; \ 99 static inline const t * const *set ## _begin(void) { return ((const t * const *)set ## _concat.begin); } \ 100 static inline const t * const *set ## _limit(void) { return ((const t * const *)set ## _concat.limit); } \ 101 void add_to_ ## set(t **b, t **e) \ 102 { \ 103 set_concat_add(&set ## _concat, b, e); \ 104 } 105 #define SET_CONCAT_DECL(set, t) \ 106 void add_to_ ## set(t **b, t **e) 107 SET_CONCAT_DECL(top, struct nvme_function); 108 SET_CONCAT_DECL(logpage, struct logpage_function); 109 110 #define NVME_CTRLR_PREFIX "nvme" 111 #define NVME_NS_PREFIX "ns" 112 113 int open_dev(const char *str, int *fd, int show_error, int exit_on_error); 114 void parse_ns_str(const char *ns_str, char *ctrlr_str, uint32_t *nsid); 115 void read_controller_data(int fd, struct nvme_controller_data *cdata); 116 void read_namespace_data(int fd, uint32_t nsid, struct nvme_namespace_data *nsdata); 117 void print_hex(void *data, uint32_t length); 118 void read_logpage(int fd, uint8_t log_page, uint32_t nsid, void *payload, 119 uint32_t payload_size); 120 void print_temp(uint16_t t); 121 void print_intel_add_smart(const struct nvme_controller_data *cdata __unused, void *buf, uint32_t size __unused); 122 123 void usage(const struct nvme_function *f); 124 void dispatch_set(int argc, char *argv[], const struct nvme_function * const *tbl, 125 const struct nvme_function * const *tbl_limit); 126 127 #define DISPATCH(argc, argv, set) \ 128 dispatch_set(argc, argv, \ 129 (const struct nvme_function * const *)NVME_CMD_BEGIN(set), \ 130 (const struct nvme_function * const *)NVME_CMD_LIMIT(set)) \ 131 132 /* Utility Routines */ 133 /* 134 * 128-bit integer augments to standard values. On i386 this 135 * doesn't exist, so we use 64-bit values. So, on 32-bit i386, 136 * you'll get truncated values until someone implement 128bit 137 * ints in sofware. 138 */ 139 #define UINT128_DIG 39 140 #ifdef __i386__ 141 typedef uint64_t uint128_t; 142 #else 143 typedef __uint128_t uint128_t; 144 #endif 145 146 static __inline uint128_t 147 to128(void *p) 148 { 149 return *(uint128_t *)p; 150 } 151 152 uint64_t le48dec(const void *pp); 153 char * uint128_to_str(uint128_t u, char *buf, size_t buflen); 154 155 #endif 156