xref: /freebsd/sys/dev/ofw/ofw_disk.c (revision aa77148daf19f09c7f2c597aa7cd3a2ef7a2cc92)
1d9611800SBenno Rice /*
2d9611800SBenno Rice  * Copyright (C) 2002 Benno Rice <benno@FreeBSD.org>
3d9611800SBenno Rice  * All rights reserved.
4d9611800SBenno Rice  *
5d9611800SBenno Rice  * Redistribution and use in source and binary forms, with or without
6d9611800SBenno Rice  * modification, are permitted provided that the following conditions
7d9611800SBenno Rice  * are met:
8d9611800SBenno Rice  * 1. Redistributions of source code must retain the above copyright
9d9611800SBenno Rice  *    notice, this list of conditions and the following disclaimer.
10d9611800SBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
11d9611800SBenno Rice  *    notice, this list of conditions and the following disclaimer in the
12d9611800SBenno Rice  *    documentation and/or other materials provided with the distribution.
13d9611800SBenno Rice  *
14d9611800SBenno Rice  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
15d9611800SBenno Rice  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16d9611800SBenno Rice  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17d9611800SBenno Rice  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18d9611800SBenno Rice  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19d9611800SBenno Rice  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20d9611800SBenno Rice  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21d9611800SBenno Rice  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22d9611800SBenno Rice  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23d9611800SBenno Rice  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24d9611800SBenno Rice  *
25d9611800SBenno Rice  *
26d9611800SBenno Rice  */
27d9611800SBenno Rice 
28aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
29aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
30aad970f1SDavid E. O'Brien 
31d9611800SBenno Rice #include <sys/param.h>
32d9611800SBenno Rice #include <sys/systm.h>
33d9611800SBenno Rice #include <sys/bio.h>
34aa77148dSPeter Grehan #include <sys/module.h>
35d9611800SBenno Rice #include <sys/bus.h>
36d9611800SBenno Rice #include <sys/conf.h>
37d9611800SBenno Rice #include <sys/kernel.h>
381ac37de6SPeter Grehan #include <sys/limits.h>
3991b928c9SDag-Erling Smørgrav #include <geom/geom_disk.h>
40d9611800SBenno Rice 
41d9611800SBenno Rice #include <dev/ofw/openfirm.h>
42d9611800SBenno Rice 
43d9611800SBenno Rice #include <machine/bus.h>
44d9611800SBenno Rice #include <machine/md_var.h>
45d9611800SBenno Rice #include <machine/nexusvar.h>
46d9611800SBenno Rice 
47d9611800SBenno Rice #define	OFWD_BLOCKSIZE	512
48d9611800SBenno Rice 
49d9611800SBenno Rice struct ofwd_softc
50d9611800SBenno Rice {
51d9611800SBenno Rice 	device_t	ofwd_dev;
520b7ed341SPoul-Henning Kamp 	struct		disk *ofwd_disk;
53d9611800SBenno Rice 	phandle_t	ofwd_package;
54d9611800SBenno Rice 	ihandle_t	ofwd_instance;
55d9611800SBenno Rice };
56d9611800SBenno Rice 
57d9611800SBenno Rice /*
58d9611800SBenno Rice  * Disk device bus interface.
59d9611800SBenno Rice  */
601ac37de6SPeter Grehan static void	ofwd_identify(driver_t *, device_t);
61d9611800SBenno Rice static int	ofwd_probe(device_t);
62d9611800SBenno Rice static int	ofwd_attach(device_t);
63d9611800SBenno Rice 
64d9611800SBenno Rice static device_method_t	ofwd_methods[] = {
651ac37de6SPeter Grehan 	DEVMETHOD(device_identify,	ofwd_identify),
66d9611800SBenno Rice 	DEVMETHOD(device_probe, 	ofwd_probe),
67d9611800SBenno Rice 	DEVMETHOD(device_attach,	ofwd_attach),
68d9611800SBenno Rice 	{ 0, 0 }
69d9611800SBenno Rice };
70d9611800SBenno Rice 
71d9611800SBenno Rice static driver_t ofwd_driver = {
72d9611800SBenno Rice 	"ofwd",
73d9611800SBenno Rice 	ofwd_methods,
74d9611800SBenno Rice 	sizeof(struct ofwd_softc)
75d9611800SBenno Rice };
76d9611800SBenno Rice 
77d9611800SBenno Rice static devclass_t	ofwd_devclass;
78d9611800SBenno Rice 
79d9611800SBenno Rice DRIVER_MODULE(ofwd, nexus, ofwd_driver, ofwd_devclass, 0, 0);
80d9611800SBenno Rice 
81d9611800SBenno Rice /*
82d9611800SBenno Rice  * Disk device control interface.
83d9611800SBenno Rice  */
84e165e4d2SPoul-Henning Kamp static disk_strategy_t	ofwd_strategy;
85d9611800SBenno Rice 
86d9611800SBenno Rice /*
87d9611800SBenno Rice  * Handle an I/O request.
88d9611800SBenno Rice  */
89d9611800SBenno Rice static void
90d9611800SBenno Rice ofwd_strategy(struct bio *bp)
91d9611800SBenno Rice {
92d9611800SBenno Rice 	struct	ofwd_softc *sc;
93d9611800SBenno Rice 	long	r;
94d9611800SBenno Rice 
95e165e4d2SPoul-Henning Kamp 	sc = (struct ofwd_softc *)bp->bio_disk->d_drv1;
96d9611800SBenno Rice 
97d9611800SBenno Rice 	if (sc == NULL) {
98d9611800SBenno Rice 		printf("ofwd: bio for invalid disk!\n");
99e165e4d2SPoul-Henning Kamp 		biofinish(bp, NULL, EINVAL);
100d9611800SBenno Rice 		return;
101d9611800SBenno Rice 	}
102d9611800SBenno Rice 
103aa77148dSPeter Grehan 	bp->bio_resid = bp->bio_bcount;
104aa77148dSPeter Grehan 
10524730dd7SPoul-Henning Kamp 	r = OF_seek(sc->ofwd_instance, bp->bio_offset);
106d9611800SBenno Rice 	if (r == -1) {
107d9611800SBenno Rice 		device_printf(sc->ofwd_dev, "seek failed\n");
108e165e4d2SPoul-Henning Kamp 		biofinish(bp, NULL, EIO);
109d9611800SBenno Rice 		return;
110d9611800SBenno Rice 	}
111d9611800SBenno Rice 
112d9611800SBenno Rice 	if (bp->bio_cmd == BIO_READ) {
113d9611800SBenno Rice 		r = OF_read(sc->ofwd_instance, (void *)bp->bio_data,
114d9611800SBenno Rice 		    bp->bio_bcount);
115d9611800SBenno Rice 	} else {
116d9611800SBenno Rice 		r = OF_write(sc->ofwd_instance, (void *)bp->bio_data,
117d9611800SBenno Rice 			    bp->bio_bcount);
118d9611800SBenno Rice 	}
119d9611800SBenno Rice 
120e165e4d2SPoul-Henning Kamp 	if (r > bp->bio_bcount)
121e165e4d2SPoul-Henning Kamp 		panic("ofwd: more bytes read/written than requested");
122e165e4d2SPoul-Henning Kamp 	if (r == -1) {
123e165e4d2SPoul-Henning Kamp 		device_printf(sc->ofwd_dev, "r (%ld) < bp->bio_bcount (%ld)\n",
124e165e4d2SPoul-Henning Kamp 		    r, bp->bio_bcount);
125e165e4d2SPoul-Henning Kamp 		biofinish(bp, NULL, EIO);
126e165e4d2SPoul-Henning Kamp 		return;
127e165e4d2SPoul-Henning Kamp 	}
128e165e4d2SPoul-Henning Kamp 
129e165e4d2SPoul-Henning Kamp 	bp->bio_resid -= r;
130e165e4d2SPoul-Henning Kamp 
131d9611800SBenno Rice 	if (r < bp->bio_bcount) {
132d9611800SBenno Rice 		device_printf(sc->ofwd_dev, "r (%ld) < bp->bio_bcount (%ld)\n",
133d9611800SBenno Rice 		    r, bp->bio_bcount);
134e165e4d2SPoul-Henning Kamp 		biofinish(bp, NULL, EIO);	/* XXX: probably not an error */
135e165e4d2SPoul-Henning Kamp 		return;
136e165e4d2SPoul-Henning Kamp 	}
13760794e04SPoul-Henning Kamp 	biodone(bp);
138d9611800SBenno Rice 	return;
139d9611800SBenno Rice }
140d9611800SBenno Rice 
141d9611800SBenno Rice /*
1421ac37de6SPeter Grehan  * Attach the OpenFirmware disk to nexus if present
1431ac37de6SPeter Grehan  */
1441ac37de6SPeter Grehan static void
1451ac37de6SPeter Grehan ofwd_identify(driver_t *driver, device_t parent)
1461ac37de6SPeter Grehan {
1471ac37de6SPeter Grehan 	device_t child;
1481ac37de6SPeter Grehan 	phandle_t ofd;
1491ac37de6SPeter Grehan 	static char type[8];
1501ac37de6SPeter Grehan 
1511ac37de6SPeter Grehan 	ofd = OF_finddevice("ofwdisk");
1521ac37de6SPeter Grehan 	if (ofd == -1)
1531ac37de6SPeter Grehan 		return;
1541ac37de6SPeter Grehan 
1551ac37de6SPeter Grehan 	OF_getprop(ofd, "device_type", type, sizeof(type));
1561ac37de6SPeter Grehan 
1571ac37de6SPeter Grehan 	child = BUS_ADD_CHILD(parent, INT_MAX, "ofwd", 0);
1581ac37de6SPeter Grehan 	if (child != NULL) {
1591ac37de6SPeter Grehan 		nexus_set_device_type(child, type);
1601ac37de6SPeter Grehan 		nexus_set_node(child, ofd);
1611ac37de6SPeter Grehan 	}
1621ac37de6SPeter Grehan }
1631ac37de6SPeter Grehan 
1641ac37de6SPeter Grehan /*
165d9611800SBenno Rice  * Probe for an OpenFirmware disk.
166d9611800SBenno Rice  */
167d9611800SBenno Rice static int
168d9611800SBenno Rice ofwd_probe(device_t dev)
169d9611800SBenno Rice {
170d9611800SBenno Rice 	char		*type;
171ad1c13f4SPeter Grehan 	char		fname[32];
172ad1c13f4SPeter Grehan 	phandle_t	node;
173d9611800SBenno Rice 
174d9611800SBenno Rice 	type = nexus_get_device_type(dev);
175ad1c13f4SPeter Grehan 	node = nexus_get_node(dev);
176d9611800SBenno Rice 
1772abd35bcSPeter Grehan 	if (type == NULL ||
1782abd35bcSPeter Grehan 	    (strcmp(type, "disk") != 0 && strcmp(type, "block") != 0))
179d9611800SBenno Rice 		return (ENXIO);
180d9611800SBenno Rice 
181ad1c13f4SPeter Grehan 	if (OF_getprop(node, "file", fname, sizeof(fname)) == -1)
182ad1c13f4SPeter Grehan 		return (ENXIO);
183ad1c13f4SPeter Grehan 
184d9611800SBenno Rice 	device_set_desc(dev, "OpenFirmware disk");
185d9611800SBenno Rice 	return (0);
186d9611800SBenno Rice }
187d9611800SBenno Rice 
188d9611800SBenno Rice static int
189d9611800SBenno Rice ofwd_attach(device_t dev)
190d9611800SBenno Rice {
191d9611800SBenno Rice 	struct	ofwd_softc *sc;
192d9611800SBenno Rice 	char	path[128];
193ad1c13f4SPeter Grehan 	char	fname[32];
194d9611800SBenno Rice 
195d9611800SBenno Rice 	sc = device_get_softc(dev);
196d9611800SBenno Rice 	sc->ofwd_dev = dev;
197d9611800SBenno Rice 
198d9611800SBenno Rice 	bzero(path, 128);
199d9611800SBenno Rice 	OF_package_to_path(nexus_get_node(dev), path, 128);
200ad1c13f4SPeter Grehan 	OF_getprop(nexus_get_node(dev), "file", fname, sizeof(fname));
201ad1c13f4SPeter Grehan 	device_printf(dev, "located at %s, file %s\n", path, fname);
202d9611800SBenno Rice 	sc->ofwd_instance = OF_open(path);
203d9611800SBenno Rice 	if (sc->ofwd_instance == -1) {
204d9611800SBenno Rice 		device_printf(dev, "could not create instance\n");
205d9611800SBenno Rice 		return (ENXIO);
206d9611800SBenno Rice 	}
207d9611800SBenno Rice 
2080b7ed341SPoul-Henning Kamp 	sc->ofwd_disk = disk_alloc();
2090b7ed341SPoul-Henning Kamp 	sc->ofwd_disk->d_strategy = ofwd_strategy;
2100b7ed341SPoul-Henning Kamp 	sc->ofwd_disk->d_name = "ofwd";
2110b7ed341SPoul-Henning Kamp 	sc->ofwd_disk->d_sectorsize = OFWD_BLOCKSIZE;
2120b7ed341SPoul-Henning Kamp 	sc->ofwd_disk->d_mediasize = (off_t)33554432 * OFWD_BLOCKSIZE;
2130b7ed341SPoul-Henning Kamp 	sc->ofwd_disk->d_fwsectors = 0;
2140b7ed341SPoul-Henning Kamp 	sc->ofwd_disk->d_fwheads = 0;
2150b7ed341SPoul-Henning Kamp 	sc->ofwd_disk->d_drv1 = sc;
2160b7ed341SPoul-Henning Kamp 	sc->ofwd_disk->d_maxsize = PAGE_SIZE;
2170b7ed341SPoul-Henning Kamp 	sc->ofwd_disk->d_unit = device_get_unit(dev);
2180b7ed341SPoul-Henning Kamp 	sc->ofwd_disk->d_flags = DISKFLAG_NEEDSGIANT;
2190b7ed341SPoul-Henning Kamp 	disk_create(sc->ofwd_disk, DISK_VERSION);
220d9611800SBenno Rice 
221d9611800SBenno Rice 	return (0);
222d9611800SBenno Rice }
223