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_addr.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 49 typedef int (*ftl_md_pack_fn)(struct ftl_band *); 50 typedef void (*ftl_io_fn)(struct ftl_io *, void *, int); 51 52 /* IO flags */ 53 enum ftl_io_flags { 54 /* Indicates whether IO is already initialized */ 55 FTL_IO_INITIALIZED = (1 << 0), 56 /* Internal based IO (defrag, metadata etc.) */ 57 FTL_IO_INTERNAL = (1 << 1), 58 /* Indicates that the IO should not go through if there's */ 59 /* already another one scheduled to the same LBA */ 60 FTL_IO_WEAK = (1 << 2), 61 /* Indicates that the IO is used for padding */ 62 FTL_IO_PAD = (1 << 3), 63 /* The IO operates on metadata */ 64 FTL_IO_MD = (1 << 4), 65 /* Using physical instead of logical address */ 66 FTL_IO_PHYSICAL_MODE = (1 << 5), 67 /* Indicates that IO contains noncontiguous LBAs */ 68 FTL_IO_VECTOR_LBA = (1 << 6), 69 /* Indicates that IO is being retried */ 70 FTL_IO_RETRY = (1 << 7), 71 /* The IO is directed to non-volatile cache */ 72 FTL_IO_CACHE = (1 << 8), 73 /* Indicates that physical address should be taken from IO struct, */ 74 /* not assigned by wptr, only works if wptr is also in direct mode */ 75 FTL_IO_DIRECT_ACCESS = (1 << 9), 76 /* Bypass the non-volatile cache */ 77 FTL_IO_BYPASS_CACHE = (1 << 10), 78 }; 79 80 enum ftl_io_type { 81 FTL_IO_READ, 82 FTL_IO_WRITE, 83 FTL_IO_ERASE, 84 }; 85 86 struct ftl_io_init_opts { 87 struct spdk_ftl_dev *dev; 88 89 /* IO descriptor */ 90 struct ftl_io *io; 91 92 /* Parent request */ 93 struct ftl_io *parent; 94 95 /* Size of IO descriptor */ 96 size_t size; 97 98 /* IO flags */ 99 int flags; 100 101 /* IO type */ 102 enum ftl_io_type type; 103 104 /* RWB entry */ 105 struct ftl_rwb_batch *rwb_batch; 106 107 /* Band to which the IO is directed */ 108 struct ftl_band *band; 109 110 /* Number of logical blocks */ 111 size_t num_blocks; 112 113 /* Data */ 114 void *data; 115 116 /* Metadata */ 117 void *md; 118 119 /* Callback's function */ 120 ftl_io_fn cb_fn; 121 122 /* Callback's context */ 123 void *cb_ctx; 124 }; 125 126 struct ftl_io_channel { 127 /* Device */ 128 struct spdk_ftl_dev *dev; 129 /* IO pool element size */ 130 size_t elem_size; 131 /* IO pool */ 132 struct spdk_mempool *io_pool; 133 /* Underlying device IO channel */ 134 struct spdk_io_channel *base_ioch; 135 /* Persistent cache IO channel */ 136 struct spdk_io_channel *cache_ioch; 137 }; 138 139 /* General IO descriptor */ 140 struct ftl_io { 141 /* Device */ 142 struct spdk_ftl_dev *dev; 143 144 /* IO channel */ 145 struct spdk_io_channel *ioch; 146 147 union { 148 /* LBA table */ 149 uint64_t *vector; 150 151 /* First LBA */ 152 uint64_t single; 153 } lba; 154 155 /* First block address */ 156 struct ftl_addr addr; 157 158 /* Number of processed blocks */ 159 size_t pos; 160 161 /* Number of blocks */ 162 size_t num_blocks; 163 164 #define FTL_IO_MAX_IOVEC 64 165 struct iovec iov[FTL_IO_MAX_IOVEC]; 166 167 /* Metadata */ 168 void *md; 169 170 /* Number of IO vectors */ 171 size_t iov_cnt; 172 173 /* Position within the iovec */ 174 size_t iov_pos; 175 176 /* Offset within the iovec (in blocks) */ 177 size_t iov_off; 178 179 /* RWB entry (valid only for RWB-based IO) */ 180 struct ftl_rwb_batch *rwb_batch; 181 182 /* Band this IO is being written to */ 183 struct ftl_band *band; 184 185 /* Request status */ 186 int status; 187 188 /* Number of split requests */ 189 size_t req_cnt; 190 191 /* Callback's function */ 192 ftl_io_fn cb_fn; 193 194 /* Callback's context */ 195 void *cb_ctx; 196 197 /* User callback function */ 198 spdk_ftl_fn user_fn; 199 200 /* Flags */ 201 int flags; 202 203 /* IO type */ 204 enum ftl_io_type type; 205 206 /* Done flag */ 207 bool done; 208 209 /* Parent request */ 210 struct ftl_io *parent; 211 /* Child requests list */ 212 LIST_HEAD(, ftl_io) children; 213 /* Child list link */ 214 LIST_ENTRY(ftl_io) child_entry; 215 /* Children lock */ 216 pthread_spinlock_t lock; 217 218 /* Trace group id */ 219 uint64_t trace; 220 221 TAILQ_ENTRY(ftl_io) retry_entry; 222 }; 223 224 /* Metadata IO */ 225 struct ftl_md_io { 226 /* Parent IO structure */ 227 struct ftl_io io; 228 229 /* Serialization/deserialization callback */ 230 ftl_md_pack_fn pack_fn; 231 232 /* Callback's function */ 233 ftl_io_fn cb_fn; 234 235 /* Callback's context */ 236 void *cb_ctx; 237 }; 238 239 static inline bool 240 ftl_io_mode_physical(const struct ftl_io *io) 241 { 242 return io->flags & FTL_IO_PHYSICAL_MODE; 243 } 244 245 static inline bool 246 ftl_io_mode_logical(const struct ftl_io *io) 247 { 248 return !ftl_io_mode_physical(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->num_blocks && 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, ftl_io_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 num_blocks); 273 size_t ftl_iovec_num_blocks(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 struct ftl_io *ftl_io_rwb_init(struct spdk_ftl_dev *dev, struct ftl_addr addr, 277 struct ftl_band *band, 278 struct ftl_rwb_batch *entry, ftl_io_fn cb); 279 struct ftl_io *ftl_io_erase_init(struct ftl_band *band, size_t num_blocks, ftl_io_fn cb); 280 struct ftl_io *ftl_io_user_init(struct spdk_io_channel *ioch, uint64_t lba, size_t num_blocks, 281 struct iovec *iov, size_t iov_cnt, spdk_ftl_fn cb_fn, 282 void *cb_arg, int type); 283 void *ftl_io_get_md(const struct ftl_io *io); 284 void ftl_io_complete(struct ftl_io *io); 285 void ftl_io_shrink_iovec(struct ftl_io *io, size_t num_blocks); 286 void ftl_io_process_error(struct ftl_io *io, const struct spdk_nvme_cpl *status); 287 void ftl_io_reset(struct ftl_io *io); 288 void ftl_io_call_foreach_child(struct ftl_io *io, int (*callback)(struct ftl_io *)); 289 290 #endif /* FTL_IO_H */ 291