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 }; 72 73 enum ftl_io_type { 74 FTL_IO_READ, 75 FTL_IO_WRITE, 76 FTL_IO_ERASE, 77 }; 78 79 struct ftl_io_init_opts { 80 struct spdk_ftl_dev *dev; 81 82 /* IO descriptor */ 83 struct ftl_io *io; 84 85 /* Size of IO descriptor */ 86 size_t size; 87 88 /* IO flags */ 89 int flags; 90 91 /* IO type */ 92 enum ftl_io_type type; 93 94 /* Number of split requests */ 95 size_t iov_cnt; 96 97 /* RWB entry */ 98 struct ftl_rwb_batch *rwb_batch; 99 100 /* Band to which the IO is directed */ 101 struct ftl_band *band; 102 103 /* Request size */ 104 size_t req_size; 105 106 /* Data */ 107 void *data; 108 109 /* Metadata */ 110 void *md; 111 112 /* Callback */ 113 spdk_ftl_fn fn; 114 }; 115 116 struct ftl_cb { 117 /* Callback function */ 118 spdk_ftl_fn fn; 119 120 /* Callback's context */ 121 void *ctx; 122 }; 123 124 struct ftl_io_channel { 125 /* IO pool element size */ 126 size_t elem_size; 127 128 /* IO pool */ 129 struct spdk_mempool *io_pool; 130 }; 131 132 /* General IO descriptor */ 133 struct ftl_io { 134 /* Device */ 135 struct spdk_ftl_dev *dev; 136 137 /* IO channel */ 138 struct spdk_io_channel *ch; 139 140 union { 141 /* LBA table */ 142 uint64_t *lbas; 143 144 /* First LBA */ 145 uint64_t lba; 146 }; 147 148 /* First PPA */ 149 struct ftl_ppa ppa; 150 151 /* Number of processed lbks */ 152 size_t pos; 153 154 /* Number of lbks */ 155 size_t lbk_cnt; 156 157 union { 158 /* IO vector table */ 159 struct iovec *iovs; 160 161 /* Single iovec */ 162 struct iovec iov; 163 }; 164 165 /* Metadata */ 166 void *md; 167 168 /* Number of IO vectors */ 169 size_t iov_cnt; 170 171 /* Position within the iovec */ 172 size_t iov_pos; 173 174 /* Offset within the iovec (in lbks) */ 175 size_t iov_off; 176 177 /* RWB entry (valid only for RWB-based IO) */ 178 struct ftl_rwb_batch *rwb_batch; 179 180 /* Band this IO is being written to */ 181 struct ftl_band *band; 182 183 /* Request status */ 184 int status; 185 186 /* Number of split requests */ 187 size_t req_cnt; 188 189 /* Completion callback */ 190 struct ftl_cb cb; 191 192 /* Flags */ 193 int flags; 194 195 /* IO type */ 196 enum ftl_io_type type; 197 198 /* Trace group id */ 199 uint64_t trace; 200 }; 201 202 /* Metadata IO */ 203 struct ftl_md_io { 204 /* Parent IO structure */ 205 struct ftl_io io; 206 207 /* Destination metadata pointer */ 208 struct ftl_md *md; 209 210 /* Metadata's buffer */ 211 void *buf; 212 213 /* Serialization/deserialization callback */ 214 ftl_md_pack_fn pack_fn; 215 216 /* User's callback */ 217 struct ftl_cb cb; 218 }; 219 220 static inline bool 221 ftl_io_mode_ppa(const struct ftl_io *io) 222 { 223 return io->flags & FTL_IO_PPA_MODE; 224 } 225 226 static inline bool 227 ftl_io_mode_lba(const struct ftl_io *io) 228 { 229 return !ftl_io_mode_ppa(io); 230 } 231 232 static inline bool 233 ftl_io_done(const struct ftl_io *io) 234 { 235 return io->req_cnt == 0; 236 } 237 238 struct ftl_io *ftl_io_alloc(struct spdk_io_channel *ch); 239 void ftl_io_free(struct ftl_io *io); 240 struct ftl_io *ftl_io_init_internal(const struct ftl_io_init_opts *opts); 241 void ftl_io_reinit(struct ftl_io *io, spdk_ftl_fn cb, 242 void *ctx, int flags, int type); 243 void ftl_io_clear(struct ftl_io *io); 244 size_t ftl_io_inc_req(struct ftl_io *io); 245 size_t ftl_io_dec_req(struct ftl_io *io); 246 struct iovec *ftl_io_iovec(struct ftl_io *io); 247 uint64_t ftl_io_current_lba(struct ftl_io *io); 248 void ftl_io_update_iovec(struct ftl_io *io, size_t lbk_cnt); 249 size_t ftl_iovec_num_lbks(struct iovec *iov, size_t iov_cnt); 250 void *ftl_io_iovec_addr(struct ftl_io *io); 251 size_t ftl_io_iovec_len_left(struct ftl_io *io); 252 int ftl_io_init_iovec(struct ftl_io *io, void *buf, 253 size_t iov_cnt, size_t req_size); 254 struct ftl_io *ftl_io_init_internal(const struct ftl_io_init_opts *opts); 255 struct ftl_io *ftl_io_rwb_init(struct spdk_ftl_dev *dev, struct ftl_band *band, 256 struct ftl_rwb_batch *entry, spdk_ftl_fn cb); 257 struct ftl_io *ftl_io_erase_init(struct ftl_band *band, size_t lbk_cnt, spdk_ftl_fn cb); 258 void ftl_io_user_init(struct spdk_ftl_dev *dev, struct ftl_io *io, uint64_t lba, size_t lbk_cnt, 259 struct iovec *iov, size_t iov_cnt, 260 spdk_ftl_fn fn, void *cb_arg, int type); 261 void *ftl_io_get_md(const struct ftl_io *io); 262 void ftl_io_complete(struct ftl_io *io); 263 void ftl_io_process_error(struct ftl_io *io, const struct spdk_nvme_cpl *status); 264 265 #endif /* FTL_IO_H */ 266