xref: /spdk/module/bdev/nvme/bdev_nvme.h (revision 2ee6ab36f9a0e38f0e47e9dab3db40a6ea72cfd5)
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 SPDK_BDEV_NVME_H
35 #define SPDK_BDEV_NVME_H
36 
37 #include "spdk/stdinc.h"
38 
39 #include "spdk/queue.h"
40 #include "spdk/nvme.h"
41 #include "spdk/bdev_module.h"
42 
43 TAILQ_HEAD(nvme_ctrlrs, nvme_ctrlr);
44 extern struct nvme_ctrlrs g_nvme_ctrlrs;
45 extern pthread_mutex_t g_bdev_nvme_mutex;
46 extern bool g_bdev_nvme_module_finish;
47 
48 #define NVME_MAX_CONTROLLERS 1024
49 
50 typedef void (*spdk_bdev_create_nvme_fn)(void *ctx, size_t bdev_count, int rc);
51 
52 struct nvme_async_probe_ctx {
53 	struct spdk_nvme_probe_ctx *probe_ctx;
54 	const char *base_name;
55 	const char **names;
56 	uint32_t count;
57 	uint32_t prchk_flags;
58 	struct spdk_poller *poller;
59 	struct spdk_nvme_transport_id trid;
60 	struct spdk_nvme_ctrlr_opts opts;
61 	spdk_bdev_create_nvme_fn cb_fn;
62 	void *cb_ctx;
63 	uint32_t populates_in_progress;
64 	bool ctrlr_attached;
65 	bool probe_done;
66 	bool namespaces_populated;
67 };
68 
69 struct nvme_ns {
70 	uint32_t			id;
71 	struct spdk_nvme_ns		*ns;
72 	struct nvme_ctrlr		*ctrlr;
73 	struct nvme_bdev		*bdev;
74 	uint32_t			ana_group_id;
75 	enum spdk_nvme_ana_state	ana_state;
76 	struct nvme_async_probe_ctx	*probe_ctx;
77 };
78 
79 struct nvme_bdev_io;
80 
81 struct nvme_ctrlr_trid {
82 	struct spdk_nvme_transport_id		trid;
83 	TAILQ_ENTRY(nvme_ctrlr_trid)		link;
84 	bool					is_failed;
85 };
86 
87 typedef void (*bdev_nvme_reset_cb)(void *cb_arg, int rc);
88 
89 struct nvme_ctrlr {
90 	/**
91 	 * points to pinned, physically contiguous memory region;
92 	 * contains 4KB IDENTIFY structure for controller which is
93 	 *  target for CONTROLLER IDENTIFY command during initialization
94 	 */
95 	struct spdk_nvme_ctrlr			*ctrlr;
96 	struct spdk_nvme_transport_id		*connected_trid;
97 	char					*name;
98 	int					ref;
99 	bool					resetting;
100 	bool					failover_in_progress;
101 	bool					destruct;
102 	bool					destruct_after_reset;
103 	/**
104 	 * PI check flags. This flags is set to NVMe controllers created only
105 	 * through bdev_nvme_attach_controller RPC or .INI config file. Hot added
106 	 * NVMe controllers are not included.
107 	 */
108 	uint32_t				prchk_flags;
109 	uint32_t				num_ns;
110 	/** Array of pointers to namespaces indexed by nsid - 1 */
111 	struct nvme_ns				**namespaces;
112 
113 	struct spdk_opal_dev			*opal_dev;
114 
115 	struct spdk_poller			*adminq_timer_poller;
116 	struct spdk_thread			*thread;
117 
118 	bdev_nvme_reset_cb			reset_cb_fn;
119 	void					*reset_cb_arg;
120 	struct spdk_nvme_ctrlr_reset_ctx	*reset_ctx;
121 	struct spdk_poller			*reset_poller;
122 
123 	/** linked list pointer for device list */
124 	TAILQ_ENTRY(nvme_ctrlr)			tailq;
125 
126 	TAILQ_HEAD(, nvme_ctrlr_trid)		trids;
127 
128 	uint32_t				ana_log_page_size;
129 	struct spdk_nvme_ana_page		*ana_log_page;
130 	struct spdk_nvme_ana_group_descriptor	*copied_ana_desc;
131 
132 	struct nvme_async_probe_ctx		*probe_ctx;
133 
134 	pthread_mutex_t				mutex;
135 };
136 
137 struct nvme_bdev {
138 	struct spdk_bdev	disk;
139 	struct nvme_ns		*nvme_ns;
140 	bool			opal;
141 };
142 
143 struct nvme_poll_group {
144 	struct spdk_nvme_poll_group		*group;
145 	struct spdk_io_channel			*accel_channel;
146 	struct spdk_poller			*poller;
147 	bool					collect_spin_stat;
148 	uint64_t				spin_ticks;
149 	uint64_t				start_ticks;
150 	uint64_t				end_ticks;
151 };
152 
153 struct nvme_ctrlr_channel {
154 	struct nvme_ctrlr		*ctrlr;
155 	struct spdk_nvme_qpair		*qpair;
156 	struct nvme_poll_group		*group;
157 	TAILQ_HEAD(, spdk_bdev_io)	pending_resets;
158 };
159 
160 struct nvme_bdev_channel {
161 	struct nvme_ns			*nvme_ns;
162 	struct nvme_ctrlr_channel	*ctrlr_ch;
163 };
164 
165 struct nvme_ctrlr *nvme_ctrlr_get_by_name(const char *name);
166 
167 typedef void (*nvme_ctrlr_for_each_fn)(struct nvme_ctrlr *nvme_ctrlr, void *ctx);
168 
169 void nvme_ctrlr_for_each(nvme_ctrlr_for_each_fn fn, void *ctx);
170 
171 void nvme_bdev_dump_trid_json(const struct spdk_nvme_transport_id *trid,
172 			      struct spdk_json_write_ctx *w);
173 
174 struct nvme_ns *nvme_ctrlr_get_ns(struct nvme_ctrlr *nvme_ctrlr, uint32_t nsid);
175 struct nvme_ns *nvme_ctrlr_get_first_active_ns(struct nvme_ctrlr *nvme_ctrlr);
176 struct nvme_ns *nvme_ctrlr_get_next_active_ns(struct nvme_ctrlr *nvme_ctrlr, struct nvme_ns *ns);
177 
178 enum spdk_bdev_timeout_action {
179 	SPDK_BDEV_NVME_TIMEOUT_ACTION_NONE = 0,
180 	SPDK_BDEV_NVME_TIMEOUT_ACTION_RESET,
181 	SPDK_BDEV_NVME_TIMEOUT_ACTION_ABORT,
182 };
183 
184 struct spdk_bdev_nvme_opts {
185 	enum spdk_bdev_timeout_action action_on_timeout;
186 	uint64_t timeout_us;
187 	uint64_t timeout_admin_us;
188 	uint32_t keep_alive_timeout_ms;
189 	uint32_t retry_count;
190 	uint32_t arbitration_burst;
191 	uint32_t low_priority_weight;
192 	uint32_t medium_priority_weight;
193 	uint32_t high_priority_weight;
194 	uint64_t nvme_adminq_poll_period_us;
195 	uint64_t nvme_ioq_poll_period_us;
196 	uint32_t io_queue_requests;
197 	bool delay_cmd_submit;
198 };
199 
200 struct spdk_nvme_qpair *bdev_nvme_get_io_qpair(struct spdk_io_channel *ctrlr_io_ch);
201 void bdev_nvme_get_opts(struct spdk_bdev_nvme_opts *opts);
202 int bdev_nvme_set_opts(const struct spdk_bdev_nvme_opts *opts);
203 int bdev_nvme_set_hotplug(bool enabled, uint64_t period_us, spdk_msg_fn cb, void *cb_ctx);
204 
205 int bdev_nvme_create(struct spdk_nvme_transport_id *trid,
206 		     const char *base_name,
207 		     const char **names,
208 		     uint32_t count,
209 		     uint32_t prchk_flags,
210 		     spdk_bdev_create_nvme_fn cb_fn,
211 		     void *cb_ctx,
212 		     struct spdk_nvme_ctrlr_opts *opts);
213 struct spdk_nvme_ctrlr *bdev_nvme_get_ctrlr(struct spdk_bdev *bdev);
214 
215 /**
216  * Delete NVMe controller with all bdevs on top of it, or delete the specified path
217  * if there is any alternative path. Requires to pass name of NVMe controller.
218  *
219  * \param name NVMe controller name
220  * \param trid The specified transport ID to remove (optional)
221  * \return zero on success, -EINVAL on wrong parameters or -ENODEV if controller is not found
222  */
223 int bdev_nvme_delete(const char *name, const struct spdk_nvme_transport_id *trid);
224 
225 /**
226  * Reset NVMe controller.
227  *
228  * \param nvme_ctrlr The specified NVMe controller to reset
229  * \param cb_fn Function to be called back after reset completes
230  * \param cb_arg Argument for callback function
231  * \return zero on success. Negated errno on the following error conditions:
232  * -ENXIO: controller is being destroyed.
233  * -EBUSY: controller is already being reset.
234  */
235 int bdev_nvme_reset_rpc(struct nvme_ctrlr *nvme_ctrlr, bdev_nvme_reset_cb cb_fn, void *cb_arg);
236 
237 #endif /* SPDK_BDEV_NVME_H */
238