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