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