[66cb7a2] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Jiri Svoboda
|
---|
| 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 |
|
---|
[c8ea6eca] | 29 | /** @addtogroup ata_bd
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[66cb7a2] | 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <assert.h>
|
---|
| 37 | #include <stdio.h>
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include <str_error.h>
|
---|
| 40 | #include <ddf/driver.h>
|
---|
| 41 | #include <ddf/log.h>
|
---|
[3de67b4c] | 42 | #include <device/hw_res_parsed.h>
|
---|
[66cb7a2] | 43 |
|
---|
| 44 | #include "ata_bd.h"
|
---|
| 45 | #include "main.h"
|
---|
| 46 |
|
---|
[b7fd2a0] | 47 | static errno_t ata_dev_add(ddf_dev_t *dev);
|
---|
| 48 | static errno_t ata_dev_remove(ddf_dev_t *dev);
|
---|
| 49 | static errno_t ata_dev_gone(ddf_dev_t *dev);
|
---|
| 50 | static errno_t ata_fun_online(ddf_fun_t *fun);
|
---|
| 51 | static errno_t ata_fun_offline(ddf_fun_t *fun);
|
---|
[66cb7a2] | 52 |
|
---|
[984a9ba] | 53 | static void ata_bd_connection(ipc_call_t *, void *);
|
---|
[66cb7a2] | 54 |
|
---|
| 55 | static driver_ops_t driver_ops = {
|
---|
| 56 | .dev_add = &ata_dev_add,
|
---|
| 57 | .dev_remove = &ata_dev_remove,
|
---|
| 58 | .dev_gone = &ata_dev_gone,
|
---|
| 59 | .fun_online = &ata_fun_online,
|
---|
| 60 | .fun_offline = &ata_fun_offline
|
---|
| 61 | };
|
---|
| 62 |
|
---|
| 63 | static driver_t ata_driver = {
|
---|
| 64 | .name = NAME,
|
---|
| 65 | .driver_ops = &driver_ops
|
---|
| 66 | };
|
---|
| 67 |
|
---|
[b7fd2a0] | 68 | static errno_t ata_get_res(ddf_dev_t *dev, ata_base_t *ata_res)
|
---|
[3de67b4c] | 69 | {
|
---|
| 70 | async_sess_t *parent_sess;
|
---|
| 71 | hw_res_list_parsed_t hw_res;
|
---|
[b7fd2a0] | 72 | errno_t rc;
|
---|
[3de67b4c] | 73 |
|
---|
[2fd26bb] | 74 | parent_sess = ddf_dev_parent_sess_get(dev);
|
---|
[3de67b4c] | 75 | if (parent_sess == NULL)
|
---|
| 76 | return ENOMEM;
|
---|
| 77 |
|
---|
| 78 | hw_res_list_parsed_init(&hw_res);
|
---|
| 79 | rc = hw_res_get_list_parsed(parent_sess, &hw_res, 0);
|
---|
| 80 | if (rc != EOK)
|
---|
| 81 | return rc;
|
---|
| 82 |
|
---|
| 83 | if (hw_res.io_ranges.count != 2) {
|
---|
| 84 | rc = EINVAL;
|
---|
| 85 | goto error;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[7de1988c] | 88 | addr_range_t *cmd_rng = &hw_res.io_ranges.ranges[0];
|
---|
| 89 | addr_range_t *ctl_rng = &hw_res.io_ranges.ranges[1];
|
---|
| 90 | ata_res->cmd = RNGABS(*cmd_rng);
|
---|
| 91 | ata_res->ctl = RNGABS(*ctl_rng);
|
---|
[3de67b4c] | 92 |
|
---|
[7de1988c] | 93 | if (RNGSZ(*ctl_rng) < sizeof(ata_ctl_t)) {
|
---|
[3de67b4c] | 94 | rc = EINVAL;
|
---|
| 95 | goto error;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[7de1988c] | 98 | if (RNGSZ(*cmd_rng) < sizeof(ata_cmd_t)) {
|
---|
[3de67b4c] | 99 | rc = EINVAL;
|
---|
| 100 | goto error;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | return EOK;
|
---|
| 104 | error:
|
---|
| 105 | hw_res_list_parsed_clean(&hw_res);
|
---|
| 106 | return rc;
|
---|
| 107 | }
|
---|
[66cb7a2] | 108 |
|
---|
| 109 | /** Add new device
|
---|
| 110 | *
|
---|
| 111 | * @param dev New device
|
---|
[cde999a] | 112 | * @return EOK on success or an error code.
|
---|
[66cb7a2] | 113 | */
|
---|
[b7fd2a0] | 114 | static errno_t ata_dev_add(ddf_dev_t *dev)
|
---|
[66cb7a2] | 115 | {
|
---|
| 116 | ata_ctrl_t *ctrl;
|
---|
[3de67b4c] | 117 | ata_base_t res;
|
---|
[b7fd2a0] | 118 | errno_t rc;
|
---|
[66cb7a2] | 119 |
|
---|
[3de67b4c] | 120 | rc = ata_get_res(dev, &res);
|
---|
| 121 | if (rc != EOK) {
|
---|
| 122 | ddf_msg(LVL_ERROR, "Invalid HW resource configuration.");
|
---|
| 123 | return EINVAL;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[66cb7a2] | 126 | ctrl = ddf_dev_data_alloc(dev, sizeof(ata_ctrl_t));
|
---|
| 127 | if (ctrl == NULL) {
|
---|
| 128 | ddf_msg(LVL_ERROR, "Failed allocating soft state.");
|
---|
| 129 | rc = ENOMEM;
|
---|
| 130 | goto error;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | ctrl->dev = dev;
|
---|
| 134 |
|
---|
[3de67b4c] | 135 | rc = ata_ctrl_init(ctrl, &res);
|
---|
[eaf4e2fc] | 136 | if (rc == ENOENT)
|
---|
| 137 | goto error;
|
---|
| 138 |
|
---|
[66cb7a2] | 139 | if (rc != EOK) {
|
---|
| 140 | ddf_msg(LVL_ERROR, "Failed initializing ATA controller.");
|
---|
| 141 | rc = EIO;
|
---|
| 142 | goto error;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | return EOK;
|
---|
| 146 | error:
|
---|
| 147 | return rc;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | static char *ata_fun_name(disk_t *disk)
|
---|
| 151 | {
|
---|
| 152 | char *fun_name;
|
---|
| 153 |
|
---|
| 154 | if (asprintf(&fun_name, "d%u", disk->disk_id) < 0)
|
---|
| 155 | return NULL;
|
---|
| 156 |
|
---|
| 157 | return fun_name;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[b7fd2a0] | 160 | errno_t ata_fun_create(disk_t *disk)
|
---|
[66cb7a2] | 161 | {
|
---|
| 162 | ata_ctrl_t *ctrl = disk->ctrl;
|
---|
[b7fd2a0] | 163 | errno_t rc;
|
---|
[66cb7a2] | 164 | char *fun_name = NULL;
|
---|
| 165 | ddf_fun_t *fun = NULL;
|
---|
| 166 | ata_fun_t *afun = NULL;
|
---|
[4f87a85a] | 167 | bool bound = false;
|
---|
[66cb7a2] | 168 |
|
---|
| 169 | fun_name = ata_fun_name(disk);
|
---|
| 170 | if (fun_name == NULL) {
|
---|
| 171 | ddf_msg(LVL_ERROR, "Out of memory.");
|
---|
| 172 | rc = ENOMEM;
|
---|
| 173 | goto error;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | fun = ddf_fun_create(ctrl->dev, fun_exposed, fun_name);
|
---|
| 177 | if (fun == NULL) {
|
---|
| 178 | ddf_msg(LVL_ERROR, "Failed creating DDF function.");
|
---|
| 179 | rc = ENOMEM;
|
---|
| 180 | goto error;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /* Allocate soft state */
|
---|
| 184 | afun = ddf_fun_data_alloc(fun, sizeof(ata_fun_t));
|
---|
| 185 | if (afun == NULL) {
|
---|
| 186 | ddf_msg(LVL_ERROR, "Failed allocating softstate.");
|
---|
| 187 | rc = ENOMEM;
|
---|
| 188 | goto error;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | afun->fun = fun;
|
---|
| 192 | afun->disk = disk;
|
---|
| 193 |
|
---|
| 194 | bd_srvs_init(&afun->bds);
|
---|
| 195 | afun->bds.ops = &ata_bd_ops;
|
---|
| 196 | afun->bds.sarg = disk;
|
---|
| 197 |
|
---|
| 198 | /* Set up a connection handler. */
|
---|
| 199 | ddf_fun_set_conn_handler(fun, ata_bd_connection);
|
---|
| 200 |
|
---|
| 201 | rc = ddf_fun_bind(fun);
|
---|
| 202 | if (rc != EOK) {
|
---|
| 203 | ddf_msg(LVL_ERROR, "Failed binding DDF function %s: %s",
|
---|
| 204 | fun_name, str_error(rc));
|
---|
| 205 | goto error;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[4f87a85a] | 208 | bound = true;
|
---|
| 209 |
|
---|
| 210 | rc = ddf_fun_add_to_category(fun, "disk");
|
---|
| 211 | if (rc != EOK) {
|
---|
| 212 | ddf_msg(LVL_ERROR, "Failed adding function %s to "
|
---|
| 213 | "category 'disk': %s", fun_name, str_error(rc));
|
---|
| 214 | goto error;
|
---|
| 215 | }
|
---|
[c0dea01] | 216 |
|
---|
[66cb7a2] | 217 | free(fun_name);
|
---|
| 218 | disk->afun = afun;
|
---|
| 219 | return EOK;
|
---|
| 220 | error:
|
---|
[4f87a85a] | 221 | if (bound)
|
---|
| 222 | ddf_fun_unbind(fun);
|
---|
[66cb7a2] | 223 | if (fun != NULL)
|
---|
| 224 | ddf_fun_destroy(fun);
|
---|
| 225 | if (fun_name != NULL)
|
---|
| 226 | free(fun_name);
|
---|
| 227 |
|
---|
| 228 | return rc;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[b7fd2a0] | 231 | errno_t ata_fun_remove(disk_t *disk)
|
---|
[66cb7a2] | 232 | {
|
---|
[b7fd2a0] | 233 | errno_t rc;
|
---|
[66cb7a2] | 234 | char *fun_name;
|
---|
| 235 |
|
---|
| 236 | if (disk->afun == NULL)
|
---|
| 237 | return EOK;
|
---|
| 238 |
|
---|
| 239 | fun_name = ata_fun_name(disk);
|
---|
| 240 | if (fun_name == NULL) {
|
---|
| 241 | ddf_msg(LVL_ERROR, "Out of memory.");
|
---|
| 242 | rc = ENOMEM;
|
---|
| 243 | goto error;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | ddf_msg(LVL_DEBUG, "ata_fun_remove(%p, '%s')", disk, fun_name);
|
---|
| 247 | rc = ddf_fun_offline(disk->afun->fun);
|
---|
| 248 | if (rc != EOK) {
|
---|
| 249 | ddf_msg(LVL_ERROR, "Error offlining function '%s'.", fun_name);
|
---|
| 250 | goto error;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | rc = ddf_fun_unbind(disk->afun->fun);
|
---|
| 254 | if (rc != EOK) {
|
---|
| 255 | ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", fun_name);
|
---|
| 256 | goto error;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | ddf_fun_destroy(disk->afun->fun);
|
---|
| 260 | disk->afun = NULL;
|
---|
| 261 | free(fun_name);
|
---|
| 262 | return EOK;
|
---|
| 263 | error:
|
---|
| 264 | if (fun_name != NULL)
|
---|
| 265 | free(fun_name);
|
---|
| 266 | return rc;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[b7fd2a0] | 269 | errno_t ata_fun_unbind(disk_t *disk)
|
---|
[66cb7a2] | 270 | {
|
---|
[b7fd2a0] | 271 | errno_t rc;
|
---|
[66cb7a2] | 272 | char *fun_name;
|
---|
| 273 |
|
---|
| 274 | if (disk->afun == NULL)
|
---|
| 275 | return EOK;
|
---|
| 276 |
|
---|
| 277 | fun_name = ata_fun_name(disk);
|
---|
| 278 | if (fun_name == NULL) {
|
---|
| 279 | ddf_msg(LVL_ERROR, "Out of memory.");
|
---|
| 280 | rc = ENOMEM;
|
---|
| 281 | goto error;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | ddf_msg(LVL_DEBUG, "ata_fun_unbind(%p, '%s')", disk, fun_name);
|
---|
| 285 | rc = ddf_fun_unbind(disk->afun->fun);
|
---|
| 286 | if (rc != EOK) {
|
---|
| 287 | ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", fun_name);
|
---|
| 288 | goto error;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | ddf_fun_destroy(disk->afun->fun);
|
---|
| 292 | disk->afun = NULL;
|
---|
| 293 | free(fun_name);
|
---|
| 294 | return EOK;
|
---|
| 295 | error:
|
---|
| 296 | if (fun_name != NULL)
|
---|
| 297 | free(fun_name);
|
---|
| 298 | return rc;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
[b7fd2a0] | 301 | static errno_t ata_dev_remove(ddf_dev_t *dev)
|
---|
[66cb7a2] | 302 | {
|
---|
| 303 | ata_ctrl_t *ctrl = (ata_ctrl_t *)ddf_dev_data_get(dev);
|
---|
| 304 |
|
---|
| 305 | ddf_msg(LVL_DEBUG, "ata_dev_remove(%p)", dev);
|
---|
| 306 |
|
---|
| 307 | return ata_ctrl_remove(ctrl);
|
---|
| 308 | }
|
---|
| 309 |
|
---|
[b7fd2a0] | 310 | static errno_t ata_dev_gone(ddf_dev_t *dev)
|
---|
[66cb7a2] | 311 | {
|
---|
| 312 | ata_ctrl_t *ctrl = (ata_ctrl_t *)ddf_dev_data_get(dev);
|
---|
| 313 |
|
---|
| 314 | ddf_msg(LVL_DEBUG, "ata_dev_gone(%p)", dev);
|
---|
| 315 |
|
---|
| 316 | return ata_ctrl_gone(ctrl);
|
---|
| 317 | }
|
---|
| 318 |
|
---|
[b7fd2a0] | 319 | static errno_t ata_fun_online(ddf_fun_t *fun)
|
---|
[66cb7a2] | 320 | {
|
---|
| 321 | ddf_msg(LVL_DEBUG, "ata_fun_online()");
|
---|
| 322 | return ddf_fun_online(fun);
|
---|
| 323 | }
|
---|
| 324 |
|
---|
[b7fd2a0] | 325 | static errno_t ata_fun_offline(ddf_fun_t *fun)
|
---|
[66cb7a2] | 326 | {
|
---|
| 327 | ddf_msg(LVL_DEBUG, "ata_fun_offline()");
|
---|
| 328 | return ddf_fun_offline(fun);
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | /** Block device connection handler */
|
---|
[984a9ba] | 332 | static void ata_bd_connection(ipc_call_t *icall, void *arg)
|
---|
[66cb7a2] | 333 | {
|
---|
| 334 | ata_fun_t *afun;
|
---|
| 335 |
|
---|
| 336 | afun = (ata_fun_t *) ddf_fun_data_get((ddf_fun_t *)arg);
|
---|
[984a9ba] | 337 | bd_conn(icall, &afun->bds);
|
---|
[66cb7a2] | 338 | }
|
---|
| 339 |
|
---|
| 340 | int main(int argc, char *argv[])
|
---|
| 341 | {
|
---|
| 342 | printf(NAME ": HelenOS ATA(PI) device driver\n");
|
---|
| 343 | ddf_log_init(NAME);
|
---|
| 344 | return ddf_driver_main(&ata_driver);
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[c8ea6eca] | 347 | /**
|
---|
| 348 | * @}
|
---|
| 349 | */
|
---|