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