ofw_disk.c revision 94749
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: head/sys/dev/ofw/ofw_disk.c 94749 2002-04-15 10:54:22Z benno $
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
45struct ofwd_softc
46{
47	device_t	ofwd_dev;
48	dev_t		ofwd_dev_t;
49	struct		disk ofwd_disk;
50	phandle_t	ofwd_package;
51	ihandle_t	ofwd_instance;
52	u_int		ofwd_flags;
53#define	OFWD_OPEN	(1<<0)
54};
55
56/*
57 * Disk device bus interface.
58 */
59static void	ofwd_identify(driver_t *, device_t);
60static int	ofwd_probe(device_t);
61static int	ofwd_attach(device_t);
62
63static 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
70static driver_t ofwd_driver = {
71	"ofwd",
72	ofwd_methods,
73	sizeof(struct ofwd_softc)
74};
75
76static devclass_t	ofwd_devclass;
77
78DRIVER_MODULE(ofwd, nexus, ofwd_driver, ofwd_devclass, 0, 0);
79
80/*
81 * Disk device control interface.
82 */
83static d_open_t		ofwd_open;
84static d_close_t	ofwd_close;
85static d_strategy_t	ofwd_strategy;
86
87#define	OFWD_CDEV_MAJOR	169
88
89static struct cdevsw ofwd_cdevsw = {
90	ofwd_open,
91	ofwd_close,
92	physread,
93	physwrite,
94	noioctl,
95	nopoll,
96	nommap,
97	ofwd_strategy,
98	"ofwd",
99	OFWD_CDEV_MAJOR,
100	nodump,
101	nopsize,
102	D_DISK
103};
104
105static struct cdevsw	ofwddisk_cdevsw;
106
107/*
108 * Handle open from generic layer.
109 *
110 * This is typically only called by the diskslice code and not for opens on
111 * subdevices.
112 */
113static int
114ofwd_open(dev_t dev, int flags, int fmt, struct thread *td)
115{
116	struct	ofwd_softc *sc;
117	struct	disklabel *label;
118
119	sc = (struct ofwd_softc *)dev->si_drv1;
120	if (sc == NULL)
121		return (ENXIO);
122
123	/*
124	 * Build synthetic label.
125	 */
126	label = &sc->ofwd_disk.d_label;
127	bzero(label, sizeof(*label));
128	label->d_type = DTYPE_ESDI;
129	label->d_secsize = OFWD_BLOCKSIZE;
130	label->d_nsectors = 33554432;
131	label->d_ntracks = 1;
132	label->d_ncylinders = 1024;
133	label->d_secpercyl = 32768;
134	label->d_secperunit = 33554432;
135
136	sc->ofwd_flags |= OFWD_OPEN;
137	return (0);
138}
139
140/*
141 * Handle last close of the disk device.
142 */
143static int
144ofwd_close(dev_t dev, int flags, int fmt, struct thread *td)
145{
146	struct	ofwd_softc *sc;
147
148	sc = (struct ofwd_softc *)dev->si_drv1;
149
150	if (sc == NULL)
151		return (ENXIO);
152
153	sc->ofwd_flags &= ~OFWD_OPEN;
154
155	return (0);
156}
157
158/*
159 * Handle an I/O request.
160 */
161static void
162ofwd_strategy(struct bio *bp)
163{
164	struct	ofwd_softc *sc;
165	long	r;
166
167	sc = (struct ofwd_softc *)bp->bio_dev->si_drv1;
168
169	if (sc == NULL) {
170		bp->bio_error = EINVAL;
171		bp->bio_flags |= BIO_ERROR;
172		printf("ofwd: bio for invalid disk!\n");
173		biodone(bp);
174		return;
175	}
176
177	r = OF_seek(sc->ofwd_instance,
178	    (u_quad_t)(bp->bio_blkno * OFWD_BLOCKSIZE));
179	if (r == -1) {
180		bp->bio_resid = bp->bio_bcount;
181		bp->bio_error = EIO;
182		bp->bio_flags |= BIO_ERROR;
183		device_printf(sc->ofwd_dev, "seek failed\n");
184		biodone(bp);
185		return;
186	}
187
188	if (bp->bio_cmd == BIO_READ) {
189		r = OF_read(sc->ofwd_instance, (void *)bp->bio_data,
190		    bp->bio_bcount);
191	} else {
192		r = OF_write(sc->ofwd_instance, (void *)bp->bio_data,
193			    bp->bio_bcount);
194	}
195
196	if (r < bp->bio_bcount) {
197		device_printf(sc->ofwd_dev, "r (%ld) < bp->bio_bcount (%ld)\n",
198		    r, bp->bio_bcount);
199		if (r != -1)
200			bp->bio_resid = bp->bio_bcount - r;
201		bp->bio_error = EIO;
202		bp->bio_flags |= BIO_ERROR;
203	} else if (r > bp->bio_bcount)
204		panic("ofwd: more bytes read/written than requested");
205
206	bp->bio_resid -= r;
207	biodone(bp);
208
209	return;
210}
211
212/*
213 * Probe for an OpenFirmware disk.
214 */
215static int
216ofwd_probe(device_t dev)
217{
218	char		*type;
219
220	type = nexus_get_device_type(dev);
221
222	if (type == NULL || strcmp(type, "disk") != 0)
223		return (ENXIO);
224
225	device_set_desc(dev, "OpenFirmware disk");
226	return (0);
227}
228
229static int
230ofwd_attach(device_t dev)
231{
232	struct	ofwd_softc *sc;
233	char	path[128];
234	dev_t	dsk;
235
236	sc = device_get_softc(dev);
237	sc->ofwd_dev = dev;
238
239	bzero(path, 128);
240	OF_package_to_path(nexus_get_node(dev), path, 128);
241	device_printf(dev, "located at %s\n", path);
242	sc->ofwd_instance = OF_open(path);
243	if (sc->ofwd_instance == -1) {
244		device_printf(dev, "could not create instance\n");
245		return (ENXIO);
246	}
247
248	dsk = disk_create(device_get_unit(dev), &sc->ofwd_disk, 0,
249	    &ofwd_cdevsw, &ofwddisk_cdevsw);
250	dsk->si_drv1 = sc;
251	sc->ofwd_dev_t = dsk;
252
253	dsk->si_iosize_max = PAGE_SIZE;
254
255	return (0);
256}
257
258static void
259ofwd_identify(driver_t *driver, device_t parent)
260{
261}
262