xref: /netbsd-src/sys/dev/ofw/ofdisk.c (revision 50b1e3ebe4f6d9ff87f705092d1bf6c123992bb4)
1*50b1e3ebSthorpej /*	$NetBSD: ofdisk.c,v 1.54 2020/01/26 21:43:52 thorpej Exp $	*/
25804d3f6Sws 
35804d3f6Sws /*
45804d3f6Sws  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
55804d3f6Sws  * Copyright (C) 1995, 1996 TooLs GmbH.
65804d3f6Sws  * All rights reserved.
75804d3f6Sws  *
85804d3f6Sws  * Redistribution and use in source and binary forms, with or without
95804d3f6Sws  * modification, are permitted provided that the following conditions
105804d3f6Sws  * are met:
115804d3f6Sws  * 1. Redistributions of source code must retain the above copyright
125804d3f6Sws  *    notice, this list of conditions and the following disclaimer.
135804d3f6Sws  * 2. Redistributions in binary form must reproduce the above copyright
145804d3f6Sws  *    notice, this list of conditions and the following disclaimer in the
155804d3f6Sws  *    documentation and/or other materials provided with the distribution.
165804d3f6Sws  * 3. All advertising materials mentioning features or use of this software
175804d3f6Sws  *    must display the following acknowledgement:
185804d3f6Sws  *	This product includes software developed by TooLs GmbH.
195804d3f6Sws  * 4. The name of TooLs GmbH may not be used to endorse or promote products
205804d3f6Sws  *    derived from this software without specific prior written permission.
215804d3f6Sws  *
225804d3f6Sws  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
235804d3f6Sws  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
245804d3f6Sws  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
255804d3f6Sws  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265804d3f6Sws  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
275804d3f6Sws  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
285804d3f6Sws  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
295804d3f6Sws  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
305804d3f6Sws  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
315804d3f6Sws  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325804d3f6Sws  */
335804d3f6Sws 
34ab5d9d2bSlukem #include <sys/cdefs.h>
35*50b1e3ebSthorpej __KERNEL_RCSID(0, "$NetBSD: ofdisk.c,v 1.54 2020/01/26 21:43:52 thorpej Exp $");
36ab5d9d2bSlukem 
375804d3f6Sws #include <sys/param.h>
385804d3f6Sws #include <sys/buf.h>
395804d3f6Sws #include <sys/device.h>
403ec769dfSmatt #include <sys/conf.h>
415804d3f6Sws #include <sys/disklabel.h>
425804d3f6Sws #include <sys/disk.h>
435804d3f6Sws #include <sys/fcntl.h>
445804d3f6Sws #include <sys/ioctl.h>
455804d3f6Sws #include <sys/stat.h>
465804d3f6Sws #include <sys/systm.h>
476dce360fSthorpej #include <sys/proc.h>
485804d3f6Sws 
495804d3f6Sws #include <dev/ofw/openfirm.h>
505804d3f6Sws 
51310f6fefSmycroft struct ofdisk_softc {
529af887f0Smrg 	device_t sc_dev;
535804d3f6Sws 	int sc_phandle;
545804d3f6Sws 	int sc_unit;
552747d6c8Sthorpej 	int sc_flags;
565804d3f6Sws 	struct disk sc_dk;
575804d3f6Sws 	int sc_ihandle;
585804d3f6Sws 	u_long max_transfer;
595804d3f6Sws };
605804d3f6Sws 
612747d6c8Sthorpej /* sc_flags */
622747d6c8Sthorpej #define OFDF_ISFLOPPY	0x01		/* we are a floppy drive */
632747d6c8Sthorpej 
64281037afSthorpej #define	OFDISK_FLOPPY_P(of)		((of)->sc_flags & OFDF_ISFLOPPY)
65281037afSthorpej 
667cf29912Scegger static int ofdisk_match (device_t, cfdata_t, void *);
677cf29912Scegger static void ofdisk_attach (device_t, device_t, void *);
685804d3f6Sws 
699af887f0Smrg CFATTACH_DECL_NEW(ofdisk, sizeof(struct ofdisk_softc),
70c9b3657cSthorpej     ofdisk_match, ofdisk_attach, NULL, NULL);
715804d3f6Sws 
72d02585cfSthorpej extern struct cfdriver ofdisk_cd;
735804d3f6Sws 
7477a6b82bSgehenna dev_type_open(ofdisk_open);
7577a6b82bSgehenna dev_type_close(ofdisk_close);
7677a6b82bSgehenna dev_type_read(ofdisk_read);
7777a6b82bSgehenna dev_type_write(ofdisk_write);
7877a6b82bSgehenna dev_type_ioctl(ofdisk_ioctl);
7977a6b82bSgehenna dev_type_strategy(ofdisk_strategy);
8077a6b82bSgehenna dev_type_dump(ofdisk_dump);
8177a6b82bSgehenna dev_type_size(ofdisk_size);
8277a6b82bSgehenna 
8377a6b82bSgehenna const struct bdevsw ofdisk_bdevsw = {
84a68f9396Sdholland 	.d_open = ofdisk_open,
85a68f9396Sdholland 	.d_close = ofdisk_close,
86a68f9396Sdholland 	.d_strategy = ofdisk_strategy,
87a68f9396Sdholland 	.d_ioctl = ofdisk_ioctl,
88a68f9396Sdholland 	.d_dump = ofdisk_dump,
89a68f9396Sdholland 	.d_psize = ofdisk_size,
908c70ef39Sdholland 	.d_discard = nodiscard,
91a68f9396Sdholland 	.d_flag = D_DISK
9277a6b82bSgehenna };
9377a6b82bSgehenna 
9477a6b82bSgehenna const struct cdevsw ofdisk_cdevsw = {
95a68f9396Sdholland 	.d_open = ofdisk_open,
96a68f9396Sdholland 	.d_close = ofdisk_close,
97a68f9396Sdholland 	.d_read = ofdisk_read,
98a68f9396Sdholland 	.d_write = ofdisk_write,
99a68f9396Sdholland 	.d_ioctl = ofdisk_ioctl,
100a68f9396Sdholland 	.d_stop = nostop,
101a68f9396Sdholland 	.d_tty = notty,
102a68f9396Sdholland 	.d_poll = nopoll,
103a68f9396Sdholland 	.d_mmap = nommap,
104a68f9396Sdholland 	.d_kqfilter = nokqfilter,
105f9228f42Sdholland 	.d_discard = nodiscard,
106a68f9396Sdholland 	.d_flag = D_DISK
10777a6b82bSgehenna };
1085804d3f6Sws 
109281037afSthorpej static void ofminphys(struct buf *);
110281037afSthorpej 
1116f00c789Smlelstv struct dkdriver ofdisk_dkdriver = {
1126f00c789Smlelstv 	.d_strategy = ofdisk_strategy,
1136f00c789Smlelstv 	.d_minphys = ofminphys
1146f00c789Smlelstv };
1155804d3f6Sws 
1165911b9a6Smatt void ofdisk_getdefaultlabel (struct ofdisk_softc *, struct disklabel *);
1175911b9a6Smatt void ofdisk_getdisklabel (dev_t);
1185e055931Sthorpej 
1195804d3f6Sws static int
ofdisk_match(device_t parent,cfdata_t match,void * aux)1207cf29912Scegger ofdisk_match(device_t parent, cfdata_t match, void *aux)
1215804d3f6Sws {
122310f6fefSmycroft 	struct ofbus_attach_args *oba = aux;
1235804d3f6Sws 	char type[8];
1245804d3f6Sws 	int l;
1255804d3f6Sws 
126310f6fefSmycroft 	if (strcmp(oba->oba_busname, "ofw"))
127310f6fefSmycroft 		return (0);
128310f6fefSmycroft 	if ((l = OF_getprop(oba->oba_phandle, "device_type", type,
1292747d6c8Sthorpej 	    sizeof type - 1)) < 0)
1305804d3f6Sws 		return 0;
1315804d3f6Sws 	if (l >= sizeof type)
1325804d3f6Sws 		return 0;
1335804d3f6Sws 	type[l] = 0;
1340ed481bcSmartin 	return strcmp(type, "block") == 0 || strcmp(type, "scsi") == 0;
1355804d3f6Sws }
1365804d3f6Sws 
1375804d3f6Sws static void
ofdisk_attach(device_t parent,device_t self,void * aux)1387cf29912Scegger ofdisk_attach(device_t parent, device_t self, void *aux)
1395804d3f6Sws {
140838ee1e0Sthorpej 	struct ofdisk_softc *of = device_private(self);
141310f6fefSmycroft 	struct ofbus_attach_args *oba = aux;
1422747d6c8Sthorpej 	char child[64];
1435804d3f6Sws 	int l;
1445804d3f6Sws 
1459af887f0Smrg 	of->sc_dev = self;
146310f6fefSmycroft 	if ((l = OF_getprop(oba->oba_phandle, "name", child,
1472747d6c8Sthorpej 	    sizeof child - 1)) < 0)
1482747d6c8Sthorpej 		panic("device without name?");
1492747d6c8Sthorpej 	if (l >= sizeof child)
1502747d6c8Sthorpej 		l = sizeof child - 1;
1512747d6c8Sthorpej 	child[l] = 0;
1522747d6c8Sthorpej 
1532747d6c8Sthorpej 	of->sc_flags = 0;
154310f6fefSmycroft 	of->sc_phandle = oba->oba_phandle;
155310f6fefSmycroft 	of->sc_unit = oba->oba_unit;
1565804d3f6Sws 	of->sc_ihandle = 0;
1579af887f0Smrg 	disk_init(&of->sc_dk, device_xname(of->sc_dev), &ofdisk_dkdriver);
1585804d3f6Sws 	disk_attach(&of->sc_dk);
15986373f8cSchristos 	printf("\n");
1602747d6c8Sthorpej 
1612747d6c8Sthorpej 	if (strcmp(child, "floppy") == 0)
1622747d6c8Sthorpej 		of->sc_flags |= OFDF_ISFLOPPY;
163281037afSthorpej 	else {
164281037afSthorpej 		/* Discover wedges on this disk. */
165281037afSthorpej 		dkwedge_discover(&of->sc_dk);
166281037afSthorpej 	}
1675804d3f6Sws }
1685804d3f6Sws 
1695804d3f6Sws int
ofdisk_open(dev_t dev,int flags,int fmt,struct lwp * lwp)17095e1ffb1Schristos ofdisk_open(dev_t dev, int flags, int fmt, struct lwp *lwp)
1715804d3f6Sws {
172310f6fefSmycroft 	struct ofdisk_softc *of;
1735804d3f6Sws 	char path[256];
174281037afSthorpej 	int error, l, part;
1755804d3f6Sws 
1764795ee76Scegger 	of = device_lookup_private(&ofdisk_cd, DISKUNIT(dev));
1774795ee76Scegger 	if (of == NULL)
1785804d3f6Sws 		return ENXIO;
1795804d3f6Sws 
180281037afSthorpej 	part = DISKPART(dev);
181281037afSthorpej 
182b5a9ff06Sad 	mutex_enter(&of->sc_dk.dk_openlock);
183281037afSthorpej 
184281037afSthorpej 	/*
185281037afSthorpej 	 * If there are wedges, and this is not RAW_PART, then we
186281037afSthorpej 	 * need to fail.
187281037afSthorpej 	 */
188281037afSthorpej 	if (of->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
189281037afSthorpej 		error = EBUSY;
190281037afSthorpej 		goto bad1;
191281037afSthorpej 	}
192281037afSthorpej 
1935804d3f6Sws 	if (!of->sc_ihandle) {
1942747d6c8Sthorpej 		if ((l = OF_package_to_path(of->sc_phandle, path,
19564d86b59Smycroft 		    sizeof path - 3)) < 0 ||
196281037afSthorpej 		    l >= sizeof path - 3) {
197281037afSthorpej 			error = ENXIO;
198281037afSthorpej 			goto bad1;
199281037afSthorpej 		}
2005804d3f6Sws 		path[l] = 0;
2015804d3f6Sws 
2025804d3f6Sws 		/*
2032747d6c8Sthorpej 		 * XXX This is for the benefit of SCSI/IDE disks that don't
2042747d6c8Sthorpej 		 * XXX have all their childs in the device tree.
2052747d6c8Sthorpej 		 * XXX YES, I DO THINK THIS IS A BUG IN OPENFIRMWARE!!!
2062747d6c8Sthorpej 		 * XXX And yes, this is a very gross hack!
2072747d6c8Sthorpej 		 * XXX See also ofscsi.c
2085804d3f6Sws 		 */
2095804d3f6Sws 		if (!strcmp(path + l - 4, "disk")) {
2105804d3f6Sws 			path[l++] = '@';
2115804d3f6Sws 			path[l++] = '0' + of->sc_unit;
2125804d3f6Sws 			path[l] = 0;
2135804d3f6Sws 		}
2145804d3f6Sws 
215804f7095Sitojun 		strlcat(path, ":0", sizeof(path));
2165804d3f6Sws 
217281037afSthorpej 		if ((of->sc_ihandle = OF_open(path)) == -1) {
218281037afSthorpej 			error = ENXIO;
219281037afSthorpej 			goto bad1;
220281037afSthorpej 		}
2215804d3f6Sws 
2225804d3f6Sws 		/*
2235804d3f6Sws 		 * Try to get characteristics of the disk.
2245804d3f6Sws 		 */
2252747d6c8Sthorpej 		of->max_transfer = OF_call_method_1("max-transfer",
2262747d6c8Sthorpej 		    of->sc_ihandle, 0);
2275804d3f6Sws 		if (of->max_transfer > MAXPHYS)
2285804d3f6Sws 			of->max_transfer = MAXPHYS;
2295804d3f6Sws 
230310f6fefSmycroft 		ofdisk_getdisklabel(dev);
2315804d3f6Sws 	}
2325804d3f6Sws 
2335804d3f6Sws 	switch (fmt) {
2345804d3f6Sws 	case S_IFCHR:
235281037afSthorpej 		of->sc_dk.dk_copenmask |= 1 << part;
2365804d3f6Sws 		break;
2375804d3f6Sws 	case S_IFBLK:
238281037afSthorpej 		of->sc_dk.dk_bopenmask |= 1 << part;
2395804d3f6Sws 		break;
2405804d3f6Sws 	}
2412747d6c8Sthorpej 	of->sc_dk.dk_openmask =
2422747d6c8Sthorpej 	    of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
2435804d3f6Sws 
244281037afSthorpej 
245b5a9ff06Sad 	error = 0;
246281037afSthorpej  bad1:
247b5a9ff06Sad 	mutex_exit(&of->sc_dk.dk_openlock);
248281037afSthorpej 	return (error);
2495804d3f6Sws }
2505804d3f6Sws 
2515804d3f6Sws int
ofdisk_close(dev_t dev,int flags,int fmt,struct lwp * l)25295e1ffb1Schristos ofdisk_close(dev_t dev, int flags, int fmt, struct lwp *l)
2535804d3f6Sws {
2544795ee76Scegger 	struct ofdisk_softc *of =
2554795ee76Scegger 		device_lookup_private(&ofdisk_cd, DISKUNIT(dev));
256281037afSthorpej 
257b5a9ff06Sad 	mutex_enter(&of->sc_dk.dk_openlock);
2585804d3f6Sws 
2595804d3f6Sws 	switch (fmt) {
2605804d3f6Sws 	case S_IFCHR:
2615804d3f6Sws 		of->sc_dk.dk_copenmask &= ~(1 << DISKPART(dev));
2625804d3f6Sws 		break;
2635804d3f6Sws 	case S_IFBLK:
2645804d3f6Sws 		of->sc_dk.dk_bopenmask &= ~(1 << DISKPART(dev));
2655804d3f6Sws 		break;
2665804d3f6Sws 	}
2675804d3f6Sws 	of->sc_dk.dk_openmask = of->sc_dk.dk_copenmask | of->sc_dk.dk_bopenmask;
2685804d3f6Sws 
2692747d6c8Sthorpej #ifdef	FIRMWORKSBUGS
2705804d3f6Sws 	/*
2715804d3f6Sws 	 * This is a hack to get the firmware to flush its buffers.
2725804d3f6Sws 	 */
2735804d3f6Sws 	OF_seek(of->sc_ihandle, 0);
2745804d3f6Sws #endif
2755804d3f6Sws 	if (!of->sc_dk.dk_openmask) {
2765804d3f6Sws 		OF_close(of->sc_ihandle);
2775804d3f6Sws 		of->sc_ihandle = 0;
2785804d3f6Sws 	}
2795804d3f6Sws 
280b5a9ff06Sad 	mutex_exit(&of->sc_dk.dk_openlock);
2815804d3f6Sws 	return 0;
2825804d3f6Sws }
2835804d3f6Sws 
2845804d3f6Sws void
ofdisk_strategy(struct buf * bp)2855911b9a6Smatt ofdisk_strategy(struct buf *bp)
2865804d3f6Sws {
2874795ee76Scegger 	struct ofdisk_softc *of =
2884795ee76Scegger 		device_lookup_private(&ofdisk_cd, DISKUNIT(bp->b_dev));
2895804d3f6Sws 	struct partition *p;
2905804d3f6Sws 	u_quad_t off;
2915804d3f6Sws 	int read;
2925804d3f6Sws 	int (*OF_io)(int, void *, int);
2935804d3f6Sws 	daddr_t blkno = bp->b_blkno;
2945804d3f6Sws 
2955804d3f6Sws 	bp->b_resid = 0;
2965804d3f6Sws 	if (bp->b_bcount == 0)
2975804d3f6Sws 		goto done;
2985804d3f6Sws 
2993ee7e97fShe 	OF_io = bp->b_flags & B_READ ? OF_read :
3003ee7e97fShe 		(int(*)(int, void*, int))OF_write;
3015804d3f6Sws 
3025804d3f6Sws 	if (DISKPART(bp->b_dev) != RAW_PART) {
303e43fecb2Sthorpej 		if (bounds_check_with_label(&of->sc_dk, bp, 0) <= 0) {
3045804d3f6Sws 			bp->b_resid = bp->b_bcount;
3055804d3f6Sws 			goto done;
3065804d3f6Sws 		}
3075804d3f6Sws 		p = &of->sc_dk.dk_label->d_partitions[DISKPART(bp->b_dev)];
3085804d3f6Sws 		blkno = bp->b_blkno + p->p_offset;
3095804d3f6Sws 	}
3105804d3f6Sws 
3115804d3f6Sws 	disk_busy(&of->sc_dk);
3125804d3f6Sws 
3135804d3f6Sws 	off = (u_quad_t)blkno * DEV_BSIZE;
3145804d3f6Sws 	read = -1;
3155804d3f6Sws 	do {
3165804d3f6Sws 		if (OF_seek(of->sc_ihandle, off) < 0)
3175804d3f6Sws 			break;
3185804d3f6Sws 		read = OF_io(of->sc_ihandle, bp->b_data, bp->b_bcount);
3195804d3f6Sws 	} while (read == -2);
3202747d6c8Sthorpej 
3215804d3f6Sws 	if (read < 0) {
3225804d3f6Sws 		bp->b_error = EIO;
3235804d3f6Sws 		bp->b_resid = bp->b_bcount;
3245804d3f6Sws 	} else
3255804d3f6Sws 		bp->b_resid = bp->b_bcount - read;
3265804d3f6Sws 
327603098b9Smrg 	disk_unbusy(&of->sc_dk, bp->b_bcount - bp->b_resid,
328603098b9Smrg 	    (bp->b_flags & B_READ));
3295804d3f6Sws 
3305804d3f6Sws done:
3315804d3f6Sws 	biodone(bp);
3325804d3f6Sws }
3335804d3f6Sws 
3345804d3f6Sws static void
ofminphys(struct buf * bp)3355911b9a6Smatt ofminphys(struct buf *bp)
3365804d3f6Sws {
3374795ee76Scegger 	struct ofdisk_softc *of =
3384795ee76Scegger 		device_lookup_private(&ofdisk_cd, DISKUNIT(bp->b_dev));
3395804d3f6Sws 
3405804d3f6Sws 	if (bp->b_bcount > of->max_transfer)
3415804d3f6Sws 		bp->b_bcount = of->max_transfer;
3425804d3f6Sws }
3435804d3f6Sws 
3445804d3f6Sws int
ofdisk_read(dev_t dev,struct uio * uio,int flags)3455911b9a6Smatt ofdisk_read(dev_t dev, struct uio *uio, int flags)
3465804d3f6Sws {
347310f6fefSmycroft 	return physio(ofdisk_strategy, NULL, dev, B_READ, ofminphys, uio);
3485804d3f6Sws }
3495804d3f6Sws 
3505804d3f6Sws int
ofdisk_write(dev_t dev,struct uio * uio,int flags)3515911b9a6Smatt ofdisk_write(dev_t dev, struct uio *uio, int flags)
3525804d3f6Sws {
353310f6fefSmycroft 	return physio(ofdisk_strategy, NULL, dev, B_WRITE, ofminphys, uio);
3545804d3f6Sws }
3555804d3f6Sws 
3565804d3f6Sws int
ofdisk_ioctl(dev_t dev,u_long cmd,void * data,int flag,struct lwp * l)35753524e44Schristos ofdisk_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
3585804d3f6Sws {
3594795ee76Scegger 	struct ofdisk_softc *of =
3604795ee76Scegger 		device_lookup_private(&ofdisk_cd, DISKUNIT(dev));
3615804d3f6Sws 	int error;
362e2d1c1f9Sfvdl #ifdef __HAVE_OLD_DISKLABEL
363e2d1c1f9Sfvdl 	struct disklabel newlabel;
364e2d1c1f9Sfvdl #endif
3653be6bb24Schristos 	/* XXX: Why not allow wedges on floppy? */
3663be6bb24Schristos 	switch (cmd) {
3673be6bb24Schristos 	case DIOCDWEDGE:
3683be6bb24Schristos 	case DIOCAWEDGE:
3693be6bb24Schristos 	case DIOCLWEDGES:
370ad4912fdSmartin 	case DIOCRMWEDGES:
3713be6bb24Schristos 	case DIOCMWEDGES:
3723be6bb24Schristos 		if (OFDISK_FLOPPY_P(of))
3733be6bb24Schristos 			return ENOTTY;
3743be6bb24Schristos 	}
3753be6bb24Schristos 
376c60db2e9Schristos 	error = disk_ioctl(&of->sc_dk, dev, cmd, data, flag, l);
3773be6bb24Schristos 	if (error != EPASSTHROUGH)
3783be6bb24Schristos 		return error;
3795804d3f6Sws 
3805804d3f6Sws 	switch (cmd) {
3815804d3f6Sws 	case DIOCWDINFO:
3825804d3f6Sws 	case DIOCSDINFO:
383e2d1c1f9Sfvdl #ifdef __HAVE_OLD_DISKLABEL
384e2d1c1f9Sfvdl 	case ODIOCWDINFO:
385e2d1c1f9Sfvdl 	case ODIOCSDINFO:
386e2d1c1f9Sfvdl #endif
387e2d1c1f9Sfvdl 	{
388e2d1c1f9Sfvdl 		struct disklabel *lp;
389e2d1c1f9Sfvdl 
390e2d1c1f9Sfvdl #ifdef __HAVE_OLD_DISKLABEL
391e2d1c1f9Sfvdl 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
392e2d1c1f9Sfvdl 			memset(&newlabel, 0, sizeof newlabel);
393e2d1c1f9Sfvdl 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
394e2d1c1f9Sfvdl 			lp = &newlabel;
395e2d1c1f9Sfvdl 		} else
396e2d1c1f9Sfvdl #endif
397e2d1c1f9Sfvdl 		lp = (struct disklabel *)data;
398e2d1c1f9Sfvdl 
3995804d3f6Sws 		if ((flag & FWRITE) == 0)
4005804d3f6Sws 			return EBADF;
4015804d3f6Sws 
402b5a9ff06Sad 		mutex_enter(&of->sc_dk.dk_openlock);
403281037afSthorpej 
4045804d3f6Sws 		error = setdisklabel(of->sc_dk.dk_label,
405e2d1c1f9Sfvdl 		    lp, /*of->sc_dk.dk_openmask */0,
4065804d3f6Sws 		    of->sc_dk.dk_cpulabel);
407*50b1e3ebSthorpej 		if (error == 0 && (cmd == DIOCWDINFO
408e2d1c1f9Sfvdl #ifdef __HAVE_OLD_DISKLABEL
409*50b1e3ebSthorpej 		    || cmd == ODIOCWDINFO
410e2d1c1f9Sfvdl #endif
411*50b1e3ebSthorpej 		    ))
4125804d3f6Sws 			error = writedisklabel(MAKEDISKDEV(major(dev),
413310f6fefSmycroft 			    DISKUNIT(dev), RAW_PART), ofdisk_strategy,
4142747d6c8Sthorpej 			    of->sc_dk.dk_label, of->sc_dk.dk_cpulabel);
4155804d3f6Sws 
416b5a9ff06Sad 		mutex_exit(&of->sc_dk.dk_openlock);
417281037afSthorpej 
4185804d3f6Sws 		return error;
419e2d1c1f9Sfvdl 	}
4205e055931Sthorpej 
4215e055931Sthorpej 	case DIOCGDEFLABEL:
422310f6fefSmycroft 		ofdisk_getdefaultlabel(of, (struct disklabel *)data);
4235e055931Sthorpej 		return 0;
424e2d1c1f9Sfvdl #ifdef __HAVE_OLD_DISKLABEL
425*50b1e3ebSthorpej 	case ODIOCGDEFLABEL:
426e2d1c1f9Sfvdl 		ofdisk_getdefaultlabel(of, &newlabel);
427e2d1c1f9Sfvdl 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
428d040bd59Sfvdl 			return ENOTTY;
429e2d1c1f9Sfvdl 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
430e2d1c1f9Sfvdl 		return 0;
431e2d1c1f9Sfvdl #endif
4325e055931Sthorpej 
4335804d3f6Sws 	default:
4345804d3f6Sws 		return ENOTTY;
4355804d3f6Sws 	}
4365804d3f6Sws }
4375804d3f6Sws 
4385804d3f6Sws int
ofdisk_dump(dev_t dev,daddr_t blkno,void * va,size_t size)43953524e44Schristos ofdisk_dump(dev_t dev, daddr_t blkno, void *va, size_t size)
4405804d3f6Sws {
4415804d3f6Sws 	return EINVAL;
4425804d3f6Sws }
4435804d3f6Sws 
4445804d3f6Sws int
ofdisk_size(dev_t dev)4455911b9a6Smatt ofdisk_size(dev_t dev)
4465804d3f6Sws {
447310f6fefSmycroft 	struct ofdisk_softc *of;
448a9710d48Sthorpej 	struct disklabel *lp;
4494795ee76Scegger 	int size, part, omask;
4505804d3f6Sws 
4514795ee76Scegger 	of = device_lookup_private(&ofdisk_cd, DISKUNIT(dev));
4524795ee76Scegger 	if (of == NULL)
4534795ee76Scegger 		return ENXIO;
454a9710d48Sthorpej 
4555804d3f6Sws 	part = DISKPART(dev);
456a9710d48Sthorpej 	omask = of->sc_dk.dk_openmask & (1 << part);
4576dce360fSthorpej 	lp = of->sc_dk.dk_label;
458a9710d48Sthorpej 
45995e1ffb1Schristos 	if (omask == 0 && ofdisk_open(dev, 0, S_IFBLK, curlwp) != 0)
460a9710d48Sthorpej 		return -1;
461a9710d48Sthorpej 
462a9710d48Sthorpej 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
4635804d3f6Sws 		size = -1;
4645804d3f6Sws 	else
465a9710d48Sthorpej 		size = lp->d_partitions[part].p_size *
466a9710d48Sthorpej 		    (lp->d_secsize / DEV_BSIZE);
467a9710d48Sthorpej 
46895e1ffb1Schristos 	if (omask == 0 && ofdisk_close(dev, 0, S_IFBLK, curlwp) != 0)
4695804d3f6Sws 		return -1;
470a9710d48Sthorpej 
4715804d3f6Sws 	return size;
4725804d3f6Sws }
4735e055931Sthorpej 
4745e055931Sthorpej void
ofdisk_getdefaultlabel(struct ofdisk_softc * of,struct disklabel * lp)4755911b9a6Smatt ofdisk_getdefaultlabel(struct ofdisk_softc *of, struct disklabel *lp)
4765e055931Sthorpej {
4775e055931Sthorpej 
4785911b9a6Smatt 	memset(lp, 0, sizeof *lp);
4795e055931Sthorpej 
4805e055931Sthorpej 	/*
4815e055931Sthorpej 	 * XXX Firmware bug?  Asking for block size gives a
482658ce04aSchs 	 * XXX ridiculous number!  So we use what the boot program
4835e055931Sthorpej 	 * XXX uses.
4845e055931Sthorpej 	 */
4855e055931Sthorpej 	lp->d_secsize = DEV_BSIZE;
4865e055931Sthorpej 
4875e055931Sthorpej 	lp->d_secperunit = OF_call_method_1("#blocks",
4885e055931Sthorpej 	    of->sc_ihandle, 0);
4895e055931Sthorpej 	if (lp->d_secperunit == (u_int32_t)-1)
4905e055931Sthorpej 		lp->d_secperunit = 0x7fffffff;
4915e055931Sthorpej 
4925e055931Sthorpej 	lp->d_secpercyl = 1;
4935e055931Sthorpej 	lp->d_nsectors = 1;
4945e055931Sthorpej 	lp->d_ntracks = 1;
4955e055931Sthorpej 	lp->d_ncylinders = lp->d_secperunit;
4965e055931Sthorpej 
4975e055931Sthorpej 	lp->d_partitions[RAW_PART].p_offset = 0;
4985e055931Sthorpej 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
4995e055931Sthorpej 	lp->d_npartitions = RAW_PART + 1;
5005e055931Sthorpej 
5015e055931Sthorpej 	lp->d_magic = DISKMAGIC;
5025e055931Sthorpej 	lp->d_magic2 = DISKMAGIC;
5035e055931Sthorpej 	lp->d_checksum = dkcksum(lp);
5045e055931Sthorpej }
5055e055931Sthorpej 
5065e055931Sthorpej void
ofdisk_getdisklabel(dev_t dev)5074795ee76Scegger ofdisk_getdisklabel(dev_t dev)
5085e055931Sthorpej {
5095e055931Sthorpej 	int unit = DISKUNIT(dev);
5104795ee76Scegger 	struct ofdisk_softc *of =
5114795ee76Scegger 		device_lookup_private(&ofdisk_cd, unit);
5125e055931Sthorpej 	struct disklabel *lp = of->sc_dk.dk_label;
513d91455ceSdsl 	const char *errmes;
5145e055931Sthorpej 	int l;
5155e055931Sthorpej 
516310f6fefSmycroft 	ofdisk_getdefaultlabel(of, lp);
5175e055931Sthorpej 
5185e055931Sthorpej 	/*
5195e055931Sthorpej 	 * Don't read the disklabel on a floppy; simply
5205e055931Sthorpej 	 * assign all partitions the same size/offset as
5215e055931Sthorpej 	 * RAW_PART.  (This is essentially what the ISA
5225e055931Sthorpej 	 * floppy driver does, but we don't deal with
5235e055931Sthorpej 	 * density stuff.)
5245e055931Sthorpej 	 */
525281037afSthorpej 	if (OFDISK_FLOPPY_P(of)) {
5265e055931Sthorpej 		lp->d_npartitions = MAXPARTITIONS;
5275e055931Sthorpej 		for (l = 0; l < lp->d_npartitions; l++) {
5285e055931Sthorpej 			if (l == RAW_PART)
5295e055931Sthorpej 				continue;
5305e055931Sthorpej 			/* struct copy */
5315e055931Sthorpej 			lp->d_partitions[l] =
5325e055931Sthorpej 			    lp->d_partitions[RAW_PART];
5335e055931Sthorpej 		}
5345e055931Sthorpej 		lp->d_checksum = dkcksum(lp);
5355e055931Sthorpej 	} else {
5365e055931Sthorpej 		errmes = readdisklabel(MAKEDISKDEV(major(dev),
537310f6fefSmycroft 		    unit, RAW_PART), ofdisk_strategy, lp,
5385e055931Sthorpej 		    of->sc_dk.dk_cpulabel);
5395e055931Sthorpej 		if (errmes != NULL)
5409af887f0Smrg 			printf("%s: %s\n", device_xname(of->sc_dev), errmes);
5415e055931Sthorpej 	}
5425e055931Sthorpej }
543