xref: /freebsd/sys/dev/ofw/ofw_disk.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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 <geom/geom_disk.h>
38 
39 #include <dev/ofw/openfirm.h>
40 
41 #include <machine/bus.h>
42 #include <machine/md_var.h>
43 #include <machine/nexusvar.h>
44 
45 #define	OFWD_BLOCKSIZE	512
46 
47 struct ofwd_softc
48 {
49 	device_t	ofwd_dev;
50 	struct		disk ofwd_disk;
51 	phandle_t	ofwd_package;
52 	ihandle_t	ofwd_instance;
53 };
54 
55 /*
56  * Disk device bus interface.
57  */
58 static void	ofwd_identify(driver_t *, device_t);
59 static int	ofwd_probe(device_t);
60 static int	ofwd_attach(device_t);
61 
62 static device_method_t	ofwd_methods[] = {
63 	DEVMETHOD(device_identify,	ofwd_identify),
64 	DEVMETHOD(device_probe, 	ofwd_probe),
65 	DEVMETHOD(device_attach,	ofwd_attach),
66 	{ 0, 0 }
67 };
68 
69 static driver_t ofwd_driver = {
70 	"ofwd",
71 	ofwd_methods,
72 	sizeof(struct ofwd_softc)
73 };
74 
75 static devclass_t	ofwd_devclass;
76 
77 DRIVER_MODULE(ofwd, nexus, ofwd_driver, ofwd_devclass, 0, 0);
78 
79 /*
80  * Disk device control interface.
81  */
82 static disk_strategy_t	ofwd_strategy;
83 
84 /*
85  * Handle an I/O request.
86  */
87 static void
88 ofwd_strategy(struct bio *bp)
89 {
90 	struct	ofwd_softc *sc;
91 	long	r;
92 
93 	sc = (struct ofwd_softc *)bp->bio_disk->d_drv1;
94 
95 	if (sc == NULL) {
96 		printf("ofwd: bio for invalid disk!\n");
97 		biofinish(bp, NULL, EINVAL);
98 		return;
99 	}
100 
101 	r = OF_seek(sc->ofwd_instance,
102 	    (u_quad_t)(bp->bio_blkno * OFWD_BLOCKSIZE));
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  * Probe for an OpenFirmware disk.
141  */
142 static int
143 ofwd_probe(device_t dev)
144 {
145 	char		*type;
146 
147 	type = nexus_get_device_type(dev);
148 
149 	if (type == NULL || strcmp(type, "disk") != 0)
150 		return (ENXIO);
151 
152 	device_set_desc(dev, "OpenFirmware disk");
153 	return (0);
154 }
155 
156 static int
157 ofwd_attach(device_t dev)
158 {
159 	struct	ofwd_softc *sc;
160 	char	path[128];
161 	dev_t	dsk;
162 
163 	sc = device_get_softc(dev);
164 	sc->ofwd_dev = dev;
165 
166 	bzero(path, 128);
167 	OF_package_to_path(nexus_get_node(dev), path, 128);
168 	device_printf(dev, "located at %s\n", path);
169 	sc->ofwd_instance = OF_open(path);
170 	if (sc->ofwd_instance == -1) {
171 		device_printf(dev, "could not create instance\n");
172 		return (ENXIO);
173 	}
174 
175 	sc->ofwd_disk.d_strategy = ofwd_strategy;
176 	sc->ofwd_disk.d_name = "ofwd";
177 	sc->ofwd_disk.d_sectorsize = OFWD_BLOCKSIZE;
178 	sc->ofwd_disk.d_mediasize = (off_t)33554432 * OFWD_BLOCKSIZE;
179 	sc->ofwd_disk.d_fwsectors = 0;
180 	sc->ofwd_disk.d_fwheads = 0;
181 	sc->ofwd_disk.d_drv1 = sc;
182 	sc->ofwd_disk.d_maxsize = PAGE_SIZE;
183 	disk_create(device_get_unit(dev), &sc->ofwd_disk, 0, NULL, NULL);
184 
185 	return (0);
186 }
187 
188 static void
189 ofwd_identify(driver_t *driver, device_t parent)
190 {
191 }
192