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