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