Home | History | Annotate | Line # | Download | only in ofw
ofdisk.c revision 1.32
      1  1.32  christos /*	$NetBSD: ofdisk.c,v 1.32 2005/12/11 12:22:48 christos 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.18     lukem 
     34  1.18     lukem #include <sys/cdefs.h>
     35  1.32  christos __KERNEL_RCSID(0, "$NetBSD: ofdisk.c,v 1.32 2005/12/11 12:22:48 christos Exp $");
     36   1.1        ws 
     37   1.1        ws #include <sys/param.h>
     38   1.1        ws #include <sys/buf.h>
     39   1.1        ws #include <sys/device.h>
     40  1.16      matt #include <sys/conf.h>
     41   1.1        ws #include <sys/disklabel.h>
     42   1.1        ws #include <sys/disk.h>
     43   1.1        ws #include <sys/fcntl.h>
     44   1.1        ws #include <sys/ioctl.h>
     45   1.1        ws #include <sys/stat.h>
     46   1.1        ws #include <sys/systm.h>
     47   1.6   thorpej #include <sys/proc.h>
     48  1.19   gehenna #include <sys/conf.h>
     49   1.1        ws 
     50   1.1        ws #include <dev/ofw/openfirm.h>
     51   1.1        ws 
     52  1.10   mycroft struct ofdisk_softc {
     53   1.1        ws 	struct device sc_dev;
     54   1.1        ws 	int sc_phandle;
     55   1.1        ws 	int sc_unit;
     56   1.4   thorpej 	int sc_flags;
     57   1.1        ws 	struct disk sc_dk;
     58   1.1        ws 	int sc_ihandle;
     59   1.1        ws 	u_long max_transfer;
     60   1.1        ws };
     61   1.1        ws 
     62   1.4   thorpej /* sc_flags */
     63   1.4   thorpej #define OFDF_ISFLOPPY	0x01		/* we are a floppy drive */
     64   1.4   thorpej 
     65  1.29   thorpej #define	OFDISK_FLOPPY_P(of)		((of)->sc_flags & OFDF_ISFLOPPY)
     66  1.29   thorpej 
     67  1.17      matt static int ofdisk_match (struct device *, struct cfdata *, void *);
     68  1.17      matt static void ofdisk_attach (struct device *, struct device *, void *);
     69   1.1        ws 
     70  1.22   thorpej CFATTACH_DECL(ofdisk, sizeof(struct ofdisk_softc),
     71  1.23   thorpej     ofdisk_match, ofdisk_attach, NULL, NULL);
     72   1.1        ws 
     73   1.9   thorpej extern struct cfdriver ofdisk_cd;
     74   1.1        ws 
     75  1.19   gehenna dev_type_open(ofdisk_open);
     76  1.19   gehenna dev_type_close(ofdisk_close);
     77  1.19   gehenna dev_type_read(ofdisk_read);
     78  1.19   gehenna dev_type_write(ofdisk_write);
     79  1.19   gehenna dev_type_ioctl(ofdisk_ioctl);
     80  1.19   gehenna dev_type_strategy(ofdisk_strategy);
     81  1.19   gehenna dev_type_dump(ofdisk_dump);
     82  1.19   gehenna dev_type_size(ofdisk_size);
     83  1.19   gehenna 
     84  1.19   gehenna const struct bdevsw ofdisk_bdevsw = {
     85  1.19   gehenna 	ofdisk_open, ofdisk_close, ofdisk_strategy, ofdisk_ioctl,
     86  1.19   gehenna 	ofdisk_dump, ofdisk_size, D_DISK
     87  1.19   gehenna };
     88  1.19   gehenna 
     89  1.19   gehenna const struct cdevsw ofdisk_cdevsw = {
     90  1.19   gehenna 	ofdisk_open, ofdisk_close, ofdisk_read, ofdisk_write, ofdisk_ioctl,
     91  1.24  jdolecek 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
     92  1.19   gehenna };
     93   1.1        ws 
     94  1.29   thorpej static void ofminphys(struct buf *);
     95  1.29   thorpej 
     96  1.29   thorpej struct dkdriver ofdisk_dkdriver = { ofdisk_strategy, ofminphys };
     97   1.1        ws 
     98  1.17      matt void ofdisk_getdefaultlabel (struct ofdisk_softc *, struct disklabel *);
     99  1.17      matt void ofdisk_getdisklabel (dev_t);
    100   1.7   thorpej 
    101   1.1        ws static int
    102  1.17      matt ofdisk_match(struct device *parent, struct cfdata *match, void *aux)
    103   1.1        ws {
    104  1.10   mycroft 	struct ofbus_attach_args *oba = aux;
    105   1.1        ws 	char type[8];
    106   1.1        ws 	int l;
    107  1.30     perry 
    108  1.10   mycroft 	if (strcmp(oba->oba_busname, "ofw"))
    109  1.10   mycroft 		return (0);
    110  1.10   mycroft 	if ((l = OF_getprop(oba->oba_phandle, "device_type", type,
    111   1.4   thorpej 	    sizeof type - 1)) < 0)
    112   1.1        ws 		return 0;
    113   1.1        ws 	if (l >= sizeof type)
    114   1.1        ws 		return 0;
    115   1.1        ws 	type[l] = 0;
    116   1.1        ws 	return !strcmp(type, "block");
    117   1.1        ws }
    118   1.1        ws 
    119   1.1        ws static void
    120  1.17      matt ofdisk_attach(struct device *parent, struct device *self, void *aux)
    121   1.1        ws {
    122  1.10   mycroft 	struct ofdisk_softc *of = (void *)self;
    123  1.10   mycroft 	struct ofbus_attach_args *oba = aux;
    124   1.4   thorpej 	char child[64];
    125   1.1        ws 	int l;
    126   1.4   thorpej 
    127  1.10   mycroft 	if ((l = OF_getprop(oba->oba_phandle, "name", child,
    128   1.4   thorpej 	    sizeof child - 1)) < 0)
    129   1.4   thorpej 		panic("device without name?");
    130   1.4   thorpej 	if (l >= sizeof child)
    131   1.4   thorpej 		l = sizeof child - 1;
    132   1.4   thorpej 	child[l] = 0;
    133   1.4   thorpej 
    134   1.4   thorpej 	of->sc_flags = 0;
    135  1.10   mycroft 	of->sc_phandle = oba->oba_phandle;
    136  1.10   mycroft 	of->sc_unit = oba->oba_unit;
    137   1.1        ws 	of->sc_ihandle = 0;
    138  1.10   mycroft 	of->sc_dk.dk_driver = &ofdisk_dkdriver;
    139  1.29   thorpej 	of->sc_dk.dk_name = of->sc_dev.dv_xname;
    140   1.1        ws 	disk_attach(&of->sc_dk);
    141   1.3  christos 	printf("\n");
    142   1.4   thorpej 
    143   1.4   thorpej 	if (strcmp(child, "floppy") == 0)
    144   1.4   thorpej 		of->sc_flags |= OFDF_ISFLOPPY;
    145  1.29   thorpej 	else {
    146  1.29   thorpej 		/* Discover wedges on this disk. */
    147  1.29   thorpej 		dkwedge_discover(&of->sc_dk);
    148  1.29   thorpej 	}
    149   1.1        ws }
    150   1.1        ws 
    151   1.1        ws int
    152  1.32  christos ofdisk_open(dev_t dev, int flags, int fmt, struct lwp *lwp)
    153   1.1        ws {
    154   1.1        ws 	int unit = DISKUNIT(dev);
    155  1.10   mycroft 	struct ofdisk_softc *of;
    156   1.1        ws 	char path[256];
    157  1.29   thorpej 	int error, l, part;
    158  1.30     perry 
    159   1.1        ws 	if (unit >= ofdisk_cd.cd_ndevs)
    160   1.1        ws 		return ENXIO;
    161   1.1        ws 	if (!(of = ofdisk_cd.cd_devs[unit]))
    162   1.1        ws 		return ENXIO;
    163   1.1        ws 
    164  1.29   thorpej 	part = DISKPART(dev);
    165  1.29   thorpej 
    166  1.29   thorpej 	if ((error = lockmgr(&of->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
    167  1.29   thorpej 		return (error);
    168  1.29   thorpej 
    169  1.29   thorpej 	/*
    170  1.29   thorpej 	 * If there are wedges, and this is not RAW_PART, then we
    171  1.29   thorpej 	 * need to fail.
    172  1.29   thorpej 	 */
    173  1.29   thorpej 	if (of->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
    174  1.29   thorpej 		error = EBUSY;
    175  1.29   thorpej 		goto bad1;
    176  1.29   thorpej 	}
    177  1.29   thorpej 
    178   1.1        ws 	if (!of->sc_ihandle) {
    179   1.4   thorpej 		if ((l = OF_package_to_path(of->sc_phandle, path,
    180  1.11   mycroft 		    sizeof path - 3)) < 0 ||
    181  1.29   thorpej 		    l >= sizeof path - 3) {
    182  1.29   thorpej 			error = ENXIO;
    183  1.29   thorpej 			goto bad1;
    184  1.29   thorpej 		}
    185   1.1        ws 		path[l] = 0;
    186   1.1        ws 
    187   1.1        ws 		/*
    188   1.4   thorpej 		 * XXX This is for the benefit of SCSI/IDE disks that don't
    189   1.4   thorpej 		 * XXX have all their childs in the device tree.
    190   1.4   thorpej 		 * XXX YES, I DO THINK THIS IS A BUG IN OPENFIRMWARE!!!
    191   1.4   thorpej 		 * XXX And yes, this is a very gross hack!
    192   1.4   thorpej 		 * XXX See also ofscsi.c
    193   1.1        ws 		 */
    194   1.1        ws 		if (!strcmp(path + l - 4, "disk")) {
    195   1.1        ws 			path[l++] = '@';
    196   1.1        ws 			path[l++] = '0' + of->sc_unit;
    197   1.1        ws 			path[l] = 0;
    198   1.1        ws 		}
    199   1.1        ws 
    200  1.28    itojun 		strlcat(path, ":0", sizeof(path));
    201   1.1        ws 
    202  1.29   thorpej 		if ((of->sc_ihandle = OF_open(path)) == -1) {
    203  1.29   thorpej 			error = ENXIO;
    204  1.29   thorpej 			goto bad1;
    205  1.29   thorpej 		}
    206   1.1        ws 
    207   1.1        ws 		/*
    208   1.1        ws 		 * Try to get characteristics of the disk.
    209   1.1        ws 		 */
    210   1.4   thorpej 		of->max_transfer = OF_call_method_1("max-transfer",
    211   1.4   thorpej 		    of->sc_ihandle, 0);
    212   1.1        ws 		if (of->max_transfer > MAXPHYS)
    213   1.1        ws 			of->max_transfer = MAXPHYS;
    214   1.4   thorpej 
    215  1.10   mycroft 		ofdisk_getdisklabel(dev);
    216   1.1        ws 	}
    217   1.1        ws 
    218   1.1        ws 	switch (fmt) {
    219   1.1        ws 	case S_IFCHR:
    220  1.29   thorpej 		of->sc_dk.dk_copenmask |= 1 << part;
    221   1.1        ws 		break;
    222   1.1        ws 	case S_IFBLK:
    223  1.29   thorpej 		of->sc_dk.dk_bopenmask |= 1 << part;
    224   1.1        ws 		break;
    225   1.1        ws 	}
    226   1.4   thorpej 	of->sc_dk.dk_openmask =
    227   1.4   thorpej 	    of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
    228  1.29   thorpej 
    229  1.29   thorpej 	(void) lockmgr(&of->sc_dk.dk_openlock, LK_RELEASE, NULL);
    230   1.1        ws 	return 0;
    231  1.29   thorpej 
    232  1.29   thorpej  bad1:
    233  1.29   thorpej 	(void) lockmgr(&of->sc_dk.dk_openlock, LK_RELEASE, NULL);
    234  1.29   thorpej 	return (error);
    235   1.1        ws }
    236   1.1        ws 
    237   1.1        ws int
    238  1.32  christos ofdisk_close(dev_t dev, int flags, int fmt, struct lwp *l)
    239   1.1        ws {
    240  1.10   mycroft 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
    241  1.29   thorpej 	int error;
    242  1.29   thorpej 
    243  1.29   thorpej 	if ((error = lockmgr(&of->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
    244  1.29   thorpej 		return (error);
    245   1.1        ws 
    246   1.1        ws 	switch (fmt) {
    247   1.1        ws 	case S_IFCHR:
    248   1.1        ws 		of->sc_dk.dk_copenmask &= ~(1 << DISKPART(dev));
    249   1.1        ws 		break;
    250   1.1        ws 	case S_IFBLK:
    251   1.1        ws 		of->sc_dk.dk_bopenmask &= ~(1 << DISKPART(dev));
    252   1.1        ws 		break;
    253   1.1        ws 	}
    254   1.1        ws 	of->sc_dk.dk_openmask = of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
    255  1.30     perry 
    256   1.4   thorpej #ifdef	FIRMWORKSBUGS
    257   1.1        ws 	/*
    258   1.1        ws 	 * This is a hack to get the firmware to flush its buffers.
    259   1.1        ws 	 */
    260   1.1        ws 	OF_seek(of->sc_ihandle, 0);
    261   1.1        ws #endif
    262   1.1        ws 	if (!of->sc_dk.dk_openmask) {
    263   1.1        ws 		OF_close(of->sc_ihandle);
    264   1.1        ws 		of->sc_ihandle = 0;
    265   1.1        ws 	}
    266   1.1        ws 
    267  1.29   thorpej 	(void) lockmgr(&of->sc_dk.dk_openlock, LK_RELEASE, NULL);
    268   1.1        ws 	return 0;
    269   1.1        ws }
    270   1.1        ws 
    271   1.1        ws void
    272  1.17      matt ofdisk_strategy(struct buf *bp)
    273   1.1        ws {
    274  1.10   mycroft 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
    275   1.1        ws 	struct partition *p;
    276   1.1        ws 	u_quad_t off;
    277   1.1        ws 	int read;
    278   1.1        ws 	int (*OF_io)(int, void *, int);
    279   1.1        ws 	daddr_t blkno = bp->b_blkno;
    280   1.4   thorpej 
    281   1.1        ws 	bp->b_resid = 0;
    282   1.1        ws 	if (bp->b_bcount == 0)
    283   1.1        ws 		goto done;
    284  1.30     perry 
    285  1.31        he 	OF_io = bp->b_flags & B_READ ? OF_read :
    286  1.31        he 		(int(*)(int, void*, int))OF_write;
    287   1.1        ws 
    288   1.1        ws 	if (DISKPART(bp->b_dev) != RAW_PART) {
    289  1.27   thorpej 		if (bounds_check_with_label(&of->sc_dk, bp, 0) <= 0) {
    290   1.1        ws 			bp->b_resid = bp->b_bcount;
    291   1.1        ws 			goto done;
    292   1.1        ws 		}
    293   1.1        ws 		p = &of->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)];
    294   1.1        ws 		blkno = bp->b_blkno + p->p_offset;
    295   1.1        ws 	}
    296   1.4   thorpej 
    297   1.1        ws 	disk_busy(&of->sc_dk);
    298   1.1        ws 
    299   1.1        ws 	off = (u_quad_t)blkno * DEV_BSIZE;
    300   1.1        ws 	read = -1;
    301   1.1        ws 	do {
    302   1.1        ws 		if (OF_seek(of->sc_ihandle, off) < 0)
    303   1.1        ws 			break;
    304   1.1        ws 		read = OF_io(of->sc_ihandle, bp->b_data, bp->b_bcount);
    305   1.1        ws 	} while (read == -2);
    306   1.4   thorpej 
    307   1.1        ws 	if (read < 0) {
    308   1.1        ws 		bp->b_error = EIO;
    309   1.1        ws 		bp->b_flags |= B_ERROR;
    310   1.1        ws 		bp->b_resid = bp->b_bcount;
    311   1.1        ws 	} else
    312   1.1        ws 		bp->b_resid = bp->b_bcount - read;
    313   1.1        ws 
    314  1.25       mrg 	disk_unbusy(&of->sc_dk, bp->b_bcount - bp->b_resid,
    315  1.25       mrg 	    (bp->b_flags & B_READ));
    316   1.1        ws 
    317   1.1        ws done:
    318   1.1        ws 	biodone(bp);
    319   1.1        ws }
    320   1.1        ws 
    321   1.1        ws static void
    322  1.17      matt ofminphys(struct buf *bp)
    323   1.1        ws {
    324  1.10   mycroft 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(bp->b_dev)];
    325  1.30     perry 
    326   1.1        ws 	if (bp->b_bcount > of->max_transfer)
    327   1.1        ws 		bp->b_bcount = of->max_transfer;
    328   1.1        ws }
    329   1.1        ws 
    330   1.1        ws int
    331  1.17      matt ofdisk_read(dev_t dev, struct uio *uio, int flags)
    332   1.1        ws {
    333  1.10   mycroft 	return physio(ofdisk_strategy, NULL, dev, B_READ, ofminphys, uio);
    334   1.1        ws }
    335   1.1        ws 
    336   1.1        ws int
    337  1.17      matt ofdisk_write(dev_t dev, struct uio *uio, int flags)
    338   1.1        ws {
    339  1.10   mycroft 	return physio(ofdisk_strategy, NULL, dev, B_WRITE, ofminphys, uio);
    340   1.1        ws }
    341   1.1        ws 
    342   1.1        ws int
    343  1.32  christos ofdisk_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    344   1.1        ws {
    345  1.10   mycroft 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[DISKUNIT(dev)];
    346   1.1        ws 	int error;
    347  1.14      fvdl #ifdef __HAVE_OLD_DISKLABEL
    348  1.14      fvdl 	struct disklabel newlabel;
    349  1.14      fvdl #endif
    350  1.30     perry 
    351   1.1        ws 	switch (cmd) {
    352   1.1        ws 	case DIOCGDINFO:
    353   1.1        ws 		*(struct disklabel *)data = *of->sc_dk.dk_label;
    354   1.1        ws 		return 0;
    355  1.14      fvdl #ifdef __HAVE_OLD_DISKLABEL
    356  1.14      fvdl 	case ODIOCGDINFO:
    357  1.14      fvdl 		newlabel = *of->sc_dk.dk_label;
    358  1.14      fvdl 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    359  1.15      fvdl 			return ENOTTY;
    360  1.14      fvdl 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
    361  1.14      fvdl 		return 0;
    362  1.14      fvdl #endif
    363  1.30     perry 
    364   1.1        ws 	case DIOCGPART:
    365   1.1        ws 		((struct partinfo *)data)->disklab = of->sc_dk.dk_label;
    366   1.1        ws 		((struct partinfo *)data)->part =
    367   1.1        ws 			&of->sc_dk.dk_label->d_partitions[DISKPART(dev)];
    368   1.1        ws 		return 0;
    369  1.30     perry 
    370   1.1        ws 	case DIOCWDINFO:
    371   1.1        ws 	case DIOCSDINFO:
    372  1.14      fvdl #ifdef __HAVE_OLD_DISKLABEL
    373  1.14      fvdl 	case ODIOCWDINFO:
    374  1.14      fvdl 	case ODIOCSDINFO:
    375  1.14      fvdl #endif
    376  1.14      fvdl 	{
    377  1.14      fvdl 		struct disklabel *lp;
    378  1.14      fvdl 
    379  1.14      fvdl #ifdef __HAVE_OLD_DISKLABEL
    380  1.14      fvdl 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
    381  1.14      fvdl 			memset(&newlabel, 0, sizeof newlabel);
    382  1.14      fvdl 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
    383  1.14      fvdl 			lp = &newlabel;
    384  1.14      fvdl 		} else
    385  1.14      fvdl #endif
    386  1.14      fvdl 		lp = (struct disklabel *)data;
    387  1.14      fvdl 
    388   1.1        ws 		if ((flag & FWRITE) == 0)
    389   1.1        ws 			return EBADF;
    390  1.29   thorpej 
    391  1.29   thorpej 		if ((error = lockmgr(&of->sc_dk.dk_openlock, LK_EXCLUSIVE,
    392  1.29   thorpej 				     NULL)) != 0)
    393  1.29   thorpej 			return (error);
    394  1.29   thorpej 
    395   1.1        ws 		error = setdisklabel(of->sc_dk.dk_label,
    396  1.14      fvdl 		    lp, /*of->sc_dk.dk_openmask */0,
    397   1.4   thorpej 		    of->sc_dk.dk_cpulabel);
    398  1.14      fvdl 		if (error == 0 && cmd == DIOCWDINFO
    399  1.14      fvdl #ifdef __HAVE_OLD_DISKLABEL
    400  1.14      fvdl 		    || xfer == ODIOCWDINFO
    401  1.14      fvdl #endif
    402  1.14      fvdl 		    )
    403   1.1        ws 			error = writedisklabel(MAKEDISKDEV(major(dev),
    404  1.10   mycroft 			    DISKUNIT(dev), RAW_PART), ofdisk_strategy,
    405   1.4   thorpej 			    of->sc_dk.dk_label, of->sc_dk.dk_cpulabel);
    406   1.1        ws 
    407  1.29   thorpej 		(void) lockmgr(&of->sc_dk.dk_openlock, LK_RELEASE, NULL);
    408  1.29   thorpej 
    409   1.1        ws 		return error;
    410  1.14      fvdl 	}
    411   1.7   thorpej 
    412   1.7   thorpej 	case DIOCGDEFLABEL:
    413  1.10   mycroft 		ofdisk_getdefaultlabel(of, (struct disklabel *)data);
    414   1.7   thorpej 		return 0;
    415  1.14      fvdl #ifdef __HAVE_OLD_DISKLABEL
    416  1.14      fvdl 	case DIOCGDEFLABEL:
    417  1.14      fvdl 		ofdisk_getdefaultlabel(of, &newlabel);
    418  1.14      fvdl 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    419  1.15      fvdl 			return ENOTTY;
    420  1.14      fvdl 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
    421  1.14      fvdl 		return 0;
    422  1.14      fvdl #endif
    423   1.7   thorpej 
    424  1.29   thorpej 	case DIOCAWEDGE:
    425  1.29   thorpej 	    {
    426  1.29   thorpej 	    	struct dkwedge_info *dkw = (void *) data;
    427  1.29   thorpej 
    428  1.29   thorpej 		if (OFDISK_FLOPPY_P(of))
    429  1.29   thorpej 			return (ENOTTY);
    430  1.29   thorpej 
    431  1.29   thorpej 		if ((flag & FWRITE) == 0)
    432  1.29   thorpej 			return (EBADF);
    433  1.29   thorpej 
    434  1.29   thorpej 		/* If the ioctl happens here, the parent is us. */
    435  1.29   thorpej 		strcpy(dkw->dkw_parent, of->sc_dev.dv_xname);
    436  1.29   thorpej 		return (dkwedge_add(dkw));
    437  1.29   thorpej 	    }
    438  1.29   thorpej 
    439  1.29   thorpej 	case DIOCDWEDGE:
    440  1.29   thorpej 	    {
    441  1.29   thorpej 	    	struct dkwedge_info *dkw = (void *) data;
    442  1.29   thorpej 
    443  1.29   thorpej 		if (OFDISK_FLOPPY_P(of))
    444  1.29   thorpej 			return (ENOTTY);
    445  1.29   thorpej 
    446  1.29   thorpej 		if ((flag & FWRITE) == 0)
    447  1.29   thorpej 			return (EBADF);
    448  1.29   thorpej 
    449  1.29   thorpej 		/* If the ioctl happens here, the parent is us. */
    450  1.29   thorpej 		strcpy(dkw->dkw_parent, of->sc_dev.dv_xname);
    451  1.29   thorpej 		return (dkwedge_del(dkw));
    452  1.29   thorpej 	    }
    453  1.30     perry 
    454  1.29   thorpej 	case DIOCLWEDGES:
    455  1.29   thorpej 	    {
    456  1.29   thorpej 	    	struct dkwedge_list *dkwl = (void *) data;
    457  1.29   thorpej 
    458  1.29   thorpej 		if (OFDISK_FLOPPY_P(of))
    459  1.29   thorpej 			return (ENOTTY);
    460  1.29   thorpej 
    461  1.32  christos 		return (dkwedge_list(&of->sc_dk, dkwl, l));
    462  1.29   thorpej 	    }
    463  1.29   thorpej 
    464   1.1        ws 	default:
    465   1.1        ws 		return ENOTTY;
    466   1.1        ws 	}
    467   1.1        ws }
    468   1.1        ws 
    469   1.1        ws int
    470  1.17      matt ofdisk_dump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
    471   1.1        ws {
    472   1.1        ws 	return EINVAL;
    473   1.1        ws }
    474   1.1        ws 
    475   1.1        ws int
    476  1.17      matt ofdisk_size(dev_t dev)
    477   1.1        ws {
    478  1.10   mycroft 	struct ofdisk_softc *of;
    479   1.5   thorpej 	struct disklabel *lp;
    480   1.5   thorpej 	int size, part, omask, unit;
    481   1.5   thorpej 
    482   1.5   thorpej 	unit = DISKUNIT(dev);
    483   1.5   thorpej 	if (unit >= ofdisk_cd.cd_ndevs ||
    484   1.5   thorpej 	    (of = ofdisk_cd.cd_devs[unit]) == NULL)
    485   1.1        ws 		return -1;
    486   1.5   thorpej 
    487   1.1        ws 	part = DISKPART(dev);
    488   1.5   thorpej 	omask = of->sc_dk.dk_openmask & (1 << part);
    489   1.6   thorpej 	lp = of->sc_dk.dk_label;
    490   1.5   thorpej 
    491  1.32  christos 	if (omask == 0 && ofdisk_open(dev, 0, S_IFBLK, curlwp) != 0)
    492   1.5   thorpej 		return -1;
    493   1.5   thorpej 
    494   1.5   thorpej 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
    495   1.1        ws 		size = -1;
    496   1.1        ws 	else
    497   1.5   thorpej 		size = lp->d_partitions[part].p_size *
    498   1.5   thorpej 		    (lp->d_secsize / DEV_BSIZE);
    499   1.5   thorpej 
    500  1.32  christos 	if (omask == 0 && ofdisk_close(dev, 0, S_IFBLK, curlwp) != 0)
    501   1.1        ws 		return -1;
    502   1.5   thorpej 
    503   1.1        ws 	return size;
    504   1.7   thorpej }
    505   1.7   thorpej 
    506   1.7   thorpej void
    507  1.17      matt ofdisk_getdefaultlabel(struct ofdisk_softc *of, struct disklabel *lp)
    508   1.7   thorpej {
    509   1.7   thorpej 
    510  1.17      matt 	memset(lp, 0, sizeof *lp);
    511   1.7   thorpej 
    512   1.7   thorpej 	/*
    513   1.7   thorpej 	 * XXX Firmware bug?  Asking for block size gives a
    514  1.20       chs 	 * XXX ridiculous number!  So we use what the boot program
    515   1.7   thorpej 	 * XXX uses.
    516   1.7   thorpej 	 */
    517   1.7   thorpej 	lp->d_secsize = DEV_BSIZE;
    518   1.7   thorpej 
    519   1.7   thorpej 	lp->d_secperunit = OF_call_method_1("#blocks",
    520   1.7   thorpej 	    of->sc_ihandle, 0);
    521   1.7   thorpej 	if (lp->d_secperunit == (u_int32_t)-1)
    522   1.7   thorpej 		lp->d_secperunit = 0x7fffffff;
    523   1.7   thorpej 
    524   1.7   thorpej 	lp->d_secpercyl = 1;
    525   1.7   thorpej 	lp->d_nsectors = 1;
    526   1.7   thorpej 	lp->d_ntracks = 1;
    527   1.7   thorpej 	lp->d_ncylinders = lp->d_secperunit;
    528   1.7   thorpej 
    529   1.7   thorpej 	lp->d_partitions[RAW_PART].p_offset = 0;
    530   1.7   thorpej 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
    531   1.7   thorpej 	lp->d_npartitions = RAW_PART + 1;
    532   1.7   thorpej 
    533   1.7   thorpej 	lp->d_magic = DISKMAGIC;
    534   1.7   thorpej 	lp->d_magic2 = DISKMAGIC;
    535   1.7   thorpej 	lp->d_checksum = dkcksum(lp);
    536   1.7   thorpej }
    537   1.7   thorpej 
    538   1.7   thorpej void
    539  1.10   mycroft ofdisk_getdisklabel(dev)
    540   1.7   thorpej 	dev_t dev;
    541   1.7   thorpej {
    542   1.7   thorpej 	int unit = DISKUNIT(dev);
    543  1.10   mycroft 	struct ofdisk_softc *of = ofdisk_cd.cd_devs[unit];
    544   1.7   thorpej 	struct disklabel *lp = of->sc_dk.dk_label;
    545  1.26       dsl 	const char *errmes;
    546   1.7   thorpej 	int l;
    547   1.7   thorpej 
    548  1.10   mycroft 	ofdisk_getdefaultlabel(of, lp);
    549   1.7   thorpej 
    550   1.7   thorpej 	/*
    551   1.7   thorpej 	 * Don't read the disklabel on a floppy; simply
    552   1.7   thorpej 	 * assign all partitions the same size/offset as
    553   1.7   thorpej 	 * RAW_PART.  (This is essentially what the ISA
    554   1.7   thorpej 	 * floppy driver does, but we don't deal with
    555   1.7   thorpej 	 * density stuff.)
    556   1.7   thorpej 	 */
    557  1.29   thorpej 	if (OFDISK_FLOPPY_P(of)) {
    558   1.7   thorpej 		lp->d_npartitions = MAXPARTITIONS;
    559   1.7   thorpej 		for (l = 0; l < lp->d_npartitions; l++) {
    560   1.7   thorpej 			if (l == RAW_PART)
    561   1.7   thorpej 				continue;
    562   1.7   thorpej 			/* struct copy */
    563   1.7   thorpej 			lp->d_partitions[l] =
    564   1.7   thorpej 			    lp->d_partitions[RAW_PART];
    565   1.7   thorpej 		}
    566   1.7   thorpej 		lp->d_checksum = dkcksum(lp);
    567   1.7   thorpej 	} else {
    568   1.7   thorpej 		errmes = readdisklabel(MAKEDISKDEV(major(dev),
    569  1.10   mycroft 		    unit, RAW_PART), ofdisk_strategy, lp,
    570   1.7   thorpej 		    of->sc_dk.dk_cpulabel);
    571   1.7   thorpej 		if (errmes != NULL)
    572  1.12       cgd 			printf("%s: %s\n", of->sc_dev.dv_xname, errmes);
    573   1.7   thorpej 	}
    574   1.1        ws }
    575