xref: /spdk/lib/nvmf/nvmf_internal.h (revision 06b537bfdb4393dea857e204b85d8df46a351d8a)
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 	SPDK_NVMF_SUBSYSTEM_NUM_STATES,
61 };
62 
63 struct spdk_nvmf_tgt {
64 	char					name[NVMF_TGT_NAME_MAX_LENGTH];
65 
66 	pthread_mutex_t				mutex;
67 
68 	uint64_t				discovery_genctr;
69 
70 	struct spdk_poller			*accept_poller;
71 
72 	uint32_t				max_subsystems;
73 
74 	/* Array of subsystem pointers of size max_subsystems indexed by sid */
75 	struct spdk_nvmf_subsystem		**subsystems;
76 
77 	TAILQ_HEAD(, spdk_nvmf_transport)	transports;
78 	TAILQ_HEAD(, spdk_nvmf_poll_group)	poll_groups;
79 
80 	/* Used for round-robin assignment of connections to poll groups */
81 	struct spdk_nvmf_poll_group		*next_poll_group;
82 
83 	spdk_nvmf_tgt_destroy_done_fn		*destroy_cb_fn;
84 	void					*destroy_cb_arg;
85 
86 	TAILQ_ENTRY(spdk_nvmf_tgt)		link;
87 };
88 
89 struct spdk_nvmf_host {
90 	char				nqn[SPDK_NVMF_NQN_MAX_LEN + 1];
91 	TAILQ_ENTRY(spdk_nvmf_host)	link;
92 };
93 
94 struct spdk_nvmf_subsystem_listener {
95 	struct spdk_nvmf_subsystem			*subsystem;
96 	spdk_nvmf_tgt_subsystem_listen_done_fn		cb_fn;
97 	void						*cb_arg;
98 	struct spdk_nvme_transport_id			*trid;
99 	struct spdk_nvmf_transport			*transport;
100 	enum spdk_nvme_ana_state			ana_state;
101 	uint64_t					ana_state_change_count;
102 	TAILQ_ENTRY(spdk_nvmf_subsystem_listener)	link;
103 };
104 
105 /* Maximum number of registrants supported per namespace */
106 #define SPDK_NVMF_MAX_NUM_REGISTRANTS		16
107 
108 struct spdk_nvmf_registrant_info {
109 	uint64_t		rkey;
110 	char			host_uuid[SPDK_UUID_STRING_LEN];
111 };
112 
113 struct spdk_nvmf_reservation_info {
114 	bool					ptpl_activated;
115 	enum spdk_nvme_reservation_type		rtype;
116 	uint64_t				crkey;
117 	char					bdev_uuid[SPDK_UUID_STRING_LEN];
118 	char					holder_uuid[SPDK_UUID_STRING_LEN];
119 	uint32_t				num_regs;
120 	struct spdk_nvmf_registrant_info	registrants[SPDK_NVMF_MAX_NUM_REGISTRANTS];
121 };
122 
123 struct spdk_nvmf_subsystem_pg_ns_info {
124 	struct spdk_io_channel		*channel;
125 	struct spdk_uuid		uuid;
126 	/* current reservation key, no reservation if the value is 0 */
127 	uint64_t			crkey;
128 	/* reservation type */
129 	enum spdk_nvme_reservation_type	rtype;
130 	/* Host ID which holds the reservation */
131 	struct spdk_uuid		holder_id;
132 	/* Host ID for the registrants with the namespace */
133 	struct spdk_uuid		reg_hostid[SPDK_NVMF_MAX_NUM_REGISTRANTS];
134 	uint64_t			num_blocks;
135 };
136 
137 typedef void(*spdk_nvmf_poll_group_mod_done)(void *cb_arg, int status);
138 
139 struct spdk_nvmf_subsystem_poll_group {
140 	/* Array of namespace information for each namespace indexed by nsid - 1 */
141 	struct spdk_nvmf_subsystem_pg_ns_info	*ns_info;
142 	uint32_t				num_ns;
143 
144 	uint64_t				io_outstanding;
145 	spdk_nvmf_poll_group_mod_done		cb_fn;
146 	void					*cb_arg;
147 
148 	enum spdk_nvmf_subsystem_state		state;
149 
150 	TAILQ_HEAD(, spdk_nvmf_request)		queued;
151 };
152 
153 struct spdk_nvmf_registrant {
154 	TAILQ_ENTRY(spdk_nvmf_registrant) link;
155 	struct spdk_uuid hostid;
156 	/* Registration key */
157 	uint64_t rkey;
158 };
159 
160 struct spdk_nvmf_ns {
161 	uint32_t nsid;
162 	struct spdk_nvmf_subsystem *subsystem;
163 	struct spdk_bdev *bdev;
164 	struct spdk_bdev_desc *desc;
165 	struct spdk_nvmf_ns_opts opts;
166 	/* reservation notificaton mask */
167 	uint32_t mask;
168 	/* generation code */
169 	uint32_t gen;
170 	/* registrants head */
171 	TAILQ_HEAD(, spdk_nvmf_registrant) registrants;
172 	/* current reservation key */
173 	uint64_t crkey;
174 	/* reservation type */
175 	enum spdk_nvme_reservation_type rtype;
176 	/* current reservation holder, only valid if reservation type can only have one holder */
177 	struct spdk_nvmf_registrant *holder;
178 	/* Persist Through Power Loss file which contains the persistent reservation */
179 	char *ptpl_file;
180 	/* Persist Through Power Loss feature is enabled */
181 	bool ptpl_activated;
182 };
183 
184 struct spdk_nvmf_ctrlr_feat {
185 	union spdk_nvme_feat_arbitration arbitration;
186 	union spdk_nvme_feat_power_management power_management;
187 	union spdk_nvme_feat_error_recovery error_recovery;
188 	union spdk_nvme_feat_volatile_write_cache volatile_write_cache;
189 	union spdk_nvme_feat_number_of_queues number_of_queues;
190 	union spdk_nvme_feat_write_atomicity write_atomicity;
191 	union spdk_nvme_feat_async_event_configuration async_event_configuration;
192 	union spdk_nvme_feat_keep_alive_timer keep_alive_timer;
193 };
194 
195 /*
196  * NVMf reservation notificaton log page.
197  */
198 struct spdk_nvmf_reservation_log {
199 	struct spdk_nvme_reservation_notification_log	log;
200 	TAILQ_ENTRY(spdk_nvmf_reservation_log)		link;
201 	struct spdk_nvmf_ctrlr				*ctrlr;
202 };
203 
204 /*
205  * This structure represents an NVMe-oF controller,
206  * which is like a "session" in networking terms.
207  */
208 struct spdk_nvmf_ctrlr {
209 	uint16_t			cntlid;
210 	char				hostnqn[SPDK_NVMF_NQN_MAX_LEN + 1];
211 	struct spdk_nvmf_subsystem	*subsys;
212 
213 	struct spdk_nvmf_ctrlr_data	cdata;
214 
215 	struct spdk_nvmf_registers	vcprop;
216 
217 	struct spdk_nvmf_ctrlr_feat feat;
218 
219 	struct spdk_nvmf_qpair	*admin_qpair;
220 	struct spdk_thread	*thread;
221 	struct spdk_bit_array	*qpair_mask;
222 
223 	const struct spdk_nvmf_subsystem_listener	*listener;
224 
225 	struct spdk_nvmf_request *aer_req[NVMF_MAX_ASYNC_EVENTS];
226 	union spdk_nvme_async_event_completion notice_event;
227 	union spdk_nvme_async_event_completion reservation_event;
228 	uint8_t nr_aer_reqs;
229 	struct spdk_uuid  hostid;
230 
231 	uint16_t changed_ns_list_count;
232 	struct spdk_nvme_ns_list changed_ns_list;
233 	uint64_t log_page_count;
234 	uint8_t num_avail_log_pages;
235 	TAILQ_HEAD(log_page_head, spdk_nvmf_reservation_log) log_head;
236 
237 	/* Time to trigger keep-alive--poller_time = now_tick + period */
238 	uint64_t			last_keep_alive_tick;
239 	struct spdk_poller		*keep_alive_poller;
240 
241 	struct spdk_poller		*association_timer;
242 
243 	bool				dif_insert_or_strip;
244 	bool				in_destruct;
245 
246 	TAILQ_ENTRY(spdk_nvmf_ctrlr)	link;
247 };
248 
249 struct spdk_nvmf_subsystem {
250 	struct spdk_thread				*thread;
251 
252 	uint32_t					id;
253 
254 	enum spdk_nvmf_subsystem_state			state;
255 	enum spdk_nvmf_subtype				subtype;
256 
257 	uint16_t					next_cntlid;
258 	struct {
259 		uint8_t					allow_any_host : 1;
260 		uint8_t					allow_any_listener : 1;
261 		uint8_t					ana_reporting : 1;
262 		uint8_t					reserved : 5;
263 	} flags;
264 
265 	/* boolean for state change synchronization */
266 	bool						changing_state;
267 
268 	struct spdk_nvmf_tgt				*tgt;
269 
270 	/* Array of pointers to namespaces of size max_nsid indexed by nsid - 1 */
271 	struct spdk_nvmf_ns				**ns;
272 	uint32_t					max_nsid;
273 	/* This is the maximum allowed nsid to a subsystem */
274 	uint32_t					max_allowed_nsid;
275 
276 	TAILQ_HEAD(, spdk_nvmf_ctrlr)			ctrlrs;
277 	TAILQ_HEAD(, spdk_nvmf_host)			hosts;
278 	TAILQ_HEAD(, spdk_nvmf_subsystem_listener)	listeners;
279 
280 	TAILQ_ENTRY(spdk_nvmf_subsystem)		entries;
281 
282 	char						sn[SPDK_NVME_CTRLR_SN_LEN + 1];
283 	char						mn[SPDK_NVME_CTRLR_MN_LEN + 1];
284 	char						subnqn[SPDK_NVMF_NQN_MAX_LEN + 1];
285 };
286 
287 int nvmf_poll_group_add_transport(struct spdk_nvmf_poll_group *group,
288 				  struct spdk_nvmf_transport *transport);
289 int nvmf_poll_group_update_subsystem(struct spdk_nvmf_poll_group *group,
290 				     struct spdk_nvmf_subsystem *subsystem);
291 int nvmf_poll_group_add_subsystem(struct spdk_nvmf_poll_group *group,
292 				  struct spdk_nvmf_subsystem *subsystem,
293 				  spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg);
294 void nvmf_poll_group_remove_subsystem(struct spdk_nvmf_poll_group *group,
295 				      struct spdk_nvmf_subsystem *subsystem, spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg);
296 void nvmf_poll_group_pause_subsystem(struct spdk_nvmf_poll_group *group,
297 				     struct spdk_nvmf_subsystem *subsystem, spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg);
298 void nvmf_poll_group_resume_subsystem(struct spdk_nvmf_poll_group *group,
299 				      struct spdk_nvmf_subsystem *subsystem, spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg);
300 
301 void nvmf_get_discovery_log_page(struct spdk_nvmf_tgt *tgt, const char *hostnqn,
302 				 struct iovec *iov,
303 				 uint32_t iovcnt, uint64_t offset, uint32_t length);
304 
305 void nvmf_ctrlr_destruct(struct spdk_nvmf_ctrlr *ctrlr);
306 int nvmf_ctrlr_process_admin_cmd(struct spdk_nvmf_request *req);
307 int nvmf_ctrlr_process_io_cmd(struct spdk_nvmf_request *req);
308 bool nvmf_ctrlr_dsm_supported(struct spdk_nvmf_ctrlr *ctrlr);
309 bool nvmf_ctrlr_write_zeroes_supported(struct spdk_nvmf_ctrlr *ctrlr);
310 void nvmf_ctrlr_ns_changed(struct spdk_nvmf_ctrlr *ctrlr, uint32_t nsid);
311 
312 void nvmf_bdev_ctrlr_identify_ns(struct spdk_nvmf_ns *ns, struct spdk_nvme_ns_data *nsdata,
313 				 bool dif_insert_or_strip);
314 int nvmf_bdev_ctrlr_read_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
315 			     struct spdk_io_channel *ch, struct spdk_nvmf_request *req);
316 int nvmf_bdev_ctrlr_write_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
317 			      struct spdk_io_channel *ch, struct spdk_nvmf_request *req);
318 int nvmf_bdev_ctrlr_compare_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
319 				struct spdk_io_channel *ch, struct spdk_nvmf_request *req);
320 int nvmf_bdev_ctrlr_compare_and_write_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
321 		struct spdk_io_channel *ch, struct spdk_nvmf_request *cmp_req, struct spdk_nvmf_request *write_req);
322 int nvmf_bdev_ctrlr_write_zeroes_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
323 				     struct spdk_io_channel *ch, struct spdk_nvmf_request *req);
324 int nvmf_bdev_ctrlr_flush_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
325 			      struct spdk_io_channel *ch, struct spdk_nvmf_request *req);
326 int nvmf_bdev_ctrlr_dsm_cmd(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
327 			    struct spdk_io_channel *ch, struct spdk_nvmf_request *req);
328 int nvmf_bdev_ctrlr_nvme_passthru_io(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
329 				     struct spdk_io_channel *ch, struct spdk_nvmf_request *req);
330 bool nvmf_bdev_ctrlr_get_dif_ctx(struct spdk_bdev *bdev, struct spdk_nvme_cmd *cmd,
331 				 struct spdk_dif_ctx *dif_ctx);
332 
333 int nvmf_subsystem_add_ctrlr(struct spdk_nvmf_subsystem *subsystem,
334 			     struct spdk_nvmf_ctrlr *ctrlr);
335 void nvmf_subsystem_remove_ctrlr(struct spdk_nvmf_subsystem *subsystem,
336 				 struct spdk_nvmf_ctrlr *ctrlr);
337 void nvmf_subsystem_remove_all_listeners(struct spdk_nvmf_subsystem *subsystem,
338 		bool stop);
339 struct spdk_nvmf_ctrlr *nvmf_subsystem_get_ctrlr(struct spdk_nvmf_subsystem *subsystem,
340 		uint16_t cntlid);
341 struct spdk_nvmf_subsystem_listener *nvmf_subsystem_find_listener(
342 	struct spdk_nvmf_subsystem *subsystem,
343 	const struct spdk_nvme_transport_id *trid);
344 struct spdk_nvmf_listener *nvmf_transport_find_listener(
345 	struct spdk_nvmf_transport *transport,
346 	const struct spdk_nvme_transport_id *trid);
347 void nvmf_subsystem_set_ana_state(struct spdk_nvmf_subsystem *subsystem,
348 				  const struct spdk_nvme_transport_id *trid,
349 				  enum spdk_nvme_ana_state ana_state,
350 				  spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn, void *cb_arg);
351 
352 int nvmf_ctrlr_async_event_ns_notice(struct spdk_nvmf_ctrlr *ctrlr);
353 int nvmf_ctrlr_async_event_ana_change_notice(struct spdk_nvmf_ctrlr *ctrlr);
354 void nvmf_ctrlr_async_event_reservation_notification(struct spdk_nvmf_ctrlr *ctrlr);
355 void nvmf_ns_reservation_request(void *ctx);
356 void nvmf_ctrlr_reservation_notice_log(struct spdk_nvmf_ctrlr *ctrlr,
357 				       struct spdk_nvmf_ns *ns,
358 				       enum spdk_nvme_reservation_notification_log_page_type type);
359 
360 /*
361  * Abort aer is sent on a per controller basis and sends a completion for the aer to the host.
362  * This function should be called when attempting to recover in error paths when it is OK for
363  * the host to send a subsequent AER.
364  */
365 void nvmf_ctrlr_abort_aer(struct spdk_nvmf_ctrlr *ctrlr);
366 
367 /*
368  * Free aer simply frees the rdma resources for the aer without informing the host.
369  * This function should be called when deleting a qpair when one wants to make sure
370  * the qpair is completely empty before freeing the request. The reason we free the
371  * AER without sending a completion is to prevent the host from sending another AER.
372  */
373 void nvmf_qpair_free_aer(struct spdk_nvmf_qpair *qpair);
374 
375 int nvmf_ctrlr_abort_request(struct spdk_nvmf_request *req);
376 
377 static inline struct spdk_nvmf_ns *
378 _nvmf_subsystem_get_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid)
379 {
380 	/* NOTE: This implicitly also checks for 0, since 0 - 1 wraps around to UINT32_MAX. */
381 	if (spdk_unlikely(nsid - 1 >= subsystem->max_nsid)) {
382 		return NULL;
383 	}
384 
385 	return subsystem->ns[nsid - 1];
386 }
387 
388 static inline bool
389 nvmf_qpair_is_admin_queue(struct spdk_nvmf_qpair *qpair)
390 {
391 	return qpair->qid == 0;
392 }
393 
394 #endif /* __NVMF_INTERNAL_H__ */
395