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