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.
27 * Disk I/O routines using Open Firmware
30 #include <sys/param.h>
32 #include <netinet/in.h>
34 #include <machine/stdarg.h>
42 static int ofwd_init(void);
43 static int ofwd_strategy(void *devdata
, int flag
, daddr_t dblk
,
44 size_t size
, char *buf
, size_t *rsize
);
45 static int ofwd_open(struct open_file
*f
, ...);
46 static int ofwd_close(struct open_file
*f
);
47 static int ofwd_ioctl(struct open_file
*f
, u_long cmd
, void *data
);
48 static int ofwd_print(int verbose
);
49 static char * ofwd_fmtdev(struct devdesc
*);
50 static int ofwd_parsedev(struct devdesc
**, const char *, const char **);
51 static bool ofwd_match(struct devsw
*, const char *);
53 struct devsw ofwdisk
= {
55 .dv_type
= DEVT_OFDISK
,
57 .dv_strategy
= ofwd_strategy
,
59 .dv_close
= ofwd_close
,
60 .dv_ioctl
= ofwd_ioctl
,
61 .dv_print
= ofwd_print
,
62 .dv_cleanup
= nullsys
,
63 .dv_match
= ofwd_match
,
64 .dv_fmtdev
= ofwd_fmtdev
,
65 .dv_parsedev
= ofwd_parsedev
,
69 * We're not guaranteed to be able to open a device more than once and there
70 * is no OFW standard method to determine whether a device is already opened.
71 * Opening a device multiple times simultaneously happens to work with most
72 * OFW block device drivers but triggers a trap with at least the driver for
73 * the on-board controllers of Sun Fire V100 and Ultra 1. Upper layers and MI
74 * code expect to be able to open a device more than once however. Given that
75 * different partitions of the same device might be opened at the same time as
76 * done by ZFS, we can't generally just keep track of the opened devices and
77 * reuse the instance handle when asked to open an already opened device. So
78 * the best we can do is to cache the lastly used device path and close and
79 * open devices in ofwd_strategy() as needed.
81 static struct ofw_devdesc
*kdp
;
91 ofwd_strategy(void *devdata
, int flag __unused
, daddr_t dblk
, size_t size
,
92 char *buf
, size_t *rsize
)
94 struct ofw_devdesc
*dp
= (struct ofw_devdesc
*)devdata
;
100 #if !defined(__powerpc__)
101 OF_close(kdp
->d_handle
);
105 if ((dp
->d_handle
= OF_open(dp
->d_path
)) == -1)
112 if (OF_seek(dp
->d_handle
, pos
) < 0)
114 n
= OF_read(dp
->d_handle
, buf
, size
);
115 if (n
< 0 && n
!= -2)
123 ofwd_open(struct open_file
*f
, ...)
125 struct ofw_devdesc
*dp
;
129 dp
= va_arg(vl
, struct ofw_devdesc
*);
134 OF_close(kdp
->d_handle
);
137 if ((dp
->d_handle
= OF_open(dp
->d_path
)) == -1) {
138 printf("%s: Could not open %s\n", __func__
,
148 ofwd_close(struct open_file
*f
)
150 struct ofw_devdesc
*dev
= f
->f_devdata
;
153 #if !defined(__powerpc__)
154 OF_close(dev
->d_handle
);
162 ofwd_ioctl(struct open_file
*f
, u_long cmd
, void *data
)
164 struct ofw_devdesc
*dev
= f
->f_devdata
;
169 case DIOCGSECTORSIZE
:
170 block_size
= OF_block_size(dev
->d_handle
);
171 *(u_int
*)data
= block_size
;
174 block_size
= OF_block_size(dev
->d_handle
);
175 n
= OF_blocks(dev
->d_handle
);
176 *(uint64_t *)data
= ((uint64_t)n
* block_size
);
185 ofwd_print(int verbose __unused
)
187 uintmax_t block_size
, n
;
192 * We don't have a list of devices since we don't parse the whole OFW
193 * tree to find them. Instead, if we have an open device print info
194 * about it. Otherwise say we can't. Makes lsdev nicer.
196 if ((ret
= pager_output("block devices:\n")) != 0)
199 block_size
= OF_block_size(kdp
->d_handle
);
200 n
= OF_blocks(kdp
->d_handle
);
201 snprintf(line
, sizeof(line
),
202 " %s: OFW block device (%ju X %ju): %ju bytes\n",
203 kdp
->d_path
, n
, block_size
, n
* block_size
);
204 if ((ret
= pager_output(line
)) != 0)
207 if ((ret
= pager_output(" none are open, so no info\n")) != 0)
216 ofwd_match(struct devsw
*devsw
, const char *devspec
)
220 return (ofw_path_to_handle(devspec
, devsw
->dv_name
, &path
) != -1);
224 ofwd_fmtdev(struct devdesc
*idev
)
226 struct ofw_devdesc
*dev
= (struct ofw_devdesc
*)idev
;
228 return (dev
->d_path
);
232 ofwd_parsedev(struct devdesc
**dev
, const char *devspec
, const char **path
)
234 return (ofw_common_parsedev(dev
, devspec
, path
, ofwdisk
.dv_name
));