| 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 <bd_srv.h>
|
|---|
| 41 | #include <devman.h>
|
|---|
| 42 | #include <errno.h>
|
|---|
| 43 | #include <stdio.h>
|
|---|
| 44 | #include <str.h>
|
|---|
| 45 | #include <loc.h>
|
|---|
| 46 | #include <macros.h>
|
|---|
| 47 |
|
|---|
| 48 | #include <ahci_iface.h>
|
|---|
| 49 | #include "sata_bd.h"
|
|---|
| 50 |
|
|---|
| 51 | #define NAME "sata_bd"
|
|---|
| 52 | #define NAMESPACE "bd"
|
|---|
| 53 |
|
|---|
| 54 | /** Maximum number of disks handled */
|
|---|
| 55 | #define MAXDISKS 256
|
|---|
| 56 |
|
|---|
| 57 | static sata_bd_dev_t disk[MAXDISKS];
|
|---|
| 58 | static int disk_count;
|
|---|
| 59 |
|
|---|
| 60 | static int sata_bd_open(bd_srvs_t *, bd_srv_t *);
|
|---|
| 61 | static int sata_bd_close(bd_srv_t *);
|
|---|
| 62 | static int sata_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
|
|---|
| 63 | static int sata_bd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t);
|
|---|
| 64 | static int sata_bd_get_block_size(bd_srv_t *, size_t *);
|
|---|
| 65 | static int sata_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
|
|---|
| 66 |
|
|---|
| 67 | static bd_ops_t sata_bd_ops = {
|
|---|
| 68 | .open = sata_bd_open,
|
|---|
| 69 | .close = sata_bd_close,
|
|---|
| 70 | .read_blocks = sata_bd_read_blocks,
|
|---|
| 71 | .write_blocks = sata_bd_write_blocks,
|
|---|
| 72 | .get_block_size = sata_bd_get_block_size,
|
|---|
| 73 | .get_num_blocks = sata_bd_get_num_blocks
|
|---|
| 74 | };
|
|---|
| 75 |
|
|---|
| 76 | static sata_bd_dev_t *bd_srv_sata(bd_srv_t *bd)
|
|---|
| 77 | {
|
|---|
| 78 | return (sata_bd_dev_t *) bd->srvs->sarg;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /** Find SATA devices in device tree.
|
|---|
| 82 | *
|
|---|
| 83 | * @param Device manager handle describing container for searching.
|
|---|
| 84 | *
|
|---|
| 85 | * @return EOK if succeed, error code otherwise.
|
|---|
| 86 | *
|
|---|
| 87 | */
|
|---|
| 88 | static int scan_device_tree(devman_handle_t funh)
|
|---|
| 89 | {
|
|---|
| 90 | devman_handle_t devh;
|
|---|
| 91 | devman_handle_t *cfuns;
|
|---|
| 92 | size_t count, i;
|
|---|
| 93 | int rc;
|
|---|
| 94 |
|
|---|
| 95 | /* If device is SATA, add device to the disk array. */
|
|---|
| 96 | disk[disk_count].sess = ahci_get_sess(funh, &disk[disk_count].dev_name);
|
|---|
| 97 | if(disk[disk_count].sess != NULL) {
|
|---|
| 98 |
|
|---|
| 99 | ahci_get_sata_device_name(disk[disk_count].sess,
|
|---|
| 100 | SATA_DEV_NAME_LENGTH, disk[disk_count].sata_dev_name);
|
|---|
| 101 |
|
|---|
| 102 | ahci_get_block_size(disk[disk_count].sess,
|
|---|
| 103 | &disk[disk_count].block_size);
|
|---|
| 104 |
|
|---|
| 105 | ahci_get_num_blocks(disk[disk_count].sess, &disk[disk_count].blocks);
|
|---|
| 106 |
|
|---|
| 107 | bd_srvs_init(&disk[disk_count].bds);
|
|---|
| 108 | disk[disk_count].bds.ops = &sata_bd_ops;
|
|---|
| 109 | disk[disk_count].bds.sarg = &disk[disk_count];
|
|---|
| 110 |
|
|---|
| 111 | printf("Device %s - %s , blocks: %lu, block_size: %lu\n",
|
|---|
| 112 | disk[disk_count].dev_name, disk[disk_count].sata_dev_name,
|
|---|
| 113 | (long unsigned int) disk[disk_count].blocks,
|
|---|
| 114 | (long unsigned int) disk[disk_count].block_size);
|
|---|
| 115 |
|
|---|
| 116 | ++disk_count;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | /* search children */
|
|---|
| 120 | rc = devman_fun_get_child(funh, &devh);
|
|---|
| 121 | if (rc == ENOENT)
|
|---|
| 122 | return EOK;
|
|---|
| 123 |
|
|---|
| 124 | if (rc != EOK) {
|
|---|
| 125 | printf(NAME ": Failed getting child device for function %s.\n", "xxx");
|
|---|
| 126 | return rc;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | rc = devman_dev_get_functions(devh, &cfuns, &count);
|
|---|
| 130 | if (rc != EOK) {
|
|---|
| 131 | printf(NAME ": Failed getting list of functions for device %s.\n",
|
|---|
| 132 | "xxx");
|
|---|
| 133 | return rc;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | for (i = 0; i < count; i++)
|
|---|
| 137 | scan_device_tree(cfuns[i]);
|
|---|
| 138 |
|
|---|
| 139 | free(cfuns);
|
|---|
| 140 | return EOK;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /** Find sata devices in device tree from root.
|
|---|
| 144 | *
|
|---|
| 145 | * @return EOK if succeed, error code otherwise.
|
|---|
| 146 | *
|
|---|
| 147 | */
|
|---|
| 148 | static int get_sata_disks()
|
|---|
| 149 | {
|
|---|
| 150 | devman_handle_t root_fun;
|
|---|
| 151 | int rc;
|
|---|
| 152 |
|
|---|
| 153 | disk_count = 0;
|
|---|
| 154 |
|
|---|
| 155 | rc = devman_fun_get_handle("/", &root_fun, 0);
|
|---|
| 156 | if (rc != EOK) {
|
|---|
| 157 | printf(NAME ": Error resolving root function.\n");
|
|---|
| 158 | return EIO;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | scan_device_tree(root_fun);
|
|---|
| 162 |
|
|---|
| 163 | return EOK;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /** Block device connection handler. */
|
|---|
| 167 | static void sata_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
|---|
| 168 | {
|
|---|
| 169 | service_id_t dsid;
|
|---|
| 170 | int disk_id, i;
|
|---|
| 171 |
|
|---|
| 172 | /* Get the device service ID. */
|
|---|
| 173 | dsid = IPC_GET_ARG1(*icall);
|
|---|
| 174 |
|
|---|
| 175 | /* Determine which disk device is the client connecting to. */
|
|---|
| 176 | disk_id = -1;
|
|---|
| 177 | for (i = 0; i < MAXDISKS; i++)
|
|---|
| 178 | if (disk[i].service_id == dsid)
|
|---|
| 179 | disk_id = i;
|
|---|
| 180 |
|
|---|
| 181 | if (disk_id < 0) {
|
|---|
| 182 | async_answer_0(iid, EINVAL);
|
|---|
| 183 | return;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | bd_conn(iid, icall, &disk[disk_id].bds);
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | /** Open device. */
|
|---|
| 190 | static int sata_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
|---|
| 191 | {
|
|---|
| 192 | return EOK;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /** Close device. */
|
|---|
| 196 | static int sata_bd_close(bd_srv_t *bd)
|
|---|
| 197 | {
|
|---|
| 198 | return EOK;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | /** Read blocks from partition. */
|
|---|
| 202 | static int sata_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt, void *buf,
|
|---|
| 203 | size_t size)
|
|---|
| 204 | {
|
|---|
| 205 | sata_bd_dev_t *sbd = bd_srv_sata(bd);
|
|---|
| 206 |
|
|---|
| 207 | if (size < cnt * sbd->block_size)
|
|---|
| 208 | return EINVAL;
|
|---|
| 209 |
|
|---|
| 210 | return ahci_read_blocks(sbd->sess, ba, cnt, buf);
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | /** Write blocks to partition. */
|
|---|
| 214 | static int sata_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
|
|---|
| 215 | const void *buf, size_t size)
|
|---|
| 216 | {
|
|---|
| 217 | sata_bd_dev_t *sbd = bd_srv_sata(bd);
|
|---|
| 218 |
|
|---|
| 219 | if (size < cnt * sbd->block_size)
|
|---|
| 220 | return EINVAL;
|
|---|
| 221 |
|
|---|
| 222 | return ahci_write_blocks(sbd->sess, ba, cnt, (void *)buf);
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | /** Get device block size. */
|
|---|
| 226 | static int sata_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
|
|---|
| 227 | {
|
|---|
| 228 | sata_bd_dev_t *sbd = bd_srv_sata(bd);
|
|---|
| 229 |
|
|---|
| 230 | *rsize = sbd->block_size;
|
|---|
| 231 | return EOK;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | /** Get number of blocks on device. */
|
|---|
| 235 | static int sata_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
|
|---|
| 236 | {
|
|---|
| 237 | sata_bd_dev_t *sbd = bd_srv_sata(bd);
|
|---|
| 238 |
|
|---|
| 239 | *rnb = sbd->blocks;
|
|---|
| 240 | return EOK;
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 | int main(int argc, char **argv)
|
|---|
| 245 | {
|
|---|
| 246 | int rc;
|
|---|
| 247 |
|
|---|
| 248 | async_set_client_connection(sata_bd_connection);
|
|---|
| 249 | rc = loc_server_register(NAME);
|
|---|
| 250 | if (rc < 0) {
|
|---|
| 251 | printf(NAME ": Unable to register driver.\n");
|
|---|
| 252 | return rc;
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | rc = get_sata_disks();
|
|---|
| 256 | if (rc != EOK) {
|
|---|
| 257 | return rc;
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | for(int i=0; i < disk_count; i++) {
|
|---|
| 261 | char name[1024];
|
|---|
| 262 | snprintf(name, 1024, "%s/%s", NAMESPACE, disk[i].dev_name);
|
|---|
| 263 | rc = loc_service_register(name, &disk[i].service_id);
|
|---|
| 264 | if (rc != EOK) {
|
|---|
| 265 | printf(NAME ": Unable to register device %s.\n", name);
|
|---|
| 266 | return rc;
|
|---|
| 267 | }
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | printf(NAME ": Accepting connections\n");
|
|---|
| 271 | task_retval(0);
|
|---|
| 272 | async_manager();
|
|---|
| 273 |
|
|---|
| 274 | /* Not reached */
|
|---|
| 275 | return 0;
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | /**
|
|---|
| 279 | * @}
|
|---|
| 280 | */
|
|---|