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 #include "spdk/io_channel.h" 35 36 #define DEV_BUFFER_SIZE (64 * 1024 * 1024) 37 #define DEV_BUFFER_BLOCKLEN (4096) 38 #define DEV_BUFFER_BLOCKCNT (DEV_BUFFER_SIZE / DEV_BUFFER_BLOCKLEN) 39 uint8_t *g_dev_buffer; 40 41 /* Define here for UT only. */ 42 struct spdk_io_channel g_io_channel; 43 44 static struct spdk_io_channel * 45 dev_create_channel(struct spdk_bs_dev *dev) 46 { 47 return &g_io_channel; 48 } 49 50 static void 51 dev_destroy_channel(struct spdk_bs_dev *dev, struct spdk_io_channel *channel) 52 { 53 } 54 55 static void 56 dev_destroy(struct spdk_bs_dev *dev) 57 { 58 free(dev); 59 } 60 61 static void 62 dev_complete(void *arg) 63 { 64 struct spdk_bs_dev_cb_args *cb_args = arg; 65 66 cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, 0); 67 } 68 69 static void 70 dev_read(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, void *payload, 71 uint64_t lba, uint32_t lba_count, 72 struct spdk_bs_dev_cb_args *cb_args) 73 { 74 uint64_t offset, length; 75 76 offset = lba * DEV_BUFFER_BLOCKLEN; 77 length = lba_count * DEV_BUFFER_BLOCKLEN; 78 SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE); 79 memcpy(payload, &g_dev_buffer[offset], length); 80 spdk_thread_send_msg(spdk_get_thread(), dev_complete, cb_args); 81 } 82 83 static void 84 dev_write(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, void *payload, 85 uint64_t lba, uint32_t lba_count, 86 struct spdk_bs_dev_cb_args *cb_args) 87 { 88 uint64_t offset, length; 89 90 offset = lba * DEV_BUFFER_BLOCKLEN; 91 length = lba_count * DEV_BUFFER_BLOCKLEN; 92 SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE); 93 memcpy(&g_dev_buffer[offset], payload, length); 94 spdk_thread_send_msg(spdk_get_thread(), dev_complete, cb_args); 95 } 96 97 static void 98 __check_iov(struct iovec *iov, int iovcnt, uint64_t length) 99 { 100 int i; 101 102 for (i = 0; i < iovcnt; i++) { 103 length -= iov[i].iov_len; 104 } 105 106 CU_ASSERT(length == 0); 107 } 108 109 static void 110 dev_readv(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, 111 struct iovec *iov, int iovcnt, 112 uint64_t lba, uint32_t lba_count, 113 struct spdk_bs_dev_cb_args *cb_args) 114 { 115 uint64_t offset, length; 116 int i; 117 118 offset = lba * DEV_BUFFER_BLOCKLEN; 119 length = lba_count * DEV_BUFFER_BLOCKLEN; 120 SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE); 121 __check_iov(iov, iovcnt, length); 122 123 for (i = 0; i < iovcnt; i++) { 124 memcpy(iov[i].iov_base, &g_dev_buffer[offset], iov[i].iov_len); 125 offset += iov[i].iov_len; 126 } 127 128 spdk_thread_send_msg(spdk_get_thread(), dev_complete, cb_args); 129 } 130 131 static void 132 dev_writev(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, 133 struct iovec *iov, int iovcnt, 134 uint64_t lba, uint32_t lba_count, 135 struct spdk_bs_dev_cb_args *cb_args) 136 { 137 uint64_t offset, length; 138 int i; 139 140 offset = lba * DEV_BUFFER_BLOCKLEN; 141 length = lba_count * DEV_BUFFER_BLOCKLEN; 142 SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE); 143 __check_iov(iov, iovcnt, length); 144 145 for (i = 0; i < iovcnt; i++) { 146 memcpy(&g_dev_buffer[offset], iov[i].iov_base, iov[i].iov_len); 147 offset += iov[i].iov_len; 148 } 149 150 spdk_thread_send_msg(spdk_get_thread(), dev_complete, cb_args); 151 } 152 153 static void 154 dev_flush(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, 155 struct spdk_bs_dev_cb_args *cb_args) 156 { 157 spdk_thread_send_msg(spdk_get_thread(), dev_complete, cb_args); 158 } 159 160 static void 161 dev_unmap(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, 162 uint64_t lba, uint32_t lba_count, 163 struct spdk_bs_dev_cb_args *cb_args) 164 { 165 uint64_t offset, length; 166 167 offset = lba * DEV_BUFFER_BLOCKLEN; 168 length = lba_count * DEV_BUFFER_BLOCKLEN; 169 SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE); 170 memset(&g_dev_buffer[offset], 0, length); 171 spdk_thread_send_msg(spdk_get_thread(), dev_complete, cb_args); 172 } 173 174 static void 175 dev_write_zeroes(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, 176 uint64_t lba, uint32_t lba_count, 177 struct spdk_bs_dev_cb_args *cb_args) 178 { 179 uint64_t offset, length; 180 181 offset = lba * DEV_BUFFER_BLOCKLEN; 182 length = lba_count * DEV_BUFFER_BLOCKLEN; 183 SPDK_CU_ASSERT_FATAL(offset + length <= DEV_BUFFER_SIZE); 184 memset(&g_dev_buffer[offset], 0, length); 185 spdk_thread_send_msg(spdk_get_thread(), dev_complete, cb_args); 186 } 187 188 static struct spdk_bs_dev * 189 init_dev(void) 190 { 191 struct spdk_bs_dev *dev = calloc(1, sizeof(*dev)); 192 193 SPDK_CU_ASSERT_FATAL(dev != NULL); 194 195 dev->create_channel = dev_create_channel; 196 dev->destroy_channel = dev_destroy_channel; 197 dev->destroy = dev_destroy; 198 dev->read = dev_read; 199 dev->write = dev_write; 200 dev->readv = dev_readv; 201 dev->writev = dev_writev; 202 dev->flush = dev_flush; 203 dev->unmap = dev_unmap; 204 dev->write_zeroes = dev_write_zeroes; 205 dev->blockcnt = DEV_BUFFER_BLOCKCNT; 206 dev->blocklen = DEV_BUFFER_BLOCKLEN; 207 208 return dev; 209 } 210