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