xref: /spdk/lib/nvmf/nvmf_internal.h (revision 7192849ed24874f3e9cc31e8a33a9b32c49b9506)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation. All rights reserved.
5  *   Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef __NVMF_INTERNAL_H__
35 #define __NVMF_INTERNAL_H__
36 
37 #include "spdk/stdinc.h"
38 
39 #include "spdk/likely.h"
40 #include "spdk/nvmf.h"
41 #include "spdk/nvmf_cmd.h"
42 #include "spdk/nvmf_transport.h"
43 #include "spdk/nvmf_spec.h"
44 #include "spdk/assert.h"
45 #include "spdk/bdev.h"
46 #include "spdk/queue.h"
47 #include "spdk/util.h"
48 #include "spdk/thread.h"
49 
50 #define NVMF_MAX_ASYNC_EVENTS	(4)
51 
52 enum spdk_nvmf_subsystem_state {
53 	SPDK_NVMF_SUBSYSTEM_INACTIVE = 0,
54 	SPDK_NVMF_SUBSYSTEM_ACTIVATING,
55 	SPDK_NVMF_SUBSYSTEM_ACTIVE,
56 	SPDK_NVMF_SUBSYSTEM_PAUSING,
57 	SPDK_NVMF_SUBSYSTEM_PAUSED,
58 	SPDK_NVMF_SUBSYSTEM_RESUMING,
59 	SPDK_NVMF_SUBSYSTEM_DEACTIVATING,
60 };
61 
62 struct spdk_nvmf_tgt {
63 	char					name[NVMF_TGT_NAME_MAX_LENGTH];
64 
65 	pthread_mutex_t				mutex;
66 
67 	uint64_t				discovery_genctr;
68 
69 	uint32_t				max_subsystems;
70 
71 	/* Array of subsystem pointers of size max_subsystems indexed by sid */
72 	struct spdk_nvmf_subsystem		**subsystems;
73 
74 	TAILQ_HEAD(, spdk_nvmf_transport)	transports;
75 	TAILQ_HEAD(, spdk_nvmf_poll_group)	poll_groups;
76 
77 	/* Used for round-robin assignment of connections to poll groups */
78 	struct spdk_nvmf_poll_group		*next_poll_group;
79 
80 	spdk_nvmf_tgt_destroy_done_fn		*destroy_cb_fn;
81 	void					*destroy_cb_arg;
82 
83 	TAILQ_ENTRY(spdk_nvmf_tgt)		link;
84 };
85 
86 struct spdk_nvmf_host {
87 	char				nqn[SPDK_NVMF_NQN_MAX_LEN + 1];
88 	TAILQ_ENTRY(spdk_nvmf_host)	link;
89 };
90 
91 struct spdk_nvmf_subsystem_listener {
92 	struct spdk_nvmf_subsystem			*subsystem;
93 	spdk_nvmf_tgt_subsystem_listen_done_fn		cb_fn;
94 	void						*cb_arg;
95 	struct spdk_nvme_transport_id			*trid;
96 	struct spdk_nvmf_transport			*transport;
97 	TAILQ_ENTRY(spdk_nvmf_subsystem_listener)	link;
98 };
99 
100 /* Maximum number of registrants supported per namespace */
101 #define SPDK_NVMF_MAX_NUM_REGISTRANTS		16
102 
103 struct spdk_nvmf_registrant_info {
104 	uint64_t		rkey;
105 	char			host_uuid[SPDK_UUID_STRING_LEN];
106 };
107 
108 struct spdk_nvmf_reservation_info {
109 	bool					ptpl_activated;
110 	enum spdk_nvme_reservation_type		rtype;
111 	uint64_t				crkey;
112 	char					bdev_uuid[SPDK_UUID_STRING_LEN];
113 	char					holder_uuid[SPDK_UUID_STRING_LEN];
114 	uint32_t				num_regs;
115 	struct spdk_nvmf_registrant_info	registrants[SPDK_NVMF_MAX_NUM_REGISTRANTS];
116 };
117 
118 struct spdk_nvmf_subsystem_pg_ns_info {
119 	struct spdk_io_channel		*channel;
120 	struct spdk_uuid		uuid;
121 	/* current reservation key, no reservation if the value is 0 */
122 	uint64_t			crkey;
123 	/* reservation type */
124 	enum spdk_nvme_reservation_type	rtype;
125 	/* Host ID which holds the reservation */
126 	struct spdk_uuid		holder_id;
127 	/* Host ID for the registrants with the namespace */
128 	struct spdk_uuid		reg_hostid[SPDK_NVMF_MAX_NUM_REGISTRANTS];
129 	uint64_t			num_blocks;
130 };
131 
132 typedef void(*spdk_nvmf_poll_group_mod_done)(void *cb_arg, int status);
133 
134 struct spdk_nvmf_subsystem_poll_group {
135 	/* Array of namespace information for each namespace indexed by nsid - 1 */
136 	struct spdk_nvmf_subsystem_pg_ns_info	*ns_info;
137 	uint32_t				num_ns;
138 
139 	uint64_t				io_outstanding;
140 	spdk_nvmf_poll_group_mod_done		cb_fn;
141 	void					*cb_arg;
142 
143 	enum spdk_nvmf_subsystem_state		state;
144 
145 	TAILQ_HEAD(, spdk_nvmf_request)		queued;
146 };
147 
148 struct spdk_nvmf_registrant {
149 	TAILQ_ENTRY(spdk_nvmf_registrant) link;
150 	struct spdk_uuid hostid;
151 	/* Registration key */
152 	uint64_t rkey;
153 };
154 
155 struct spdk_nvmf_ns {
156 	uint32_t nsid;
157 	struct spdk_nvmf_subsystem *subsystem;
158 	struct spdk_bdev *bdev;
159 	struct spdk_bdev_desc *desc;
160 	struct spdk_nvmf_ns_opts opts;
161 	/* reservation notificaton mask */
162 	uint32_t mask;
163 	/* generation code */
164 	uint32_t gen;
165 	/* registrants head */
166 	TAILQ_HEAD(, spdk_nvmf_registrant) registrants;
167 	/* current reservation key */
168 	uint64_t crkey;
169 	/* reservation type */
170 	enum spdk_nvme_reservation_type rtype;
171 	/* current reservation holder, only valid if reservation type can only have one holder */
172 	struct spdk_nvmf_registrant *holder;
173 	/* Persist Through Power Loss file which contains the persistent reservation */
174 	char *ptpl_file;
175 	/* Persist Through Power Loss feature is enabled */
176 	bool ptpl_activated;
177 };
178 
179 struct spdk_nvmf_ctrlr_feat {
180 	union spdk_nvme_feat_arbitration arbitration;
181 	union spdk_nvme_feat_power_management power_management;
182 	union spdk_nvme_feat_error_recovery error_recovery;
183 	union spdk_nvme_feat_volatile_write_cache volatile_write_cache;
184 	union spdk_nvme_feat_number_of_queues number_of_queues;
185 	union spdk_nvme_feat_write_atomicity write_atomicity;
186 	union spdk_nvme_feat_async_event_configuration async_event_configuration;
187 	union spdk_nvme_feat_keep_alive_timer keep_alive_timer;
188 };
189 
190 /*
191  * NVMf reservation notificaton log page.
192  */
193 struct spdk_nvmf_reservation_log {
194 	struct spdk_nvme_reservation_notification_log	log;
195 	TAILQ_ENTRY(spdk_nvmf_reservation_log)		link;
196 	struct spdk_nvmf_ctrlr				*ctrlr;
197 };
198 
199 /*
200  * This structure represents an NVMe-oF controller,
201  * which is like a "session" in networking terms.
202  */
203 struct spdk_nvmf_ctrlr {
204 	uint16_t			cntlid;
205 	char				hostnqn[SPDK_NVMF_NQN_MAX_LEN + 1];
206 	struct spdk_nvmf_subsystem	*subsys;
207 
208 	struct spdk_nvmf_ctrlr_data	cdata;
209 
210 	struct spdk_nvmf_registers	vcprop;
211 
212 	struct spdk_nvmf_ctrlr_feat feat;
213 
214 	struct spdk_nvmf_qpair	*admin_qpair;
215 	struct spdk_thread	*thread;
216 	struct spdk_bit_array	*qpair_mask;
217 
218 	struct spdk_nvmf_request *aer_req[NVMF_MAX_ASYNC_EVENTS];
219 	union spdk_nvme_async_event_completion notice_event;
220 	union spdk_nvme_async_event_completion reservation_event;
221 	uint8_t nr_aer_reqs;
222 	struct spdk_uuid  hostid;
223 
224 	uint16_t changed_ns_list_count;
225 	struct spdk_nvme_ns_list changed_ns_list;
226 	uint64_t log_page_count;
227 	uint8_t num_avail_log_pages;
228 	TAILQ_HEAD(log_page_head, spdk_nvmf_reservation_log) log_head;
229 
230 	/* Time to trigger keep-alive--poller_time = now_tick + period */
231 	uint64_t			last_keep_alive_tick;
232 	struct spdk_poller		*keep_alive_poller;
233 
234 	bool				dif_insert_or_strip;
235 
236 	TAILQ_ENTRY(spdk_nvmf_ctrlr)	link;
237 };
238 
239 struct spdk_nvmf_subsystem {
240 	struct spdk_thread		*thread;
241 	uint32_t			id;
242 	enum spdk_nvmf_subsystem_state	state;
243 
244 	char subnqn[SPDK_NVMF_NQN_MAX_LEN + 1];
245 	enum spdk_nvmf_subtype subtype;
246 	uint16_t next_cntlid;
247 	bool allow_any_host;
248 	bool allow_any_listener;
249 
250 	struct spdk_nvmf_tgt			*tgt;
251 
252 	char sn[SPDK_NVME_CTRLR_SN_LEN + 1];
253 	char mn[SPDK_NVME_CTRLR_MN_LEN + 1];
254 
255 	/* Array of pointers to namespaces of size max_nsid indexed by nsid - 1 */
256 	struct spdk_nvmf_ns			**ns;
257 	uint32_t				max_nsid;
258 	/* This is the maximum allowed nsid to a subsystem */
259 	uint32_t				max_allowed_nsid;
260 
261 	TAILQ_HEAD(, spdk_nvmf_ctrlr)			ctrlrs;
262 	TAILQ_HEAD(, spdk_nvmf_host)			hosts;
263 	TAILQ_HEAD(, spdk_nvmf_subsystem_listener)	listeners;
264 
265 	TAILQ_ENTRY(spdk_nvmf_subsystem)	entries;
266 };
267 
268 int nvmf_poll_group_add_transport(struct spdk_nvmf_poll_group *group,
269 				  struct spdk_nvmf_transport *transport);
270 int nvmf_poll_group_update_subsystem(struct spdk_nvmf_poll_group *group,
271 				     struct spdk_nvmf_subsystem *subsystem);
272 int nvmf_poll_group_add_subsystem(struct spdk_nvmf_poll_group *group,
273 				  struct spdk_nvmf_subsystem *subsystem,
274 				  spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg);
275 void nvmf_poll_group_remove_subsystem(struct spdk_nvmf_poll_group *group,
276 				      struct spdk_nvmf_subsystem *subsystem, spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg);
277 void nvmf_poll_group_pause_subsystem(struct spdk_nvmf_poll_group *group,
278 				     struct spdk_nvmf_subsystem *subsystem, spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg);
279 void nvmf_poll_group_resume_subsystem(struct spdk_nvmf_poll_group *group,
280 				      struct spdk_nvmf_subsystem *subsystem, spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg);
281 
282 void nvmf_get_discovery_log_page(struct spdk_nvmf_tgt *tgt, const char *hostnqn,
283 				 struct iovec *iov,
284 				 uint32_t iovcnt, uint64_t offset, uint32_t length);
285 
286 void nvmf_ctrlr_destruct(struct spdk_nvmf_ctrlr *ctrlr);
287 int nvmf_ctrlr_process_fabrics_cmd(struct spdk_nvmf_request *req);
288 int nvmf_ctrlr_process_admin_cmd(struct spdk_nvmf_request *req);
289 int nvmf_ctrlr_process_io_cmd(struct spdk_nvmf_request *req);
290 bool nvmf_ctrlr_dsm_supported(struct spdk_nvmf_ctrlr *ctrlr);
291 bool nvmf_ctrlr_write_zeroes_supported(struct spdk_nvmf_ctrlr *ctrlr);
292 void nvmf_ctrlr_ns_changed(struct spdk_nvmf_ctrlr *ctrlr, uint32_t nsid);
293 
294 void nvmf_bdev_ctrlr_identify_ns(struct spdk_nvmf_ns *ns, struct spdk_nvme_ns_data *nsdata,
295 				 bool dif_insert_or_strip);
296 int nvmf_bdev_ctrlr_read_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
297 			     struct spdk_io_channel *ch, struct spdk_nvmf_request *req);
298 int nvmf_bdev_ctrlr_write_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
299 			      struct spdk_io_channel *ch, struct spdk_nvmf_request *req);
300 int nvmf_bdev_ctrlr_compare_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
301 				struct spdk_io_channel *ch, struct spdk_nvmf_request *req);
302 int nvmf_bdev_ctrlr_compare_and_write_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
303 		struct spdk_io_channel *ch, struct spdk_nvmf_request *cmp_req, struct spdk_nvmf_request *write_req);
304 int nvmf_bdev_ctrlr_write_zeroes_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
305 				     struct spdk_io_channel *ch, struct spdk_nvmf_request *req);
306 int nvmf_bdev_ctrlr_flush_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
307 			      struct spdk_io_channel *ch, struct spdk_nvmf_request *req);
308 int nvmf_bdev_ctrlr_dsm_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
309 			    struct spdk_io_channel *ch, struct spdk_nvmf_request *req);
310 int nvmf_bdev_ctrlr_nvme_passthru_io(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
311 				     struct spdk_io_channel *ch, struct spdk_nvmf_request *req);
312 int nvmf_bdev_ctrlr_abort_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
313 			      struct spdk_io_channel *ch, struct spdk_nvmf_request *req,
314 			      struct spdk_nvmf_request *req_to_abort);
315 bool nvmf_bdev_ctrlr_get_dif_ctx(struct spdk_bdev *bdev, struct spdk_nvme_cmd *cmd,
316 				 struct spdk_dif_ctx *dif_ctx);
317 
318 int nvmf_subsystem_add_ctrlr(struct spdk_nvmf_subsystem *subsystem,
319 			     struct spdk_nvmf_ctrlr *ctrlr);
320 void nvmf_subsystem_remove_ctrlr(struct spdk_nvmf_subsystem *subsystem,
321 				 struct spdk_nvmf_ctrlr *ctrlr);
322 void nvmf_subsystem_remove_all_listeners(struct spdk_nvmf_subsystem *subsystem,
323 		bool stop);
324 struct spdk_nvmf_ctrlr *nvmf_subsystem_get_ctrlr(struct spdk_nvmf_subsystem *subsystem,
325 		uint16_t cntlid);
326 struct spdk_nvmf_subsystem_listener *nvmf_subsystem_find_listener(
327 	struct spdk_nvmf_subsystem *subsystem,
328 	const struct spdk_nvme_transport_id *trid);
329 struct spdk_nvmf_listener *nvmf_transport_find_listener(
330 	struct spdk_nvmf_transport *transport,
331 	const struct spdk_nvme_transport_id *trid);
332 
333 int nvmf_ctrlr_async_event_ns_notice(struct spdk_nvmf_ctrlr *ctrlr);
334 void nvmf_ctrlr_async_event_reservation_notification(struct spdk_nvmf_ctrlr *ctrlr);
335 void nvmf_ns_reservation_request(void *ctx);
336 void nvmf_ctrlr_reservation_notice_log(struct spdk_nvmf_ctrlr *ctrlr,
337 				       struct spdk_nvmf_ns *ns,
338 				       enum spdk_nvme_reservation_notification_log_page_type type);
339 
340 /*
341  * Abort aer is sent on a per controller basis and sends a completion for the aer to the host.
342  * This function should be called when attempting to recover in error paths when it is OK for
343  * the host to send a subsequent AER.
344  */
345 void nvmf_ctrlr_abort_aer(struct spdk_nvmf_ctrlr *ctrlr);
346 
347 /*
348  * Free aer simply frees the rdma resources for the aer without informing the host.
349  * This function should be called when deleting a qpair when one wants to make sure
350  * the qpair is completely empty before freeing the request. The reason we free the
351  * AER without sending a completion is to prevent the host from sending another AER.
352  */
353 void nvmf_qpair_free_aer(struct spdk_nvmf_qpair *qpair);
354 
355 int nvmf_ctrlr_abort_request(struct spdk_nvmf_request *req,
356 			     struct spdk_nvmf_request *req_to_abort);
357 
358 static inline struct spdk_nvmf_ns *
359 _nvmf_subsystem_get_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
360 {
361 	/* NOTE: This implicitly also checks for 0, since 0 - 1 wraps around to UINT32_MAX. */
362 	if (spdk_unlikely(nsid - 1 >= subsystem->max_nsid)) {
363 		return NULL;
364 	}
365 
366 	return subsystem->ns[nsid - 1];
367 }
368 
369 static inline bool
370 nvmf_qpair_is_admin_queue(struct spdk_nvmf_qpair *qpair)
371 {
372 	return qpair->qid == 0;
373 }
374 
375 #endif /* __NVMF_INTERNAL_H__ */
376