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