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>
35 #include <netinet/in.h>
37 #include <machine/stdarg.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 int ofwd_print(int verbose);
52 static char * ofwd_fmtdev(struct devdesc *);
53 static int ofwd_parsedev(struct devdesc **, const char *, const char **);
54 static bool ofwd_match(struct devsw *, const char *);
56 struct devsw ofwdisk = {
58 .dv_type = DEVT_OFDISK,
60 .dv_strategy = ofwd_strategy,
62 .dv_close = ofwd_close,
63 .dv_ioctl = ofwd_ioctl,
64 .dv_print = ofwd_print,
65 .dv_cleanup = nullsys,
66 .dv_match = ofwd_match,
67 .dv_fmtdev = ofwd_fmtdev,
68 .dv_parsedev = ofwd_parsedev,
72 * We're not guaranteed to be able to open a device more than once and there
73 * is no OFW standard method to determine whether a device is already opened.
74 * Opening a device multiple times simultaneously happens to work with most
75 * OFW block device drivers but triggers a trap with at least the driver for
76 * the on-board controllers of Sun Fire V100 and Ultra 1. Upper layers and MI
77 * code expect to be able to open a device more than once however. Given that
78 * different partitions of the same device might be opened at the same time as
79 * done by ZFS, we can't generally just keep track of the opened devices and
80 * reuse the instance handle when asked to open an already opened device. So
81 * the best we can do is to cache the lastly used device path and close and
82 * open devices in ofwd_strategy() as needed.
84 static struct ofw_devdesc *kdp;
94 ofwd_strategy(void *devdata, int flag __unused, daddr_t dblk, size_t size,
95 char *buf, size_t *rsize)
97 struct ofw_devdesc *dp = (struct ofw_devdesc *)devdata;
103 #if !defined(__powerpc__)
104 OF_close(kdp->d_handle);
108 if ((dp->d_handle = OF_open(dp->d_path)) == -1)
115 if (OF_seek(dp->d_handle, pos) < 0)
117 n = OF_read(dp->d_handle, buf, size);
118 if (n < 0 && n != -2)
126 ofwd_open(struct open_file *f, ...)
128 struct ofw_devdesc *dp;
132 dp = va_arg(vl, struct ofw_devdesc *);
137 OF_close(kdp->d_handle);
140 if ((dp->d_handle = OF_open(dp->d_path)) == -1) {
141 printf("%s: Could not open %s\n", __func__,
151 ofwd_close(struct open_file *f)
153 struct ofw_devdesc *dev = f->f_devdata;
156 #if !defined(__powerpc__)
157 OF_close(dev->d_handle);
165 ofwd_ioctl(struct open_file *f, u_long cmd, void *data)
167 struct ofw_devdesc *dev = f->f_devdata;
172 case DIOCGSECTORSIZE:
173 block_size = OF_block_size(dev->d_handle);
174 *(u_int *)data = block_size;
177 block_size = OF_block_size(dev->d_handle);
178 n = OF_blocks(dev->d_handle);
179 *(uint64_t *)data = (uint64_t)(n * block_size);
188 ofwd_print(int verbose __unused)
190 uintmax_t block_size, n;
195 * We don't have a list of devices since we don't parse the whole OFW
196 * tree to find them. Instead, if we have an open device print info
197 * about it. Otherwise say we can't. Makes lsdev nicer.
199 if ((ret = pager_output("block devices:\n")) != 0)
202 block_size = OF_block_size(kdp->d_handle);
203 n = OF_blocks(kdp->d_handle);
204 snprintf(line, sizeof(line),
205 " %s: OFW block device (%ju X %ju): %ju bytes\n",
206 kdp->d_path, n, block_size, n * block_size);
207 if ((ret = pager_output(line)) != 0)
210 if ((ret = pager_output(" none are open, so no info\n")) != 0)
219 ofwd_match(struct devsw *devsw, const char *devspec)
223 return (ofw_path_to_handle(devspec, devsw->dv_name, &path) != -1);
227 ofwd_fmtdev(struct devdesc *idev)
229 struct ofw_devdesc *dev = (struct ofw_devdesc *)idev;
231 return (dev->d_path);
235 ofwd_parsedev(struct devdesc **dev, const char *devspec, const char **path)
237 return (ofw_common_parsedev(dev, devspec, path, ofwdisk.dv_name));