xref: /onnv-gate/usr/src/uts/common/io/fd.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * Floppy Disk driver
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  * Set CMOS feature:
35*0Sstevel@tonic-gate  *	CMOS_CONF_MEM:	CMOS memory contains configuration info
36*0Sstevel@tonic-gate  */
37*0Sstevel@tonic-gate #define	CMOS_CONF_MEM
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include <sys/types.h>
40*0Sstevel@tonic-gate #include <sys/param.h>
41*0Sstevel@tonic-gate #include <sys/systm.h>
42*0Sstevel@tonic-gate #include <sys/buf.h>
43*0Sstevel@tonic-gate #include <sys/file.h>
44*0Sstevel@tonic-gate #include <sys/open.h>
45*0Sstevel@tonic-gate #include <sys/ioctl.h>
46*0Sstevel@tonic-gate #include <sys/uio.h>
47*0Sstevel@tonic-gate #include <sys/conf.h>
48*0Sstevel@tonic-gate #include <sys/stat.h>
49*0Sstevel@tonic-gate #include <sys/autoconf.h>
50*0Sstevel@tonic-gate #include <sys/vtoc.h>
51*0Sstevel@tonic-gate #include <sys/dkio.h>
52*0Sstevel@tonic-gate #include <sys/ddi.h>
53*0Sstevel@tonic-gate #include <sys/sunddi.h>
54*0Sstevel@tonic-gate #include <sys/kstat.h>
55*0Sstevel@tonic-gate #include <sys/kmem.h>
56*0Sstevel@tonic-gate #include <sys/ddidmareq.h>
57*0Sstevel@tonic-gate #include <sys/fdio.h>
58*0Sstevel@tonic-gate #include <sys/fdc.h>
59*0Sstevel@tonic-gate #include <sys/fd_debug.h>
60*0Sstevel@tonic-gate #include <sys/fdmedia.h>
61*0Sstevel@tonic-gate #include <sys/debug.h>
62*0Sstevel@tonic-gate #include <sys/modctl.h>
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate /*
65*0Sstevel@tonic-gate  * Local Function Prototypes
66*0Sstevel@tonic-gate  */
67*0Sstevel@tonic-gate static int fd_unit_is_open(struct fdisk *);
68*0Sstevel@tonic-gate static int fdgetlabel(struct fcu_obj *, int);
69*0Sstevel@tonic-gate static void fdstart(struct fcu_obj *);
70*0Sstevel@tonic-gate static int fd_build_label_vtoc(struct fcu_obj *, struct fdisk *,
71*0Sstevel@tonic-gate     struct vtoc *, struct dk_label *);
72*0Sstevel@tonic-gate static void fd_build_user_vtoc(struct fcu_obj *, struct fdisk *,
73*0Sstevel@tonic-gate     struct vtoc *);
74*0Sstevel@tonic-gate static int fd_rawioctl(struct fcu_obj *, int, caddr_t, int);
75*0Sstevel@tonic-gate static void fd_media_watch(void *);
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate static int fd_open(dev_t *, int, int, cred_t *);
78*0Sstevel@tonic-gate static int fd_close(dev_t, int, int, cred_t *);
79*0Sstevel@tonic-gate static int fd_strategy(struct buf *);
80*0Sstevel@tonic-gate static int fd_read(dev_t, struct uio *, cred_t *);
81*0Sstevel@tonic-gate static int fd_write(dev_t, struct uio *, cred_t *);
82*0Sstevel@tonic-gate static int fd_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
83*0Sstevel@tonic-gate static int fd_prop_op(dev_t, dev_info_t *, ddi_prop_op_t, int, char *,
84*0Sstevel@tonic-gate     caddr_t, int *);
85*0Sstevel@tonic-gate static int fd_check_media(dev_t dev, enum dkio_state state);
86*0Sstevel@tonic-gate static int fd_get_media_info(struct fcu_obj *fjp, caddr_t buf, int flag);
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate static struct cb_ops fd_cb_ops = {
89*0Sstevel@tonic-gate 	fd_open,		/* open */
90*0Sstevel@tonic-gate 	fd_close,		/* close */
91*0Sstevel@tonic-gate 	fd_strategy,		/* strategy */
92*0Sstevel@tonic-gate 	nodev,			/* print */
93*0Sstevel@tonic-gate 	nodev,			/* dump */
94*0Sstevel@tonic-gate 	fd_read,		/* read */
95*0Sstevel@tonic-gate 	fd_write,		/* write */
96*0Sstevel@tonic-gate 	fd_ioctl,		/* ioctl */
97*0Sstevel@tonic-gate 	nodev,			/* devmap */
98*0Sstevel@tonic-gate 	nodev,			/* mmap */
99*0Sstevel@tonic-gate 	nodev,			/* segmap */
100*0Sstevel@tonic-gate 	nochpoll,		/* poll */
101*0Sstevel@tonic-gate 	fd_prop_op,		/* cb_prop_op */
102*0Sstevel@tonic-gate 	0,			/* streamtab  */
103*0Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
104*0Sstevel@tonic-gate };
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate static int fd_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
107*0Sstevel@tonic-gate static int fd_probe(dev_info_t *);
108*0Sstevel@tonic-gate static int fd_attach(dev_info_t *, ddi_attach_cmd_t);
109*0Sstevel@tonic-gate static int fd_detach(dev_info_t *, ddi_detach_cmd_t);
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate static struct dev_ops fd_ops = {
112*0Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
113*0Sstevel@tonic-gate 	0,			/* refcnt  */
114*0Sstevel@tonic-gate 	fd_getinfo,		/* getinfo */
115*0Sstevel@tonic-gate 	nulldev,		/* identify */
116*0Sstevel@tonic-gate 	fd_probe,		/* probe */
117*0Sstevel@tonic-gate 	fd_attach,		/* attach */
118*0Sstevel@tonic-gate 	fd_detach,		/* detach */
119*0Sstevel@tonic-gate 	nodev,			/* reset */
120*0Sstevel@tonic-gate 	&fd_cb_ops,		/* driver operations */
121*0Sstevel@tonic-gate 	(struct bus_ops *)0	/* bus operations */
122*0Sstevel@tonic-gate };
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate /*
126*0Sstevel@tonic-gate  * static data
127*0Sstevel@tonic-gate  */
128*0Sstevel@tonic-gate static void *fd_state_head;		/* opaque handle top of state structs */
129*0Sstevel@tonic-gate static int fd_check_media_time = 5000000;	/* 5 second state check */
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate /*
132*0Sstevel@tonic-gate  * error handling
133*0Sstevel@tonic-gate  *
134*0Sstevel@tonic-gate  * for debugging,
135*0Sstevel@tonic-gate  *		set fderrlevel to 1
136*0Sstevel@tonic-gate  *		set fderrmask  to 224  or 644
137*0Sstevel@tonic-gate  */
138*0Sstevel@tonic-gate #ifdef DEBUG
139*0Sstevel@tonic-gate static uint_t fderrmask = FDEM_ALL;
140*0Sstevel@tonic-gate #endif
141*0Sstevel@tonic-gate static int fderrlevel = 5;
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate #define	KIOSP	KSTAT_IO_PTR(fdp->d_iostat)
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate static struct driver_minor_data {
146*0Sstevel@tonic-gate 	char	*name;
147*0Sstevel@tonic-gate 	int	minor;
148*0Sstevel@tonic-gate 	int	type;
149*0Sstevel@tonic-gate } fd_minor [] = {
150*0Sstevel@tonic-gate 	{ "a", 0, S_IFBLK},
151*0Sstevel@tonic-gate 	{ "b", 1, S_IFBLK},
152*0Sstevel@tonic-gate 	{ "c", 2, S_IFBLK},
153*0Sstevel@tonic-gate 	{ "a,raw", 0, S_IFCHR},
154*0Sstevel@tonic-gate 	{ "b,raw", 1, S_IFCHR},
155*0Sstevel@tonic-gate 	{ "c,raw", 2, S_IFCHR},
156*0Sstevel@tonic-gate 	{0}
157*0Sstevel@tonic-gate };
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate static struct modldrv modldrv = {
160*0Sstevel@tonic-gate 	&mod_driverops,		/* Type of module. This one is a driver */
161*0Sstevel@tonic-gate 	"Floppy Disk driver %I%",	/* Name of the module. */
162*0Sstevel@tonic-gate 	&fd_ops,		/* driver ops */
163*0Sstevel@tonic-gate };
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
166*0Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
167*0Sstevel@tonic-gate };
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate int
171*0Sstevel@tonic-gate _init(void)
172*0Sstevel@tonic-gate {
173*0Sstevel@tonic-gate 	int retval;
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	if ((retval = ddi_soft_state_init(&fd_state_head,
176*0Sstevel@tonic-gate 	    sizeof (struct fdisk) + sizeof (struct fd_drive) +
177*0Sstevel@tonic-gate 	    sizeof (struct fd_char) + sizeof (struct fdattr), 0)) != 0)
178*0Sstevel@tonic-gate 		return (retval);
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	if ((retval = mod_install(&modlinkage)) != 0)
181*0Sstevel@tonic-gate 		ddi_soft_state_fini(&fd_state_head);
182*0Sstevel@tonic-gate 	return (retval);
183*0Sstevel@tonic-gate }
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate int
186*0Sstevel@tonic-gate _fini(void)
187*0Sstevel@tonic-gate {
188*0Sstevel@tonic-gate 	int retval;
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	if ((retval = mod_remove(&modlinkage)) != 0)
191*0Sstevel@tonic-gate 		return (retval);
192*0Sstevel@tonic-gate 	ddi_soft_state_fini(&fd_state_head);
193*0Sstevel@tonic-gate 	return (retval);
194*0Sstevel@tonic-gate }
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate int
197*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
198*0Sstevel@tonic-gate {
199*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
200*0Sstevel@tonic-gate }
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate static int
204*0Sstevel@tonic-gate fd_getdrive(dev_t dev, struct fcu_obj **fjpp, struct fdisk **fdpp)
205*0Sstevel@tonic-gate {
206*0Sstevel@tonic-gate 	if (fdpp) {
207*0Sstevel@tonic-gate 		*fdpp = ddi_get_soft_state(fd_state_head, DRIVE(dev));
208*0Sstevel@tonic-gate 		if (*fdpp && fjpp) {
209*0Sstevel@tonic-gate 			*fjpp = (*fdpp)->d_obj;
210*0Sstevel@tonic-gate 			if (*fjpp)
211*0Sstevel@tonic-gate 				return ((*fjpp)->fj_unit);
212*0Sstevel@tonic-gate 		}
213*0Sstevel@tonic-gate 	}
214*0Sstevel@tonic-gate 	return (-1);
215*0Sstevel@tonic-gate }
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate /*ARGSUSED*/
218*0Sstevel@tonic-gate static int
219*0Sstevel@tonic-gate fd_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
220*0Sstevel@tonic-gate {
221*0Sstevel@tonic-gate 	dev_t dev = (dev_t)arg;
222*0Sstevel@tonic-gate 	struct fcu_obj *fjp = NULL;
223*0Sstevel@tonic-gate 	struct fdisk *fdp = NULL;
224*0Sstevel@tonic-gate 	int rval;
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	switch (cmd) {
227*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
228*0Sstevel@tonic-gate 		(void) fd_getdrive(dev, &fjp, &fdp);
229*0Sstevel@tonic-gate 		/*
230*0Sstevel@tonic-gate 		 * Ignoring return value because success is checked by
231*0Sstevel@tonic-gate 		 * verifying fjp and fdp and returned unit value is not used.
232*0Sstevel@tonic-gate 		 */
233*0Sstevel@tonic-gate 		if (fjp && fdp) {
234*0Sstevel@tonic-gate 			*result = fjp->fj_dip;
235*0Sstevel@tonic-gate 			rval = DDI_SUCCESS;
236*0Sstevel@tonic-gate 		} else
237*0Sstevel@tonic-gate 			rval = DDI_FAILURE;
238*0Sstevel@tonic-gate 		break;
239*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
240*0Sstevel@tonic-gate 		*result = (void *)(uintptr_t)DRIVE(dev);
241*0Sstevel@tonic-gate 		rval = DDI_SUCCESS;
242*0Sstevel@tonic-gate 		break;
243*0Sstevel@tonic-gate 	default:
244*0Sstevel@tonic-gate 		rval = DDI_FAILURE;
245*0Sstevel@tonic-gate 	}
246*0Sstevel@tonic-gate 	return (rval);
247*0Sstevel@tonic-gate }
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate #ifdef CMOS_CONF_MEM
250*0Sstevel@tonic-gate #define	CMOS_ADDR	0x70
251*0Sstevel@tonic-gate #define	CMOS_DATA	0x71
252*0Sstevel@tonic-gate #define	CMOS_FDRV	0x10
253*0Sstevel@tonic-gate #endif	/* CMOS_CONF_MEM */
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate static int
256*0Sstevel@tonic-gate fd_probe(dev_info_t *dip)
257*0Sstevel@tonic-gate {
258*0Sstevel@tonic-gate #ifdef CMOS_CONF_MEM
259*0Sstevel@tonic-gate 	int cmos;
260*0Sstevel@tonic-gate 	int drive_type;
261*0Sstevel@tonic-gate #endif	/* CMOS_CONF_MEM */
262*0Sstevel@tonic-gate 	int debug[2];
263*0Sstevel@tonic-gate 	int drive_size;
264*0Sstevel@tonic-gate 	int len;
265*0Sstevel@tonic-gate 	int unit_num;
266*0Sstevel@tonic-gate 	char density[8];
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 	len = sizeof (debug);
269*0Sstevel@tonic-gate 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
270*0Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "debug", (caddr_t)debug, &len) ==
271*0Sstevel@tonic-gate 	    DDI_PROP_SUCCESS) {
272*0Sstevel@tonic-gate 		fderrlevel = debug[0];
273*0Sstevel@tonic-gate #ifdef DEBUG
274*0Sstevel@tonic-gate 		fderrmask = (uint_t)debug[1];
275*0Sstevel@tonic-gate #endif
276*0Sstevel@tonic-gate 	}
277*0Sstevel@tonic-gate 	len = sizeof (unit_num);
278*0Sstevel@tonic-gate 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
279*0Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "unit", (caddr_t)&unit_num, &len) !=
280*0Sstevel@tonic-gate 	    DDI_PROP_SUCCESS) {
281*0Sstevel@tonic-gate 		FDERRPRINT(FDEP_L3, FDEM_ATTA,
282*0Sstevel@tonic-gate 		    (CE_WARN, "fd_probe failed: dip %p", (void *)dip));
283*0Sstevel@tonic-gate 		return (DDI_PROBE_FAILURE);
284*0Sstevel@tonic-gate 	}
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate #ifdef CMOS_CONF_MEM
287*0Sstevel@tonic-gate 	/* get the cmos memory values quick and dirty */
288*0Sstevel@tonic-gate 	outb(CMOS_ADDR, CMOS_FDRV);
289*0Sstevel@tonic-gate 	cmos = drive_type = (int)inb(CMOS_DATA);
290*0Sstevel@tonic-gate #endif	/* CMOS_CONF_MEM */
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	switch (unit_num) {
293*0Sstevel@tonic-gate #ifdef CMOS_CONF_MEM
294*0Sstevel@tonic-gate 	case 0:
295*0Sstevel@tonic-gate 		drive_type = drive_type >> 4;
296*0Sstevel@tonic-gate 		/* FALLTHROUGH */
297*0Sstevel@tonic-gate 	case 1:
298*0Sstevel@tonic-gate 		if (cmos && (drive_type & 0x0F)) {
299*0Sstevel@tonic-gate 			break;
300*0Sstevel@tonic-gate 		}
301*0Sstevel@tonic-gate 		/*
302*0Sstevel@tonic-gate 		 * Some enhanced floppy-disk controller adaptor cards
303*0Sstevel@tonic-gate 		 * require NO drives defined in the CMOS configuration
304*0Sstevel@tonic-gate 		 * memory.
305*0Sstevel@tonic-gate 		 * So fall through
306*0Sstevel@tonic-gate 		 */
307*0Sstevel@tonic-gate #endif	/* CMOS_CONF_MEM */
308*0Sstevel@tonic-gate 	default:		/* need to check conf file */
309*0Sstevel@tonic-gate 		len = sizeof (density);
310*0Sstevel@tonic-gate 		if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
311*0Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "density", (caddr_t)&density, &len) !=
312*0Sstevel@tonic-gate 		    DDI_PROP_SUCCESS) {
313*0Sstevel@tonic-gate 			FDERRPRINT(FDEP_L3, FDEM_ATTA,
314*0Sstevel@tonic-gate 			    (CE_WARN,
315*0Sstevel@tonic-gate 			    "fd_probe failed density: dip %p unit %d",
316*0Sstevel@tonic-gate 			    (void *)dip, unit_num));
317*0Sstevel@tonic-gate 			return (DDI_PROBE_FAILURE);
318*0Sstevel@tonic-gate 		}
319*0Sstevel@tonic-gate 		len = sizeof (drive_size);
320*0Sstevel@tonic-gate 		if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
321*0Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "size", (caddr_t)&drive_size, &len) !=
322*0Sstevel@tonic-gate 		    DDI_PROP_SUCCESS) {
323*0Sstevel@tonic-gate 			FDERRPRINT(FDEP_L3, FDEM_ATTA,
324*0Sstevel@tonic-gate 			    (CE_WARN, "fd_probe failed size: dip %p unit %d",
325*0Sstevel@tonic-gate 			    (void *)dip, unit_num));
326*0Sstevel@tonic-gate 			return (DDI_PROBE_FAILURE);
327*0Sstevel@tonic-gate 		}
328*0Sstevel@tonic-gate 	}
329*0Sstevel@tonic-gate 	FDERRPRINT(FDEP_L3, FDEM_ATTA,
330*0Sstevel@tonic-gate 	    (CE_WARN, "fd_probe dip %p unit %d", (void *)dip, unit_num));
331*0Sstevel@tonic-gate 	return (DDI_PROBE_SUCCESS);
332*0Sstevel@tonic-gate }
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate /* ARGSUSED */
336*0Sstevel@tonic-gate static int
337*0Sstevel@tonic-gate fd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
338*0Sstevel@tonic-gate {
339*0Sstevel@tonic-gate 	struct fcu_obj *fjp;
340*0Sstevel@tonic-gate 	struct fdisk *fdp;
341*0Sstevel@tonic-gate 	struct driver_minor_data *dmdp;
342*0Sstevel@tonic-gate 	int mode_3D;
343*0Sstevel@tonic-gate 	int drive_num, drive_size, drive_type;
344*0Sstevel@tonic-gate #ifdef CMOS_CONF_MEM
345*0Sstevel@tonic-gate 	int cmos;
346*0Sstevel@tonic-gate #endif	/* CMOS_CONF_MEM */
347*0Sstevel@tonic-gate 	int len, sig_minor;
348*0Sstevel@tonic-gate 	int unit_num;
349*0Sstevel@tonic-gate 	char density[8];
350*0Sstevel@tonic-gate 	char name[MAXNAMELEN];
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	switch (cmd) {
353*0Sstevel@tonic-gate 	case DDI_ATTACH:
354*0Sstevel@tonic-gate 		len = sizeof (unit_num);
355*0Sstevel@tonic-gate 		if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
356*0Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "unit", (caddr_t)&unit_num, &len) !=
357*0Sstevel@tonic-gate 		    DDI_PROP_SUCCESS) {
358*0Sstevel@tonic-gate 			FDERRPRINT(FDEP_L3, FDEM_ATTA,
359*0Sstevel@tonic-gate 			    (CE_WARN, "fd_attach failed: dip %p", (void *)dip));
360*0Sstevel@tonic-gate 			return (DDI_FAILURE);
361*0Sstevel@tonic-gate 		}
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate #ifdef CMOS_CONF_MEM
364*0Sstevel@tonic-gate 		outb(CMOS_ADDR, CMOS_FDRV);
365*0Sstevel@tonic-gate 		cmos = drive_type = (int)inb(CMOS_DATA);
366*0Sstevel@tonic-gate #endif	/* CMOS_CONF_MEM */
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 		switch (unit_num) {
369*0Sstevel@tonic-gate #ifdef CMOS_CONF_MEM
370*0Sstevel@tonic-gate 		case 0:
371*0Sstevel@tonic-gate 			drive_type = drive_type >> 4;
372*0Sstevel@tonic-gate 			/* FALLTHROUGH */
373*0Sstevel@tonic-gate 		case 1:
374*0Sstevel@tonic-gate 			drive_type = drive_type & 0x0F;
375*0Sstevel@tonic-gate 			if (cmos)
376*0Sstevel@tonic-gate 				break;
377*0Sstevel@tonic-gate 			/*
378*0Sstevel@tonic-gate 			 * Some enhanced floppy-disk controller adaptor cards
379*0Sstevel@tonic-gate 			 * require NO drives defined in the CMOS configuration
380*0Sstevel@tonic-gate 			 * memory.
381*0Sstevel@tonic-gate 			 * So fall through
382*0Sstevel@tonic-gate 			 */
383*0Sstevel@tonic-gate #endif	/* CMOS_CONF_MEM */
384*0Sstevel@tonic-gate 		default:		/* need to check .conf file */
385*0Sstevel@tonic-gate 			drive_type = 0;
386*0Sstevel@tonic-gate 			len = sizeof (density);
387*0Sstevel@tonic-gate 			if (ddi_prop_op(DDI_DEV_T_ANY, dip,
388*0Sstevel@tonic-gate 			    PROP_LEN_AND_VAL_BUF, DDI_PROP_DONTPASS, "density",
389*0Sstevel@tonic-gate 			    (caddr_t)&density, &len) != DDI_PROP_SUCCESS)
390*0Sstevel@tonic-gate 				density[0] = '\0';
391*0Sstevel@tonic-gate 			len = sizeof (drive_size);
392*0Sstevel@tonic-gate 			if (ddi_prop_op(DDI_DEV_T_ANY, dip,
393*0Sstevel@tonic-gate 			    PROP_LEN_AND_VAL_BUF, DDI_PROP_DONTPASS, "size",
394*0Sstevel@tonic-gate 			    (caddr_t)&drive_size, &len) != DDI_PROP_SUCCESS)
395*0Sstevel@tonic-gate 				drive_size = 0;
396*0Sstevel@tonic-gate 			if (strcmp(density, "DSDD") == 0) {
397*0Sstevel@tonic-gate 				if (drive_size == 5)
398*0Sstevel@tonic-gate 					drive_type = 1;
399*0Sstevel@tonic-gate 				else if (drive_size == 3)
400*0Sstevel@tonic-gate 					drive_type = 3;
401*0Sstevel@tonic-gate 			} else if (strcmp(density, "DSHD") == 0) {
402*0Sstevel@tonic-gate 				if (drive_size == 5)
403*0Sstevel@tonic-gate 					drive_type = 2;
404*0Sstevel@tonic-gate 				else if (drive_size == 3)
405*0Sstevel@tonic-gate 					drive_type = 4;
406*0Sstevel@tonic-gate 			} else if (strcmp(density, "DSED") == 0 &&
407*0Sstevel@tonic-gate 			    drive_size == 3) {
408*0Sstevel@tonic-gate 				drive_type = 6;
409*0Sstevel@tonic-gate 			}
410*0Sstevel@tonic-gate 			break;
411*0Sstevel@tonic-gate 		}
412*0Sstevel@tonic-gate 		if (drive_type == 0) {
413*0Sstevel@tonic-gate 			FDERRPRINT(FDEP_L3, FDEM_ATTA,
414*0Sstevel@tonic-gate 			    (CE_WARN, "fd_attach failed type: dip %p unit %d",
415*0Sstevel@tonic-gate 			    (void *)dip, unit_num));
416*0Sstevel@tonic-gate 			return (DDI_FAILURE);
417*0Sstevel@tonic-gate 		}
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate 		drive_num = ddi_get_instance(dip);
420*0Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(fd_state_head, drive_num) != 0)
421*0Sstevel@tonic-gate 			return (DDI_FAILURE);
422*0Sstevel@tonic-gate 		fdp = ddi_get_soft_state(fd_state_head, drive_num);
423*0Sstevel@tonic-gate 		fjp = fdp->d_obj = ddi_get_driver_private(dip);
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate 		mutex_init(&fjp->fj_lock, NULL, MUTEX_DRIVER, *fjp->fj_iblock);
426*0Sstevel@tonic-gate 		sema_init(&fdp->d_ocsem, 1, NULL, SEMA_DRIVER, NULL);
427*0Sstevel@tonic-gate 
428*0Sstevel@tonic-gate 		fjp->fj_drive = (struct fd_drive *)(fdp + 1);
429*0Sstevel@tonic-gate 		fjp->fj_chars = (struct fd_char *)(fjp->fj_drive + 1);
430*0Sstevel@tonic-gate 		fjp->fj_attr = (struct fdattr *)(fjp->fj_chars + 1);
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate 		/*
433*0Sstevel@tonic-gate 		 * set default floppy drive characteristics & geometry
434*0Sstevel@tonic-gate 		 */
435*0Sstevel@tonic-gate 		switch (drive_type) {	/* assume doubled sided */
436*0Sstevel@tonic-gate 		case 2:			/* 5.25 high density */
437*0Sstevel@tonic-gate 			*fjp->fj_drive = dfd_525HD;
438*0Sstevel@tonic-gate 			fdp->d_media = 1<<FMT_5H | 1<<FMT_5D9 | 1<<FMT_5D8 |
439*0Sstevel@tonic-gate 			    1<<FMT_5D4 | 1<<FMT_5D16;
440*0Sstevel@tonic-gate 			fdp->d_deffdtype = fdp->d_curfdtype = FMT_5H;
441*0Sstevel@tonic-gate 			break;
442*0Sstevel@tonic-gate 		case 4:			/* 3.5 high density */
443*0Sstevel@tonic-gate 			*fjp->fj_drive = dfd_350HD;
444*0Sstevel@tonic-gate 			fdp->d_media = 1<<FMT_3H | 1<<FMT_3I | 1<<FMT_3D;
445*0Sstevel@tonic-gate 			len = sizeof (mode_3D);
446*0Sstevel@tonic-gate 			if (ddi_prop_op(DDI_DEV_T_ANY, dip,
447*0Sstevel@tonic-gate 			    PROP_LEN_AND_VAL_BUF, DDI_PROP_DONTPASS, "mode_3D",
448*0Sstevel@tonic-gate 			    (caddr_t)&mode_3D, &len) != DDI_PROP_SUCCESS)
449*0Sstevel@tonic-gate 				mode_3D = 0;
450*0Sstevel@tonic-gate 			if (mode_3D && (fjp->fj_fdc->c_flags & FCFLG_3DMODE))
451*0Sstevel@tonic-gate 				/*
452*0Sstevel@tonic-gate 				 * 3D mode should be enabled only if a dual-
453*0Sstevel@tonic-gate 				 * speed 3.5" high-density drive and a
454*0Sstevel@tonic-gate 				 * supported floppy controller are installed.
455*0Sstevel@tonic-gate 				 */
456*0Sstevel@tonic-gate 				fdp->d_media |= 1 << FMT_3M;
457*0Sstevel@tonic-gate 			fdp->d_deffdtype = fdp->d_curfdtype = FMT_3H;
458*0Sstevel@tonic-gate 			break;
459*0Sstevel@tonic-gate 		case 1:			/* 5.25 double density */
460*0Sstevel@tonic-gate 			*fjp->fj_drive = dfd_525DD;
461*0Sstevel@tonic-gate 			fdp->d_media = 1<<FMT_5D9 | 1<<FMT_5D8 | 1<<FMT_5D4 |
462*0Sstevel@tonic-gate 			    1<<FMT_5D16;
463*0Sstevel@tonic-gate 			fdp->d_deffdtype = fdp->d_curfdtype = FMT_5D9;
464*0Sstevel@tonic-gate 			break;
465*0Sstevel@tonic-gate 		case 3:			/* 3.5 double density */
466*0Sstevel@tonic-gate 			*fjp->fj_drive = dfd_350HD;
467*0Sstevel@tonic-gate 			fdp->d_media = 1<<FMT_3D;
468*0Sstevel@tonic-gate 			fdp->d_deffdtype = fdp->d_curfdtype = FMT_3D;
469*0Sstevel@tonic-gate 			break;
470*0Sstevel@tonic-gate 		case 5:			/* 3.5 extended density */
471*0Sstevel@tonic-gate 		case 6:
472*0Sstevel@tonic-gate 		case 7:
473*0Sstevel@tonic-gate 			*fjp->fj_drive = dfd_350ED;
474*0Sstevel@tonic-gate 			fdp->d_media = 1<<FMT_3E | 1<<FMT_3H | 1<<FMT_3I |
475*0Sstevel@tonic-gate 			    1<<FMT_3D;
476*0Sstevel@tonic-gate 			fdp->d_deffdtype = fdp->d_curfdtype = FMT_3E;
477*0Sstevel@tonic-gate 			break;
478*0Sstevel@tonic-gate 		case 0:			/* no drive defined */
479*0Sstevel@tonic-gate 		default:
480*0Sstevel@tonic-gate 			goto no_attach;
481*0Sstevel@tonic-gate 		}
482*0Sstevel@tonic-gate 		*fjp->fj_chars = *defchar[fdp->d_deffdtype];
483*0Sstevel@tonic-gate 		*fjp->fj_attr = fdtypes[fdp->d_deffdtype];
484*0Sstevel@tonic-gate 		bcopy(fdparts[fdp->d_deffdtype], fdp->d_part,
485*0Sstevel@tonic-gate 		    sizeof (struct partition) * NDKMAP);
486*0Sstevel@tonic-gate 		fjp->fj_rotspd = fdtypes[fdp->d_deffdtype].fda_rotatespd;
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate 		sig_minor = drive_num << 3;
489*0Sstevel@tonic-gate 		for (dmdp = fd_minor; dmdp->name != NULL; dmdp++) {
490*0Sstevel@tonic-gate 			if (ddi_create_minor_node(dip, dmdp->name, dmdp->type,
491*0Sstevel@tonic-gate 			    sig_minor | dmdp->minor, DDI_NT_FD, NULL)
492*0Sstevel@tonic-gate 			    == DDI_FAILURE) {
493*0Sstevel@tonic-gate 				ddi_remove_minor_node(dip, NULL);
494*0Sstevel@tonic-gate 				goto no_attach;
495*0Sstevel@tonic-gate 			}
496*0Sstevel@tonic-gate 		}
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 		FDERRPRINT(FDEP_L3, FDEM_ATTA,
499*0Sstevel@tonic-gate 		    (CE_WARN, "fd_attach: dip %p unit %d",
500*0Sstevel@tonic-gate 		    (void *)dip, unit_num));
501*0Sstevel@tonic-gate 		(void) sprintf(name, "fd%d", drive_num);
502*0Sstevel@tonic-gate 		fdp->d_iostat = kstat_create("fd", drive_num, name, "disk",
503*0Sstevel@tonic-gate 		    KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
504*0Sstevel@tonic-gate 		if (fdp->d_iostat) {
505*0Sstevel@tonic-gate 			fdp->d_iostat->ks_lock = &fjp->fj_lock;
506*0Sstevel@tonic-gate 			kstat_install(fdp->d_iostat);
507*0Sstevel@tonic-gate 		}
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate 		fjp->fj_data = (caddr_t)fdp;
510*0Sstevel@tonic-gate 		fjp->fj_flags |= FUNIT_DRVATCH;
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate 		/*
513*0Sstevel@tonic-gate 		 * Add a zero-length attribute to tell the world we support
514*0Sstevel@tonic-gate 		 * kernel ioctls (for layered drivers)
515*0Sstevel@tonic-gate 		 */
516*0Sstevel@tonic-gate 		(void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
517*0Sstevel@tonic-gate 		    DDI_KERNEL_IOCTL, NULL, 0);
518*0Sstevel@tonic-gate 		/*
519*0Sstevel@tonic-gate 		 * Ignoring return value because, for passed arguments, only
520*0Sstevel@tonic-gate 		 * DDI_SUCCESS is returned.
521*0Sstevel@tonic-gate 		 */
522*0Sstevel@tonic-gate 		ddi_report_dev(dip);
523*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate #ifdef NOT_YET
526*0Sstevel@tonic-gate 	case DDI_RESUME:
527*0Sstevel@tonic-gate 		drive_num = ddi_get_instance(dip);
528*0Sstevel@tonic-gate 		if (!(fdp = ddi_get_soft_state(fd_state_head, drive_num)))
529*0Sstevel@tonic-gate 			return (DDI_FAILURE);
530*0Sstevel@tonic-gate 		fjp = (struct fcu_obj *)fdp->d_obj;
531*0Sstevel@tonic-gate 		mutex_enter(&fjp->fj_lock);
532*0Sstevel@tonic-gate 		if (!fjp->fj_suspended) {
533*0Sstevel@tonic-gate 			mutex_exit(&fjp->fj_lock);
534*0Sstevel@tonic-gate 			return (DDI_SUCCESS);
535*0Sstevel@tonic-gate 		}
536*0Sstevel@tonic-gate 		fjp->fj_fdc->c_curpcyl[drive_num & 3] = -1;
537*0Sstevel@tonic-gate 		fjp->fj_suspended = 0;
538*0Sstevel@tonic-gate 		mutex_exit(&fjp->fj_lock);
539*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
540*0Sstevel@tonic-gate #endif
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate 	default:
543*0Sstevel@tonic-gate 		return (DDI_FAILURE);
544*0Sstevel@tonic-gate 	}
545*0Sstevel@tonic-gate no_attach:
546*0Sstevel@tonic-gate 	fjp->fj_drive = NULL;
547*0Sstevel@tonic-gate 	fjp->fj_chars = NULL;
548*0Sstevel@tonic-gate 	fjp->fj_attr = NULL;
549*0Sstevel@tonic-gate 	mutex_destroy(&fjp->fj_lock);
550*0Sstevel@tonic-gate 	sema_destroy(&fdp->d_ocsem);
551*0Sstevel@tonic-gate 	ddi_soft_state_free(fd_state_head, drive_num);
552*0Sstevel@tonic-gate 	FDERRPRINT(FDEP_L3, FDEM_ATTA,
553*0Sstevel@tonic-gate 	    (CE_WARN, "fd_attach failed: dip %p unit %d",
554*0Sstevel@tonic-gate 	    (void *)dip, unit_num));
555*0Sstevel@tonic-gate 	return (DDI_FAILURE);
556*0Sstevel@tonic-gate }
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 
559*0Sstevel@tonic-gate /* ARGSUSED */
560*0Sstevel@tonic-gate static int
561*0Sstevel@tonic-gate fd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
562*0Sstevel@tonic-gate {
563*0Sstevel@tonic-gate 	struct fcu_obj *fjp;
564*0Sstevel@tonic-gate 	struct fdisk *fdp;
565*0Sstevel@tonic-gate 	int drive_num;
566*0Sstevel@tonic-gate 	int rval = DDI_SUCCESS;
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 	FDERRPRINT(FDEP_L3, FDEM_ATTA, (CE_WARN, "fd_detach dip %p",
569*0Sstevel@tonic-gate 		(void *)dip));
570*0Sstevel@tonic-gate 
571*0Sstevel@tonic-gate 	drive_num = ddi_get_instance(dip);
572*0Sstevel@tonic-gate 	if (!(fdp = ddi_get_soft_state(fd_state_head, drive_num)))
573*0Sstevel@tonic-gate 		return (rval);
574*0Sstevel@tonic-gate 
575*0Sstevel@tonic-gate 	switch (cmd) {
576*0Sstevel@tonic-gate 	case DDI_DETACH:
577*0Sstevel@tonic-gate 		if (fd_unit_is_open(fdp)) {
578*0Sstevel@tonic-gate 			rval = EBUSY;
579*0Sstevel@tonic-gate 			break;
580*0Sstevel@tonic-gate 		}
581*0Sstevel@tonic-gate 		kstat_delete(fdp->d_iostat);
582*0Sstevel@tonic-gate 		fdp->d_iostat = NULL;
583*0Sstevel@tonic-gate 		fjp = (struct fcu_obj *)fdp->d_obj;
584*0Sstevel@tonic-gate 		fjp->fj_data = NULL;
585*0Sstevel@tonic-gate 		fjp->fj_drive = NULL;
586*0Sstevel@tonic-gate 		fjp->fj_chars = NULL;
587*0Sstevel@tonic-gate 		fjp->fj_attr = NULL;
588*0Sstevel@tonic-gate 		ddi_prop_remove_all(dip);
589*0Sstevel@tonic-gate 		mutex_destroy(&fjp->fj_lock);
590*0Sstevel@tonic-gate 		sema_destroy(&fdp->d_ocsem);
591*0Sstevel@tonic-gate 		ddi_soft_state_free(fd_state_head, drive_num);
592*0Sstevel@tonic-gate 		break;
593*0Sstevel@tonic-gate 
594*0Sstevel@tonic-gate #ifdef NOT_YET
595*0Sstevel@tonic-gate 	case DDI_SUSPEND:
596*0Sstevel@tonic-gate 		fjp = (struct fcu_obj *)fdp->d_obj;
597*0Sstevel@tonic-gate 		fjp->fj_suspended = 1;	/* Must be before mutex */
598*0Sstevel@tonic-gate 		mutex_enter(&fjp->fj_lock);
599*0Sstevel@tonic-gate 		while (fjp->fj_flags & FUNIT_BUSY) {
600*0Sstevel@tonic-gate 			/* Wait for I/O to finish */
601*0Sstevel@tonic-gate 			cv_wait(&fjp->fj_flags, &fjp->fj_lock);
602*0Sstevel@tonic-gate 		}
603*0Sstevel@tonic-gate 		mutex_exit(&fjp->fj_lock);
604*0Sstevel@tonic-gate 		break;
605*0Sstevel@tonic-gate #endif
606*0Sstevel@tonic-gate 
607*0Sstevel@tonic-gate 	default:
608*0Sstevel@tonic-gate 		rval = EINVAL;
609*0Sstevel@tonic-gate 		break;
610*0Sstevel@tonic-gate 	}
611*0Sstevel@tonic-gate 	return (rval);
612*0Sstevel@tonic-gate }
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate static int
616*0Sstevel@tonic-gate fd_part_is_open(struct fdisk *fdp, int part)
617*0Sstevel@tonic-gate {
618*0Sstevel@tonic-gate 	int i;
619*0Sstevel@tonic-gate 
620*0Sstevel@tonic-gate 	for (i = 0; i < (OTYPCNT - 1); i++)
621*0Sstevel@tonic-gate 		if (fdp->d_regopen[i] & (1 << part))
622*0Sstevel@tonic-gate 			return (1);
623*0Sstevel@tonic-gate 	return (0);
624*0Sstevel@tonic-gate }
625*0Sstevel@tonic-gate 
626*0Sstevel@tonic-gate static int
627*0Sstevel@tonic-gate fd_unit_is_open(struct fdisk *fdp)
628*0Sstevel@tonic-gate {
629*0Sstevel@tonic-gate 	int i;
630*0Sstevel@tonic-gate 
631*0Sstevel@tonic-gate 	for (i = 0; i < NDKMAP; i++)
632*0Sstevel@tonic-gate 		if (fdp->d_lyropen[i])
633*0Sstevel@tonic-gate 			return (1);
634*0Sstevel@tonic-gate 	for (i = 0; i < (OTYPCNT - 1); i++)
635*0Sstevel@tonic-gate 		if (fdp->d_regopen[i])
636*0Sstevel@tonic-gate 			return (1);
637*0Sstevel@tonic-gate 	return (0);
638*0Sstevel@tonic-gate }
639*0Sstevel@tonic-gate 
640*0Sstevel@tonic-gate /*ARGSUSED*/
641*0Sstevel@tonic-gate static int
642*0Sstevel@tonic-gate fd_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
643*0Sstevel@tonic-gate {
644*0Sstevel@tonic-gate 	struct fcu_obj *fjp = NULL;
645*0Sstevel@tonic-gate 	struct fdisk *fdp = NULL;
646*0Sstevel@tonic-gate 	struct partition *pp;
647*0Sstevel@tonic-gate 	dev_t dev;
648*0Sstevel@tonic-gate 	int part, unit;
649*0Sstevel@tonic-gate 	int part_is_open;
650*0Sstevel@tonic-gate 	int rval;
651*0Sstevel@tonic-gate 	uint_t pbit;
652*0Sstevel@tonic-gate 
653*0Sstevel@tonic-gate 	dev = *devp;
654*0Sstevel@tonic-gate 	unit = fd_getdrive(dev, &fjp, &fdp);
655*0Sstevel@tonic-gate 	if (!fjp || !fdp)
656*0Sstevel@tonic-gate 		return (ENXIO);
657*0Sstevel@tonic-gate 	part = PARTITION(dev);
658*0Sstevel@tonic-gate 	pbit = 1 << part;
659*0Sstevel@tonic-gate 	pp = &fdp->d_part[part];
660*0Sstevel@tonic-gate 
661*0Sstevel@tonic-gate 	/*
662*0Sstevel@tonic-gate 	 * Serialize opens/closes
663*0Sstevel@tonic-gate 	 */
664*0Sstevel@tonic-gate 	sema_p(&fdp->d_ocsem);
665*0Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_OPEN,
666*0Sstevel@tonic-gate 	    (CE_CONT, "fd_open: fd%d part %d flag %x otype %x\n", DRIVE(dev),
667*0Sstevel@tonic-gate 	    part, flag, otyp));
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate 	/*
670*0Sstevel@tonic-gate 	 * Check for previous exclusive open, or trying to exclusive open
671*0Sstevel@tonic-gate 	 * An "exclusive open" on any partition is not guaranteed to
672*0Sstevel@tonic-gate 	 * protect against opens on another partition that overlaps it.
673*0Sstevel@tonic-gate 	 */
674*0Sstevel@tonic-gate 	if (otyp == OTYP_LYR) {
675*0Sstevel@tonic-gate 		part_is_open = (fdp->d_lyropen[part] != 0);
676*0Sstevel@tonic-gate 	} else {
677*0Sstevel@tonic-gate 		part_is_open = fd_part_is_open(fdp, part);
678*0Sstevel@tonic-gate 	}
679*0Sstevel@tonic-gate 	if ((fdp->d_exclmask & pbit) || ((flag & FEXCL) && part_is_open)) {
680*0Sstevel@tonic-gate 		FDERRPRINT(FDEP_L0, FDEM_OPEN, (CE_CONT,
681*0Sstevel@tonic-gate 		    "fd_open: exclparts %lx openparts %lx lyrcnt %lx pbit %x\n",
682*0Sstevel@tonic-gate 		    fdp->d_exclmask, fdp->d_regopen[otyp], fdp->d_lyropen[part],
683*0Sstevel@tonic-gate 		    pbit));
684*0Sstevel@tonic-gate 		sema_v(&fdp->d_ocsem);
685*0Sstevel@tonic-gate 		return (EBUSY);
686*0Sstevel@tonic-gate 	}
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate 	/*
689*0Sstevel@tonic-gate 	 * Ensure that drive is recalibrated on first open of new diskette.
690*0Sstevel@tonic-gate 	 */
691*0Sstevel@tonic-gate 	fjp->fj_ops->fco_select(fjp, unit, 1);
692*0Sstevel@tonic-gate 	if (fjp->fj_ops->fco_getchng(fjp, unit) != 0) {
693*0Sstevel@tonic-gate 		if (fjp->fj_ops->fco_rcseek(fjp, unit, -1, 0)) {
694*0Sstevel@tonic-gate 			FDERRPRINT(FDEP_L2, FDEM_OPEN,
695*0Sstevel@tonic-gate 			    (CE_NOTE, "fd_open fd%d: not ready", DRIVE(dev)));
696*0Sstevel@tonic-gate 			fjp->fj_ops->fco_select(fjp, unit, 0);
697*0Sstevel@tonic-gate 			sema_v(&fdp->d_ocsem);
698*0Sstevel@tonic-gate 			return (ENXIO);
699*0Sstevel@tonic-gate 		}
700*0Sstevel@tonic-gate 		fjp->fj_flags &= ~(FUNIT_LABELOK | FUNIT_UNLABELED);
701*0Sstevel@tonic-gate 	}
702*0Sstevel@tonic-gate 	if (flag & (FNDELAY | FNONBLOCK)) {
703*0Sstevel@tonic-gate 		/* don't attempt access, just return successfully */
704*0Sstevel@tonic-gate 		fjp->fj_ops->fco_select(fjp, unit, 0);
705*0Sstevel@tonic-gate 		goto out;
706*0Sstevel@tonic-gate 	}
707*0Sstevel@tonic-gate 
708*0Sstevel@tonic-gate 	/*
709*0Sstevel@tonic-gate 	 * auto-sense the density/format of the diskette
710*0Sstevel@tonic-gate 	 */
711*0Sstevel@tonic-gate 	rval = fdgetlabel(fjp, unit);
712*0Sstevel@tonic-gate 	fjp->fj_ops->fco_select(fjp, unit, 0);
713*0Sstevel@tonic-gate 	if (rval) {
714*0Sstevel@tonic-gate 		/* didn't find label (couldn't read anything) */
715*0Sstevel@tonic-gate 		FDERRPRINT(FDEP_L2, FDEM_OPEN,
716*0Sstevel@tonic-gate 		    (CE_NOTE, "fd%d: drive not ready", DRIVE(dev)));
717*0Sstevel@tonic-gate 		sema_v(&fdp->d_ocsem);
718*0Sstevel@tonic-gate 		return (EIO);
719*0Sstevel@tonic-gate 	}
720*0Sstevel@tonic-gate 	/* check partition */
721*0Sstevel@tonic-gate 	if (pp->p_size == 0) {
722*0Sstevel@tonic-gate 		sema_v(&fdp->d_ocsem);
723*0Sstevel@tonic-gate 		return (ENXIO);
724*0Sstevel@tonic-gate 	}
725*0Sstevel@tonic-gate 	/*
726*0Sstevel@tonic-gate 	 * if opening for writing, check write protect on diskette
727*0Sstevel@tonic-gate 	 */
728*0Sstevel@tonic-gate 	if ((flag & FWRITE) && (fdp->d_obj->fj_flags & FUNIT_WPROT)) {
729*0Sstevel@tonic-gate 		sema_v(&fdp->d_ocsem);
730*0Sstevel@tonic-gate 		return (EROFS);
731*0Sstevel@tonic-gate 	}
732*0Sstevel@tonic-gate 
733*0Sstevel@tonic-gate out:
734*0Sstevel@tonic-gate 	/*
735*0Sstevel@tonic-gate 	 * mark open as having succeeded
736*0Sstevel@tonic-gate 	 */
737*0Sstevel@tonic-gate 	if (flag & FEXCL)
738*0Sstevel@tonic-gate 		fdp->d_exclmask |= pbit;
739*0Sstevel@tonic-gate 	if (otyp == OTYP_LYR)
740*0Sstevel@tonic-gate 		fdp->d_lyropen[part]++;
741*0Sstevel@tonic-gate 	else
742*0Sstevel@tonic-gate 		fdp->d_regopen[otyp] |= 1 << part;
743*0Sstevel@tonic-gate 
744*0Sstevel@tonic-gate 	sema_v(&fdp->d_ocsem);
745*0Sstevel@tonic-gate 	return (0);
746*0Sstevel@tonic-gate }
747*0Sstevel@tonic-gate 
748*0Sstevel@tonic-gate /*
749*0Sstevel@tonic-gate  * fdgetlabel - read the SunOS label off the diskette
750*0Sstevel@tonic-gate  *	if it can read a valid label it does so, else it will use a
751*0Sstevel@tonic-gate  *	default.  If it can`t read the diskette - that is an error.
752*0Sstevel@tonic-gate  *
753*0Sstevel@tonic-gate  * RETURNS: 0 for ok - meaning that it could at least read the device,
754*0Sstevel@tonic-gate  *	!0 for error XXX TBD NYD error codes
755*0Sstevel@tonic-gate  */
756*0Sstevel@tonic-gate static int
757*0Sstevel@tonic-gate fdgetlabel(struct fcu_obj *fjp, int unit)
758*0Sstevel@tonic-gate {
759*0Sstevel@tonic-gate 	struct dk_label *label;
760*0Sstevel@tonic-gate 	struct fdisk *fdp;
761*0Sstevel@tonic-gate 	char *newlabel;
762*0Sstevel@tonic-gate 	short *sp;
763*0Sstevel@tonic-gate 	short count;
764*0Sstevel@tonic-gate 	short xsum;
765*0Sstevel@tonic-gate 	int tries, try_this;
766*0Sstevel@tonic-gate 	uint_t nexttype;
767*0Sstevel@tonic-gate 	int rval;
768*0Sstevel@tonic-gate 	short oldlvl;
769*0Sstevel@tonic-gate 	int i;
770*0Sstevel@tonic-gate 
771*0Sstevel@tonic-gate 	FDERRPRINT(FDEP_L0, FDEM_GETL,
772*0Sstevel@tonic-gate 	    (CE_CONT, "fdgetlabel fd unit %d\n", unit));
773*0Sstevel@tonic-gate 	fdp = (struct fdisk *)fjp->fj_data;
774*0Sstevel@tonic-gate 	fjp->fj_flags &= ~(FUNIT_UNLABELED);
775*0Sstevel@tonic-gate 
776*0Sstevel@tonic-gate 	/*
777*0Sstevel@tonic-gate 	 * get some space to play with the label
778*0Sstevel@tonic-gate 	 */
779*0Sstevel@tonic-gate 	label = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);
780*0Sstevel@tonic-gate 	FDERRPRINT(FDEP_L0, FDEM_GETL, (CE_CONT,
781*0Sstevel@tonic-gate 	    "fdgetlabel fd unit %d kmem_zalloc: ptr = %p, size = %lx\n",
782*0Sstevel@tonic-gate 	    unit, (void *)label, (size_t)sizeof (struct dk_label)));
783*0Sstevel@tonic-gate 
784*0Sstevel@tonic-gate 	/*
785*0Sstevel@tonic-gate 	 * read block 0 (0/0/1) to find the label
786*0Sstevel@tonic-gate 	 * (disk is potentially not present or unformatted)
787*0Sstevel@tonic-gate 	 */
788*0Sstevel@tonic-gate 	/* noerrprint since this is a private cmd */
789*0Sstevel@tonic-gate 	oldlvl = fderrlevel;
790*0Sstevel@tonic-gate 	fderrlevel = FDEP_LMAX;
791*0Sstevel@tonic-gate 	/*
792*0Sstevel@tonic-gate 	 * try different characteristics (ie densities)
793*0Sstevel@tonic-gate 	 *
794*0Sstevel@tonic-gate 	 * if fdp->d_curfdtype is -1 then the current characteristics
795*0Sstevel@tonic-gate 	 * were set by ioctl and need to try it as well as everything
796*0Sstevel@tonic-gate 	 * in the table
797*0Sstevel@tonic-gate 	 */
798*0Sstevel@tonic-gate 	nexttype = fdp->d_deffdtype;
799*0Sstevel@tonic-gate 	try_this = 1;		/* always try the current characteristics */
800*0Sstevel@tonic-gate 
801*0Sstevel@tonic-gate 	for (tries = nfdtypes; tries; tries--) {
802*0Sstevel@tonic-gate 		if (try_this) {
803*0Sstevel@tonic-gate 			fjp->fj_flags &= ~FUNIT_CHAROK;
804*0Sstevel@tonic-gate 
805*0Sstevel@tonic-gate 			/* try reading last sector of cyl 1, head 0 */
806*0Sstevel@tonic-gate 			if (!(rval = fjp->fj_ops->fco_rw(fjp, unit,
807*0Sstevel@tonic-gate 			    FDREAD, 1, 0, fjp->fj_chars->fdc_secptrack,
808*0Sstevel@tonic-gate 			    (caddr_t)label,
809*0Sstevel@tonic-gate 			    sizeof (struct dk_label))) &&
810*0Sstevel@tonic-gate 			    /* and last sector plus 1 of cylinder 1 */
811*0Sstevel@tonic-gate 			    fjp->fj_ops->fco_rw(fjp, unit, FDREAD, 1,
812*0Sstevel@tonic-gate 			    0, fjp->fj_chars->fdc_secptrack + 1,
813*0Sstevel@tonic-gate 			    (caddr_t)label,
814*0Sstevel@tonic-gate 			    sizeof (struct dk_label)) &&
815*0Sstevel@tonic-gate 			    /* and label sector on cylinder 0 */
816*0Sstevel@tonic-gate 			    !(rval = fjp->fj_ops->fco_rw(fjp, unit,
817*0Sstevel@tonic-gate 			    FDREAD, 0, 0, 1, (caddr_t)label,
818*0Sstevel@tonic-gate 			    sizeof (struct dk_label))))
819*0Sstevel@tonic-gate 				break;
820*0Sstevel@tonic-gate 			if (rval == ENXIO)
821*0Sstevel@tonic-gate 				break;
822*0Sstevel@tonic-gate 		}
823*0Sstevel@tonic-gate 		/*
824*0Sstevel@tonic-gate 		 * try the next entry in the characteristics tbl
825*0Sstevel@tonic-gate 		 */
826*0Sstevel@tonic-gate 		fdp->d_curfdtype = (signed char)nexttype;
827*0Sstevel@tonic-gate 		nexttype = (nexttype + 1) % nfdtypes;
828*0Sstevel@tonic-gate 		if ((1 << fdp->d_curfdtype) & fdp->d_media) {
829*0Sstevel@tonic-gate 			*fjp->fj_chars = *defchar[fdp->d_curfdtype];
830*0Sstevel@tonic-gate 			*fjp->fj_attr = fdtypes[fdp->d_curfdtype];
831*0Sstevel@tonic-gate 			bcopy(fdparts[fdp->d_curfdtype], fdp->d_part,
832*0Sstevel@tonic-gate 			    sizeof (struct partition) * NDKMAP);
833*0Sstevel@tonic-gate 			/*
834*0Sstevel@tonic-gate 			 * check for a double_density diskette
835*0Sstevel@tonic-gate 			 * in a high_density 5.25" drive
836*0Sstevel@tonic-gate 			 */
837*0Sstevel@tonic-gate 			if (fjp->fj_chars->fdc_transfer_rate == 250 &&
838*0Sstevel@tonic-gate 			    fjp->fj_rotspd > fjp->fj_attr->fda_rotatespd) {
839*0Sstevel@tonic-gate 				/*
840*0Sstevel@tonic-gate 				 * yes - adjust transfer rate since we don't
841*0Sstevel@tonic-gate 				 * know if we have a 5.25" dual-speed drive
842*0Sstevel@tonic-gate 				 */
843*0Sstevel@tonic-gate 				fjp->fj_attr->fda_rotatespd = 360;
844*0Sstevel@tonic-gate 				fjp->fj_chars->fdc_transfer_rate = 300;
845*0Sstevel@tonic-gate 				fjp->fj_chars->fdc_medium = 5;
846*0Sstevel@tonic-gate 			}
847*0Sstevel@tonic-gate 			if ((2 * fjp->fj_chars->fdc_ncyl) ==
848*0Sstevel@tonic-gate 			    defchar[fdp->d_deffdtype]->fdc_ncyl) {
849*0Sstevel@tonic-gate 				/* yes - adjust steps per cylinder */
850*0Sstevel@tonic-gate 				fjp->fj_chars->fdc_steps = 2;
851*0Sstevel@tonic-gate 			} else
852*0Sstevel@tonic-gate 				fjp->fj_chars->fdc_steps = 1;
853*0Sstevel@tonic-gate 			try_this = 1;
854*0Sstevel@tonic-gate 		} else
855*0Sstevel@tonic-gate 			try_this = 0;
856*0Sstevel@tonic-gate 	}
857*0Sstevel@tonic-gate 	fderrlevel = oldlvl;	/* print errors again */
858*0Sstevel@tonic-gate 
859*0Sstevel@tonic-gate 	if (rval) {
860*0Sstevel@tonic-gate 		fdp->d_curfdtype = fdp->d_deffdtype;
861*0Sstevel@tonic-gate 		goto out;			/* couldn't read anything */
862*0Sstevel@tonic-gate 	}
863*0Sstevel@tonic-gate 
864*0Sstevel@tonic-gate 	FDERRPRINT(FDEP_L0, FDEM_GETL,
865*0Sstevel@tonic-gate 	    (CE_CONT,
866*0Sstevel@tonic-gate 	    "fdgetlabel fd unit=%d ncyl=%d nsct=%d step=%d rpm=%d intlv=%d\n",
867*0Sstevel@tonic-gate 	    unit, fjp->fj_chars->fdc_ncyl, fjp->fj_chars->fdc_secptrack,
868*0Sstevel@tonic-gate 	    fjp->fj_chars->fdc_steps, fjp->fj_attr->fda_rotatespd,
869*0Sstevel@tonic-gate 	    fjp->fj_attr->fda_intrlv));
870*0Sstevel@tonic-gate 
871*0Sstevel@tonic-gate 	/*
872*0Sstevel@tonic-gate 	 * _something_ was read  -  look for unixtype label
873*0Sstevel@tonic-gate 	 */
874*0Sstevel@tonic-gate 	if (label->dkl_magic != DKL_MAGIC ||
875*0Sstevel@tonic-gate 	    label->dkl_vtoc.v_sanity != VTOC_SANE) {
876*0Sstevel@tonic-gate 		/* not a label - no magic number */
877*0Sstevel@tonic-gate 		goto nolabel;	/* no errors, but no label */
878*0Sstevel@tonic-gate 	}
879*0Sstevel@tonic-gate 
880*0Sstevel@tonic-gate 	count = sizeof (struct dk_label) / sizeof (short);
881*0Sstevel@tonic-gate 	sp = (short *)label;
882*0Sstevel@tonic-gate 	xsum = 0;
883*0Sstevel@tonic-gate 	while (count--)
884*0Sstevel@tonic-gate 		xsum ^= *sp++;	/* should add up to 0 */
885*0Sstevel@tonic-gate 	if (xsum) {
886*0Sstevel@tonic-gate 		/* not a label - checksum didn't compute */
887*0Sstevel@tonic-gate 		goto nolabel;	/* no errors, but no label */
888*0Sstevel@tonic-gate 	}
889*0Sstevel@tonic-gate 
890*0Sstevel@tonic-gate 	/*
891*0Sstevel@tonic-gate 	 * the SunOS label overrides current diskette characteristics
892*0Sstevel@tonic-gate 	 */
893*0Sstevel@tonic-gate 	fjp->fj_chars->fdc_ncyl = label->dkl_pcyl;
894*0Sstevel@tonic-gate 	fjp->fj_chars->fdc_nhead = label->dkl_nhead;
895*0Sstevel@tonic-gate 	fjp->fj_chars->fdc_secptrack = (label->dkl_nsect * DEV_BSIZE) /
896*0Sstevel@tonic-gate 	    fjp->fj_chars->fdc_sec_size;
897*0Sstevel@tonic-gate 	if (defchar[fdp->d_deffdtype]->fdc_ncyl == 2 * fjp->fj_chars->fdc_ncyl)
898*0Sstevel@tonic-gate 		fjp->fj_chars->fdc_steps = 2;
899*0Sstevel@tonic-gate 	else
900*0Sstevel@tonic-gate 		fjp->fj_chars->fdc_steps = 1;
901*0Sstevel@tonic-gate 
902*0Sstevel@tonic-gate 	fjp->fj_attr->fda_rotatespd = label->dkl_rpm;
903*0Sstevel@tonic-gate 	fjp->fj_attr->fda_intrlv = label->dkl_intrlv;
904*0Sstevel@tonic-gate 
905*0Sstevel@tonic-gate 	fdp->d_vtoc_version = label->dkl_vtoc.v_version;
906*0Sstevel@tonic-gate 	bcopy(label->dkl_vtoc.v_volume, fdp->d_vtoc_volume, LEN_DKL_VVOL);
907*0Sstevel@tonic-gate 	bcopy(label->dkl_vtoc.v_asciilabel,
908*0Sstevel@tonic-gate 	    fdp->d_vtoc_asciilabel, LEN_DKL_ASCII);
909*0Sstevel@tonic-gate 	/*
910*0Sstevel@tonic-gate 	 * logical partitions
911*0Sstevel@tonic-gate 	 */
912*0Sstevel@tonic-gate 	for (i = 0; i < NDKMAP; i++) {
913*0Sstevel@tonic-gate 		fdp->d_part[i].p_tag = label->dkl_vtoc.v_part[i].p_tag;
914*0Sstevel@tonic-gate 		fdp->d_part[i].p_flag = label->dkl_vtoc.v_part[i].p_flag;
915*0Sstevel@tonic-gate 		fdp->d_part[i].p_start = label->dkl_vtoc.v_part[i].p_start;
916*0Sstevel@tonic-gate 		fdp->d_part[i].p_size = label->dkl_vtoc.v_part[i].p_size;
917*0Sstevel@tonic-gate 
918*0Sstevel@tonic-gate 		fdp->d_vtoc_timestamp[i] = label->dkl_vtoc.timestamp[i];
919*0Sstevel@tonic-gate 	}
920*0Sstevel@tonic-gate 
921*0Sstevel@tonic-gate 	fjp->fj_flags |= FUNIT_LABELOK;
922*0Sstevel@tonic-gate 	goto out;
923*0Sstevel@tonic-gate 
924*0Sstevel@tonic-gate nolabel:
925*0Sstevel@tonic-gate 	/*
926*0Sstevel@tonic-gate 	 * if not found, fill in label info from default (mark default used)
927*0Sstevel@tonic-gate 	 */
928*0Sstevel@tonic-gate 	if (fdp->d_media & (1<<FMT_3D))
929*0Sstevel@tonic-gate 		newlabel = deflabel_35;
930*0Sstevel@tonic-gate 	else /* if (fdp->d_media & (1<<FMT_5D9)) */
931*0Sstevel@tonic-gate 		newlabel = deflabel_525;
932*0Sstevel@tonic-gate 	bzero(fdp->d_vtoc_volume, LEN_DKL_VVOL);
933*0Sstevel@tonic-gate 	(void) sprintf(fdp->d_vtoc_asciilabel, newlabel,
934*0Sstevel@tonic-gate 	    fjp->fj_chars->fdc_ncyl, fjp->fj_chars->fdc_nhead,
935*0Sstevel@tonic-gate 	    fjp->fj_chars->fdc_secptrack);
936*0Sstevel@tonic-gate 	fjp->fj_flags |= FUNIT_UNLABELED;
937*0Sstevel@tonic-gate 
938*0Sstevel@tonic-gate out:
939*0Sstevel@tonic-gate 	kmem_free(label, sizeof (struct dk_label));
940*0Sstevel@tonic-gate 	return (rval);
941*0Sstevel@tonic-gate }
942*0Sstevel@tonic-gate 
943*0Sstevel@tonic-gate 
944*0Sstevel@tonic-gate /*ARGSUSED*/
945*0Sstevel@tonic-gate static int
946*0Sstevel@tonic-gate fd_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
947*0Sstevel@tonic-gate {
948*0Sstevel@tonic-gate 	struct fcu_obj *fjp = NULL;
949*0Sstevel@tonic-gate 	struct fdisk *fdp = NULL;
950*0Sstevel@tonic-gate 	int part, part_is_closed;
951*0Sstevel@tonic-gate 
952*0Sstevel@tonic-gate #ifdef DEBUG
953*0Sstevel@tonic-gate 	int unit;
954*0Sstevel@tonic-gate #define	DEBUG_ASSIGN	unit=
955*0Sstevel@tonic-gate #else
956*0Sstevel@tonic-gate #define	DEBUG_ASSIGN	(void)
957*0Sstevel@tonic-gate #endif
958*0Sstevel@tonic-gate 
959*0Sstevel@tonic-gate 	DEBUG_ASSIGN fd_getdrive(dev, &fjp, &fdp);
960*0Sstevel@tonic-gate 	/*
961*0Sstevel@tonic-gate 	 * Ignoring return in non DEBUG mode because success is checked by
962*0Sstevel@tonic-gate 	 * verifying fjp and fdp and returned unit value is not used.
963*0Sstevel@tonic-gate 	 */
964*0Sstevel@tonic-gate 	if (!fjp || !fdp)
965*0Sstevel@tonic-gate 		return (ENXIO);
966*0Sstevel@tonic-gate 	part = PARTITION(dev);
967*0Sstevel@tonic-gate 
968*0Sstevel@tonic-gate 	sema_p(&fdp->d_ocsem);
969*0Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_CLOS,
970*0Sstevel@tonic-gate 	    (CE_CONT, "fd_close: fd unit %d part %d otype %x\n",
971*0Sstevel@tonic-gate 	    unit, part, otyp));
972*0Sstevel@tonic-gate 
973*0Sstevel@tonic-gate 	if (otyp == OTYP_LYR) {
974*0Sstevel@tonic-gate 		if (fdp->d_lyropen[part])
975*0Sstevel@tonic-gate 			fdp->d_lyropen[part]--;
976*0Sstevel@tonic-gate 		part_is_closed = (fdp->d_lyropen[part] == 0);
977*0Sstevel@tonic-gate 	} else {
978*0Sstevel@tonic-gate 		fdp->d_regopen[otyp] &= ~(1<<part);
979*0Sstevel@tonic-gate 		part_is_closed = 1;
980*0Sstevel@tonic-gate 	}
981*0Sstevel@tonic-gate 	if (part_is_closed) {
982*0Sstevel@tonic-gate 		if (part == 2 && fdp->d_exclmask&(1<<part))
983*0Sstevel@tonic-gate 			fdp->d_exclmask = 0;
984*0Sstevel@tonic-gate 		else
985*0Sstevel@tonic-gate 			fdp->d_exclmask &= ~(1<<part);
986*0Sstevel@tonic-gate 		FDERRPRINT(FDEP_L0, FDEM_CLOS,
987*0Sstevel@tonic-gate 		    (CE_CONT,
988*0Sstevel@tonic-gate 		    "fd_close: exclparts %lx openparts %lx lyrcnt %lx\n",
989*0Sstevel@tonic-gate 		    fdp->d_exclmask, fdp->d_regopen[otyp],
990*0Sstevel@tonic-gate 		    fdp->d_lyropen[part]));
991*0Sstevel@tonic-gate 
992*0Sstevel@tonic-gate 		if (fd_unit_is_open(fdp) == 0)
993*0Sstevel@tonic-gate 			fdp->d_obj->fj_flags &= ~FUNIT_CHANGED;
994*0Sstevel@tonic-gate 	}
995*0Sstevel@tonic-gate 	sema_v(&fdp->d_ocsem);
996*0Sstevel@tonic-gate 	return (0);
997*0Sstevel@tonic-gate }
998*0Sstevel@tonic-gate 
999*0Sstevel@tonic-gate /* ARGSUSED */
1000*0Sstevel@tonic-gate static int
1001*0Sstevel@tonic-gate fd_read(dev_t dev, struct uio *uio, cred_t *cred_p)
1002*0Sstevel@tonic-gate {
1003*0Sstevel@tonic-gate 	return (physio(fd_strategy, NULL, dev, B_READ, minphys, uio));
1004*0Sstevel@tonic-gate }
1005*0Sstevel@tonic-gate 
1006*0Sstevel@tonic-gate /* ARGSUSED */
1007*0Sstevel@tonic-gate static int
1008*0Sstevel@tonic-gate fd_write(dev_t dev, struct uio *uio, cred_t *cred_p)
1009*0Sstevel@tonic-gate {
1010*0Sstevel@tonic-gate 	return (physio(fd_strategy, NULL, dev, B_WRITE, minphys, uio));
1011*0Sstevel@tonic-gate }
1012*0Sstevel@tonic-gate 
1013*0Sstevel@tonic-gate /*
1014*0Sstevel@tonic-gate  * fd_strategy
1015*0Sstevel@tonic-gate  *	checks operation, hangs buf struct off fdcntlr, calls fdstart
1016*0Sstevel@tonic-gate  *	if not already busy.  Note that if we call start, then the operation
1017*0Sstevel@tonic-gate  *	will already be done on return (start sleeps).
1018*0Sstevel@tonic-gate  */
1019*0Sstevel@tonic-gate static int
1020*0Sstevel@tonic-gate fd_strategy(struct buf *bp)
1021*0Sstevel@tonic-gate {
1022*0Sstevel@tonic-gate 	struct fcu_obj *fjp;
1023*0Sstevel@tonic-gate 	struct fdisk *fdp;
1024*0Sstevel@tonic-gate 	struct partition *pp;
1025*0Sstevel@tonic-gate 
1026*0Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_STRA,
1027*0Sstevel@tonic-gate 	    (CE_CONT, "fd_strategy: bp = 0x%p, dev = 0x%lx\n",
1028*0Sstevel@tonic-gate 	    (void *)bp, bp->b_edev));
1029*0Sstevel@tonic-gate 
1030*0Sstevel@tonic-gate 	(void) fd_getdrive(bp->b_edev, &fjp, &fdp);
1031*0Sstevel@tonic-gate 
1032*0Sstevel@tonic-gate 	/*
1033*0Sstevel@tonic-gate 	 * Ignoring return because device exist.
1034*0Sstevel@tonic-gate 	 * Returned unit value is not used.
1035*0Sstevel@tonic-gate 	 */
1036*0Sstevel@tonic-gate 	pp = &fdp->d_part[PARTITION(bp->b_edev)];
1037*0Sstevel@tonic-gate 
1038*0Sstevel@tonic-gate 	if (fjp->fj_chars->fdc_sec_size > NBPSCTR && (bp->b_blkno & 1))  {
1039*0Sstevel@tonic-gate 		FDERRPRINT(FDEP_L3, FDEM_STRA,
1040*0Sstevel@tonic-gate 		    (CE_WARN, "fd%d: block %ld is not start of sector!",
1041*0Sstevel@tonic-gate 		    DRIVE(bp->b_edev), (long)bp->b_blkno));
1042*0Sstevel@tonic-gate 		bp->b_error = EINVAL;
1043*0Sstevel@tonic-gate 		goto bad;
1044*0Sstevel@tonic-gate 	}
1045*0Sstevel@tonic-gate 
1046*0Sstevel@tonic-gate 	if ((bp->b_blkno > pp->p_size)) {
1047*0Sstevel@tonic-gate 		FDERRPRINT(FDEP_L3, FDEM_STRA,
1048*0Sstevel@tonic-gate 		    (CE_WARN, "fd%d: block %ld is past the end! (nblk=%ld)",
1049*0Sstevel@tonic-gate 		    DRIVE(bp->b_edev), (long)bp->b_blkno, pp->p_size));
1050*0Sstevel@tonic-gate 		bp->b_error = ENOSPC;
1051*0Sstevel@tonic-gate 		goto bad;
1052*0Sstevel@tonic-gate 	}
1053*0Sstevel@tonic-gate 
1054*0Sstevel@tonic-gate 	/* if at end of file, skip out now */
1055*0Sstevel@tonic-gate 	if (bp->b_blkno == pp->p_size) {
1056*0Sstevel@tonic-gate 		if ((bp->b_flags & B_READ) == 0) {
1057*0Sstevel@tonic-gate 			/* a write needs to get an error! */
1058*0Sstevel@tonic-gate 			bp->b_error = ENOSPC;
1059*0Sstevel@tonic-gate 			goto bad;
1060*0Sstevel@tonic-gate 		}
1061*0Sstevel@tonic-gate 		bp->b_resid = bp->b_bcount;
1062*0Sstevel@tonic-gate 		biodone(bp);
1063*0Sstevel@tonic-gate 		return (0);
1064*0Sstevel@tonic-gate 	}
1065*0Sstevel@tonic-gate 
1066*0Sstevel@tonic-gate 	/* if operation not a multiple of sector size, is error! */
1067*0Sstevel@tonic-gate 	if (bp->b_bcount % fjp->fj_chars->fdc_sec_size)  {
1068*0Sstevel@tonic-gate 		FDERRPRINT(FDEP_L3, FDEM_STRA,
1069*0Sstevel@tonic-gate 		    (CE_WARN, "fd%d: count %ld must be a multiple of %d",
1070*0Sstevel@tonic-gate 		    DRIVE(bp->b_edev), bp->b_bcount,
1071*0Sstevel@tonic-gate 		    fjp->fj_chars->fdc_sec_size));
1072*0Sstevel@tonic-gate 		bp->b_error = EINVAL;
1073*0Sstevel@tonic-gate 		goto bad;
1074*0Sstevel@tonic-gate 	}
1075*0Sstevel@tonic-gate 
1076*0Sstevel@tonic-gate 	/*
1077*0Sstevel@tonic-gate 	 * Put the buf request in the drive's queue, FIFO.
1078*0Sstevel@tonic-gate 	 */
1079*0Sstevel@tonic-gate 	bp->av_forw = 0;
1080*0Sstevel@tonic-gate 	mutex_enter(&fjp->fj_lock);
1081*0Sstevel@tonic-gate 	if (fdp->d_iostat)
1082*0Sstevel@tonic-gate 		kstat_waitq_enter(KIOSP);
1083*0Sstevel@tonic-gate 	if (fdp->d_actf)
1084*0Sstevel@tonic-gate 		fdp->d_actl->av_forw = bp;
1085*0Sstevel@tonic-gate 	else
1086*0Sstevel@tonic-gate 		fdp->d_actf = bp;
1087*0Sstevel@tonic-gate 	fdp->d_actl = bp;
1088*0Sstevel@tonic-gate 	if (!(fjp->fj_flags & FUNIT_BUSY)) {
1089*0Sstevel@tonic-gate 		fdstart(fjp);
1090*0Sstevel@tonic-gate 	}
1091*0Sstevel@tonic-gate 	mutex_exit(&fjp->fj_lock);
1092*0Sstevel@tonic-gate 	return (0);
1093*0Sstevel@tonic-gate 
1094*0Sstevel@tonic-gate bad:
1095*0Sstevel@tonic-gate 	bp->b_resid = bp->b_bcount;
1096*0Sstevel@tonic-gate 	bp->b_flags |= B_ERROR;
1097*0Sstevel@tonic-gate 	biodone(bp);
1098*0Sstevel@tonic-gate 	return (0);
1099*0Sstevel@tonic-gate }
1100*0Sstevel@tonic-gate 
1101*0Sstevel@tonic-gate /*
1102*0Sstevel@tonic-gate  * fdstart
1103*0Sstevel@tonic-gate  *	called from fd_strategy() or from fdXXXX() to setup and
1104*0Sstevel@tonic-gate  *	start operations of read or write only (using buf structs).
1105*0Sstevel@tonic-gate  *	Because the chip doesn't handle crossing cylinder boundaries on
1106*0Sstevel@tonic-gate  *	the fly, this takes care of those boundary conditions.  Note that
1107*0Sstevel@tonic-gate  *	it sleeps until the operation is done *within fdstart* - so that
1108*0Sstevel@tonic-gate  *	when fdstart returns, the operation is already done.
1109*0Sstevel@tonic-gate  */
1110*0Sstevel@tonic-gate static void
1111*0Sstevel@tonic-gate fdstart(struct fcu_obj *fjp)
1112*0Sstevel@tonic-gate {
1113*0Sstevel@tonic-gate 	struct buf *bp;
1114*0Sstevel@tonic-gate 	struct fdisk *fdp = (struct fdisk *)fjp->fj_data;
1115*0Sstevel@tonic-gate 	struct fd_char *chp;
1116*0Sstevel@tonic-gate 	struct partition *pp;
1117*0Sstevel@tonic-gate 	uint_t ptend;
1118*0Sstevel@tonic-gate 	uint_t bincyl;		/* (the number of the desired) block in cyl. */
1119*0Sstevel@tonic-gate 	uint_t blk, len, tlen;
1120*0Sstevel@tonic-gate 	uint_t secpcyl;		/* number of sectors per cylinder */
1121*0Sstevel@tonic-gate 	int cyl, head, sect;
1122*0Sstevel@tonic-gate 	int sctrshft, unit;
1123*0Sstevel@tonic-gate 	caddr_t	addr;
1124*0Sstevel@tonic-gate 
1125*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fjp->fj_lock));
1126*0Sstevel@tonic-gate 	fjp->fj_flags |= FUNIT_BUSY;
1127*0Sstevel@tonic-gate 
1128*0Sstevel@tonic-gate 	while ((bp = fdp->d_actf) != NULL) {
1129*0Sstevel@tonic-gate 		fdp->d_actf = bp->av_forw;
1130*0Sstevel@tonic-gate 		fdp->d_current = bp;
1131*0Sstevel@tonic-gate 		if (fdp->d_iostat) {
1132*0Sstevel@tonic-gate 			kstat_waitq_to_runq(KIOSP);
1133*0Sstevel@tonic-gate 		}
1134*0Sstevel@tonic-gate 		mutex_exit(&fjp->fj_lock);
1135*0Sstevel@tonic-gate 
1136*0Sstevel@tonic-gate 		FDERRPRINT(FDEP_L0, FDEM_STRT,
1137*0Sstevel@tonic-gate 		    (CE_CONT, "fdstart: bp=0x%p blkno=0x%lx bcount=0x%lx\n",
1138*0Sstevel@tonic-gate 		    (void *)bp, (long)bp->b_blkno, bp->b_bcount));
1139*0Sstevel@tonic-gate 		bp->b_flags &= ~B_ERROR;
1140*0Sstevel@tonic-gate 		bp->b_error = 0;
1141*0Sstevel@tonic-gate 		bp->b_resid = bp->b_bcount;	/* init resid */
1142*0Sstevel@tonic-gate 
1143*0Sstevel@tonic-gate 		ASSERT(DRIVE(bp->b_edev) == ddi_get_instance(fjp->fj_dip));
1144*0Sstevel@tonic-gate 		unit = fjp->fj_unit;
1145*0Sstevel@tonic-gate 		fjp->fj_ops->fco_select(fjp, unit, 1);
1146*0Sstevel@tonic-gate 
1147*0Sstevel@tonic-gate 		bp_mapin(bp);			/* map in buffers */
1148*0Sstevel@tonic-gate 
1149*0Sstevel@tonic-gate 		pp = &fdp->d_part[PARTITION(bp->b_edev)];
1150*0Sstevel@tonic-gate 		/* starting blk adjusted for the partition */
1151*0Sstevel@tonic-gate 		blk = bp->b_blkno + pp->p_start;
1152*0Sstevel@tonic-gate 		ptend = pp->p_start + pp->p_size;   /* end of the partition */
1153*0Sstevel@tonic-gate 
1154*0Sstevel@tonic-gate 		chp = fjp->fj_chars;
1155*0Sstevel@tonic-gate 		secpcyl = chp->fdc_nhead * chp->fdc_secptrack;
1156*0Sstevel@tonic-gate 		switch (chp->fdc_sec_size) {
1157*0Sstevel@tonic-gate 		/* convert logical block numbers to sector numbers */
1158*0Sstevel@tonic-gate 		case 1024:
1159*0Sstevel@tonic-gate 			sctrshft = SCTRSHFT + 1;
1160*0Sstevel@tonic-gate 			blk >>= 1;
1161*0Sstevel@tonic-gate 			ptend >>= 1;
1162*0Sstevel@tonic-gate 			break;
1163*0Sstevel@tonic-gate 		default:
1164*0Sstevel@tonic-gate 		case NBPSCTR:
1165*0Sstevel@tonic-gate 			sctrshft = SCTRSHFT;
1166*0Sstevel@tonic-gate 			break;
1167*0Sstevel@tonic-gate 		case 256:
1168*0Sstevel@tonic-gate 			sctrshft = SCTRSHFT - 1;
1169*0Sstevel@tonic-gate 			blk <<= 1;
1170*0Sstevel@tonic-gate 			ptend <<= 1;
1171*0Sstevel@tonic-gate 			break;
1172*0Sstevel@tonic-gate 		}
1173*0Sstevel@tonic-gate 
1174*0Sstevel@tonic-gate 		/*
1175*0Sstevel@tonic-gate 		 * If off the end, limit to actual amount that
1176*0Sstevel@tonic-gate 		 * can be transferred.
1177*0Sstevel@tonic-gate 		 */
1178*0Sstevel@tonic-gate 		if ((blk + (bp->b_bcount >> sctrshft)) > ptend)
1179*0Sstevel@tonic-gate 			/* to end of partition */
1180*0Sstevel@tonic-gate 			len = (ptend - blk) << sctrshft;
1181*0Sstevel@tonic-gate 		else
1182*0Sstevel@tonic-gate 			len = bp->b_bcount;
1183*0Sstevel@tonic-gate 		addr = bp->b_un.b_addr;		/* data buffer address */
1184*0Sstevel@tonic-gate 
1185*0Sstevel@tonic-gate 		/*
1186*0Sstevel@tonic-gate 		 * now we have the real start blk, addr and len for xfer op
1187*0Sstevel@tonic-gate 		 */
1188*0Sstevel@tonic-gate 		while (len != 0) {
1189*0Sstevel@tonic-gate 			/* start cyl of req */
1190*0Sstevel@tonic-gate 			cyl = blk / secpcyl;
1191*0Sstevel@tonic-gate 			bincyl = blk % secpcyl;
1192*0Sstevel@tonic-gate 			/* start head of req */
1193*0Sstevel@tonic-gate 			head = bincyl / chp->fdc_secptrack;
1194*0Sstevel@tonic-gate 			/* start sector of req */
1195*0Sstevel@tonic-gate 			sect = (bincyl % chp->fdc_secptrack) + 1;
1196*0Sstevel@tonic-gate 			/*
1197*0Sstevel@tonic-gate 			 * If the desired block and length will go beyond the
1198*0Sstevel@tonic-gate 			 * cylinder end, then limit it to the cylinder end.
1199*0Sstevel@tonic-gate 			 */
1200*0Sstevel@tonic-gate 			if (bp->b_flags & B_READ) {
1201*0Sstevel@tonic-gate 				if (len > ((secpcyl - bincyl) << sctrshft))
1202*0Sstevel@tonic-gate 					tlen = (secpcyl - bincyl) << sctrshft;
1203*0Sstevel@tonic-gate 				else
1204*0Sstevel@tonic-gate 					tlen = len;
1205*0Sstevel@tonic-gate 			} else {
1206*0Sstevel@tonic-gate 				if (len >
1207*0Sstevel@tonic-gate 				    ((chp->fdc_secptrack - sect + 1) <<
1208*0Sstevel@tonic-gate 				    sctrshft))
1209*0Sstevel@tonic-gate 					tlen =
1210*0Sstevel@tonic-gate 					    (chp->fdc_secptrack - sect + 1) <<
1211*0Sstevel@tonic-gate 					    sctrshft;
1212*0Sstevel@tonic-gate 				else
1213*0Sstevel@tonic-gate 					tlen = len;
1214*0Sstevel@tonic-gate 			}
1215*0Sstevel@tonic-gate 
1216*0Sstevel@tonic-gate 			FDERRPRINT(FDEP_L0, FDEM_STRT, (CE_CONT,
1217*0Sstevel@tonic-gate 			    "  blk 0x%x addr 0x%p len 0x%x "
1218*0Sstevel@tonic-gate 			    "cyl %d head %d sec %d\n  resid 0x%lx, tlen %d\n",
1219*0Sstevel@tonic-gate 			    blk, (void *)addr, len, cyl, head, sect,
1220*0Sstevel@tonic-gate 			    bp->b_resid, tlen));
1221*0Sstevel@tonic-gate 
1222*0Sstevel@tonic-gate 			/*
1223*0Sstevel@tonic-gate 			 * (try to) do the operation - failure returns an errno
1224*0Sstevel@tonic-gate 			 */
1225*0Sstevel@tonic-gate 			bp->b_error = fjp->fj_ops->fco_rw(fjp, unit,
1226*0Sstevel@tonic-gate 			    bp->b_flags & B_READ, cyl, head, sect, addr, tlen);
1227*0Sstevel@tonic-gate 			if (bp->b_error != 0) {
1228*0Sstevel@tonic-gate 				FDERRPRINT(FDEP_L3, FDEM_STRT, (CE_WARN,
1229*0Sstevel@tonic-gate 				    "fdstart: bad exec of bp: 0x%p, err=%d",
1230*0Sstevel@tonic-gate 				    (void *)bp, bp->b_error));
1231*0Sstevel@tonic-gate 				bp->b_flags |= B_ERROR;
1232*0Sstevel@tonic-gate 				break;
1233*0Sstevel@tonic-gate 			}
1234*0Sstevel@tonic-gate 			blk += tlen >> sctrshft;
1235*0Sstevel@tonic-gate 			len -= tlen;
1236*0Sstevel@tonic-gate 			addr += tlen;
1237*0Sstevel@tonic-gate 			bp->b_resid -= tlen;
1238*0Sstevel@tonic-gate 		}
1239*0Sstevel@tonic-gate 		FDERRPRINT(FDEP_L0, FDEM_STRT,
1240*0Sstevel@tonic-gate 		    (CE_CONT, "fdstart done: b_resid %lu, b_count %lu\n",
1241*0Sstevel@tonic-gate 		    bp->b_resid, bp->b_bcount));
1242*0Sstevel@tonic-gate 		if (fdp->d_iostat) {
1243*0Sstevel@tonic-gate 			if (bp->b_flags & B_READ) {
1244*0Sstevel@tonic-gate 				KIOSP->reads++;
1245*0Sstevel@tonic-gate 				KIOSP->nread += (bp->b_bcount - bp->b_resid);
1246*0Sstevel@tonic-gate 			} else {
1247*0Sstevel@tonic-gate 				KIOSP->writes++;
1248*0Sstevel@tonic-gate 				KIOSP->nwritten += (bp->b_bcount - bp->b_resid);
1249*0Sstevel@tonic-gate 			}
1250*0Sstevel@tonic-gate 			kstat_runq_exit(KIOSP);
1251*0Sstevel@tonic-gate 		}
1252*0Sstevel@tonic-gate 		bp_mapout(bp);
1253*0Sstevel@tonic-gate 		biodone(bp);
1254*0Sstevel@tonic-gate 
1255*0Sstevel@tonic-gate 		fjp->fj_ops->fco_select(fjp, unit, 0);
1256*0Sstevel@tonic-gate 		mutex_enter(&fjp->fj_lock);
1257*0Sstevel@tonic-gate 		fdp->d_current = 0;
1258*0Sstevel@tonic-gate 	}
1259*0Sstevel@tonic-gate 	fjp->fj_flags ^= FUNIT_BUSY;
1260*0Sstevel@tonic-gate }
1261*0Sstevel@tonic-gate 
1262*0Sstevel@tonic-gate /* ARGSUSED */
1263*0Sstevel@tonic-gate static int
1264*0Sstevel@tonic-gate fd_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p,
1265*0Sstevel@tonic-gate 	int *rval_p)
1266*0Sstevel@tonic-gate {
1267*0Sstevel@tonic-gate 	union {
1268*0Sstevel@tonic-gate 		struct dk_cinfo dki;
1269*0Sstevel@tonic-gate 		struct dk_geom dkg;
1270*0Sstevel@tonic-gate 		struct dk_allmap dka;
1271*0Sstevel@tonic-gate 		struct fd_char fdchar;
1272*0Sstevel@tonic-gate 		struct fd_drive drvchar;
1273*0Sstevel@tonic-gate 		int	temp;
1274*0Sstevel@tonic-gate 	} cpy;
1275*0Sstevel@tonic-gate 	struct vtoc vtoc;
1276*0Sstevel@tonic-gate 	struct fcu_obj *fjp = NULL;
1277*0Sstevel@tonic-gate 	struct fdisk *fdp = NULL;
1278*0Sstevel@tonic-gate 	struct dk_map *dmp;
1279*0Sstevel@tonic-gate 	struct dk_label *label;
1280*0Sstevel@tonic-gate 	int nblks, part, unit;
1281*0Sstevel@tonic-gate 	int rval = 0;
1282*0Sstevel@tonic-gate 	enum dkio_state state;
1283*0Sstevel@tonic-gate 
1284*0Sstevel@tonic-gate 	unit = fd_getdrive(dev, &fjp, &fdp);
1285*0Sstevel@tonic-gate 	if (!fjp || !fdp)
1286*0Sstevel@tonic-gate 		return (ENXIO);
1287*0Sstevel@tonic-gate 
1288*0Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_IOCT,
1289*0Sstevel@tonic-gate 	    (CE_CONT, "fd_ioctl fd unit %d: cmd %x, arg %lx\n",
1290*0Sstevel@tonic-gate 	    unit, cmd, arg));
1291*0Sstevel@tonic-gate 
1292*0Sstevel@tonic-gate 	switch (cmd) {
1293*0Sstevel@tonic-gate 	case DKIOCINFO:
1294*0Sstevel@tonic-gate 		fjp->fj_ops->fco_dkinfo(fjp, &cpy.dki);
1295*0Sstevel@tonic-gate 		cpy.dki.dki_cnum = FDCTLR(fjp->fj_unit);
1296*0Sstevel@tonic-gate 		cpy.dki.dki_unit = FDUNIT(fjp->fj_unit);
1297*0Sstevel@tonic-gate 		cpy.dki.dki_partition = PARTITION(dev);
1298*0Sstevel@tonic-gate 		if (ddi_copyout(&cpy.dki, (void *)arg, sizeof (cpy.dki), flag))
1299*0Sstevel@tonic-gate 			rval = EFAULT;
1300*0Sstevel@tonic-gate 		break;
1301*0Sstevel@tonic-gate 
1302*0Sstevel@tonic-gate 	case DKIOCG_PHYGEOM:
1303*0Sstevel@tonic-gate 	case DKIOCG_VIRTGEOM:
1304*0Sstevel@tonic-gate 		cpy.dkg.dkg_nsect = fjp->fj_chars->fdc_secptrack;
1305*0Sstevel@tonic-gate 		goto get_geom;
1306*0Sstevel@tonic-gate 	case DKIOCGGEOM:
1307*0Sstevel@tonic-gate 		if (fjp->fj_flags & FUNIT_LABELOK)
1308*0Sstevel@tonic-gate 			cpy.dkg.dkg_nsect = (fjp->fj_chars->fdc_secptrack *
1309*0Sstevel@tonic-gate 			    fjp->fj_chars->fdc_sec_size) / DEV_BSIZE;
1310*0Sstevel@tonic-gate 		else
1311*0Sstevel@tonic-gate 			cpy.dkg.dkg_nsect = fjp->fj_chars->fdc_secptrack;
1312*0Sstevel@tonic-gate get_geom:
1313*0Sstevel@tonic-gate 		cpy.dkg.dkg_pcyl = fjp->fj_chars->fdc_ncyl;
1314*0Sstevel@tonic-gate 		cpy.dkg.dkg_ncyl = fjp->fj_chars->fdc_ncyl;
1315*0Sstevel@tonic-gate 		cpy.dkg.dkg_nhead = fjp->fj_chars->fdc_nhead;
1316*0Sstevel@tonic-gate 		cpy.dkg.dkg_intrlv = fjp->fj_attr->fda_intrlv;
1317*0Sstevel@tonic-gate 		cpy.dkg.dkg_rpm = fjp->fj_attr->fda_rotatespd;
1318*0Sstevel@tonic-gate 		cpy.dkg.dkg_read_reinstruct =
1319*0Sstevel@tonic-gate 		    (int)(cpy.dkg.dkg_nsect * cpy.dkg.dkg_rpm * 4) / 60000;
1320*0Sstevel@tonic-gate 		cpy.dkg.dkg_write_reinstruct = cpy.dkg.dkg_read_reinstruct;
1321*0Sstevel@tonic-gate 		if (ddi_copyout(&cpy.dkg, (void *)arg, sizeof (cpy.dkg), flag))
1322*0Sstevel@tonic-gate 			rval = EFAULT;
1323*0Sstevel@tonic-gate 		break;
1324*0Sstevel@tonic-gate 
1325*0Sstevel@tonic-gate 	case DKIOCSGEOM:
1326*0Sstevel@tonic-gate 		if (ddi_copyin((void *)arg, &cpy.dkg,
1327*0Sstevel@tonic-gate 		    sizeof (struct dk_geom), flag)) {
1328*0Sstevel@tonic-gate 			rval = EFAULT;
1329*0Sstevel@tonic-gate 			break;
1330*0Sstevel@tonic-gate 		}
1331*0Sstevel@tonic-gate 		mutex_enter(&fjp->fj_lock);
1332*0Sstevel@tonic-gate 		fjp->fj_chars->fdc_ncyl = cpy.dkg.dkg_ncyl;
1333*0Sstevel@tonic-gate 		fjp->fj_chars->fdc_nhead = cpy.dkg.dkg_nhead;
1334*0Sstevel@tonic-gate 		fjp->fj_chars->fdc_secptrack = cpy.dkg.dkg_nsect;
1335*0Sstevel@tonic-gate 		fjp->fj_attr->fda_intrlv = cpy.dkg.dkg_intrlv;
1336*0Sstevel@tonic-gate 		fjp->fj_attr->fda_rotatespd = cpy.dkg.dkg_rpm;
1337*0Sstevel@tonic-gate 		fdp->d_curfdtype = -1;
1338*0Sstevel@tonic-gate 		mutex_exit(&fjp->fj_lock);
1339*0Sstevel@tonic-gate 		break;
1340*0Sstevel@tonic-gate 
1341*0Sstevel@tonic-gate 	/*
1342*0Sstevel@tonic-gate 	 * return the map of all logical partitions
1343*0Sstevel@tonic-gate 	 */
1344*0Sstevel@tonic-gate 	case DKIOCGAPART:
1345*0Sstevel@tonic-gate 		/*
1346*0Sstevel@tonic-gate 		 * Note the conversion from starting sector number
1347*0Sstevel@tonic-gate 		 * to starting cylinder number.
1348*0Sstevel@tonic-gate 		 * Return error if division results in a remainder.
1349*0Sstevel@tonic-gate 		 */
1350*0Sstevel@tonic-gate 		nblks = fjp->fj_chars->fdc_nhead * fjp->fj_chars->fdc_secptrack;
1351*0Sstevel@tonic-gate 
1352*0Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
1353*0Sstevel@tonic-gate 		switch (ddi_model_convert_from(flag & FMODELS)) {
1354*0Sstevel@tonic-gate 		case DDI_MODEL_ILP32:
1355*0Sstevel@tonic-gate 		{
1356*0Sstevel@tonic-gate 			struct dk_allmap32 dka32;
1357*0Sstevel@tonic-gate 
1358*0Sstevel@tonic-gate 			for (part = 0; part < NDKMAP; part++) {
1359*0Sstevel@tonic-gate 				if ((fdp->d_part[part].p_start % nblks) != 0)
1360*0Sstevel@tonic-gate 					return (EINVAL);
1361*0Sstevel@tonic-gate 				dka32.dka_map[part].dkl_cylno =
1362*0Sstevel@tonic-gate 				    fdp->d_part[part].p_start / nblks;
1363*0Sstevel@tonic-gate 				dka32.dka_map[part].dkl_nblk =
1364*0Sstevel@tonic-gate 				    fdp->d_part[part].p_size;
1365*0Sstevel@tonic-gate 			}
1366*0Sstevel@tonic-gate 
1367*0Sstevel@tonic-gate 			if (ddi_copyout(&dka32, (void *)arg,
1368*0Sstevel@tonic-gate 			    sizeof (struct dk_allmap32), flag))
1369*0Sstevel@tonic-gate 				rval = EFAULT;
1370*0Sstevel@tonic-gate 
1371*0Sstevel@tonic-gate 			break;
1372*0Sstevel@tonic-gate 		}
1373*0Sstevel@tonic-gate 		case DDI_MODEL_NONE:
1374*0Sstevel@tonic-gate 
1375*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
1376*0Sstevel@tonic-gate 
1377*0Sstevel@tonic-gate 			dmp = (struct dk_map *)&cpy.dka;
1378*0Sstevel@tonic-gate 			for (part = 0; part < NDKMAP; part++) {
1379*0Sstevel@tonic-gate 				if ((fdp->d_part[part].p_start % nblks) != 0)
1380*0Sstevel@tonic-gate 					return (EINVAL);
1381*0Sstevel@tonic-gate 				dmp->dkl_cylno =
1382*0Sstevel@tonic-gate 				    fdp->d_part[part].p_start / nblks;
1383*0Sstevel@tonic-gate 				dmp->dkl_nblk = fdp->d_part[part].p_size;
1384*0Sstevel@tonic-gate 				dmp++;
1385*0Sstevel@tonic-gate 			}
1386*0Sstevel@tonic-gate 
1387*0Sstevel@tonic-gate 			if (ddi_copyout(&cpy.dka, (void *)arg,
1388*0Sstevel@tonic-gate 			    sizeof (struct dk_allmap), flag))
1389*0Sstevel@tonic-gate 				rval = EFAULT;
1390*0Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
1391*0Sstevel@tonic-gate 			break;
1392*0Sstevel@tonic-gate 
1393*0Sstevel@tonic-gate 		}
1394*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
1395*0Sstevel@tonic-gate 
1396*0Sstevel@tonic-gate 		break;
1397*0Sstevel@tonic-gate 
1398*0Sstevel@tonic-gate 	/*
1399*0Sstevel@tonic-gate 	 * Set the map of all logical partitions
1400*0Sstevel@tonic-gate 	 */
1401*0Sstevel@tonic-gate 	case DKIOCSAPART:
1402*0Sstevel@tonic-gate 
1403*0Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
1404*0Sstevel@tonic-gate 		switch (ddi_model_convert_from(flag & FMODELS)) {
1405*0Sstevel@tonic-gate 		case DDI_MODEL_ILP32:
1406*0Sstevel@tonic-gate 		{
1407*0Sstevel@tonic-gate 			struct dk_allmap32 dka32;
1408*0Sstevel@tonic-gate 
1409*0Sstevel@tonic-gate 			if (ddi_copyin((void *)arg, &dka32,
1410*0Sstevel@tonic-gate 			    sizeof (dka32), flag)) {
1411*0Sstevel@tonic-gate 				rval = EFAULT;
1412*0Sstevel@tonic-gate 				break;
1413*0Sstevel@tonic-gate 			}
1414*0Sstevel@tonic-gate 			for (part = 0; part < NDKMAP; part++) {
1415*0Sstevel@tonic-gate 				cpy.dka.dka_map[part].dkl_cylno =
1416*0Sstevel@tonic-gate 				    dka32.dka_map[part].dkl_cylno;
1417*0Sstevel@tonic-gate 				cpy.dka.dka_map[part].dkl_nblk =
1418*0Sstevel@tonic-gate 				    dka32.dka_map[part].dkl_nblk;
1419*0Sstevel@tonic-gate 			}
1420*0Sstevel@tonic-gate 			break;
1421*0Sstevel@tonic-gate 		}
1422*0Sstevel@tonic-gate 		case DDI_MODEL_NONE:
1423*0Sstevel@tonic-gate 
1424*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
1425*0Sstevel@tonic-gate 		if (ddi_copyin((void *)arg, &cpy.dka, sizeof (cpy.dka), flag))
1426*0Sstevel@tonic-gate 			rval = EFAULT;
1427*0Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
1428*0Sstevel@tonic-gate 
1429*0Sstevel@tonic-gate 			break;
1430*0Sstevel@tonic-gate 		}
1431*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
1432*0Sstevel@tonic-gate 
1433*0Sstevel@tonic-gate 		if (rval != 0)
1434*0Sstevel@tonic-gate 			break;
1435*0Sstevel@tonic-gate 
1436*0Sstevel@tonic-gate 		dmp = (struct dk_map *)&cpy.dka;
1437*0Sstevel@tonic-gate 		nblks = fjp->fj_chars->fdc_nhead *
1438*0Sstevel@tonic-gate 		    fjp->fj_chars->fdc_secptrack;
1439*0Sstevel@tonic-gate 		mutex_enter(&fjp->fj_lock);
1440*0Sstevel@tonic-gate 		/*
1441*0Sstevel@tonic-gate 		 * Note the conversion from starting cylinder number
1442*0Sstevel@tonic-gate 		 * to starting sector number.
1443*0Sstevel@tonic-gate 		 */
1444*0Sstevel@tonic-gate 		for (part = 0; part < NDKMAP; part++) {
1445*0Sstevel@tonic-gate 			fdp->d_part[part].p_start = dmp->dkl_cylno *
1446*0Sstevel@tonic-gate 			    nblks;
1447*0Sstevel@tonic-gate 			fdp->d_part[part].p_size = dmp->dkl_nblk;
1448*0Sstevel@tonic-gate 			dmp++;
1449*0Sstevel@tonic-gate 		}
1450*0Sstevel@tonic-gate 		mutex_exit(&fjp->fj_lock);
1451*0Sstevel@tonic-gate 
1452*0Sstevel@tonic-gate 		break;
1453*0Sstevel@tonic-gate 
1454*0Sstevel@tonic-gate 	case DKIOCGVTOC:
1455*0Sstevel@tonic-gate 		mutex_enter(&fjp->fj_lock);
1456*0Sstevel@tonic-gate 
1457*0Sstevel@tonic-gate 		/*
1458*0Sstevel@tonic-gate 		 * Exit if the diskette has no label.
1459*0Sstevel@tonic-gate 		 * Also, get the label to make sure the correct one is
1460*0Sstevel@tonic-gate 		 * being used since the diskette may have changed
1461*0Sstevel@tonic-gate 		 */
1462*0Sstevel@tonic-gate 		fjp->fj_ops->fco_select(fjp, unit, 1);
1463*0Sstevel@tonic-gate 		rval = fdgetlabel(fjp, unit);
1464*0Sstevel@tonic-gate 		fjp->fj_ops->fco_select(fjp, unit, 0);
1465*0Sstevel@tonic-gate 		if (rval) {
1466*0Sstevel@tonic-gate 			mutex_exit(&fjp->fj_lock);
1467*0Sstevel@tonic-gate 			rval = EINVAL;
1468*0Sstevel@tonic-gate 			break;
1469*0Sstevel@tonic-gate 		}
1470*0Sstevel@tonic-gate 
1471*0Sstevel@tonic-gate 		fd_build_user_vtoc(fjp, fdp, &vtoc);
1472*0Sstevel@tonic-gate 		mutex_exit(&fjp->fj_lock);
1473*0Sstevel@tonic-gate 
1474*0Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
1475*0Sstevel@tonic-gate 		switch (ddi_model_convert_from(flag & FMODELS)) {
1476*0Sstevel@tonic-gate 		case DDI_MODEL_ILP32:
1477*0Sstevel@tonic-gate 		{
1478*0Sstevel@tonic-gate 			struct vtoc32	vtoc32;
1479*0Sstevel@tonic-gate 
1480*0Sstevel@tonic-gate 			vtoctovtoc32(vtoc, vtoc32);
1481*0Sstevel@tonic-gate 
1482*0Sstevel@tonic-gate 			if (ddi_copyout(&vtoc32, (void *)arg,
1483*0Sstevel@tonic-gate 			    sizeof (vtoc32), flag))
1484*0Sstevel@tonic-gate 				rval = EFAULT;
1485*0Sstevel@tonic-gate 
1486*0Sstevel@tonic-gate 			break;
1487*0Sstevel@tonic-gate 		}
1488*0Sstevel@tonic-gate 		case DDI_MODEL_NONE:
1489*0Sstevel@tonic-gate 
1490*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
1491*0Sstevel@tonic-gate 			if (ddi_copyout(&vtoc, (void *)arg,
1492*0Sstevel@tonic-gate 			    sizeof (vtoc), flag))
1493*0Sstevel@tonic-gate 				rval = EFAULT;
1494*0Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
1495*0Sstevel@tonic-gate 			break;
1496*0Sstevel@tonic-gate 		}
1497*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
1498*0Sstevel@tonic-gate 
1499*0Sstevel@tonic-gate 		break;
1500*0Sstevel@tonic-gate 
1501*0Sstevel@tonic-gate 	case DKIOCSVTOC:
1502*0Sstevel@tonic-gate 
1503*0Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
1504*0Sstevel@tonic-gate 		switch (ddi_model_convert_from(flag & FMODELS)) {
1505*0Sstevel@tonic-gate 		case DDI_MODEL_ILP32:
1506*0Sstevel@tonic-gate 		{
1507*0Sstevel@tonic-gate 			struct vtoc32	vtoc32;
1508*0Sstevel@tonic-gate 
1509*0Sstevel@tonic-gate 			if (ddi_copyin((void *)arg, &vtoc32,
1510*0Sstevel@tonic-gate 			    sizeof (vtoc32), flag)) {
1511*0Sstevel@tonic-gate 				rval = EFAULT;
1512*0Sstevel@tonic-gate 				break;
1513*0Sstevel@tonic-gate 			}
1514*0Sstevel@tonic-gate 
1515*0Sstevel@tonic-gate 			vtoc32tovtoc(vtoc32, vtoc);
1516*0Sstevel@tonic-gate 
1517*0Sstevel@tonic-gate 			break;
1518*0Sstevel@tonic-gate 		}
1519*0Sstevel@tonic-gate 		case DDI_MODEL_NONE:
1520*0Sstevel@tonic-gate 
1521*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
1522*0Sstevel@tonic-gate 			if (ddi_copyin((void *)arg, &vtoc, sizeof (vtoc), flag))
1523*0Sstevel@tonic-gate 				rval = EFAULT;
1524*0Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
1525*0Sstevel@tonic-gate 			break;
1526*0Sstevel@tonic-gate 		}
1527*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
1528*0Sstevel@tonic-gate 
1529*0Sstevel@tonic-gate 		if (rval != 0)
1530*0Sstevel@tonic-gate 			break;
1531*0Sstevel@tonic-gate 
1532*0Sstevel@tonic-gate 
1533*0Sstevel@tonic-gate 		label = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);
1534*0Sstevel@tonic-gate 
1535*0Sstevel@tonic-gate 		mutex_enter(&fjp->fj_lock);
1536*0Sstevel@tonic-gate 
1537*0Sstevel@tonic-gate 		if ((rval = fd_build_label_vtoc(fjp, fdp, &vtoc, label)) == 0) {
1538*0Sstevel@tonic-gate 			fjp->fj_ops->fco_select(fjp, unit, 1);
1539*0Sstevel@tonic-gate 			rval = fjp->fj_ops->fco_rw(fjp, unit, FDWRITE,
1540*0Sstevel@tonic-gate 			    0, 0, 1, (caddr_t)label, sizeof (struct dk_label));
1541*0Sstevel@tonic-gate 			fjp->fj_ops->fco_select(fjp, unit, 0);
1542*0Sstevel@tonic-gate 		}
1543*0Sstevel@tonic-gate 		mutex_exit(&fjp->fj_lock);
1544*0Sstevel@tonic-gate 		kmem_free(label, sizeof (struct dk_label));
1545*0Sstevel@tonic-gate 		break;
1546*0Sstevel@tonic-gate 
1547*0Sstevel@tonic-gate 	case DKIOCSTATE:
1548*0Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_IOCT,
1549*0Sstevel@tonic-gate 		    (CE_CONT, "fd_ioctl fd unit %d: DKIOCSTATE\n", unit));
1550*0Sstevel@tonic-gate 
1551*0Sstevel@tonic-gate 		if (ddi_copyin((void *)arg, &state, sizeof (int), flag)) {
1552*0Sstevel@tonic-gate 			rval = EFAULT;
1553*0Sstevel@tonic-gate 			break;
1554*0Sstevel@tonic-gate 		}
1555*0Sstevel@tonic-gate 
1556*0Sstevel@tonic-gate 		rval = fd_check_media(dev, state);
1557*0Sstevel@tonic-gate 
1558*0Sstevel@tonic-gate 		if (ddi_copyout(&fdp->d_media_state, (void *)arg,
1559*0Sstevel@tonic-gate 		    sizeof (int), flag))
1560*0Sstevel@tonic-gate 			rval = EFAULT;
1561*0Sstevel@tonic-gate 		break;
1562*0Sstevel@tonic-gate 
1563*0Sstevel@tonic-gate 	case FDIOGCHAR:
1564*0Sstevel@tonic-gate 		if (ddi_copyout(fjp->fj_chars, (void *)arg,
1565*0Sstevel@tonic-gate 		    sizeof (struct fd_char), flag))
1566*0Sstevel@tonic-gate 			rval = EFAULT;
1567*0Sstevel@tonic-gate 		break;
1568*0Sstevel@tonic-gate 
1569*0Sstevel@tonic-gate 	case FDIOSCHAR:
1570*0Sstevel@tonic-gate 		if (ddi_copyin((void *)arg, &cpy.fdchar,
1571*0Sstevel@tonic-gate 		    sizeof (struct fd_char), flag)) {
1572*0Sstevel@tonic-gate 			rval = EFAULT;
1573*0Sstevel@tonic-gate 			break;
1574*0Sstevel@tonic-gate 		}
1575*0Sstevel@tonic-gate 		switch (cpy.fdchar.fdc_transfer_rate) {
1576*0Sstevel@tonic-gate 		case 417:
1577*0Sstevel@tonic-gate 			if ((fdp->d_media & (1 << FMT_3M)) == 0) {
1578*0Sstevel@tonic-gate 				cmn_err(CE_CONT,
1579*0Sstevel@tonic-gate 				    "fdioschar:Medium density not supported\n");
1580*0Sstevel@tonic-gate 				rval = EINVAL;
1581*0Sstevel@tonic-gate 				break;
1582*0Sstevel@tonic-gate 			}
1583*0Sstevel@tonic-gate 			mutex_enter(&fjp->fj_lock);
1584*0Sstevel@tonic-gate 			fjp->fj_attr->fda_rotatespd = 360;
1585*0Sstevel@tonic-gate 			mutex_exit(&fjp->fj_lock);
1586*0Sstevel@tonic-gate 			/* cpy.fdchar.fdc_transfer_rate = 500; */
1587*0Sstevel@tonic-gate 			/* FALLTHROUGH */
1588*0Sstevel@tonic-gate 		case 1000:
1589*0Sstevel@tonic-gate 		case 500:
1590*0Sstevel@tonic-gate 		case 300:
1591*0Sstevel@tonic-gate 		case 250:
1592*0Sstevel@tonic-gate 			mutex_enter(&fjp->fj_lock);
1593*0Sstevel@tonic-gate 			*(fjp->fj_chars) = cpy.fdchar;
1594*0Sstevel@tonic-gate 			fdp->d_curfdtype = -1;
1595*0Sstevel@tonic-gate 			fjp->fj_flags &= ~FUNIT_CHAROK;
1596*0Sstevel@tonic-gate 			mutex_exit(&fjp->fj_lock);
1597*0Sstevel@tonic-gate 
1598*0Sstevel@tonic-gate 			break;
1599*0Sstevel@tonic-gate 
1600*0Sstevel@tonic-gate 		default:
1601*0Sstevel@tonic-gate 			FDERRPRINT(FDEP_L4, FDEM_IOCT,
1602*0Sstevel@tonic-gate 			    (CE_WARN, "fd_ioctl fd unit %d: FDIOSCHAR odd "
1603*0Sstevel@tonic-gate 				"xfer rate %dkbs",
1604*0Sstevel@tonic-gate 				unit, cpy.fdchar.fdc_transfer_rate));
1605*0Sstevel@tonic-gate 			rval = EINVAL;
1606*0Sstevel@tonic-gate 			break;
1607*0Sstevel@tonic-gate 		}
1608*0Sstevel@tonic-gate 		break;
1609*0Sstevel@tonic-gate 
1610*0Sstevel@tonic-gate 	/*
1611*0Sstevel@tonic-gate 	 * set all characteristics and geometry to the defaults
1612*0Sstevel@tonic-gate 	 */
1613*0Sstevel@tonic-gate 	case FDDEFGEOCHAR:
1614*0Sstevel@tonic-gate 		mutex_enter(&fjp->fj_lock);
1615*0Sstevel@tonic-gate 		fdp->d_curfdtype = fdp->d_deffdtype;
1616*0Sstevel@tonic-gate 		*fjp->fj_chars = *defchar[fdp->d_curfdtype];
1617*0Sstevel@tonic-gate 		*fjp->fj_attr = fdtypes[fdp->d_curfdtype];
1618*0Sstevel@tonic-gate 		bcopy(fdparts[fdp->d_curfdtype],
1619*0Sstevel@tonic-gate 		    fdp->d_part, sizeof (struct partition) * NDKMAP);
1620*0Sstevel@tonic-gate 		fjp->fj_flags &= ~FUNIT_CHAROK;
1621*0Sstevel@tonic-gate 		mutex_exit(&fjp->fj_lock);
1622*0Sstevel@tonic-gate 		break;
1623*0Sstevel@tonic-gate 
1624*0Sstevel@tonic-gate 	case FDEJECT:  /* eject disk */
1625*0Sstevel@tonic-gate 	case DKIOCEJECT:
1626*0Sstevel@tonic-gate 		fjp->fj_flags &= ~(FUNIT_LABELOK | FUNIT_UNLABELED);
1627*0Sstevel@tonic-gate 		rval = ENOSYS;
1628*0Sstevel@tonic-gate 		break;
1629*0Sstevel@tonic-gate 
1630*0Sstevel@tonic-gate 	case FDGETCHANGE: /* disk changed */
1631*0Sstevel@tonic-gate 		if (ddi_copyin((void *)arg, &cpy.temp, sizeof (int), flag)) {
1632*0Sstevel@tonic-gate 			rval = EFAULT;
1633*0Sstevel@tonic-gate 			break;
1634*0Sstevel@tonic-gate 		}
1635*0Sstevel@tonic-gate 		mutex_enter(&fjp->fj_lock);
1636*0Sstevel@tonic-gate 		fjp->fj_ops->fco_select(fjp, unit, 1);
1637*0Sstevel@tonic-gate 
1638*0Sstevel@tonic-gate 		if (fjp->fj_flags & FUNIT_CHANGED)
1639*0Sstevel@tonic-gate 			cpy.temp |= FDGC_HISTORY;
1640*0Sstevel@tonic-gate 		else
1641*0Sstevel@tonic-gate 			cpy.temp &= ~FDGC_HISTORY;
1642*0Sstevel@tonic-gate 		fjp->fj_flags &= ~FUNIT_CHANGED;
1643*0Sstevel@tonic-gate 
1644*0Sstevel@tonic-gate 		if (fjp->fj_ops->fco_getchng(fjp, unit)) {
1645*0Sstevel@tonic-gate 			cpy.temp |= FDGC_DETECTED;
1646*0Sstevel@tonic-gate 			fjp->fj_ops->fco_resetchng(fjp, unit);
1647*0Sstevel@tonic-gate 			/*
1648*0Sstevel@tonic-gate 			 * check diskette again only if it was removed
1649*0Sstevel@tonic-gate 			 */
1650*0Sstevel@tonic-gate 			if (fjp->fj_ops->fco_getchng(fjp, unit)) {
1651*0Sstevel@tonic-gate 				/*
1652*0Sstevel@tonic-gate 				 * no diskette is present
1653*0Sstevel@tonic-gate 				 */
1654*0Sstevel@tonic-gate 				cpy.temp |= FDGC_CURRENT;
1655*0Sstevel@tonic-gate 				if (fjp->fj_flags & FUNIT_CHGDET)
1656*0Sstevel@tonic-gate 					/*
1657*0Sstevel@tonic-gate 					 * again no diskette; not a new change
1658*0Sstevel@tonic-gate 					 */
1659*0Sstevel@tonic-gate 					cpy.temp ^= FDGC_DETECTED;
1660*0Sstevel@tonic-gate 				else
1661*0Sstevel@tonic-gate 					fjp->fj_flags |= FUNIT_CHGDET;
1662*0Sstevel@tonic-gate 			} else {
1663*0Sstevel@tonic-gate 				/*
1664*0Sstevel@tonic-gate 				 * a new diskette is present
1665*0Sstevel@tonic-gate 				 */
1666*0Sstevel@tonic-gate 				cpy.temp &= ~FDGC_CURRENT;
1667*0Sstevel@tonic-gate 				fjp->fj_flags &= ~FUNIT_CHGDET;
1668*0Sstevel@tonic-gate 			}
1669*0Sstevel@tonic-gate 		} else {
1670*0Sstevel@tonic-gate 			cpy.temp &= ~(FDGC_DETECTED | FDGC_CURRENT);
1671*0Sstevel@tonic-gate 			fjp->fj_flags &= ~FUNIT_CHGDET;
1672*0Sstevel@tonic-gate 		}
1673*0Sstevel@tonic-gate 		/*
1674*0Sstevel@tonic-gate 		 * also get state of write protection
1675*0Sstevel@tonic-gate 		 */
1676*0Sstevel@tonic-gate 		if (fjp->fj_flags & FUNIT_WPROT) {
1677*0Sstevel@tonic-gate 			cpy.temp |= FDGC_CURWPROT;
1678*0Sstevel@tonic-gate 		} else {
1679*0Sstevel@tonic-gate 			cpy.temp &= ~FDGC_CURWPROT;
1680*0Sstevel@tonic-gate 		}
1681*0Sstevel@tonic-gate 		fjp->fj_ops->fco_select(fjp, unit, 0);
1682*0Sstevel@tonic-gate 		mutex_exit(&fjp->fj_lock);
1683*0Sstevel@tonic-gate 
1684*0Sstevel@tonic-gate 		if (ddi_copyout(&cpy.temp, (void *)arg, sizeof (int), flag))
1685*0Sstevel@tonic-gate 			rval = EFAULT;
1686*0Sstevel@tonic-gate 		break;
1687*0Sstevel@tonic-gate 
1688*0Sstevel@tonic-gate 	case FDGETDRIVECHAR:
1689*0Sstevel@tonic-gate 		if (ddi_copyout(fjp->fj_drive, (void *)arg,
1690*0Sstevel@tonic-gate 		    sizeof (struct fd_drive), flag))
1691*0Sstevel@tonic-gate 			rval = EFAULT;
1692*0Sstevel@tonic-gate 		break;
1693*0Sstevel@tonic-gate 
1694*0Sstevel@tonic-gate 	case FDSETDRIVECHAR:
1695*0Sstevel@tonic-gate 		if (ddi_copyin((void *)arg, &cpy.drvchar,
1696*0Sstevel@tonic-gate 		    sizeof (struct fd_drive), flag)) {
1697*0Sstevel@tonic-gate 			rval = EFAULT;
1698*0Sstevel@tonic-gate 			break;
1699*0Sstevel@tonic-gate 		}
1700*0Sstevel@tonic-gate 		mutex_enter(&fjp->fj_lock);
1701*0Sstevel@tonic-gate 		*(fjp->fj_drive) = cpy.drvchar;
1702*0Sstevel@tonic-gate 		fdp->d_curfdtype = -1;
1703*0Sstevel@tonic-gate 		fjp->fj_flags &= ~FUNIT_CHAROK;
1704*0Sstevel@tonic-gate 		mutex_exit(&fjp->fj_lock);
1705*0Sstevel@tonic-gate 		break;
1706*0Sstevel@tonic-gate 
1707*0Sstevel@tonic-gate 	case DKIOCREMOVABLE: {
1708*0Sstevel@tonic-gate 		int	i = 1;
1709*0Sstevel@tonic-gate 
1710*0Sstevel@tonic-gate 		/* no brainer: floppies are always removable */
1711*0Sstevel@tonic-gate 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag)) {
1712*0Sstevel@tonic-gate 			rval = EFAULT;
1713*0Sstevel@tonic-gate 		}
1714*0Sstevel@tonic-gate 		break;
1715*0Sstevel@tonic-gate 	}
1716*0Sstevel@tonic-gate 
1717*0Sstevel@tonic-gate 	case DKIOCGMEDIAINFO:
1718*0Sstevel@tonic-gate 		rval = fd_get_media_info(fjp, (caddr_t)arg, flag);
1719*0Sstevel@tonic-gate 		break;
1720*0Sstevel@tonic-gate 
1721*0Sstevel@tonic-gate 	case FDIOCMD:
1722*0Sstevel@tonic-gate 	{
1723*0Sstevel@tonic-gate 		struct fd_cmd fc;
1724*0Sstevel@tonic-gate 		int cyl, head, spc, spt;
1725*0Sstevel@tonic-gate 
1726*0Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
1727*0Sstevel@tonic-gate 		switch (ddi_model_convert_from(flag & FMODELS)) {
1728*0Sstevel@tonic-gate 		case DDI_MODEL_ILP32:
1729*0Sstevel@tonic-gate 		{
1730*0Sstevel@tonic-gate 			struct fd_cmd32 fc32;
1731*0Sstevel@tonic-gate 
1732*0Sstevel@tonic-gate 			if (ddi_copyin((void *)arg, &fc32,
1733*0Sstevel@tonic-gate 			    sizeof (fc32), flag)) {
1734*0Sstevel@tonic-gate 				rval = EFAULT;
1735*0Sstevel@tonic-gate 				break;
1736*0Sstevel@tonic-gate 			}
1737*0Sstevel@tonic-gate 
1738*0Sstevel@tonic-gate 			fc.fdc_cmd = fc32.fdc_cmd;
1739*0Sstevel@tonic-gate 			fc.fdc_flags = fc32.fdc_flags;
1740*0Sstevel@tonic-gate 			fc.fdc_blkno = fc32.fdc_blkno;
1741*0Sstevel@tonic-gate 			fc.fdc_secnt = fc32.fdc_secnt;
1742*0Sstevel@tonic-gate 			fc.fdc_bufaddr = (caddr_t)(uintptr_t)fc32.fdc_bufaddr;
1743*0Sstevel@tonic-gate 			fc.fdc_buflen = fc32.fdc_buflen;
1744*0Sstevel@tonic-gate 
1745*0Sstevel@tonic-gate 			break;
1746*0Sstevel@tonic-gate 		}
1747*0Sstevel@tonic-gate 		case DDI_MODEL_NONE:
1748*0Sstevel@tonic-gate 
1749*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
1750*0Sstevel@tonic-gate 
1751*0Sstevel@tonic-gate 			if (ddi_copyin((void *)arg, &fc, sizeof (fc), flag)) {
1752*0Sstevel@tonic-gate 				rval = EFAULT;
1753*0Sstevel@tonic-gate 				break;
1754*0Sstevel@tonic-gate 			}
1755*0Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
1756*0Sstevel@tonic-gate 			break;
1757*0Sstevel@tonic-gate 		}
1758*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
1759*0Sstevel@tonic-gate 
1760*0Sstevel@tonic-gate 		if (rval != 0)
1761*0Sstevel@tonic-gate 			break;
1762*0Sstevel@tonic-gate 
1763*0Sstevel@tonic-gate 	if (fc.fdc_cmd == FDCMD_READ || fc.fdc_cmd == FDCMD_WRITE) {
1764*0Sstevel@tonic-gate 			auto struct iovec aiov;
1765*0Sstevel@tonic-gate 			auto struct uio auio;
1766*0Sstevel@tonic-gate 			struct uio *uio = &auio;
1767*0Sstevel@tonic-gate 
1768*0Sstevel@tonic-gate 			spc = (fc.fdc_cmd == FDCMD_READ)? B_READ: B_WRITE;
1769*0Sstevel@tonic-gate 
1770*0Sstevel@tonic-gate 			bzero(&auio, sizeof (struct uio));
1771*0Sstevel@tonic-gate 			bzero(&aiov, sizeof (struct iovec));
1772*0Sstevel@tonic-gate 			aiov.iov_base = fc.fdc_bufaddr;
1773*0Sstevel@tonic-gate 			aiov.iov_len = (uint_t)fc.fdc_secnt *
1774*0Sstevel@tonic-gate 			    fjp->fj_chars->fdc_sec_size;
1775*0Sstevel@tonic-gate 			uio->uio_iov = &aiov;
1776*0Sstevel@tonic-gate 
1777*0Sstevel@tonic-gate 			uio->uio_iovcnt = 1;
1778*0Sstevel@tonic-gate 			uio->uio_resid = aiov.iov_len;
1779*0Sstevel@tonic-gate 			uio->uio_segflg = UIO_USERSPACE;
1780*0Sstevel@tonic-gate 
1781*0Sstevel@tonic-gate 			rval = physio(fd_strategy, (struct buf *)0, dev,
1782*0Sstevel@tonic-gate 			    spc, minphys, uio);
1783*0Sstevel@tonic-gate 			break;
1784*0Sstevel@tonic-gate 		} else if (fc.fdc_cmd == FDCMD_FORMAT_TRACK) {
1785*0Sstevel@tonic-gate 			spt = fjp->fj_chars->fdc_secptrack;	/* sec/trk */
1786*0Sstevel@tonic-gate 			spc = fjp->fj_chars->fdc_nhead * spt;	/* sec/cyl */
1787*0Sstevel@tonic-gate 			cyl = fc.fdc_blkno / spc;
1788*0Sstevel@tonic-gate 			head = (fc.fdc_blkno % spc) / spt;
1789*0Sstevel@tonic-gate 			if ((cyl | head) == 0)
1790*0Sstevel@tonic-gate 				fjp->fj_flags &=
1791*0Sstevel@tonic-gate 				    ~(FUNIT_LABELOK | FUNIT_UNLABELED);
1792*0Sstevel@tonic-gate 
1793*0Sstevel@tonic-gate 			FDERRPRINT(FDEP_L0, FDEM_FORM,
1794*0Sstevel@tonic-gate 			    (CE_CONT, "fd_format cyl %d, hd %d\n", cyl, head));
1795*0Sstevel@tonic-gate 			fjp->fj_ops->fco_select(fjp, unit, 1);
1796*0Sstevel@tonic-gate 			rval = fjp->fj_ops->fco_format(fjp, unit, cyl, head,
1797*0Sstevel@tonic-gate 			    (int)fc.fdc_flags);
1798*0Sstevel@tonic-gate 			fjp->fj_ops->fco_select(fjp, unit, 0);
1799*0Sstevel@tonic-gate 
1800*0Sstevel@tonic-gate 			break;
1801*0Sstevel@tonic-gate 		}
1802*0Sstevel@tonic-gate 		FDERRPRINT(FDEP_L4, FDEM_IOCT,
1803*0Sstevel@tonic-gate 		    (CE_WARN, "fd_ioctl fd unit %d: FDIOCSCMD not yet complete",
1804*0Sstevel@tonic-gate 		    unit));
1805*0Sstevel@tonic-gate 		rval = EINVAL;
1806*0Sstevel@tonic-gate 		break;
1807*0Sstevel@tonic-gate 	}
1808*0Sstevel@tonic-gate 
1809*0Sstevel@tonic-gate 	case FDRAW:
1810*0Sstevel@tonic-gate 		rval = fd_rawioctl(fjp, unit, (caddr_t)arg, flag);
1811*0Sstevel@tonic-gate 		break;
1812*0Sstevel@tonic-gate 
1813*0Sstevel@tonic-gate 	default:
1814*0Sstevel@tonic-gate 		FDERRPRINT(FDEP_L4, FDEM_IOCT,
1815*0Sstevel@tonic-gate 		    (CE_WARN, "fd_ioctl fd unit %d: invalid ioctl 0x%x",
1816*0Sstevel@tonic-gate 		    unit, cmd));
1817*0Sstevel@tonic-gate 		rval = ENOTTY;
1818*0Sstevel@tonic-gate 		break;
1819*0Sstevel@tonic-gate 	}
1820*0Sstevel@tonic-gate 	return (rval);
1821*0Sstevel@tonic-gate }
1822*0Sstevel@tonic-gate 
1823*0Sstevel@tonic-gate static void
1824*0Sstevel@tonic-gate fd_build_user_vtoc(struct fcu_obj *fjp, struct fdisk *fdp, struct vtoc *vtocp)
1825*0Sstevel@tonic-gate {
1826*0Sstevel@tonic-gate 	struct partition *vpart;
1827*0Sstevel@tonic-gate 	int	i;
1828*0Sstevel@tonic-gate 	int	xblk;
1829*0Sstevel@tonic-gate 
1830*0Sstevel@tonic-gate 	/*
1831*0Sstevel@tonic-gate 	 * Return vtoc structure fields in the provided VTOC area, addressed
1832*0Sstevel@tonic-gate 	 * by *vtocp.
1833*0Sstevel@tonic-gate 	 *
1834*0Sstevel@tonic-gate 	 */
1835*0Sstevel@tonic-gate 	bzero(vtocp, sizeof (struct vtoc));
1836*0Sstevel@tonic-gate 
1837*0Sstevel@tonic-gate 	bcopy(fdp->d_vtoc_bootinfo,
1838*0Sstevel@tonic-gate 	    vtocp->v_bootinfo, sizeof (vtocp->v_bootinfo));
1839*0Sstevel@tonic-gate 
1840*0Sstevel@tonic-gate 	vtocp->v_sanity = VTOC_SANE;
1841*0Sstevel@tonic-gate 	vtocp->v_version = fdp->d_vtoc_version;
1842*0Sstevel@tonic-gate 	bcopy(fdp->d_vtoc_volume, vtocp->v_volume, LEN_DKL_VVOL);
1843*0Sstevel@tonic-gate 	if (fjp->fj_flags & FUNIT_LABELOK) {
1844*0Sstevel@tonic-gate 		vtocp->v_sectorsz = DEV_BSIZE;
1845*0Sstevel@tonic-gate 		xblk = 1;
1846*0Sstevel@tonic-gate 	} else {
1847*0Sstevel@tonic-gate 		vtocp->v_sectorsz = fjp->fj_chars->fdc_sec_size;
1848*0Sstevel@tonic-gate 		xblk = vtocp->v_sectorsz / DEV_BSIZE;
1849*0Sstevel@tonic-gate 	}
1850*0Sstevel@tonic-gate 	vtocp->v_nparts = 3;	/* <= NDKMAP;	*/
1851*0Sstevel@tonic-gate 
1852*0Sstevel@tonic-gate 	/*
1853*0Sstevel@tonic-gate 	 * Copy partitioning information.
1854*0Sstevel@tonic-gate 	 */
1855*0Sstevel@tonic-gate 	bcopy(fdp->d_part, vtocp->v_part, sizeof (struct partition) * NDKMAP);
1856*0Sstevel@tonic-gate 	for (i = NDKMAP, vpart = vtocp->v_part; i && (xblk > 1); i--, vpart++) {
1857*0Sstevel@tonic-gate 		/* correct partition info if sector size > 512 bytes */
1858*0Sstevel@tonic-gate 		vpart->p_start /= xblk;
1859*0Sstevel@tonic-gate 		vpart->p_size /= xblk;
1860*0Sstevel@tonic-gate 	}
1861*0Sstevel@tonic-gate 
1862*0Sstevel@tonic-gate 	bcopy(fdp->d_vtoc_timestamp,
1863*0Sstevel@tonic-gate 	    vtocp->timestamp, sizeof (fdp->d_vtoc_timestamp));
1864*0Sstevel@tonic-gate 	bcopy(fdp->d_vtoc_asciilabel, vtocp->v_asciilabel, LEN_DKL_ASCII);
1865*0Sstevel@tonic-gate }
1866*0Sstevel@tonic-gate 
1867*0Sstevel@tonic-gate 
1868*0Sstevel@tonic-gate static int
1869*0Sstevel@tonic-gate fd_build_label_vtoc(struct fcu_obj *fjp, struct fdisk *fdp, struct vtoc *vtocp,
1870*0Sstevel@tonic-gate     struct dk_label *labelp)
1871*0Sstevel@tonic-gate {
1872*0Sstevel@tonic-gate 	struct partition *vpart;
1873*0Sstevel@tonic-gate 	int	i;
1874*0Sstevel@tonic-gate 	int	nblks;
1875*0Sstevel@tonic-gate 	int	ncyl;
1876*0Sstevel@tonic-gate 	ushort_t sum, *sp;
1877*0Sstevel@tonic-gate 
1878*0Sstevel@tonic-gate 
1879*0Sstevel@tonic-gate 	/*
1880*0Sstevel@tonic-gate 	 * Sanity-check the vtoc
1881*0Sstevel@tonic-gate 	 */
1882*0Sstevel@tonic-gate 	if (vtocp->v_sanity != VTOC_SANE ||
1883*0Sstevel@tonic-gate 	    vtocp->v_nparts > NDKMAP || vtocp->v_nparts <= 0) {
1884*0Sstevel@tonic-gate 		FDERRPRINT(FDEP_L3, FDEM_IOCT,
1885*0Sstevel@tonic-gate 		    (CE_WARN, "fd_build_label:  sanity check on vtoc failed"));
1886*0Sstevel@tonic-gate 		return (EINVAL);
1887*0Sstevel@tonic-gate 	}
1888*0Sstevel@tonic-gate 
1889*0Sstevel@tonic-gate 	/*
1890*0Sstevel@tonic-gate 	 * before copying the vtoc, the partition information in it should be
1891*0Sstevel@tonic-gate 	 * checked against the information the driver already has on the
1892*0Sstevel@tonic-gate 	 * diskette.
1893*0Sstevel@tonic-gate 	 */
1894*0Sstevel@tonic-gate 
1895*0Sstevel@tonic-gate 	nblks = (fjp->fj_chars->fdc_nhead * fjp->fj_chars->fdc_secptrack *
1896*0Sstevel@tonic-gate 		fjp->fj_chars->fdc_sec_size) / DEV_BSIZE;
1897*0Sstevel@tonic-gate 	if (nblks == 0 || fjp->fj_chars->fdc_ncyl == 0)
1898*0Sstevel@tonic-gate 		return (EFAULT);
1899*0Sstevel@tonic-gate 	vpart = vtocp->v_part;
1900*0Sstevel@tonic-gate 
1901*0Sstevel@tonic-gate 	/*
1902*0Sstevel@tonic-gate 	 * Check the partition information in the vtoc.  The starting sectors
1903*0Sstevel@tonic-gate 	 * must lie along cylinder boundaries. (NDKMAP entries are checked
1904*0Sstevel@tonic-gate 	 * to ensure that the unused entries are set to 0 if vtoc->v_nparts
1905*0Sstevel@tonic-gate 	 * is less than NDKMAP)
1906*0Sstevel@tonic-gate 	 */
1907*0Sstevel@tonic-gate 	for (i = NDKMAP; i; i--) {
1908*0Sstevel@tonic-gate 		if ((vpart->p_start % nblks) != 0) {
1909*0Sstevel@tonic-gate 			return (EINVAL);
1910*0Sstevel@tonic-gate 		}
1911*0Sstevel@tonic-gate 		ncyl = vpart->p_start / nblks;
1912*0Sstevel@tonic-gate 		ncyl += vpart->p_size / nblks;
1913*0Sstevel@tonic-gate 		if ((vpart->p_size % nblks) != 0)
1914*0Sstevel@tonic-gate 			ncyl++;
1915*0Sstevel@tonic-gate 		if (ncyl > (long)fjp->fj_chars->fdc_ncyl) {
1916*0Sstevel@tonic-gate 			return (EINVAL);
1917*0Sstevel@tonic-gate 		}
1918*0Sstevel@tonic-gate 		vpart++;
1919*0Sstevel@tonic-gate 	}
1920*0Sstevel@tonic-gate 
1921*0Sstevel@tonic-gate 
1922*0Sstevel@tonic-gate 	bcopy(vtocp->v_bootinfo, fdp->d_vtoc_bootinfo,
1923*0Sstevel@tonic-gate 	    sizeof (vtocp->v_bootinfo));
1924*0Sstevel@tonic-gate 	fdp->d_vtoc_version = vtocp->v_version;
1925*0Sstevel@tonic-gate 	bcopy(vtocp->v_volume, fdp->d_vtoc_volume, LEN_DKL_VVOL);
1926*0Sstevel@tonic-gate 
1927*0Sstevel@tonic-gate 	/*
1928*0Sstevel@tonic-gate 	 * Copy partitioning information.
1929*0Sstevel@tonic-gate 	 */
1930*0Sstevel@tonic-gate 	bcopy(vtocp->v_part, fdp->d_part, sizeof (struct partition) * NDKMAP);
1931*0Sstevel@tonic-gate 	bcopy(vtocp->timestamp, fdp->d_vtoc_timestamp,
1932*0Sstevel@tonic-gate 	    sizeof (fdp->d_vtoc_timestamp));
1933*0Sstevel@tonic-gate 	bcopy(vtocp->v_asciilabel, fdp->d_vtoc_asciilabel, LEN_DKL_ASCII);
1934*0Sstevel@tonic-gate 
1935*0Sstevel@tonic-gate 	/*
1936*0Sstevel@tonic-gate 	 * construct the diskette label in supplied buffer
1937*0Sstevel@tonic-gate 	 */
1938*0Sstevel@tonic-gate 
1939*0Sstevel@tonic-gate 	/* Put appropriate vtoc structure fields into the disk label */
1940*0Sstevel@tonic-gate 	labelp->dkl_vtoc.v_bootinfo[0] = (uint32_t)vtocp->v_bootinfo[0];
1941*0Sstevel@tonic-gate 	labelp->dkl_vtoc.v_bootinfo[1] = (uint32_t)vtocp->v_bootinfo[1];
1942*0Sstevel@tonic-gate 	labelp->dkl_vtoc.v_bootinfo[2] = (uint32_t)vtocp->v_bootinfo[2];
1943*0Sstevel@tonic-gate 
1944*0Sstevel@tonic-gate 	labelp->dkl_vtoc.v_sanity = vtocp->v_sanity;
1945*0Sstevel@tonic-gate 	labelp->dkl_vtoc.v_version = vtocp->v_version;
1946*0Sstevel@tonic-gate 
1947*0Sstevel@tonic-gate 	bcopy(vtocp->v_volume, labelp->dkl_vtoc.v_volume, LEN_DKL_VVOL);
1948*0Sstevel@tonic-gate 
1949*0Sstevel@tonic-gate 	labelp->dkl_vtoc.v_nparts = vtocp->v_nparts;
1950*0Sstevel@tonic-gate 
1951*0Sstevel@tonic-gate 	bcopy(vtocp->v_reserved, labelp->dkl_vtoc.v_reserved,
1952*0Sstevel@tonic-gate 	    sizeof (labelp->dkl_vtoc.v_reserved));
1953*0Sstevel@tonic-gate 
1954*0Sstevel@tonic-gate 	for (i = 0; i < (int)vtocp->v_nparts; i++) {
1955*0Sstevel@tonic-gate 		labelp->dkl_vtoc.v_part[i].p_tag  = vtocp->v_part[i].p_tag;
1956*0Sstevel@tonic-gate 		labelp->dkl_vtoc.v_part[i].p_flag  = vtocp->v_part[i].p_flag;
1957*0Sstevel@tonic-gate 		labelp->dkl_vtoc.v_part[i].p_start  = vtocp->v_part[i].p_start;
1958*0Sstevel@tonic-gate 		labelp->dkl_vtoc.v_part[i].p_size  = vtocp->v_part[i].p_size;
1959*0Sstevel@tonic-gate 	}
1960*0Sstevel@tonic-gate 
1961*0Sstevel@tonic-gate 	for (i = 0; i < NDKMAP; i++) {
1962*0Sstevel@tonic-gate 		labelp->dkl_vtoc.v_timestamp[i] = vtocp->timestamp[i];
1963*0Sstevel@tonic-gate 	}
1964*0Sstevel@tonic-gate 	bcopy(vtocp->v_asciilabel, labelp->dkl_asciilabel, LEN_DKL_ASCII);
1965*0Sstevel@tonic-gate 
1966*0Sstevel@tonic-gate 
1967*0Sstevel@tonic-gate 	labelp->dkl_pcyl = fjp->fj_chars->fdc_ncyl;
1968*0Sstevel@tonic-gate 	labelp->dkl_ncyl = fjp->fj_chars->fdc_ncyl;
1969*0Sstevel@tonic-gate 	labelp->dkl_nhead = fjp->fj_chars->fdc_nhead;
1970*0Sstevel@tonic-gate 	/*
1971*0Sstevel@tonic-gate 	 * The fdc_secptrack field of the fd_char structure is the number
1972*0Sstevel@tonic-gate 	 * of sectors per track where the sectors are fdc_sec_size.
1973*0Sstevel@tonic-gate 	 * The dkl_nsect field of the dk_label structure is the number of
1974*0Sstevel@tonic-gate 	 * DEV_BSIZE (512) byte sectors per track.
1975*0Sstevel@tonic-gate 	 */
1976*0Sstevel@tonic-gate 	labelp->dkl_nsect = (fjp->fj_chars->fdc_secptrack *
1977*0Sstevel@tonic-gate 	    fjp->fj_chars->fdc_sec_size) / DEV_BSIZE;
1978*0Sstevel@tonic-gate 	labelp->dkl_intrlv = fjp->fj_attr->fda_intrlv;
1979*0Sstevel@tonic-gate 	labelp->dkl_rpm = fjp->fj_attr->fda_rotatespd;
1980*0Sstevel@tonic-gate 	labelp->dkl_read_reinstruct =
1981*0Sstevel@tonic-gate 	    (int)(labelp->dkl_nsect * labelp->dkl_rpm * 4) / 60000;
1982*0Sstevel@tonic-gate 	labelp->dkl_write_reinstruct = labelp->dkl_read_reinstruct;
1983*0Sstevel@tonic-gate 
1984*0Sstevel@tonic-gate 	labelp->dkl_magic = DKL_MAGIC;
1985*0Sstevel@tonic-gate 
1986*0Sstevel@tonic-gate 	sum = 0;
1987*0Sstevel@tonic-gate 	labelp->dkl_cksum = 0;
1988*0Sstevel@tonic-gate 	sp = (ushort_t *)labelp;
1989*0Sstevel@tonic-gate 	while (sp < &(labelp->dkl_cksum)) {
1990*0Sstevel@tonic-gate 		sum ^= *sp++;
1991*0Sstevel@tonic-gate 	}
1992*0Sstevel@tonic-gate 	labelp->dkl_cksum = sum;
1993*0Sstevel@tonic-gate 
1994*0Sstevel@tonic-gate 	return (0);
1995*0Sstevel@tonic-gate }
1996*0Sstevel@tonic-gate 
1997*0Sstevel@tonic-gate static int
1998*0Sstevel@tonic-gate fd_rawioctl(struct fcu_obj *fjp, int unit, caddr_t arg, int mode)
1999*0Sstevel@tonic-gate {
2000*0Sstevel@tonic-gate 	struct fd_raw fdr;
2001*0Sstevel@tonic-gate 	char *arg_result = NULL;
2002*0Sstevel@tonic-gate 	int flag = B_READ;
2003*0Sstevel@tonic-gate 	int rval = 0;
2004*0Sstevel@tonic-gate 	caddr_t	uaddr;
2005*0Sstevel@tonic-gate 	uint_t ucount;
2006*0Sstevel@tonic-gate 
2007*0Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_RAWI,
2008*0Sstevel@tonic-gate 	    (CE_CONT, "fd_rawioctl: cmd[0]=0x%x\n", fdr.fdr_cmd[0]));
2009*0Sstevel@tonic-gate 
2010*0Sstevel@tonic-gate 	if (fjp->fj_chars->fdc_medium != 3 && fjp->fj_chars->fdc_medium != 5) {
2011*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "fd_rawioctl: Medium density not supported\n");
2012*0Sstevel@tonic-gate 		return (ENXIO);
2013*0Sstevel@tonic-gate 	}
2014*0Sstevel@tonic-gate 
2015*0Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
2016*0Sstevel@tonic-gate 	switch (ddi_model_convert_from(mode & FMODELS)) {
2017*0Sstevel@tonic-gate 	case DDI_MODEL_ILP32:
2018*0Sstevel@tonic-gate 	{
2019*0Sstevel@tonic-gate 		struct fd_raw32 fdr32;
2020*0Sstevel@tonic-gate 
2021*0Sstevel@tonic-gate 		if (ddi_copyin(arg, &fdr32, sizeof (fdr32), mode))
2022*0Sstevel@tonic-gate 			return (EFAULT);
2023*0Sstevel@tonic-gate 
2024*0Sstevel@tonic-gate 		bcopy(fdr32.fdr_cmd, fdr.fdr_cmd, sizeof (fdr.fdr_cmd));
2025*0Sstevel@tonic-gate 		fdr.fdr_cnum = fdr32.fdr_cnum;
2026*0Sstevel@tonic-gate 		fdr.fdr_nbytes = fdr32.fdr_nbytes;
2027*0Sstevel@tonic-gate 		fdr.fdr_addr = (caddr_t)(uintptr_t)fdr32.fdr_addr;
2028*0Sstevel@tonic-gate 		arg_result = ((struct fd_raw32 *)arg)->fdr_result;
2029*0Sstevel@tonic-gate 
2030*0Sstevel@tonic-gate 		break;
2031*0Sstevel@tonic-gate 	}
2032*0Sstevel@tonic-gate 	case DDI_MODEL_NONE:
2033*0Sstevel@tonic-gate #endif /* ! _MULTI_DATAMODEL */
2034*0Sstevel@tonic-gate 
2035*0Sstevel@tonic-gate 		if (ddi_copyin(arg, &fdr, sizeof (fdr), mode))
2036*0Sstevel@tonic-gate 			return (EFAULT);
2037*0Sstevel@tonic-gate 
2038*0Sstevel@tonic-gate 		arg_result = ((struct fd_raw *)arg)->fdr_result;
2039*0Sstevel@tonic-gate 
2040*0Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
2041*0Sstevel@tonic-gate 		break;
2042*0Sstevel@tonic-gate 	}
2043*0Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
2044*0Sstevel@tonic-gate 
2045*0Sstevel@tonic-gate 
2046*0Sstevel@tonic-gate 
2047*0Sstevel@tonic-gate 	/*
2048*0Sstevel@tonic-gate 	 * copy user address & nbytes from raw_req so that we can
2049*0Sstevel@tonic-gate 	 * put kernel address in req structure
2050*0Sstevel@tonic-gate 	 */
2051*0Sstevel@tonic-gate 	uaddr = fdr.fdr_addr;
2052*0Sstevel@tonic-gate 	ucount = (uint_t)fdr.fdr_nbytes;
2053*0Sstevel@tonic-gate 	unit &= 3;
2054*0Sstevel@tonic-gate 
2055*0Sstevel@tonic-gate 	switch (fdr.fdr_cmd[0] & 0x0f) {
2056*0Sstevel@tonic-gate 
2057*0Sstevel@tonic-gate 	case FDRAW_FORMAT:
2058*0Sstevel@tonic-gate 		ucount += 16;
2059*0Sstevel@tonic-gate 		fdr.fdr_addr = kmem_zalloc(ucount, KM_SLEEP);
2060*0Sstevel@tonic-gate 		if (ddi_copyin(uaddr, fdr.fdr_addr,
2061*0Sstevel@tonic-gate 		    (size_t)fdr.fdr_nbytes, mode)) {
2062*0Sstevel@tonic-gate 			kmem_free(fdr.fdr_addr, ucount);
2063*0Sstevel@tonic-gate 			return (EFAULT);
2064*0Sstevel@tonic-gate 		}
2065*0Sstevel@tonic-gate 		if ((*fdr.fdr_addr | fdr.fdr_addr[1]) == 0)
2066*0Sstevel@tonic-gate 			fjp->fj_flags &= ~(FUNIT_LABELOK | FUNIT_UNLABELED);
2067*0Sstevel@tonic-gate 		flag = B_WRITE;
2068*0Sstevel@tonic-gate 		fdr.fdr_cmd[1] = (fdr.fdr_cmd[1] & ~3) | unit;
2069*0Sstevel@tonic-gate 		break;
2070*0Sstevel@tonic-gate 
2071*0Sstevel@tonic-gate 	case FDRAW_WRCMD:
2072*0Sstevel@tonic-gate 	case FDRAW_WRITEDEL:
2073*0Sstevel@tonic-gate 		flag = B_WRITE;
2074*0Sstevel@tonic-gate 		/* FALLTHROUGH */
2075*0Sstevel@tonic-gate 	case FDRAW_RDCMD:
2076*0Sstevel@tonic-gate 	case FDRAW_READDEL:
2077*0Sstevel@tonic-gate 	case FDRAW_READTRACK:
2078*0Sstevel@tonic-gate 		if (ucount) {
2079*0Sstevel@tonic-gate 			/*
2080*0Sstevel@tonic-gate 			 * In SunOS 4.X, we used to as_fault things in.
2081*0Sstevel@tonic-gate 			 * We really cannot do this in 5.0/SVr4. Unless
2082*0Sstevel@tonic-gate 			 * someone really believes that speed is of the
2083*0Sstevel@tonic-gate 			 * essence here, it is just much simpler to do
2084*0Sstevel@tonic-gate 			 * this in kernel space and use copyin/copyout.
2085*0Sstevel@tonic-gate 			 */
2086*0Sstevel@tonic-gate 			fdr.fdr_addr = kmem_alloc((size_t)ucount, KM_SLEEP);
2087*0Sstevel@tonic-gate 			if (flag == B_WRITE) {
2088*0Sstevel@tonic-gate 				if (ddi_copyin(uaddr, fdr.fdr_addr, ucount,
2089*0Sstevel@tonic-gate 				    mode)) {
2090*0Sstevel@tonic-gate 					kmem_free(fdr.fdr_addr, ucount);
2091*0Sstevel@tonic-gate 					return (EFAULT);
2092*0Sstevel@tonic-gate 				}
2093*0Sstevel@tonic-gate 			}
2094*0Sstevel@tonic-gate 		} else
2095*0Sstevel@tonic-gate 			return (EINVAL);
2096*0Sstevel@tonic-gate 		fdr.fdr_cmd[1] = (fdr.fdr_cmd[1] & ~3) | unit;
2097*0Sstevel@tonic-gate 		break;
2098*0Sstevel@tonic-gate 
2099*0Sstevel@tonic-gate 	case FDRAW_READID:
2100*0Sstevel@tonic-gate 	case FDRAW_REZERO:
2101*0Sstevel@tonic-gate 	case FDRAW_SEEK:
2102*0Sstevel@tonic-gate 	case FDRAW_SENSE_DRV:
2103*0Sstevel@tonic-gate 		ucount = 0;
2104*0Sstevel@tonic-gate 		fdr.fdr_cmd[1] = (fdr.fdr_cmd[1] & ~3) | unit;
2105*0Sstevel@tonic-gate 		break;
2106*0Sstevel@tonic-gate 
2107*0Sstevel@tonic-gate 	case FDRAW_SPECIFY:
2108*0Sstevel@tonic-gate 		fdr.fdr_cmd[2] &= 0xfe;	/* keep NoDMA bit clear */
2109*0Sstevel@tonic-gate 		/* FALLTHROUGH */
2110*0Sstevel@tonic-gate 	case FDRAW_SENSE_INT:
2111*0Sstevel@tonic-gate 		ucount = 0;
2112*0Sstevel@tonic-gate 		break;
2113*0Sstevel@tonic-gate 
2114*0Sstevel@tonic-gate 	default:
2115*0Sstevel@tonic-gate 		return (EINVAL);
2116*0Sstevel@tonic-gate 	}
2117*0Sstevel@tonic-gate 
2118*0Sstevel@tonic-gate 	/*
2119*0Sstevel@tonic-gate 	 * Note that we ignore any error returns from controller
2120*0Sstevel@tonic-gate 	 * This is the way the driver has been, and it may be
2121*0Sstevel@tonic-gate 	 * that the raw ioctl senders simply don't want to
2122*0Sstevel@tonic-gate 	 * see any errors returned in this fashion.
2123*0Sstevel@tonic-gate 	 */
2124*0Sstevel@tonic-gate 
2125*0Sstevel@tonic-gate 	fjp->fj_ops->fco_select(fjp, unit, 1);
2126*0Sstevel@tonic-gate 	rval = fjp->fj_ops->fco_rwioctl(fjp, unit, (caddr_t)&fdr);
2127*0Sstevel@tonic-gate 
2128*0Sstevel@tonic-gate 	if (ucount && flag == B_READ && rval == 0) {
2129*0Sstevel@tonic-gate 		if (ddi_copyout(fdr.fdr_addr, uaddr, ucount, mode)) {
2130*0Sstevel@tonic-gate 			rval = EFAULT;
2131*0Sstevel@tonic-gate 		}
2132*0Sstevel@tonic-gate 	}
2133*0Sstevel@tonic-gate 	if (ddi_copyout(fdr.fdr_result, arg_result, sizeof (fdr.fdr_cmd), mode))
2134*0Sstevel@tonic-gate 		rval = EFAULT;
2135*0Sstevel@tonic-gate 
2136*0Sstevel@tonic-gate 	fjp->fj_ops->fco_select(fjp, unit, 0);
2137*0Sstevel@tonic-gate 	if (ucount)
2138*0Sstevel@tonic-gate 		kmem_free(fdr.fdr_addr, ucount);
2139*0Sstevel@tonic-gate 
2140*0Sstevel@tonic-gate 	return (rval);
2141*0Sstevel@tonic-gate }
2142*0Sstevel@tonic-gate 
2143*0Sstevel@tonic-gate /*
2144*0Sstevel@tonic-gate  * property operation routine.  return the number of blocks for the partition
2145*0Sstevel@tonic-gate  * in question or forward the request to the property facilities.
2146*0Sstevel@tonic-gate  */
2147*0Sstevel@tonic-gate static int
2148*0Sstevel@tonic-gate fd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2149*0Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
2150*0Sstevel@tonic-gate {
2151*0Sstevel@tonic-gate 	struct fcu_obj	*fjp = NULL;
2152*0Sstevel@tonic-gate 	struct fdisk	*fdp = NULL;
2153*0Sstevel@tonic-gate 	uint64_t	nblocks64;
2154*0Sstevel@tonic-gate 
2155*0Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_PROP,
2156*0Sstevel@tonic-gate 	    (CE_CONT, "fd_prop_op: dip %p %s\n", (void *)dip, name));
2157*0Sstevel@tonic-gate 
2158*0Sstevel@tonic-gate 	/*
2159*0Sstevel@tonic-gate 	 * Our dynamic properties are all device specific and size oriented.
2160*0Sstevel@tonic-gate 	 * Requests issued under conditions where size is valid are passed
2161*0Sstevel@tonic-gate 	 * to ddi_prop_op_nblocks with the size information, otherwise the
2162*0Sstevel@tonic-gate 	 * request is passed to ddi_prop_op.
2163*0Sstevel@tonic-gate 	 */
2164*0Sstevel@tonic-gate 	if (dev == DDI_DEV_T_ANY) {
2165*0Sstevel@tonic-gate pass:  		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
2166*0Sstevel@tonic-gate 		    name, valuep, lengthp));
2167*0Sstevel@tonic-gate 	} else {
2168*0Sstevel@tonic-gate 		/*
2169*0Sstevel@tonic-gate 		 * Ignoring return value because success is checked by
2170*0Sstevel@tonic-gate 		 * verifying fjp and fdp and returned unit value is not used.
2171*0Sstevel@tonic-gate 		 */
2172*0Sstevel@tonic-gate 		(void) fd_getdrive(dev, &fjp, &fdp);
2173*0Sstevel@tonic-gate 		if (!fjp || !fdp)
2174*0Sstevel@tonic-gate 			goto pass;
2175*0Sstevel@tonic-gate 
2176*0Sstevel@tonic-gate 		/* get nblocks value */
2177*0Sstevel@tonic-gate 		nblocks64 = (ulong_t)fdp->d_part[PARTITION(dev)].p_size;
2178*0Sstevel@tonic-gate 
2179*0Sstevel@tonic-gate 		return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags,
2180*0Sstevel@tonic-gate 		    name, valuep, lengthp, nblocks64));
2181*0Sstevel@tonic-gate 	}
2182*0Sstevel@tonic-gate }
2183*0Sstevel@tonic-gate 
2184*0Sstevel@tonic-gate static void
2185*0Sstevel@tonic-gate fd_media_watch(void *arg)
2186*0Sstevel@tonic-gate {
2187*0Sstevel@tonic-gate 	struct fcu_obj *fjp;
2188*0Sstevel@tonic-gate 	struct fdisk *fdp;
2189*0Sstevel@tonic-gate 
2190*0Sstevel@tonic-gate #ifdef DEBUG
2191*0Sstevel@tonic-gate 	int	unit;
2192*0Sstevel@tonic-gate #define	DEBUG_ASSIGN	unit=
2193*0Sstevel@tonic-gate #else
2194*0Sstevel@tonic-gate #define	DEBUG_ASSIGN	(void)
2195*0Sstevel@tonic-gate #endif
2196*0Sstevel@tonic-gate 	DEBUG_ASSIGN fd_getdrive((dev_t)arg, &fjp, &fdp);
2197*0Sstevel@tonic-gate 	/*
2198*0Sstevel@tonic-gate 	 * Ignoring return in non DEBUG mode because device exist.
2199*0Sstevel@tonic-gate 	 * Returned unit value is not used.
2200*0Sstevel@tonic-gate 	 */
2201*0Sstevel@tonic-gate 
2202*0Sstevel@tonic-gate 	FDERRPRINT(FDEP_L0, FDEM_IOCT,
2203*0Sstevel@tonic-gate 	    (CE_CONT, "fd_media_watch unit %d\n", unit));
2204*0Sstevel@tonic-gate 
2205*0Sstevel@tonic-gate 	/*
2206*0Sstevel@tonic-gate 	 * fd_get_media_state() cannot be called from this timeout function
2207*0Sstevel@tonic-gate 	 * because the  floppy drive has to be selected first, and that could
2208*0Sstevel@tonic-gate 	 * force this function to sleep (while waiting for the select
2209*0Sstevel@tonic-gate 	 * semaphore).
2210*0Sstevel@tonic-gate 	 * Instead, just wakeup up driver.
2211*0Sstevel@tonic-gate 	 */
2212*0Sstevel@tonic-gate 	mutex_enter(&fjp->fj_lock);
2213*0Sstevel@tonic-gate 	cv_broadcast(&fdp->d_statecv);
2214*0Sstevel@tonic-gate 	mutex_exit(&fjp->fj_lock);
2215*0Sstevel@tonic-gate }
2216*0Sstevel@tonic-gate 
2217*0Sstevel@tonic-gate enum dkio_state
2218*0Sstevel@tonic-gate fd_get_media_state(struct fcu_obj *fjp, int unit)
2219*0Sstevel@tonic-gate {
2220*0Sstevel@tonic-gate 	enum dkio_state state;
2221*0Sstevel@tonic-gate 
2222*0Sstevel@tonic-gate 	if (fjp->fj_ops->fco_getchng(fjp, unit)) {
2223*0Sstevel@tonic-gate 		/* recheck disk only if DSKCHG "high" */
2224*0Sstevel@tonic-gate 		fjp->fj_ops->fco_resetchng(fjp, unit);
2225*0Sstevel@tonic-gate 		if (fjp->fj_ops->fco_getchng(fjp, unit)) {
2226*0Sstevel@tonic-gate 			if (fjp->fj_flags & FUNIT_CHGDET) {
2227*0Sstevel@tonic-gate 				/*
2228*0Sstevel@tonic-gate 				 * again no diskette; not a new change
2229*0Sstevel@tonic-gate 				 */
2230*0Sstevel@tonic-gate 				state = DKIO_NONE;
2231*0Sstevel@tonic-gate 			} else {
2232*0Sstevel@tonic-gate 				/*
2233*0Sstevel@tonic-gate 				 * a new change; diskette was ejected
2234*0Sstevel@tonic-gate 				 */
2235*0Sstevel@tonic-gate 				fjp->fj_flags |= FUNIT_CHGDET;
2236*0Sstevel@tonic-gate 				state = DKIO_EJECTED;
2237*0Sstevel@tonic-gate 			}
2238*0Sstevel@tonic-gate 		} else {
2239*0Sstevel@tonic-gate 			fjp->fj_flags &= ~FUNIT_CHGDET;
2240*0Sstevel@tonic-gate 			state = DKIO_INSERTED;
2241*0Sstevel@tonic-gate 		}
2242*0Sstevel@tonic-gate 	} else {
2243*0Sstevel@tonic-gate 		fjp->fj_flags &= ~FUNIT_CHGDET;
2244*0Sstevel@tonic-gate 		state = DKIO_INSERTED;
2245*0Sstevel@tonic-gate 	}
2246*0Sstevel@tonic-gate 	FDERRPRINT(FDEP_L0, FDEM_IOCT,
2247*0Sstevel@tonic-gate 	    (CE_CONT, "fd_get_media_state unit %d: state %x\n", unit, state));
2248*0Sstevel@tonic-gate 	return (state);
2249*0Sstevel@tonic-gate }
2250*0Sstevel@tonic-gate 
2251*0Sstevel@tonic-gate static int
2252*0Sstevel@tonic-gate fd_check_media(dev_t dev, enum dkio_state state)
2253*0Sstevel@tonic-gate {
2254*0Sstevel@tonic-gate 	struct fcu_obj *fjp;
2255*0Sstevel@tonic-gate 	struct fdisk *fdp;
2256*0Sstevel@tonic-gate 	int	unit;
2257*0Sstevel@tonic-gate 	int	err;
2258*0Sstevel@tonic-gate 
2259*0Sstevel@tonic-gate 	unit = fd_getdrive(dev, &fjp, &fdp);
2260*0Sstevel@tonic-gate 
2261*0Sstevel@tonic-gate 	mutex_enter(&fjp->fj_lock);
2262*0Sstevel@tonic-gate 
2263*0Sstevel@tonic-gate 	fjp->fj_ops->fco_select(fjp, unit, 1);
2264*0Sstevel@tonic-gate 	fdp->d_media_state = fd_get_media_state(fjp, unit);
2265*0Sstevel@tonic-gate 	fdp->d_media_timeout = drv_usectohz(fd_check_media_time);
2266*0Sstevel@tonic-gate 
2267*0Sstevel@tonic-gate 	while (fdp->d_media_state == state) {
2268*0Sstevel@tonic-gate 		/* release the controller and drive */
2269*0Sstevel@tonic-gate 		fjp->fj_ops->fco_select(fjp, unit, 0);
2270*0Sstevel@tonic-gate 
2271*0Sstevel@tonic-gate 		/* turn on timer */
2272*0Sstevel@tonic-gate 		fdp->d_media_timeout_id = timeout(fd_media_watch,
2273*0Sstevel@tonic-gate 			(void *)dev, fdp->d_media_timeout);
2274*0Sstevel@tonic-gate 
2275*0Sstevel@tonic-gate 		if (cv_wait_sig(&fdp->d_statecv, &fjp->fj_lock) == 0) {
2276*0Sstevel@tonic-gate 			fdp->d_media_timeout = 0;
2277*0Sstevel@tonic-gate 			mutex_exit(&fjp->fj_lock);
2278*0Sstevel@tonic-gate 			return (EINTR);
2279*0Sstevel@tonic-gate 		}
2280*0Sstevel@tonic-gate 		fjp->fj_ops->fco_select(fjp, unit, 1);
2281*0Sstevel@tonic-gate 		fdp->d_media_state = fd_get_media_state(fjp, unit);
2282*0Sstevel@tonic-gate 	}
2283*0Sstevel@tonic-gate 
2284*0Sstevel@tonic-gate 	if (fdp->d_media_state == DKIO_INSERTED) {
2285*0Sstevel@tonic-gate 		err = fdgetlabel(fjp, unit);
2286*0Sstevel@tonic-gate 		if (err) {
2287*0Sstevel@tonic-gate 			fjp->fj_ops->fco_select(fjp, unit, 0);
2288*0Sstevel@tonic-gate 			mutex_exit(&fjp->fj_lock);
2289*0Sstevel@tonic-gate 			return (EIO);
2290*0Sstevel@tonic-gate 		}
2291*0Sstevel@tonic-gate 	}
2292*0Sstevel@tonic-gate 	fjp->fj_ops->fco_select(fjp, unit, 0);
2293*0Sstevel@tonic-gate 	mutex_exit(&fjp->fj_lock);
2294*0Sstevel@tonic-gate 	return (0);
2295*0Sstevel@tonic-gate }
2296*0Sstevel@tonic-gate 
2297*0Sstevel@tonic-gate /*
2298*0Sstevel@tonic-gate  * fd_get_media_info :
2299*0Sstevel@tonic-gate  * 	Collects medium information for
2300*0Sstevel@tonic-gate  *	DKIOCGMEDIAINFO ioctl.
2301*0Sstevel@tonic-gate  */
2302*0Sstevel@tonic-gate 
2303*0Sstevel@tonic-gate static int
2304*0Sstevel@tonic-gate fd_get_media_info(struct fcu_obj *fjp, caddr_t buf, int flag)
2305*0Sstevel@tonic-gate {
2306*0Sstevel@tonic-gate 	struct dk_minfo media_info;
2307*0Sstevel@tonic-gate 	int err = 0;
2308*0Sstevel@tonic-gate 
2309*0Sstevel@tonic-gate 	media_info.dki_media_type = DK_FLOPPY;
2310*0Sstevel@tonic-gate 	media_info.dki_lbsize = fjp->fj_chars->fdc_sec_size;
2311*0Sstevel@tonic-gate 	media_info.dki_capacity = fjp->fj_chars->fdc_ncyl *
2312*0Sstevel@tonic-gate 		fjp->fj_chars->fdc_secptrack * fjp->fj_chars->fdc_nhead;
2313*0Sstevel@tonic-gate 
2314*0Sstevel@tonic-gate 	if (ddi_copyout(&media_info, buf, sizeof (struct dk_minfo), flag))
2315*0Sstevel@tonic-gate 		err = EFAULT;
2316*0Sstevel@tonic-gate 	return (err);
2317*0Sstevel@tonic-gate }
2318