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