|
| 1 | +// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 | +/* |
| 3 | + * This file is part of libnvme. |
| 4 | + * Copyright (c) 2021 Code Construct Pty Ltd |
| 5 | + * |
| 6 | + * Authors: Jeremy Kerr <[email protected]> |
| 7 | + */ |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <poll.h> |
| 12 | + |
| 13 | +#include <sys/socket.h> |
| 14 | + |
| 15 | +#include <ccan/list/list.h> |
| 16 | + |
| 17 | +#include <nvme/mi.h> |
| 18 | + |
| 19 | +/* internal transport API */ |
| 20 | +struct libnvme_mi_req { |
| 21 | + struct nvme_mi_msg_hdr *hdr; |
| 22 | + size_t hdr_len; |
| 23 | + void *data; |
| 24 | + size_t data_len; |
| 25 | + __u32 mic; |
| 26 | +}; |
| 27 | + |
| 28 | +struct libnvme_mi_resp { |
| 29 | + struct nvme_mi_msg_hdr *hdr; |
| 30 | + size_t hdr_len; |
| 31 | + void *data; |
| 32 | + size_t data_len; |
| 33 | + __u32 mic; |
| 34 | +}; |
| 35 | + |
| 36 | +struct libnvme_mi_aem_ctx { |
| 37 | + struct nvme_mi_aem_occ_list_hdr *occ_header; |
| 38 | + struct nvme_mi_aem_occ_data *list_start; |
| 39 | + struct nvme_mi_aem_occ_data *list_current; |
| 40 | + int list_current_index; |
| 41 | + struct libnvme_mi_aem_config callbacks; |
| 42 | + int last_generation_num; |
| 43 | + struct libnvme_mi_event event; |
| 44 | +}; |
| 45 | + |
| 46 | +struct libnvme_mi_ep { |
| 47 | + struct libnvme_global_ctx *ctx; |
| 48 | + const struct libnvme_mi_transport *transport; |
| 49 | + void *transport_data; |
| 50 | + struct list_node root_entry; |
| 51 | + struct list_head controllers; |
| 52 | + bool quirks_probed; |
| 53 | + bool controllers_scanned; |
| 54 | + unsigned int timeout; |
| 55 | + unsigned int mprt_max; |
| 56 | + unsigned long quirks; |
| 57 | + |
| 58 | + __u8 csi; |
| 59 | + |
| 60 | + /* inter-command delay, for LIBNVME_QUIRK_MIN_INTER_COMMAND_TIME */ |
| 61 | + unsigned int inter_command_us; |
| 62 | + struct timespec last_resp_time; |
| 63 | + bool last_resp_time_valid; |
| 64 | + |
| 65 | + struct libnvme_mi_aem_ctx *aem_ctx; |
| 66 | +}; |
| 67 | + |
| 68 | +struct libnvme_mi_transport { |
| 69 | + const char *name; |
| 70 | + bool mic_enabled; |
| 71 | + int (*submit)(struct libnvme_mi_ep *ep, |
| 72 | + struct libnvme_mi_req *req, |
| 73 | + struct libnvme_mi_resp *resp); |
| 74 | + void (*close)(struct libnvme_mi_ep *ep); |
| 75 | + int (*desc_ep)(struct libnvme_mi_ep *ep, char *buf, size_t len); |
| 76 | + int (*check_timeout)(struct libnvme_mi_ep *ep, unsigned int timeout); |
| 77 | + int (*aem_fd)(struct libnvme_mi_ep *ep); |
| 78 | + int (*aem_read)(struct libnvme_mi_ep *ep, |
| 79 | + struct libnvme_mi_resp *resp); |
| 80 | + int (*aem_purge)(struct libnvme_mi_ep *ep); |
| 81 | +}; |
| 82 | + |
| 83 | +struct libnvme_mi_ep *libnvme_mi_init_ep(struct libnvme_global_ctx *ctx); |
| 84 | +void libnvme_mi_ep_probe(struct libnvme_mi_ep *ep); |
| 85 | + |
| 86 | +/* for tests, we need to calculate the correct MICs */ |
| 87 | +__u32 libnvme_mi_crc32_update(__u32 crc, void *data, size_t len); |
| 88 | + |
| 89 | +/* we have a facility to mock MCTP socket operations in the mi-mctp transport, |
| 90 | + * using this ops type. This should only be used for test, and isn't exposed |
| 91 | + * in the shared lib */; |
| 92 | +struct mctp_ioc_tag_ctl; |
| 93 | +struct __mi_mctp_socket_ops { |
| 94 | + int (*msg_socket)(void); |
| 95 | + int (*aem_socket)(__u8 eid, unsigned int network); |
| 96 | + ssize_t (*sendmsg)(int, const struct msghdr *, int); |
| 97 | + ssize_t (*recvmsg)(int, struct msghdr *, int); |
| 98 | + int (*poll)(struct pollfd *, nfds_t, int); |
| 99 | + int (*ioctl_tag)(int, unsigned long, struct mctp_ioc_tag_ctl *); |
| 100 | +}; |
| 101 | +void __libnvme_mi_mctp_set_ops(const struct __mi_mctp_socket_ops *newops); |
| 102 | + |
| 103 | +/* quirks */ |
| 104 | + |
| 105 | +/* Set a minimum time between receiving a response from one command and |
| 106 | + * sending the next request. Some devices may ignore new commands sent too soon |
| 107 | + * after the previous request, so manually insert a delay |
| 108 | + */ |
| 109 | +#define LIBNVME_QUIRK_MIN_INTER_COMMAND_TIME (1 << 0) |
| 110 | + |
| 111 | +/* Some devices may not support using CSI 1. Attempting to set an |
| 112 | + * endpoint to use this with these devices should return an error |
| 113 | + */ |
| 114 | +#define LIBNVME_QUIRK_CSI_1_NOT_SUPPORTED (1 << 1) |
| 115 | + |
| 116 | +int __libnvme_transport_handle_open_mi(struct libnvme_transport_handle *hdl, |
| 117 | + const char *devname); |
| 118 | +int __libnvme_transport_handle_init_mi(struct libnvme_transport_handle *hdl); |
| 119 | +void __libnvme_transport_handle_close_mi(struct libnvme_transport_handle *hdl); |
0 commit comments