Home | History | Annotate | Line # | Download | only in ofw
ofdisk.c revision 1.11
      1 /*	$NetBSD: ofdisk.c,v 1.11 1998/02/24 07:10:39 mycroft 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/disklabel.h>
     38 #include <sys/disk.h>
     39 #include <sys/fcntl.h>
     40 #include <sys/ioctl.h>
     41 #include <sys/stat.h>
     42 #include <sys/systm.h>
     43 #include <sys/proc.h>
     44 
     45 #include <dev/ofw/openfirm.h>
     46 
     47 struct ofdisk_softc {
     48 	struct device sc_dev;
     49 	int sc_phandle;
     50 	int sc_unit;
     51 	int sc_flags;
     52 	struct disk sc_dk;
     53 	int sc_ihandle;
     54 	u_long max_transfer;
     55 	char sc_name[16];
     56 };
     57 
     58 /* sc_flags */
     59 #define OFDF_ISFLOPPY	0x01		/* we are a floppy drive */
     60 
     61 static int ofdisk_match __P((struct device *, struct cfdata *, void *));
     62 static void ofdisk_attach __P((struct device *, struct device *, void *));
     63 
     64 struct cfattach ofdisk_ca = {
     65 	sizeof(struct ofdisk_softc), ofdisk_match, ofdisk_attach
     66 };
     67 
     68 extern struct cfdriver ofdisk_cd;
     69 
     70 void ofdisk_strategy __P((struct buf *));
     71 
     72 struct dkdriver ofdisk_dkdriver = { ofdisk_strategy };
     73 
     74 void ofdisk_getdefaultlabel __P((struct ofdisk_softc *, struct disklabel *));
     75 void ofdisk_getdisklabel __P((dev_t));
     76 
     77 static int
     78 ofdisk_match(parent, match, aux)
     79 	struct device *parent;
     80 	struct cfdata *match;
     81 	void *aux;
     82 {
     83 	struct ofbus_attach_args *oba = aux;
     84 	char type[8];
     85 	int l;
     86 
     87 	if (strcmp(oba->oba_busname, "ofw"))
     88 		return (0);
     89 	if ((l = OF_getprop(oba->oba_phandle, "device_type", type,
     90 	    sizeof type - 1)) < 0)
     91 		return 0;
     92 	if (l >= sizeof type)
     93 		return 0;
     94 	type[l] = 0;
     95 	return !strcmp(type, "block");
     96 }
     97 
     98 static void
     99 ofdisk_attach(parent, self, aux)
    100 	struct device *parent, *self;
    101 	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 	dk_establish(&of->sc_dk, self);				/* XXX */
    124 	printf("\n");
    125 
    126 	if (strcmp(child, "floppy") == 0)
    127 		of->sc_flags |= OFDF_ISFLOPPY;
    128 }
    129 
    130 int
    131 ofdisk_open(dev, flags, fmt, p)
    132 	dev_t dev;
    133 	int flags;
    134 	int fmt;
    135 	struct proc *p;
    136 {
    137 	int unit = DISKUNIT(dev);
    138 	struct ofdisk_softc *of;
    139 	char path[256];
    140 	struct disklabel *lp;
    141 	int l;
    142 
    143 	if (unit >= ofdisk_cd.cd_ndevs)
    144 		return ENXIO;
    145 	if (!(of = ofdisk_cd.cd_devs[unit]))
    146 		return ENXIO;
    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(dev);
    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(dev, flags, fmt, p)
    200 	dev_t dev;
    201 	int flags;
    202 	int fmt;
    203 	struct proc *p;
    204 {
    205 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
    206 
    207 	switch (fmt) {
    208 	case S_IFCHR:
    209 		of->sc_dk.dk_copenmask &= ~(1 << DISKPART(dev));
    210 		break;
    211 	case S_IFBLK:
    212 		of->sc_dk.dk_bopenmask &= ~(1 << DISKPART(dev));
    213 		break;
    214 	}
    215 	of->sc_dk.dk_openmask = of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
    216 
    217 #ifdef	FIRMWORKSBUGS
    218 	/*
    219 	 * This is a hack to get the firmware to flush its buffers.
    220 	 */
    221 	OF_seek(of->sc_ihandle, 0);
    222 #endif
    223 	if (!of->sc_dk.dk_openmask) {
    224 		OF_close(of->sc_ihandle);
    225 		of->sc_ihandle = 0;
    226 	}
    227 
    228 	return 0;
    229 }
    230 
    231 void
    232 ofdisk_strategy(bp)
    233 	struct buf *bp;
    234 {
    235 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
    236 	struct partition *p;
    237 	u_quad_t off;
    238 	int read;
    239 	int (*OF_io)(int, void *, int);
    240 	daddr_t blkno = bp->b_blkno;
    241 
    242 	bp->b_resid = 0;
    243 	if (bp->b_bcount == 0)
    244 		goto done;
    245 
    246 	OF_io = bp->b_flags & B_READ ? OF_read : OF_write;
    247 
    248 	if (DISKPART(bp->b_dev) != RAW_PART) {
    249 		if (bounds_check_with_label(bp, of->sc_dk.dk_label, 0) <= 0) {
    250 			bp->b_resid = bp->b_bcount;
    251 			goto done;
    252 		}
    253 		p = &of->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)];
    254 		blkno = bp->b_blkno + p->p_offset;
    255 	}
    256 
    257 	disk_busy(&of->sc_dk);
    258 
    259 	off = (u_quad_t)blkno * DEV_BSIZE;
    260 	read = -1;
    261 	do {
    262 		if (OF_seek(of->sc_ihandle, off) < 0)
    263 			break;
    264 		read = OF_io(of->sc_ihandle, bp->b_data, bp->b_bcount);
    265 	} while (read == -2);
    266 
    267 	if (read < 0) {
    268 		bp->b_error = EIO;
    269 		bp->b_flags |= B_ERROR;
    270 		bp->b_resid = bp->b_bcount;
    271 	} else
    272 		bp->b_resid = bp->b_bcount - read;
    273 
    274 	disk_unbusy(&of->sc_dk, bp->b_bcount - bp->b_resid);
    275 
    276 done:
    277 	biodone(bp);
    278 }
    279 
    280 static void
    281 ofminphys(bp)
    282 	struct buf *bp;
    283 {
    284 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
    285 
    286 	if (bp->b_bcount > of->max_transfer)
    287 		bp->b_bcount = of->max_transfer;
    288 }
    289 
    290 int
    291 ofdisk_read(dev, uio)
    292 	dev_t dev;
    293 	struct uio *uio;
    294 {
    295 	return physio(ofdisk_strategy, NULL, dev, B_READ, ofminphys, uio);
    296 }
    297 
    298 int
    299 ofdisk_write(dev, uio)
    300 	dev_t dev;
    301 	struct uio *uio;
    302 {
    303 	return physio(ofdisk_strategy, NULL, dev, B_WRITE, ofminphys, uio);
    304 }
    305 
    306 int
    307 ofdisk_ioctl(dev, cmd, data, flag, p)
    308 	dev_t dev;
    309 	u_long cmd;
    310 	caddr_t data;
    311 	int flag;
    312 	struct proc *p;
    313 {
    314 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
    315 	int error;
    316 
    317 	switch (cmd) {
    318 	case DIOCGDINFO:
    319 		*(struct disklabel *)data = *of->sc_dk.dk_label;
    320 		return 0;
    321 
    322 	case DIOCGPART:
    323 		((struct partinfo *)data)->disklab = of->sc_dk.dk_label;
    324 		((struct partinfo *)data)->part =
    325 			&of->sc_dk.dk_label->d_partitions[DISKPART(dev)];
    326 		return 0;
    327 
    328 	case DIOCWDINFO:
    329 	case DIOCSDINFO:
    330 		if ((flag & FWRITE) == 0)
    331 			return EBADF;
    332 
    333 		error = setdisklabel(of->sc_dk.dk_label,
    334 		    (struct disklabel *)data, /*of->sc_dk.dk_openmask */0,
    335 		    of->sc_dk.dk_cpulabel);
    336 		if (error == 0 && cmd == DIOCWDINFO)
    337 			error = writedisklabel(MAKEDISKDEV(major(dev),
    338 			    DISKUNIT(dev), RAW_PART), ofdisk_strategy,
    339 			    of->sc_dk.dk_label, of->sc_dk.dk_cpulabel);
    340 
    341 		return error;
    342 
    343 	case DIOCGDEFLABEL:
    344 		ofdisk_getdefaultlabel(of, (struct disklabel *)data);
    345 		return 0;
    346 
    347 	default:
    348 		return ENOTTY;
    349 	}
    350 }
    351 
    352 int
    353 ofdisk_dump(dev, blkno, va, size)
    354 	dev_t dev;
    355 	daddr_t blkno;
    356 	caddr_t va;
    357 	size_t size;
    358 {
    359 	return EINVAL;
    360 }
    361 
    362 int
    363 ofdisk_size(dev)
    364 	dev_t dev;
    365 {
    366 	struct ofdisk_softc *of;
    367 	struct disklabel *lp;
    368 	int size, part, omask, unit;
    369 
    370 	unit = DISKUNIT(dev);
    371 	if (unit >= ofdisk_cd.cd_ndevs ||
    372 	    (of = ofdisk_cd.cd_devs[unit]) == NULL)
    373 		return -1;
    374 
    375 	part = DISKPART(dev);
    376 	omask = of->sc_dk.dk_openmask & (1 << part);
    377 	lp = of->sc_dk.dk_label;
    378 
    379 	if (omask == 0 && ofdisk_open(dev, 0, S_IFBLK, curproc) != 0)
    380 		return -1;
    381 
    382 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
    383 		size = -1;
    384 	else
    385 		size = lp->d_partitions[part].p_size *
    386 		    (lp->d_secsize / DEV_BSIZE);
    387 
    388 	if (omask == 0 && ofdisk_close(dev, 0, S_IFBLK, curproc) != 0)
    389 		return -1;
    390 
    391 	return size;
    392 }
    393 
    394 void
    395 ofdisk_getdefaultlabel(of, lp)
    396 	struct ofdisk_softc *of;
    397 	struct disklabel *lp;
    398 {
    399 
    400 	bzero(lp, sizeof *lp);
    401 
    402 	/*
    403 	 * XXX Firmware bug?  Asking for block size gives a
    404 	 * XXX rediculous number!  So we use what the boot program
    405 	 * XXX uses.
    406 	 */
    407 	lp->d_secsize = DEV_BSIZE;
    408 
    409 	lp->d_secperunit = OF_call_method_1("#blocks",
    410 	    of->sc_ihandle, 0);
    411 	if (lp->d_secperunit == (u_int32_t)-1)
    412 		lp->d_secperunit = 0x7fffffff;
    413 
    414 	lp->d_secpercyl = 1;
    415 	lp->d_nsectors = 1;
    416 	lp->d_ntracks = 1;
    417 	lp->d_ncylinders = lp->d_secperunit;
    418 
    419 	lp->d_partitions[RAW_PART].p_offset = 0;
    420 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
    421 	lp->d_npartitions = RAW_PART + 1;
    422 
    423 	lp->d_magic = DISKMAGIC;
    424 	lp->d_magic2 = DISKMAGIC;
    425 	lp->d_checksum = dkcksum(lp);
    426 }
    427 
    428 void
    429 ofdisk_getdisklabel(dev)
    430 	dev_t dev;
    431 {
    432 	int unit = DISKUNIT(dev);
    433 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[unit];
    434 	struct disklabel *lp = of->sc_dk.dk_label;
    435 	char *errmes;
    436 	int l;
    437 
    438 	ofdisk_getdefaultlabel(of, lp);
    439 
    440 	/*
    441 	 * Don't read the disklabel on a floppy; simply
    442 	 * assign all partitions the same size/offset as
    443 	 * RAW_PART.  (This is essentially what the ISA
    444 	 * floppy driver does, but we don't deal with
    445 	 * density stuff.)
    446 	 */
    447 	if (of->sc_flags & OFDF_ISFLOPPY) {
    448 		lp->d_npartitions = MAXPARTITIONS;
    449 		for (l = 0; l < lp->d_npartitions; l++) {
    450 			if (l == RAW_PART)
    451 				continue;
    452 			/* struct copy */
    453 			lp->d_partitions[l] =
    454 			    lp->d_partitions[RAW_PART];
    455 		}
    456 		lp->d_checksum = dkcksum(lp);
    457 	} else {
    458 		errmes = readdisklabel(MAKEDISKDEV(major(dev),
    459 		    unit, RAW_PART), ofdisk_strategy, lp,
    460 		    of->sc_dk.dk_cpulabel);
    461 		if (errmes != NULL)
    462 			printf("%s: %s\n", errmes);
    463 	}
    464 }
    465