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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 96323d2 was 43523b1, checked in by Maurizio Lombardi <m.lombardi85@…>, 11 years ago

ata_bd: remove unreacheable code

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