Deleted Added
full compact
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 $
25 * $FreeBSD: head/sys/dev/ofw/ofw_disk.c 103714 2002-09-20 19:36:05Z phk $
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;
117
118 sc = (struct ofwd_softc *)dev->si_drv1;
119 if (sc == NULL)
120 return (ENXIO);
121
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;
122 sc->ofwd_disk.d_sectorsize = OFWD_BLOCKSIZE;
123 sc->ofwd_disk.d_mediasize = (off_t)33554432 * OFWD_BLOCKSIZE;
124 /* XXX: probably don't need the next two */
125 sc->ofwd_disk.d_fwsectors = 33554432;
126 sc->ofwd_disk.d_fwheads = 1
127
128 sc->ofwd_flags |= OFWD_OPEN;
129 return (0);
130}
131
132/*
133 * Handle last close of the disk device.
134 */
135static int
136ofwd_close(dev_t dev, int flags, int fmt, struct thread *td)
137{
138 struct ofwd_softc *sc;
139
140 sc = (struct ofwd_softc *)dev->si_drv1;
141
142 if (sc == NULL)
143 return (ENXIO);
144
145 sc->ofwd_flags &= ~OFWD_OPEN;
146
147 return (0);
148}
149
150/*
151 * Handle an I/O request.
152 */
153static void
154ofwd_strategy(struct bio *bp)
155{
156 struct ofwd_softc *sc;
157 long r;
158
159 sc = (struct ofwd_softc *)bp->bio_dev->si_drv1;
160
161 if (sc == NULL) {
162 bp->bio_error = EINVAL;
163 bp->bio_flags |= BIO_ERROR;
164 printf("ofwd: bio for invalid disk!\n");
165 biodone(bp);
166 return;
167 }
168
169 r = OF_seek(sc->ofwd_instance,
170 (u_quad_t)(bp->bio_blkno * OFWD_BLOCKSIZE));
171 if (r == -1) {
172 bp->bio_resid = bp->bio_bcount;
173 bp->bio_error = EIO;
174 bp->bio_flags |= BIO_ERROR;
175 device_printf(sc->ofwd_dev, "seek failed\n");
176 biodone(bp);
177 return;
178 }
179
180 if (bp->bio_cmd == BIO_READ) {
181 r = OF_read(sc->ofwd_instance, (void *)bp->bio_data,
182 bp->bio_bcount);
183 } else {
184 r = OF_write(sc->ofwd_instance, (void *)bp->bio_data,
185 bp->bio_bcount);
186 }
187
188 if (r < bp->bio_bcount) {
189 device_printf(sc->ofwd_dev, "r (%ld) < bp->bio_bcount (%ld)\n",
190 r, bp->bio_bcount);
191 if (r != -1)
192 bp->bio_resid = bp->bio_bcount - r;
193 bp->bio_error = EIO;
194 bp->bio_flags |= BIO_ERROR;
195 } else if (r > bp->bio_bcount)
196 panic("ofwd: more bytes read/written than requested");
197
198 bp->bio_resid -= r;
199 biodone(bp);
200
201 return;
202}
203
204/*
205 * Probe for an OpenFirmware disk.
206 */
207static int
208ofwd_probe(device_t dev)
209{
210 char *type;
211
212 type = nexus_get_device_type(dev);
213
214 if (type == NULL || strcmp(type, "disk") != 0)
215 return (ENXIO);
216
217 device_set_desc(dev, "OpenFirmware disk");
218 return (0);
219}
220
221static int
222ofwd_attach(device_t dev)
223{
224 struct ofwd_softc *sc;
225 char path[128];
226 dev_t dsk;
227
228 sc = device_get_softc(dev);
229 sc->ofwd_dev = dev;
230
231 bzero(path, 128);
232 OF_package_to_path(nexus_get_node(dev), path, 128);
233 device_printf(dev, "located at %s\n", path);
234 sc->ofwd_instance = OF_open(path);
235 if (sc->ofwd_instance == -1) {
236 device_printf(dev, "could not create instance\n");
237 return (ENXIO);
238 }
239
240 dsk = disk_create(device_get_unit(dev), &sc->ofwd_disk, 0,
241 &ofwd_cdevsw, &ofwddisk_cdevsw);
242 dsk->si_drv1 = sc;
243 sc->ofwd_dev_t = dsk;
244
245 dsk->si_iosize_max = PAGE_SIZE;
246
247 return (0);
248}
249
250static void
251ofwd_identify(driver_t *driver, device_t parent)
252{
253}