xref: /freebsd/lib/libnvmf/internal.h (revision 8bba2c0f8958443790b1f3abc0675719da987e87)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2022-2024 Chelsio Communications, Inc.
5  * Written by: John Baldwin <jhb@FreeBSD.org>
6  */
7 
8 #ifndef __LIBNVMF_INTERNAL_H__
9 #define __LIBNVMF_INTERNAL_H__
10 
11 #include <sys/nv.h>
12 #include <sys/queue.h>
13 
14 struct nvmf_transport_ops {
15 	/* Association management. */
16 	struct nvmf_association *(*allocate_association)(bool controller,
17 	    const struct nvmf_association_params *params);
18 	void (*update_association)(struct nvmf_association *na,
19 	    const struct nvme_controller_data *cdata);
20 	void (*free_association)(struct nvmf_association *na);
21 
22 	/* Queue pair management. */
23 	struct nvmf_qpair *(*allocate_qpair)(struct nvmf_association *na,
24 	    const struct nvmf_qpair_params *params);
25 	void (*free_qpair)(struct nvmf_qpair *qp);
26 
27 	/* Add params for kernel handoff. */
28 	void (*kernel_handoff_params)(struct nvmf_qpair *qp, nvlist_t *nvl);
29 	int (*populate_dle)(struct nvmf_qpair *qp,
30 	    struct nvme_discovery_log_entry *dle);
31 
32 	/* Capsule operations. */
33 	struct nvmf_capsule *(*allocate_capsule)(struct nvmf_qpair *qp);
34 	void (*free_capsule)(struct nvmf_capsule *nc);
35 	int (*transmit_capsule)(struct nvmf_capsule *nc);
36 	int (*receive_capsule)(struct nvmf_qpair *qp,
37 	    struct nvmf_capsule **ncp);
38 	uint8_t (*validate_command_capsule)(const struct nvmf_capsule *nc);
39 
40 	/* Transferring controller data. */
41 	size_t (*capsule_data_len)(const struct nvmf_capsule *nc);
42 	int (*receive_controller_data)(const struct nvmf_capsule *nc,
43 	    uint32_t data_offset, void *buf, size_t len);
44 	int (*send_controller_data)(const struct nvmf_capsule *nc,
45 	    const void *buf, size_t len);
46 };
47 
48 struct nvmf_association {
49 	struct nvmf_transport_ops *na_ops;
50 	enum nvmf_trtype na_trtype;
51 	bool na_controller;
52 
53 	struct nvmf_association_params na_params;
54 
55 	/* Each qpair holds a reference on an association. */
56 	u_int na_refs;
57 
58 	char *na_last_error;
59 };
60 
61 struct nvmf_qpair {
62 	struct nvmf_association *nq_association;
63 	bool nq_admin;
64 
65 	uint16_t nq_cid;	/* host only */
66 
67 	/*
68 	 * Queue sizes.  This assumes the same size for both the
69 	 * completion and submission queues within a pair.
70 	 */
71 	u_int	nq_qsize;
72 
73 	/* Flow control management for submission queues. */
74 	bool nq_flow_control;
75 	uint16_t nq_sqhd;
76 	uint16_t nq_sqtail;	/* host only */
77 
78 	/* Value in response to/from CONNECT. */
79 	uint16_t nq_cntlid;
80 
81 	uint32_t nq_kato;	/* valid on admin queue only */
82 
83 	TAILQ_HEAD(, nvmf_capsule) nq_rx_capsules;
84 };
85 
86 struct nvmf_capsule {
87 	struct nvmf_qpair *nc_qpair;
88 
89 	/* Either a SQE or CQE. */
90 	union {
91 		struct nvme_command nc_sqe;
92 		struct nvme_completion nc_cqe;
93 	};
94 	int	nc_qe_len;
95 
96 	/*
97 	 * Is SQHD in received capsule valid?  False for locally-
98 	 * synthesized responses.
99 	 */
100 	bool	nc_sqhd_valid;
101 
102 	/* Data buffer. */
103 	bool	nc_send_data;
104 	void	*nc_data;
105 	size_t	nc_data_len;
106 
107 	TAILQ_ENTRY(nvmf_capsule) nc_link;
108 };
109 
110 extern struct nvmf_transport_ops tcp_ops;
111 
112 void	na_clear_error(struct nvmf_association *na);
113 void	na_error(struct nvmf_association *na, const char *fmt, ...);
114 
115 int	nvmf_kernel_handoff_params(struct nvmf_qpair *qp, nvlist_t **nvlp);
116 int	nvmf_populate_dle(struct nvmf_qpair *qp,
117     struct nvme_discovery_log_entry *dle);
118 int	nvmf_pack_ioc_nvlist(struct nvmf_ioc_nv *nv, nvlist_t *nvl);
119 
120 #endif /* !__LIBNVMF_INTERNAL_H__ */
121