xref: /freebsd/sys/dev/nvmf/host/nvmf_var.h (revision 5036d9652a5701d00e9e40ea942c278e9f77d33d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2023-2024 Chelsio Communications, Inc.
5  * Written by: John Baldwin <jhb@FreeBSD.org>
6  */
7 
8 #ifndef __NVMF_VAR_H__
9 #define	__NVMF_VAR_H__
10 
11 #include <sys/_callout.h>
12 #include <sys/_eventhandler.h>
13 #include <sys/_lock.h>
14 #include <sys/_mutex.h>
15 #include <sys/_sx.h>
16 #include <sys/_task.h>
17 #include <sys/queue.h>
18 #include <dev/nvme/nvme.h>
19 #include <dev/nvmf/nvmf_transport.h>
20 
21 struct nvmf_aer;
22 struct nvmf_capsule;
23 struct nvmf_host_qpair;
24 struct nvmf_namespace;
25 struct sysctl_oid_list;
26 
27 typedef void nvmf_request_complete_t(void *, const struct nvme_completion *);
28 
29 struct nvmf_ivars {
30 	struct nvmf_handoff_host *hh;
31 	struct nvmf_handoff_qpair_params *io_params;
32 	struct nvme_controller_data *cdata;
33 };
34 
35 struct nvmf_softc {
36 	device_t dev;
37 
38 	struct nvmf_host_qpair *admin;
39 	struct nvmf_host_qpair **io;
40 	u_int	num_io_queues;
41 	enum nvmf_trtype trtype;
42 
43 	struct cam_sim *sim;
44 	struct cam_path *path;
45 	struct mtx sim_mtx;
46 	bool sim_disconnected;
47 	bool sim_shutdown;
48 
49 	struct nvmf_namespace **ns;
50 
51 	struct nvme_controller_data *cdata;
52 	uint64_t cap;
53 	uint32_t vs;
54 	u_int max_pending_io;
55 	u_long max_xfer_size;
56 
57 	struct cdev *cdev;
58 
59 	/*
60 	 * Keep Alive support depends on two timers.  The 'tx' timer
61 	 * is responsible for sending KeepAlive commands and runs at
62 	 * half the timeout interval.  The 'rx' timer is responsible
63 	 * for detecting an actual timeout.
64 	 *
65 	 * For efficient support of TKAS, the host does not reschedule
66 	 * these timers every time new commands are scheduled.
67 	 * Instead, the host sets the *_traffic flags when commands
68 	 * are sent and received.  The timeout handlers check and
69 	 * clear these flags.  This does mean it can take up to twice
70 	 * the timeout time to detect an AWOL controller.
71 	 */
72 	bool	ka_traffic;			/* Using TKAS? */
73 
74 	volatile int ka_active_tx_traffic;
75 	struct callout ka_tx_timer;
76 	sbintime_t ka_tx_sbt;
77 
78 	volatile int ka_active_rx_traffic;
79 	struct callout ka_rx_timer;
80 	sbintime_t ka_rx_sbt;
81 
82 	struct sx connection_lock;
83 	struct task disconnect_task;
84 	bool detaching;
85 
86 	u_int num_aer;
87 	struct nvmf_aer *aer;
88 
89 	struct sysctl_oid_list *ioq_oid_list;
90 
91 	eventhandler_tag shutdown_pre_sync_eh;
92 	eventhandler_tag shutdown_post_sync_eh;
93 };
94 
95 struct nvmf_request {
96 	struct nvmf_host_qpair *qp;
97 	struct nvmf_capsule *nc;
98 	nvmf_request_complete_t *cb;
99 	void	*cb_arg;
100 	bool	aer;
101 
102 	STAILQ_ENTRY(nvmf_request) link;
103 };
104 
105 struct nvmf_completion_status {
106 	struct nvme_completion cqe;
107 	bool	done;
108 	bool	io_done;
109 	int	io_error;
110 };
111 
112 static __inline struct nvmf_host_qpair *
113 nvmf_select_io_queue(struct nvmf_softc *sc)
114 {
115 	/* TODO: Support multiple queues? */
116 	return (sc->io[0]);
117 }
118 
119 static __inline bool
120 nvmf_cqe_aborted(const struct nvme_completion *cqe)
121 {
122 	uint16_t status;
123 
124 	status = le16toh(cqe->status);
125 	return (NVME_STATUS_GET_SCT(status) == NVME_SCT_PATH_RELATED &&
126 	    NVME_STATUS_GET_SC(status) == NVME_SC_COMMAND_ABORTED_BY_HOST);
127 }
128 
129 static __inline void
130 nvmf_status_init(struct nvmf_completion_status *status)
131 {
132 	status->done = false;
133 	status->io_done = true;
134 	status->io_error = 0;
135 }
136 
137 static __inline void
138 nvmf_status_wait_io(struct nvmf_completion_status *status)
139 {
140 	status->io_done = false;
141 }
142 
143 #ifdef DRIVER_MODULE
144 extern driver_t nvme_nvmf_driver;
145 #endif
146 
147 #ifdef MALLOC_DECLARE
148 MALLOC_DECLARE(M_NVMF);
149 #endif
150 
151 /* If true, I/O requests will fail while the host is disconnected. */
152 extern bool nvmf_fail_disconnect;
153 
154 /* nvmf.c */
155 void	nvmf_complete(void *arg, const struct nvme_completion *cqe);
156 void	nvmf_io_complete(void *arg, size_t xfered, int error);
157 void	nvmf_wait_for_reply(struct nvmf_completion_status *status);
158 int	nvmf_init_ivars(struct nvmf_ivars *ivars, struct nvmf_handoff_host *hh);
159 void	nvmf_free_ivars(struct nvmf_ivars *ivars);
160 void	nvmf_disconnect(struct nvmf_softc *sc);
161 void	nvmf_rescan_ns(struct nvmf_softc *sc, uint32_t nsid);
162 void	nvmf_rescan_all_ns(struct nvmf_softc *sc);
163 int	nvmf_passthrough_cmd(struct nvmf_softc *sc, struct nvme_pt_command *pt,
164     bool admin);
165 
166 /* nvmf_aer.c */
167 void	nvmf_init_aer(struct nvmf_softc *sc);
168 int	nvmf_start_aer(struct nvmf_softc *sc);
169 void	nvmf_destroy_aer(struct nvmf_softc *sc);
170 
171 /* nvmf_cmd.c */
172 bool	nvmf_cmd_get_property(struct nvmf_softc *sc, uint32_t offset,
173     uint8_t size, nvmf_request_complete_t *cb, void *cb_arg, int how);
174 bool	nvmf_cmd_set_property(struct nvmf_softc *sc, uint32_t offset,
175     uint8_t size, uint64_t value, nvmf_request_complete_t *cb, void *cb_arg,
176     int how);
177 bool	nvmf_cmd_keep_alive(struct nvmf_softc *sc, nvmf_request_complete_t *cb,
178     void *cb_arg, int how);
179 bool	nvmf_cmd_identify_active_namespaces(struct nvmf_softc *sc, uint32_t id,
180     struct nvme_ns_list *nslist, nvmf_request_complete_t *req_cb,
181     void *req_cb_arg, nvmf_io_complete_t *io_cb, void *io_cb_arg, int how);
182 bool	nvmf_cmd_identify_namespace(struct nvmf_softc *sc, uint32_t id,
183     struct nvme_namespace_data *nsdata, nvmf_request_complete_t *req_cb,
184     void *req_cb_arg, nvmf_io_complete_t *io_cb, void *io_cb_arg, int how);
185 bool	nvmf_cmd_get_log_page(struct nvmf_softc *sc, uint32_t nsid, uint8_t lid,
186     uint64_t offset, void *buf, size_t len, nvmf_request_complete_t *req_cb,
187     void *req_cb_arg, nvmf_io_complete_t *io_cb, void *io_cb_arg, int how);
188 
189 /* nvmf_ctldev.c */
190 int	nvmf_ctl_load(void);
191 void	nvmf_ctl_unload(void);
192 
193 /* nvmf_ns.c */
194 struct nvmf_namespace *nvmf_init_ns(struct nvmf_softc *sc, uint32_t id,
195     const struct nvme_namespace_data *data);
196 void	nvmf_disconnect_ns(struct nvmf_namespace *ns);
197 void	nvmf_reconnect_ns(struct nvmf_namespace *ns);
198 void	nvmf_shutdown_ns(struct nvmf_namespace *ns);
199 void	nvmf_destroy_ns(struct nvmf_namespace *ns);
200 bool	nvmf_update_ns(struct nvmf_namespace *ns,
201     const struct nvme_namespace_data *data);
202 
203 /* nvmf_qpair.c */
204 struct nvmf_host_qpair *nvmf_init_qp(struct nvmf_softc *sc,
205     enum nvmf_trtype trtype, struct nvmf_handoff_qpair_params *handoff,
206     const char *name, u_int qid);
207 void	nvmf_shutdown_qp(struct nvmf_host_qpair *qp);
208 void	nvmf_destroy_qp(struct nvmf_host_qpair *qp);
209 struct nvmf_request *nvmf_allocate_request(struct nvmf_host_qpair *qp,
210     void *sqe, nvmf_request_complete_t *cb, void *cb_arg, int how);
211 void	nvmf_submit_request(struct nvmf_request *req);
212 void	nvmf_free_request(struct nvmf_request *req);
213 
214 /* nvmf_sim.c */
215 int	nvmf_init_sim(struct nvmf_softc *sc);
216 void	nvmf_disconnect_sim(struct nvmf_softc *sc);
217 void	nvmf_reconnect_sim(struct nvmf_softc *sc);
218 void	nvmf_shutdown_sim(struct nvmf_softc *sc);
219 void	nvmf_destroy_sim(struct nvmf_softc *sc);
220 void	nvmf_sim_rescan_ns(struct nvmf_softc *sc, uint32_t id);
221 
222 #endif /* !__NVMF_VAR_H__ */
223