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