source: mainline/uspace/drv/block/pci-ide/main.c@ 144fafd

Last change on this file since 144fafd was 2a5d4649, checked in by Jiri Svoboda <jiri@…>, 11 months ago

ISA/PCI IDE needs to attach even if one channel is empty.

  • Property mode set to 100644
File size: 9.2 KB
Line 
1/*
2 * Copyright (c) 2025 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 pci-ide
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 "pci-ide.h"
45#include "pci-ide_hw.h"
46#include "main.h"
47
48static errno_t pci_ide_dev_add(ddf_dev_t *dev);
49static errno_t pci_ide_dev_remove(ddf_dev_t *dev);
50static errno_t pci_ide_dev_gone(ddf_dev_t *dev);
51static errno_t pci_ide_dev_quiesce(ddf_dev_t *dev);
52static errno_t pci_ide_fun_online(ddf_fun_t *fun);
53static errno_t pci_ide_fun_offline(ddf_fun_t *fun);
54
55static void pci_ide_connection(ipc_call_t *, void *);
56
57static driver_ops_t driver_ops = {
58 .dev_add = pci_ide_dev_add,
59 .dev_remove = pci_ide_dev_remove,
60 .dev_gone = pci_ide_dev_gone,
61 .dev_quiesce = pci_ide_dev_quiesce,
62 .fun_online = pci_ide_fun_online,
63 .fun_offline = pci_ide_fun_offline
64};
65
66static driver_t pci_ide_driver = {
67 .name = NAME,
68 .driver_ops = &driver_ops
69};
70
71static errno_t pci_ide_get_res(ddf_dev_t *dev, pci_ide_hwres_t *res)
72{
73 async_sess_t *parent_sess;
74 hw_res_list_parsed_t hw_res;
75 errno_t rc;
76
77 parent_sess = ddf_dev_parent_sess_get(dev);
78 if (parent_sess == NULL)
79 return ENOMEM;
80
81 hw_res_list_parsed_init(&hw_res);
82 rc = hw_res_get_list_parsed(parent_sess, &hw_res, 0);
83 if (rc != EOK)
84 return rc;
85
86 if (hw_res.io_ranges.count != 1) {
87 rc = EINVAL;
88 goto error;
89 }
90
91 /* Legacty ISA I/O ranges are fixed */
92
93 res->cmd1 = pci_ide_ata_cmd_p;
94 res->ctl1 = pci_ide_ata_ctl_p;
95 res->cmd2 = pci_ide_ata_cmd_s;
96 res->ctl2 = pci_ide_ata_ctl_s;
97
98 /* PCI I/O range */
99 addr_range_t *bmregs_rng = &hw_res.io_ranges.ranges[0];
100 res->bmregs = RNGABS(*bmregs_rng);
101
102 ddf_msg(LVL_NOTE, "sizes: %zu", RNGSZ(*bmregs_rng));
103
104 if (RNGSZ(*bmregs_rng) < sizeof(pci_ide_regs_t)) {
105 rc = EINVAL;
106 goto error;
107 }
108
109 /* IRQ */
110 if (hw_res.irqs.count > 0) {
111 res->irq1 = hw_res.irqs.irqs[0];
112 } else {
113 res->irq1 = -1;
114 }
115
116 if (hw_res.irqs.count > 1) {
117 res->irq2 = hw_res.irqs.irqs[1];
118 } else {
119 res->irq2 = -1;
120 }
121
122 return EOK;
123error:
124 hw_res_list_parsed_clean(&hw_res);
125 return rc;
126}
127
128/** Add new device
129 *
130 * @param dev New device
131 * @return EOK on success or an error code.
132 */
133static errno_t pci_ide_dev_add(ddf_dev_t *dev)
134{
135 pci_ide_ctrl_t *ctrl;
136 pci_ide_hwres_t res;
137 async_sess_t *parent_sess;
138 unsigned chans;
139 errno_t rc;
140
141 rc = pci_ide_get_res(dev, &res);
142 if (rc != EOK) {
143 ddf_msg(LVL_ERROR, "Invalid HW resource configuration.");
144 return EINVAL;
145 }
146
147 ctrl = ddf_dev_data_alloc(dev, sizeof(pci_ide_ctrl_t));
148 if (ctrl == NULL) {
149 ddf_msg(LVL_ERROR, "Failed allocating soft state.");
150 rc = ENOMEM;
151 goto error;
152 }
153
154 ctrl->dev = dev;
155
156 rc = pci_ide_ctrl_init(ctrl, &res);
157 if (rc != EOK)
158 goto error;
159
160 chans = 0;
161
162 rc = pci_ide_channel_init(ctrl, &ctrl->channel[0], 0, &res);
163 if (rc == EOK)
164 ++chans;
165 else if (rc != ENOENT)
166 goto error;
167
168 rc = pci_ide_channel_init(ctrl, &ctrl->channel[1], 1, &res);
169 if (rc == EOK)
170 ++chans;
171 else if (rc != ENOENT)
172 goto error;
173
174 if (chans == 0) {
175 ddf_msg(LVL_ERROR, "No PCI IDE devices found.");
176 rc = EIO;
177 goto error;
178 }
179
180 parent_sess = ddf_dev_parent_sess_get(dev);
181 if (parent_sess == NULL) {
182 rc = ENOMEM;
183 goto error;
184 }
185
186 /* Claim legacy I/O range to prevent ISA IDE from attaching there. */
187 rc = hw_res_claim_legacy_io(parent_sess, hwc_isa_ide);
188 if (rc != EOK) {
189 ddf_msg(LVL_ERROR, "Failed claiming legacy I/O range.");
190 rc = EIO;
191 goto error;
192 }
193
194 return EOK;
195error:
196 return rc;
197}
198
199static char *pci_ide_fun_name(pci_ide_channel_t *chan, unsigned idx)
200{
201 char *fun_name;
202
203 if (asprintf(&fun_name, "c%ud%u", chan->chan_id, idx) < 0)
204 return NULL;
205
206 return fun_name;
207}
208
209errno_t pci_ide_fun_create(pci_ide_channel_t *chan, unsigned idx, void *charg)
210{
211 errno_t rc;
212 char *fun_name = NULL;
213 ddf_fun_t *fun = NULL;
214 pci_ide_fun_t *ifun = NULL;
215 bool bound = false;
216
217 fun_name = pci_ide_fun_name(chan, idx);
218 if (fun_name == NULL) {
219 ddf_msg(LVL_ERROR, "Out of memory.");
220 rc = ENOMEM;
221 goto error;
222 }
223
224 fun = ddf_fun_create(chan->ctrl->dev, fun_exposed, fun_name);
225 if (fun == NULL) {
226 ddf_msg(LVL_ERROR, "Failed creating DDF function.");
227 rc = ENOMEM;
228 goto error;
229 }
230
231 /* Allocate soft state */
232 ifun = ddf_fun_data_alloc(fun, sizeof(pci_ide_fun_t));
233 if (ifun == NULL) {
234 ddf_msg(LVL_ERROR, "Failed allocating softstate.");
235 rc = ENOMEM;
236 goto error;
237 }
238
239 ifun->fun = fun;
240 ifun->charg = charg;
241
242 /* Set up a connection handler. */
243 ddf_fun_set_conn_handler(fun, pci_ide_connection);
244
245 rc = ddf_fun_bind(fun);
246 if (rc != EOK) {
247 ddf_msg(LVL_ERROR, "Failed binding DDF function %s: %s",
248 fun_name, str_error(rc));
249 goto error;
250 }
251
252 bound = true;
253
254 rc = ddf_fun_add_to_category(fun, "disk");
255 if (rc != EOK) {
256 ddf_msg(LVL_ERROR, "Failed adding function %s to "
257 "category 'disk': %s", fun_name, str_error(rc));
258 goto error;
259 }
260
261 free(fun_name);
262 return EOK;
263error:
264 if (bound)
265 ddf_fun_unbind(fun);
266 if (fun != NULL)
267 ddf_fun_destroy(fun);
268 if (fun_name != NULL)
269 free(fun_name);
270
271 return rc;
272}
273
274errno_t pci_ide_fun_remove(pci_ide_channel_t *chan, unsigned idx)
275{
276 errno_t rc;
277 char *fun_name;
278 pci_ide_fun_t *ifun = chan->fun[idx];
279
280 fun_name = pci_ide_fun_name(chan, idx);
281 if (fun_name == NULL) {
282 ddf_msg(LVL_ERROR, "Out of memory.");
283 rc = ENOMEM;
284 goto error;
285 }
286
287 ddf_msg(LVL_DEBUG, "pci_ide_fun_remove(%p, '%s')", ifun, fun_name);
288 rc = ddf_fun_offline(ifun->fun);
289 if (rc != EOK) {
290 ddf_msg(LVL_ERROR, "Error offlining function '%s'.", fun_name);
291 goto error;
292 }
293
294 rc = ddf_fun_unbind(ifun->fun);
295 if (rc != EOK) {
296 ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", fun_name);
297 goto error;
298 }
299
300 ddf_fun_destroy(ifun->fun);
301 free(fun_name);
302 return EOK;
303error:
304 if (fun_name != NULL)
305 free(fun_name);
306 return rc;
307}
308
309errno_t pci_ide_fun_unbind(pci_ide_channel_t *chan, unsigned idx)
310{
311 errno_t rc;
312 char *fun_name;
313 pci_ide_fun_t *ifun = chan->fun[idx];
314
315 fun_name = pci_ide_fun_name(chan, idx);
316 if (fun_name == NULL) {
317 ddf_msg(LVL_ERROR, "Out of memory.");
318 rc = ENOMEM;
319 goto error;
320 }
321
322 ddf_msg(LVL_DEBUG, "pci_ide_fun_unbind(%p, '%s')", ifun, fun_name);
323 rc = ddf_fun_unbind(ifun->fun);
324 if (rc != EOK) {
325 ddf_msg(LVL_ERROR, "Failed unbinding function '%s'.", fun_name);
326 goto error;
327 }
328
329 ddf_fun_destroy(ifun->fun);
330 free(fun_name);
331 return EOK;
332error:
333 if (fun_name != NULL)
334 free(fun_name);
335 return rc;
336}
337
338static errno_t pci_ide_dev_remove(ddf_dev_t *dev)
339{
340 pci_ide_ctrl_t *ctrl = (pci_ide_ctrl_t *)ddf_dev_data_get(dev);
341 errno_t rc;
342
343 ddf_msg(LVL_DEBUG, "pci_ide_dev_remove(%p)", dev);
344
345 rc = pci_ide_channel_fini(&ctrl->channel[0]);
346 if (rc != EOK)
347 return rc;
348
349 rc = pci_ide_channel_fini(&ctrl->channel[1]);
350 if (rc != EOK)
351 return rc;
352
353 return EOK;
354}
355
356static errno_t pci_ide_dev_gone(ddf_dev_t *dev)
357{
358 pci_ide_ctrl_t *ctrl = (pci_ide_ctrl_t *)ddf_dev_data_get(dev);
359 errno_t rc;
360
361 ddf_msg(LVL_DEBUG, "pci_ide_dev_gone(%p)", dev);
362
363 rc = pci_ide_ctrl_fini(ctrl);
364 if (rc != EOK)
365 return rc;
366
367 rc = pci_ide_channel_fini(&ctrl->channel[0]);
368 if (rc != EOK)
369 return rc;
370
371 rc = pci_ide_channel_fini(&ctrl->channel[1]);
372 if (rc != EOK)
373 return rc;
374
375 return EOK;
376}
377
378static errno_t pci_ide_dev_quiesce(ddf_dev_t *dev)
379{
380 pci_ide_ctrl_t *ctrl = (pci_ide_ctrl_t *)ddf_dev_data_get(dev);
381
382 ddf_msg(LVL_DEBUG, "pci_ide_dev_quiesce(%p)", dev);
383
384 pci_ide_channel_quiesce(&ctrl->channel[0]);
385 pci_ide_channel_quiesce(&ctrl->channel[1]);
386
387 return EOK;
388}
389
390static errno_t pci_ide_fun_online(ddf_fun_t *fun)
391{
392 ddf_msg(LVL_DEBUG, "pci_ide_fun_online()");
393 return ddf_fun_online(fun);
394}
395
396static errno_t pci_ide_fun_offline(ddf_fun_t *fun)
397{
398 ddf_msg(LVL_DEBUG, "pci_ide_fun_offline()");
399 return ddf_fun_offline(fun);
400}
401
402static void pci_ide_connection(ipc_call_t *icall, void *arg)
403{
404 pci_ide_fun_t *ifun;
405
406 ifun = (pci_ide_fun_t *) ddf_fun_data_get((ddf_fun_t *)arg);
407 ata_connection(icall, ifun->charg);
408}
409
410int main(int argc, char *argv[])
411{
412 printf(NAME ": HelenOS PCI IDE device driver\n");
413 ddf_log_init(NAME);
414 return ddf_driver_main(&pci_ide_driver);
415}
416
417/**
418 * @}
419 */
Note: See TracBrowser for help on using the repository browser.