| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/* |
| 3 |
* This file is part of libnvme. |
| 4 |
* Copyright (c) 2021 SUSE Software Solutions |
| 5 |
* |
| 6 |
* Authors: Hannes Reinecke <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
#ifndef _LIBNVME_PRIVATE_H |
| 10 |
#define _LIBNVME_PRIVATE_H |
| 11 |
|
| 12 |
#include <ccan/list/list.h> |
| 13 |
#include <poll.h> |
| 14 |
#include <sys/socket.h> |
| 15 |
|
| 16 |
#include <nvme/fabrics.h> |
| 17 |
#include <nvme/mi.h> |
| 18 |
|
| 19 |
const char *nvme_subsys_sysfs_dir(void); |
| 20 |
const char *nvme_ctrl_sysfs_dir(void); |
| 21 |
const char *nvme_ns_sysfs_dir(void); |
| 22 |
const char *nvme_slots_sysfs_dir(void); |
| 23 |
const char *nvme_uuid_ibm_filename(void); |
| 24 |
const char *nvme_dmi_entries_dir(void); |
| 25 |
|
| 26 |
struct nvme_path { |
| 27 |
struct list_node entry; |
| 28 |
struct list_node nentry; |
| 29 |
|
| 30 |
struct nvme_ctrl *c; |
| 31 |
struct nvme_ns *n; |
| 32 |
|
| 33 |
char *name; |
| 34 |
char *sysfs_dir; |
| 35 |
char *ana_state; |
| 36 |
char *numa_nodes; |
| 37 |
int grpid; |
| 38 |
int queue_depth; |
| 39 |
}; |
| 40 |
|
| 41 |
struct nvme_ns_head { |
| 42 |
struct list_head paths; |
| 43 |
struct nvme_ns *n; |
| 44 |
|
| 45 |
char *sysfs_dir; |
| 46 |
}; |
| 47 |
|
| 48 |
struct nvme_ns { |
| 49 |
struct list_node entry; |
| 50 |
|
| 51 |
struct nvme_subsystem *s; |
| 52 |
struct nvme_ctrl *c; |
| 53 |
struct nvme_ns_head *head; |
| 54 |
|
| 55 |
int fd; |
| 56 |
__u32 nsid; |
| 57 |
char *name; |
| 58 |
char *generic_name; |
| 59 |
char *sysfs_dir; |
| 60 |
|
| 61 |
int lba_shift; |
| 62 |
int lba_size; |
| 63 |
int meta_size; |
| 64 |
uint64_t lba_count; |
| 65 |
uint64_t lba_util; |
| 66 |
|
| 67 |
uint8_t eui64[8]; |
| 68 |
uint8_t nguid[16]; |
| 69 |
unsigned char uuid[NVME_UUID_LEN]; |
| 70 |
enum nvme_csi csi; |
| 71 |
}; |
| 72 |
|
| 73 |
struct nvme_ctrl { |
| 74 |
struct list_node entry; |
| 75 |
struct list_head paths; |
| 76 |
struct list_head namespaces; |
| 77 |
struct nvme_subsystem *s; |
| 78 |
|
| 79 |
int fd; |
| 80 |
char *name; |
| 81 |
char *sysfs_dir; |
| 82 |
char *address; |
| 83 |
char *firmware; |
| 84 |
char *model; |
| 85 |
char *state; |
| 86 |
char *numa_node; |
| 87 |
char *queue_count; |
| 88 |
char *serial; |
| 89 |
char *sqsize; |
| 90 |
char *transport; |
| 91 |
char *subsysnqn; |
| 92 |
char *traddr; |
| 93 |
char *trsvcid; |
| 94 |
char *dhchap_key; |
| 95 |
char *dhchap_ctrl_key; |
| 96 |
char *keyring; |
| 97 |
char *tls_key_identity; |
| 98 |
char *tls_key; |
| 99 |
char *cntrltype; |
| 100 |
char *cntlid; |
| 101 |
char *dctype; |
| 102 |
char *phy_slot; |
| 103 |
bool discovery_ctrl; |
| 104 |
bool unique_discovery_ctrl; |
| 105 |
bool discovered; |
| 106 |
bool persistent; |
| 107 |
struct nvme_fabrics_config cfg; |
| 108 |
}; |
| 109 |
|
| 110 |
struct nvme_subsystem { |
| 111 |
struct list_node entry; |
| 112 |
struct list_head ctrls; |
| 113 |
struct list_head namespaces; |
| 114 |
struct nvme_host *h; |
| 115 |
|
| 116 |
char *name; |
| 117 |
char *sysfs_dir; |
| 118 |
char *subsysnqn; |
| 119 |
char *model; |
| 120 |
char *serial; |
| 121 |
char *firmware; |
| 122 |
char *subsystype; |
| 123 |
char *application; |
| 124 |
char *iopolicy; |
| 125 |
}; |
| 126 |
|
| 127 |
struct nvme_host { |
| 128 |
struct list_node entry; |
| 129 |
struct list_head subsystems; |
| 130 |
struct nvme_root *r; |
| 131 |
|
| 132 |
char *hostnqn; |
| 133 |
char *hostid; |
| 134 |
char *dhchap_key; |
| 135 |
char *hostsymname; |
| 136 |
bool pdc_enabled; |
| 137 |
bool pdc_enabled_valid; /* set if pdc_enabled doesn't have an undefined |
| 138 |
* value */ |
| 139 |
}; |
| 140 |
|
| 141 |
struct nvme_fabric_options { |
| 142 |
bool cntlid; |
| 143 |
bool concat; |
| 144 |
bool ctrl_loss_tmo; |
| 145 |
bool data_digest; |
| 146 |
bool dhchap_ctrl_secret; |
| 147 |
bool dhchap_secret; |
| 148 |
bool disable_sqflow; |
| 149 |
bool discovery; |
| 150 |
bool duplicate_connect; |
| 151 |
bool fast_io_fail_tmo; |
| 152 |
bool hdr_digest; |
| 153 |
bool host_iface; |
| 154 |
bool host_traddr; |
| 155 |
bool hostid; |
| 156 |
bool hostnqn; |
| 157 |
bool instance; |
| 158 |
bool keep_alive_tmo; |
| 159 |
bool keyring; |
| 160 |
bool nqn; |
| 161 |
bool nr_io_queues; |
| 162 |
bool nr_poll_queues; |
| 163 |
bool nr_write_queues; |
| 164 |
bool queue_size; |
| 165 |
bool reconnect_delay; |
| 166 |
bool tls; |
| 167 |
bool tls_key; |
| 168 |
bool tos; |
| 169 |
bool traddr; |
| 170 |
bool transport; |
| 171 |
bool trsvcid; |
| 172 |
}; |
| 173 |
|
| 174 |
struct nvme_log { |
| 175 |
int fd; |
| 176 |
int level; |
| 177 |
bool pid; |
| 178 |
bool timestamp; |
| 179 |
}; |
| 180 |
|
| 181 |
struct nvme_root { |
| 182 |
char *config_file; |
| 183 |
char *application; |
| 184 |
struct list_head hosts; |
| 185 |
struct list_head endpoints; /* MI endpoints */ |
| 186 |
struct nvme_log log; |
| 187 |
bool modified; |
| 188 |
bool mi_probe_enabled; |
| 189 |
bool create_only; |
| 190 |
struct nvme_fabric_options *options; |
| 191 |
}; |
| 192 |
|
| 193 |
int nvme_set_attr(const char *dir, const char *attr, const char *value); |
| 194 |
|
| 195 |
int json_read_config(nvme_root_t r, const char *config_file); |
| 196 |
|
| 197 |
int json_update_config(nvme_root_t r, const char *config_file); |
| 198 |
|
| 199 |
int json_dump_tree(nvme_root_t r); |
| 200 |
|
| 201 |
nvme_ctrl_t __nvme_lookup_ctrl(nvme_subsystem_t s, const char *transport, |
| 202 |
const char *traddr, const char *host_traddr, |
| 203 |
const char *host_iface, const char *trsvcid, |
| 204 |
const char *subsysnqn, nvme_ctrl_t p); |
| 205 |
|
| 206 |
void *__nvme_alloc(size_t len); |
| 207 |
|
| 208 |
void *__nvme_realloc(void *p, size_t len); |
| 209 |
|
| 210 |
#if (LOG_FUNCNAME == 1) |
| 211 |
#define __nvme_log_func __func__ |
| 212 |
#else |
| 213 |
#define __nvme_log_func NULL |
| 214 |
#endif |
| 215 |
|
| 216 |
void __attribute__((format(printf, 4, 5))) |
| 217 |
__nvme_msg(nvme_root_t r, int level, const char *func, const char *format, ...); |
| 218 |
|
| 219 |
#define nvme_msg(r, level, format, ...) \ |
| 220 |
__nvme_msg(r, level, __nvme_log_func, format, ##__VA_ARGS__) |
| 221 |
|
| 222 |
#define root_from_ctrl(c) ((c)->s && (c)->s->h ? (c)->s->h->r : NULL) |
| 223 |
#define root_from_ns(n) ((n)->s && (n)->s->h ? (n)->s->h->r : \ |
| 224 |
(n)->c && (n)->c->s && (n)->c->s->h ? (n)->c->s->h->r : \ |
| 225 |
NULL) |
| 226 |
|
| 227 |
/* mi internal headers */ |
| 228 |
|
| 229 |
/* internal transport API */ |
| 230 |
struct nvme_mi_req { |
| 231 |
struct nvme_mi_msg_hdr *hdr; |
| 232 |
size_t hdr_len; |
| 233 |
void *data; |
| 234 |
size_t data_len; |
| 235 |
__u32 mic; |
| 236 |
}; |
| 237 |
|
| 238 |
struct nvme_mi_resp { |
| 239 |
struct nvme_mi_msg_hdr *hdr; |
| 240 |
size_t hdr_len; |
| 241 |
void *data; |
| 242 |
size_t data_len; |
| 243 |
__u32 mic; |
| 244 |
}; |
| 245 |
|
| 246 |
struct nvme_mi_transport { |
| 247 |
const char *name; |
| 248 |
bool mic_enabled; |
| 249 |
int (*submit)(struct nvme_mi_ep *ep, |
| 250 |
struct nvme_mi_req *req, |
| 251 |
struct nvme_mi_resp *resp); |
| 252 |
void (*close)(struct nvme_mi_ep *ep); |
| 253 |
int (*desc_ep)(struct nvme_mi_ep *ep, char *buf, size_t len); |
| 254 |
int (*check_timeout)(struct nvme_mi_ep *ep, unsigned int timeout); |
| 255 |
int (*aem_fd)(struct nvme_mi_ep *ep); |
| 256 |
int (*aem_read)(struct nvme_mi_ep *ep, |
| 257 |
struct nvme_mi_resp *resp); |
| 258 |
int (*aem_purge)(struct nvme_mi_ep *ep); |
| 259 |
}; |
| 260 |
|
| 261 |
struct nvme_mi_aem_ctx { |
| 262 |
struct nvme_mi_aem_occ_list_hdr *occ_header; |
| 263 |
struct nvme_mi_aem_occ_data *list_start; |
| 264 |
struct nvme_mi_aem_occ_data *list_current; |
| 265 |
int list_current_index; |
| 266 |
struct nvme_mi_aem_config callbacks; |
| 267 |
int last_generation_num; |
| 268 |
struct nvme_mi_event event; |
| 269 |
}; |
| 270 |
|
| 271 |
/* quirks */ |
| 272 |
|
| 273 |
/* Set a minimum time between receiving a response from one command and |
| 274 |
* sending the next request. Some devices may ignore new commands sent too soon |
| 275 |
* after the previous request, so manually insert a delay |
| 276 |
*/ |
| 277 |
#define NVME_QUIRK_MIN_INTER_COMMAND_TIME (1 << 0) |
| 278 |
|
| 279 |
/* Some devices may not support using CSI 1. Attempting to set an |
| 280 |
* endpoint to use this with these devices should return an error |
| 281 |
*/ |
| 282 |
#define NVME_QUIRK_CSI_1_NOT_SUPPORTED (1 << 1) |
| 283 |
|
| 284 |
struct nvme_mi_ep { |
| 285 |
struct nvme_root *root; |
| 286 |
const struct nvme_mi_transport *transport; |
| 287 |
void *transport_data; |
| 288 |
struct list_node root_entry; |
| 289 |
struct list_head controllers; |
| 290 |
bool quirks_probed; |
| 291 |
bool controllers_scanned; |
| 292 |
unsigned int timeout; |
| 293 |
unsigned int mprt_max; |
| 294 |
unsigned long quirks; |
| 295 |
|
| 296 |
__u8 csi; |
| 297 |
|
| 298 |
/* inter-command delay, for NVME_QUIRK_MIN_INTER_COMMAND_TIME */ |
| 299 |
unsigned int inter_command_us; |
| 300 |
struct timespec last_resp_time; |
| 301 |
bool last_resp_time_valid; |
| 302 |
|
| 303 |
struct nvme_mi_aem_ctx *aem_ctx; |
| 304 |
}; |
| 305 |
|
| 306 |
struct nvme_mi_ctrl { |
| 307 |
struct nvme_mi_ep *ep; |
| 308 |
__u16 id; |
| 309 |
struct list_node ep_entry; |
| 310 |
}; |
| 311 |
|
| 312 |
struct nvme_mi_ep *nvme_mi_init_ep(struct nvme_root *root); |
| 313 |
void nvme_mi_ep_probe(struct nvme_mi_ep *ep); |
| 314 |
|
| 315 |
/* for tests, we need to calculate the correct MICs */ |
| 316 |
__u32 nvme_mi_crc32_update(__u32 crc, void *data, size_t len); |
| 317 |
|
| 318 |
/* we have a facility to mock MCTP socket operations in the mi-mctp transport, |
| 319 |
* using this ops type. This should only be used for test, and isn't exposed |
| 320 |
* in the shared lib */; |
| 321 |
struct mctp_ioc_tag_ctl; |
| 322 |
struct __mi_mctp_socket_ops { |
| 323 |
int (*msg_socket)(void); |
| 324 |
int (*aem_socket)(__u8 eid, unsigned int network); |
| 325 |
ssize_t (*sendmsg)(int, const struct msghdr *, int); |
| 326 |
ssize_t (*recvmsg)(int, struct msghdr *, int); |
| 327 |
int (*poll)(struct pollfd *, nfds_t, int); |
| 328 |
int (*ioctl_tag)(int, unsigned long, struct mctp_ioc_tag_ctl *); |
| 329 |
}; |
| 330 |
void __nvme_mi_mctp_set_ops(const struct __mi_mctp_socket_ops *newops); |
| 331 |
|
| 332 |
#define SECTOR_SIZE 512 |
| 333 |
#define SECTOR_SHIFT 9 |
| 334 |
|
| 335 |
int __nvme_import_keys_from_config(nvme_host_t h, nvme_ctrl_t c, |
| 336 |
long *keyring_id, long *key_id); |
| 337 |
|
| 338 |
static inline char *xstrdup(const char *s) |
| 339 |
{ |
| 340 |
if (!s) |
| 341 |
return NULL; |
| 342 |
return strdup(s); |
| 343 |
} |
| 344 |
|
| 345 |
#endif /* _LIBNVME_PRIVATE_H */ |