| 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 <loc.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 service_id_t service_id[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, void *);
|
|---|
| 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 | async_set_client_connection(gxe_bd_connection);
|
|---|
| 125 | int rc = loc_server_register(NAME);
|
|---|
| 126 | if (rc != EOK) {
|
|---|
| 127 | printf("%s: Unable to register driver.\n", NAME);
|
|---|
| 128 | return rc;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | void *vaddr;
|
|---|
| 132 | rc = pio_enable((void *) dev_physical, sizeof(gxe_bd_t), &vaddr);
|
|---|
| 133 | if (rc != EOK) {
|
|---|
| 134 | printf("%s: Could not initialize device I/O space.\n", NAME);
|
|---|
| 135 | return rc;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | dev = vaddr;
|
|---|
| 139 |
|
|---|
| 140 | for (unsigned int i = 0; i < MAX_DISKS; i++) {
|
|---|
| 141 | char name[16];
|
|---|
| 142 |
|
|---|
| 143 | snprintf(name, 16, "%s/disk%u", NAMESPACE, i);
|
|---|
| 144 | rc = loc_service_register(name, &service_id[i]);
|
|---|
| 145 | if (rc != EOK) {
|
|---|
| 146 | printf("%s: Unable to register device %s.\n", NAME,
|
|---|
| 147 | name);
|
|---|
| 148 | return rc;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | fibril_mutex_initialize(&dev_lock[i]);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | return EOK;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | static void gxe_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
|---|
| 158 | {
|
|---|
| 159 | void *fs_va = NULL;
|
|---|
| 160 | ipc_callid_t callid;
|
|---|
| 161 | ipc_call_t call;
|
|---|
| 162 | sysarg_t method;
|
|---|
| 163 | service_id_t dsid;
|
|---|
| 164 | unsigned int flags;
|
|---|
| 165 | int retval;
|
|---|
| 166 | uint64_t ba;
|
|---|
| 167 | unsigned cnt;
|
|---|
| 168 | int disk_id, i;
|
|---|
| 169 |
|
|---|
| 170 | /* Get the device handle. */
|
|---|
| 171 | dsid = IPC_GET_ARG1(*icall);
|
|---|
| 172 |
|
|---|
| 173 | /* Determine which disk device is the client connecting to. */
|
|---|
| 174 | disk_id = -1;
|
|---|
| 175 | for (i = 0; i < MAX_DISKS; i++)
|
|---|
| 176 | if (service_id[i] == dsid)
|
|---|
| 177 | disk_id = i;
|
|---|
| 178 |
|
|---|
| 179 | if (disk_id < 0) {
|
|---|
| 180 | async_answer_0(iid, EINVAL);
|
|---|
| 181 | return;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | /* Answer the IPC_M_CONNECT_ME_TO call. */
|
|---|
| 185 | async_answer_0(iid, EOK);
|
|---|
| 186 |
|
|---|
| 187 | if (!async_share_out_receive(&callid, &comm_size, &flags)) {
|
|---|
| 188 | async_answer_0(callid, EHANGUP);
|
|---|
| 189 | return;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | if (comm_size < block_size) {
|
|---|
| 193 | async_answer_0(callid, EHANGUP);
|
|---|
| 194 | return;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | (void) async_share_out_finalize(callid, &fs_va);
|
|---|
| 198 | if (fs_va == (void *) -1) {
|
|---|
| 199 | async_answer_0(callid, EHANGUP);
|
|---|
| 200 | return;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | while (true) {
|
|---|
| 204 | callid = async_get_call(&call);
|
|---|
| 205 | method = IPC_GET_IMETHOD(call);
|
|---|
| 206 |
|
|---|
| 207 | if (!method) {
|
|---|
| 208 | /* The other side has hung up. */
|
|---|
| 209 | async_answer_0(callid, EOK);
|
|---|
| 210 | return;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | switch (method) {
|
|---|
| 214 | case BD_READ_BLOCKS:
|
|---|
| 215 | ba = MERGE_LOUP32(IPC_GET_ARG1(call),
|
|---|
| 216 | IPC_GET_ARG2(call));
|
|---|
| 217 | cnt = IPC_GET_ARG3(call);
|
|---|
| 218 | if (cnt * block_size > comm_size) {
|
|---|
| 219 | retval = ELIMIT;
|
|---|
| 220 | break;
|
|---|
| 221 | }
|
|---|
| 222 | retval = gxe_bd_read_blocks(disk_id, ba, cnt, fs_va);
|
|---|
| 223 | break;
|
|---|
| 224 | case BD_WRITE_BLOCKS:
|
|---|
| 225 | ba = MERGE_LOUP32(IPC_GET_ARG1(call),
|
|---|
| 226 | IPC_GET_ARG2(call));
|
|---|
| 227 | cnt = IPC_GET_ARG3(call);
|
|---|
| 228 | if (cnt * block_size > comm_size) {
|
|---|
| 229 | retval = ELIMIT;
|
|---|
| 230 | break;
|
|---|
| 231 | }
|
|---|
| 232 | retval = gxe_bd_write_blocks(disk_id, ba, cnt, fs_va);
|
|---|
| 233 | break;
|
|---|
| 234 | case BD_GET_BLOCK_SIZE:
|
|---|
| 235 | async_answer_1(callid, EOK, block_size);
|
|---|
| 236 | continue;
|
|---|
| 237 | case BD_GET_NUM_BLOCKS:
|
|---|
| 238 | retval = ENOTSUP;
|
|---|
| 239 | break;
|
|---|
| 240 | default:
|
|---|
| 241 | retval = EINVAL;
|
|---|
| 242 | break;
|
|---|
| 243 | }
|
|---|
| 244 | async_answer_0(callid, retval);
|
|---|
| 245 | }
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | /** Read multiple blocks from the device. */
|
|---|
| 249 | static int gxe_bd_read_blocks(int disk_id, uint64_t ba, unsigned cnt,
|
|---|
| 250 | void *buf) {
|
|---|
| 251 |
|
|---|
| 252 | int rc;
|
|---|
| 253 |
|
|---|
| 254 | while (cnt > 0) {
|
|---|
| 255 | rc = gxe_bd_read_block(disk_id, ba, buf);
|
|---|
| 256 | if (rc != EOK)
|
|---|
| 257 | return rc;
|
|---|
| 258 |
|
|---|
| 259 | ++ba;
|
|---|
| 260 | --cnt;
|
|---|
| 261 | buf += block_size;
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | return EOK;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | /** Write multiple blocks to the device. */
|
|---|
| 268 | static int gxe_bd_write_blocks(int disk_id, uint64_t ba, unsigned cnt,
|
|---|
| 269 | const void *buf) {
|
|---|
| 270 |
|
|---|
| 271 | int rc;
|
|---|
| 272 |
|
|---|
| 273 | while (cnt > 0) {
|
|---|
| 274 | rc = gxe_bd_write_block(disk_id, ba, buf);
|
|---|
| 275 | if (rc != EOK)
|
|---|
| 276 | return rc;
|
|---|
| 277 |
|
|---|
| 278 | ++ba;
|
|---|
| 279 | --cnt;
|
|---|
| 280 | buf += block_size;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | return EOK;
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | /** Read a block from the device. */
|
|---|
| 287 | static int gxe_bd_read_block(int disk_id, uint64_t ba, void *buf)
|
|---|
| 288 | {
|
|---|
| 289 | uint32_t status;
|
|---|
| 290 | uint64_t byte_addr;
|
|---|
| 291 | size_t i;
|
|---|
| 292 | uint32_t w;
|
|---|
| 293 |
|
|---|
| 294 | byte_addr = ba * block_size;
|
|---|
| 295 |
|
|---|
| 296 | fibril_mutex_lock(&dev_lock[disk_id]);
|
|---|
| 297 | pio_write_32(&dev->offset_lo, (uint32_t) byte_addr);
|
|---|
| 298 | pio_write_32(&dev->offset_hi, byte_addr >> 32);
|
|---|
| 299 | pio_write_32(&dev->disk_id, disk_id);
|
|---|
| 300 | pio_write_32(&dev->control, CTL_READ_START);
|
|---|
| 301 |
|
|---|
| 302 | status = pio_read_32(&dev->status);
|
|---|
| 303 | if (status == STATUS_FAILURE) {
|
|---|
| 304 | fibril_mutex_unlock(&dev_lock[disk_id]);
|
|---|
| 305 | return EIO;
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | for (i = 0; i < block_size; i++) {
|
|---|
| 309 | ((uint8_t *) buf)[i] = w = pio_read_8(&dev->buffer[i]);
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | fibril_mutex_unlock(&dev_lock[disk_id]);
|
|---|
| 313 | return EOK;
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | /** Write a block to the device. */
|
|---|
| 317 | static int gxe_bd_write_block(int disk_id, uint64_t ba, const void *buf)
|
|---|
| 318 | {
|
|---|
| 319 | uint32_t status;
|
|---|
| 320 | uint64_t byte_addr;
|
|---|
| 321 | size_t i;
|
|---|
| 322 |
|
|---|
| 323 | byte_addr = ba * block_size;
|
|---|
| 324 |
|
|---|
| 325 | fibril_mutex_lock(&dev_lock[disk_id]);
|
|---|
| 326 |
|
|---|
| 327 | for (i = 0; i < block_size; i++) {
|
|---|
| 328 | pio_write_8(&dev->buffer[i], ((const uint8_t *) buf)[i]);
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | pio_write_32(&dev->offset_lo, (uint32_t) byte_addr);
|
|---|
| 332 | pio_write_32(&dev->offset_hi, byte_addr >> 32);
|
|---|
| 333 | pio_write_32(&dev->disk_id, disk_id);
|
|---|
| 334 | pio_write_32(&dev->control, CTL_WRITE_START);
|
|---|
| 335 |
|
|---|
| 336 | status = pio_read_32(&dev->status);
|
|---|
| 337 | if (status == STATUS_FAILURE) {
|
|---|
| 338 | fibril_mutex_unlock(&dev_lock[disk_id]);
|
|---|
| 339 | return EIO;
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | fibril_mutex_unlock(&dev_lock[disk_id]);
|
|---|
| 343 | return EOK;
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | /**
|
|---|
| 347 | * @}
|
|---|
| 348 | */
|
|---|