Home | History | Annotate | Line # | Download | only in ofw
ofdisk.c revision 1.36
      1 /*	$NetBSD: ofdisk.c,v 1.36 2007/07/21 19:51:48 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.36 2007/07/21 19:51:48 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_flags |= B_ERROR;
    305 		bp->b_resid = bp->b_bcount;
    306 	} else
    307 		bp->b_resid = bp->b_bcount - read;
    308 
    309 	disk_unbusy(&of->sc_dk, bp->b_bcount - bp->b_resid,
    310 	    (bp->b_flags & B_READ));
    311 
    312 done:
    313 	biodone(bp);
    314 }
    315 
    316 static void
    317 ofminphys(struct buf *bp)
    318 {
    319 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
    320 
    321 	if (bp->b_bcount > of->max_transfer)
    322 		bp->b_bcount = of->max_transfer;
    323 }
    324 
    325 int
    326 ofdisk_read(dev_t dev, struct uio *uio, int flags)
    327 {
    328 	return physio(ofdisk_strategy, NULL, dev, B_READ, ofminphys, uio);
    329 }
    330 
    331 int
    332 ofdisk_write(dev_t dev, struct uio *uio, int flags)
    333 {
    334 	return physio(ofdisk_strategy, NULL, dev, B_WRITE, ofminphys, uio);
    335 }
    336 
    337 int
    338 ofdisk_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    339 {
    340 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
    341 	int error;
    342 #ifdef __HAVE_OLD_DISKLABEL
    343 	struct disklabel newlabel;
    344 #endif
    345 
    346 	switch (cmd) {
    347 	case DIOCGDINFO:
    348 		*(struct disklabel *)data = *of->sc_dk.dk_label;
    349 		return 0;
    350 #ifdef __HAVE_OLD_DISKLABEL
    351 	case ODIOCGDINFO:
    352 		newlabel = *of->sc_dk.dk_label;
    353 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    354 			return ENOTTY;
    355 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
    356 		return 0;
    357 #endif
    358 
    359 	case DIOCGPART:
    360 		((struct partinfo *)data)->disklab = of->sc_dk.dk_label;
    361 		((struct partinfo *)data)->part =
    362 			&of->sc_dk.dk_label->d_partitions[DISKPART(dev)];
    363 		return 0;
    364 
    365 	case DIOCWDINFO:
    366 	case DIOCSDINFO:
    367 #ifdef __HAVE_OLD_DISKLABEL
    368 	case ODIOCWDINFO:
    369 	case ODIOCSDINFO:
    370 #endif
    371 	{
    372 		struct disklabel *lp;
    373 
    374 #ifdef __HAVE_OLD_DISKLABEL
    375 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
    376 			memset(&newlabel, 0, sizeof newlabel);
    377 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
    378 			lp = &newlabel;
    379 		} else
    380 #endif
    381 		lp = (struct disklabel *)data;
    382 
    383 		if ((flag & FWRITE) == 0)
    384 			return EBADF;
    385 
    386 		mutex_enter(&of->sc_dk.dk_openlock);
    387 
    388 		error = setdisklabel(of->sc_dk.dk_label,
    389 		    lp, /*of->sc_dk.dk_openmask */0,
    390 		    of->sc_dk.dk_cpulabel);
    391 		if (error == 0 && cmd == DIOCWDINFO
    392 #ifdef __HAVE_OLD_DISKLABEL
    393 		    || xfer == ODIOCWDINFO
    394 #endif
    395 		    )
    396 			error = writedisklabel(MAKEDISKDEV(major(dev),
    397 			    DISKUNIT(dev), RAW_PART), ofdisk_strategy,
    398 			    of->sc_dk.dk_label, of->sc_dk.dk_cpulabel);
    399 
    400 		mutex_exit(&of->sc_dk.dk_openlock);
    401 
    402 		return error;
    403 	}
    404 
    405 	case DIOCGDEFLABEL:
    406 		ofdisk_getdefaultlabel(of, (struct disklabel *)data);
    407 		return 0;
    408 #ifdef __HAVE_OLD_DISKLABEL
    409 	case DIOCGDEFLABEL:
    410 		ofdisk_getdefaultlabel(of, &newlabel);
    411 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    412 			return ENOTTY;
    413 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
    414 		return 0;
    415 #endif
    416 
    417 	case DIOCAWEDGE:
    418 	    {
    419 	    	struct dkwedge_info *dkw = (void *) data;
    420 
    421 		if (OFDISK_FLOPPY_P(of))
    422 			return (ENOTTY);
    423 
    424 		if ((flag & FWRITE) == 0)
    425 			return (EBADF);
    426 
    427 		/* If the ioctl happens here, the parent is us. */
    428 		strcpy(dkw->dkw_parent, of->sc_dev.dv_xname);
    429 		return (dkwedge_add(dkw));
    430 	    }
    431 
    432 	case DIOCDWEDGE:
    433 	    {
    434 	    	struct dkwedge_info *dkw = (void *) data;
    435 
    436 		if (OFDISK_FLOPPY_P(of))
    437 			return (ENOTTY);
    438 
    439 		if ((flag & FWRITE) == 0)
    440 			return (EBADF);
    441 
    442 		/* If the ioctl happens here, the parent is us. */
    443 		strcpy(dkw->dkw_parent, of->sc_dev.dv_xname);
    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", of->sc_dev.dv_xname, errmes);
    566 	}
    567 }
    568