xref: /freebsd/sys/dev/ofw/ofw_disk.c (revision dba6dd177bdee890cf445fbe21a5dccefd5de18e)
1 /*
2  * Copyright (C) 2002 Benno Rice <benno@FreeBSD.org>
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 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bio.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/kernel.h>
37 #include <sys/limits.h>
38 #include <geom/geom_disk.h>
39 
40 #include <dev/ofw/openfirm.h>
41 
42 #include <machine/bus.h>
43 #include <machine/md_var.h>
44 #include <machine/nexusvar.h>
45 
46 #define	OFWD_BLOCKSIZE	512
47 
48 struct ofwd_softc
49 {
50 	device_t	ofwd_dev;
51 	struct		disk *ofwd_disk;
52 	phandle_t	ofwd_package;
53 	ihandle_t	ofwd_instance;
54 };
55 
56 /*
57  * Disk device bus interface.
58  */
59 static void	ofwd_identify(driver_t *, device_t);
60 static int	ofwd_probe(device_t);
61 static int	ofwd_attach(device_t);
62 
63 static device_method_t	ofwd_methods[] = {
64 	DEVMETHOD(device_identify,	ofwd_identify),
65 	DEVMETHOD(device_probe, 	ofwd_probe),
66 	DEVMETHOD(device_attach,	ofwd_attach),
67 	{ 0, 0 }
68 };
69 
70 static driver_t ofwd_driver = {
71 	"ofwd",
72 	ofwd_methods,
73 	sizeof(struct ofwd_softc)
74 };
75 
76 static devclass_t	ofwd_devclass;
77 
78 DRIVER_MODULE(ofwd, nexus, ofwd_driver, ofwd_devclass, 0, 0);
79 
80 /*
81  * Disk device control interface.
82  */
83 static disk_strategy_t	ofwd_strategy;
84 
85 /*
86  * Handle an I/O request.
87  */
88 static void
89 ofwd_strategy(struct bio *bp)
90 {
91 	struct	ofwd_softc *sc;
92 	long	r;
93 
94 	sc = (struct ofwd_softc *)bp->bio_disk->d_drv1;
95 
96 	if (sc == NULL) {
97 		printf("ofwd: bio for invalid disk!\n");
98 		biofinish(bp, NULL, EINVAL);
99 		return;
100 	}
101 
102 	r = OF_seek(sc->ofwd_instance, bp->bio_offset);
103 	if (r == -1) {
104 		bp->bio_resid = bp->bio_bcount;
105 		device_printf(sc->ofwd_dev, "seek failed\n");
106 		biofinish(bp, NULL, EIO);
107 		return;
108 	}
109 
110 	if (bp->bio_cmd == BIO_READ) {
111 		r = OF_read(sc->ofwd_instance, (void *)bp->bio_data,
112 		    bp->bio_bcount);
113 	} else {
114 		r = OF_write(sc->ofwd_instance, (void *)bp->bio_data,
115 			    bp->bio_bcount);
116 	}
117 
118 	if (r > bp->bio_bcount)
119 		panic("ofwd: more bytes read/written than requested");
120 	if (r == -1) {
121 		device_printf(sc->ofwd_dev, "r (%ld) < bp->bio_bcount (%ld)\n",
122 		    r, bp->bio_bcount);
123 		biofinish(bp, NULL, EIO);
124 		return;
125 	}
126 
127 	bp->bio_resid -= r;
128 
129 	if (r < bp->bio_bcount) {
130 		device_printf(sc->ofwd_dev, "r (%ld) < bp->bio_bcount (%ld)\n",
131 		    r, bp->bio_bcount);
132 		biofinish(bp, NULL, EIO);	/* XXX: probably not an error */
133 		return;
134 	}
135 	biodone(bp);
136 	return;
137 }
138 
139 /*
140  * Attach the OpenFirmware disk to nexus if present
141  */
142 static void
143 ofwd_identify(driver_t *driver, device_t parent)
144 {
145 	device_t child;
146 	phandle_t ofd;
147 	static char type[8];
148 
149 	ofd = OF_finddevice("ofwdisk");
150 	if (ofd == -1)
151 		return;
152 
153 	OF_getprop(ofd, "device_type", type, sizeof(type));
154 
155 	child = BUS_ADD_CHILD(parent, INT_MAX, "ofwd", 0);
156 	if (child != NULL) {
157 		nexus_set_device_type(child, type);
158 		nexus_set_node(child, ofd);
159 	}
160 }
161 
162 /*
163  * Probe for an OpenFirmware disk.
164  */
165 static int
166 ofwd_probe(device_t dev)
167 {
168 	char		*type;
169 	char		fname[32];
170 	phandle_t	node;
171 
172 	type = nexus_get_device_type(dev);
173 	node = nexus_get_node(dev);
174 
175 	if (type == NULL ||
176 	    (strcmp(type, "disk") != 0 && strcmp(type, "block") != 0))
177 		return (ENXIO);
178 
179 	if (OF_getprop(node, "file", fname, sizeof(fname)) == -1)
180 		return (ENXIO);
181 
182 	device_set_desc(dev, "OpenFirmware disk");
183 	return (0);
184 }
185 
186 static int
187 ofwd_attach(device_t dev)
188 {
189 	struct	ofwd_softc *sc;
190 	char	path[128];
191 	char	fname[32];
192 
193 	sc = device_get_softc(dev);
194 	sc->ofwd_dev = dev;
195 
196 	bzero(path, 128);
197 	OF_package_to_path(nexus_get_node(dev), path, 128);
198 	OF_getprop(nexus_get_node(dev), "file", fname, sizeof(fname));
199 	device_printf(dev, "located at %s, file %s\n", path, fname);
200 	sc->ofwd_instance = OF_open(path);
201 	if (sc->ofwd_instance == -1) {
202 		device_printf(dev, "could not create instance\n");
203 		return (ENXIO);
204 	}
205 
206 	sc->ofwd_disk = disk_alloc();
207 	sc->ofwd_disk->d_strategy = ofwd_strategy;
208 	sc->ofwd_disk->d_name = "ofwd";
209 	sc->ofwd_disk->d_sectorsize = OFWD_BLOCKSIZE;
210 	sc->ofwd_disk->d_mediasize = (off_t)33554432 * OFWD_BLOCKSIZE;
211 	sc->ofwd_disk->d_fwsectors = 0;
212 	sc->ofwd_disk->d_fwheads = 0;
213 	sc->ofwd_disk->d_drv1 = sc;
214 	sc->ofwd_disk->d_maxsize = PAGE_SIZE;
215 	sc->ofwd_disk->d_unit = device_get_unit(dev);
216 	sc->ofwd_disk->d_flags = DISKFLAG_NEEDSGIANT;
217 	disk_create(sc->ofwd_disk, DISK_VERSION);
218 
219 	return (0);
220 }
221