[f2e78b0] | 1 | /*
|
---|
| 2 | * Copyright (c) 2015 Jakub Jermar
|
---|
| 3 | * Copyright (c) 2013 Jiri Svoboda
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @file
|
---|
| 31 | * MSIM ddisk block device driver
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | #include <assert.h>
|
---|
| 35 | #include <stdio.h>
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <str_error.h>
|
---|
| 38 | #include <ddf/driver.h>
|
---|
[0eacc32] | 39 | #include <ddf/interrupt.h>
|
---|
[f2e78b0] | 40 | #include <ddf/log.h>
|
---|
| 41 | #include <device/hw_res_parsed.h>
|
---|
| 42 | #include <ddi.h>
|
---|
| 43 | #include <bd_srv.h>
|
---|
| 44 | #include <fibril_synch.h>
|
---|
| 45 | #include <as.h>
|
---|
| 46 |
|
---|
| 47 | #define NAME "ddisk"
|
---|
| 48 |
|
---|
| 49 | #define DDISK_FUN_NAME "a"
|
---|
| 50 |
|
---|
| 51 | #define DDISK_BLOCK_SIZE 512
|
---|
| 52 |
|
---|
[0eacc32] | 53 | #define DDISK_STAT_IRQ_PENDING 0x4
|
---|
| 54 | #define DDISK_CMD_READ 0x1
|
---|
| 55 | #define DDISK_CMD_WRITE 0x2
|
---|
| 56 | #define DDISK_CMD_IRQ_DEASSERT 0x4
|
---|
| 57 |
|
---|
[b7fd2a0] | 58 | static errno_t ddisk_dev_add(ddf_dev_t *);
|
---|
| 59 | static errno_t ddisk_dev_remove(ddf_dev_t *);
|
---|
| 60 | static errno_t ddisk_dev_gone(ddf_dev_t *);
|
---|
| 61 | static errno_t ddisk_fun_online(ddf_fun_t *);
|
---|
| 62 | static errno_t ddisk_fun_offline(ddf_fun_t *);
|
---|
[f2e78b0] | 63 |
|
---|
[984a9ba] | 64 | static void ddisk_bd_connection(ipc_call_t *, void *);
|
---|
[f2e78b0] | 65 |
|
---|
[01c3bb4] | 66 | static void ddisk_irq_handler(ipc_call_t *, ddf_dev_t *);
|
---|
[0eacc32] | 67 |
|
---|
[f2e78b0] | 68 | static driver_ops_t driver_ops = {
|
---|
| 69 | .dev_add = ddisk_dev_add,
|
---|
| 70 | .dev_remove = ddisk_dev_remove,
|
---|
| 71 | .dev_gone = ddisk_dev_gone,
|
---|
| 72 | .fun_online = ddisk_fun_online,
|
---|
| 73 | .fun_offline = ddisk_fun_offline
|
---|
| 74 | };
|
---|
| 75 |
|
---|
| 76 | static driver_t ddisk_driver = {
|
---|
| 77 | .name = NAME,
|
---|
| 78 | .driver_ops = &driver_ops
|
---|
| 79 | };
|
---|
| 80 |
|
---|
| 81 | typedef struct {
|
---|
| 82 | int irq;
|
---|
| 83 | uintptr_t base;
|
---|
| 84 | } ddisk_res_t;
|
---|
| 85 |
|
---|
| 86 | typedef struct {
|
---|
| 87 | ioport32_t dma_buffer;
|
---|
| 88 | ioport32_t block;
|
---|
| 89 | union {
|
---|
| 90 | ioport32_t status;
|
---|
| 91 | ioport32_t command;
|
---|
| 92 | };
|
---|
| 93 | ioport32_t size;
|
---|
| 94 | } ddisk_regs_t;
|
---|
| 95 |
|
---|
[7b084d3] | 96 | typedef struct {
|
---|
[f2e78b0] | 97 | fibril_mutex_t lock;
|
---|
[c4f32fff] | 98 |
|
---|
[0eacc32] | 99 | fibril_condvar_t io_cv;
|
---|
| 100 | bool io_busy;
|
---|
[c4f32fff] | 101 |
|
---|
| 102 | ssize_t size;
|
---|
| 103 | size_t blocks;
|
---|
| 104 |
|
---|
| 105 | uintptr_t dma_buffer_phys;
|
---|
| 106 | void *dma_buffer;
|
---|
| 107 |
|
---|
[f2e78b0] | 108 | ddf_dev_t *dev;
|
---|
[7b084d3] | 109 | ddf_fun_t *fun;
|
---|
[c4f32fff] | 110 |
|
---|
[f2e78b0] | 111 | ddisk_res_t ddisk_res;
|
---|
| 112 | ddisk_regs_t *ddisk_regs;
|
---|
[c4f32fff] | 113 |
|
---|
[eadaeae8] | 114 | cap_irq_handle_t irq_cap;
|
---|
[e9d15d9] | 115 |
|
---|
[7b084d3] | 116 | bd_srvs_t bds;
|
---|
[42d0eeb6] | 117 | } ddisk_t;
|
---|
[f2e78b0] | 118 |
|
---|
[b7fd2a0] | 119 | static errno_t ddisk_bd_open(bd_srvs_t *, bd_srv_t *);
|
---|
| 120 | static errno_t ddisk_bd_close(bd_srv_t *);
|
---|
| 121 | static errno_t ddisk_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
|
---|
| 122 | static errno_t ddisk_bd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *,
|
---|
[f2e78b0] | 123 | size_t);
|
---|
[b7fd2a0] | 124 | static errno_t ddisk_bd_get_block_size(bd_srv_t *, size_t *);
|
---|
| 125 | static errno_t ddisk_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
|
---|
[f2e78b0] | 126 |
|
---|
| 127 | bd_ops_t ddisk_bd_ops = {
|
---|
| 128 | .open = ddisk_bd_open,
|
---|
| 129 | .close = ddisk_bd_close,
|
---|
| 130 | .read_blocks = ddisk_bd_read_blocks,
|
---|
| 131 | .write_blocks = ddisk_bd_write_blocks,
|
---|
| 132 | .get_block_size = ddisk_bd_get_block_size,
|
---|
| 133 | .get_num_blocks = ddisk_bd_get_num_blocks,
|
---|
| 134 | };
|
---|
| 135 |
|
---|
[0eacc32] | 136 | irq_pio_range_t ddisk_irq_pio_ranges[] = {
|
---|
| 137 | {
|
---|
| 138 | .base = 0,
|
---|
| 139 | .size = sizeof(ddisk_regs_t)
|
---|
| 140 | }
|
---|
| 141 | };
|
---|
| 142 |
|
---|
| 143 | irq_cmd_t ddisk_irq_commands[] = {
|
---|
| 144 | {
|
---|
| 145 | .cmd = CMD_PIO_READ_32,
|
---|
| 146 | .addr = NULL,
|
---|
| 147 | .dstarg = 1
|
---|
| 148 | },
|
---|
| 149 | {
|
---|
| 150 | .cmd = CMD_AND,
|
---|
| 151 | .srcarg = 1,
|
---|
| 152 | .value = DDISK_STAT_IRQ_PENDING,
|
---|
| 153 | .dstarg = 2
|
---|
| 154 | },
|
---|
| 155 | {
|
---|
| 156 | .cmd = CMD_PREDICATE,
|
---|
| 157 | .srcarg = 2,
|
---|
[1b20da0] | 158 | .value = 2
|
---|
[0eacc32] | 159 | },
|
---|
| 160 | {
|
---|
| 161 | /* Deassert the DMA interrupt. */
|
---|
| 162 | .cmd = CMD_PIO_WRITE_32,
|
---|
| 163 | .value = DDISK_CMD_IRQ_DEASSERT,
|
---|
[1b20da0] | 164 | .addr = NULL
|
---|
[0eacc32] | 165 | },
|
---|
| 166 | {
|
---|
| 167 | .cmd = CMD_ACCEPT
|
---|
| 168 | }
|
---|
| 169 | };
|
---|
| 170 |
|
---|
| 171 | irq_code_t ddisk_irq_code = {
|
---|
| 172 | .rangecount = 1,
|
---|
| 173 | .ranges = ddisk_irq_pio_ranges,
|
---|
| 174 | .cmdcount = sizeof(ddisk_irq_commands) / sizeof(irq_cmd_t),
|
---|
| 175 | .cmds = ddisk_irq_commands,
|
---|
| 176 | };
|
---|
| 177 |
|
---|
[01c3bb4] | 178 | void ddisk_irq_handler(ipc_call_t *icall, ddf_dev_t *dev)
|
---|
[0eacc32] | 179 | {
|
---|
[3f04c28] | 180 | ddf_msg(LVL_DEBUG, "ddisk_irq_handler(), status=%" PRIx32,
|
---|
[0eacc32] | 181 | (uint32_t) IPC_GET_ARG1(*icall));
|
---|
| 182 |
|
---|
[42d0eeb6] | 183 | ddisk_t *ddisk = (ddisk_t *) ddf_dev_data_get(dev);
|
---|
[a35b458] | 184 |
|
---|
[42d0eeb6] | 185 | fibril_mutex_lock(&ddisk->lock);
|
---|
| 186 | fibril_condvar_broadcast(&ddisk->io_cv);
|
---|
| 187 | fibril_mutex_unlock(&ddisk->lock);
|
---|
[0eacc32] | 188 | }
|
---|
| 189 |
|
---|
[b7fd2a0] | 190 | errno_t ddisk_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
---|
[f2e78b0] | 191 | {
|
---|
| 192 | return EOK;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[b7fd2a0] | 195 | errno_t ddisk_bd_close(bd_srv_t *bd)
|
---|
[f2e78b0] | 196 | {
|
---|
| 197 | return EOK;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[18b6a88] | 200 | static errno_t ddisk_rw_block(ddisk_t *ddisk, bool read, aoff64_t ba, void *buf)
|
---|
[f2e78b0] | 201 | {
|
---|
[42d0eeb6] | 202 | fibril_mutex_lock(&ddisk->lock);
|
---|
[0eacc32] | 203 |
|
---|
[3f04c28] | 204 | ddf_msg(LVL_DEBUG, "ddisk_rw_block(): read=%d, ba=%" PRId64 ", buf=%p",
|
---|
[0eacc32] | 205 | read, ba, buf);
|
---|
| 206 |
|
---|
[42d0eeb6] | 207 | if (ba >= ddisk->blocks)
|
---|
[0eacc32] | 208 | return ELIMIT;
|
---|
| 209 |
|
---|
[42d0eeb6] | 210 | while (ddisk->io_busy)
|
---|
| 211 | fibril_condvar_wait(&ddisk->io_cv, &ddisk->lock);
|
---|
[0eacc32] | 212 |
|
---|
[42d0eeb6] | 213 | ddisk->io_busy = true;
|
---|
[0eacc32] | 214 |
|
---|
| 215 | if (!read)
|
---|
[42d0eeb6] | 216 | memcpy(ddisk->dma_buffer, buf, DDISK_BLOCK_SIZE);
|
---|
[a35b458] | 217 |
|
---|
[42d0eeb6] | 218 | pio_write_32(&ddisk->ddisk_regs->dma_buffer,
|
---|
| 219 | ddisk->dma_buffer_phys);
|
---|
| 220 | pio_write_32(&ddisk->ddisk_regs->block, (uint32_t) ba);
|
---|
| 221 | pio_write_32(&ddisk->ddisk_regs->command,
|
---|
[0eacc32] | 222 | read ? DDISK_CMD_READ : DDISK_CMD_WRITE);
|
---|
| 223 |
|
---|
[42d0eeb6] | 224 | fibril_condvar_wait(&ddisk->io_cv, &ddisk->lock);
|
---|
[f2e78b0] | 225 |
|
---|
[0eacc32] | 226 | if (read)
|
---|
[42d0eeb6] | 227 | memcpy(buf, ddisk->dma_buffer, DDISK_BLOCK_SIZE);
|
---|
[0eacc32] | 228 |
|
---|
[42d0eeb6] | 229 | ddisk->io_busy = false;
|
---|
| 230 | fibril_condvar_signal(&ddisk->io_cv);
|
---|
| 231 | fibril_mutex_unlock(&ddisk->lock);
|
---|
[0eacc32] | 232 |
|
---|
| 233 | return EOK;
|
---|
[f2e78b0] | 234 | }
|
---|
| 235 |
|
---|
[18b6a88] | 236 | static errno_t ddisk_bd_rw_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
| 237 | void *buf, size_t size, bool is_read)
|
---|
[f2e78b0] | 238 | {
|
---|
[42d0eeb6] | 239 | ddisk_t *ddisk = (ddisk_t *) bd->srvs->sarg;
|
---|
[0eacc32] | 240 | aoff64_t i;
|
---|
[b7fd2a0] | 241 | errno_t rc;
|
---|
[0eacc32] | 242 |
|
---|
[f2e78b0] | 243 | if (size < cnt * DDISK_BLOCK_SIZE)
|
---|
[1b20da0] | 244 | return EINVAL;
|
---|
[f2e78b0] | 245 |
|
---|
[0eacc32] | 246 | for (i = 0; i < cnt; i++) {
|
---|
[42d0eeb6] | 247 | rc = ddisk_rw_block(ddisk, is_read, ba + i,
|
---|
[0eacc32] | 248 | buf + i * DDISK_BLOCK_SIZE);
|
---|
| 249 | if (rc != EOK)
|
---|
| 250 | return rc;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | return EOK;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[b7fd2a0] | 256 | errno_t ddisk_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt, void *buf,
|
---|
[0eacc32] | 257 | size_t size)
|
---|
| 258 | {
|
---|
| 259 | return ddisk_bd_rw_blocks(bd, ba, cnt, buf, size, true);
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[b7fd2a0] | 262 | errno_t ddisk_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
---|
[0eacc32] | 263 | const void *buf, size_t size)
|
---|
| 264 | {
|
---|
| 265 | return ddisk_bd_rw_blocks(bd, ba, cnt, (void *) buf, size, false);
|
---|
[f2e78b0] | 266 | }
|
---|
| 267 |
|
---|
[b7fd2a0] | 268 | errno_t ddisk_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
|
---|
[f2e78b0] | 269 | {
|
---|
[1b20da0] | 270 | *rsize = DDISK_BLOCK_SIZE;
|
---|
[f2e78b0] | 271 | return EOK;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[b7fd2a0] | 274 | errno_t ddisk_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
|
---|
[f2e78b0] | 275 | {
|
---|
[42d0eeb6] | 276 | ddisk_t *ddisk = (ddisk_t *) bd->srvs->sarg;
|
---|
[f2e78b0] | 277 |
|
---|
[42d0eeb6] | 278 | *rnb = ddisk->blocks;
|
---|
[1b20da0] | 279 | return EOK;
|
---|
[f2e78b0] | 280 | }
|
---|
| 281 |
|
---|
[b7fd2a0] | 282 | static errno_t ddisk_get_res(ddf_dev_t *dev, ddisk_res_t *ddisk_res)
|
---|
[f2e78b0] | 283 | {
|
---|
| 284 | async_sess_t *parent_sess;
|
---|
| 285 | hw_res_list_parsed_t hw_res;
|
---|
[b7fd2a0] | 286 | errno_t rc;
|
---|
[f2e78b0] | 287 |
|
---|
[2fd26bb] | 288 | parent_sess = ddf_dev_parent_sess_get(dev);
|
---|
[f2e78b0] | 289 | if (parent_sess == NULL)
|
---|
| 290 | return ENOMEM;
|
---|
| 291 |
|
---|
| 292 | hw_res_list_parsed_init(&hw_res);
|
---|
| 293 | rc = hw_res_get_list_parsed(parent_sess, &hw_res, 0);
|
---|
| 294 | if (rc != EOK)
|
---|
| 295 | return rc;
|
---|
| 296 |
|
---|
| 297 | if ((hw_res.mem_ranges.count != 1) || (hw_res.irqs.count != 1)) {
|
---|
| 298 | rc = EINVAL;
|
---|
| 299 | goto error;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | addr_range_t *regs = &hw_res.mem_ranges.ranges[0];
|
---|
| 303 | ddisk_res->base = RNGABS(*regs);
|
---|
[1b20da0] | 304 | ddisk_res->irq = hw_res.irqs.irqs[0];
|
---|
[f2e78b0] | 305 |
|
---|
| 306 | if (RNGSZ(*regs) < sizeof(ddisk_regs_t)) {
|
---|
| 307 | rc = EINVAL;
|
---|
| 308 | goto error;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | rc = EOK;
|
---|
| 312 | error:
|
---|
| 313 | hw_res_list_parsed_clean(&hw_res);
|
---|
| 314 | return rc;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
[b7fd2a0] | 317 | static errno_t ddisk_fun_create(ddisk_t *ddisk)
|
---|
[f2e78b0] | 318 | {
|
---|
[b7fd2a0] | 319 | errno_t rc;
|
---|
[f2e78b0] | 320 | ddf_fun_t *fun = NULL;
|
---|
[4f87a85a] | 321 | bool bound = false;
|
---|
[f2e78b0] | 322 |
|
---|
[42d0eeb6] | 323 | fun = ddf_fun_create(ddisk->dev, fun_exposed, DDISK_FUN_NAME);
|
---|
[f2e78b0] | 324 | if (fun == NULL) {
|
---|
| 325 | ddf_msg(LVL_ERROR, "Failed creating DDF function.");
|
---|
| 326 | rc = ENOMEM;
|
---|
| 327 | goto error;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | /* Set up a connection handler. */
|
---|
| 331 | ddf_fun_set_conn_handler(fun, ddisk_bd_connection);
|
---|
| 332 |
|
---|
| 333 | rc = ddf_fun_bind(fun);
|
---|
| 334 | if (rc != EOK) {
|
---|
| 335 | ddf_msg(LVL_ERROR, "Failed binding DDF function %s: %s",
|
---|
| 336 | DDISK_FUN_NAME, str_error(rc));
|
---|
| 337 | goto error;
|
---|
| 338 | }
|
---|
| 339 |
|
---|
[4f87a85a] | 340 | bound = true;
|
---|
| 341 |
|
---|
| 342 | rc = ddf_fun_add_to_category(fun, "disk");
|
---|
| 343 | if (rc != EOK) {
|
---|
| 344 | ddf_msg(LVL_ERROR, "Failed adding function %s to category "
|
---|
| 345 | "'disk': %s.\n", DDISK_FUN_NAME, str_error(rc));
|
---|
| 346 | goto error;
|
---|
| 347 | }
|
---|
[42d0eeb6] | 348 | ddisk->fun = fun;
|
---|
[f2e78b0] | 349 |
|
---|
| 350 | return EOK;
|
---|
| 351 | error:
|
---|
[4f87a85a] | 352 | if (bound)
|
---|
| 353 | ddf_fun_unbind(fun);
|
---|
[f2e78b0] | 354 | if (fun != NULL)
|
---|
| 355 | ddf_fun_destroy(fun);
|
---|
| 356 |
|
---|
| 357 | return rc;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
[b7fd2a0] | 360 | static errno_t ddisk_fun_remove(ddisk_t *ddisk)
|
---|
[f2e78b0] | 361 | {
|
---|
[b7fd2a0] | 362 | errno_t rc;
|
---|
[f2e78b0] | 363 |
|
---|
[42d0eeb6] | 364 | if (ddisk->fun == NULL)
|
---|
[f2e78b0] | 365 | return EOK;
|
---|
| 366 |
|
---|
[42d0eeb6] | 367 | ddf_msg(LVL_DEBUG, "ddisk_fun_remove(%p, '%s')", ddisk,
|
---|
[f2e78b0] | 368 | DDISK_FUN_NAME);
|
---|
[42d0eeb6] | 369 | rc = ddf_fun_offline(ddisk->fun);
|
---|
[f2e78b0] | 370 | if (rc != EOK) {
|
---|
| 371 | ddf_msg(LVL_ERROR, "Error offlining function '%s'.",
|
---|
| 372 | DDISK_FUN_NAME);
|
---|
| 373 | goto error;
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[42d0eeb6] | 376 | rc = ddf_fun_unbind(ddisk->fun);
|
---|
[f2e78b0] | 377 | if (rc != EOK) {
|
---|
| 378 | ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.",
|
---|
| 379 | DDISK_FUN_NAME);
|
---|
| 380 | goto error;
|
---|
| 381 | }
|
---|
| 382 |
|
---|
[42d0eeb6] | 383 | ddf_fun_destroy(ddisk->fun);
|
---|
| 384 | ddisk->fun = NULL;
|
---|
[f2e78b0] | 385 | rc = EOK;
|
---|
[c4f32fff] | 386 |
|
---|
[f2e78b0] | 387 | error:
|
---|
| 388 | return rc;
|
---|
| 389 | }
|
---|
| 390 |
|
---|
[b7fd2a0] | 391 | static errno_t ddisk_fun_unbind(ddisk_t *ddisk)
|
---|
[f2e78b0] | 392 | {
|
---|
[b7fd2a0] | 393 | errno_t rc;
|
---|
[f2e78b0] | 394 |
|
---|
[42d0eeb6] | 395 | if (ddisk->fun == NULL)
|
---|
[f2e78b0] | 396 | return EOK;
|
---|
| 397 |
|
---|
[42d0eeb6] | 398 | ddf_msg(LVL_DEBUG, "ddisk_fun_unbind(%p, '%s')", ddisk,
|
---|
[f2e78b0] | 399 | DDISK_FUN_NAME);
|
---|
[42d0eeb6] | 400 | rc = ddf_fun_unbind(ddisk->fun);
|
---|
[f2e78b0] | 401 | if (rc != EOK) {
|
---|
| 402 | ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.",
|
---|
| 403 | DDISK_FUN_NAME);
|
---|
| 404 | goto error;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
[42d0eeb6] | 407 | ddf_fun_destroy(ddisk->fun);
|
---|
| 408 | ddisk->fun = NULL;
|
---|
[f2e78b0] | 409 | rc = EOK;
|
---|
[7b084d3] | 410 |
|
---|
[f2e78b0] | 411 | error:
|
---|
| 412 | return rc;
|
---|
| 413 | }
|
---|
| 414 |
|
---|
| 415 | /** Add new device
|
---|
| 416 | *
|
---|
| 417 | * @param dev New device
|
---|
[cde999a] | 418 | * @return EOK on success or an error code.
|
---|
[f2e78b0] | 419 | */
|
---|
[b7fd2a0] | 420 | static errno_t ddisk_dev_add(ddf_dev_t *dev)
|
---|
[f2e78b0] | 421 | {
|
---|
[42d0eeb6] | 422 | ddisk_t *ddisk;
|
---|
[f2e78b0] | 423 | ddisk_res_t res;
|
---|
[b7fd2a0] | 424 | errno_t rc;
|
---|
[f2e78b0] | 425 |
|
---|
[c4f32fff] | 426 | /*
|
---|
| 427 | * Get our resources.
|
---|
| 428 | */
|
---|
[f2e78b0] | 429 | rc = ddisk_get_res(dev, &res);
|
---|
| 430 | if (rc != EOK) {
|
---|
| 431 | ddf_msg(LVL_ERROR, "Invalid HW resource configuration.");
|
---|
| 432 | return EINVAL;
|
---|
| 433 | }
|
---|
| 434 |
|
---|
[c4f32fff] | 435 | /*
|
---|
| 436 | * Allocate soft state.
|
---|
| 437 | */
|
---|
[42d0eeb6] | 438 | ddisk = ddf_dev_data_alloc(dev, sizeof(ddisk_t));
|
---|
| 439 | if (!ddisk) {
|
---|
[f2e78b0] | 440 | ddf_msg(LVL_ERROR, "Failed allocating soft state.");
|
---|
| 441 | rc = ENOMEM;
|
---|
| 442 | goto error;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
[c4f32fff] | 445 | /*
|
---|
| 446 | * Initialize soft state.
|
---|
| 447 | */
|
---|
[42d0eeb6] | 448 | fibril_mutex_initialize(&ddisk->lock);
|
---|
| 449 | ddisk->dev = dev;
|
---|
| 450 | ddisk->ddisk_res = res;
|
---|
[f2e78b0] | 451 |
|
---|
[42d0eeb6] | 452 | fibril_condvar_initialize(&ddisk->io_cv);
|
---|
| 453 | ddisk->io_busy = false;
|
---|
[c4f32fff] | 454 |
|
---|
[42d0eeb6] | 455 | bd_srvs_init(&ddisk->bds);
|
---|
| 456 | ddisk->bds.ops = &ddisk_bd_ops;
|
---|
| 457 | ddisk->bds.sarg = ddisk;
|
---|
[c4f32fff] | 458 |
|
---|
[eadaeae8] | 459 | ddisk->irq_cap = CAP_NIL;
|
---|
[e9d15d9] | 460 |
|
---|
[f2e78b0] | 461 | /*
|
---|
| 462 | * Enable access to ddisk's PIO registers.
|
---|
| 463 | */
|
---|
| 464 | void *vaddr;
|
---|
| 465 | rc = pio_enable((void *) res.base, sizeof(ddisk_regs_t), &vaddr);
|
---|
| 466 | if (rc != EOK) {
|
---|
| 467 | ddf_msg(LVL_ERROR, "Cannot initialize device I/O space.");
|
---|
| 468 | goto error;
|
---|
| 469 | }
|
---|
[42d0eeb6] | 470 | ddisk->ddisk_regs = vaddr;
|
---|
[f2e78b0] | 471 |
|
---|
[42d0eeb6] | 472 | ddisk->size = (int32_t) pio_read_32(&ddisk->ddisk_regs->size);
|
---|
| 473 | ddisk->blocks = ddisk->size / DDISK_BLOCK_SIZE;
|
---|
[c4f32fff] | 474 |
|
---|
[42d0eeb6] | 475 | if (ddisk->size <= 0) {
|
---|
[f2e78b0] | 476 | ddf_msg(LVL_WARN, "No disk detected.");
|
---|
| 477 | rc = EIO;
|
---|
| 478 | goto error;
|
---|
| 479 | }
|
---|
| 480 |
|
---|
| 481 | /*
|
---|
| 482 | * Allocate DMA buffer.
|
---|
| 483 | */
|
---|
[42d0eeb6] | 484 | ddisk->dma_buffer = AS_AREA_ANY;
|
---|
[f2e78b0] | 485 | rc = dmamem_map_anonymous(DDISK_BLOCK_SIZE, DMAMEM_4GiB,
|
---|
[42d0eeb6] | 486 | AS_AREA_READ | AS_AREA_WRITE, 0, &ddisk->dma_buffer_phys,
|
---|
| 487 | &ddisk->dma_buffer);
|
---|
[f2e78b0] | 488 | if (rc != EOK) {
|
---|
| 489 | ddf_msg(LVL_ERROR, "Cannot allocate DMA memory.");
|
---|
| 490 | goto error;
|
---|
| 491 | }
|
---|
| 492 |
|
---|
| 493 | ddf_msg(LVL_NOTE, "Allocated DMA buffer at %p virtual and %p physical.",
|
---|
[42d0eeb6] | 494 | ddisk->dma_buffer, (void *) ddisk->dma_buffer_phys);
|
---|
[f2e78b0] | 495 |
|
---|
[c4f32fff] | 496 | /*
|
---|
| 497 | * Create an exposed function.
|
---|
| 498 | */
|
---|
[42d0eeb6] | 499 | rc = ddisk_fun_create(ddisk);
|
---|
[f2e78b0] | 500 | if (rc != EOK) {
|
---|
| 501 | ddf_msg(LVL_ERROR, "Failed initializing ddisk controller.");
|
---|
| 502 | rc = EIO;
|
---|
| 503 | goto error;
|
---|
| 504 | }
|
---|
| 505 |
|
---|
[0eacc32] | 506 | /*
|
---|
[ae7d03c] | 507 | * Register IRQ handler.
|
---|
| 508 | */
|
---|
[0eacc32] | 509 | ddisk_regs_t *res_phys = (ddisk_regs_t *) res.base;
|
---|
| 510 | ddisk_irq_pio_ranges[0].base = res.base;
|
---|
| 511 | ddisk_irq_commands[0].addr = (void *) &res_phys->status;
|
---|
| 512 | ddisk_irq_commands[3].addr = (void *) &res_phys->command;
|
---|
[071a1ddb] | 513 | rc = register_interrupt_handler(dev, ddisk->ddisk_res.irq,
|
---|
| 514 | ddisk_irq_handler, &ddisk_irq_code, &ddisk->irq_cap);
|
---|
| 515 | if (rc != EOK) {
|
---|
[0eacc32] | 516 | ddf_msg(LVL_ERROR, "Failed to register interrupt handler.");
|
---|
| 517 | goto error;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 | /*
|
---|
| 521 | * Success, report what we have found.
|
---|
| 522 | */
|
---|
| 523 | ddf_msg(LVL_NOTE,
|
---|
[c4f32fff] | 524 | "Device at %p with %zd blocks (%zuB) using interrupt %d",
|
---|
[42d0eeb6] | 525 | (void *) ddisk->ddisk_res.base, ddisk->blocks,
|
---|
| 526 | ddisk->size, ddisk->ddisk_res.irq);
|
---|
[f2e78b0] | 527 |
|
---|
| 528 | return EOK;
|
---|
[7b084d3] | 529 |
|
---|
[f2e78b0] | 530 | error:
|
---|
[42d0eeb6] | 531 | if (ddisk->ddisk_regs)
|
---|
| 532 | pio_disable(ddisk->ddisk_regs, sizeof(ddisk_regs_t));
|
---|
| 533 | if (ddisk->dma_buffer)
|
---|
| 534 | dmamem_unmap_anonymous(ddisk->dma_buffer);
|
---|
[c4f32fff] | 535 |
|
---|
[f2e78b0] | 536 | return rc;
|
---|
| 537 | }
|
---|
| 538 |
|
---|
[b7fd2a0] | 539 | static errno_t ddisk_dev_remove_common(ddisk_t *ddisk, bool surprise)
|
---|
[f2e78b0] | 540 | {
|
---|
[b7fd2a0] | 541 | errno_t rc;
|
---|
[f2e78b0] | 542 |
|
---|
| 543 | if (!surprise)
|
---|
[42d0eeb6] | 544 | rc = ddisk_fun_remove(ddisk);
|
---|
[f2e78b0] | 545 | else
|
---|
[42d0eeb6] | 546 | rc = ddisk_fun_unbind(ddisk);
|
---|
[7b084d3] | 547 |
|
---|
[f2e78b0] | 548 | if (rc != EOK) {
|
---|
| 549 | ddf_msg(LVL_ERROR, "Unable to cleanup function '%s'.",
|
---|
| 550 | DDISK_FUN_NAME);
|
---|
| 551 | return rc;
|
---|
| 552 | }
|
---|
[73e894c] | 553 |
|
---|
[e9d15d9] | 554 | unregister_interrupt_handler(ddisk->dev, ddisk->irq_cap);
|
---|
[a35b458] | 555 |
|
---|
[42d0eeb6] | 556 | rc = pio_disable(ddisk->ddisk_regs, sizeof(ddisk_regs_t));
|
---|
[f2e78b0] | 557 | if (rc != EOK) {
|
---|
| 558 | ddf_msg(LVL_ERROR, "Unable to disable PIO.");
|
---|
[1b20da0] | 559 | return rc;
|
---|
[f2e78b0] | 560 | }
|
---|
| 561 |
|
---|
[42d0eeb6] | 562 | dmamem_unmap_anonymous(ddisk->dma_buffer);
|
---|
[f2e78b0] | 563 |
|
---|
| 564 | return EOK;
|
---|
| 565 | }
|
---|
| 566 |
|
---|
[b7fd2a0] | 567 | static errno_t ddisk_dev_remove(ddf_dev_t *dev)
|
---|
[f2e78b0] | 568 | {
|
---|
[42d0eeb6] | 569 | ddisk_t *ddisk = (ddisk_t *) ddf_dev_data_get(dev);
|
---|
[f2e78b0] | 570 |
|
---|
| 571 | ddf_msg(LVL_DEBUG, "ddisk_dev_remove(%p)", dev);
|
---|
[42d0eeb6] | 572 | return ddisk_dev_remove_common(ddisk, false);
|
---|
[f2e78b0] | 573 | }
|
---|
| 574 |
|
---|
[b7fd2a0] | 575 | static errno_t ddisk_dev_gone(ddf_dev_t *dev)
|
---|
[f2e78b0] | 576 | {
|
---|
[42d0eeb6] | 577 | ddisk_t *ddisk = (ddisk_t *) ddf_dev_data_get(dev);
|
---|
[f2e78b0] | 578 |
|
---|
| 579 | ddf_msg(LVL_DEBUG, "ddisk_dev_gone(%p)", dev);
|
---|
[42d0eeb6] | 580 | return ddisk_dev_remove_common(ddisk, true);
|
---|
[f2e78b0] | 581 | }
|
---|
| 582 |
|
---|
[b7fd2a0] | 583 | static errno_t ddisk_fun_online(ddf_fun_t *fun)
|
---|
[f2e78b0] | 584 | {
|
---|
| 585 | ddf_msg(LVL_DEBUG, "ddisk_fun_online()");
|
---|
| 586 | return ddf_fun_online(fun);
|
---|
| 587 | }
|
---|
| 588 |
|
---|
[b7fd2a0] | 589 | static errno_t ddisk_fun_offline(ddf_fun_t *fun)
|
---|
[f2e78b0] | 590 | {
|
---|
| 591 | ddf_msg(LVL_DEBUG, "ddisk_fun_offline()");
|
---|
| 592 | return ddf_fun_offline(fun);
|
---|
| 593 | }
|
---|
| 594 |
|
---|
| 595 | /** Block device connection handler */
|
---|
[984a9ba] | 596 | static void ddisk_bd_connection(ipc_call_t *icall, void *arg)
|
---|
[f2e78b0] | 597 | {
|
---|
[42d0eeb6] | 598 | ddisk_t *ddisk;
|
---|
[7b084d3] | 599 | ddf_fun_t *fun = (ddf_fun_t *) arg;
|
---|
[f2e78b0] | 600 |
|
---|
[42d0eeb6] | 601 | ddisk = (ddisk_t *) ddf_dev_data_get(ddf_fun_get_dev(fun));
|
---|
[984a9ba] | 602 | bd_conn(icall, &ddisk->bds);
|
---|
[f2e78b0] | 603 | }
|
---|
| 604 |
|
---|
| 605 | int main(int argc, char *argv[])
|
---|
| 606 | {
|
---|
| 607 | printf(NAME ": HelenOS MSIM ddisk device driver\n");
|
---|
| 608 | ddf_log_init(NAME);
|
---|
| 609 | return ddf_driver_main(&ddisk_driver);
|
---|
| 610 | }
|
---|