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