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