[9904eb90] | 1 | /*
|
---|
[ae7d03c] | 2 | * Copyright (c) 2012 Petr Jerman
|
---|
[9904eb90] | 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 libdrv
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[7f620e8] | 35 | #include <as.h>
|
---|
[9904eb90] | 36 | #include <async.h>
|
---|
[7f620e8] | 37 | #include <devman.h>
|
---|
[9904eb90] | 38 | #include <errno.h>
|
---|
| 39 | #include <stdio.h>
|
---|
[9be30cdf] | 40 | #include <macros.h>
|
---|
[1d6dd2a] | 41 | #include <str.h>
|
---|
[9904eb90] | 42 | #include "ahci_iface.h"
|
---|
| 43 | #include "ddf/driver.h"
|
---|
| 44 |
|
---|
| 45 | typedef enum {
|
---|
| 46 | IPC_M_AHCI_GET_SATA_DEVICE_NAME,
|
---|
| 47 | IPC_M_AHCI_GET_NUM_BLOCKS,
|
---|
| 48 | IPC_M_AHCI_GET_BLOCK_SIZE,
|
---|
| 49 | IPC_M_AHCI_READ_BLOCKS,
|
---|
| 50 | IPC_M_AHCI_WRITE_BLOCKS
|
---|
| 51 | } ahci_iface_funcs_t;
|
---|
| 52 |
|
---|
[7f620e8] | 53 | #define MAX_NAME_LENGTH 1024
|
---|
| 54 |
|
---|
[9904eb90] | 55 | #define LO(ptr) \
|
---|
| 56 | ((uint32_t) (((uint64_t) ((uintptr_t) (ptr))) & 0xffffffff))
|
---|
| 57 |
|
---|
| 58 | #define HI(ptr) \
|
---|
| 59 | ((uint32_t) (((uint64_t) ((uintptr_t) (ptr))) >> 32))
|
---|
| 60 |
|
---|
[3bacee1] | 61 | async_sess_t *ahci_get_sess(devman_handle_t funh, char **name)
|
---|
[7f620e8] | 62 | {
|
---|
| 63 | // FIXME: Use a better way than substring match
|
---|
[a35b458] | 64 |
|
---|
[7f620e8] | 65 | *name = NULL;
|
---|
[a35b458] | 66 |
|
---|
[7f620e8] | 67 | char devn[MAX_NAME_LENGTH];
|
---|
[b7fd2a0] | 68 | errno_t rc = devman_fun_get_name(funh, devn, MAX_NAME_LENGTH);
|
---|
[7f620e8] | 69 | if (rc != EOK)
|
---|
| 70 | return NULL;
|
---|
[a35b458] | 71 |
|
---|
[7f620e8] | 72 | size_t devn_size = str_size(devn);
|
---|
[a35b458] | 73 |
|
---|
[7f620e8] | 74 | if ((devn_size > 5) && (str_lcmp(devn, "ahci_", 5) == 0)) {
|
---|
[f9b2cb4c] | 75 | async_sess_t *sess = devman_device_connect(funh, IPC_FLAG_BLOCKING);
|
---|
[a35b458] | 76 |
|
---|
[7f620e8] | 77 | if (sess) {
|
---|
| 78 | *name = str_dup(devn);
|
---|
| 79 | return sess;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
[a35b458] | 82 |
|
---|
[7f620e8] | 83 | return NULL;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[b7fd2a0] | 86 | errno_t ahci_get_sata_device_name(async_sess_t *sess, size_t sata_dev_name_length,
|
---|
[7f620e8] | 87 | char *sata_dev_name)
|
---|
| 88 | {
|
---|
| 89 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 90 | if (!exch)
|
---|
| 91 | return EINVAL;
|
---|
[a35b458] | 92 |
|
---|
[7f620e8] | 93 | aid_t req = async_send_2(exch, DEV_IFACE_ID(AHCI_DEV_IFACE),
|
---|
| 94 | IPC_M_AHCI_GET_SATA_DEVICE_NAME, sata_dev_name_length, NULL);
|
---|
[a35b458] | 95 |
|
---|
[7f620e8] | 96 | async_data_read_start(exch, sata_dev_name, sata_dev_name_length);
|
---|
[a35b458] | 97 |
|
---|
[b7fd2a0] | 98 | errno_t rc;
|
---|
[7f620e8] | 99 | async_wait_for(req, &rc);
|
---|
[a35b458] | 100 |
|
---|
[7f620e8] | 101 | return rc;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[b7fd2a0] | 104 | errno_t ahci_get_num_blocks(async_sess_t *sess, uint64_t *blocks)
|
---|
[7f620e8] | 105 | {
|
---|
| 106 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 107 | if (!exch)
|
---|
| 108 | return EINVAL;
|
---|
[a35b458] | 109 |
|
---|
[7f620e8] | 110 | sysarg_t blocks_hi;
|
---|
| 111 | sysarg_t blocks_lo;
|
---|
[b7fd2a0] | 112 | errno_t rc = async_req_1_2(exch, DEV_IFACE_ID(AHCI_DEV_IFACE),
|
---|
[7f620e8] | 113 | IPC_M_AHCI_GET_NUM_BLOCKS, &blocks_hi, &blocks_lo);
|
---|
[a35b458] | 114 |
|
---|
[7f620e8] | 115 | async_exchange_end(exch);
|
---|
[a35b458] | 116 |
|
---|
[7f620e8] | 117 | if (rc == EOK) {
|
---|
[3bacee1] | 118 | *blocks = (((uint64_t) blocks_hi) << 32) |
|
---|
| 119 | (((uint64_t) blocks_lo) & 0xffffffff);
|
---|
[7f620e8] | 120 | }
|
---|
[a35b458] | 121 |
|
---|
[7f620e8] | 122 | return rc;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[b7fd2a0] | 125 | errno_t ahci_get_block_size(async_sess_t *sess, size_t *blocks_size)
|
---|
[7f620e8] | 126 | {
|
---|
| 127 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 128 | if (!exch)
|
---|
| 129 | return EINVAL;
|
---|
[a35b458] | 130 |
|
---|
[7f620e8] | 131 | sysarg_t bs;
|
---|
[b7fd2a0] | 132 | errno_t rc = async_req_1_1(exch, DEV_IFACE_ID(AHCI_DEV_IFACE),
|
---|
[7f620e8] | 133 | IPC_M_AHCI_GET_BLOCK_SIZE, &bs);
|
---|
[a35b458] | 134 |
|
---|
[7f620e8] | 135 | async_exchange_end(exch);
|
---|
[a35b458] | 136 |
|
---|
[7f620e8] | 137 | if (rc == EOK)
|
---|
| 138 | *blocks_size = (size_t) bs;
|
---|
[a35b458] | 139 |
|
---|
[7f620e8] | 140 | return rc;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[b7fd2a0] | 143 | errno_t ahci_read_blocks(async_sess_t *sess, uint64_t blocknum, size_t count,
|
---|
[7f620e8] | 144 | void *buf)
|
---|
| 145 | {
|
---|
| 146 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 147 | if (!exch)
|
---|
| 148 | return EINVAL;
|
---|
[a35b458] | 149 |
|
---|
[7f620e8] | 150 | aid_t req;
|
---|
| 151 | req = async_send_4(exch, DEV_IFACE_ID(AHCI_DEV_IFACE),
|
---|
| 152 | IPC_M_AHCI_READ_BLOCKS, HI(blocknum), LO(blocknum), count, NULL);
|
---|
[a35b458] | 153 |
|
---|
[7f620e8] | 154 | async_share_out_start(exch, buf, AS_AREA_READ | AS_AREA_WRITE);
|
---|
[a35b458] | 155 |
|
---|
[7f620e8] | 156 | async_exchange_end(exch);
|
---|
[a35b458] | 157 |
|
---|
[b7fd2a0] | 158 | errno_t rc;
|
---|
[7f620e8] | 159 | async_wait_for(req, &rc);
|
---|
[a35b458] | 160 |
|
---|
[7f620e8] | 161 | return rc;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[b7fd2a0] | 164 | errno_t ahci_write_blocks(async_sess_t *sess, uint64_t blocknum, size_t count,
|
---|
[3bacee1] | 165 | void *buf)
|
---|
[7f620e8] | 166 | {
|
---|
| 167 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 168 | if (!exch)
|
---|
| 169 | return EINVAL;
|
---|
[a35b458] | 170 |
|
---|
[7f620e8] | 171 | aid_t req = async_send_4(exch, DEV_IFACE_ID(AHCI_DEV_IFACE),
|
---|
| 172 | IPC_M_AHCI_WRITE_BLOCKS, HI(blocknum), LO(blocknum), count, NULL);
|
---|
[a35b458] | 173 |
|
---|
[7f620e8] | 174 | async_share_out_start(exch, buf, AS_AREA_READ | AS_AREA_WRITE);
|
---|
[a35b458] | 175 |
|
---|
[7f620e8] | 176 | async_exchange_end(exch);
|
---|
[a35b458] | 177 |
|
---|
[b7fd2a0] | 178 | errno_t rc;
|
---|
[7f620e8] | 179 | async_wait_for(req, &rc);
|
---|
[a35b458] | 180 |
|
---|
[7f620e8] | 181 | return rc;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[984a9ba] | 184 | static void remote_ahci_get_sata_device_name(ddf_fun_t *, void *, ipc_call_t *);
|
---|
| 185 | static void remote_ahci_get_num_blocks(ddf_fun_t *, void *, ipc_call_t *);
|
---|
| 186 | static void remote_ahci_get_block_size(ddf_fun_t *, void *, ipc_call_t *);
|
---|
| 187 | static void remote_ahci_read_blocks(ddf_fun_t *, void *, ipc_call_t *);
|
---|
| 188 | static void remote_ahci_write_blocks(ddf_fun_t *, void *, ipc_call_t *);
|
---|
[9904eb90] | 189 |
|
---|
| 190 | /** Remote AHCI interface operations. */
|
---|
[9be30cdf] | 191 | static const remote_iface_func_ptr_t remote_ahci_iface_ops [] = {
|
---|
[9904eb90] | 192 | [IPC_M_AHCI_GET_SATA_DEVICE_NAME] = remote_ahci_get_sata_device_name,
|
---|
| 193 | [IPC_M_AHCI_GET_NUM_BLOCKS] = remote_ahci_get_num_blocks,
|
---|
| 194 | [IPC_M_AHCI_GET_BLOCK_SIZE] = remote_ahci_get_block_size,
|
---|
| 195 | [IPC_M_AHCI_READ_BLOCKS] = remote_ahci_read_blocks,
|
---|
| 196 | [IPC_M_AHCI_WRITE_BLOCKS] = remote_ahci_write_blocks
|
---|
| 197 | };
|
---|
| 198 |
|
---|
| 199 | /** Remote AHCI interface structure.
|
---|
| 200 | */
|
---|
[7f80313] | 201 | const remote_iface_t remote_ahci_iface = {
|
---|
[9be30cdf] | 202 | .method_count = ARRAY_SIZE(remote_ahci_iface_ops),
|
---|
[9904eb90] | 203 | .methods = remote_ahci_iface_ops
|
---|
| 204 | };
|
---|
| 205 |
|
---|
| 206 | void remote_ahci_get_sata_device_name(ddf_fun_t *fun, void *iface,
|
---|
[984a9ba] | 207 | ipc_call_t *call)
|
---|
[9904eb90] | 208 | {
|
---|
| 209 | const ahci_iface_t *ahci_iface = (ahci_iface_t *) iface;
|
---|
[a35b458] | 210 |
|
---|
[9904eb90] | 211 | if (ahci_iface->get_sata_device_name == NULL) {
|
---|
[984a9ba] | 212 | async_answer_0(call, ENOTSUP);
|
---|
[9904eb90] | 213 | return;
|
---|
| 214 | }
|
---|
[a35b458] | 215 |
|
---|
[9904eb90] | 216 | const size_t sata_dev_name_length =
|
---|
| 217 | (size_t) DEV_IPC_GET_ARG1(*call);
|
---|
[a35b458] | 218 |
|
---|
[3bacee1] | 219 | char *sata_dev_name = malloc(sata_dev_name_length);
|
---|
[485dacd] | 220 | if (sata_dev_name == NULL) {
|
---|
[984a9ba] | 221 | async_answer_0(call, ENOMEM);
|
---|
[485dacd] | 222 | return;
|
---|
[1b20da0] | 223 | }
|
---|
[a35b458] | 224 |
|
---|
[b7fd2a0] | 225 | const errno_t ret = ahci_iface->get_sata_device_name(fun,
|
---|
[9904eb90] | 226 | sata_dev_name_length, sata_dev_name);
|
---|
[a35b458] | 227 |
|
---|
[984a9ba] | 228 | ipc_call_t data;
|
---|
[9904eb90] | 229 | size_t real_size;
|
---|
[984a9ba] | 230 | if ((async_data_read_receive(&data, &real_size)) &&
|
---|
[9904eb90] | 231 | (real_size == sata_dev_name_length))
|
---|
[984a9ba] | 232 | async_data_read_finalize(&data, sata_dev_name,
|
---|
[a46e56b] | 233 | sata_dev_name_length);
|
---|
[a35b458] | 234 |
|
---|
[485dacd] | 235 | free(sata_dev_name);
|
---|
[984a9ba] | 236 | async_answer_0(call, ret);
|
---|
[9904eb90] | 237 | }
|
---|
| 238 |
|
---|
| 239 | static void remote_ahci_get_num_blocks(ddf_fun_t *fun, void *iface,
|
---|
[984a9ba] | 240 | ipc_call_t *call)
|
---|
[9904eb90] | 241 | {
|
---|
| 242 | const ahci_iface_t *ahci_iface = (ahci_iface_t *) iface;
|
---|
[a35b458] | 243 |
|
---|
[9904eb90] | 244 | if (ahci_iface->get_num_blocks == NULL) {
|
---|
[984a9ba] | 245 | async_answer_0(call, ENOTSUP);
|
---|
[9904eb90] | 246 | return;
|
---|
| 247 | }
|
---|
[a35b458] | 248 |
|
---|
[9904eb90] | 249 | uint64_t blocks;
|
---|
[b7fd2a0] | 250 | const errno_t ret = ahci_iface->get_num_blocks(fun, &blocks);
|
---|
[a35b458] | 251 |
|
---|
[9904eb90] | 252 | if (ret != EOK)
|
---|
[984a9ba] | 253 | async_answer_0(call, ret);
|
---|
[9904eb90] | 254 | else
|
---|
[984a9ba] | 255 | async_answer_2(call, EOK, HI(blocks), LO(blocks));
|
---|
[9904eb90] | 256 | }
|
---|
| 257 |
|
---|
| 258 | static void remote_ahci_get_block_size(ddf_fun_t *fun, void *iface,
|
---|
[984a9ba] | 259 | ipc_call_t *call)
|
---|
[9904eb90] | 260 | {
|
---|
| 261 | const ahci_iface_t *ahci_iface = (ahci_iface_t *) iface;
|
---|
[a35b458] | 262 |
|
---|
[9904eb90] | 263 | if (ahci_iface->get_block_size == NULL) {
|
---|
[984a9ba] | 264 | async_answer_0(call, ENOTSUP);
|
---|
[9904eb90] | 265 | return;
|
---|
| 266 | }
|
---|
[a35b458] | 267 |
|
---|
[9904eb90] | 268 | size_t blocks;
|
---|
[b7fd2a0] | 269 | const errno_t ret = ahci_iface->get_block_size(fun, &blocks);
|
---|
[a35b458] | 270 |
|
---|
[9904eb90] | 271 | if (ret != EOK)
|
---|
[984a9ba] | 272 | async_answer_0(call, ret);
|
---|
[9904eb90] | 273 | else
|
---|
[984a9ba] | 274 | async_answer_1(call, EOK, blocks);
|
---|
[9904eb90] | 275 | }
|
---|
| 276 |
|
---|
[984a9ba] | 277 | void remote_ahci_read_blocks(ddf_fun_t *fun, void *iface, ipc_call_t *call)
|
---|
[9904eb90] | 278 | {
|
---|
| 279 | const ahci_iface_t *ahci_iface = (ahci_iface_t *) iface;
|
---|
[a35b458] | 280 |
|
---|
[9904eb90] | 281 | if (ahci_iface->read_blocks == NULL) {
|
---|
[984a9ba] | 282 | async_answer_0(call, ENOTSUP);
|
---|
[9904eb90] | 283 | return;
|
---|
| 284 | }
|
---|
[a35b458] | 285 |
|
---|
[984a9ba] | 286 | ipc_call_t data;
|
---|
[9904eb90] | 287 | size_t maxblock_size;
|
---|
| 288 | unsigned int flags;
|
---|
[984a9ba] | 289 | async_share_out_receive(&data, &maxblock_size, &flags);
|
---|
[a35b458] | 290 |
|
---|
[9904eb90] | 291 | void *buf;
|
---|
[984a9ba] | 292 | async_share_out_finalize(&data, &buf);
|
---|
[a35b458] | 293 |
|
---|
[9904eb90] | 294 | const uint64_t blocknum =
|
---|
| 295 | (((uint64_t) (DEV_IPC_GET_ARG1(*call))) << 32) |
|
---|
| 296 | (((uint64_t) (DEV_IPC_GET_ARG2(*call))) & 0xffffffff);
|
---|
| 297 | const size_t cnt = (size_t) DEV_IPC_GET_ARG3(*call);
|
---|
[a35b458] | 298 |
|
---|
[b7fd2a0] | 299 | const errno_t ret = ahci_iface->read_blocks(fun, blocknum, cnt, buf);
|
---|
[a35b458] | 300 |
|
---|
[984a9ba] | 301 | async_answer_0(call, ret);
|
---|
[9904eb90] | 302 | }
|
---|
| 303 |
|
---|
[984a9ba] | 304 | void remote_ahci_write_blocks(ddf_fun_t *fun, void *iface, ipc_call_t *call)
|
---|
[9904eb90] | 305 | {
|
---|
| 306 | const ahci_iface_t *ahci_iface = (ahci_iface_t *) iface;
|
---|
[a35b458] | 307 |
|
---|
[9904eb90] | 308 | if (ahci_iface->read_blocks == NULL) {
|
---|
[984a9ba] | 309 | async_answer_0(call, ENOTSUP);
|
---|
[9904eb90] | 310 | return;
|
---|
| 311 | }
|
---|
[a35b458] | 312 |
|
---|
[984a9ba] | 313 | ipc_call_t data;
|
---|
[9904eb90] | 314 | size_t maxblock_size;
|
---|
| 315 | unsigned int flags;
|
---|
[984a9ba] | 316 | async_share_out_receive(&data, &maxblock_size, &flags);
|
---|
[a35b458] | 317 |
|
---|
[9904eb90] | 318 | void *buf;
|
---|
[984a9ba] | 319 | async_share_out_finalize(&data, &buf);
|
---|
[a35b458] | 320 |
|
---|
[9904eb90] | 321 | const uint64_t blocknum =
|
---|
| 322 | (((uint64_t)(DEV_IPC_GET_ARG1(*call))) << 32) |
|
---|
| 323 | (((uint64_t)(DEV_IPC_GET_ARG2(*call))) & 0xffffffff);
|
---|
| 324 | const size_t cnt = (size_t) DEV_IPC_GET_ARG3(*call);
|
---|
[a35b458] | 325 |
|
---|
[b7fd2a0] | 326 | const errno_t ret = ahci_iface->write_blocks(fun, blocknum, cnt, buf);
|
---|
[a35b458] | 327 |
|
---|
[984a9ba] | 328 | async_answer_0(call, ret);
|
---|
[9904eb90] | 329 | }
|
---|
| 330 |
|
---|
| 331 | /**
|
---|
| 332 | * @}
|
---|
| 333 | */
|
---|