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