Home | History | Annotate | Line # | Download | only in ofw
ofdisk.c revision 1.35.2.1
      1 /*	$NetBSD: ofdisk.c,v 1.35.2.1 2007/08/19 19:24:31 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.35.2.1 2007/08/19 19:24:31 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 	if ((error = lockmgr(&of->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
    166 		return (error);
    167 
    168 	/*
    169 	 * If there are wedges, and this is not RAW_PART, then we
    170 	 * need to fail.
    171 	 */
    172 	if (of->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
    173 		error = EBUSY;
    174 		goto bad1;
    175 	}
    176 
    177 	if (!of->sc_ihandle) {
    178 		if ((l = OF_package_to_path(of->sc_phandle, path,
    179 		    sizeof path - 3)) < 0 ||
    180 		    l >= sizeof path - 3) {
    181 			error = ENXIO;
    182 			goto bad1;
    183 		}
    184 		path[l] = 0;
    185 
    186 		/*
    187 		 * XXX This is for the benefit of SCSI/IDE disks that don't
    188 		 * XXX have all their childs in the device tree.
    189 		 * XXX YES, I DO THINK THIS IS A BUG IN OPENFIRMWARE!!!
    190 		 * XXX And yes, this is a very gross hack!
    191 		 * XXX See also ofscsi.c
    192 		 */
    193 		if (!strcmp(path + l - 4, "disk")) {
    194 			path[l++] = '@';
    195 			path[l++] = '0' + of->sc_unit;
    196 			path[l] = 0;
    197 		}
    198 
    199 		strlcat(path, ":0", sizeof(path));
    200 
    201 		if ((of->sc_ihandle = OF_open(path)) == -1) {
    202 			error = ENXIO;
    203 			goto bad1;
    204 		}
    205 
    206 		/*
    207 		 * Try to get characteristics of the disk.
    208 		 */
    209 		of->max_transfer = OF_call_method_1("max-transfer",
    210 		    of->sc_ihandle, 0);
    211 		if (of->max_transfer > MAXPHYS)
    212 			of->max_transfer = MAXPHYS;
    213 
    214 		ofdisk_getdisklabel(dev);
    215 	}
    216 
    217 	switch (fmt) {
    218 	case S_IFCHR:
    219 		of->sc_dk.dk_copenmask |= 1 << part;
    220 		break;
    221 	case S_IFBLK:
    222 		of->sc_dk.dk_bopenmask |= 1 << part;
    223 		break;
    224 	}
    225 	of->sc_dk.dk_openmask =
    226 	    of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
    227 
    228 	(void) lockmgr(&of->sc_dk.dk_openlock, LK_RELEASE, NULL);
    229 	return 0;
    230 
    231  bad1:
    232 	(void) lockmgr(&of->sc_dk.dk_openlock, LK_RELEASE, NULL);
    233 	return (error);
    234 }
    235 
    236 int
    237 ofdisk_close(dev_t dev, int flags, int fmt, struct lwp *l)
    238 {
    239 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
    240 	int error;
    241 
    242 	if ((error = lockmgr(&of->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
    243 		return (error);
    244 
    245 	switch (fmt) {
    246 	case S_IFCHR:
    247 		of->sc_dk.dk_copenmask &= ~(1 << DISKPART(dev));
    248 		break;
    249 	case S_IFBLK:
    250 		of->sc_dk.dk_bopenmask &= ~(1 << DISKPART(dev));
    251 		break;
    252 	}
    253 	of->sc_dk.dk_openmask = of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
    254 
    255 #ifdef	FIRMWORKSBUGS
    256 	/*
    257 	 * This is a hack to get the firmware to flush its buffers.
    258 	 */
    259 	OF_seek(of->sc_ihandle, 0);
    260 #endif
    261 	if (!of->sc_dk.dk_openmask) {
    262 		OF_close(of->sc_ihandle);
    263 		of->sc_ihandle = 0;
    264 	}
    265 
    266 	(void) lockmgr(&of->sc_dk.dk_openlock, LK_RELEASE, NULL);
    267 	return 0;
    268 }
    269 
    270 void
    271 ofdisk_strategy(struct buf *bp)
    272 {
    273 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
    274 	struct partition *p;
    275 	u_quad_t off;
    276 	int read;
    277 	int (*OF_io)(int, void *, int);
    278 	daddr_t blkno = bp->b_blkno;
    279 
    280 	bp->b_resid = 0;
    281 	if (bp->b_bcount == 0)
    282 		goto done;
    283 
    284 	OF_io = bp->b_flags & B_READ ? OF_read :
    285 		(int(*)(int, void*, int))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_resid = bp->b_bcount;
    309 	} else
    310 		bp->b_resid = bp->b_bcount - read;
    311 
    312 	disk_unbusy(&of->sc_dk, bp->b_bcount - bp->b_resid,
    313 	    (bp->b_flags & B_READ));
    314 
    315 done:
    316 	biodone(bp);
    317 }
    318 
    319 static void
    320 ofminphys(struct buf *bp)
    321 {
    322 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
    323 
    324 	if (bp->b_bcount > of->max_transfer)
    325 		bp->b_bcount = of->max_transfer;
    326 }
    327 
    328 int
    329 ofdisk_read(dev_t dev, struct uio *uio, int flags)
    330 {
    331 	return physio(ofdisk_strategy, NULL, dev, B_READ, ofminphys, uio);
    332 }
    333 
    334 int
    335 ofdisk_write(dev_t dev, struct uio *uio, int flags)
    336 {
    337 	return physio(ofdisk_strategy, NULL, dev, B_WRITE, ofminphys, uio);
    338 }
    339 
    340 int
    341 ofdisk_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    342 {
    343 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
    344 	int error;
    345 #ifdef __HAVE_OLD_DISKLABEL
    346 	struct disklabel newlabel;
    347 #endif
    348 
    349 	switch (cmd) {
    350 	case DIOCGDINFO:
    351 		*(struct disklabel *)data = *of->sc_dk.dk_label;
    352 		return 0;
    353 #ifdef __HAVE_OLD_DISKLABEL
    354 	case ODIOCGDINFO:
    355 		newlabel = *of->sc_dk.dk_label;
    356 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    357 			return ENOTTY;
    358 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
    359 		return 0;
    360 #endif
    361 
    362 	case DIOCGPART:
    363 		((struct partinfo *)data)->disklab = of->sc_dk.dk_label;
    364 		((struct partinfo *)data)->part =
    365 			&of->sc_dk.dk_label->d_partitions[DISKPART(dev)];
    366 		return 0;
    367 
    368 	case DIOCWDINFO:
    369 	case DIOCSDINFO:
    370 #ifdef __HAVE_OLD_DISKLABEL
    371 	case ODIOCWDINFO:
    372 	case ODIOCSDINFO:
    373 #endif
    374 	{
    375 		struct disklabel *lp;
    376 
    377 #ifdef __HAVE_OLD_DISKLABEL
    378 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
    379 			memset(&newlabel, 0, sizeof newlabel);
    380 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
    381 			lp = &newlabel;
    382 		} else
    383 #endif
    384 		lp = (struct disklabel *)data;
    385 
    386 		if ((flag & FWRITE) == 0)
    387 			return EBADF;
    388 
    389 		if ((error = lockmgr(&of->sc_dk.dk_openlock, LK_EXCLUSIVE,
    390 				     NULL)) != 0)
    391 			return (error);
    392 
    393 		error = setdisklabel(of->sc_dk.dk_label,
    394 		    lp, /*of->sc_dk.dk_openmask */0,
    395 		    of->sc_dk.dk_cpulabel);
    396 		if (error == 0 && cmd == DIOCWDINFO
    397 #ifdef __HAVE_OLD_DISKLABEL
    398 		    || xfer == ODIOCWDINFO
    399 #endif
    400 		    )
    401 			error = writedisklabel(MAKEDISKDEV(major(dev),
    402 			    DISKUNIT(dev), RAW_PART), ofdisk_strategy,
    403 			    of->sc_dk.dk_label, of->sc_dk.dk_cpulabel);
    404 
    405 		(void) lockmgr(&of->sc_dk.dk_openlock, LK_RELEASE, NULL);
    406 
    407 		return error;
    408 	}
    409 
    410 	case DIOCGDEFLABEL:
    411 		ofdisk_getdefaultlabel(of, (struct disklabel *)data);
    412 		return 0;
    413 #ifdef __HAVE_OLD_DISKLABEL
    414 	case DIOCGDEFLABEL:
    415 		ofdisk_getdefaultlabel(of, &newlabel);
    416 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    417 			return ENOTTY;
    418 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
    419 		return 0;
    420 #endif
    421 
    422 	case DIOCAWEDGE:
    423 	    {
    424 	    	struct dkwedge_info *dkw = (void *) data;
    425 
    426 		if (OFDISK_FLOPPY_P(of))
    427 			return (ENOTTY);
    428 
    429 		if ((flag & FWRITE) == 0)
    430 			return (EBADF);
    431 
    432 		/* If the ioctl happens here, the parent is us. */
    433 		strcpy(dkw->dkw_parent, of->sc_dev.dv_xname);
    434 		return (dkwedge_add(dkw));
    435 	    }
    436 
    437 	case DIOCDWEDGE:
    438 	    {
    439 	    	struct dkwedge_info *dkw = (void *) data;
    440 
    441 		if (OFDISK_FLOPPY_P(of))
    442 			return (ENOTTY);
    443 
    444 		if ((flag & FWRITE) == 0)
    445 			return (EBADF);
    446 
    447 		/* If the ioctl happens here, the parent is us. */
    448 		strcpy(dkw->dkw_parent, of->sc_dev.dv_xname);
    449 		return (dkwedge_del(dkw));
    450 	    }
    451 
    452 	case DIOCLWEDGES:
    453 	    {
    454 	    	struct dkwedge_list *dkwl = (void *) data;
    455 
    456 		if (OFDISK_FLOPPY_P(of))
    457 			return (ENOTTY);
    458 
    459 		return (dkwedge_list(&of->sc_dk, dkwl, l));
    460 	    }
    461 
    462 	default:
    463 		return ENOTTY;
    464 	}
    465 }
    466 
    467 int
    468 ofdisk_dump(dev_t dev, daddr_t blkno, void *va, size_t size)
    469 {
    470 	return EINVAL;
    471 }
    472 
    473 int
    474 ofdisk_size(dev_t dev)
    475 {
    476 	struct ofdisk_softc *of;
    477 	struct disklabel *lp;
    478 	int size, part, omask, unit;
    479 
    480 	unit = DISKUNIT(dev);
    481 	if (unit >= ofdisk_cd.cd_ndevs ||
    482 	    (of = ofdisk_cd.cd_devs[unit]) == NULL)
    483 		return -1;
    484 
    485 	part = DISKPART(dev);
    486 	omask = of->sc_dk.dk_openmask & (1 << part);
    487 	lp = of->sc_dk.dk_label;
    488 
    489 	if (omask == 0 && ofdisk_open(dev, 0, S_IFBLK, curlwp) != 0)
    490 		return -1;
    491 
    492 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
    493 		size = -1;
    494 	else
    495 		size = lp->d_partitions[part].p_size *
    496 		    (lp->d_secsize / DEV_BSIZE);
    497 
    498 	if (omask == 0 && ofdisk_close(dev, 0, S_IFBLK, curlwp) != 0)
    499 		return -1;
    500 
    501 	return size;
    502 }
    503 
    504 void
    505 ofdisk_getdefaultlabel(struct ofdisk_softc *of, struct disklabel *lp)
    506 {
    507 
    508 	memset(lp, 0, sizeof *lp);
    509 
    510 	/*
    511 	 * XXX Firmware bug?  Asking for block size gives a
    512 	 * XXX ridiculous number!  So we use what the boot program
    513 	 * XXX uses.
    514 	 */
    515 	lp->d_secsize = DEV_BSIZE;
    516 
    517 	lp->d_secperunit = OF_call_method_1("#blocks",
    518 	    of->sc_ihandle, 0);
    519 	if (lp->d_secperunit == (u_int32_t)-1)
    520 		lp->d_secperunit = 0x7fffffff;
    521 
    522 	lp->d_secpercyl = 1;
    523 	lp->d_nsectors = 1;
    524 	lp->d_ntracks = 1;
    525 	lp->d_ncylinders = lp->d_secperunit;
    526 
    527 	lp->d_partitions[RAW_PART].p_offset = 0;
    528 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
    529 	lp->d_npartitions = RAW_PART + 1;
    530 
    531 	lp->d_magic = DISKMAGIC;
    532 	lp->d_magic2 = DISKMAGIC;
    533 	lp->d_checksum = dkcksum(lp);
    534 }
    535 
    536 void
    537 ofdisk_getdisklabel(dev)
    538 	dev_t dev;
    539 {
    540 	int unit = DISKUNIT(dev);
    541 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[unit];
    542 	struct disklabel *lp = of->sc_dk.dk_label;
    543 	const char *errmes;
    544 	int l;
    545 
    546 	ofdisk_getdefaultlabel(of, lp);
    547 
    548 	/*
    549 	 * Don't read the disklabel on a floppy; simply
    550 	 * assign all partitions the same size/offset as
    551 	 * RAW_PART.  (This is essentially what the ISA
    552 	 * floppy driver does, but we don't deal with
    553 	 * density stuff.)
    554 	 */
    555 	if (OFDISK_FLOPPY_P(of)) {
    556 		lp->d_npartitions = MAXPARTITIONS;
    557 		for (l = 0; l < lp->d_npartitions; l++) {
    558 			if (l == RAW_PART)
    559 				continue;
    560 			/* struct copy */
    561 			lp->d_partitions[l] =
    562 			    lp->d_partitions[RAW_PART];
    563 		}
    564 		lp->d_checksum = dkcksum(lp);
    565 	} else {
    566 		errmes = readdisklabel(MAKEDISKDEV(major(dev),
    567 		    unit, RAW_PART), ofdisk_strategy, lp,
    568 		    of->sc_dk.dk_cpulabel);
    569 		if (errmes != NULL)
    570 			printf("%s: %s\n", of->sc_dev.dv_xname, errmes);
    571 	}
    572 }
    573