| 1 | /*
|
|---|
| 2 | * Copyright (c) 2012 Petr Jerman
|
|---|
| 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 SATA disk driver
|
|---|
| 36 | *
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | #include <sys/types.h>
|
|---|
| 40 | #include <errno.h>
|
|---|
| 41 | #include <stdio.h>
|
|---|
| 42 | #include <ipc/bd.h>
|
|---|
| 43 | #include <str.h>
|
|---|
| 44 | #include <loc.h>
|
|---|
| 45 | #include <macros.h>
|
|---|
| 46 |
|
|---|
| 47 | #include <device/ahci.h>
|
|---|
| 48 | #include "sata_bd.h"
|
|---|
| 49 |
|
|---|
| 50 | #define NAME "sata_bd"
|
|---|
| 51 | #define NAMESPACE "bd"
|
|---|
| 52 |
|
|---|
| 53 | /** Maximum number of disks handled */
|
|---|
| 54 | #define MAXDISKS 256
|
|---|
| 55 |
|
|---|
| 56 | static sata_bd_dev_t disk[MAXDISKS];
|
|---|
| 57 | static int disk_count;
|
|---|
| 58 |
|
|---|
| 59 | /** Find SATA devices in device tree.
|
|---|
| 60 | *
|
|---|
| 61 | * @param Device manager handle describing container for searching.
|
|---|
| 62 | *
|
|---|
| 63 | * @return EOK if succeed, error code otherwise.
|
|---|
| 64 | *
|
|---|
| 65 | */
|
|---|
| 66 | static int scan_device_tree(devman_handle_t funh)
|
|---|
| 67 | {
|
|---|
| 68 | devman_handle_t devh;
|
|---|
| 69 | devman_handle_t *cfuns;
|
|---|
| 70 | size_t count, i;
|
|---|
| 71 | int rc;
|
|---|
| 72 |
|
|---|
| 73 | /* If device is SATA, add device to the disk array. */
|
|---|
| 74 | disk[disk_count].sess = ahci_get_sess(funh, &disk[disk_count].dev_name);
|
|---|
| 75 | if(disk[disk_count].sess != NULL) {
|
|---|
| 76 |
|
|---|
| 77 | ahci_get_sata_device_name(disk[disk_count].sess,
|
|---|
| 78 | SATA_DEV_NAME_LENGTH, disk[disk_count].sata_dev_name);
|
|---|
| 79 |
|
|---|
| 80 | ahci_get_block_size(disk[disk_count].sess,
|
|---|
| 81 | &disk[disk_count].block_size);
|
|---|
| 82 |
|
|---|
| 83 | ahci_get_num_blocks(disk[disk_count].sess, &disk[disk_count].blocks);
|
|---|
| 84 |
|
|---|
| 85 | printf("Device %s - %s , blocks: %lu, block_size: %lu\n",
|
|---|
| 86 | disk[disk_count].dev_name, disk[disk_count].sata_dev_name,
|
|---|
| 87 | (long unsigned int) disk[disk_count].blocks,
|
|---|
| 88 | (long unsigned int) disk[disk_count].block_size);
|
|---|
| 89 |
|
|---|
| 90 | ++disk_count;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /* search children */
|
|---|
| 94 | rc = devman_fun_get_child(funh, &devh);
|
|---|
| 95 | if (rc == ENOENT)
|
|---|
| 96 | return EOK;
|
|---|
| 97 |
|
|---|
| 98 | if (rc != EOK) {
|
|---|
| 99 | printf(NAME ": Failed getting child device for function %s.\n", "xxx");
|
|---|
| 100 | return rc;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | rc = devman_dev_get_functions(devh, &cfuns, &count);
|
|---|
| 104 | if (rc != EOK) {
|
|---|
| 105 | printf(NAME ": Failed getting list of functions for device %s.\n",
|
|---|
| 106 | "xxx");
|
|---|
| 107 | return rc;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | for (i = 0; i < count; i++)
|
|---|
| 111 | scan_device_tree(cfuns[i]);
|
|---|
| 112 |
|
|---|
| 113 | free(cfuns);
|
|---|
| 114 | return EOK;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | /** Find sata devices in device tree from root.
|
|---|
| 118 | *
|
|---|
| 119 | * @return EOK if succeed, error code otherwise.
|
|---|
| 120 | *
|
|---|
| 121 | */
|
|---|
| 122 | static int get_sata_disks()
|
|---|
| 123 | {
|
|---|
| 124 | devman_handle_t root_fun;
|
|---|
| 125 | int rc;
|
|---|
| 126 |
|
|---|
| 127 | disk_count = 0;
|
|---|
| 128 |
|
|---|
| 129 | rc = devman_fun_get_handle("/", &root_fun, 0);
|
|---|
| 130 | if (rc != EOK) {
|
|---|
| 131 | printf(NAME ": Error resolving root function.\n");
|
|---|
| 132 | return EIO;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | scan_device_tree(root_fun);
|
|---|
| 136 |
|
|---|
| 137 | return EOK;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | /** Block device connection handler. */
|
|---|
| 141 | static void sata_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
|---|
| 142 | {
|
|---|
| 143 | void *fs_va = NULL;
|
|---|
| 144 | ipc_callid_t callid;
|
|---|
| 145 | ipc_call_t call;
|
|---|
| 146 | sysarg_t method;
|
|---|
| 147 | service_id_t dsid;
|
|---|
| 148 | /* Size of the communication area. */
|
|---|
| 149 | size_t comm_size;
|
|---|
| 150 | unsigned int flags;
|
|---|
| 151 | int retval = 0;
|
|---|
| 152 | uint64_t ba;
|
|---|
| 153 | size_t cnt;
|
|---|
| 154 | int disk_id, i;
|
|---|
| 155 |
|
|---|
| 156 | /* Get the device service ID. */
|
|---|
| 157 | dsid = IPC_GET_ARG1(*icall);
|
|---|
| 158 |
|
|---|
| 159 | /* Determine which disk device is the client connecting to. */
|
|---|
| 160 | disk_id = -1;
|
|---|
| 161 | for (i = 0; i < MAXDISKS; i++)
|
|---|
| 162 | if (disk[i].service_id == dsid)
|
|---|
| 163 | disk_id = i;
|
|---|
| 164 |
|
|---|
| 165 | if (disk_id < 0) {
|
|---|
| 166 | async_answer_0(iid, EINVAL);
|
|---|
| 167 | return;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | /* Answer the IPC_M_CONNECT_ME_TO call. */
|
|---|
| 171 | async_answer_0(iid, EOK);
|
|---|
| 172 |
|
|---|
| 173 | if (!async_share_out_receive(&callid, &comm_size, &flags)) {
|
|---|
| 174 | async_answer_0(callid, EHANGUP);
|
|---|
| 175 | return;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | (void) async_share_out_finalize(callid, &fs_va);
|
|---|
| 179 | if (fs_va == (void *) -1) {
|
|---|
| 180 | async_answer_0(callid, EHANGUP);
|
|---|
| 181 | return;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | while (true) {
|
|---|
| 185 | callid = async_get_call(&call);
|
|---|
| 186 | method = IPC_GET_IMETHOD(call);
|
|---|
| 187 |
|
|---|
| 188 | if (!method) {
|
|---|
| 189 | /* The other side has hung up. */
|
|---|
| 190 | async_answer_0(callid, EOK);
|
|---|
| 191 | return;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | switch (method) {
|
|---|
| 195 | case BD_READ_BLOCKS:
|
|---|
| 196 | ba = MERGE_LOUP32(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
|
|---|
| 197 | cnt = IPC_GET_ARG3(call);
|
|---|
| 198 | if (cnt * disk[disk_id].block_size > comm_size) {
|
|---|
| 199 | retval = ELIMIT;
|
|---|
| 200 | break;
|
|---|
| 201 | }
|
|---|
| 202 | retval = ahci_read_blocks(disk[disk_id].sess, ba, cnt, fs_va);
|
|---|
| 203 | break;
|
|---|
| 204 | case BD_WRITE_BLOCKS:
|
|---|
| 205 | ba = MERGE_LOUP32(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
|
|---|
| 206 | cnt = IPC_GET_ARG3(call);
|
|---|
| 207 | if (cnt * disk[disk_id].block_size > comm_size) {
|
|---|
| 208 | retval = ELIMIT;
|
|---|
| 209 | break;
|
|---|
| 210 | }
|
|---|
| 211 | retval = ahci_write_blocks(disk[disk_id].sess, ba, cnt, fs_va);
|
|---|
| 212 | break;
|
|---|
| 213 | case BD_GET_BLOCK_SIZE:
|
|---|
| 214 | async_answer_1(callid, EOK, disk[disk_id].block_size);
|
|---|
| 215 | continue;
|
|---|
| 216 | case BD_GET_NUM_BLOCKS:
|
|---|
| 217 | async_answer_2(callid, EOK, LOWER32(disk[disk_id].blocks),
|
|---|
| 218 | UPPER32(disk[disk_id].blocks));
|
|---|
| 219 | break;
|
|---|
| 220 | default:
|
|---|
| 221 | retval = EINVAL;
|
|---|
| 222 | break;
|
|---|
| 223 | }
|
|---|
| 224 | async_answer_0(callid, retval);
|
|---|
| 225 | }
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | int main(int argc, char **argv)
|
|---|
| 229 | {
|
|---|
| 230 | int rc;
|
|---|
| 231 |
|
|---|
| 232 | async_set_client_connection(sata_bd_connection);
|
|---|
| 233 | rc = loc_server_register(NAME);
|
|---|
| 234 | if (rc < 0) {
|
|---|
| 235 | printf(NAME ": Unable to register driver.\n");
|
|---|
| 236 | return rc;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | rc = get_sata_disks();
|
|---|
| 240 | if (rc != EOK) {
|
|---|
| 241 | return rc;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | for(int i=0; i < disk_count; i++) {
|
|---|
| 245 | char name[1024];
|
|---|
| 246 | snprintf(name, 1024, "%s/%s", NAMESPACE, disk[i].dev_name);
|
|---|
| 247 | rc = loc_service_register(name, &disk[i].service_id);
|
|---|
| 248 | if (rc != EOK) {
|
|---|
| 249 | printf(NAME ": Unable to register device %s.\n", name);
|
|---|
| 250 | return rc;
|
|---|
| 251 | }
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | printf(NAME ": Accepting connections\n");
|
|---|
| 255 | task_retval(0);
|
|---|
| 256 | async_manager();
|
|---|
| 257 |
|
|---|
| 258 | /* Not reached */
|
|---|
| 259 | return 0;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | /**
|
|---|
| 263 | * @}
|
|---|
| 264 | */
|
|---|