ofdisk.c revision 1.6 1 1.6 thorpej /* $NetBSD: ofdisk.c,v 1.6 1997/07/23 18:42:40 thorpej Exp $ */
2 1.1 ws
3 1.1 ws /*
4 1.1 ws * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 1.1 ws * Copyright (C) 1995, 1996 TooLs GmbH.
6 1.1 ws * All rights reserved.
7 1.1 ws *
8 1.1 ws * Redistribution and use in source and binary forms, with or without
9 1.1 ws * modification, are permitted provided that the following conditions
10 1.1 ws * are met:
11 1.1 ws * 1. Redistributions of source code must retain the above copyright
12 1.1 ws * notice, this list of conditions and the following disclaimer.
13 1.1 ws * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 ws * notice, this list of conditions and the following disclaimer in the
15 1.1 ws * documentation and/or other materials provided with the distribution.
16 1.1 ws * 3. All advertising materials mentioning features or use of this software
17 1.1 ws * must display the following acknowledgement:
18 1.1 ws * This product includes software developed by TooLs GmbH.
19 1.1 ws * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 1.1 ws * derived from this software without specific prior written permission.
21 1.1 ws *
22 1.1 ws * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 1.1 ws * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 ws * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 ws * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 1.1 ws * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 1.1 ws * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 1.1 ws * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.1 ws * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 1.1 ws * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 1.1 ws * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 ws */
33 1.1 ws
34 1.1 ws #include <sys/param.h>
35 1.1 ws #include <sys/buf.h>
36 1.1 ws #include <sys/device.h>
37 1.1 ws #include <sys/disklabel.h>
38 1.1 ws #include <sys/disk.h>
39 1.1 ws #include <sys/fcntl.h>
40 1.1 ws #include <sys/ioctl.h>
41 1.1 ws #include <sys/stat.h>
42 1.1 ws #include <sys/systm.h>
43 1.6 thorpej #include <sys/proc.h>
44 1.1 ws
45 1.1 ws #include <dev/ofw/openfirm.h>
46 1.1 ws
47 1.1 ws struct ofd_softc {
48 1.1 ws struct device sc_dev;
49 1.1 ws int sc_phandle;
50 1.1 ws int sc_unit;
51 1.4 thorpej int sc_flags;
52 1.1 ws struct disk sc_dk;
53 1.1 ws int sc_ihandle;
54 1.1 ws u_long max_transfer;
55 1.1 ws char sc_name[16];
56 1.1 ws };
57 1.1 ws
58 1.4 thorpej /* sc_flags */
59 1.4 thorpej #define OFDF_ISFLOPPY 0x01 /* we are a floppy drive */
60 1.4 thorpej
61 1.4 thorpej static int ofdprobe __P((struct device *, struct cfdata *, void *));
62 1.1 ws static void ofdattach __P((struct device *, struct device *, void *));
63 1.1 ws
64 1.1 ws struct cfattach ofdisk_ca = {
65 1.1 ws sizeof(struct ofd_softc), ofdprobe, ofdattach
66 1.1 ws };
67 1.1 ws
68 1.1 ws struct cfdriver ofdisk_cd = {
69 1.1 ws NULL, "ofdisk", DV_DISK
70 1.1 ws };
71 1.1 ws
72 1.1 ws void ofdstrategy __P((struct buf *));
73 1.1 ws
74 1.1 ws struct dkdriver ofdkdriver = { ofdstrategy };
75 1.1 ws
76 1.1 ws static int
77 1.1 ws ofdprobe(parent, match, aux)
78 1.1 ws struct device *parent;
79 1.4 thorpej struct cfdata *match;
80 1.4 thorpej void *aux;
81 1.1 ws {
82 1.1 ws struct ofprobe *ofp = aux;
83 1.1 ws char type[8];
84 1.1 ws int l;
85 1.1 ws
86 1.4 thorpej if ((l = OF_getprop(ofp->phandle, "device_type", type,
87 1.4 thorpej sizeof type - 1)) < 0)
88 1.1 ws return 0;
89 1.1 ws if (l >= sizeof type)
90 1.1 ws return 0;
91 1.1 ws type[l] = 0;
92 1.1 ws return !strcmp(type, "block");
93 1.1 ws }
94 1.1 ws
95 1.1 ws static void
96 1.1 ws ofdattach(parent, self, aux)
97 1.1 ws struct device *parent, *self;
98 1.1 ws void *aux;
99 1.1 ws {
100 1.1 ws struct ofd_softc *of = (void *)self;
101 1.1 ws struct ofprobe *ofp = aux;
102 1.4 thorpej char child[64];
103 1.1 ws int l;
104 1.4 thorpej
105 1.4 thorpej if ((l = OF_getprop(ofp->phandle, "name", child,
106 1.4 thorpej sizeof child - 1)) < 0)
107 1.4 thorpej panic("device without name?");
108 1.4 thorpej if (l >= sizeof child)
109 1.4 thorpej l = sizeof child - 1;
110 1.4 thorpej child[l] = 0;
111 1.4 thorpej
112 1.4 thorpej of->sc_flags = 0;
113 1.1 ws of->sc_phandle = ofp->phandle;
114 1.1 ws of->sc_unit = ofp->unit;
115 1.1 ws of->sc_ihandle = 0;
116 1.1 ws of->sc_dk.dk_driver = &ofdkdriver;
117 1.1 ws of->sc_dk.dk_name = of->sc_name;
118 1.1 ws strcpy(of->sc_name, of->sc_dev.dv_xname);
119 1.1 ws disk_attach(&of->sc_dk);
120 1.1 ws dk_establish(&of->sc_dk, self); /* XXX */
121 1.3 christos printf("\n");
122 1.4 thorpej
123 1.4 thorpej if (strcmp(child, "floppy") == 0)
124 1.4 thorpej of->sc_flags |= OFDF_ISFLOPPY;
125 1.1 ws }
126 1.1 ws
127 1.1 ws int
128 1.1 ws ofdopen(dev, flags, fmt, p)
129 1.1 ws dev_t dev;
130 1.1 ws int flags;
131 1.1 ws int fmt;
132 1.1 ws struct proc *p;
133 1.1 ws {
134 1.1 ws int unit = DISKUNIT(dev);
135 1.1 ws struct ofd_softc *of;
136 1.1 ws char path[256];
137 1.1 ws struct disklabel *lp;
138 1.4 thorpej char *errmes;
139 1.1 ws int l;
140 1.1 ws
141 1.1 ws if (unit >= ofdisk_cd.cd_ndevs)
142 1.1 ws return ENXIO;
143 1.1 ws if (!(of = ofdisk_cd.cd_devs[unit]))
144 1.1 ws return ENXIO;
145 1.1 ws
146 1.1 ws if (!of->sc_ihandle) {
147 1.4 thorpej if ((l = OF_package_to_path(of->sc_phandle, path,
148 1.4 thorpej sizeof path - 3)) < 0)
149 1.1 ws return ENXIO;
150 1.1 ws if (l >= sizeof path - 3)
151 1.1 ws return ENXIO;
152 1.1 ws path[l] = 0;
153 1.1 ws
154 1.1 ws /*
155 1.4 thorpej * XXX This is for the benefit of SCSI/IDE disks that don't
156 1.4 thorpej * XXX have all their childs in the device tree.
157 1.4 thorpej * XXX YES, I DO THINK THIS IS A BUG IN OPENFIRMWARE!!!
158 1.4 thorpej * XXX And yes, this is a very gross hack!
159 1.4 thorpej * XXX See also ofscsi.c
160 1.1 ws */
161 1.1 ws if (!strcmp(path + l - 4, "disk")) {
162 1.1 ws path[l++] = '@';
163 1.1 ws path[l++] = '0' + of->sc_unit;
164 1.1 ws path[l] = 0;
165 1.1 ws }
166 1.1 ws
167 1.1 ws strcat(path, ":0");
168 1.1 ws
169 1.1 ws if (!(of->sc_ihandle = OF_open(path)))
170 1.1 ws return ENXIO;
171 1.1 ws
172 1.1 ws /*
173 1.1 ws * Try to get characteristics of the disk.
174 1.1 ws */
175 1.4 thorpej of->max_transfer = OF_call_method_1("max-transfer",
176 1.4 thorpej of->sc_ihandle, 0);
177 1.1 ws if (of->max_transfer > MAXPHYS)
178 1.1 ws of->max_transfer = MAXPHYS;
179 1.4 thorpej
180 1.1 ws lp = of->sc_dk.dk_label;
181 1.1 ws bzero(lp, sizeof *lp);
182 1.4 thorpej
183 1.4 thorpej /*
184 1.4 thorpej * XXX Firmware bug? Asking for block size gives a
185 1.4 thorpej * XXX rediculous number! So we use what the boot program
186 1.4 thorpej * XXX uses.
187 1.4 thorpej */
188 1.4 thorpej lp->d_secsize = DEV_BSIZE;
189 1.4 thorpej
190 1.4 thorpej lp->d_secperunit = OF_call_method_1("#blocks",
191 1.4 thorpej of->sc_ihandle, 0);
192 1.1 ws if (lp->d_secperunit == (u_int32_t)-1)
193 1.1 ws lp->d_secperunit = 0x7fffffff;
194 1.4 thorpej
195 1.1 ws lp->d_secpercyl = 1;
196 1.1 ws lp->d_nsectors = 1;
197 1.1 ws lp->d_ntracks = 1;
198 1.1 ws lp->d_ncylinders = lp->d_secperunit;
199 1.1 ws
200 1.1 ws lp->d_partitions[RAW_PART].p_offset = 0;
201 1.1 ws lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
202 1.4 thorpej lp->d_npartitions = RAW_PART + 1;
203 1.4 thorpej
204 1.4 thorpej /*
205 1.4 thorpej * Don't read the disklabel on a floppy; simply
206 1.4 thorpej * assign all partitions the same size/offset as
207 1.4 thorpej * RAW_PART. (This is essentially what the ISA
208 1.4 thorpej * floppy driver does, but we don't deal with
209 1.4 thorpej * density stuff.)
210 1.4 thorpej */
211 1.4 thorpej if (of->sc_flags & OFDF_ISFLOPPY) {
212 1.4 thorpej lp->d_npartitions = MAXPARTITIONS;
213 1.4 thorpej for (l = 0; l < lp->d_npartitions; l++) {
214 1.4 thorpej if (l == RAW_PART)
215 1.4 thorpej continue;
216 1.4 thorpej /* struct copy */
217 1.4 thorpej lp->d_partitions[l] =
218 1.4 thorpej lp->d_partitions[RAW_PART];
219 1.4 thorpej }
220 1.4 thorpej } else {
221 1.4 thorpej errmes = readdisklabel(MAKEDISKDEV(major(dev),
222 1.4 thorpej unit, RAW_PART), ofdstrategy, lp,
223 1.4 thorpej of->sc_dk.dk_cpulabel);
224 1.4 thorpej if (errmes != NULL)
225 1.4 thorpej printf("%s: %s\n", errmes);
226 1.4 thorpej }
227 1.1 ws }
228 1.1 ws
229 1.1 ws switch (fmt) {
230 1.1 ws case S_IFCHR:
231 1.1 ws of->sc_dk.dk_copenmask |= 1 << DISKPART(dev);
232 1.1 ws break;
233 1.1 ws case S_IFBLK:
234 1.1 ws of->sc_dk.dk_bopenmask |= 1 << DISKPART(dev);
235 1.1 ws break;
236 1.1 ws }
237 1.4 thorpej of->sc_dk.dk_openmask =
238 1.4 thorpej of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
239 1.1 ws
240 1.1 ws return 0;
241 1.1 ws }
242 1.1 ws
243 1.1 ws int
244 1.1 ws ofdclose(dev, flags, fmt, p)
245 1.1 ws dev_t dev;
246 1.1 ws int flags;
247 1.1 ws int fmt;
248 1.1 ws struct proc *p;
249 1.1 ws {
250 1.1 ws struct ofd_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
251 1.1 ws
252 1.1 ws switch (fmt) {
253 1.1 ws case S_IFCHR:
254 1.1 ws of->sc_dk.dk_copenmask &= ~(1 << DISKPART(dev));
255 1.1 ws break;
256 1.1 ws case S_IFBLK:
257 1.1 ws of->sc_dk.dk_bopenmask &= ~(1 << DISKPART(dev));
258 1.1 ws break;
259 1.1 ws }
260 1.1 ws of->sc_dk.dk_openmask = of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
261 1.1 ws
262 1.4 thorpej #ifdef FIRMWORKSBUGS
263 1.1 ws /*
264 1.1 ws * This is a hack to get the firmware to flush its buffers.
265 1.1 ws */
266 1.1 ws OF_seek(of->sc_ihandle, 0);
267 1.1 ws #endif
268 1.1 ws if (!of->sc_dk.dk_openmask) {
269 1.1 ws OF_close(of->sc_ihandle);
270 1.1 ws of->sc_ihandle = 0;
271 1.1 ws }
272 1.1 ws
273 1.1 ws return 0;
274 1.1 ws }
275 1.1 ws
276 1.1 ws void
277 1.1 ws ofdstrategy(bp)
278 1.1 ws struct buf *bp;
279 1.1 ws {
280 1.1 ws struct ofd_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
281 1.1 ws struct partition *p;
282 1.1 ws u_quad_t off;
283 1.1 ws int read;
284 1.1 ws int (*OF_io)(int, void *, int);
285 1.1 ws daddr_t blkno = bp->b_blkno;
286 1.4 thorpej
287 1.1 ws bp->b_resid = 0;
288 1.1 ws if (bp->b_bcount == 0)
289 1.1 ws goto done;
290 1.1 ws
291 1.1 ws OF_io = bp->b_flags & B_READ ? OF_read : OF_write;
292 1.1 ws
293 1.1 ws if (DISKPART(bp->b_dev) != RAW_PART) {
294 1.1 ws if (bounds_check_with_label(bp, of->sc_dk.dk_label, 0) <= 0) {
295 1.1 ws bp->b_resid = bp->b_bcount;
296 1.1 ws goto done;
297 1.1 ws }
298 1.1 ws p = &of->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)];
299 1.1 ws blkno = bp->b_blkno + p->p_offset;
300 1.1 ws }
301 1.4 thorpej
302 1.1 ws disk_busy(&of->sc_dk);
303 1.1 ws
304 1.1 ws off = (u_quad_t)blkno * DEV_BSIZE;
305 1.1 ws read = -1;
306 1.1 ws do {
307 1.1 ws if (OF_seek(of->sc_ihandle, off) < 0)
308 1.1 ws break;
309 1.1 ws read = OF_io(of->sc_ihandle, bp->b_data, bp->b_bcount);
310 1.1 ws } while (read == -2);
311 1.4 thorpej
312 1.1 ws if (read < 0) {
313 1.1 ws bp->b_error = EIO;
314 1.1 ws bp->b_flags |= B_ERROR;
315 1.1 ws bp->b_resid = bp->b_bcount;
316 1.1 ws } else
317 1.1 ws bp->b_resid = bp->b_bcount - read;
318 1.1 ws
319 1.1 ws disk_unbusy(&of->sc_dk, bp->b_bcount - bp->b_resid);
320 1.1 ws
321 1.1 ws done:
322 1.1 ws biodone(bp);
323 1.1 ws }
324 1.1 ws
325 1.1 ws static void
326 1.1 ws ofminphys(bp)
327 1.1 ws struct buf *bp;
328 1.1 ws {
329 1.1 ws struct ofd_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
330 1.1 ws
331 1.1 ws if (bp->b_bcount > of->max_transfer)
332 1.1 ws bp->b_bcount = of->max_transfer;
333 1.1 ws }
334 1.1 ws
335 1.1 ws int
336 1.1 ws ofdread(dev, uio)
337 1.1 ws dev_t dev;
338 1.1 ws struct uio *uio;
339 1.1 ws {
340 1.1 ws return physio(ofdstrategy, NULL, dev, B_READ, ofminphys, uio);
341 1.1 ws }
342 1.1 ws
343 1.1 ws int
344 1.1 ws ofdwrite(dev, uio)
345 1.1 ws dev_t dev;
346 1.1 ws struct uio *uio;
347 1.1 ws {
348 1.1 ws return physio(ofdstrategy, NULL, dev, B_WRITE, ofminphys, uio);
349 1.1 ws }
350 1.1 ws
351 1.1 ws int
352 1.1 ws ofdioctl(dev, cmd, data, flag, p)
353 1.1 ws dev_t dev;
354 1.1 ws u_long cmd;
355 1.1 ws caddr_t data;
356 1.1 ws int flag;
357 1.1 ws struct proc *p;
358 1.1 ws {
359 1.1 ws struct ofd_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
360 1.1 ws int error;
361 1.1 ws
362 1.1 ws switch (cmd) {
363 1.1 ws case DIOCGDINFO:
364 1.1 ws *(struct disklabel *)data = *of->sc_dk.dk_label;
365 1.1 ws return 0;
366 1.1 ws
367 1.1 ws case DIOCGPART:
368 1.1 ws ((struct partinfo *)data)->disklab = of->sc_dk.dk_label;
369 1.1 ws ((struct partinfo *)data)->part =
370 1.1 ws &of->sc_dk.dk_label->d_partitions[DISKPART(dev)];
371 1.1 ws return 0;
372 1.1 ws
373 1.1 ws case DIOCWDINFO:
374 1.1 ws case DIOCSDINFO:
375 1.1 ws if ((flag & FWRITE) == 0)
376 1.1 ws return EBADF;
377 1.1 ws
378 1.1 ws error = setdisklabel(of->sc_dk.dk_label,
379 1.4 thorpej (struct disklabel *)data, /*of->sc_dk.dk_openmask */0,
380 1.4 thorpej of->sc_dk.dk_cpulabel);
381 1.1 ws if (error == 0 && cmd == DIOCWDINFO)
382 1.1 ws error = writedisklabel(MAKEDISKDEV(major(dev),
383 1.4 thorpej DISKUNIT(dev), RAW_PART), ofdstrategy,
384 1.4 thorpej of->sc_dk.dk_label, of->sc_dk.dk_cpulabel);
385 1.1 ws
386 1.1 ws return error;
387 1.1 ws default:
388 1.1 ws return ENOTTY;
389 1.1 ws }
390 1.1 ws }
391 1.1 ws
392 1.1 ws int
393 1.1 ws ofddump(dev, blkno, va, size)
394 1.1 ws dev_t dev;
395 1.1 ws daddr_t blkno;
396 1.1 ws caddr_t va;
397 1.1 ws size_t size;
398 1.1 ws {
399 1.1 ws return EINVAL;
400 1.1 ws }
401 1.1 ws
402 1.1 ws int
403 1.1 ws ofdsize(dev)
404 1.1 ws dev_t dev;
405 1.1 ws {
406 1.1 ws struct ofd_softc *of;
407 1.5 thorpej struct disklabel *lp;
408 1.5 thorpej int size, part, omask, unit;
409 1.5 thorpej
410 1.5 thorpej unit = DISKUNIT(dev);
411 1.5 thorpej if (unit >= ofdisk_cd.cd_ndevs ||
412 1.5 thorpej (of = ofdisk_cd.cd_devs[unit]) == NULL)
413 1.1 ws return -1;
414 1.5 thorpej
415 1.1 ws part = DISKPART(dev);
416 1.5 thorpej omask = of->sc_dk.dk_openmask & (1 << part);
417 1.6 thorpej lp = of->sc_dk.dk_label;
418 1.5 thorpej
419 1.5 thorpej if (omask == 0 && ofdopen(dev, 0, S_IFBLK, curproc) != 0)
420 1.5 thorpej return -1;
421 1.5 thorpej
422 1.5 thorpej if (lp->d_partitions[part].p_fstype != FS_SWAP)
423 1.1 ws size = -1;
424 1.1 ws else
425 1.5 thorpej size = lp->d_partitions[part].p_size *
426 1.5 thorpej (lp->d_secsize / DEV_BSIZE);
427 1.5 thorpej
428 1.5 thorpej if (omask == 0 && ofdclose(dev, 0, S_IFBLK, curproc) != 0)
429 1.1 ws return -1;
430 1.5 thorpej
431 1.1 ws return size;
432 1.1 ws }
433