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