]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/libofw/ofw_disk.c
zfs: merge openzfs/zfs@2e6b3c4d9
[FreeBSD/FreeBSD.git] / stand / libofw / ofw_disk.c
1 /*-
2  * Copyright (C) 2000 Benno Rice.
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  * 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.
13  *
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.
24  */
25
26 #include <sys/cdefs.h>
27 /*
28  * Disk I/O routines using Open Firmware
29  */
30
31 #include <sys/param.h>
32
33 #include <netinet/in.h>
34
35 #include <machine/stdarg.h>
36
37 #include <stand.h>
38 #include <sys/disk.h>
39
40 #include "disk.h"
41 #include "libofw.h"
42
43 static int      ofwd_init(void);
44 static int      ofwd_strategy(void *devdata, int flag, daddr_t dblk,
45                     size_t size, char *buf, size_t *rsize);
46 static int      ofwd_open(struct open_file *f, ...);
47 static int      ofwd_close(struct open_file *f);
48 static int      ofwd_ioctl(struct open_file *f, u_long cmd, void *data);
49 static int      ofwd_print(int verbose);
50 static char *   ofwd_fmtdev(struct devdesc *);
51 static int      ofwd_parsedev(struct devdesc **, const char *, const char **);
52 static bool     ofwd_match(struct devsw *, const char *);
53
54 struct devsw ofwdisk = {
55         .dv_name = "block",
56         .dv_type = DEVT_OFDISK,
57         .dv_init = ofwd_init,
58         .dv_strategy = ofwd_strategy,
59         .dv_open = ofwd_open,
60         .dv_close = ofwd_close,
61         .dv_ioctl = ofwd_ioctl,
62         .dv_print = ofwd_print,
63         .dv_cleanup = nullsys,
64         .dv_match = ofwd_match,
65         .dv_fmtdev = ofwd_fmtdev,
66         .dv_parsedev = ofwd_parsedev,
67 };
68
69 /*
70  * We're not guaranteed to be able to open a device more than once and there
71  * is no OFW standard method to determine whether a device is already opened.
72  * Opening a device multiple times simultaneously happens to work with most
73  * OFW block device drivers but triggers a trap with at least the driver for
74  * the on-board controllers of Sun Fire V100 and Ultra 1.  Upper layers and MI
75  * code expect to be able to open a device more than once however.  Given that
76  * different partitions of the same device might be opened at the same time as
77  * done by ZFS, we can't generally just keep track of the opened devices and
78  * reuse the instance handle when asked to open an already opened device.  So
79  * the best we can do is to cache the lastly used device path and close and
80  * open devices in ofwd_strategy() as needed.
81  */
82 static struct ofw_devdesc *kdp;
83
84 static int
85 ofwd_init(void)
86 {
87
88         return (0);
89 }
90
91 static int
92 ofwd_strategy(void *devdata, int flag __unused, daddr_t dblk, size_t size,
93     char *buf, size_t *rsize)
94 {
95         struct ofw_devdesc *dp = (struct ofw_devdesc *)devdata;
96         daddr_t pos;
97         int n;
98
99         if (dp != kdp) {
100                 if (kdp != NULL) {
101 #if !defined(__powerpc__)
102                         OF_close(kdp->d_handle);
103 #endif
104                         kdp = NULL;
105                 }
106                 if ((dp->d_handle = OF_open(dp->d_path)) == -1)
107                         return (ENOENT);
108                 kdp = dp;
109         }
110
111         pos = dblk * 512;
112         do {
113                 if (OF_seek(dp->d_handle, pos) < 0)
114                         return (EIO);
115                 n = OF_read(dp->d_handle, buf, size);
116                 if (n < 0 && n != -2)
117                         return (EIO);
118         } while (n == -2);
119         *rsize = size;
120         return (0);
121 }
122
123 static int
124 ofwd_open(struct open_file *f, ...)
125 {
126         struct ofw_devdesc *dp;
127         va_list vl;
128
129         va_start(vl, f);
130         dp = va_arg(vl, struct ofw_devdesc *);
131         va_end(vl);
132
133         if (dp != kdp) {
134                 if (kdp != NULL) {
135                         OF_close(kdp->d_handle);
136                         kdp = NULL;
137                 }
138                 if ((dp->d_handle = OF_open(dp->d_path)) == -1) {
139                         printf("%s: Could not open %s\n", __func__,
140                             dp->d_path);
141                         return (ENOENT);
142                 }
143                 kdp = dp;
144         }
145         return (0);
146 }
147
148 static int
149 ofwd_close(struct open_file *f)
150 {
151         struct ofw_devdesc *dev = f->f_devdata;
152
153         if (dev == kdp) {
154 #if !defined(__powerpc__)
155                 OF_close(dev->d_handle);
156 #endif
157                 kdp = NULL;
158         }
159         return (0);
160 }
161
162 static int
163 ofwd_ioctl(struct open_file *f, u_long cmd, void *data)
164 {
165         struct ofw_devdesc *dev = f->f_devdata;
166         int block_size;
167         unsigned int n;
168
169         switch (cmd) {
170         case DIOCGSECTORSIZE:
171                 block_size = OF_block_size(dev->d_handle);
172                 *(u_int *)data = block_size;
173                 break;
174         case DIOCGMEDIASIZE:
175                 block_size = OF_block_size(dev->d_handle);
176                 n = OF_blocks(dev->d_handle);
177                 *(uint64_t *)data = (uint64_t)(n * block_size);
178                 break;
179         default:
180                 return (ENOTTY);
181         }
182         return (0);
183 }
184
185 static int
186 ofwd_print(int verbose __unused)
187 {
188         uintmax_t block_size, n;
189         int ret;
190         char line[80];
191
192         /*
193          * We don't have a list of devices since we don't parse the whole OFW
194          * tree to find them. Instead, if we have an open device print info
195          * about it. Otherwise say we can't. Makes lsdev nicer.
196          */
197         if ((ret = pager_output("block devices:\n")) != 0)
198                 return (ret);
199         if (kdp != NULL) {
200                 block_size = OF_block_size(kdp->d_handle);
201                 n = OF_blocks(kdp->d_handle);
202                 snprintf(line, sizeof(line),
203                     "    %s:   OFW block device (%ju X %ju): %ju bytes\n",
204                     kdp->d_path, n, block_size, n * block_size);
205                 if ((ret = pager_output(line)) != 0)
206                         return (ret);
207         } else {
208                 if ((ret = pager_output("  none are open, so no info\n")) != 0)
209                         return (ret);
210         }
211
212         return (0);
213 }
214
215
216 static bool
217 ofwd_match(struct devsw *devsw, const char *devspec)
218 {
219         const char *path;
220
221         return (ofw_path_to_handle(devspec, devsw->dv_name, &path) != -1);
222 }
223
224 static char *
225 ofwd_fmtdev(struct devdesc *idev)
226 {
227         struct ofw_devdesc *dev = (struct ofw_devdesc *)idev;
228
229         return (dev->d_path);
230 }
231
232 static int
233 ofwd_parsedev(struct devdesc **dev, const char *devspec, const char **path)
234 {
235         return (ofw_common_parsedev(dev, devspec, path, ofwdisk.dv_name));
236 }