source: mainline/uspace/drv/block/ata_bd/main.c@ b9be9b0

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b9be9b0 was eaf4e2fc, checked in by Jiri Svoboda <jiri@…>, 6 years ago

Make ATA driver less verbose

  • Property mode set to 100644
File size: 7.5 KB
Line 
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/** @addtogroup ata_bd
30 * @{
31 */
32
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>
42#include <device/hw_res_parsed.h>
43
44#include "ata_bd.h"
45#include "main.h"
46
47static errno_t ata_dev_add(ddf_dev_t *dev);
48static errno_t ata_dev_remove(ddf_dev_t *dev);
49static errno_t ata_dev_gone(ddf_dev_t *dev);
50static errno_t ata_fun_online(ddf_fun_t *fun);
51static errno_t ata_fun_offline(ddf_fun_t *fun);
52
53static void ata_bd_connection(ipc_call_t *, void *);
54
55static 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
63static driver_t ata_driver = {
64 .name = NAME,
65 .driver_ops = &driver_ops
66};
67
68static errno_t ata_get_res(ddf_dev_t *dev, ata_base_t *ata_res)
69{
70 async_sess_t *parent_sess;
71 hw_res_list_parsed_t hw_res;
72 errno_t rc;
73
74 parent_sess = ddf_dev_parent_sess_get(dev);
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
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);
92
93 if (RNGSZ(*ctl_rng) < sizeof(ata_ctl_t)) {
94 rc = EINVAL;
95 goto error;
96 }
97
98 if (RNGSZ(*cmd_rng) < sizeof(ata_cmd_t)) {
99 rc = EINVAL;
100 goto error;
101 }
102
103 return EOK;
104error:
105 hw_res_list_parsed_clean(&hw_res);
106 return rc;
107}
108
109/** Add new device
110 *
111 * @param dev New device
112 * @return EOK on success or an error code.
113 */
114static errno_t ata_dev_add(ddf_dev_t *dev)
115{
116 ata_ctrl_t *ctrl;
117 ata_base_t res;
118 errno_t rc;
119
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
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
135 rc = ata_ctrl_init(ctrl, &res);
136 if (rc == ENOENT)
137 goto error;
138
139 if (rc != EOK) {
140 ddf_msg(LVL_ERROR, "Failed initializing ATA controller.");
141 rc = EIO;
142 goto error;
143 }
144
145 return EOK;
146error:
147 return rc;
148}
149
150static 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
160errno_t ata_fun_create(disk_t *disk)
161{
162 ata_ctrl_t *ctrl = disk->ctrl;
163 errno_t rc;
164 char *fun_name = NULL;
165 ddf_fun_t *fun = NULL;
166 ata_fun_t *afun = NULL;
167 bool bound = false;
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
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 }
216
217 free(fun_name);
218 disk->afun = afun;
219 return EOK;
220error:
221 if (bound)
222 ddf_fun_unbind(fun);
223 if (fun != NULL)
224 ddf_fun_destroy(fun);
225 if (fun_name != NULL)
226 free(fun_name);
227
228 return rc;
229}
230
231errno_t ata_fun_remove(disk_t *disk)
232{
233 errno_t rc;
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;
263error:
264 if (fun_name != NULL)
265 free(fun_name);
266 return rc;
267}
268
269errno_t ata_fun_unbind(disk_t *disk)
270{
271 errno_t rc;
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;
295error:
296 if (fun_name != NULL)
297 free(fun_name);
298 return rc;
299}
300
301static errno_t ata_dev_remove(ddf_dev_t *dev)
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
310static errno_t ata_dev_gone(ddf_dev_t *dev)
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
319static errno_t ata_fun_online(ddf_fun_t *fun)
320{
321 ddf_msg(LVL_DEBUG, "ata_fun_online()");
322 return ddf_fun_online(fun);
323}
324
325static errno_t ata_fun_offline(ddf_fun_t *fun)
326{
327 ddf_msg(LVL_DEBUG, "ata_fun_offline()");
328 return ddf_fun_offline(fun);
329}
330
331/** Block device connection handler */
332static void ata_bd_connection(ipc_call_t *icall, void *arg)
333{
334 ata_fun_t *afun;
335
336 afun = (ata_fun_t *) ddf_fun_data_get((ddf_fun_t *)arg);
337 bd_conn(icall, &afun->bds);
338}
339
340int 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
347/**
348 * @}
349 */
Note: See TracBrowser for help on using the repository browser.