xref: /freebsd/stand/libofw/ofw_disk.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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 __FBSDID("$FreeBSD$");
28 
29 /*
30  * Disk I/O routines using Open Firmware
31  */
32 
33 #include <sys/param.h>
34 
35 #include <netinet/in.h>
36 
37 #include <machine/stdarg.h>
38 
39 #include <stand.h>
40 #include <sys/disk.h>
41 
42 #include "disk.h"
43 #include "libofw.h"
44 
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 *);
55 
56 struct devsw ofwdisk = {
57 	.dv_name = "block",
58 	.dv_type = DEVT_OFDISK,
59 	.dv_init = ofwd_init,
60 	.dv_strategy = ofwd_strategy,
61 	.dv_open = ofwd_open,
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,
69 };
70 
71 /*
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.
83  */
84 static struct ofw_devdesc *kdp;
85 
86 static int
87 ofwd_init(void)
88 {
89 
90 	return (0);
91 }
92 
93 static int
94 ofwd_strategy(void *devdata, int flag __unused, daddr_t dblk, size_t size,
95     char *buf, size_t *rsize)
96 {
97 	struct ofw_devdesc *dp = (struct ofw_devdesc *)devdata;
98 	daddr_t pos;
99 	int n;
100 
101 	if (dp != kdp) {
102 		if (kdp != NULL) {
103 #if !defined(__powerpc__)
104 			OF_close(kdp->d_handle);
105 #endif
106 			kdp = NULL;
107 		}
108 		if ((dp->d_handle = OF_open(dp->d_path)) == -1)
109 			return (ENOENT);
110 		kdp = dp;
111 	}
112 
113 	pos = dblk * 512;
114 	do {
115 		if (OF_seek(dp->d_handle, pos) < 0)
116 			return (EIO);
117 		n = OF_read(dp->d_handle, buf, size);
118 		if (n < 0 && n != -2)
119 			return (EIO);
120 	} while (n == -2);
121 	*rsize = size;
122 	return (0);
123 }
124 
125 static int
126 ofwd_open(struct open_file *f, ...)
127 {
128 	struct ofw_devdesc *dp;
129 	va_list vl;
130 
131 	va_start(vl, f);
132 	dp = va_arg(vl, struct ofw_devdesc *);
133 	va_end(vl);
134 
135 	if (dp != kdp) {
136 		if (kdp != NULL) {
137 			OF_close(kdp->d_handle);
138 			kdp = NULL;
139 		}
140 		if ((dp->d_handle = OF_open(dp->d_path)) == -1) {
141 			printf("%s: Could not open %s\n", __func__,
142 			    dp->d_path);
143 			return (ENOENT);
144 		}
145 		kdp = dp;
146 	}
147 	return (0);
148 }
149 
150 static int
151 ofwd_close(struct open_file *f)
152 {
153 	struct ofw_devdesc *dev = f->f_devdata;
154 
155 	if (dev == kdp) {
156 #if !defined(__powerpc__)
157 		OF_close(dev->d_handle);
158 #endif
159 		kdp = NULL;
160 	}
161 	return (0);
162 }
163 
164 static int
165 ofwd_ioctl(struct open_file *f, u_long cmd, void *data)
166 {
167 	struct ofw_devdesc *dev = f->f_devdata;
168 	int block_size;
169 	unsigned int n;
170 
171 	switch (cmd) {
172 	case DIOCGSECTORSIZE:
173 		block_size = OF_block_size(dev->d_handle);
174 		*(u_int *)data = block_size;
175 		break;
176 	case DIOCGMEDIASIZE:
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);
180 		break;
181 	default:
182 		return (ENOTTY);
183 	}
184 	return (0);
185 }
186 
187 static int
188 ofwd_print(int verbose __unused)
189 {
190 	uintmax_t block_size, n;
191 	int ret;
192 	char line[80];
193 
194 	/*
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.
198 	 */
199 	if ((ret = pager_output("block devices:\n")) != 0)
200 		return (ret);
201 	if (kdp != NULL) {
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)
208 			return (ret);
209 	} else {
210 		if ((ret = pager_output("  none are open, so no info\n")) != 0)
211 			return (ret);
212 	}
213 
214 	return (0);
215 }
216 
217 
218 static bool
219 ofwd_match(struct devsw *devsw, const char *devspec)
220 {
221 	const char *path;
222 
223 	return (ofw_path_to_handle(devspec, devsw->dv_name, &path) != -1);
224 }
225 
226 static char *
227 ofwd_fmtdev(struct devdesc *idev)
228 {
229 	struct ofw_devdesc *dev = (struct ofw_devdesc *)idev;
230 
231 	return (dev->d_path);
232 }
233 
234 static int
235 ofwd_parsedev(struct devdesc **dev, const char *devspec, const char **path)
236 {
237 	return (ofw_common_parsedev(dev, devspec, path, ofwdisk.dv_name));
238 }
239