2 * Copyright (C) 2000 Benno Rice.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 * Disk I/O routines using Open Firmware
33 #include <sys/param.h>
34 #include <sys/queue.h>
36 #include <netinet/in.h>
38 #include <machine/stdarg.h>
42 #include "bootstrap.h"
45 static int ofwd_init(void);
46 static int ofwd_strategy(void *devdata, int flag, daddr_t dblk,
47 size_t size, char *buf, size_t *rsize);
48 static int ofwd_open(struct open_file *f, ...);
49 static int ofwd_close(struct open_file *f);
50 static int ofwd_ioctl(struct open_file *f, u_long cmd, void *data);
51 static void ofwd_print(int verbose);
53 struct devsw ofwdisk = {
67 SLIST_ENTRY(opened_dev) link;
70 SLIST_HEAD(, opened_dev) opened_devs = SLIST_HEAD_INITIALIZER(opened_devs);
80 ofwd_strategy(void *devdata, int flag, daddr_t dblk, size_t size, char *buf,
83 struct ofw_devdesc *dp = (struct ofw_devdesc *)devdata;
89 if (OF_seek(dp->d_handle, pos) < 0)
91 n = OF_read(dp->d_handle, buf, size);
100 ofwd_open(struct open_file *f, ...)
103 struct ofw_devdesc *dp;
104 struct opened_dev *odp;
108 dp = va_arg(vl, struct ofw_devdesc *);
111 * We're not guaranteed to be able to open a device more than once
112 * simultaneously and there is no OFW standard method to determine
113 * whether a device is already opened. Opening a device more than
114 * once happens to work with most OFW block device drivers but
115 * triggers a trap with at least the driver for the on-board SCSI
116 * controller in Sun Ultra 1. Upper layers and MI code expect to
117 * be able to open a device more than once however. As a workaround
118 * keep track of the opened devices and reuse the instance handle
119 * when asked to open an already opened device.
121 SLIST_FOREACH(odp, &opened_devs, link) {
122 if (OF_instance_to_path(odp->handle, path, sizeof(path)) == -1)
124 if (strcmp(path, dp->d_path) == 0) {
126 dp->d_handle = odp->handle;
130 odp = malloc(sizeof(struct opened_dev));
132 printf("ofwd_open: malloc failed\n");
135 if ((odp->handle = OF_open(dp->d_path)) == -1) {
136 printf("ofwd_open: Could not open %s\n", dp->d_path);
141 SLIST_INSERT_HEAD(&opened_devs, odp, link);
142 dp->d_handle = odp->handle;
147 ofwd_close(struct open_file *f)
149 struct ofw_devdesc *dev = f->f_devdata;
150 struct opened_dev *odp;
152 SLIST_FOREACH(odp, &opened_devs, link) {
153 if (odp->handle == dev->d_handle) {
155 if (odp->count == 0) {
156 SLIST_REMOVE(&opened_devs, odp, opened_dev,
158 #if !defined(__powerpc__)
159 OF_close(odp->handle);
170 ofwd_ioctl(struct open_file *f, u_long cmd, void *data)
177 ofwd_print(int verbose)