Home | History | Annotate | Line # | Download | only in ofw
ofdisk.c revision 1.35
      1 /*	$NetBSD: ofdisk.c,v 1.35 2007/03/04 06:02:15 christos 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 2007/03/04 06:02:15 christos 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_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, void *data, int flag, struct lwp *l)
    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, l));
    461 	    }
    462 
    463 	default:
    464 		return ENOTTY;
    465 	}
    466 }
    467 
    468 int
    469 ofdisk_dump(dev_t dev, daddr_t blkno, void *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, curlwp) != 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, curlwp) != 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