xref: /spdk/module/bdev/nvme/bdev_nvme.h (revision 12fbe739a31b09aff0d05f354d4f3bbef99afc55)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2016 Intel Corporation. All rights reserved.
3  *   Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved.
4  *   Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5  *   Copyright (c) 2022 Dell Inc, or its subsidiaries. All rights reserved.
6  */
7 
8 #ifndef SPDK_BDEV_NVME_H
9 #define SPDK_BDEV_NVME_H
10 
11 #include "spdk/stdinc.h"
12 
13 #include "spdk/queue.h"
14 #include "spdk/nvme.h"
15 #include "spdk/bdev_module.h"
16 #include "spdk/jsonrpc.h"
17 
18 TAILQ_HEAD(nvme_bdev_ctrlrs, nvme_bdev_ctrlr);
19 extern struct nvme_bdev_ctrlrs g_nvme_bdev_ctrlrs;
20 extern pthread_mutex_t g_bdev_nvme_mutex;
21 extern bool g_bdev_nvme_module_finish;
22 extern struct spdk_thread *g_bdev_nvme_init_thread;
23 
24 #define NVME_MAX_CONTROLLERS 1024
25 
26 enum bdev_nvme_multipath_policy {
27 	BDEV_NVME_MP_POLICY_ACTIVE_PASSIVE,
28 	BDEV_NVME_MP_POLICY_ACTIVE_ACTIVE,
29 };
30 
31 enum bdev_nvme_multipath_selector {
32 	BDEV_NVME_MP_SELECTOR_ROUND_ROBIN = 1,
33 	BDEV_NVME_MP_SELECTOR_QUEUE_DEPTH,
34 };
35 
36 typedef void (*spdk_bdev_create_nvme_fn)(void *ctx, size_t bdev_count, int rc);
37 typedef void (*spdk_bdev_nvme_start_discovery_fn)(void *ctx, int status);
38 typedef void (*spdk_bdev_nvme_stop_discovery_fn)(void *ctx);
39 
40 struct nvme_ctrlr_opts {
41 	uint32_t prchk_flags;
42 	int32_t ctrlr_loss_timeout_sec;
43 	uint32_t reconnect_delay_sec;
44 	uint32_t fast_io_fail_timeout_sec;
45 	bool from_discovery_service;
46 	/* Path to the file containing PSK, used for dumping configuration. */
47 	char psk_path[PATH_MAX];
48 };
49 
50 struct nvme_async_probe_ctx {
51 	struct spdk_nvme_probe_ctx *probe_ctx;
52 	const char *base_name;
53 	const char **names;
54 	uint32_t max_bdevs;
55 	uint32_t reported_bdevs;
56 	struct spdk_poller *poller;
57 	struct spdk_nvme_transport_id trid;
58 	struct nvme_ctrlr_opts bdev_opts;
59 	struct spdk_nvme_ctrlr_opts drv_opts;
60 	spdk_bdev_create_nvme_fn cb_fn;
61 	void *cb_ctx;
62 	uint32_t populates_in_progress;
63 	bool ctrlr_attached;
64 	bool probe_done;
65 	bool namespaces_populated;
66 };
67 
68 struct nvme_ns {
69 	uint32_t			id;
70 	struct spdk_nvme_ns		*ns;
71 	struct nvme_ctrlr		*ctrlr;
72 	struct nvme_bdev		*bdev;
73 	uint32_t			ana_group_id;
74 	enum spdk_nvme_ana_state	ana_state;
75 	bool				ana_state_updating;
76 	bool				ana_transition_timedout;
77 	struct spdk_poller		*anatt_timer;
78 	struct nvme_async_probe_ctx	*probe_ctx;
79 	TAILQ_ENTRY(nvme_ns)		tailq;
80 	RB_ENTRY(nvme_ns)		node;
81 
82 	/**
83 	 * record io path stat before destroyed. Allocation of stat is
84 	 * decided by option io_path_stat of RPC
85 	 * bdev_nvme_set_options
86 	 */
87 	struct spdk_bdev_io_stat	*stat;
88 };
89 
90 struct nvme_bdev_io;
91 struct nvme_bdev_ctrlr;
92 struct nvme_bdev;
93 struct nvme_io_path;
94 
95 struct nvme_path_id {
96 	struct spdk_nvme_transport_id		trid;
97 	struct spdk_nvme_host_id		hostid;
98 	TAILQ_ENTRY(nvme_path_id)		link;
99 	uint64_t				last_failed_tsc;
100 };
101 
102 typedef void (*bdev_nvme_ctrlr_op_cb)(void *cb_arg, int rc);
103 typedef void (*nvme_ctrlr_disconnected_cb)(struct nvme_ctrlr *nvme_ctrlr);
104 
105 struct nvme_ctrlr {
106 	/**
107 	 * points to pinned, physically contiguous memory region;
108 	 * contains 4KB IDENTIFY structure for controller which is
109 	 *  target for CONTROLLER IDENTIFY command during initialization
110 	 */
111 	struct spdk_nvme_ctrlr			*ctrlr;
112 	struct nvme_path_id			*active_path_id;
113 	int					ref;
114 
115 	uint32_t				resetting : 1;
116 	uint32_t				reconnect_is_delayed : 1;
117 	uint32_t				in_failover : 1;
118 	uint32_t				pending_failover : 1;
119 	uint32_t				fast_io_fail_timedout : 1;
120 	uint32_t				destruct : 1;
121 	uint32_t				ana_log_page_updating : 1;
122 	uint32_t				io_path_cache_clearing : 1;
123 	uint32_t				dont_retry : 1;
124 	uint32_t				disabled : 1;
125 
126 	struct nvme_ctrlr_opts			opts;
127 
128 	RB_HEAD(nvme_ns_tree, nvme_ns)		namespaces;
129 
130 	struct spdk_opal_dev			*opal_dev;
131 
132 	struct spdk_poller			*adminq_timer_poller;
133 	struct spdk_thread			*thread;
134 
135 	bdev_nvme_ctrlr_op_cb			ctrlr_op_cb_fn;
136 	void					*ctrlr_op_cb_arg;
137 	/* Poller used to check for reset/detach completion */
138 	struct spdk_poller			*reset_detach_poller;
139 	struct spdk_nvme_detach_ctx		*detach_ctx;
140 
141 	uint64_t				reset_start_tsc;
142 	struct spdk_poller			*reconnect_delay_timer;
143 
144 	nvme_ctrlr_disconnected_cb		disconnected_cb;
145 
146 	/** linked list pointer for device list */
147 	TAILQ_ENTRY(nvme_ctrlr)			tailq;
148 	struct nvme_bdev_ctrlr			*nbdev_ctrlr;
149 
150 	TAILQ_HEAD(nvme_paths, nvme_path_id)	trids;
151 
152 	uint32_t				max_ana_log_page_size;
153 	struct spdk_nvme_ana_page		*ana_log_page;
154 	struct spdk_nvme_ana_group_descriptor	*copied_ana_desc;
155 
156 	struct nvme_async_probe_ctx		*probe_ctx;
157 
158 	pthread_mutex_t				mutex;
159 };
160 
161 struct nvme_bdev_ctrlr {
162 	char				*name;
163 	TAILQ_HEAD(, nvme_ctrlr)	ctrlrs;
164 	TAILQ_HEAD(, nvme_bdev)		bdevs;
165 	TAILQ_ENTRY(nvme_bdev_ctrlr)	tailq;
166 };
167 
168 struct nvme_error_stat {
169 	uint32_t status_type[8];
170 	uint32_t status[4][256];
171 };
172 
173 struct nvme_bdev {
174 	struct spdk_bdev		disk;
175 	uint32_t			nsid;
176 	struct nvme_bdev_ctrlr		*nbdev_ctrlr;
177 	pthread_mutex_t			mutex;
178 	int				ref;
179 	enum bdev_nvme_multipath_policy	mp_policy;
180 	enum bdev_nvme_multipath_selector mp_selector;
181 	uint32_t			rr_min_io;
182 	TAILQ_HEAD(, nvme_ns)		nvme_ns_list;
183 	bool				opal;
184 	TAILQ_ENTRY(nvme_bdev)		tailq;
185 	struct nvme_error_stat		*err_stat;
186 };
187 
188 struct nvme_qpair {
189 	struct nvme_ctrlr		*ctrlr;
190 	struct spdk_nvme_qpair		*qpair;
191 	struct nvme_poll_group		*group;
192 	struct nvme_ctrlr_channel	*ctrlr_ch;
193 
194 	/* The following is used to update io_path cache of nvme_bdev_channels. */
195 	TAILQ_HEAD(, nvme_io_path)	io_path_list;
196 
197 	TAILQ_ENTRY(nvme_qpair)		tailq;
198 };
199 
200 struct nvme_ctrlr_channel {
201 	struct nvme_qpair		*qpair;
202 	TAILQ_HEAD(, spdk_bdev_io)	pending_resets;
203 
204 	struct spdk_io_channel_iter	*reset_iter;
205 	struct spdk_poller		*connect_poller;
206 };
207 
208 struct nvme_io_path {
209 	struct nvme_ns			*nvme_ns;
210 	struct nvme_qpair		*qpair;
211 	STAILQ_ENTRY(nvme_io_path)	stailq;
212 
213 	/* The following are used to update io_path cache of the nvme_bdev_channel. */
214 	struct nvme_bdev_channel	*nbdev_ch;
215 	TAILQ_ENTRY(nvme_io_path)	tailq;
216 
217 	/* allocation of stat is decided by option io_path_stat of RPC bdev_nvme_set_options */
218 	struct spdk_bdev_io_stat	*stat;
219 };
220 
221 struct nvme_bdev_channel {
222 	struct nvme_io_path			*current_io_path;
223 	enum bdev_nvme_multipath_policy		mp_policy;
224 	enum bdev_nvme_multipath_selector	mp_selector;
225 	uint32_t				rr_min_io;
226 	uint32_t				rr_counter;
227 	STAILQ_HEAD(, nvme_io_path)		io_path_list;
228 	TAILQ_HEAD(retry_io_head, spdk_bdev_io)	retry_io_list;
229 	struct spdk_poller			*retry_io_poller;
230 };
231 
232 struct nvme_poll_group {
233 	struct spdk_nvme_poll_group		*group;
234 	struct spdk_io_channel			*accel_channel;
235 	struct spdk_poller			*poller;
236 	bool					collect_spin_stat;
237 	uint64_t				spin_ticks;
238 	uint64_t				start_ticks;
239 	uint64_t				end_ticks;
240 	TAILQ_HEAD(, nvme_qpair)		qpair_list;
241 };
242 
243 void nvme_io_path_info_json(struct spdk_json_write_ctx *w, struct nvme_io_path *io_path);
244 
245 struct nvme_ctrlr *nvme_ctrlr_get_by_name(const char *name);
246 
247 struct nvme_ctrlr *nvme_bdev_ctrlr_get_ctrlr_by_id(struct nvme_bdev_ctrlr *nbdev_ctrlr,
248 		uint16_t cntlid);
249 
250 struct nvme_bdev_ctrlr *nvme_bdev_ctrlr_get_by_name(const char *name);
251 
252 typedef void (*nvme_bdev_ctrlr_for_each_fn)(struct nvme_bdev_ctrlr *nbdev_ctrlr, void *ctx);
253 
254 void nvme_bdev_ctrlr_for_each(nvme_bdev_ctrlr_for_each_fn fn, void *ctx);
255 
256 void nvme_bdev_dump_trid_json(const struct spdk_nvme_transport_id *trid,
257 			      struct spdk_json_write_ctx *w);
258 
259 void nvme_ctrlr_info_json(struct spdk_json_write_ctx *w, struct nvme_ctrlr *nvme_ctrlr);
260 
261 struct nvme_ns *nvme_ctrlr_get_ns(struct nvme_ctrlr *nvme_ctrlr, uint32_t nsid);
262 struct nvme_ns *nvme_ctrlr_get_first_active_ns(struct nvme_ctrlr *nvme_ctrlr);
263 struct nvme_ns *nvme_ctrlr_get_next_active_ns(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ns *ns);
264 
265 enum spdk_bdev_timeout_action {
266 	SPDK_BDEV_NVME_TIMEOUT_ACTION_NONE = 0,
267 	SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET,
268 	SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT,
269 };
270 
271 struct spdk_bdev_nvme_opts {
272 	enum spdk_bdev_timeout_action action_on_timeout;
273 	uint64_t timeout_us;
274 	uint64_t timeout_admin_us;
275 	uint32_t keep_alive_timeout_ms;
276 	/* The number of attempts per I/O in the transport layer before an I/O fails. */
277 	uint32_t transport_retry_count;
278 	uint32_t arbitration_burst;
279 	uint32_t low_priority_weight;
280 	uint32_t medium_priority_weight;
281 	uint32_t high_priority_weight;
282 	uint64_t nvme_adminq_poll_period_us;
283 	uint64_t nvme_ioq_poll_period_us;
284 	uint32_t io_queue_requests;
285 	bool delay_cmd_submit;
286 	/* The number of attempts per I/O in the bdev layer before an I/O fails. */
287 	int32_t bdev_retry_count;
288 	uint8_t transport_ack_timeout;
289 	int32_t ctrlr_loss_timeout_sec;
290 	uint32_t reconnect_delay_sec;
291 	uint32_t fast_io_fail_timeout_sec;
292 	bool disable_auto_failback;
293 	bool generate_uuids;
294 	/* Type of Service - RDMA only */
295 	uint8_t transport_tos;
296 	bool nvme_error_stat;
297 	uint32_t rdma_srq_size;
298 	bool io_path_stat;
299 	bool allow_accel_sequence;
300 };
301 
302 struct spdk_nvme_qpair *bdev_nvme_get_io_qpair(struct spdk_io_channel *ctrlr_io_ch);
303 void bdev_nvme_get_opts(struct spdk_bdev_nvme_opts *opts);
304 int bdev_nvme_set_opts(const struct spdk_bdev_nvme_opts *opts);
305 int bdev_nvme_set_hotplug(bool enabled, uint64_t period_us, spdk_msg_fn cb, void *cb_ctx);
306 
307 void bdev_nvme_get_default_ctrlr_opts(struct nvme_ctrlr_opts *opts);
308 
309 int bdev_nvme_create(struct spdk_nvme_transport_id *trid,
310 		     const char *base_name,
311 		     const char **names,
312 		     uint32_t count,
313 		     spdk_bdev_create_nvme_fn cb_fn,
314 		     void *cb_ctx,
315 		     struct spdk_nvme_ctrlr_opts *drv_opts,
316 		     struct nvme_ctrlr_opts *bdev_opts,
317 		     bool multipath);
318 
319 int bdev_nvme_start_discovery(struct spdk_nvme_transport_id *trid, const char *base_name,
320 			      struct spdk_nvme_ctrlr_opts *drv_opts, struct nvme_ctrlr_opts *bdev_opts,
321 			      uint64_t timeout, bool from_mdns,
322 			      spdk_bdev_nvme_start_discovery_fn cb_fn, void *cb_ctx);
323 int bdev_nvme_stop_discovery(const char *name, spdk_bdev_nvme_stop_discovery_fn cb_fn,
324 			     void *cb_ctx);
325 void bdev_nvme_get_discovery_info(struct spdk_json_write_ctx *w);
326 
327 int bdev_nvme_start_mdns_discovery(const char *base_name,
328 				   const char *svcname,
329 				   struct spdk_nvme_ctrlr_opts *drv_opts,
330 				   struct nvme_ctrlr_opts *bdev_opts);
331 int bdev_nvme_stop_mdns_discovery(const char *name);
332 void bdev_nvme_get_mdns_discovery_info(struct spdk_jsonrpc_request *request);
333 void bdev_nvme_mdns_discovery_config_json(struct spdk_json_write_ctx *w);
334 
335 struct spdk_nvme_ctrlr *bdev_nvme_get_ctrlr(struct spdk_bdev *bdev);
336 
337 typedef void (*bdev_nvme_delete_done_fn)(void *ctx, int rc);
338 
339 /**
340  * Delete NVMe controller with all bdevs on top of it, or delete the specified path
341  * if there is any alternative path. Requires to pass name of NVMe controller.
342  *
343  * \param name NVMe controller name
344  * \param path_id The specified path to remove (optional)
345  * \param delete_done Callback function on delete complete (optional)
346  * \param delete_done_ctx Context passed to callback (optional)
347  * \return zero on success,
348  *		-EINVAL on wrong parameters or
349  *		-ENODEV if controller is not found or
350  *		-ENOMEM on no memory
351  */
352 int bdev_nvme_delete(const char *name, const struct nvme_path_id *path_id,
353 		     bdev_nvme_delete_done_fn delete_done, void *delete_done_ctx);
354 
355 enum nvme_ctrlr_op {
356 	NVME_CTRLR_OP_RESET = 1,
357 	NVME_CTRLR_OP_ENABLE,
358 	NVME_CTRLR_OP_DISABLE,
359 };
360 
361 /**
362  * Perform specified operation on an NVMe controller.
363  *
364  * NOTE: The callback function is always called after this function returns except for
365  * out of memory cases.
366  *
367  * \param nvme_ctrlr The specified NVMe controller to operate
368  * \param op Operation code
369  * \param cb_fn Function to be called back after operation completes
370  * \param cb_arg Argument for callback function
371  */
372 void nvme_ctrlr_op_rpc(struct nvme_ctrlr *nvme_ctrlr, enum nvme_ctrlr_op op,
373 		       bdev_nvme_ctrlr_op_cb cb_fn, void *cb_arg);
374 
375 /**
376  * Perform specified operation on all NVMe controllers in an NVMe bdev controller.
377  *
378  * NOTE: The callback function is always called after this function returns except for
379  * out of memory cases.
380  *
381  * \param nbdev_ctrlr The specified NVMe bdev controller to operate
382  * \param op Operation code
383  * \param cb_fn Function to be called back after operation completes
384  * \param cb_arg Argument for callback function
385  */
386 void nvme_bdev_ctrlr_op_rpc(struct nvme_bdev_ctrlr *nbdev_ctrlr, enum nvme_ctrlr_op op,
387 			    bdev_nvme_ctrlr_op_cb cb_fn, void *cb_arg);
388 
389 typedef void (*bdev_nvme_set_preferred_path_cb)(void *cb_arg, int rc);
390 
391 /**
392  * Set the preferred I/O path for an NVMe bdev in multipath mode.
393  *
394  * NOTE: This function does not support NVMe bdevs in failover mode.
395  *
396  * \param name NVMe bdev name
397  * \param cntlid NVMe-oF controller ID
398  * \param cb_fn Function to be called back after completion.
399  * \param cb_arg Argument for callback function.
400  */
401 void bdev_nvme_set_preferred_path(const char *name, uint16_t cntlid,
402 				  bdev_nvme_set_preferred_path_cb cb_fn, void *cb_arg);
403 
404 typedef void (*bdev_nvme_set_multipath_policy_cb)(void *cb_arg, int rc);
405 
406 /**
407  * Set multipath policy of the NVMe bdev.
408  *
409  * \param name NVMe bdev name
410  * \param policy Multipath policy (active-passive or active-active)
411  * \param selector Multipath selector (round_robin, queue_depth)
412  * \param rr_min_io Number of IO to route to a path before switching to another for round-robin
413  * \param cb_fn Function to be called back after completion.
414  */
415 void bdev_nvme_set_multipath_policy(const char *name,
416 				    enum bdev_nvme_multipath_policy policy,
417 				    enum bdev_nvme_multipath_selector selector,
418 				    uint32_t rr_min_io,
419 				    bdev_nvme_set_multipath_policy_cb cb_fn,
420 				    void *cb_arg);
421 
422 #endif /* SPDK_BDEV_NVME_H */
423