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