Home | History | Annotate | Line # | Download | only in ofw
ofdisk.c revision 1.1
      1  1.1  ws /*	$NetBSD: ofdisk.c,v 1.1 1996/09/30 16:35:08 ws Exp $	*/
      2  1.1  ws 
      3  1.1  ws /*
      4  1.1  ws  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
      5  1.1  ws  * Copyright (C) 1995, 1996 TooLs GmbH.
      6  1.1  ws  * All rights reserved.
      7  1.1  ws  *
      8  1.1  ws  * Redistribution and use in source and binary forms, with or without
      9  1.1  ws  * modification, are permitted provided that the following conditions
     10  1.1  ws  * are met:
     11  1.1  ws  * 1. Redistributions of source code must retain the above copyright
     12  1.1  ws  *    notice, this list of conditions and the following disclaimer.
     13  1.1  ws  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  ws  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  ws  *    documentation and/or other materials provided with the distribution.
     16  1.1  ws  * 3. All advertising materials mentioning features or use of this software
     17  1.1  ws  *    must display the following acknowledgement:
     18  1.1  ws  *	This product includes software developed by TooLs GmbH.
     19  1.1  ws  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     20  1.1  ws  *    derived from this software without specific prior written permission.
     21  1.1  ws  *
     22  1.1  ws  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     23  1.1  ws  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.1  ws  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.1  ws  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26  1.1  ws  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     27  1.1  ws  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     28  1.1  ws  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  1.1  ws  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     30  1.1  ws  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     31  1.1  ws  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  1.1  ws  */
     33  1.1  ws 
     34  1.1  ws #include <sys/param.h>
     35  1.1  ws #include <sys/buf.h>
     36  1.1  ws #include <sys/device.h>
     37  1.1  ws #include <sys/disklabel.h>
     38  1.1  ws #include <sys/disk.h>
     39  1.1  ws #include <sys/fcntl.h>
     40  1.1  ws #include <sys/ioctl.h>
     41  1.1  ws #include <sys/stat.h>
     42  1.1  ws #include <sys/systm.h>
     43  1.1  ws 
     44  1.1  ws #include <dev/ofw/openfirm.h>
     45  1.1  ws 
     46  1.1  ws struct ofd_softc {
     47  1.1  ws 	struct device sc_dev;
     48  1.1  ws 	int sc_phandle;
     49  1.1  ws 	int sc_unit;
     50  1.1  ws 	struct disk sc_dk;
     51  1.1  ws 	int sc_ihandle;
     52  1.1  ws 	u_long max_transfer;
     53  1.1  ws 	char sc_name[16];
     54  1.1  ws };
     55  1.1  ws 
     56  1.1  ws static int ofdprobe __P((struct device *, void *, void *));
     57  1.1  ws static void ofdattach __P((struct device *, struct device *, void *));
     58  1.1  ws 
     59  1.1  ws struct cfattach ofdisk_ca = {
     60  1.1  ws 	sizeof(struct ofd_softc), ofdprobe, ofdattach
     61  1.1  ws };
     62  1.1  ws 
     63  1.1  ws struct cfdriver ofdisk_cd = {
     64  1.1  ws 	NULL, "ofdisk", DV_DISK
     65  1.1  ws };
     66  1.1  ws 
     67  1.1  ws void ofdstrategy __P((struct buf *));
     68  1.1  ws 
     69  1.1  ws struct dkdriver ofdkdriver = { ofdstrategy };
     70  1.1  ws 
     71  1.1  ws static int
     72  1.1  ws ofdprobe(parent, match, aux)
     73  1.1  ws 	struct device *parent;
     74  1.1  ws 	void *match, *aux;
     75  1.1  ws {
     76  1.1  ws 	struct ofprobe *ofp = aux;
     77  1.1  ws 	char type[8];
     78  1.1  ws 	int l;
     79  1.1  ws 
     80  1.1  ws 	if ((l = OF_getprop(ofp->phandle, "device_type", type, sizeof type - 1)) < 0)
     81  1.1  ws 		return 0;
     82  1.1  ws 	if (l >= sizeof type)
     83  1.1  ws 		return 0;
     84  1.1  ws 	type[l] = 0;
     85  1.1  ws 	return !strcmp(type, "block");
     86  1.1  ws }
     87  1.1  ws 
     88  1.1  ws static void
     89  1.1  ws ofdattach(parent, self, aux)
     90  1.1  ws 	struct device *parent, *self;
     91  1.1  ws 	void *aux;
     92  1.1  ws {
     93  1.1  ws 	struct ofd_softc *of = (void *)self;
     94  1.1  ws 	struct ofprobe *ofp = aux;
     95  1.1  ws 	int l;
     96  1.1  ws 
     97  1.1  ws 	of->sc_phandle = ofp->phandle;
     98  1.1  ws 	of->sc_unit = ofp->unit;
     99  1.1  ws 	of->sc_ihandle = 0;
    100  1.1  ws 	of->sc_dk.dk_driver = &ofdkdriver;
    101  1.1  ws 	of->sc_dk.dk_name = of->sc_name;
    102  1.1  ws 	strcpy(of->sc_name, of->sc_dev.dv_xname);
    103  1.1  ws 	disk_attach(&of->sc_dk);
    104  1.1  ws 	dk_establish(&of->sc_dk, self);				/* XXX */
    105  1.1  ws 	printf("\n");
    106  1.1  ws }
    107  1.1  ws 
    108  1.1  ws int
    109  1.1  ws ofdopen(dev, flags, fmt, p)
    110  1.1  ws 	dev_t dev;
    111  1.1  ws 	int flags;
    112  1.1  ws 	int fmt;
    113  1.1  ws 	struct proc *p;
    114  1.1  ws {
    115  1.1  ws 	int unit = DISKUNIT(dev);
    116  1.1  ws 	struct ofd_softc *of;
    117  1.1  ws 	char path[256];
    118  1.1  ws 	struct disklabel *lp;
    119  1.1  ws 	int l;
    120  1.1  ws 
    121  1.1  ws 	if (unit >= ofdisk_cd.cd_ndevs)
    122  1.1  ws 		return ENXIO;
    123  1.1  ws 	if (!(of = ofdisk_cd.cd_devs[unit]))
    124  1.1  ws 		return ENXIO;
    125  1.1  ws 
    126  1.1  ws 	if (!of->sc_ihandle) {
    127  1.1  ws 
    128  1.1  ws 		if ((l = OF_package_to_path(of->sc_phandle, path, sizeof path - 3)) < 0)
    129  1.1  ws 			return ENXIO;
    130  1.1  ws 		if (l >= sizeof path - 3)
    131  1.1  ws 			return ENXIO;
    132  1.1  ws 		path[l] = 0;
    133  1.1  ws 
    134  1.1  ws 		/*
    135  1.1  ws 		 * This is for the benefit of SCSI/IDE disks that don't
    136  1.1  ws 		 * have all their childs in the device tree.
    137  1.1  ws 		 * YES, I DO THINK THIS IS A BUG IN OPENFIRMWARE!!!
    138  1.1  ws 		 * And yes, this is a very gross hack!					XXX
    139  1.1  ws 		 * See also ofscsi.c
    140  1.1  ws 		 */
    141  1.1  ws 		if (!strcmp(path + l - 4, "disk")) {
    142  1.1  ws 			path[l++] = '@';
    143  1.1  ws 			path[l++] = '0' + of->sc_unit;
    144  1.1  ws 			path[l] = 0;
    145  1.1  ws 		}
    146  1.1  ws 
    147  1.1  ws 		strcat(path, ":0");
    148  1.1  ws 
    149  1.1  ws 		if (!(of->sc_ihandle = OF_open(path)))
    150  1.1  ws 			return ENXIO;
    151  1.1  ws 
    152  1.1  ws 		/*
    153  1.1  ws 		 * Try to get characteristics of the disk.
    154  1.1  ws 		 */
    155  1.1  ws 		of->max_transfer = OF_call_method_1("max-transfer", of->sc_ihandle, 0);
    156  1.1  ws 		if (of->max_transfer > MAXPHYS)
    157  1.1  ws 			of->max_transfer = MAXPHYS;
    158  1.1  ws 
    159  1.1  ws 		lp = of->sc_dk.dk_label;
    160  1.1  ws 		bzero(lp, sizeof *lp);
    161  1.1  ws 
    162  1.1  ws 		lp->d_secsize = OF_call_method_1("block-size", of->sc_ihandle, 0);
    163  1.1  ws 		if (lp->d_secsize == (u_int32_t)-1 || lp->d_secsize > MAXBSIZE)
    164  1.1  ws 			lp->d_secsize = DEV_BSIZE;
    165  1.1  ws 
    166  1.1  ws 		lp->d_secperunit = OF_call_method_1("#blocks", of->sc_ihandle, 0);
    167  1.1  ws 		if (lp->d_secperunit == (u_int32_t)-1)
    168  1.1  ws 			lp->d_secperunit = 0x7fffffff;
    169  1.1  ws 
    170  1.1  ws 		lp->d_secpercyl = 1;
    171  1.1  ws 		lp->d_nsectors = 1;
    172  1.1  ws 		lp->d_ntracks = 1;
    173  1.1  ws 		lp->d_ncylinders = lp->d_secperunit;
    174  1.1  ws 
    175  1.1  ws 		lp->d_partitions[RAW_PART].p_offset = 0;
    176  1.1  ws 		lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
    177  1.1  ws 
    178  1.1  ws 		readdisklabel(MAKEDISKDEV(major(dev), unit, RAW_PART), ofdstrategy,
    179  1.1  ws 			      lp, of->sc_dk.dk_cpulabel);
    180  1.1  ws 	}
    181  1.1  ws 
    182  1.1  ws 	switch (fmt) {
    183  1.1  ws 	case S_IFCHR:
    184  1.1  ws 		of->sc_dk.dk_copenmask |= 1 << DISKPART(dev);
    185  1.1  ws 		break;
    186  1.1  ws 	case S_IFBLK:
    187  1.1  ws 		of->sc_dk.dk_bopenmask |= 1 << DISKPART(dev);
    188  1.1  ws 		break;
    189  1.1  ws 	}
    190  1.1  ws 	of->sc_dk.dk_openmask = of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
    191  1.1  ws 
    192  1.1  ws 	return 0;
    193  1.1  ws }
    194  1.1  ws 
    195  1.1  ws int
    196  1.1  ws ofdclose(dev, flags, fmt, p)
    197  1.1  ws 	dev_t dev;
    198  1.1  ws 	int flags;
    199  1.1  ws 	int fmt;
    200  1.1  ws 	struct proc *p;
    201  1.1  ws {
    202  1.1  ws 	struct ofd_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
    203  1.1  ws 
    204  1.1  ws 	switch (fmt) {
    205  1.1  ws 	case S_IFCHR:
    206  1.1  ws 		of->sc_dk.dk_copenmask &= ~(1 << DISKPART(dev));
    207  1.1  ws 		break;
    208  1.1  ws 	case S_IFBLK:
    209  1.1  ws 		of->sc_dk.dk_bopenmask &= ~(1 << DISKPART(dev));
    210  1.1  ws 		break;
    211  1.1  ws 	}
    212  1.1  ws 	of->sc_dk.dk_openmask = of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
    213  1.1  ws 
    214  1.1  ws #ifdef	FIREPOWERBUGS
    215  1.1  ws 	/*
    216  1.1  ws 	 * This is a hack to get the firmware to flush its buffers.
    217  1.1  ws 	 */
    218  1.1  ws 	OF_seek(of->sc_ihandle, 0);
    219  1.1  ws #endif
    220  1.1  ws 	if (!of->sc_dk.dk_openmask) {
    221  1.1  ws 		OF_close(of->sc_ihandle);
    222  1.1  ws 		of->sc_ihandle = 0;
    223  1.1  ws 	}
    224  1.1  ws 
    225  1.1  ws 	return 0;
    226  1.1  ws }
    227  1.1  ws 
    228  1.1  ws void
    229  1.1  ws ofdstrategy(bp)
    230  1.1  ws 	struct buf *bp;
    231  1.1  ws {
    232  1.1  ws 	struct ofd_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
    233  1.1  ws 	struct partition *p;
    234  1.1  ws 	u_quad_t off;
    235  1.1  ws 	int read;
    236  1.1  ws 	int (*OF_io)(int, void *, int);
    237  1.1  ws 	daddr_t blkno = bp->b_blkno;
    238  1.1  ws 
    239  1.1  ws 	bp->b_resid = 0;
    240  1.1  ws 	if (bp->b_bcount == 0)
    241  1.1  ws 		goto done;
    242  1.1  ws 
    243  1.1  ws 	OF_io = bp->b_flags & B_READ ? OF_read : OF_write;
    244  1.1  ws 
    245  1.1  ws 	if (DISKPART(bp->b_dev) != RAW_PART) {
    246  1.1  ws 		if (bounds_check_with_label(bp, of->sc_dk.dk_label, 0) <= 0) {
    247  1.1  ws 			bp->b_resid = bp->b_bcount;
    248  1.1  ws 			goto done;
    249  1.1  ws 		}
    250  1.1  ws 		p = &of->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)];
    251  1.1  ws 		blkno = bp->b_blkno + p->p_offset;
    252  1.1  ws 	}
    253  1.1  ws 
    254  1.1  ws 	disk_busy(&of->sc_dk);
    255  1.1  ws 
    256  1.1  ws 	off = (u_quad_t)blkno * DEV_BSIZE;
    257  1.1  ws 	read = -1;
    258  1.1  ws 	do {
    259  1.1  ws 		if (OF_seek(of->sc_ihandle, off) < 0)
    260  1.1  ws 			break;
    261  1.1  ws 		read = OF_io(of->sc_ihandle, bp->b_data, bp->b_bcount);
    262  1.1  ws 	} while (read == -2);
    263  1.1  ws 	if (read < 0) {
    264  1.1  ws 		bp->b_error = EIO;
    265  1.1  ws 		bp->b_flags |= B_ERROR;
    266  1.1  ws 		bp->b_resid = bp->b_bcount;
    267  1.1  ws 	} else
    268  1.1  ws 		bp->b_resid = bp->b_bcount - read;
    269  1.1  ws 
    270  1.1  ws 	disk_unbusy(&of->sc_dk, bp->b_bcount - bp->b_resid);
    271  1.1  ws 
    272  1.1  ws done:
    273  1.1  ws 	biodone(bp);
    274  1.1  ws }
    275  1.1  ws 
    276  1.1  ws static void
    277  1.1  ws ofminphys(bp)
    278  1.1  ws 	struct buf *bp;
    279  1.1  ws {
    280  1.1  ws 	struct ofd_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
    281  1.1  ws 
    282  1.1  ws 	if (bp->b_bcount > of->max_transfer)
    283  1.1  ws 		bp->b_bcount = of->max_transfer;
    284  1.1  ws }
    285  1.1  ws 
    286  1.1  ws int
    287  1.1  ws ofdread(dev, uio)
    288  1.1  ws 	dev_t dev;
    289  1.1  ws 	struct uio *uio;
    290  1.1  ws {
    291  1.1  ws 	return physio(ofdstrategy, NULL, dev, B_READ, ofminphys, uio);
    292  1.1  ws }
    293  1.1  ws 
    294  1.1  ws int
    295  1.1  ws ofdwrite(dev, uio)
    296  1.1  ws 	dev_t dev;
    297  1.1  ws 	struct uio *uio;
    298  1.1  ws {
    299  1.1  ws 	return physio(ofdstrategy, NULL, dev, B_WRITE, ofminphys, uio);
    300  1.1  ws }
    301  1.1  ws 
    302  1.1  ws int
    303  1.1  ws ofdioctl(dev, cmd, data, flag, p)
    304  1.1  ws 	dev_t dev;
    305  1.1  ws 	u_long cmd;
    306  1.1  ws 	caddr_t data;
    307  1.1  ws 	int flag;
    308  1.1  ws 	struct proc *p;
    309  1.1  ws {
    310  1.1  ws 	struct ofd_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
    311  1.1  ws 	int error;
    312  1.1  ws 
    313  1.1  ws 	switch (cmd) {
    314  1.1  ws 	case DIOCGDINFO:
    315  1.1  ws 		*(struct disklabel *)data = *of->sc_dk.dk_label;
    316  1.1  ws 		return 0;
    317  1.1  ws 
    318  1.1  ws 	case DIOCGPART:
    319  1.1  ws 		((struct partinfo *)data)->disklab = of->sc_dk.dk_label;
    320  1.1  ws 		((struct partinfo *)data)->part =
    321  1.1  ws 			&of->sc_dk.dk_label->d_partitions[DISKPART(dev)];
    322  1.1  ws 		return 0;
    323  1.1  ws 
    324  1.1  ws 	case DIOCWDINFO:
    325  1.1  ws 	case DIOCSDINFO:
    326  1.1  ws 		if ((flag & FWRITE) == 0)
    327  1.1  ws 			return EBADF;
    328  1.1  ws 
    329  1.1  ws 		error = setdisklabel(of->sc_dk.dk_label,
    330  1.1  ws 				     (struct disklabel *)data, /*of->sc_dk.dk_openmask */0,
    331  1.1  ws 				     of->sc_dk.dk_cpulabel);
    332  1.1  ws 		if (error == 0 && cmd == DIOCWDINFO)
    333  1.1  ws 			error = writedisklabel(MAKEDISKDEV(major(dev),
    334  1.1  ws 							   DISKUNIT(dev), RAW_PART),
    335  1.1  ws 					       ofdstrategy,
    336  1.1  ws 					       of->sc_dk.dk_label,
    337  1.1  ws 					       of->sc_dk.dk_cpulabel);
    338  1.1  ws 
    339  1.1  ws 		return error;
    340  1.1  ws 	default:
    341  1.1  ws 		return ENOTTY;
    342  1.1  ws 	}
    343  1.1  ws }
    344  1.1  ws 
    345  1.1  ws int
    346  1.1  ws ofddump(dev, blkno, va, size)
    347  1.1  ws 	dev_t dev;
    348  1.1  ws 	daddr_t blkno;
    349  1.1  ws 	caddr_t va;
    350  1.1  ws 	size_t size;
    351  1.1  ws {
    352  1.1  ws 	return EINVAL;
    353  1.1  ws }
    354  1.1  ws 
    355  1.1  ws int
    356  1.1  ws ofdsize(dev)
    357  1.1  ws 	dev_t dev;
    358  1.1  ws {
    359  1.1  ws 	struct ofd_softc *of;
    360  1.1  ws 	int part;
    361  1.1  ws 	int size;
    362  1.1  ws 
    363  1.1  ws 	if (ofdopen(dev, 0, S_IFBLK) != 0)
    364  1.1  ws 		return -1;
    365  1.1  ws 	of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
    366  1.1  ws 	part = DISKPART(dev);
    367  1.1  ws 	if (of->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
    368  1.1  ws 		size = -1;
    369  1.1  ws 	else
    370  1.1  ws 		size = of->sc_dk.dk_label->d_partitions[part].p_size;
    371  1.1  ws 	if (ofdclose(dev, 0, S_IFBLK) != 0)
    372  1.1  ws 		return -1;
    373  1.1  ws 	return size;
    374  1.1  ws }
    375