1aa1207eaSJohn Baldwin /*- 2aa1207eaSJohn Baldwin * SPDX-License-Identifier: BSD-2-Clause 3aa1207eaSJohn Baldwin * 4aa1207eaSJohn Baldwin * Copyright (c) 2022-2024 Chelsio Communications, Inc. 5aa1207eaSJohn Baldwin * Written by: John Baldwin <jhb@FreeBSD.org> 6aa1207eaSJohn Baldwin */ 7aa1207eaSJohn Baldwin 8aa1207eaSJohn Baldwin #ifndef __NVMF_TRANSPORT_H__ 9aa1207eaSJohn Baldwin #define __NVMF_TRANSPORT_H__ 10aa1207eaSJohn Baldwin 11aa1207eaSJohn Baldwin /* 12aa1207eaSJohn Baldwin * Interface used by the Fabrics host (initiator) and controller 13aa1207eaSJohn Baldwin * (target) to send and receive capsules and associated data. 14aa1207eaSJohn Baldwin */ 15aa1207eaSJohn Baldwin 16aa1207eaSJohn Baldwin #include <sys/sysctl.h> 17aa1207eaSJohn Baldwin #include <dev/nvmf/nvmf_proto.h> 18aa1207eaSJohn Baldwin 19aa1207eaSJohn Baldwin struct mbuf; 20aa1207eaSJohn Baldwin struct memdesc; 21aa1207eaSJohn Baldwin struct nvmf_capsule; 22aa1207eaSJohn Baldwin struct nvmf_connection; 23aa1207eaSJohn Baldwin struct nvmf_qpair; 24aa1207eaSJohn Baldwin struct nvmf_handoff_qpair_params; 25aa1207eaSJohn Baldwin 26aa1207eaSJohn Baldwin SYSCTL_DECL(_kern_nvmf); 27aa1207eaSJohn Baldwin 28aa1207eaSJohn Baldwin /* 29aa1207eaSJohn Baldwin * Callback to invoke when an error occurs on a qpair. The last 30aa1207eaSJohn Baldwin * parameter is an error value. If the error value is zero, the qpair 31aa1207eaSJohn Baldwin * has been closed at the transport level rather than a transport 32aa1207eaSJohn Baldwin * error occuring. 33aa1207eaSJohn Baldwin */ 34aa1207eaSJohn Baldwin typedef void nvmf_qpair_error_t(void *, int); 35aa1207eaSJohn Baldwin 36aa1207eaSJohn Baldwin /* Callback to invoke when a capsule is received. */ 37aa1207eaSJohn Baldwin typedef void nvmf_capsule_receive_t(void *, struct nvmf_capsule *); 38aa1207eaSJohn Baldwin 39aa1207eaSJohn Baldwin /* 40aa1207eaSJohn Baldwin * Callback to invoke when an I/O request has completed. The second 41aa1207eaSJohn Baldwin * parameter is the amount of data transferred. The last parameter is 42aa1207eaSJohn Baldwin * an error value which is non-zero if the request did not complete 43aa1207eaSJohn Baldwin * successfully. A request with an error may complete partially. 44aa1207eaSJohn Baldwin */ 45aa1207eaSJohn Baldwin typedef void nvmf_io_complete_t(void *, size_t, int); 46aa1207eaSJohn Baldwin 47aa1207eaSJohn Baldwin /* 48aa1207eaSJohn Baldwin * A queue pair represents either an Admin or I/O 49aa1207eaSJohn Baldwin * submission/completion queue pair. The params contains negotiated 50aa1207eaSJohn Baldwin * values passed in from userland. 51aa1207eaSJohn Baldwin * 52aa1207eaSJohn Baldwin * Unlike libnvmf in userland, the kernel transport interface does not 53aa1207eaSJohn Baldwin * have any notion of an association. Instead, qpairs are 54aa1207eaSJohn Baldwin * independent. 55aa1207eaSJohn Baldwin */ 56aa1207eaSJohn Baldwin struct nvmf_qpair *nvmf_allocate_qpair(enum nvmf_trtype trtype, 57aa1207eaSJohn Baldwin bool controller, const struct nvmf_handoff_qpair_params *params, 58aa1207eaSJohn Baldwin nvmf_qpair_error_t *error_cb, void *error_cb_arg, 59aa1207eaSJohn Baldwin nvmf_capsule_receive_t *receive_cb, void *receive_cb_arg); 60aa1207eaSJohn Baldwin void nvmf_free_qpair(struct nvmf_qpair *qp); 61aa1207eaSJohn Baldwin 62aa1207eaSJohn Baldwin /* 63aa1207eaSJohn Baldwin * Capsules are either commands (host -> controller) or responses 64aa1207eaSJohn Baldwin * (controller -> host). A data buffer may be associated with a 65aa1207eaSJohn Baldwin * command capsule. Transmitted data is not copied by this API but 66aa1207eaSJohn Baldwin * instead must be preserved until the completion callback is invoked 67aa1207eaSJohn Baldwin * to indicate capsule transmission has completed. 68aa1207eaSJohn Baldwin */ 69aa1207eaSJohn Baldwin struct nvmf_capsule *nvmf_allocate_command(struct nvmf_qpair *qp, 70aa1207eaSJohn Baldwin const void *sqe, int how); 71aa1207eaSJohn Baldwin struct nvmf_capsule *nvmf_allocate_response(struct nvmf_qpair *qp, 72aa1207eaSJohn Baldwin const void *cqe, int how); 73aa1207eaSJohn Baldwin void nvmf_free_capsule(struct nvmf_capsule *nc); 74aa1207eaSJohn Baldwin int nvmf_capsule_append_data(struct nvmf_capsule *nc, 75aa1207eaSJohn Baldwin struct memdesc *mem, size_t len, bool send, 76aa1207eaSJohn Baldwin nvmf_io_complete_t *complete_cb, void *cb_arg); 77aa1207eaSJohn Baldwin int nvmf_transmit_capsule(struct nvmf_capsule *nc); 78aa1207eaSJohn Baldwin void nvmf_abort_capsule_data(struct nvmf_capsule *nc, int error); 79aa1207eaSJohn Baldwin void *nvmf_capsule_sqe(struct nvmf_capsule *nc); 80aa1207eaSJohn Baldwin void *nvmf_capsule_cqe(struct nvmf_capsule *nc); 81*4d3b659fSJohn Baldwin bool nvmf_sqhd_valid(struct nvmf_capsule *nc); 82aa1207eaSJohn Baldwin 83aa1207eaSJohn Baldwin /* Controller-specific APIs. */ 84aa1207eaSJohn Baldwin 85aa1207eaSJohn Baldwin /* 86aa1207eaSJohn Baldwin * A controller calls this function to check for any 87aa1207eaSJohn Baldwin * transport-specific errors (invalid fields) in a received command 88aa1207eaSJohn Baldwin * capsule. The callback returns a generic command status value: 89aa1207eaSJohn Baldwin * NVME_SC_SUCCESS if no error is found. 90aa1207eaSJohn Baldwin */ 91aa1207eaSJohn Baldwin uint8_t nvmf_validate_command_capsule(struct nvmf_capsule *nc); 92aa1207eaSJohn Baldwin 93aa1207eaSJohn Baldwin /* 94aa1207eaSJohn Baldwin * A controller calls this function to query the amount of data 95aa1207eaSJohn Baldwin * associated with a command capsule. 96aa1207eaSJohn Baldwin */ 97aa1207eaSJohn Baldwin size_t nvmf_capsule_data_len(const struct nvmf_capsule *cc); 98aa1207eaSJohn Baldwin 99aa1207eaSJohn Baldwin /* 100aa1207eaSJohn Baldwin * A controller calls this function to receive data associated with a 101aa1207eaSJohn Baldwin * command capsule (e.g. the data for a WRITE command). This can 102aa1207eaSJohn Baldwin * either return in-capsule data or fetch data from the host 103aa1207eaSJohn Baldwin * (e.g. using a R2T PDU over TCP). The received command capsule 104aa1207eaSJohn Baldwin * should be passed in 'nc'. The received data is stored in 'mem'. 105aa1207eaSJohn Baldwin * If this function returns success, then the callback will be invoked 106aa1207eaSJohn Baldwin * once the operation has completed. Note that the callback might be 107aa1207eaSJohn Baldwin * invoked before this function returns. 108aa1207eaSJohn Baldwin */ 109aa1207eaSJohn Baldwin int nvmf_receive_controller_data(struct nvmf_capsule *nc, 110aa1207eaSJohn Baldwin uint32_t data_offset, struct memdesc *mem, size_t len, 111aa1207eaSJohn Baldwin nvmf_io_complete_t *complete_cb, void *cb_arg); 112aa1207eaSJohn Baldwin 113aa1207eaSJohn Baldwin /* 114aa1207eaSJohn Baldwin * A controller calls this function to send data in response to a 115aa1207eaSJohn Baldwin * command prior to sending a response capsule. If an error occurs, 116aa1207eaSJohn Baldwin * the function returns a generic status completion code to be sent in 117aa1207eaSJohn Baldwin * the following CQE. Note that the transfer might send a subset of 118aa1207eaSJohn Baldwin * the data requested by nc. If the transfer succeeds, this function 119aa1207eaSJohn Baldwin * can return one of the following values: 120aa1207eaSJohn Baldwin * 121aa1207eaSJohn Baldwin * - NVME_SC_SUCCESS: The transfer has completed successfully and the 122aa1207eaSJohn Baldwin * caller should send a success CQE in a response capsule. 123aa1207eaSJohn Baldwin * 124aa1207eaSJohn Baldwin * - NVMF_SUCCESS_SENT: The transfer has completed successfully and 125aa1207eaSJohn Baldwin * the transport layer has sent an implicit success CQE to the 126aa1207eaSJohn Baldwin * remote host (e.g. the SUCCESS flag for TCP). The caller should 127aa1207eaSJohn Baldwin * not send a response capsule. 128aa1207eaSJohn Baldwin * 129aa1207eaSJohn Baldwin * - NVMF_MORE: The transfer has completed successfully, but the 130aa1207eaSJohn Baldwin * transfer did not complete the data buffer. 131aa1207eaSJohn Baldwin * 132aa1207eaSJohn Baldwin * The mbuf chain in 'm' is consumed by this function even if an error 133aa1207eaSJohn Baldwin * is returned. 134aa1207eaSJohn Baldwin */ 135aa1207eaSJohn Baldwin u_int nvmf_send_controller_data(struct nvmf_capsule *nc, 136aa1207eaSJohn Baldwin uint32_t data_offset, struct mbuf *m, size_t len); 137aa1207eaSJohn Baldwin 138aa1207eaSJohn Baldwin #define NVMF_SUCCESS_SENT 0x100 139aa1207eaSJohn Baldwin #define NVMF_MORE 0x101 140aa1207eaSJohn Baldwin 141aa1207eaSJohn Baldwin #endif /* !__NVMF_TRANSPORT_H__ */ 142