ofdisk.c revision 1.29 1 /* $NetBSD: ofdisk.c,v 1.29 2004/09/25 05:21:03 thorpej 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/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ofdisk.c,v 1.29 2004/09/25 05:21:03 thorpej Exp $");
36
37 #include <sys/param.h>
38 #include <sys/buf.h>
39 #include <sys/device.h>
40 #include <sys/conf.h>
41 #include <sys/disklabel.h>
42 #include <sys/disk.h>
43 #include <sys/fcntl.h>
44 #include <sys/ioctl.h>
45 #include <sys/stat.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 #include <sys/conf.h>
49
50 #include <dev/ofw/openfirm.h>
51
52 struct ofdisk_softc {
53 struct device sc_dev;
54 int sc_phandle;
55 int sc_unit;
56 int sc_flags;
57 struct disk sc_dk;
58 int sc_ihandle;
59 u_long max_transfer;
60 };
61
62 /* sc_flags */
63 #define OFDF_ISFLOPPY 0x01 /* we are a floppy drive */
64
65 #define OFDISK_FLOPPY_P(of) ((of)->sc_flags & OFDF_ISFLOPPY)
66
67 static int ofdisk_match (struct device *, struct cfdata *, void *);
68 static void ofdisk_attach (struct device *, struct device *, void *);
69
70 CFATTACH_DECL(ofdisk, sizeof(struct ofdisk_softc),
71 ofdisk_match, ofdisk_attach, NULL, NULL);
72
73 extern struct cfdriver ofdisk_cd;
74
75 dev_type_open(ofdisk_open);
76 dev_type_close(ofdisk_close);
77 dev_type_read(ofdisk_read);
78 dev_type_write(ofdisk_write);
79 dev_type_ioctl(ofdisk_ioctl);
80 dev_type_strategy(ofdisk_strategy);
81 dev_type_dump(ofdisk_dump);
82 dev_type_size(ofdisk_size);
83
84 const struct bdevsw ofdisk_bdevsw = {
85 ofdisk_open, ofdisk_close, ofdisk_strategy, ofdisk_ioctl,
86 ofdisk_dump, ofdisk_size, D_DISK
87 };
88
89 const struct cdevsw ofdisk_cdevsw = {
90 ofdisk_open, ofdisk_close, ofdisk_read, ofdisk_write, ofdisk_ioctl,
91 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
92 };
93
94 static void ofminphys(struct buf *);
95
96 struct dkdriver ofdisk_dkdriver = { ofdisk_strategy, ofminphys };
97
98 void ofdisk_getdefaultlabel (struct ofdisk_softc *, struct disklabel *);
99 void ofdisk_getdisklabel (dev_t);
100
101 static int
102 ofdisk_match(struct device *parent, struct cfdata *match, void *aux)
103 {
104 struct ofbus_attach_args *oba = aux;
105 char type[8];
106 int l;
107
108 if (strcmp(oba->oba_busname, "ofw"))
109 return (0);
110 if ((l = OF_getprop(oba->oba_phandle, "device_type", type,
111 sizeof type - 1)) < 0)
112 return 0;
113 if (l >= sizeof type)
114 return 0;
115 type[l] = 0;
116 return !strcmp(type, "block");
117 }
118
119 static void
120 ofdisk_attach(struct device *parent, struct device *self, void *aux)
121 {
122 struct ofdisk_softc *of = (void *)self;
123 struct ofbus_attach_args *oba = aux;
124 char child[64];
125 int l;
126
127 if ((l = OF_getprop(oba->oba_phandle, "name", child,
128 sizeof child - 1)) < 0)
129 panic("device without name?");
130 if (l >= sizeof child)
131 l = sizeof child - 1;
132 child[l] = 0;
133
134 of->sc_flags = 0;
135 of->sc_phandle = oba->oba_phandle;
136 of->sc_unit = oba->oba_unit;
137 of->sc_ihandle = 0;
138 of->sc_dk.dk_driver = &ofdisk_dkdriver;
139 of->sc_dk.dk_name = of->sc_dev.dv_xname;
140 disk_attach(&of->sc_dk);
141 printf("\n");
142
143 if (strcmp(child, "floppy") == 0)
144 of->sc_flags |= OFDF_ISFLOPPY;
145 else {
146 /* Discover wedges on this disk. */
147 dkwedge_discover(&of->sc_dk);
148 }
149 }
150
151 int
152 ofdisk_open(dev_t dev, int flags, int fmt, struct proc *p)
153 {
154 int unit = DISKUNIT(dev);
155 struct ofdisk_softc *of;
156 char path[256];
157 int error, l, part;
158
159 if (unit >= ofdisk_cd.cd_ndevs)
160 return ENXIO;
161 if (!(of = ofdisk_cd.cd_devs[unit]))
162 return ENXIO;
163
164 part = DISKPART(dev);
165
166 if ((error = lockmgr(&of->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
167 return (error);
168
169 /*
170 * If there are wedges, and this is not RAW_PART, then we
171 * need to fail.
172 */
173 if (of->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
174 error = EBUSY;
175 goto bad1;
176 }
177
178 if (!of->sc_ihandle) {
179 if ((l = OF_package_to_path(of->sc_phandle, path,
180 sizeof path - 3)) < 0 ||
181 l >= sizeof path - 3) {
182 error = ENXIO;
183 goto bad1;
184 }
185 path[l] = 0;
186
187 /*
188 * XXX This is for the benefit of SCSI/IDE disks that don't
189 * XXX have all their childs in the device tree.
190 * XXX YES, I DO THINK THIS IS A BUG IN OPENFIRMWARE!!!
191 * XXX And yes, this is a very gross hack!
192 * XXX See also ofscsi.c
193 */
194 if (!strcmp(path + l - 4, "disk")) {
195 path[l++] = '@';
196 path[l++] = '0' + of->sc_unit;
197 path[l] = 0;
198 }
199
200 strlcat(path, ":0", sizeof(path));
201
202 if ((of->sc_ihandle = OF_open(path)) == -1) {
203 error = ENXIO;
204 goto bad1;
205 }
206
207 /*
208 * Try to get characteristics of the disk.
209 */
210 of->max_transfer = OF_call_method_1("max-transfer",
211 of->sc_ihandle, 0);
212 if (of->max_transfer > MAXPHYS)
213 of->max_transfer = MAXPHYS;
214
215 ofdisk_getdisklabel(dev);
216 }
217
218 switch (fmt) {
219 case S_IFCHR:
220 of->sc_dk.dk_copenmask |= 1 << part;
221 break;
222 case S_IFBLK:
223 of->sc_dk.dk_bopenmask |= 1 << part;
224 break;
225 }
226 of->sc_dk.dk_openmask =
227 of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
228
229 (void) lockmgr(&of->sc_dk.dk_openlock, LK_RELEASE, NULL);
230 return 0;
231
232 bad1:
233 (void) lockmgr(&of->sc_dk.dk_openlock, LK_RELEASE, NULL);
234 return (error);
235 }
236
237 int
238 ofdisk_close(dev_t dev, int flags, int fmt, struct proc *p)
239 {
240 struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
241 int error;
242
243 if ((error = lockmgr(&of->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
244 return (error);
245
246 switch (fmt) {
247 case S_IFCHR:
248 of->sc_dk.dk_copenmask &= ~(1 << DISKPART(dev));
249 break;
250 case S_IFBLK:
251 of->sc_dk.dk_bopenmask &= ~(1 << DISKPART(dev));
252 break;
253 }
254 of->sc_dk.dk_openmask = of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
255
256 #ifdef FIRMWORKSBUGS
257 /*
258 * This is a hack to get the firmware to flush its buffers.
259 */
260 OF_seek(of->sc_ihandle, 0);
261 #endif
262 if (!of->sc_dk.dk_openmask) {
263 OF_close(of->sc_ihandle);
264 of->sc_ihandle = 0;
265 }
266
267 (void) lockmgr(&of->sc_dk.dk_openlock, LK_RELEASE, NULL);
268 return 0;
269 }
270
271 void
272 ofdisk_strategy(struct buf *bp)
273 {
274 struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
275 struct partition *p;
276 u_quad_t off;
277 int read;
278 int (*OF_io)(int, void *, int);
279 daddr_t blkno = bp->b_blkno;
280
281 bp->b_resid = 0;
282 if (bp->b_bcount == 0)
283 goto done;
284
285 OF_io = bp->b_flags & B_READ ? OF_read : OF_write;
286
287 if (DISKPART(bp->b_dev) != RAW_PART) {
288 if (bounds_check_with_label(&of->sc_dk, bp, 0) <= 0) {
289 bp->b_resid = bp->b_bcount;
290 goto done;
291 }
292 p = &of->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)];
293 blkno = bp->b_blkno + p->p_offset;
294 }
295
296 disk_busy(&of->sc_dk);
297
298 off = (u_quad_t)blkno * DEV_BSIZE;
299 read = -1;
300 do {
301 if (OF_seek(of->sc_ihandle, off) < 0)
302 break;
303 read = OF_io(of->sc_ihandle, bp->b_data, bp->b_bcount);
304 } while (read == -2);
305
306 if (read < 0) {
307 bp->b_error = EIO;
308 bp->b_flags |= B_ERROR;
309 bp->b_resid = bp->b_bcount;
310 } else
311 bp->b_resid = bp->b_bcount - read;
312
313 disk_unbusy(&of->sc_dk, bp->b_bcount - bp->b_resid,
314 (bp->b_flags & B_READ));
315
316 done:
317 biodone(bp);
318 }
319
320 static void
321 ofminphys(struct buf *bp)
322 {
323 struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
324
325 if (bp->b_bcount > of->max_transfer)
326 bp->b_bcount = of->max_transfer;
327 }
328
329 int
330 ofdisk_read(dev_t dev, struct uio *uio, int flags)
331 {
332 return physio(ofdisk_strategy, NULL, dev, B_READ, ofminphys, uio);
333 }
334
335 int
336 ofdisk_write(dev_t dev, struct uio *uio, int flags)
337 {
338 return physio(ofdisk_strategy, NULL, dev, B_WRITE, ofminphys, uio);
339 }
340
341 int
342 ofdisk_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
343 {
344 struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
345 int error;
346 #ifdef __HAVE_OLD_DISKLABEL
347 struct disklabel newlabel;
348 #endif
349
350 switch (cmd) {
351 case DIOCGDINFO:
352 *(struct disklabel *)data = *of->sc_dk.dk_label;
353 return 0;
354 #ifdef __HAVE_OLD_DISKLABEL
355 case ODIOCGDINFO:
356 newlabel = *of->sc_dk.dk_label;
357 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
358 return ENOTTY;
359 memcpy(data, &newlabel, sizeof (struct olddisklabel));
360 return 0;
361 #endif
362
363 case DIOCGPART:
364 ((struct partinfo *)data)->disklab = of->sc_dk.dk_label;
365 ((struct partinfo *)data)->part =
366 &of->sc_dk.dk_label->d_partitions[DISKPART(dev)];
367 return 0;
368
369 case DIOCWDINFO:
370 case DIOCSDINFO:
371 #ifdef __HAVE_OLD_DISKLABEL
372 case ODIOCWDINFO:
373 case ODIOCSDINFO:
374 #endif
375 {
376 struct disklabel *lp;
377
378 #ifdef __HAVE_OLD_DISKLABEL
379 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
380 memset(&newlabel, 0, sizeof newlabel);
381 memcpy(&newlabel, data, sizeof (struct olddisklabel));
382 lp = &newlabel;
383 } else
384 #endif
385 lp = (struct disklabel *)data;
386
387 if ((flag & FWRITE) == 0)
388 return EBADF;
389
390 if ((error = lockmgr(&of->sc_dk.dk_openlock, LK_EXCLUSIVE,
391 NULL)) != 0)
392 return (error);
393
394 error = setdisklabel(of->sc_dk.dk_label,
395 lp, /*of->sc_dk.dk_openmask */0,
396 of->sc_dk.dk_cpulabel);
397 if (error == 0 && cmd == DIOCWDINFO
398 #ifdef __HAVE_OLD_DISKLABEL
399 || xfer == ODIOCWDINFO
400 #endif
401 )
402 error = writedisklabel(MAKEDISKDEV(major(dev),
403 DISKUNIT(dev), RAW_PART), ofdisk_strategy,
404 of->sc_dk.dk_label, of->sc_dk.dk_cpulabel);
405
406 (void) lockmgr(&of->sc_dk.dk_openlock, LK_RELEASE, NULL);
407
408 return error;
409 }
410
411 case DIOCGDEFLABEL:
412 ofdisk_getdefaultlabel(of, (struct disklabel *)data);
413 return 0;
414 #ifdef __HAVE_OLD_DISKLABEL
415 case DIOCGDEFLABEL:
416 ofdisk_getdefaultlabel(of, &newlabel);
417 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
418 return ENOTTY;
419 memcpy(data, &newlabel, sizeof (struct olddisklabel));
420 return 0;
421 #endif
422
423 case DIOCAWEDGE:
424 {
425 struct dkwedge_info *dkw = (void *) data;
426
427 if (OFDISK_FLOPPY_P(of))
428 return (ENOTTY);
429
430 if ((flag & FWRITE) == 0)
431 return (EBADF);
432
433 /* If the ioctl happens here, the parent is us. */
434 strcpy(dkw->dkw_parent, of->sc_dev.dv_xname);
435 return (dkwedge_add(dkw));
436 }
437
438 case DIOCDWEDGE:
439 {
440 struct dkwedge_info *dkw = (void *) data;
441
442 if (OFDISK_FLOPPY_P(of))
443 return (ENOTTY);
444
445 if ((flag & FWRITE) == 0)
446 return (EBADF);
447
448 /* If the ioctl happens here, the parent is us. */
449 strcpy(dkw->dkw_parent, of->sc_dev.dv_xname);
450 return (dkwedge_del(dkw));
451 }
452
453 case DIOCLWEDGES:
454 {
455 struct dkwedge_list *dkwl = (void *) data;
456
457 if (OFDISK_FLOPPY_P(of))
458 return (ENOTTY);
459
460 return (dkwedge_list(&of->sc_dk, dkwl, p));
461 }
462
463 default:
464 return ENOTTY;
465 }
466 }
467
468 int
469 ofdisk_dump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
470 {
471 return EINVAL;
472 }
473
474 int
475 ofdisk_size(dev_t dev)
476 {
477 struct ofdisk_softc *of;
478 struct disklabel *lp;
479 int size, part, omask, unit;
480
481 unit = DISKUNIT(dev);
482 if (unit >= ofdisk_cd.cd_ndevs ||
483 (of = ofdisk_cd.cd_devs[unit]) == NULL)
484 return -1;
485
486 part = DISKPART(dev);
487 omask = of->sc_dk.dk_openmask & (1 << part);
488 lp = of->sc_dk.dk_label;
489
490 if (omask == 0 && ofdisk_open(dev, 0, S_IFBLK, curproc) != 0)
491 return -1;
492
493 if (lp->d_partitions[part].p_fstype != FS_SWAP)
494 size = -1;
495 else
496 size = lp->d_partitions[part].p_size *
497 (lp->d_secsize / DEV_BSIZE);
498
499 if (omask == 0 && ofdisk_close(dev, 0, S_IFBLK, curproc) != 0)
500 return -1;
501
502 return size;
503 }
504
505 void
506 ofdisk_getdefaultlabel(struct ofdisk_softc *of, struct disklabel *lp)
507 {
508
509 memset(lp, 0, sizeof *lp);
510
511 /*
512 * XXX Firmware bug? Asking for block size gives a
513 * XXX ridiculous number! So we use what the boot program
514 * XXX uses.
515 */
516 lp->d_secsize = DEV_BSIZE;
517
518 lp->d_secperunit = OF_call_method_1("#blocks",
519 of->sc_ihandle, 0);
520 if (lp->d_secperunit == (u_int32_t)-1)
521 lp->d_secperunit = 0x7fffffff;
522
523 lp->d_secpercyl = 1;
524 lp->d_nsectors = 1;
525 lp->d_ntracks = 1;
526 lp->d_ncylinders = lp->d_secperunit;
527
528 lp->d_partitions[RAW_PART].p_offset = 0;
529 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
530 lp->d_npartitions = RAW_PART + 1;
531
532 lp->d_magic = DISKMAGIC;
533 lp->d_magic2 = DISKMAGIC;
534 lp->d_checksum = dkcksum(lp);
535 }
536
537 void
538 ofdisk_getdisklabel(dev)
539 dev_t dev;
540 {
541 int unit = DISKUNIT(dev);
542 struct ofdisk_softc *of = ofdisk_cd.cd_devs[unit];
543 struct disklabel *lp = of->sc_dk.dk_label;
544 const char *errmes;
545 int l;
546
547 ofdisk_getdefaultlabel(of, lp);
548
549 /*
550 * Don't read the disklabel on a floppy; simply
551 * assign all partitions the same size/offset as
552 * RAW_PART. (This is essentially what the ISA
553 * floppy driver does, but we don't deal with
554 * density stuff.)
555 */
556 if (OFDISK_FLOPPY_P(of)) {
557 lp->d_npartitions = MAXPARTITIONS;
558 for (l = 0; l < lp->d_npartitions; l++) {
559 if (l == RAW_PART)
560 continue;
561 /* struct copy */
562 lp->d_partitions[l] =
563 lp->d_partitions[RAW_PART];
564 }
565 lp->d_checksum = dkcksum(lp);
566 } else {
567 errmes = readdisklabel(MAKEDISKDEV(major(dev),
568 unit, RAW_PART), ofdisk_strategy, lp,
569 of->sc_dk.dk_cpulabel);
570 if (errmes != NULL)
571 printf("%s: %s\n", of->sc_dev.dv_xname, errmes);
572 }
573 }
574