Home | History | Annotate | Line # | Download | only in ofw
ofdisk.c revision 1.20
      1 /*	$NetBSD: ofdisk.c,v 1.20 2002/09/18 01:46:40 chs 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.20 2002/09/18 01:46:40 chs 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 	printf("\n");
    141 
    142 	if (strcmp(child, "floppy") == 0)
    143 		of->sc_flags |= OFDF_ISFLOPPY;
    144 }
    145 
    146 int
    147 ofdisk_open(dev_t dev, int flags, int fmt, struct proc *p)
    148 {
    149 	int unit = DISKUNIT(dev);
    150 	struct ofdisk_softc *of;
    151 	char path[256];
    152 	int l;
    153 
    154 	if (unit >= ofdisk_cd.cd_ndevs)
    155 		return ENXIO;
    156 	if (!(of = ofdisk_cd.cd_devs[unit]))
    157 		return ENXIO;
    158 
    159 	if (!of->sc_ihandle) {
    160 		if ((l = OF_package_to_path(of->sc_phandle, path,
    161 		    sizeof path - 3)) < 0 ||
    162 		    l >= sizeof path - 3)
    163 			return ENXIO;
    164 		path[l] = 0;
    165 
    166 		/*
    167 		 * XXX This is for the benefit of SCSI/IDE disks that don't
    168 		 * XXX have all their childs in the device tree.
    169 		 * XXX YES, I DO THINK THIS IS A BUG IN OPENFIRMWARE!!!
    170 		 * XXX And yes, this is a very gross hack!
    171 		 * XXX See also ofscsi.c
    172 		 */
    173 		if (!strcmp(path + l - 4, "disk")) {
    174 			path[l++] = '@';
    175 			path[l++] = '0' + of->sc_unit;
    176 			path[l] = 0;
    177 		}
    178 
    179 		strcat(path, ":0");
    180 
    181 		if ((of->sc_ihandle = OF_open(path)) == -1)
    182 			return ENXIO;
    183 
    184 		/*
    185 		 * Try to get characteristics of the disk.
    186 		 */
    187 		of->max_transfer = OF_call_method_1("max-transfer",
    188 		    of->sc_ihandle, 0);
    189 		if (of->max_transfer > MAXPHYS)
    190 			of->max_transfer = MAXPHYS;
    191 
    192 		ofdisk_getdisklabel(dev);
    193 	}
    194 
    195 	switch (fmt) {
    196 	case S_IFCHR:
    197 		of->sc_dk.dk_copenmask |= 1 << DISKPART(dev);
    198 		break;
    199 	case S_IFBLK:
    200 		of->sc_dk.dk_bopenmask |= 1 << DISKPART(dev);
    201 		break;
    202 	}
    203 	of->sc_dk.dk_openmask =
    204 	    of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
    205 
    206 	return 0;
    207 }
    208 
    209 int
    210 ofdisk_close(dev_t dev, int flags, int fmt, struct proc *p)
    211 {
    212 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
    213 
    214 	switch (fmt) {
    215 	case S_IFCHR:
    216 		of->sc_dk.dk_copenmask &= ~(1 << DISKPART(dev));
    217 		break;
    218 	case S_IFBLK:
    219 		of->sc_dk.dk_bopenmask &= ~(1 << DISKPART(dev));
    220 		break;
    221 	}
    222 	of->sc_dk.dk_openmask = of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
    223 
    224 #ifdef	FIRMWORKSBUGS
    225 	/*
    226 	 * This is a hack to get the firmware to flush its buffers.
    227 	 */
    228 	OF_seek(of->sc_ihandle, 0);
    229 #endif
    230 	if (!of->sc_dk.dk_openmask) {
    231 		OF_close(of->sc_ihandle);
    232 		of->sc_ihandle = 0;
    233 	}
    234 
    235 	return 0;
    236 }
    237 
    238 void
    239 ofdisk_strategy(struct buf *bp)
    240 {
    241 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
    242 	struct partition *p;
    243 	u_quad_t off;
    244 	int read;
    245 	int (*OF_io)(int, void *, int);
    246 	daddr_t blkno = bp->b_blkno;
    247 
    248 	bp->b_resid = 0;
    249 	if (bp->b_bcount == 0)
    250 		goto done;
    251 
    252 	OF_io = bp->b_flags & B_READ ? OF_read : OF_write;
    253 
    254 	if (DISKPART(bp->b_dev) != RAW_PART) {
    255 		if (bounds_check_with_label(bp, of->sc_dk.dk_label, 0) <= 0) {
    256 			bp->b_resid = bp->b_bcount;
    257 			goto done;
    258 		}
    259 		p = &of->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)];
    260 		blkno = bp->b_blkno + p->p_offset;
    261 	}
    262 
    263 	disk_busy(&of->sc_dk);
    264 
    265 	off = (u_quad_t)blkno * DEV_BSIZE;
    266 	read = -1;
    267 	do {
    268 		if (OF_seek(of->sc_ihandle, off) < 0)
    269 			break;
    270 		read = OF_io(of->sc_ihandle, bp->b_data, bp->b_bcount);
    271 	} while (read == -2);
    272 
    273 	if (read < 0) {
    274 		bp->b_error = EIO;
    275 		bp->b_flags |= B_ERROR;
    276 		bp->b_resid = bp->b_bcount;
    277 	} else
    278 		bp->b_resid = bp->b_bcount - read;
    279 
    280 	disk_unbusy(&of->sc_dk, bp->b_bcount - bp->b_resid);
    281 
    282 done:
    283 	biodone(bp);
    284 }
    285 
    286 static void
    287 ofminphys(struct buf *bp)
    288 {
    289 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
    290 
    291 	if (bp->b_bcount > of->max_transfer)
    292 		bp->b_bcount = of->max_transfer;
    293 }
    294 
    295 int
    296 ofdisk_read(dev_t dev, struct uio *uio, int flags)
    297 {
    298 	return physio(ofdisk_strategy, NULL, dev, B_READ, ofminphys, uio);
    299 }
    300 
    301 int
    302 ofdisk_write(dev_t dev, struct uio *uio, int flags)
    303 {
    304 	return physio(ofdisk_strategy, NULL, dev, B_WRITE, ofminphys, uio);
    305 }
    306 
    307 int
    308 ofdisk_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    309 {
    310 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
    311 	int error;
    312 #ifdef __HAVE_OLD_DISKLABEL
    313 	struct disklabel newlabel;
    314 #endif
    315 
    316 	switch (cmd) {
    317 	case DIOCGDINFO:
    318 		*(struct disklabel *)data = *of->sc_dk.dk_label;
    319 		return 0;
    320 #ifdef __HAVE_OLD_DISKLABEL
    321 	case ODIOCGDINFO:
    322 		newlabel = *of->sc_dk.dk_label;
    323 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    324 			return ENOTTY;
    325 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
    326 		return 0;
    327 #endif
    328 
    329 	case DIOCGPART:
    330 		((struct partinfo *)data)->disklab = of->sc_dk.dk_label;
    331 		((struct partinfo *)data)->part =
    332 			&of->sc_dk.dk_label->d_partitions[DISKPART(dev)];
    333 		return 0;
    334 
    335 	case DIOCWDINFO:
    336 	case DIOCSDINFO:
    337 #ifdef __HAVE_OLD_DISKLABEL
    338 	case ODIOCWDINFO:
    339 	case ODIOCSDINFO:
    340 #endif
    341 	{
    342 		struct disklabel *lp;
    343 
    344 #ifdef __HAVE_OLD_DISKLABEL
    345 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
    346 			memset(&newlabel, 0, sizeof newlabel);
    347 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
    348 			lp = &newlabel;
    349 		} else
    350 #endif
    351 		lp = (struct disklabel *)data;
    352 
    353 		if ((flag & FWRITE) == 0)
    354 			return EBADF;
    355 
    356 		error = setdisklabel(of->sc_dk.dk_label,
    357 		    lp, /*of->sc_dk.dk_openmask */0,
    358 		    of->sc_dk.dk_cpulabel);
    359 		if (error == 0 && cmd == DIOCWDINFO
    360 #ifdef __HAVE_OLD_DISKLABEL
    361 		    || xfer == ODIOCWDINFO
    362 #endif
    363 		    )
    364 			error = writedisklabel(MAKEDISKDEV(major(dev),
    365 			    DISKUNIT(dev), RAW_PART), ofdisk_strategy,
    366 			    of->sc_dk.dk_label, of->sc_dk.dk_cpulabel);
    367 
    368 		return error;
    369 	}
    370 
    371 	case DIOCGDEFLABEL:
    372 		ofdisk_getdefaultlabel(of, (struct disklabel *)data);
    373 		return 0;
    374 #ifdef __HAVE_OLD_DISKLABEL
    375 	case DIOCGDEFLABEL:
    376 		ofdisk_getdefaultlabel(of, &newlabel);
    377 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    378 			return ENOTTY;
    379 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
    380 		return 0;
    381 #endif
    382 
    383 	default:
    384 		return ENOTTY;
    385 	}
    386 }
    387 
    388 int
    389 ofdisk_dump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
    390 {
    391 	return EINVAL;
    392 }
    393 
    394 int
    395 ofdisk_size(dev_t dev)
    396 {
    397 	struct ofdisk_softc *of;
    398 	struct disklabel *lp;
    399 	int size, part, omask, unit;
    400 
    401 	unit = DISKUNIT(dev);
    402 	if (unit >= ofdisk_cd.cd_ndevs ||
    403 	    (of = ofdisk_cd.cd_devs[unit]) == NULL)
    404 		return -1;
    405 
    406 	part = DISKPART(dev);
    407 	omask = of->sc_dk.dk_openmask & (1 << part);
    408 	lp = of->sc_dk.dk_label;
    409 
    410 	if (omask == 0 && ofdisk_open(dev, 0, S_IFBLK, curproc) != 0)
    411 		return -1;
    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 && ofdisk_close(dev, 0, S_IFBLK, curproc) != 0)
    420 		return -1;
    421 
    422 	return size;
    423 }
    424 
    425 void
    426 ofdisk_getdefaultlabel(struct ofdisk_softc *of, struct disklabel *lp)
    427 {
    428 
    429 	memset(lp, 0, sizeof *lp);
    430 
    431 	/*
    432 	 * XXX Firmware bug?  Asking for block size gives a
    433 	 * XXX ridiculous number!  So we use what the boot program
    434 	 * XXX uses.
    435 	 */
    436 	lp->d_secsize = DEV_BSIZE;
    437 
    438 	lp->d_secperunit = OF_call_method_1("#blocks",
    439 	    of->sc_ihandle, 0);
    440 	if (lp->d_secperunit == (u_int32_t)-1)
    441 		lp->d_secperunit = 0x7fffffff;
    442 
    443 	lp->d_secpercyl = 1;
    444 	lp->d_nsectors = 1;
    445 	lp->d_ntracks = 1;
    446 	lp->d_ncylinders = lp->d_secperunit;
    447 
    448 	lp->d_partitions[RAW_PART].p_offset = 0;
    449 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
    450 	lp->d_npartitions = RAW_PART + 1;
    451 
    452 	lp->d_magic = DISKMAGIC;
    453 	lp->d_magic2 = DISKMAGIC;
    454 	lp->d_checksum = dkcksum(lp);
    455 }
    456 
    457 void
    458 ofdisk_getdisklabel(dev)
    459 	dev_t dev;
    460 {
    461 	int unit = DISKUNIT(dev);
    462 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[unit];
    463 	struct disklabel *lp = of->sc_dk.dk_label;
    464 	char *errmes;
    465 	int l;
    466 
    467 	ofdisk_getdefaultlabel(of, lp);
    468 
    469 	/*
    470 	 * Don't read the disklabel on a floppy; simply
    471 	 * assign all partitions the same size/offset as
    472 	 * RAW_PART.  (This is essentially what the ISA
    473 	 * floppy driver does, but we don't deal with
    474 	 * density stuff.)
    475 	 */
    476 	if (of->sc_flags & OFDF_ISFLOPPY) {
    477 		lp->d_npartitions = MAXPARTITIONS;
    478 		for (l = 0; l < lp->d_npartitions; l++) {
    479 			if (l == RAW_PART)
    480 				continue;
    481 			/* struct copy */
    482 			lp->d_partitions[l] =
    483 			    lp->d_partitions[RAW_PART];
    484 		}
    485 		lp->d_checksum = dkcksum(lp);
    486 	} else {
    487 		errmes = readdisklabel(MAKEDISKDEV(major(dev),
    488 		    unit, RAW_PART), ofdisk_strategy, lp,
    489 		    of->sc_dk.dk_cpulabel);
    490 		if (errmes != NULL)
    491 			printf("%s: %s\n", of->sc_dev.dv_xname, errmes);
    492 	}
    493 }
    494