xref: /spdk/lib/ftl/ftl_io.h (revision c4d9daeb7bf491bc0eb6e8d417b75d44773cb009)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   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 FTL_IO_H
35 #define FTL_IO_H
36 
37 #include "spdk/stdinc.h"
38 #include "spdk/nvme.h"
39 #include "spdk/ftl.h"
40 
41 #include "ftl_ppa.h"
42 #include "ftl_trace.h"
43 
44 struct spdk_ftl_dev;
45 struct ftl_rwb_batch;
46 struct ftl_band;
47 struct ftl_io;
48 struct ftl_md;
49 
50 typedef int (*ftl_md_pack_fn)(struct spdk_ftl_dev *, struct ftl_md *, void *);
51 
52 /* IO flags */
53 enum ftl_io_flags {
54 	/* Indicates whether IO is already initialized */
55 	FTL_IO_INITIALIZED	= (1 << 0),
56 	/* Keep the IO when done with the request */
57 	FTL_IO_KEEP_ALIVE	= (1 << 1),
58 	/* Internal based IO (defrag, metadata etc.) */
59 	FTL_IO_INTERNAL		= (1 << 2),
60 	/* Indicates that the IO should not go through if there's */
61 	/* already another one scheduled to the same LBA */
62 	FTL_IO_WEAK		= (1 << 3),
63 	/* Indicates that the IO is used for padding */
64 	FTL_IO_PAD		= (1 << 4),
65 	/* The IO operates on metadata */
66 	FTL_IO_MD		= (1 << 5),
67 	/* Using PPA instead of LBA */
68 	FTL_IO_PPA_MODE		= (1 << 6),
69 	/* Indicates that IO contains noncontiguous LBAs */
70 	FTL_IO_VECTOR_LBA	= (1 << 7),
71 	/* Indicates that IO is being retried */
72 	FTL_IO_RETRY		= (1 << 8),
73 };
74 
75 enum ftl_io_type {
76 	FTL_IO_READ,
77 	FTL_IO_WRITE,
78 	FTL_IO_ERASE,
79 };
80 
81 struct ftl_io_init_opts {
82 	struct spdk_ftl_dev			*dev;
83 
84 	/* IO descriptor */
85 	struct ftl_io				*io;
86 
87 	/* Parent request */
88 	struct ftl_io				*parent;
89 
90 	/* Size of IO descriptor */
91 	size_t                                  size;
92 
93 	/* IO flags */
94 	int                                     flags;
95 
96 	/* IO type */
97 	enum ftl_io_type			type;
98 
99 	/* Number of split requests */
100 	size_t                                  iov_cnt;
101 
102 	/* RWB entry */
103 	struct ftl_rwb_batch			*rwb_batch;
104 
105 	/* Band to which the IO is directed */
106 	struct ftl_band				*band;
107 
108 	/* Request size */
109 	size_t                                  req_size;
110 
111 	/* Data */
112 	void                                    *data;
113 
114 	/* Metadata */
115 	void                                    *md;
116 
117 	/* Callback */
118 	spdk_ftl_fn				fn;
119 };
120 
121 struct ftl_cb {
122 	/* Callback function */
123 	spdk_ftl_fn				fn;
124 
125 	/* Callback's context */
126 	void					*ctx;
127 };
128 
129 struct ftl_io_channel {
130 	/* IO pool element size */
131 	size_t					elem_size;
132 
133 	/* IO pool */
134 	struct spdk_mempool			*io_pool;
135 };
136 
137 /* General IO descriptor */
138 struct ftl_io {
139 	/* Device */
140 	struct spdk_ftl_dev			*dev;
141 
142 	/* IO channel */
143 	struct spdk_io_channel			*ioch;
144 
145 	union {
146 		/* LBA table */
147 		uint64_t			*vector;
148 
149 		/* First LBA */
150 		uint64_t			single;
151 	} lba;
152 
153 	/* First PPA */
154 	struct ftl_ppa				ppa;
155 
156 	/* Number of processed lbks */
157 	size_t					pos;
158 
159 	/* Number of lbks */
160 	size_t					lbk_cnt;
161 
162 	union {
163 		/* IO vector table */
164 		struct iovec			*vector;
165 
166 		/* Single iovec */
167 		struct iovec			single;
168 	} iov;
169 
170 	/* Metadata */
171 	void					*md;
172 
173 	/* Number of IO vectors */
174 	size_t					iov_cnt;
175 
176 	/* Position within the iovec */
177 	size_t					iov_pos;
178 
179 	/* Offset within the iovec (in lbks) */
180 	size_t					iov_off;
181 
182 	/* RWB entry (valid only for RWB-based IO) */
183 	struct ftl_rwb_batch			*rwb_batch;
184 
185 	/* Band this IO is being written to */
186 	struct ftl_band				*band;
187 
188 	/* Request status */
189 	int					status;
190 
191 	/* Number of split requests */
192 	size_t					req_cnt;
193 
194 	/* Completion callback */
195 	struct ftl_cb				cb;
196 
197 	/* Flags */
198 	int					flags;
199 
200 	/* IO type */
201 	enum ftl_io_type			type;
202 
203 	/* Done flag */
204 	bool					done;
205 
206 	/* Parent request */
207 	struct ftl_io				*parent;
208 	/* Child requests list */
209 	LIST_HEAD(, ftl_io)			children;
210 	/* Child list link */
211 	LIST_ENTRY(ftl_io)			child_entry;
212 	/* Children lock */
213 	pthread_spinlock_t			lock;
214 
215 	/* Trace group id */
216 	uint64_t				trace;
217 
218 	TAILQ_ENTRY(ftl_io)			retry_entry;
219 };
220 
221 /* Metadata IO */
222 struct ftl_md_io {
223 	/* Parent IO structure */
224 	struct ftl_io				io;
225 
226 	/* Destination metadata pointer */
227 	struct ftl_md				*md;
228 
229 	/* Metadata's buffer */
230 	void					*buf;
231 
232 	/* Serialization/deserialization callback */
233 	ftl_md_pack_fn				pack_fn;
234 
235 	/* User's callback */
236 	struct ftl_cb				cb;
237 };
238 
239 static inline bool
240 ftl_io_mode_ppa(const struct ftl_io *io)
241 {
242 	return io->flags & FTL_IO_PPA_MODE;
243 }
244 
245 static inline bool
246 ftl_io_mode_lba(const struct ftl_io *io)
247 {
248 	return !ftl_io_mode_ppa(io);
249 }
250 
251 static inline bool
252 ftl_io_done(const struct ftl_io *io)
253 {
254 	return io->req_cnt == 0 &&
255 	       io->pos == io->lbk_cnt &&
256 	       !(io->flags & FTL_IO_RETRY);
257 }
258 
259 struct ftl_io *ftl_io_alloc(struct spdk_io_channel *ch);
260 struct ftl_io *ftl_io_alloc_child(struct ftl_io *parent);
261 void ftl_io_fail(struct ftl_io *io, int status);
262 void ftl_io_free(struct ftl_io *io);
263 struct ftl_io *ftl_io_init_internal(const struct ftl_io_init_opts *opts);
264 void ftl_io_reinit(struct ftl_io *io, spdk_ftl_fn cb,
265 		   void *ctx, int flags, int type);
266 void ftl_io_clear(struct ftl_io *io);
267 void ftl_io_inc_req(struct ftl_io *io);
268 void ftl_io_dec_req(struct ftl_io *io);
269 struct iovec *ftl_io_iovec(struct ftl_io *io);
270 uint64_t ftl_io_current_lba(const struct ftl_io *io);
271 uint64_t ftl_io_get_lba(const struct ftl_io *io, size_t offset);
272 void ftl_io_advance(struct ftl_io *io, size_t lbk_cnt);
273 size_t ftl_iovec_num_lbks(struct iovec *iov, size_t iov_cnt);
274 void *ftl_io_iovec_addr(struct ftl_io *io);
275 size_t ftl_io_iovec_len_left(struct ftl_io *io);
276 int ftl_io_init_iovec(struct ftl_io *io, void *buf,
277 		      size_t iov_cnt, size_t req_size);
278 struct ftl_io *ftl_io_init_internal(const struct ftl_io_init_opts *opts);
279 struct ftl_io *ftl_io_rwb_init(struct spdk_ftl_dev *dev, struct ftl_band *band,
280 			       struct ftl_rwb_batch *entry, spdk_ftl_fn cb);
281 struct ftl_io *ftl_io_erase_init(struct ftl_band *band, size_t lbk_cnt, spdk_ftl_fn cb);
282 void ftl_io_user_init(struct spdk_ftl_dev *dev, struct ftl_io *io, uint64_t lba, size_t lbk_cnt,
283 		      struct iovec *iov, size_t iov_cnt,
284 		      spdk_ftl_fn fn, void *cb_arg, int type);
285 void *ftl_io_get_md(const struct ftl_io *io);
286 void ftl_io_complete(struct ftl_io *io);
287 void ftl_io_process_error(struct ftl_io *io, const struct spdk_nvme_cpl *status);
288 
289 #endif /* FTL_IO_H */
290