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

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

Fdisk first prototype.

  • 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 ddf_fun_add_to_category(fun, "disk");
203
204 free(fun_name);
205 disk->afun = afun;
206 return EOK;
207error:
208 if (fun != NULL)
209 ddf_fun_destroy(fun);
210 if (fun_name != NULL)
211 free(fun_name);
212
213 return rc;
214}
215
216int 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;
248error:
249 if (fun_name != NULL)
250 free(fun_name);
251 return rc;
252}
253
254int 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;
280error:
281 if (fun_name != NULL)
282 free(fun_name);
283 return rc;
284}
285
286static 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
295static 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
304static 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
310static 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 */
317static 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
325int 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
Note: See TracBrowser for help on using the repository browser.