xref: /freebsd/stand/libofw/ofw_disk.c (revision 6419bb52)
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 "bootstrap.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 
53 struct devsw ofwdisk = {
54 	"block",
55 	DEVT_DISK,
56 	ofwd_init,
57 	ofwd_strategy,
58 	ofwd_open,
59 	ofwd_close,
60 	ofwd_ioctl,
61 	ofwd_print
62 };
63 
64 /*
65  * We're not guaranteed to be able to open a device more than once and there
66  * is no OFW standard method to determine whether a device is already opened.
67  * Opening a device multiple times simultaneously happens to work with most
68  * OFW block device drivers but triggers a trap with at least the driver for
69  * the on-board controllers of Sun Fire V100 and Ultra 1.  Upper layers and MI
70  * code expect to be able to open a device more than once however.  Given that
71  * different partitions of the same device might be opened at the same time as
72  * done by ZFS, we can't generally just keep track of the opened devices and
73  * reuse the instance handle when asked to open an already opened device.  So
74  * the best we can do is to cache the lastly used device path and close and
75  * open devices in ofwd_strategy() as needed.
76  */
77 static struct ofw_devdesc *kdp;
78 
79 static int
80 ofwd_init(void)
81 {
82 
83 	return (0);
84 }
85 
86 static int
87 ofwd_strategy(void *devdata, int flag __unused, daddr_t dblk, size_t size,
88     char *buf, size_t *rsize)
89 {
90 	struct ofw_devdesc *dp = (struct ofw_devdesc *)devdata;
91 	daddr_t pos;
92 	int n;
93 
94 	if (dp != kdp) {
95 		if (kdp != NULL) {
96 #if !defined(__powerpc__)
97 			OF_close(kdp->d_handle);
98 #endif
99 			kdp = NULL;
100 		}
101 		if ((dp->d_handle = OF_open(dp->d_path)) == -1)
102 			return (ENOENT);
103 		kdp = dp;
104 	}
105 
106 	pos = dblk * 512;
107 	do {
108 		if (OF_seek(dp->d_handle, pos) < 0)
109 			return (EIO);
110 		n = OF_read(dp->d_handle, buf, size);
111 		if (n < 0 && n != -2)
112 			return (EIO);
113 	} while (n == -2);
114 	*rsize = size;
115 	return (0);
116 }
117 
118 static int
119 ofwd_open(struct open_file *f, ...)
120 {
121 	struct ofw_devdesc *dp;
122 	va_list vl;
123 
124 	va_start(vl, f);
125 	dp = va_arg(vl, struct ofw_devdesc *);
126 	va_end(vl);
127 
128 	if (dp != kdp) {
129 		if (kdp != NULL) {
130 			OF_close(kdp->d_handle);
131 			kdp = NULL;
132 		}
133 		if ((dp->d_handle = OF_open(dp->d_path)) == -1) {
134 			printf("%s: Could not open %s\n", __func__,
135 			    dp->d_path);
136 			return (ENOENT);
137 		}
138 		kdp = dp;
139 	}
140 	return (0);
141 }
142 
143 static int
144 ofwd_close(struct open_file *f)
145 {
146 	struct ofw_devdesc *dev = f->f_devdata;
147 
148 	if (dev == kdp) {
149 #if !defined(__powerpc__)
150 		OF_close(dev->d_handle);
151 #endif
152 		kdp = NULL;
153 	}
154 	return (0);
155 }
156 
157 static int
158 ofwd_ioctl(struct open_file *f, u_long cmd, void *data)
159 {
160 	struct ofw_devdesc *dev = f->f_devdata;
161 	int block_size;
162 	unsigned int n;
163 
164 	switch (cmd) {
165 	case DIOCGSECTORSIZE:
166 		block_size = OF_block_size(dev->d_handle);
167 		*(u_int *)data = block_size;
168 		break;
169 	case DIOCGMEDIASIZE:
170 		block_size = OF_block_size(dev->d_handle);
171 		n = OF_blocks(dev->d_handle);
172 		*(uint64_t *)data = (uint64_t)(n * block_size);
173 		break;
174 	default:
175 		return (ENOTTY);
176 	}
177 	return (0);
178 }
179 
180 static int
181 ofwd_print(int verbose __unused)
182 {
183 	return (0);
184 }
185