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

Last change on this file since 95feb3e9 was d7f7a4a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Replace some license headers with SPDX identifier

Headers are replaced using tools/transorm-copyright.sh only
when it can be matched verbatim with the license header used
throughout most of the codebase.

  • Property mode set to 100644
File size: 6.2 KB
Line 
1/*
2 * SPDX-FileCopyrightText: 2013 Jiri Svoboda
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7/** @addtogroup ata_bd
8 * @{
9 */
10
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>
20#include <device/hw_res_parsed.h>
21
22#include "ata_bd.h"
23#include "main.h"
24
25static errno_t ata_dev_add(ddf_dev_t *dev);
26static errno_t ata_dev_remove(ddf_dev_t *dev);
27static errno_t ata_dev_gone(ddf_dev_t *dev);
28static errno_t ata_fun_online(ddf_fun_t *fun);
29static errno_t ata_fun_offline(ddf_fun_t *fun);
30
31static void ata_bd_connection(ipc_call_t *, void *);
32
33static 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
41static driver_t ata_driver = {
42 .name = NAME,
43 .driver_ops = &driver_ops
44};
45
46static errno_t ata_get_res(ddf_dev_t *dev, ata_base_t *ata_res)
47{
48 async_sess_t *parent_sess;
49 hw_res_list_parsed_t hw_res;
50 errno_t rc;
51
52 parent_sess = ddf_dev_parent_sess_get(dev);
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
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);
70
71 if (RNGSZ(*ctl_rng) < sizeof(ata_ctl_t)) {
72 rc = EINVAL;
73 goto error;
74 }
75
76 if (RNGSZ(*cmd_rng) < sizeof(ata_cmd_t)) {
77 rc = EINVAL;
78 goto error;
79 }
80
81 return EOK;
82error:
83 hw_res_list_parsed_clean(&hw_res);
84 return rc;
85}
86
87/** Add new device
88 *
89 * @param dev New device
90 * @return EOK on success or an error code.
91 */
92static errno_t ata_dev_add(ddf_dev_t *dev)
93{
94 ata_ctrl_t *ctrl;
95 ata_base_t res;
96 errno_t rc;
97
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
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
113 rc = ata_ctrl_init(ctrl, &res);
114 if (rc == ENOENT)
115 goto error;
116
117 if (rc != EOK) {
118 ddf_msg(LVL_ERROR, "Failed initializing ATA controller.");
119 rc = EIO;
120 goto error;
121 }
122
123 return EOK;
124error:
125 return rc;
126}
127
128static 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
138errno_t ata_fun_create(disk_t *disk)
139{
140 ata_ctrl_t *ctrl = disk->ctrl;
141 errno_t rc;
142 char *fun_name = NULL;
143 ddf_fun_t *fun = NULL;
144 ata_fun_t *afun = NULL;
145 bool bound = false;
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
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 }
194
195 free(fun_name);
196 disk->afun = afun;
197 return EOK;
198error:
199 if (bound)
200 ddf_fun_unbind(fun);
201 if (fun != NULL)
202 ddf_fun_destroy(fun);
203 if (fun_name != NULL)
204 free(fun_name);
205
206 return rc;
207}
208
209errno_t ata_fun_remove(disk_t *disk)
210{
211 errno_t rc;
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;
241error:
242 if (fun_name != NULL)
243 free(fun_name);
244 return rc;
245}
246
247errno_t ata_fun_unbind(disk_t *disk)
248{
249 errno_t rc;
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;
273error:
274 if (fun_name != NULL)
275 free(fun_name);
276 return rc;
277}
278
279static errno_t ata_dev_remove(ddf_dev_t *dev)
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
288static errno_t ata_dev_gone(ddf_dev_t *dev)
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
297static errno_t ata_fun_online(ddf_fun_t *fun)
298{
299 ddf_msg(LVL_DEBUG, "ata_fun_online()");
300 return ddf_fun_online(fun);
301}
302
303static errno_t ata_fun_offline(ddf_fun_t *fun)
304{
305 ddf_msg(LVL_DEBUG, "ata_fun_offline()");
306 return ddf_fun_offline(fun);
307}
308
309/** Block device connection handler */
310static void ata_bd_connection(ipc_call_t *icall, void *arg)
311{
312 ata_fun_t *afun;
313
314 afun = (ata_fun_t *) ddf_fun_data_get((ddf_fun_t *)arg);
315 bd_conn(icall, &afun->bds);
316}
317
318int 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
325/**
326 * @}
327 */
Note: See TracBrowser for help on using the repository browser.