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