Add DMA support to PCI-IDE / libata (register commands)
[helenos.git] / uspace / drv / block / pci-ide / main.c
blobb95360485430d5d707cac248e6ce0fd3c46de806
1 /*
2 * Copyright (c) 2024 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup pci-ide
30 * @{
33 /** @file
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>
44 #include "pci-ide.h"
45 #include "pci-ide_hw.h"
46 #include "main.h"
48 static errno_t pci_ide_dev_add(ddf_dev_t *dev);
49 static errno_t pci_ide_dev_remove(ddf_dev_t *dev);
50 static errno_t pci_ide_dev_gone(ddf_dev_t *dev);
51 static errno_t pci_ide_fun_online(ddf_fun_t *fun);
52 static errno_t pci_ide_fun_offline(ddf_fun_t *fun);
54 static void pci_ide_connection(ipc_call_t *, void *);
56 static driver_ops_t driver_ops = {
57 .dev_add = &pci_ide_dev_add,
58 .dev_remove = &pci_ide_dev_remove,
59 .dev_gone = &pci_ide_dev_gone,
60 .fun_online = &pci_ide_fun_online,
61 .fun_offline = &pci_ide_fun_offline
64 static driver_t pci_ide_driver = {
65 .name = NAME,
66 .driver_ops = &driver_ops
69 static errno_t pci_ide_get_res(ddf_dev_t *dev, pci_ide_hwres_t *res)
71 async_sess_t *parent_sess;
72 hw_res_list_parsed_t hw_res;
73 errno_t rc;
75 parent_sess = ddf_dev_parent_sess_get(dev);
76 if (parent_sess == NULL)
77 return ENOMEM;
79 hw_res_list_parsed_init(&hw_res);
80 rc = hw_res_get_list_parsed(parent_sess, &hw_res, 0);
81 if (rc != EOK)
82 return rc;
84 if (hw_res.io_ranges.count != 1) {
85 rc = EINVAL;
86 goto error;
89 /* Legacty ISA I/O ranges are fixed */
91 res->cmd1 = pci_ide_ata_cmd_p;
92 res->ctl1 = pci_ide_ata_ctl_p;
93 res->cmd2 = pci_ide_ata_cmd_s;
94 res->ctl2 = pci_ide_ata_ctl_s;
96 /* PCI I/O range */
97 addr_range_t *bmregs_rng = &hw_res.io_ranges.ranges[0];
98 res->bmregs = RNGABS(*bmregs_rng);
100 ddf_msg(LVL_NOTE, "sizes: %zu", RNGSZ(*bmregs_rng));
102 if (RNGSZ(*bmregs_rng) < sizeof(pci_ide_regs_t)) {
103 rc = EINVAL;
104 goto error;
107 /* IRQ */
108 if (hw_res.irqs.count > 0) {
109 res->irq1 = hw_res.irqs.irqs[0];
110 } else {
111 res->irq1 = -1;
114 if (hw_res.irqs.count > 1) {
115 res->irq2 = hw_res.irqs.irqs[1];
116 } else {
117 res->irq2 = -1;
120 return EOK;
121 error:
122 hw_res_list_parsed_clean(&hw_res);
123 return rc;
126 /** Add new device
128 * @param dev New device
129 * @return EOK on success or an error code.
131 static errno_t pci_ide_dev_add(ddf_dev_t *dev)
133 pci_ide_ctrl_t *ctrl;
134 pci_ide_hwres_t res;
135 errno_t rc;
137 rc = pci_ide_get_res(dev, &res);
138 if (rc != EOK) {
139 ddf_msg(LVL_ERROR, "Invalid HW resource configuration.");
140 return EINVAL;
143 ctrl = ddf_dev_data_alloc(dev, sizeof(pci_ide_ctrl_t));
144 if (ctrl == NULL) {
145 ddf_msg(LVL_ERROR, "Failed allocating soft state.");
146 rc = ENOMEM;
147 goto error;
150 ctrl->dev = dev;
152 rc = pci_ide_ctrl_init(ctrl, &res);
153 if (rc != EOK)
154 goto error;
156 rc = pci_ide_channel_init(ctrl, &ctrl->channel[0], 0, &res);
157 if (rc == ENOENT)
158 goto error;
160 rc = pci_ide_channel_init(ctrl, &ctrl->channel[1], 1, &res);
161 if (rc == ENOENT)
162 goto error;
164 if (rc != EOK) {
165 ddf_msg(LVL_ERROR, "Failed initializing ATA controller.");
166 rc = EIO;
167 goto error;
170 return EOK;
171 error:
172 return rc;
175 static char *pci_ide_fun_name(pci_ide_channel_t *chan, unsigned idx)
177 char *fun_name;
179 if (asprintf(&fun_name, "c%ud%u", chan->chan_id, idx) < 0)
180 return NULL;
182 return fun_name;
185 errno_t pci_ide_fun_create(pci_ide_channel_t *chan, unsigned idx, void *charg)
187 errno_t rc;
188 char *fun_name = NULL;
189 ddf_fun_t *fun = NULL;
190 pci_ide_fun_t *ifun = NULL;
191 bool bound = false;
193 fun_name = pci_ide_fun_name(chan, idx);
194 if (fun_name == NULL) {
195 ddf_msg(LVL_ERROR, "Out of memory.");
196 rc = ENOMEM;
197 goto error;
200 fun = ddf_fun_create(chan->ctrl->dev, fun_exposed, fun_name);
201 if (fun == NULL) {
202 ddf_msg(LVL_ERROR, "Failed creating DDF function.");
203 rc = ENOMEM;
204 goto error;
207 /* Allocate soft state */
208 ifun = ddf_fun_data_alloc(fun, sizeof(pci_ide_fun_t));
209 if (ifun == NULL) {
210 ddf_msg(LVL_ERROR, "Failed allocating softstate.");
211 rc = ENOMEM;
212 goto error;
215 ifun->fun = fun;
216 ifun->charg = charg;
218 /* Set up a connection handler. */
219 ddf_fun_set_conn_handler(fun, pci_ide_connection);
221 rc = ddf_fun_bind(fun);
222 if (rc != EOK) {
223 ddf_msg(LVL_ERROR, "Failed binding DDF function %s: %s",
224 fun_name, str_error(rc));
225 goto error;
228 bound = true;
230 rc = ddf_fun_add_to_category(fun, "disk");
231 if (rc != EOK) {
232 ddf_msg(LVL_ERROR, "Failed adding function %s to "
233 "category 'disk': %s", fun_name, str_error(rc));
234 goto error;
237 free(fun_name);
238 return EOK;
239 error:
240 if (bound)
241 ddf_fun_unbind(fun);
242 if (fun != NULL)
243 ddf_fun_destroy(fun);
244 if (fun_name != NULL)
245 free(fun_name);
247 return rc;
250 errno_t pci_ide_fun_remove(pci_ide_channel_t *chan, unsigned idx)
252 errno_t rc;
253 char *fun_name;
254 pci_ide_fun_t *ifun = chan->fun[idx];
256 fun_name = pci_ide_fun_name(chan, idx);
257 if (fun_name == NULL) {
258 ddf_msg(LVL_ERROR, "Out of memory.");
259 rc = ENOMEM;
260 goto error;
263 ddf_msg(LVL_DEBUG, "pci_ide_fun_remove(%p, '%s')", ifun, fun_name);
264 rc = ddf_fun_offline(ifun->fun);
265 if (rc != EOK) {
266 ddf_msg(LVL_ERROR, "Error offlining function '%s'.", fun_name);
267 goto error;
270 rc = ddf_fun_unbind(ifun->fun);
271 if (rc != EOK) {
272 ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", fun_name);
273 goto error;
276 ddf_fun_destroy(ifun->fun);
277 free(fun_name);
278 return EOK;
279 error:
280 if (fun_name != NULL)
281 free(fun_name);
282 return rc;
285 errno_t pci_ide_fun_unbind(pci_ide_channel_t *chan, unsigned idx)
287 errno_t rc;
288 char *fun_name;
289 pci_ide_fun_t *ifun = chan->fun[idx];
291 fun_name = pci_ide_fun_name(chan, idx);
292 if (fun_name == NULL) {
293 ddf_msg(LVL_ERROR, "Out of memory.");
294 rc = ENOMEM;
295 goto error;
298 ddf_msg(LVL_DEBUG, "pci_ide_fun_unbind(%p, '%s')", ifun, fun_name);
299 rc = ddf_fun_unbind(ifun->fun);
300 if (rc != EOK) {
301 ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", fun_name);
302 goto error;
305 ddf_fun_destroy(ifun->fun);
306 free(fun_name);
307 return EOK;
308 error:
309 if (fun_name != NULL)
310 free(fun_name);
311 return rc;
314 static errno_t pci_ide_dev_remove(ddf_dev_t *dev)
316 pci_ide_ctrl_t *ctrl = (pci_ide_ctrl_t *)ddf_dev_data_get(dev);
317 errno_t rc;
319 ddf_msg(LVL_DEBUG, "pci_ide_dev_remove(%p)", dev);
321 rc = pci_ide_channel_fini(&ctrl->channel[0]);
322 if (rc != EOK)
323 return rc;
325 rc = pci_ide_channel_fini(&ctrl->channel[1]);
326 if (rc != EOK)
327 return rc;
329 return EOK;
332 static errno_t pci_ide_dev_gone(ddf_dev_t *dev)
334 pci_ide_ctrl_t *ctrl = (pci_ide_ctrl_t *)ddf_dev_data_get(dev);
335 errno_t rc;
337 ddf_msg(LVL_DEBUG, "pci_ide_dev_gone(%p)", dev);
339 rc = pci_ide_ctrl_fini(ctrl);
340 if (rc != EOK)
341 return rc;
343 rc = pci_ide_channel_fini(&ctrl->channel[0]);
344 if (rc != EOK)
345 return rc;
347 rc = pci_ide_channel_fini(&ctrl->channel[1]);
348 if (rc != EOK)
349 return rc;
351 return EOK;
354 static errno_t pci_ide_fun_online(ddf_fun_t *fun)
356 ddf_msg(LVL_DEBUG, "pci_ide_fun_online()");
357 return ddf_fun_online(fun);
360 static errno_t pci_ide_fun_offline(ddf_fun_t *fun)
362 ddf_msg(LVL_DEBUG, "pci_ide_fun_offline()");
363 return ddf_fun_offline(fun);
366 static void pci_ide_connection(ipc_call_t *icall, void *arg)
368 pci_ide_fun_t *ifun;
370 ifun = (pci_ide_fun_t *) ddf_fun_data_get((ddf_fun_t *)arg);
371 ata_connection(icall, ifun->charg);
374 int main(int argc, char *argv[])
376 printf(NAME ": HelenOS PCI IDE device driver\n");
377 ddf_log_init(NAME);
378 return ddf_driver_main(&pci_ide_driver);
382 * @}