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