| 1 | /* | 
|---|
| 2 | * Copyright (c) 2009 Jiri Svoboda | 
|---|
| 3 | * All rights reserved. | 
|---|
| 4 | * | 
|---|
| 5 | * Redistribution and use in source and binary forms, with or without | 
|---|
| 6 | * modification, are permitted provided that the following conditions | 
|---|
| 7 | * are met: | 
|---|
| 8 | * | 
|---|
| 9 | * - Redistributions of source code must retain the above copyright | 
|---|
| 10 | *   notice, this list of conditions and the following disclaimer. | 
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright | 
|---|
| 12 | *   notice, this list of conditions and the following disclaimer in the | 
|---|
| 13 | *   documentation and/or other materials provided with the distribution. | 
|---|
| 14 | * - The name of the author may not be used to endorse or promote products | 
|---|
| 15 | *   derived from this software without specific prior written permission. | 
|---|
| 16 | * | 
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | 
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** @addtogroup bd | 
|---|
| 30 | * @{ | 
|---|
| 31 | */ | 
|---|
| 32 |  | 
|---|
| 33 | /** | 
|---|
| 34 | * @file | 
|---|
| 35 | * @brief GXemul disk driver | 
|---|
| 36 | */ | 
|---|
| 37 |  | 
|---|
| 38 | #include <stdio.h> | 
|---|
| 39 | #include <libarch/ddi.h> | 
|---|
| 40 | #include <ddi.h> | 
|---|
| 41 | #include <ipc/bd.h> | 
|---|
| 42 | #include <async.h> | 
|---|
| 43 | #include <as.h> | 
|---|
| 44 | #include <fibril_synch.h> | 
|---|
| 45 | #include <devmap.h> | 
|---|
| 46 | #include <sys/types.h> | 
|---|
| 47 | #include <errno.h> | 
|---|
| 48 | #include <macros.h> | 
|---|
| 49 | #include <task.h> | 
|---|
| 50 |  | 
|---|
| 51 | #define NAME       "gxe_bd" | 
|---|
| 52 | #define NAMESPACE  "bd" | 
|---|
| 53 |  | 
|---|
| 54 | enum { | 
|---|
| 55 | CTL_READ_START  = 0, | 
|---|
| 56 | CTL_WRITE_START = 1, | 
|---|
| 57 | }; | 
|---|
| 58 |  | 
|---|
| 59 | enum { | 
|---|
| 60 | STATUS_FAILURE  = 0 | 
|---|
| 61 | }; | 
|---|
| 62 |  | 
|---|
| 63 | enum { | 
|---|
| 64 | MAX_DISKS       = 2 | 
|---|
| 65 | }; | 
|---|
| 66 |  | 
|---|
| 67 | typedef struct { | 
|---|
| 68 | uint32_t offset_lo; | 
|---|
| 69 | uint32_t pad0; | 
|---|
| 70 | uint32_t offset_hi; | 
|---|
| 71 | uint32_t pad1; | 
|---|
| 72 |  | 
|---|
| 73 | uint32_t disk_id; | 
|---|
| 74 | uint32_t pad2[3]; | 
|---|
| 75 |  | 
|---|
| 76 | uint32_t control; | 
|---|
| 77 | uint32_t pad3[3]; | 
|---|
| 78 |  | 
|---|
| 79 | uint32_t status; | 
|---|
| 80 |  | 
|---|
| 81 | uint32_t pad4[3]; | 
|---|
| 82 | uint8_t pad5[0x3fc0]; | 
|---|
| 83 |  | 
|---|
| 84 | uint8_t buffer[512]; | 
|---|
| 85 | } gxe_bd_t; | 
|---|
| 86 |  | 
|---|
| 87 |  | 
|---|
| 88 | static const size_t block_size = 512; | 
|---|
| 89 | static size_t comm_size; | 
|---|
| 90 |  | 
|---|
| 91 | static uintptr_t dev_physical = 0x13000000; | 
|---|
| 92 | static gxe_bd_t *dev; | 
|---|
| 93 |  | 
|---|
| 94 | static devmap_handle_t devmap_handle[MAX_DISKS]; | 
|---|
| 95 |  | 
|---|
| 96 | static fibril_mutex_t dev_lock[MAX_DISKS]; | 
|---|
| 97 |  | 
|---|
| 98 | static int gxe_bd_init(void); | 
|---|
| 99 | static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall); | 
|---|
| 100 | static int gxe_bd_read_blocks(int disk_id, uint64_t ba, unsigned cnt, | 
|---|
| 101 | void *buf); | 
|---|
| 102 | static int gxe_bd_write_blocks(int disk_id, uint64_t ba, unsigned cnt, | 
|---|
| 103 | const void *buf); | 
|---|
| 104 | static int gxe_bd_read_block(int disk_id, uint64_t ba, void *buf); | 
|---|
| 105 | static int gxe_bd_write_block(int disk_id, uint64_t ba, const void *buf); | 
|---|
| 106 |  | 
|---|
| 107 | int main(int argc, char **argv) | 
|---|
| 108 | { | 
|---|
| 109 | printf(NAME ": GXemul disk driver\n"); | 
|---|
| 110 |  | 
|---|
| 111 | if (gxe_bd_init() != EOK) | 
|---|
| 112 | return -1; | 
|---|
| 113 |  | 
|---|
| 114 | printf(NAME ": Accepting connections\n"); | 
|---|
| 115 | task_retval(0); | 
|---|
| 116 | async_manager(); | 
|---|
| 117 |  | 
|---|
| 118 | /* Not reached */ | 
|---|
| 119 | return 0; | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | static int gxe_bd_init(void) | 
|---|
| 123 | { | 
|---|
| 124 | void *vaddr; | 
|---|
| 125 | int rc, i; | 
|---|
| 126 | char name[16]; | 
|---|
| 127 |  | 
|---|
| 128 | rc = devmap_driver_register(NAME, gxe_bd_connection); | 
|---|
| 129 | if (rc < 0) { | 
|---|
| 130 | printf(NAME ": Unable to register driver.\n"); | 
|---|
| 131 | return rc; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | rc = pio_enable((void *) dev_physical, sizeof(gxe_bd_t), &vaddr); | 
|---|
| 135 | if (rc != EOK) { | 
|---|
| 136 | printf(NAME ": Could not initialize device I/O space.\n"); | 
|---|
| 137 | return rc; | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | dev = vaddr; | 
|---|
| 141 |  | 
|---|
| 142 | for (i = 0; i < MAX_DISKS; i++) { | 
|---|
| 143 | snprintf(name, 16, "%s/disk%d", NAMESPACE, i); | 
|---|
| 144 | rc = devmap_device_register(name, &devmap_handle[i]); | 
|---|
| 145 | if (rc != EOK) { | 
|---|
| 146 | printf(NAME ": Unable to register device %s.\n", name); | 
|---|
| 147 | return rc; | 
|---|
| 148 | } | 
|---|
| 149 | fibril_mutex_initialize(&dev_lock[i]); | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | return EOK; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall) | 
|---|
| 156 | { | 
|---|
| 157 | void *fs_va = NULL; | 
|---|
| 158 | ipc_callid_t callid; | 
|---|
| 159 | ipc_call_t call; | 
|---|
| 160 | sysarg_t method; | 
|---|
| 161 | devmap_handle_t dh; | 
|---|
| 162 | unsigned int flags; | 
|---|
| 163 | int retval; | 
|---|
| 164 | uint64_t ba; | 
|---|
| 165 | unsigned cnt; | 
|---|
| 166 | int disk_id, i; | 
|---|
| 167 |  | 
|---|
| 168 | /* Get the device handle. */ | 
|---|
| 169 | dh = IPC_GET_ARG1(*icall); | 
|---|
| 170 |  | 
|---|
| 171 | /* Determine which disk device is the client connecting to. */ | 
|---|
| 172 | disk_id = -1; | 
|---|
| 173 | for (i = 0; i < MAX_DISKS; i++) | 
|---|
| 174 | if (devmap_handle[i] == dh) | 
|---|
| 175 | disk_id = i; | 
|---|
| 176 |  | 
|---|
| 177 | if (disk_id < 0) { | 
|---|
| 178 | async_answer_0(iid, EINVAL); | 
|---|
| 179 | return; | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | /* Answer the IPC_M_CONNECT_ME_TO call. */ | 
|---|
| 183 | async_answer_0(iid, EOK); | 
|---|
| 184 |  | 
|---|
| 185 | if (!async_share_out_receive(&callid, &comm_size, &flags)) { | 
|---|
| 186 | async_answer_0(callid, EHANGUP); | 
|---|
| 187 | return; | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | if (comm_size < block_size) { | 
|---|
| 191 | async_answer_0(callid, EHANGUP); | 
|---|
| 192 | return; | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | fs_va = as_get_mappable_page(comm_size); | 
|---|
| 196 | if (fs_va == NULL) { | 
|---|
| 197 | async_answer_0(callid, EHANGUP); | 
|---|
| 198 | return; | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | (void) async_share_out_finalize(callid, fs_va); | 
|---|
| 202 |  | 
|---|
| 203 | while (1) { | 
|---|
| 204 | callid = async_get_call(&call); | 
|---|
| 205 | method = IPC_GET_IMETHOD(call); | 
|---|
| 206 | switch (method) { | 
|---|
| 207 | case IPC_M_PHONE_HUNGUP: | 
|---|
| 208 | /* The other side has hung up. */ | 
|---|
| 209 | async_answer_0(callid, EOK); | 
|---|
| 210 | return; | 
|---|
| 211 | case BD_READ_BLOCKS: | 
|---|
| 212 | ba = MERGE_LOUP32(IPC_GET_ARG1(call), | 
|---|
| 213 | IPC_GET_ARG2(call)); | 
|---|
| 214 | cnt = IPC_GET_ARG3(call); | 
|---|
| 215 | if (cnt * block_size > comm_size) { | 
|---|
| 216 | retval = ELIMIT; | 
|---|
| 217 | break; | 
|---|
| 218 | } | 
|---|
| 219 | retval = gxe_bd_read_blocks(disk_id, ba, cnt, fs_va); | 
|---|
| 220 | break; | 
|---|
| 221 | case BD_WRITE_BLOCKS: | 
|---|
| 222 | ba = MERGE_LOUP32(IPC_GET_ARG1(call), | 
|---|
| 223 | IPC_GET_ARG2(call)); | 
|---|
| 224 | cnt = IPC_GET_ARG3(call); | 
|---|
| 225 | if (cnt * block_size > comm_size) { | 
|---|
| 226 | retval = ELIMIT; | 
|---|
| 227 | break; | 
|---|
| 228 | } | 
|---|
| 229 | retval = gxe_bd_write_blocks(disk_id, ba, cnt, fs_va); | 
|---|
| 230 | break; | 
|---|
| 231 | case BD_GET_BLOCK_SIZE: | 
|---|
| 232 | async_answer_1(callid, EOK, block_size); | 
|---|
| 233 | continue; | 
|---|
| 234 | case BD_GET_NUM_BLOCKS: | 
|---|
| 235 | retval = ENOTSUP; | 
|---|
| 236 | break; | 
|---|
| 237 | default: | 
|---|
| 238 | retval = EINVAL; | 
|---|
| 239 | break; | 
|---|
| 240 | } | 
|---|
| 241 | async_answer_0(callid, retval); | 
|---|
| 242 | } | 
|---|
| 243 | } | 
|---|
| 244 |  | 
|---|
| 245 | /** Read multiple blocks from the device. */ | 
|---|
| 246 | static int gxe_bd_read_blocks(int disk_id, uint64_t ba, unsigned cnt, | 
|---|
| 247 | void *buf) { | 
|---|
| 248 |  | 
|---|
| 249 | int rc; | 
|---|
| 250 |  | 
|---|
| 251 | while (cnt > 0) { | 
|---|
| 252 | rc = gxe_bd_read_block(disk_id, ba, buf); | 
|---|
| 253 | if (rc != EOK) | 
|---|
| 254 | return rc; | 
|---|
| 255 |  | 
|---|
| 256 | ++ba; | 
|---|
| 257 | --cnt; | 
|---|
| 258 | buf += block_size; | 
|---|
| 259 | } | 
|---|
| 260 |  | 
|---|
| 261 | return EOK; | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 | /** Write multiple blocks to the device. */ | 
|---|
| 265 | static int gxe_bd_write_blocks(int disk_id, uint64_t ba, unsigned cnt, | 
|---|
| 266 | const void *buf) { | 
|---|
| 267 |  | 
|---|
| 268 | int rc; | 
|---|
| 269 |  | 
|---|
| 270 | while (cnt > 0) { | 
|---|
| 271 | rc = gxe_bd_write_block(disk_id, ba, buf); | 
|---|
| 272 | if (rc != EOK) | 
|---|
| 273 | return rc; | 
|---|
| 274 |  | 
|---|
| 275 | ++ba; | 
|---|
| 276 | --cnt; | 
|---|
| 277 | buf += block_size; | 
|---|
| 278 | } | 
|---|
| 279 |  | 
|---|
| 280 | return EOK; | 
|---|
| 281 | } | 
|---|
| 282 |  | 
|---|
| 283 | /** Read a block from the device. */ | 
|---|
| 284 | static int gxe_bd_read_block(int disk_id, uint64_t ba, void *buf) | 
|---|
| 285 | { | 
|---|
| 286 | uint32_t status; | 
|---|
| 287 | uint64_t byte_addr; | 
|---|
| 288 | size_t i; | 
|---|
| 289 | uint32_t w; | 
|---|
| 290 |  | 
|---|
| 291 | byte_addr = ba * block_size; | 
|---|
| 292 |  | 
|---|
| 293 | fibril_mutex_lock(&dev_lock[disk_id]); | 
|---|
| 294 | pio_write_32(&dev->offset_lo, (uint32_t) byte_addr); | 
|---|
| 295 | pio_write_32(&dev->offset_hi, byte_addr >> 32); | 
|---|
| 296 | pio_write_32(&dev->disk_id, disk_id); | 
|---|
| 297 | pio_write_32(&dev->control, CTL_READ_START); | 
|---|
| 298 |  | 
|---|
| 299 | status = pio_read_32(&dev->status); | 
|---|
| 300 | if (status == STATUS_FAILURE) { | 
|---|
| 301 | fibril_mutex_unlock(&dev_lock[disk_id]); | 
|---|
| 302 | return EIO; | 
|---|
| 303 | } | 
|---|
| 304 |  | 
|---|
| 305 | for (i = 0; i < block_size; i++) { | 
|---|
| 306 | ((uint8_t *) buf)[i] = w = pio_read_8(&dev->buffer[i]); | 
|---|
| 307 | } | 
|---|
| 308 |  | 
|---|
| 309 | fibril_mutex_unlock(&dev_lock[disk_id]); | 
|---|
| 310 | return EOK; | 
|---|
| 311 | } | 
|---|
| 312 |  | 
|---|
| 313 | /** Write a block to the device. */ | 
|---|
| 314 | static int gxe_bd_write_block(int disk_id, uint64_t ba, const void *buf) | 
|---|
| 315 | { | 
|---|
| 316 | uint32_t status; | 
|---|
| 317 | uint64_t byte_addr; | 
|---|
| 318 | size_t i; | 
|---|
| 319 |  | 
|---|
| 320 | byte_addr = ba * block_size; | 
|---|
| 321 |  | 
|---|
| 322 | fibril_mutex_lock(&dev_lock[disk_id]); | 
|---|
| 323 |  | 
|---|
| 324 | for (i = 0; i < block_size; i++) { | 
|---|
| 325 | pio_write_8(&dev->buffer[i], ((const uint8_t *) buf)[i]); | 
|---|
| 326 | } | 
|---|
| 327 |  | 
|---|
| 328 | pio_write_32(&dev->offset_lo, (uint32_t) byte_addr); | 
|---|
| 329 | pio_write_32(&dev->offset_hi, byte_addr >> 32); | 
|---|
| 330 | pio_write_32(&dev->disk_id, disk_id); | 
|---|
| 331 | pio_write_32(&dev->control, CTL_WRITE_START); | 
|---|
| 332 |  | 
|---|
| 333 | status = pio_read_32(&dev->status); | 
|---|
| 334 | if (status == STATUS_FAILURE) { | 
|---|
| 335 | fibril_mutex_unlock(&dev_lock[disk_id]); | 
|---|
| 336 | return EIO; | 
|---|
| 337 | } | 
|---|
| 338 |  | 
|---|
| 339 | fibril_mutex_unlock(&dev_lock[disk_id]); | 
|---|
| 340 | return EOK; | 
|---|
| 341 | } | 
|---|
| 342 |  | 
|---|
| 343 | /** | 
|---|
| 344 | * @} | 
|---|
| 345 | */ | 
|---|