xref: /onnv-gate/usr/src/uts/common/io/fdc.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 Controller Driver
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  *   for the standard PC architecture using the Intel 8272A fdc.
33*0Sstevel@tonic-gate  *   Note that motor control and drive select use a latch external
34*0Sstevel@tonic-gate  *   to the fdc.
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  *   This driver is EISA capable, and uses DMA buffer chaining if available.
37*0Sstevel@tonic-gate  *   If this driver is attached to the ISA bus nexus (or if the EISA bus driver
38*0Sstevel@tonic-gate  *   does not support DMA buffer chaining), then the bus driver must ensure
39*0Sstevel@tonic-gate  *   that dma mapping (breakup) and dma engine requests are properly degraded.
40*0Sstevel@tonic-gate  */
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate  * hack for bugid 1160621:
44*0Sstevel@tonic-gate  * workaround compiler optimization bug by turning on DEBUG
45*0Sstevel@tonic-gate  */
46*0Sstevel@tonic-gate #ifndef DEBUG
47*0Sstevel@tonic-gate #define	DEBUG	1
48*0Sstevel@tonic-gate #endif
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #include <sys/param.h>
51*0Sstevel@tonic-gate #include <sys/buf.h>
52*0Sstevel@tonic-gate #include <sys/ioctl.h>
53*0Sstevel@tonic-gate #include <sys/uio.h>
54*0Sstevel@tonic-gate #include <sys/open.h>
55*0Sstevel@tonic-gate #include <sys/conf.h>
56*0Sstevel@tonic-gate #include <sys/file.h>
57*0Sstevel@tonic-gate #include <sys/cmn_err.h>
58*0Sstevel@tonic-gate #include <sys/debug.h>
59*0Sstevel@tonic-gate #include <sys/kmem.h>
60*0Sstevel@tonic-gate #include <sys/stat.h>
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate #include <sys/autoconf.h>
63*0Sstevel@tonic-gate #include <sys/dkio.h>
64*0Sstevel@tonic-gate #include <sys/vtoc.h>
65*0Sstevel@tonic-gate #include <sys/kstat.h>
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate #include <sys/fdio.h>
68*0Sstevel@tonic-gate #include <sys/fdc.h>
69*0Sstevel@tonic-gate #include <sys/i8272A.h>
70*0Sstevel@tonic-gate #include <sys/fd_debug.h>
71*0Sstevel@tonic-gate #include <sys/promif.h>
72*0Sstevel@tonic-gate #include <sys/ddi.h>
73*0Sstevel@tonic-gate #include <sys/sunddi.h>
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate /*
76*0Sstevel@tonic-gate  * bss (uninitialized data)
77*0Sstevel@tonic-gate  */
78*0Sstevel@tonic-gate static void *fdc_state_head;		/* opaque handle top of state structs */
79*0Sstevel@tonic-gate static ddi_dma_attr_t fdc_dma_attr;
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate /*
82*0Sstevel@tonic-gate  * Local static data
83*0Sstevel@tonic-gate  */
84*0Sstevel@tonic-gate #define	OURUN_TRIES	12
85*0Sstevel@tonic-gate static uchar_t rwretry = 4;
86*0Sstevel@tonic-gate static uchar_t skretry = 3;
87*0Sstevel@tonic-gate static uchar_t configurecmd[4] = {FO_CNFG, 0, 0x0F, 0};
88*0Sstevel@tonic-gate static uchar_t recalcmd[2] = {FO_RECAL, 0};
89*0Sstevel@tonic-gate static uchar_t senseintcmd = FO_SINT;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate /*
92*0Sstevel@tonic-gate  * error handling
93*0Sstevel@tonic-gate  *
94*0Sstevel@tonic-gate  * for debugging, set rwretry and skretry = 1
95*0Sstevel@tonic-gate  *		set fcerrlevel to 1
96*0Sstevel@tonic-gate  *		set fcerrmask  to 224  or 644
97*0Sstevel@tonic-gate  *
98*0Sstevel@tonic-gate  * after debug, set rwretry to 4, skretry to 3, and fcerrlevel to 5
99*0Sstevel@tonic-gate  * set fcerrmask to FDEM_ALL
100*0Sstevel@tonic-gate  * or remove the define DEBUG
101*0Sstevel@tonic-gate  */
102*0Sstevel@tonic-gate static uint_t fcerrmask = FDEM_ALL;
103*0Sstevel@tonic-gate static int fcerrlevel = 6;
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate #define	KIOIP	KSTAT_INTR_PTR(fcp->c_intrstat)
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate static xlate_tbl_t drate_mfm[] = {
109*0Sstevel@tonic-gate 	{  250, 2},
110*0Sstevel@tonic-gate 	{  300, 1},
111*0Sstevel@tonic-gate 	{  417, 0},
112*0Sstevel@tonic-gate 	{  500, 0},
113*0Sstevel@tonic-gate 	{ 1000, 3},
114*0Sstevel@tonic-gate 	{    0, 0}
115*0Sstevel@tonic-gate };
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate static xlate_tbl_t sector_size[] = {
118*0Sstevel@tonic-gate 	{  256, 1},
119*0Sstevel@tonic-gate 	{  512, 2},
120*0Sstevel@tonic-gate 	{ 1024, 3},
121*0Sstevel@tonic-gate 	{    0, 2}
122*0Sstevel@tonic-gate };
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate static xlate_tbl_t motor_onbits[] = {
125*0Sstevel@tonic-gate 	{  0, 0x10},
126*0Sstevel@tonic-gate 	{  1, 0x20},
127*0Sstevel@tonic-gate 	{  2, 0x40},
128*0Sstevel@tonic-gate 	{  3, 0x80},
129*0Sstevel@tonic-gate 	{  0, 0x80}
130*0Sstevel@tonic-gate };
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate static xlate_tbl_t step_rate[] = {
133*0Sstevel@tonic-gate 	{  10, 0xF0},		/* for 500K data rate */
134*0Sstevel@tonic-gate 	{  20, 0xE0},
135*0Sstevel@tonic-gate 	{  30, 0xD0},
136*0Sstevel@tonic-gate 	{  40, 0xC0},
137*0Sstevel@tonic-gate 	{  50, 0xB0},
138*0Sstevel@tonic-gate 	{  60, 0xA0},
139*0Sstevel@tonic-gate 	{  70, 0x90},
140*0Sstevel@tonic-gate 	{  80, 0x80},
141*0Sstevel@tonic-gate 	{  90, 0x70},
142*0Sstevel@tonic-gate 	{ 100, 0x60},
143*0Sstevel@tonic-gate 	{ 110, 0x50},
144*0Sstevel@tonic-gate 	{ 120, 0x40},
145*0Sstevel@tonic-gate 	{ 130, 0x30},
146*0Sstevel@tonic-gate 	{ 140, 0x20},
147*0Sstevel@tonic-gate 	{ 150, 0x10},
148*0Sstevel@tonic-gate 	{ 160, 0x00},
149*0Sstevel@tonic-gate 	{   0, 0x00}
150*0Sstevel@tonic-gate };
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate #ifdef notdef
153*0Sstevel@tonic-gate static xlate_tbl_t head_unld[] = {
154*0Sstevel@tonic-gate 	{  16, 0x1},		/* for 500K data rate */
155*0Sstevel@tonic-gate 	{  32, 0x2},
156*0Sstevel@tonic-gate 	{  48, 0x3},
157*0Sstevel@tonic-gate 	{  64, 0x4},
158*0Sstevel@tonic-gate 	{  80, 0x5},
159*0Sstevel@tonic-gate 	{  96, 0x6},
160*0Sstevel@tonic-gate 	{ 112, 0x7},
161*0Sstevel@tonic-gate 	{ 128, 0x8},
162*0Sstevel@tonic-gate 	{ 144, 0x9},
163*0Sstevel@tonic-gate 	{ 160, 0xA},
164*0Sstevel@tonic-gate 	{ 176, 0xB},
165*0Sstevel@tonic-gate 	{ 192, 0xC},
166*0Sstevel@tonic-gate 	{ 208, 0xD},
167*0Sstevel@tonic-gate 	{ 224, 0xE},
168*0Sstevel@tonic-gate 	{ 240, 0xF},
169*0Sstevel@tonic-gate 	{ 256, 0x0},
170*0Sstevel@tonic-gate 	{   0, 0x0}
171*0Sstevel@tonic-gate };
172*0Sstevel@tonic-gate #endif
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate static struct fdcmdinfo {
175*0Sstevel@tonic-gate 	char *cmdname;		/* command name */
176*0Sstevel@tonic-gate 	uchar_t ncmdbytes;	/* number of bytes of command */
177*0Sstevel@tonic-gate 	uchar_t nrsltbytes;	/* number of bytes in result */
178*0Sstevel@tonic-gate 	uchar_t cmdtype;		/* characteristics */
179*0Sstevel@tonic-gate } fdcmds[] = {
180*0Sstevel@tonic-gate 	"", 0, 0, 0,			/* - */
181*0Sstevel@tonic-gate 	"", 0, 0, 0,			/* - */
182*0Sstevel@tonic-gate 	"read_track", 9, 7, 1,		/* 2 */
183*0Sstevel@tonic-gate 	"specify", 3, 0, 3,		/* 3 */
184*0Sstevel@tonic-gate 	"sense_drv_status", 2, 1, 3,	/* 4 */
185*0Sstevel@tonic-gate 	"write", 9, 7, 1,		/* 5 */
186*0Sstevel@tonic-gate 	"read", 9, 7, 1,		/* 6 */
187*0Sstevel@tonic-gate 	"recalibrate", 2, 0, 2,		/* 7 */
188*0Sstevel@tonic-gate 	"sense_int_status", 1, 2, 3,	/* 8 */
189*0Sstevel@tonic-gate 	"write_del", 9, 7, 1,		/* 9 */
190*0Sstevel@tonic-gate 	"read_id", 2, 7, 2,		/* A */
191*0Sstevel@tonic-gate 	"", 0, 0, 0,			/* - */
192*0Sstevel@tonic-gate 	"read_del", 9, 7, 1,		/* C */
193*0Sstevel@tonic-gate 	"format_track", 10, 7, 1,	/* D */
194*0Sstevel@tonic-gate 	"dump_reg", 1, 10, 4,		/* E */
195*0Sstevel@tonic-gate 	"seek", 3, 0, 2,		/* F */
196*0Sstevel@tonic-gate 	"version", 1, 1, 3,		/* 10 */
197*0Sstevel@tonic-gate 	"", 0, 0, 0,			/* - */
198*0Sstevel@tonic-gate 	"perp_mode", 2, 0, 3,		/* 12 */
199*0Sstevel@tonic-gate 	"configure", 4, 0, 4,		/* 13 */
200*0Sstevel@tonic-gate 	/* relative seek */
201*0Sstevel@tonic-gate };
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate static int
205*0Sstevel@tonic-gate fdc_bus_ctl(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
206*0Sstevel@tonic-gate static int get_ioaddr(dev_info_t *dip, int *ioaddr);
207*0Sstevel@tonic-gate static int get_unit(dev_info_t *dip, int *cntrl_num);
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate struct bus_ops fdc_bus_ops = {
210*0Sstevel@tonic-gate 	BUSO_REV,
211*0Sstevel@tonic-gate 	nullbusmap,
212*0Sstevel@tonic-gate 	0,	/* ddi_intrspec_t (*bus_get_intrspec)(); */
213*0Sstevel@tonic-gate 	0,	/* int 	(*bus_add_intrspec)(); */
214*0Sstevel@tonic-gate 	0,	/* void (*bus_remove_intrspec)(); */
215*0Sstevel@tonic-gate 	i_ddi_map_fault,
216*0Sstevel@tonic-gate 	ddi_dma_map,
217*0Sstevel@tonic-gate 	ddi_dma_allochdl,
218*0Sstevel@tonic-gate 	ddi_dma_freehdl,
219*0Sstevel@tonic-gate 	ddi_dma_bindhdl,
220*0Sstevel@tonic-gate 	ddi_dma_unbindhdl,
221*0Sstevel@tonic-gate 	ddi_dma_flush,
222*0Sstevel@tonic-gate 	ddi_dma_win,
223*0Sstevel@tonic-gate 	ddi_dma_mctl,
224*0Sstevel@tonic-gate 	fdc_bus_ctl,
225*0Sstevel@tonic-gate 	ddi_bus_prop_op,
226*0Sstevel@tonic-gate };
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate static int fdc_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
229*0Sstevel@tonic-gate static int fdc_probe(dev_info_t *);
230*0Sstevel@tonic-gate static int fdc_attach(dev_info_t *, ddi_attach_cmd_t);
231*0Sstevel@tonic-gate static int fdc_detach(dev_info_t *, ddi_detach_cmd_t);
232*0Sstevel@tonic-gate static int fdc_enhance_probe(struct fdcntlr *fcp);
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate struct dev_ops	fdc_ops = {
235*0Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
236*0Sstevel@tonic-gate 	0,			/* refcnt  */
237*0Sstevel@tonic-gate 	fdc_getinfo,		/* getinfo */
238*0Sstevel@tonic-gate 	nulldev,		/* identify */
239*0Sstevel@tonic-gate 	fdc_probe,		/* probe */
240*0Sstevel@tonic-gate 	fdc_attach,		/* attach */
241*0Sstevel@tonic-gate 	fdc_detach,		/* detach */
242*0Sstevel@tonic-gate 	nodev,			/* reset */
243*0Sstevel@tonic-gate 	(struct cb_ops *)0,	/* driver operations */
244*0Sstevel@tonic-gate 	&fdc_bus_ops		/* bus operations */
245*0Sstevel@tonic-gate };
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate /*
248*0Sstevel@tonic-gate  * This is the loadable module wrapper.
249*0Sstevel@tonic-gate  */
250*0Sstevel@tonic-gate #include <sys/modctl.h>
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate extern struct mod_ops mod_driverops;
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate static struct modldrv modldrv = {
255*0Sstevel@tonic-gate 	&mod_driverops,		/* Type of module. This one is a driver */
256*0Sstevel@tonic-gate 	"Floppy Controller %I%",	/* Name of the module. */
257*0Sstevel@tonic-gate 	&fdc_ops,		/* Driver ops vector */
258*0Sstevel@tonic-gate };
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
261*0Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
262*0Sstevel@tonic-gate };
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate int
265*0Sstevel@tonic-gate _init(void)
266*0Sstevel@tonic-gate {
267*0Sstevel@tonic-gate 	int retval;
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	if ((retval = ddi_soft_state_init(&fdc_state_head,
270*0Sstevel@tonic-gate 	    sizeof (struct fdcntlr) + NFDUN * sizeof (struct fcu_obj), 0)) != 0)
271*0Sstevel@tonic-gate 		return (retval);
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	if ((retval = mod_install(&modlinkage)) != 0)
274*0Sstevel@tonic-gate 		ddi_soft_state_fini(&fdc_state_head);
275*0Sstevel@tonic-gate 	return (retval);
276*0Sstevel@tonic-gate }
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate int
279*0Sstevel@tonic-gate _fini(void)
280*0Sstevel@tonic-gate {
281*0Sstevel@tonic-gate 	int retval;
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 	if ((retval = mod_remove(&modlinkage)) != 0)
284*0Sstevel@tonic-gate 		return (retval);
285*0Sstevel@tonic-gate 	ddi_soft_state_fini(&fdc_state_head);
286*0Sstevel@tonic-gate 	return (retval);
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate int
290*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
291*0Sstevel@tonic-gate {
292*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
293*0Sstevel@tonic-gate }
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate int fdc_start(struct fcu_obj *);
297*0Sstevel@tonic-gate int fdc_abort(struct fcu_obj *);
298*0Sstevel@tonic-gate int fdc_getcap(struct fcu_obj *, char *, int);
299*0Sstevel@tonic-gate int fdc_setcap(struct fcu_obj *, char *, int, int);
300*0Sstevel@tonic-gate int fdc_dkinfo(struct fcu_obj *, struct dk_cinfo *);
301*0Sstevel@tonic-gate int fdc_select(struct fcu_obj *, int, int);
302*0Sstevel@tonic-gate int fdgetchng(struct fcu_obj *, int);
303*0Sstevel@tonic-gate int fdresetchng(struct fcu_obj *, int);
304*0Sstevel@tonic-gate int fdrecalseek(struct fcu_obj *, int, int, int);
305*0Sstevel@tonic-gate int fdrwbuf(struct fcu_obj *, int, int, int, int, int, struct buf *);
306*0Sstevel@tonic-gate int fdrw(struct fcu_obj *, int, int, int, int, int, caddr_t, uint_t);
307*0Sstevel@tonic-gate int fdtrkformat(struct fcu_obj *, int, int, int, int);
308*0Sstevel@tonic-gate int fdrawioctl(struct fcu_obj *, int, caddr_t);
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate static struct fcobjops fdc_iops = {
311*0Sstevel@tonic-gate 		fdc_start,	/* controller start */
312*0Sstevel@tonic-gate 		fdc_abort,	/* controller abort */
313*0Sstevel@tonic-gate 		fdc_getcap,	/* capability retrieval */
314*0Sstevel@tonic-gate 		fdc_setcap,	/* capability establishment */
315*0Sstevel@tonic-gate 		fdc_dkinfo,	/* get disk controller info */
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 		fdc_select,	/* select / deselect unit */
318*0Sstevel@tonic-gate 		fdgetchng,	/* get media change */
319*0Sstevel@tonic-gate 		fdresetchng,	/* reset media change */
320*0Sstevel@tonic-gate 		fdrecalseek,	/* recal / seek */
321*0Sstevel@tonic-gate 		fdrwbuf,	/* read /write request */
322*0Sstevel@tonic-gate 		fdrw,		/* read /write sector */
323*0Sstevel@tonic-gate 		fdtrkformat,	/* format track */
324*0Sstevel@tonic-gate 		fdrawioctl	/* raw ioctl */
325*0Sstevel@tonic-gate };
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate /*
329*0Sstevel@tonic-gate  * Function prototypes
330*0Sstevel@tonic-gate  */
331*0Sstevel@tonic-gate void encode(xlate_tbl_t *tablep, int val, uchar_t *rcode);
332*0Sstevel@tonic-gate int decode(xlate_tbl_t *, int, int *);
333*0Sstevel@tonic-gate static int fdc_propinit1(struct fdcntlr *, int);
334*0Sstevel@tonic-gate static void fdc_propinit2(struct fdcntlr *, int);
335*0Sstevel@tonic-gate void fdcquiesce(struct fdcntlr *);
336*0Sstevel@tonic-gate int fdcsense_chng(struct fdcntlr *, int);
337*0Sstevel@tonic-gate int fdcsense_drv(struct fdcntlr *, int);
338*0Sstevel@tonic-gate int fdcsense_int(struct fdcntlr *, int *, int *);
339*0Sstevel@tonic-gate int fdcspecify(struct fdcntlr *, int, int, int);
340*0Sstevel@tonic-gate int fdcspdchange(struct fdcntlr *, struct fcu_obj *, int);
341*0Sstevel@tonic-gate static int fdc_exec(struct fdcntlr *, int, int);
342*0Sstevel@tonic-gate int fdcheckdisk(struct fdcntlr *, int);
343*0Sstevel@tonic-gate static uint_t fdc_intr(caddr_t arg);
344*0Sstevel@tonic-gate static void fdwatch(void *arg);
345*0Sstevel@tonic-gate static void fdmotort(void *arg);
346*0Sstevel@tonic-gate static int fdrecover(struct fdcntlr *);
347*0Sstevel@tonic-gate static int fdc_motorsm(struct fcu_obj *, int, int);
348*0Sstevel@tonic-gate static int fdc_statemach(struct fdcntlr *);
349*0Sstevel@tonic-gate int fdc_docmd(struct fdcntlr *, uchar_t *, uchar_t);
350*0Sstevel@tonic-gate int fdc_result(struct fdcntlr *, uchar_t *, uchar_t);
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate /* ARGSUSED */
354*0Sstevel@tonic-gate static int
355*0Sstevel@tonic-gate fdc_bus_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop,
356*0Sstevel@tonic-gate     void *arg, void *result)
357*0Sstevel@tonic-gate {
358*0Sstevel@tonic-gate 	struct 	fdcntlr *fcp;
359*0Sstevel@tonic-gate 	struct	fcu_obj *fjp;
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L0, FDEM_ATTA,
362*0Sstevel@tonic-gate 	    (CE_CONT, "fdc_bus_ctl: cmd= %x\n", ctlop));
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 	if ((fcp = ddi_get_driver_private(dip)) == NULL)
365*0Sstevel@tonic-gate 		return (DDI_FAILURE);
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	switch (ctlop) {
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
370*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s%d at %s%d\n",
371*0Sstevel@tonic-gate 		    ddi_get_name(rdip), ddi_get_instance(rdip),
372*0Sstevel@tonic-gate 		    ddi_get_name(dip), ddi_get_instance(dip));
373*0Sstevel@tonic-gate 		FCERRPRINT(FDEP_L3, FDEM_ATTA,
374*0Sstevel@tonic-gate 		    (CE_WARN, "fdc_bus_ctl: report %s%d at %s%d",
375*0Sstevel@tonic-gate 		    ddi_get_name(rdip), ddi_get_instance(rdip),
376*0Sstevel@tonic-gate 		    ddi_get_name(dip), ddi_get_instance(dip)));
377*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
380*0Sstevel@tonic-gate 	{
381*0Sstevel@tonic-gate 		dev_info_t *udip = (dev_info_t *)arg;
382*0Sstevel@tonic-gate 		int cntlr;
383*0Sstevel@tonic-gate 		int len;
384*0Sstevel@tonic-gate 		int unit;
385*0Sstevel@tonic-gate 		char name[MAXNAMELEN];
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 		FCERRPRINT(FDEP_L3, FDEM_ATTA,
388*0Sstevel@tonic-gate 		    (CE_WARN, "fdc_bus_ctl: init child 0x%p", (void*)udip));
389*0Sstevel@tonic-gate 		cntlr = fcp->c_number;
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 		len = sizeof (unit);
392*0Sstevel@tonic-gate 		if (ddi_prop_op(DDI_DEV_T_ANY, udip, PROP_LEN_AND_VAL_BUF,
393*0Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "unit", (caddr_t)&unit, &len)
394*0Sstevel@tonic-gate 		    != DDI_PROP_SUCCESS ||
395*0Sstevel@tonic-gate 		    cntlr != FDCTLR(unit) ||
396*0Sstevel@tonic-gate 		    (fcp->c_unit[FDUNIT(unit)])->fj_dip)
397*0Sstevel@tonic-gate 			return (DDI_NOT_WELL_FORMED);
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 		(void) sprintf(name, "%d,%d", cntlr, FDUNIT(unit));
400*0Sstevel@tonic-gate 		ddi_set_name_addr(udip, name);
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate 		fjp = fcp->c_unit[FDUNIT(unit)];
403*0Sstevel@tonic-gate 		fjp->fj_unit = unit;
404*0Sstevel@tonic-gate 		fjp->fj_dip = udip;
405*0Sstevel@tonic-gate 		fjp->fj_ops = &fdc_iops;
406*0Sstevel@tonic-gate 		fjp->fj_fdc = fcp;
407*0Sstevel@tonic-gate 		fjp->fj_iblock = &fcp->c_iblock;
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 		ddi_set_driver_private(udip, fjp);
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
412*0Sstevel@tonic-gate 	}
413*0Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
414*0Sstevel@tonic-gate 	{
415*0Sstevel@tonic-gate 		dev_info_t *udip = (dev_info_t *)arg;
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate 		FCERRPRINT(FDEP_L3, FDEM_ATTA,
418*0Sstevel@tonic-gate 		    (CE_WARN, "fdc_bus_ctl: uninit child 0x%p", (void *)udip));
419*0Sstevel@tonic-gate 		fjp = ddi_get_driver_private(udip);
420*0Sstevel@tonic-gate 		ddi_set_driver_private(udip, NULL);
421*0Sstevel@tonic-gate 		fjp->fj_dip = NULL;
422*0Sstevel@tonic-gate 		ddi_set_name_addr(udip, NULL);
423*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
424*0Sstevel@tonic-gate 	}
425*0Sstevel@tonic-gate 	default:
426*0Sstevel@tonic-gate 		return (DDI_FAILURE);
427*0Sstevel@tonic-gate 	}
428*0Sstevel@tonic-gate }
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate /* ARGSUSED */
431*0Sstevel@tonic-gate static int
432*0Sstevel@tonic-gate fdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
433*0Sstevel@tonic-gate {
434*0Sstevel@tonic-gate 	struct fdcntlr *fcp;
435*0Sstevel@tonic-gate 	int rval;
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 	switch (cmd) {
438*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
439*0Sstevel@tonic-gate 		if (fcp = ddi_get_soft_state(fdc_state_head, (dev_t)arg)) {
440*0Sstevel@tonic-gate 			*result = fcp->c_dip;
441*0Sstevel@tonic-gate 			rval = DDI_SUCCESS;
442*0Sstevel@tonic-gate 			break;
443*0Sstevel@tonic-gate 		} else {
444*0Sstevel@tonic-gate 			rval = DDI_FAILURE;
445*0Sstevel@tonic-gate 			break;
446*0Sstevel@tonic-gate 		}
447*0Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
448*0Sstevel@tonic-gate 		*result = (void *)(uintptr_t)getminor((dev_t)arg);
449*0Sstevel@tonic-gate 		rval = DDI_SUCCESS;
450*0Sstevel@tonic-gate 		break;
451*0Sstevel@tonic-gate 	default:
452*0Sstevel@tonic-gate 		rval = DDI_FAILURE;
453*0Sstevel@tonic-gate 	}
454*0Sstevel@tonic-gate 	return (rval);
455*0Sstevel@tonic-gate }
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate static int
458*0Sstevel@tonic-gate fdc_probe(dev_info_t *dip)
459*0Sstevel@tonic-gate {
460*0Sstevel@tonic-gate 	int	debug[2];
461*0Sstevel@tonic-gate 	int ioaddr;
462*0Sstevel@tonic-gate 	int	len;
463*0Sstevel@tonic-gate 	uchar_t	stat;
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 	len = sizeof (debug);
466*0Sstevel@tonic-gate 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
467*0Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "debug", (caddr_t)debug, &len) ==
468*0Sstevel@tonic-gate 	    DDI_PROP_SUCCESS) {
469*0Sstevel@tonic-gate 		fcerrlevel = debug[0];
470*0Sstevel@tonic-gate 		fcerrmask = (uint_t)debug[1];
471*0Sstevel@tonic-gate 	}
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L3, FDEM_ATTA, (CE_WARN, "fdc_probe: dip %p",
474*0Sstevel@tonic-gate 		(void*)dip));
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 	if (get_ioaddr(dip, &ioaddr) != DDI_SUCCESS)
477*0Sstevel@tonic-gate 		return (DDI_PROBE_FAILURE);
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 	stat = inb(ioaddr + FCR_MSR);
480*0Sstevel@tonic-gate 	if ((stat & (MS_RQM | MS_DIO | MS_CB)) != MS_RQM &&
481*0Sstevel@tonic-gate 	    (stat & ~MS_DIO) != MS_CB)
482*0Sstevel@tonic-gate 		return (DDI_PROBE_FAILURE);
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate 	return (DDI_PROBE_SUCCESS);
485*0Sstevel@tonic-gate }
486*0Sstevel@tonic-gate 
487*0Sstevel@tonic-gate /* ARGSUSED */
488*0Sstevel@tonic-gate static int
489*0Sstevel@tonic-gate fdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
490*0Sstevel@tonic-gate {
491*0Sstevel@tonic-gate 	struct fdcntlr *fcp;
492*0Sstevel@tonic-gate 	struct fcu_obj *fjp;
493*0Sstevel@tonic-gate 	int cntlr_num, ctlr, unit;
494*0Sstevel@tonic-gate 	int intr_set = 0;
495*0Sstevel@tonic-gate 	int len;
496*0Sstevel@tonic-gate 	char name[MAXNAMELEN];
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L3, FDEM_ATTA, (CE_WARN, "fdc_attach: dip %p",
499*0Sstevel@tonic-gate 		(void*)dip));
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	switch (cmd) {
502*0Sstevel@tonic-gate 	case DDI_ATTACH:
503*0Sstevel@tonic-gate 		if (ddi_getprop
504*0Sstevel@tonic-gate 		    (DDI_DEV_T_ANY, dip, 0, "ignore-hardware-nodes", 0)) {
505*0Sstevel@tonic-gate 			len = sizeof (cntlr_num);
506*0Sstevel@tonic-gate 			if (ddi_prop_op(DDI_DEV_T_ANY, dip,
507*0Sstevel@tonic-gate 			    PROP_LEN_AND_VAL_BUF, DDI_PROP_DONTPASS, "unit",
508*0Sstevel@tonic-gate 			    (caddr_t)&cntlr_num, &len) != DDI_PROP_SUCCESS) {
509*0Sstevel@tonic-gate 				FCERRPRINT(FDEP_L3, FDEM_ATTA, (CE_WARN,
510*0Sstevel@tonic-gate 				    "fdc_attach failed: dip %p", (void*)dip));
511*0Sstevel@tonic-gate 				return (DDI_FAILURE);
512*0Sstevel@tonic-gate 			}
513*0Sstevel@tonic-gate 		} else {
514*0Sstevel@tonic-gate 			if (get_unit(dip, &cntlr_num) != DDI_SUCCESS)
515*0Sstevel@tonic-gate 				return (DDI_FAILURE);
516*0Sstevel@tonic-gate 		}
517*0Sstevel@tonic-gate 
518*0Sstevel@tonic-gate 		ctlr = ddi_get_instance(dip);
519*0Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(fdc_state_head, ctlr) != 0)
520*0Sstevel@tonic-gate 			return (DDI_FAILURE);
521*0Sstevel@tonic-gate 		fcp = ddi_get_soft_state(fdc_state_head, ctlr);
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate 		for (unit = 0, fjp = (struct fcu_obj *)(fcp+1);
524*0Sstevel@tonic-gate 		    unit < NFDUN; unit++) {
525*0Sstevel@tonic-gate 			fcp->c_unit[unit] = fjp++;
526*0Sstevel@tonic-gate 		}
527*0Sstevel@tonic-gate 		fcp->c_dip = dip;
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 		if (fdc_propinit1(fcp, cntlr_num) != DDI_SUCCESS)
530*0Sstevel@tonic-gate 			goto no_attach;
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 		/* get iblock cookie to initialize mutex used in the ISR */
533*0Sstevel@tonic-gate 		if (ddi_get_iblock_cookie(dip, (uint_t)0, &fcp->c_iblock) !=
534*0Sstevel@tonic-gate 		    DDI_SUCCESS) {
535*0Sstevel@tonic-gate 			cmn_err(CE_WARN,
536*0Sstevel@tonic-gate 			    "fdc_attach: cannot get iblock cookie");
537*0Sstevel@tonic-gate 			goto no_attach;
538*0Sstevel@tonic-gate 		}
539*0Sstevel@tonic-gate 		mutex_init(&fcp->c_lock, NULL, MUTEX_DRIVER, fcp->c_iblock);
540*0Sstevel@tonic-gate 		intr_set = 1;
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate 		/* setup interrupt handler */
543*0Sstevel@tonic-gate 		if (ddi_add_intr(dip, (uint_t)0, NULL,
544*0Sstevel@tonic-gate 		    (ddi_idevice_cookie_t *)0, fdc_intr, (caddr_t)fcp) !=
545*0Sstevel@tonic-gate 		    DDI_SUCCESS) {
546*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdc: cannot add intr\n");
547*0Sstevel@tonic-gate 			goto no_attach;
548*0Sstevel@tonic-gate 		}
549*0Sstevel@tonic-gate 		intr_set++;
550*0Sstevel@tonic-gate 
551*0Sstevel@tonic-gate 		/*
552*0Sstevel@tonic-gate 		 * acquire the DMA channel
553*0Sstevel@tonic-gate 		 * this assumes that the chnl is not shared; else allocate
554*0Sstevel@tonic-gate 		 * and free the chnl with each fdc request
555*0Sstevel@tonic-gate 		 */
556*0Sstevel@tonic-gate 		if (ddi_dmae_alloc(dip, fcp->c_dmachan, DDI_DMA_DONTWAIT, NULL)
557*0Sstevel@tonic-gate 		    != DDI_SUCCESS) {
558*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdc: cannot acquire dma%d\n",
559*0Sstevel@tonic-gate 			    fcp->c_dmachan);
560*0Sstevel@tonic-gate 			goto no_attach;
561*0Sstevel@tonic-gate 		}
562*0Sstevel@tonic-gate 		(void) ddi_dmae_getattr(dip, &fdc_dma_attr);
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate 		mutex_init(&fcp->c_dorlock, NULL, MUTEX_DRIVER, fcp->c_iblock);
565*0Sstevel@tonic-gate 		cv_init(&fcp->c_iocv, NULL, CV_DRIVER, fcp->c_iblock);
566*0Sstevel@tonic-gate 		sema_init(&fcp->c_selsem, 1, NULL, SEMA_DRIVER, NULL);
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 		(void) sprintf(name, "fdc%d", ctlr);
569*0Sstevel@tonic-gate 		fcp->c_intrstat = kstat_create("fdc", ctlr, name,
570*0Sstevel@tonic-gate 		    "controller", KSTAT_TYPE_INTR, 1, KSTAT_FLAG_PERSISTENT);
571*0Sstevel@tonic-gate 		if (fcp->c_intrstat) {
572*0Sstevel@tonic-gate 			kstat_install(fcp->c_intrstat);
573*0Sstevel@tonic-gate 		}
574*0Sstevel@tonic-gate 
575*0Sstevel@tonic-gate 		ddi_set_driver_private(dip, fcp);
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate 		/*
578*0Sstevel@tonic-gate 		 * reset the controller
579*0Sstevel@tonic-gate 		 */
580*0Sstevel@tonic-gate 		sema_p(&fcp->c_selsem);
581*0Sstevel@tonic-gate 		mutex_enter(&fcp->c_lock);
582*0Sstevel@tonic-gate 		fcp->c_csb.csb_xstate = FXS_RESET;
583*0Sstevel@tonic-gate 		fcp->c_flags |= FCFLG_WAITING;
584*0Sstevel@tonic-gate 		fdcquiesce(fcp);
585*0Sstevel@tonic-gate 
586*0Sstevel@tonic-gate 		/* first test for mode == Model 30 */
587*0Sstevel@tonic-gate 		fcp->c_mode = (inb(fcp->c_regbase + FCR_SRB) & 0x1c) ?
588*0Sstevel@tonic-gate 		    FDCMODE_AT : FDCMODE_30;
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate 		while (fcp->c_flags & FCFLG_WAITING) {
591*0Sstevel@tonic-gate 			cv_wait(&fcp->c_iocv, &fcp->c_lock);
592*0Sstevel@tonic-gate 		}
593*0Sstevel@tonic-gate 		mutex_exit(&fcp->c_lock);
594*0Sstevel@tonic-gate 		sema_v(&fcp->c_selsem);
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 		fdc_propinit2(fcp, cntlr_num);
597*0Sstevel@tonic-gate 
598*0Sstevel@tonic-gate 		ddi_report_dev(dip);
599*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate 	default:
602*0Sstevel@tonic-gate 		return (DDI_FAILURE);
603*0Sstevel@tonic-gate 	}
604*0Sstevel@tonic-gate 
605*0Sstevel@tonic-gate no_attach:
606*0Sstevel@tonic-gate 	if (intr_set) {
607*0Sstevel@tonic-gate 		if (intr_set > 1)
608*0Sstevel@tonic-gate 			ddi_remove_intr(dip, 0, fcp->c_iblock);
609*0Sstevel@tonic-gate 		mutex_destroy(&fcp->c_lock);
610*0Sstevel@tonic-gate 	}
611*0Sstevel@tonic-gate 	ddi_soft_state_free(fdc_state_head, cntlr_num);
612*0Sstevel@tonic-gate 	return (DDI_FAILURE);
613*0Sstevel@tonic-gate }
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate static int
616*0Sstevel@tonic-gate fdc_propinit1(struct fdcntlr *fcp, int cntlr)
617*0Sstevel@tonic-gate {
618*0Sstevel@tonic-gate 	dev_info_t *dip;
619*0Sstevel@tonic-gate 	int len;
620*0Sstevel@tonic-gate 	int value;
621*0Sstevel@tonic-gate 
622*0Sstevel@tonic-gate 	dip = fcp->c_dip;
623*0Sstevel@tonic-gate 	len = sizeof (value);
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate 	if (get_ioaddr(dip, &value) != DDI_SUCCESS)
626*0Sstevel@tonic-gate 		return (DDI_FAILURE);
627*0Sstevel@tonic-gate 
628*0Sstevel@tonic-gate 	fcp->c_regbase = (ushort_t)value;
629*0Sstevel@tonic-gate 
630*0Sstevel@tonic-gate 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
631*0Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "dma-channels", (caddr_t)&value, &len)
632*0Sstevel@tonic-gate 	    != DDI_PROP_SUCCESS) {
633*0Sstevel@tonic-gate 			cmn_err(CE_WARN,
634*0Sstevel@tonic-gate 			    "fdc_attach: Error, could not find a dma channel");
635*0Sstevel@tonic-gate 			return (DDI_FAILURE);
636*0Sstevel@tonic-gate 	}
637*0Sstevel@tonic-gate 	fcp->c_dmachan = (ushort_t)value;
638*0Sstevel@tonic-gate 	fcp->c_number = cntlr;
639*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
640*0Sstevel@tonic-gate }
641*0Sstevel@tonic-gate 
642*0Sstevel@tonic-gate /* ARGSUSED */
643*0Sstevel@tonic-gate static void
644*0Sstevel@tonic-gate fdc_propinit2(struct fdcntlr *fcp, int cntlr)
645*0Sstevel@tonic-gate {
646*0Sstevel@tonic-gate 	dev_info_t *dip;
647*0Sstevel@tonic-gate 	int ccr;
648*0Sstevel@tonic-gate 	int len;
649*0Sstevel@tonic-gate 	int value;
650*0Sstevel@tonic-gate 
651*0Sstevel@tonic-gate 	dip = fcp->c_dip;
652*0Sstevel@tonic-gate 	len = sizeof (value);
653*0Sstevel@tonic-gate 
654*0Sstevel@tonic-gate 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
655*0Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "chip", (caddr_t)&value, &len)
656*0Sstevel@tonic-gate 	    == DDI_PROP_SUCCESS)
657*0Sstevel@tonic-gate 		fcp->c_chip = value;
658*0Sstevel@tonic-gate 	else {
659*0Sstevel@tonic-gate 		static uchar_t perpindcmd[2] = {FO_PERP, 0};
660*0Sstevel@tonic-gate 		static uchar_t versioncmd = FO_VRSN;
661*0Sstevel@tonic-gate 		uchar_t result;
662*0Sstevel@tonic-gate 
663*0Sstevel@tonic-gate 		fcp->c_chip = i8272A;
664*0Sstevel@tonic-gate 		(void) fdc_docmd(fcp, &versioncmd, 1);
665*0Sstevel@tonic-gate 		/*
666*0Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_docmd.
667*0Sstevel@tonic-gate 		 * fdc_results retrieves the controller/drive status
668*0Sstevel@tonic-gate 		 */
669*0Sstevel@tonic-gate 		if (!fdc_result(fcp, &result, 1) && result == 0x90) {
670*0Sstevel@tonic-gate 			/*
671*0Sstevel@tonic-gate 			 * try a perpendicular_mode cmd to ensure
672*0Sstevel@tonic-gate 			 * that we really have an enhanced controller
673*0Sstevel@tonic-gate 			 */
674*0Sstevel@tonic-gate 			if (fdc_docmd(fcp, perpindcmd, 2) ||
675*0Sstevel@tonic-gate 			    fdc_docmd(fcp, configurecmd, 4))
676*0Sstevel@tonic-gate 				/*
677*0Sstevel@tonic-gate 				 * perpindicular_mode will be rejected by
678*0Sstevel@tonic-gate 				 * older controllers; make sure we don't hang.
679*0Sstevel@tonic-gate 				 */
680*0Sstevel@tonic-gate 				(void) fdc_result(fcp, &result, 1);
681*0Sstevel@tonic-gate 				/*
682*0Sstevel@tonic-gate 				 * Ignored return. If failed, warning was
683*0Sstevel@tonic-gate 				 * issued by fdc_result.
684*0Sstevel@tonic-gate 				 */
685*0Sstevel@tonic-gate 			else
686*0Sstevel@tonic-gate 				/* enhanced type controller */
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate 				if ((fcp->c_chip = fdc_enhance_probe(fcp)) == 0)
689*0Sstevel@tonic-gate 					/* default enhanced cntlr */
690*0Sstevel@tonic-gate 					fcp->c_chip = i82077;
691*0Sstevel@tonic-gate 		}
692*0Sstevel@tonic-gate 		(void) ddi_prop_update_int(DDI_DEV_T_NONE, dip,
693*0Sstevel@tonic-gate 		    "chip", fcp->c_chip);
694*0Sstevel@tonic-gate 		/*
695*0Sstevel@tonic-gate 		 * Ignoring return value because, for passed arguments, only
696*0Sstevel@tonic-gate 		 * DDI_SUCCESS is returned.
697*0Sstevel@tonic-gate 		 */
698*0Sstevel@tonic-gate 	}
699*0Sstevel@tonic-gate 	if (fcp->c_chip >= i82077 && fcp->c_mode == FDCMODE_30 &&
700*0Sstevel@tonic-gate 	    (inb(fcp->c_regbase + FCR_DIR) & 0x70) == 0)
701*0Sstevel@tonic-gate 		for (ccr = 0; ccr <= (FCC_NOPREC | FCC_DRATE); ccr++) {
702*0Sstevel@tonic-gate 			/*
703*0Sstevel@tonic-gate 			 * run through all the combinations of NOPREC and
704*0Sstevel@tonic-gate 			 * datarate selection, and see if they show up in the
705*0Sstevel@tonic-gate 			 * Model 30 DIR
706*0Sstevel@tonic-gate 			 */
707*0Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_CCR, ccr);
708*0Sstevel@tonic-gate 			drv_usecwait(5);
709*0Sstevel@tonic-gate 			if ((inb(fcp->c_regbase + FCR_DIR) &
710*0Sstevel@tonic-gate 			    (FCC_NOPREC | FCC_DRATE)) != ccr) {
711*0Sstevel@tonic-gate 				fcp->c_mode = FDCMODE_AT;
712*0Sstevel@tonic-gate 				break;
713*0Sstevel@tonic-gate 			}
714*0Sstevel@tonic-gate 		}
715*0Sstevel@tonic-gate 	else
716*0Sstevel@tonic-gate 		fcp->c_mode = FDCMODE_AT;
717*0Sstevel@tonic-gate 	outb(fcp->c_regbase + FCR_CCR, 0);
718*0Sstevel@tonic-gate }
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate /* ARGSUSED */
721*0Sstevel@tonic-gate static int
722*0Sstevel@tonic-gate fdc_enhance_probe(struct fdcntlr *fcp)
723*0Sstevel@tonic-gate {
724*0Sstevel@tonic-gate 	static uchar_t nsccmd = FO_NSC;
725*0Sstevel@tonic-gate 	uint_t	ddic;
726*0Sstevel@tonic-gate 	int	retcode = 0;
727*0Sstevel@tonic-gate 	uchar_t	result;
728*0Sstevel@tonic-gate 	uchar_t	save;
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate 	/*
731*0Sstevel@tonic-gate 	 * Try to identify the enhanced floppy controller.
732*0Sstevel@tonic-gate 	 * This is required so that we can program the DENSEL output to
733*0Sstevel@tonic-gate 	 * control 3D mode (1.0 MB, 1.6 MB and 2.0 MB unformatted capacity,
734*0Sstevel@tonic-gate 	 * 720 KB, 1.2 MB, and 1.44 MB formatted capacity) 3.5" dual-speed
735*0Sstevel@tonic-gate 	 * floppy drives.  Refer to bugid 1195155.
736*0Sstevel@tonic-gate 	 */
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate 	(void) fdc_docmd(fcp, &nsccmd, 1);
739*0Sstevel@tonic-gate 	/*
740*0Sstevel@tonic-gate 	 * Ignored return. If failed, warning was issued by fdc_docmd.
741*0Sstevel@tonic-gate 	 * fdc_results retrieves the controller/drive status
742*0Sstevel@tonic-gate 	 */
743*0Sstevel@tonic-gate 	if (!fdc_result(fcp, &result, 1) && result != S0_IVCMD) {
744*0Sstevel@tonic-gate 		/*
745*0Sstevel@tonic-gate 		 * only enhanced National Semi PC8477 core
746*0Sstevel@tonic-gate 		 * should respond to this command
747*0Sstevel@tonic-gate 		 */
748*0Sstevel@tonic-gate 		if ((result & 0xf0) == 0x70) {
749*0Sstevel@tonic-gate 			/* low 4 bits may change */
750*0Sstevel@tonic-gate 			fcp->c_flags |= FCFLG_3DMODE;
751*0Sstevel@tonic-gate 			retcode = PC87322;
752*0Sstevel@tonic-gate 		} else
753*0Sstevel@tonic-gate 			cmn_err(CE_CONT,
754*0Sstevel@tonic-gate "?fdc: unidentified, enhanced, National Semiconductor cntlr %x\n", result);
755*0Sstevel@tonic-gate 	} else {
756*0Sstevel@tonic-gate 		save = inb(fcp->c_regbase + FCR_SRA);
757*0Sstevel@tonic-gate 
758*0Sstevel@tonic-gate 		do {
759*0Sstevel@tonic-gate 			/* probe for motherboard version of SMC cntlr */
760*0Sstevel@tonic-gate 
761*0Sstevel@tonic-gate 			/* try to enable configuration mode */
762*0Sstevel@tonic-gate 			ddic = ddi_enter_critical();
763*0Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, FSA_ENA5);
764*0Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, FSA_ENA5);
765*0Sstevel@tonic-gate 			ddi_exit_critical(ddic);
766*0Sstevel@tonic-gate 
767*0Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, 0x0F);
768*0Sstevel@tonic-gate 			if (inb(fcp->c_regbase + FCR_SRB) != 0x00)
769*0Sstevel@tonic-gate 				/* always expect 0 from config reg F */
770*0Sstevel@tonic-gate 				break;
771*0Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, 0x0D);
772*0Sstevel@tonic-gate 			if (inb(fcp->c_regbase + FCR_SRB) != 0x65)
773*0Sstevel@tonic-gate 				/* expect 0x65 from config reg D */
774*0Sstevel@tonic-gate 				break;
775*0Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, 0x0E);
776*0Sstevel@tonic-gate 			result = inb(fcp->c_regbase + FCR_SRB);
777*0Sstevel@tonic-gate 			if (result != 0x02) {
778*0Sstevel@tonic-gate 				/* expect revision level 2 from config reg E */
779*0Sstevel@tonic-gate 				cmn_err(CE_CONT,
780*0Sstevel@tonic-gate "?fdc: unidentified, enhanced, SMC cntlr revision %x\n", result);
781*0Sstevel@tonic-gate 				/* break;	*/
782*0Sstevel@tonic-gate 			}
783*0Sstevel@tonic-gate 			fcp->c_flags |= FCFLG_3DMODE;
784*0Sstevel@tonic-gate 			retcode = FDC37C665;
785*0Sstevel@tonic-gate 		} while (retcode == 0);
786*0Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_SRA, FSA_DISB);
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 		while (retcode == 0) {
789*0Sstevel@tonic-gate 			/* probe for adapter version of SMC cntlr */
790*0Sstevel@tonic-gate 			ddic = ddi_enter_critical();
791*0Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, FSA_ENA6);
792*0Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, FSA_ENA6);
793*0Sstevel@tonic-gate 			ddi_exit_critical(ddic);
794*0Sstevel@tonic-gate 
795*0Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, 0x0F);
796*0Sstevel@tonic-gate 			if (inb(fcp->c_regbase + FCR_SRB) != 0x00)
797*0Sstevel@tonic-gate 				/* always expect 0 from config reg F */
798*0Sstevel@tonic-gate 				break;
799*0Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, 0x0D);
800*0Sstevel@tonic-gate 			if (inb(fcp->c_regbase + FCR_SRB) != 0x66)
801*0Sstevel@tonic-gate 				/* expect 0x66 from config reg D */
802*0Sstevel@tonic-gate 				break;
803*0Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, 0x0E);
804*0Sstevel@tonic-gate 			result = inb(fcp->c_regbase + FCR_SRB);
805*0Sstevel@tonic-gate 			if (result != 0x02) {
806*0Sstevel@tonic-gate 				/* expect revision level 2 from config reg E */
807*0Sstevel@tonic-gate 				cmn_err(CE_CONT,
808*0Sstevel@tonic-gate "?fdc: unidentified, enhanced, SMC cntlr revision %x\n", result);
809*0Sstevel@tonic-gate 				/* break;	*/
810*0Sstevel@tonic-gate 			}
811*0Sstevel@tonic-gate 			fcp->c_flags |= FCFLG_3DMODE;
812*0Sstevel@tonic-gate 			retcode = FDC37C666;
813*0Sstevel@tonic-gate 		}
814*0Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_SRA, FSA_DISB);
815*0Sstevel@tonic-gate 
816*0Sstevel@tonic-gate 		drv_usecwait(10);
817*0Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_SRA, save);
818*0Sstevel@tonic-gate 	}
819*0Sstevel@tonic-gate 	return (retcode);
820*0Sstevel@tonic-gate }
821*0Sstevel@tonic-gate 
822*0Sstevel@tonic-gate /* ARGSUSED */
823*0Sstevel@tonic-gate static int
824*0Sstevel@tonic-gate fdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
825*0Sstevel@tonic-gate {
826*0Sstevel@tonic-gate 	struct fdcntlr *fcp;
827*0Sstevel@tonic-gate 	int unit;
828*0Sstevel@tonic-gate 	int rval = 0;
829*0Sstevel@tonic-gate 
830*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L3, FDEM_ATTA, (CE_WARN, "fdc_detach: dip %p",
831*0Sstevel@tonic-gate 		(void*)dip));
832*0Sstevel@tonic-gate 
833*0Sstevel@tonic-gate 	fcp = ddi_get_driver_private(dip);
834*0Sstevel@tonic-gate 
835*0Sstevel@tonic-gate 	switch (cmd) {
836*0Sstevel@tonic-gate 	case DDI_DETACH:
837*0Sstevel@tonic-gate 		for (unit = 0; unit < NFDUN; unit++)
838*0Sstevel@tonic-gate 			if ((fcp->c_unit[unit])->fj_dip) {
839*0Sstevel@tonic-gate 				rval = EBUSY;
840*0Sstevel@tonic-gate 				break;
841*0Sstevel@tonic-gate 			}
842*0Sstevel@tonic-gate 		kstat_delete(fcp->c_intrstat);
843*0Sstevel@tonic-gate 		fcp->c_intrstat = NULL;
844*0Sstevel@tonic-gate 		ddi_remove_intr(fcp->c_dip, 0, fcp->c_iblock);
845*0Sstevel@tonic-gate 		if (ddi_dmae_release(fcp->c_dip, fcp->c_dmachan) !=
846*0Sstevel@tonic-gate 		    DDI_SUCCESS)
847*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdc_detach: dma release failed, "
848*0Sstevel@tonic-gate 				"dip %p, dmachan %x\n",
849*0Sstevel@tonic-gate 				(void*)fcp->c_dip, fcp->c_dmachan);
850*0Sstevel@tonic-gate 		ddi_prop_remove_all(fcp->c_dip);
851*0Sstevel@tonic-gate 		ddi_set_driver_private(fcp->c_dip, NULL);
852*0Sstevel@tonic-gate 
853*0Sstevel@tonic-gate 		mutex_destroy(&fcp->c_lock);
854*0Sstevel@tonic-gate 		mutex_destroy(&fcp->c_dorlock);
855*0Sstevel@tonic-gate 		cv_destroy(&fcp->c_iocv);
856*0Sstevel@tonic-gate 		sema_destroy(&fcp->c_selsem);
857*0Sstevel@tonic-gate 		ddi_soft_state_free(fdc_state_head, ddi_get_instance(dip));
858*0Sstevel@tonic-gate 		break;
859*0Sstevel@tonic-gate 	default:
860*0Sstevel@tonic-gate 		rval = EINVAL;
861*0Sstevel@tonic-gate 		break;
862*0Sstevel@tonic-gate 	}
863*0Sstevel@tonic-gate 	return (rval);
864*0Sstevel@tonic-gate }
865*0Sstevel@tonic-gate 
866*0Sstevel@tonic-gate 
867*0Sstevel@tonic-gate /* ARGSUSED */
868*0Sstevel@tonic-gate int
869*0Sstevel@tonic-gate fdc_start(struct fcu_obj *fjp)
870*0Sstevel@tonic-gate {
871*0Sstevel@tonic-gate 	return (ENOSYS);
872*0Sstevel@tonic-gate }
873*0Sstevel@tonic-gate 
874*0Sstevel@tonic-gate int
875*0Sstevel@tonic-gate fdc_abort(struct fcu_obj *fjp)
876*0Sstevel@tonic-gate {
877*0Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
878*0Sstevel@tonic-gate 	int unit = fjp->fj_unit & 3;
879*0Sstevel@tonic-gate 
880*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L3, FDEM_RESE, (CE_WARN, "fdc_abort"));
881*0Sstevel@tonic-gate 	if (fcp->c_curunit == unit) {
882*0Sstevel@tonic-gate 		mutex_enter(&fcp->c_lock);
883*0Sstevel@tonic-gate 		if (fcp->c_flags & FCFLG_WAITING) {
884*0Sstevel@tonic-gate 			/*
885*0Sstevel@tonic-gate 			 * this can cause data corruption !
886*0Sstevel@tonic-gate 			 */
887*0Sstevel@tonic-gate 			fdcquiesce(fcp);
888*0Sstevel@tonic-gate 			fcp->c_csb.csb_xstate = FXS_RESET;
889*0Sstevel@tonic-gate 			fcp->c_flags |= FCFLG_TIMEOUT;
890*0Sstevel@tonic-gate 			if (ddi_dmae_stop(fcp->c_dip, fcp->c_dmachan) !=
891*0Sstevel@tonic-gate 			    DDI_SUCCESS)
892*0Sstevel@tonic-gate 				cmn_err(CE_WARN,
893*0Sstevel@tonic-gate 					"fdc_detach: dma release failed, "
894*0Sstevel@tonic-gate 					"dip %p, dmachan %x\n",
895*0Sstevel@tonic-gate 					(void*)fcp->c_dip, fcp->c_dmachan);
896*0Sstevel@tonic-gate 		}
897*0Sstevel@tonic-gate 		mutex_exit(&fcp->c_lock);
898*0Sstevel@tonic-gate 		drv_usecwait(500);
899*0Sstevel@tonic-gate 		return (DDI_SUCCESS);
900*0Sstevel@tonic-gate 	}
901*0Sstevel@tonic-gate 	return (DDI_FAILURE);
902*0Sstevel@tonic-gate }
903*0Sstevel@tonic-gate 
904*0Sstevel@tonic-gate /* ARGSUSED */
905*0Sstevel@tonic-gate int
906*0Sstevel@tonic-gate fdc_getcap(struct fcu_obj *fjp, char *a, int i)
907*0Sstevel@tonic-gate {
908*0Sstevel@tonic-gate 	return (ENOSYS);
909*0Sstevel@tonic-gate }
910*0Sstevel@tonic-gate 
911*0Sstevel@tonic-gate /* ARGSUSED */
912*0Sstevel@tonic-gate int
913*0Sstevel@tonic-gate fdc_setcap(struct fcu_obj *fjp, char *a, int i, int j)
914*0Sstevel@tonic-gate {
915*0Sstevel@tonic-gate 	return (ENOSYS);
916*0Sstevel@tonic-gate }
917*0Sstevel@tonic-gate 
918*0Sstevel@tonic-gate int
919*0Sstevel@tonic-gate fdc_dkinfo(struct fcu_obj *fjp, struct dk_cinfo *dcp)
920*0Sstevel@tonic-gate {
921*0Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
922*0Sstevel@tonic-gate 
923*0Sstevel@tonic-gate 	(void) strncpy((char *)&dcp->dki_cname, ddi_get_name(fcp->c_dip),
924*0Sstevel@tonic-gate 		DK_DEVLEN);
925*0Sstevel@tonic-gate 	dcp->dki_ctype = DKC_UNKNOWN; /* no code for generic PC/AT fdc */
926*0Sstevel@tonic-gate 	dcp->dki_flags = DKI_FMTTRK;
927*0Sstevel@tonic-gate 	dcp->dki_addr = fcp->c_regbase;
928*0Sstevel@tonic-gate 	dcp->dki_space = 0;
929*0Sstevel@tonic-gate 	dcp->dki_prio = fcp->c_intprio;
930*0Sstevel@tonic-gate 	dcp->dki_vec = fcp->c_intvec;
931*0Sstevel@tonic-gate 	(void) strncpy((char *)&dcp->dki_dname, ddi_driver_name(fjp->fj_dip),
932*0Sstevel@tonic-gate 		DK_DEVLEN);
933*0Sstevel@tonic-gate 	dcp->dki_slave = fjp->fj_unit & 3;
934*0Sstevel@tonic-gate 	dcp->dki_maxtransfer = maxphys / DEV_BSIZE;
935*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
936*0Sstevel@tonic-gate }
937*0Sstevel@tonic-gate 
938*0Sstevel@tonic-gate /*
939*0Sstevel@tonic-gate  * on=> non-zero = select, 0 = de-select
940*0Sstevel@tonic-gate  */
941*0Sstevel@tonic-gate /* ARGSUSED */
942*0Sstevel@tonic-gate int
943*0Sstevel@tonic-gate fdc_select(struct fcu_obj *fjp, int funit, int on)
944*0Sstevel@tonic-gate {
945*0Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
946*0Sstevel@tonic-gate 	int unit = funit & 3;
947*0Sstevel@tonic-gate 
948*0Sstevel@tonic-gate 	if (on) {
949*0Sstevel@tonic-gate 		/* possess controller */
950*0Sstevel@tonic-gate 		sema_p(&fcp->c_selsem);
951*0Sstevel@tonic-gate 		FCERRPRINT(FDEP_L2, FDEM_DSEL,
952*0Sstevel@tonic-gate 		    (CE_NOTE, "fdc_select unit %d: on", funit));
953*0Sstevel@tonic-gate 
954*0Sstevel@tonic-gate 		if (fcp->c_curunit != unit || !(fjp->fj_flags & FUNIT_CHAROK)) {
955*0Sstevel@tonic-gate 			fcp->c_curunit = unit;
956*0Sstevel@tonic-gate 			fjp->fj_flags |= FUNIT_CHAROK;
957*0Sstevel@tonic-gate 			if (fdcspecify(fcp,
958*0Sstevel@tonic-gate 			    fjp->fj_chars->fdc_transfer_rate,
959*0Sstevel@tonic-gate 			    fjp->fj_drive->fdd_steprate, 40))
960*0Sstevel@tonic-gate 				cmn_err(CE_WARN,
961*0Sstevel@tonic-gate 				    "fdc_select: controller setup rejected "
962*0Sstevel@tonic-gate 				    "fdcntrl %p transfer rate %x step rate %x"
963*0Sstevel@tonic-gate 				    " head load time 40\n", (void*)fcp,
964*0Sstevel@tonic-gate 				    fjp->fj_chars->fdc_transfer_rate,
965*0Sstevel@tonic-gate 				    fjp->fj_drive->fdd_steprate);
966*0Sstevel@tonic-gate 		}
967*0Sstevel@tonic-gate 
968*0Sstevel@tonic-gate 		mutex_enter(&fcp->c_dorlock);
969*0Sstevel@tonic-gate 
970*0Sstevel@tonic-gate 		/* make sure drive is not selected in case we change speed */
971*0Sstevel@tonic-gate 		fcp->c_digout = (fcp->c_digout & ~FD_DRSEL) |
972*0Sstevel@tonic-gate 		    (~unit & FD_DRSEL);
973*0Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_DOR, fcp->c_digout);
974*0Sstevel@tonic-gate 
975*0Sstevel@tonic-gate 		(void) fdc_motorsm(fjp, FMI_STARTCMD,
976*0Sstevel@tonic-gate 		    fjp->fj_drive->fdd_motoron);
977*0Sstevel@tonic-gate 		/*
978*0Sstevel@tonic-gate 		 * Return value ignored - fdcmotort deals with failure.
979*0Sstevel@tonic-gate 		 */
980*0Sstevel@tonic-gate 		if (fdcspdchange(fcp, fjp, fjp->fj_attr->fda_rotatespd)) {
981*0Sstevel@tonic-gate 			/* 3D drive requires 500 ms for speed change */
982*0Sstevel@tonic-gate 			(void) fdc_motorsm(fjp, FMI_RSTARTCMD, 5);
983*0Sstevel@tonic-gate 			/*
984*0Sstevel@tonic-gate 			 * Return value ignored - fdcmotort deals with failure.
985*0Sstevel@tonic-gate 			 */
986*0Sstevel@tonic-gate 		}
987*0Sstevel@tonic-gate 
988*0Sstevel@tonic-gate 		fcp->c_digout = (fcp->c_digout & ~FD_DRSEL) | (unit & FD_DRSEL);
989*0Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_DOR, fcp->c_digout);
990*0Sstevel@tonic-gate 
991*0Sstevel@tonic-gate 		mutex_exit(&fcp->c_dorlock);
992*0Sstevel@tonic-gate 		fcp->c_csb.csb_drive = (uchar_t)unit;
993*0Sstevel@tonic-gate 	} else {
994*0Sstevel@tonic-gate 		FCERRPRINT(FDEP_L2, FDEM_DSEL,
995*0Sstevel@tonic-gate 		    (CE_NOTE, "fdc_select unit %d: off", funit));
996*0Sstevel@tonic-gate 
997*0Sstevel@tonic-gate 		mutex_enter(&fcp->c_dorlock);
998*0Sstevel@tonic-gate 
999*0Sstevel@tonic-gate 		fcp->c_digout |= FD_DRSEL;
1000*0Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_DOR, fcp->c_digout);
1001*0Sstevel@tonic-gate 		(void) fdc_motorsm(fjp, FMI_IDLECMD,
1002*0Sstevel@tonic-gate 		    fjp->fj_drive->fdd_motoroff);
1003*0Sstevel@tonic-gate 		/*
1004*0Sstevel@tonic-gate 		 * Return value ignored - fdcmotort deals with failure.
1005*0Sstevel@tonic-gate 		 */
1006*0Sstevel@tonic-gate 
1007*0Sstevel@tonic-gate 		mutex_exit(&fcp->c_dorlock);
1008*0Sstevel@tonic-gate 
1009*0Sstevel@tonic-gate 		/* give up controller */
1010*0Sstevel@tonic-gate 		sema_v(&fcp->c_selsem);
1011*0Sstevel@tonic-gate 	}
1012*0Sstevel@tonic-gate 	return (0);
1013*0Sstevel@tonic-gate }
1014*0Sstevel@tonic-gate 
1015*0Sstevel@tonic-gate 
1016*0Sstevel@tonic-gate int
1017*0Sstevel@tonic-gate fdgetchng(struct fcu_obj *fjp, int funit)
1018*0Sstevel@tonic-gate {
1019*0Sstevel@tonic-gate 	if (fdcsense_drv(fjp->fj_fdc, funit & 3))
1020*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "fdgetchng: write protect check failed\n");
1021*0Sstevel@tonic-gate 	return (fdcsense_chng(fjp->fj_fdc, funit & 3));
1022*0Sstevel@tonic-gate }
1023*0Sstevel@tonic-gate 
1024*0Sstevel@tonic-gate 
1025*0Sstevel@tonic-gate int
1026*0Sstevel@tonic-gate fdresetchng(struct fcu_obj *fjp, int funit)
1027*0Sstevel@tonic-gate {
1028*0Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
1029*0Sstevel@tonic-gate 	int unit = funit & 3;
1030*0Sstevel@tonic-gate 	int newcyl;			/* where to seek for reset of DSKCHG */
1031*0Sstevel@tonic-gate 
1032*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L2, FDEM_CHEK, (CE_NOTE, "fdmediachng unit %d", funit));
1033*0Sstevel@tonic-gate 
1034*0Sstevel@tonic-gate 	if (fcp->c_curpcyl[unit])
1035*0Sstevel@tonic-gate 		newcyl = fcp->c_curpcyl[unit] - 1;
1036*0Sstevel@tonic-gate 	else
1037*0Sstevel@tonic-gate 		newcyl = 1;
1038*0Sstevel@tonic-gate 	return (fdrecalseek(fjp, funit, newcyl, 0));
1039*0Sstevel@tonic-gate }
1040*0Sstevel@tonic-gate 
1041*0Sstevel@tonic-gate 
1042*0Sstevel@tonic-gate /*
1043*0Sstevel@tonic-gate  * fdrecalseek
1044*0Sstevel@tonic-gate  */
1045*0Sstevel@tonic-gate int
1046*0Sstevel@tonic-gate fdrecalseek(struct fcu_obj *fjp, int funit, int arg, int execflg)
1047*0Sstevel@tonic-gate {
1048*0Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
1049*0Sstevel@tonic-gate 	struct fdcsb *csb;
1050*0Sstevel@tonic-gate 	int unit = funit & 3;
1051*0Sstevel@tonic-gate 	int rval;
1052*0Sstevel@tonic-gate 
1053*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L2, FDEM_RECA, (CE_NOTE, "fdrecalseek unit %d to %d",
1054*0Sstevel@tonic-gate 	    funit, arg));
1055*0Sstevel@tonic-gate 
1056*0Sstevel@tonic-gate 	csb = &fcp->c_csb;
1057*0Sstevel@tonic-gate 	csb->csb_cmd[1] = (uchar_t)unit;
1058*0Sstevel@tonic-gate 	if (arg < 0) {			/* is recal... */
1059*0Sstevel@tonic-gate 		*csb->csb_cmd = FO_RECAL;
1060*0Sstevel@tonic-gate 		csb->csb_ncmds = 2;
1061*0Sstevel@tonic-gate 		csb->csb_timer = 28;
1062*0Sstevel@tonic-gate 	} else {
1063*0Sstevel@tonic-gate 		*csb->csb_cmd = FO_SEEK;
1064*0Sstevel@tonic-gate 		csb->csb_cmd[2] = (uchar_t)arg;
1065*0Sstevel@tonic-gate 		csb->csb_ncmds = 3;
1066*0Sstevel@tonic-gate 		csb->csb_timer = 10;
1067*0Sstevel@tonic-gate 	}
1068*0Sstevel@tonic-gate 	csb->csb_nrslts = 2;	/* 2 for SENSE INTERRUPTS */
1069*0Sstevel@tonic-gate 	csb->csb_opflags = CSB_OFINRPT;
1070*0Sstevel@tonic-gate 	csb->csb_maxretry = skretry;
1071*0Sstevel@tonic-gate 	csb->csb_dmahandle = NULL;
1072*0Sstevel@tonic-gate 	csb->csb_handle_bound = 0;
1073*0Sstevel@tonic-gate 	csb->csb_dmacookiecnt = 0;
1074*0Sstevel@tonic-gate 	csb->csb_dmacurrcookie = 0;
1075*0Sstevel@tonic-gate 	csb->csb_dmawincnt = 0;
1076*0Sstevel@tonic-gate 	csb->csb_dmacurrwin = 0;
1077*0Sstevel@tonic-gate 
1078*0Sstevel@tonic-gate 	/* send cmd off to fdc_exec */
1079*0Sstevel@tonic-gate 	if (rval = fdc_exec(fcp, 1, execflg))
1080*0Sstevel@tonic-gate 		goto out;
1081*0Sstevel@tonic-gate 
1082*0Sstevel@tonic-gate 	if (!(*csb->csb_rslt & S0_SEKEND) ||
1083*0Sstevel@tonic-gate 	    (*csb->csb_rslt & S0_ICMASK) ||
1084*0Sstevel@tonic-gate 	    ((*csb->csb_rslt & S0_ECHK) && arg < 0) ||
1085*0Sstevel@tonic-gate 	    csb->csb_cmdstat)
1086*0Sstevel@tonic-gate 		rval = ENODEV;
1087*0Sstevel@tonic-gate 
1088*0Sstevel@tonic-gate 	if (fdcsense_drv(fcp, unit))
1089*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "fdgetchng: write protect check failed\n");
1090*0Sstevel@tonic-gate out:
1091*0Sstevel@tonic-gate 	return (rval);
1092*0Sstevel@tonic-gate }
1093*0Sstevel@tonic-gate 
1094*0Sstevel@tonic-gate 
1095*0Sstevel@tonic-gate int
1096*0Sstevel@tonic-gate fdrwbuf(struct fcu_obj *fjp, int funit, int rw, int cyl, int head,
1097*0Sstevel@tonic-gate     int sector, struct buf *bp)
1098*0Sstevel@tonic-gate {
1099*0Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
1100*0Sstevel@tonic-gate 	struct fdcsb *csb;
1101*0Sstevel@tonic-gate 	ddi_dma_attr_t dmaattr;
1102*0Sstevel@tonic-gate 	uint_t dmar_flags = 0;
1103*0Sstevel@tonic-gate 	int unit = funit & 3;
1104*0Sstevel@tonic-gate 	int rval;
1105*0Sstevel@tonic-gate 
1106*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L2, FDEM_RW, (CE_NOTE, "fdrwbuf unit %d", funit));
1107*0Sstevel@tonic-gate 
1108*0Sstevel@tonic-gate 	csb = &fcp->c_csb;
1109*0Sstevel@tonic-gate 	if (rw) {
1110*0Sstevel@tonic-gate 		dmar_flags = DDI_DMA_READ;
1111*0Sstevel@tonic-gate 		csb->csb_opflags = CSB_OFDMARD | CSB_OFINRPT;
1112*0Sstevel@tonic-gate 		*csb->csb_cmd = FO_MT | FO_MFM | FO_SK | FO_RDDAT;
1113*0Sstevel@tonic-gate 	} else { /* write */
1114*0Sstevel@tonic-gate 		dmar_flags = DDI_DMA_WRITE;
1115*0Sstevel@tonic-gate 		csb->csb_opflags = CSB_OFDMAWT | CSB_OFINRPT;
1116*0Sstevel@tonic-gate 		*csb->csb_cmd = FO_MT | FO_MFM | FO_WRDAT;
1117*0Sstevel@tonic-gate 	}
1118*0Sstevel@tonic-gate 	csb->csb_cmd[1] = (uchar_t)(unit | ((head & 0x1) << 2));
1119*0Sstevel@tonic-gate 	csb->csb_cmd[2] = (uchar_t)cyl;
1120*0Sstevel@tonic-gate 	csb->csb_cmd[3] = (uchar_t)head;
1121*0Sstevel@tonic-gate 	csb->csb_cmd[4] = (uchar_t)sector;
1122*0Sstevel@tonic-gate 	encode(sector_size, fjp->fj_chars->fdc_sec_size,
1123*0Sstevel@tonic-gate 	    &csb->csb_cmd[5]);
1124*0Sstevel@tonic-gate 	csb->csb_cmd[6] = fjp->fj_chars->fdc_secptrack;
1125*0Sstevel@tonic-gate 	csb->csb_cmd[7] = fjp->fj_attr->fda_gapl;
1126*0Sstevel@tonic-gate 	csb->csb_cmd[8] = 0xFF;
1127*0Sstevel@tonic-gate 
1128*0Sstevel@tonic-gate 	csb->csb_ncmds = 9;
1129*0Sstevel@tonic-gate 	csb->csb_nrslts = 7;
1130*0Sstevel@tonic-gate 	csb->csb_timer = 36;
1131*0Sstevel@tonic-gate 	if (rw == FDRDONE)
1132*0Sstevel@tonic-gate 		csb->csb_maxretry = 1;
1133*0Sstevel@tonic-gate 	else
1134*0Sstevel@tonic-gate 		csb->csb_maxretry = rwretry;
1135*0Sstevel@tonic-gate 
1136*0Sstevel@tonic-gate 	csb->csb_dmahandle = NULL;
1137*0Sstevel@tonic-gate 	csb->csb_handle_bound = 0;
1138*0Sstevel@tonic-gate 	csb->csb_dmacookiecnt = 0;
1139*0Sstevel@tonic-gate 	csb->csb_dmacurrcookie = 0;
1140*0Sstevel@tonic-gate 	csb->csb_dmawincnt = 0;
1141*0Sstevel@tonic-gate 	csb->csb_dmacurrwin = 0;
1142*0Sstevel@tonic-gate 	dmar_flags |= (DDI_DMA_STREAMING | DDI_DMA_PARTIAL);
1143*0Sstevel@tonic-gate 
1144*0Sstevel@tonic-gate 	dmaattr = fdc_dma_attr;
1145*0Sstevel@tonic-gate 	dmaattr.dma_attr_granular = fjp->fj_chars->fdc_sec_size;
1146*0Sstevel@tonic-gate 	if (ddi_dma_alloc_handle(fcp->c_dip, &dmaattr, DDI_DMA_SLEEP,
1147*0Sstevel@tonic-gate 			0, &csb->csb_dmahandle) != DDI_SUCCESS) {
1148*0Sstevel@tonic-gate 		rval = EINVAL;
1149*0Sstevel@tonic-gate 		goto out;
1150*0Sstevel@tonic-gate 	}
1151*0Sstevel@tonic-gate 
1152*0Sstevel@tonic-gate 	rval = ddi_dma_buf_bind_handle(csb->csb_dmahandle, bp, dmar_flags,
1153*0Sstevel@tonic-gate 	    DDI_DMA_SLEEP, 0, &csb->csb_dmacookie, &csb->csb_dmacookiecnt);
1154*0Sstevel@tonic-gate 
1155*0Sstevel@tonic-gate 	if (rval == DDI_DMA_MAPPED) {
1156*0Sstevel@tonic-gate 		csb->csb_dmawincnt = 1;
1157*0Sstevel@tonic-gate 		csb->csb_handle_bound = 1;
1158*0Sstevel@tonic-gate 	} else if (rval == DDI_DMA_PARTIAL_MAP) {
1159*0Sstevel@tonic-gate 		csb->csb_handle_bound = 1;
1160*0Sstevel@tonic-gate 		if (ddi_dma_numwin(csb->csb_dmahandle, &csb->csb_dmawincnt) !=
1161*0Sstevel@tonic-gate 					DDI_SUCCESS) {
1162*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdrwbuf: dma numwin failed\n");
1163*0Sstevel@tonic-gate 			rval = EINVAL;
1164*0Sstevel@tonic-gate 			goto out;
1165*0Sstevel@tonic-gate 		}
1166*0Sstevel@tonic-gate 	} else {
1167*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
1168*0Sstevel@tonic-gate 			"fdrwbuf: dma buf bind handle failed, rval = %d\n",
1169*0Sstevel@tonic-gate 			rval);
1170*0Sstevel@tonic-gate 		rval = EINVAL;
1171*0Sstevel@tonic-gate 		goto out;
1172*0Sstevel@tonic-gate 	}
1173*0Sstevel@tonic-gate 	rval = fdc_exec(fcp, 1, 1);
1174*0Sstevel@tonic-gate 
1175*0Sstevel@tonic-gate out:
1176*0Sstevel@tonic-gate 	if (csb->csb_dmahandle) {
1177*0Sstevel@tonic-gate 		if (csb->csb_handle_bound) {
1178*0Sstevel@tonic-gate 			if (ddi_dma_unbind_handle(csb->csb_dmahandle) !=
1179*0Sstevel@tonic-gate 			    DDI_SUCCESS)
1180*0Sstevel@tonic-gate 				cmn_err(CE_WARN, "fdrwbuf: "
1181*0Sstevel@tonic-gate 				    "dma unbind handle failed\n");
1182*0Sstevel@tonic-gate 			csb->csb_handle_bound = 0;
1183*0Sstevel@tonic-gate 		}
1184*0Sstevel@tonic-gate 		ddi_dma_free_handle(&csb->csb_dmahandle);
1185*0Sstevel@tonic-gate 		csb->csb_dmahandle = NULL;
1186*0Sstevel@tonic-gate 	}
1187*0Sstevel@tonic-gate 	return (rval);
1188*0Sstevel@tonic-gate }
1189*0Sstevel@tonic-gate 
1190*0Sstevel@tonic-gate 
1191*0Sstevel@tonic-gate /*
1192*0Sstevel@tonic-gate  * fdrw- used only for read/writing sectors into/from kernel buffers.
1193*0Sstevel@tonic-gate  */
1194*0Sstevel@tonic-gate int
1195*0Sstevel@tonic-gate fdrw(struct fcu_obj *fjp, int funit, int rw, int cyl, int head,
1196*0Sstevel@tonic-gate     int sector, caddr_t bufp, uint_t len)
1197*0Sstevel@tonic-gate {
1198*0Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
1199*0Sstevel@tonic-gate 	struct fdcsb *csb;
1200*0Sstevel@tonic-gate 	ddi_dma_attr_t dmaattr;
1201*0Sstevel@tonic-gate 	uint_t dmar_flags = 0;
1202*0Sstevel@tonic-gate 	int unit = funit & 3;
1203*0Sstevel@tonic-gate 	int rval;
1204*0Sstevel@tonic-gate 
1205*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L1, FDEM_RW, (CE_CONT, "fdrw unit %d\n", funit));
1206*0Sstevel@tonic-gate 
1207*0Sstevel@tonic-gate 	csb = &fcp->c_csb;
1208*0Sstevel@tonic-gate 	if (rw) {
1209*0Sstevel@tonic-gate 		dmar_flags = DDI_DMA_READ;
1210*0Sstevel@tonic-gate 		csb->csb_opflags = CSB_OFDMARD | CSB_OFINRPT;
1211*0Sstevel@tonic-gate 		*csb->csb_cmd = FO_MT | FO_MFM | FO_SK | FO_RDDAT;
1212*0Sstevel@tonic-gate 	} else { /* write */
1213*0Sstevel@tonic-gate 		dmar_flags = DDI_DMA_WRITE;
1214*0Sstevel@tonic-gate 		csb->csb_opflags = CSB_OFDMAWT | CSB_OFINRPT;
1215*0Sstevel@tonic-gate 		*csb->csb_cmd = FO_MT | FO_MFM | FO_WRDAT;
1216*0Sstevel@tonic-gate 	}
1217*0Sstevel@tonic-gate 	csb->csb_cmd[1] = (uchar_t)(unit | ((head & 0x1) << 2));
1218*0Sstevel@tonic-gate 	csb->csb_cmd[2] = (uchar_t)cyl;
1219*0Sstevel@tonic-gate 	csb->csb_cmd[3] = (uchar_t)head;
1220*0Sstevel@tonic-gate 	csb->csb_cmd[4] = (uchar_t)sector;
1221*0Sstevel@tonic-gate 	encode(sector_size, fjp->fj_chars->fdc_sec_size,
1222*0Sstevel@tonic-gate 	    &csb->csb_cmd[5]);
1223*0Sstevel@tonic-gate 	csb->csb_cmd[6] = (uchar_t)max(fjp->fj_chars->fdc_secptrack, sector);
1224*0Sstevel@tonic-gate 	csb->csb_cmd[7] = fjp->fj_attr->fda_gapl;
1225*0Sstevel@tonic-gate 	csb->csb_cmd[8] = 0xFF;
1226*0Sstevel@tonic-gate 
1227*0Sstevel@tonic-gate 	csb->csb_ncmds = 9;
1228*0Sstevel@tonic-gate 	csb->csb_nrslts = 7;
1229*0Sstevel@tonic-gate 	csb->csb_timer = 36;
1230*0Sstevel@tonic-gate 	if (rw == FDRDONE)
1231*0Sstevel@tonic-gate 		csb->csb_maxretry = 1;
1232*0Sstevel@tonic-gate 	else
1233*0Sstevel@tonic-gate 		csb->csb_maxretry = rwretry;
1234*0Sstevel@tonic-gate 
1235*0Sstevel@tonic-gate 	csb->csb_dmahandle = NULL;
1236*0Sstevel@tonic-gate 	csb->csb_handle_bound = 0;
1237*0Sstevel@tonic-gate 	csb->csb_dmacookiecnt = 0;
1238*0Sstevel@tonic-gate 	csb->csb_dmacurrcookie = 0;
1239*0Sstevel@tonic-gate 	csb->csb_dmawincnt = 0;
1240*0Sstevel@tonic-gate 	csb->csb_dmacurrwin = 0;
1241*0Sstevel@tonic-gate 	dmar_flags |= (DDI_DMA_STREAMING | DDI_DMA_PARTIAL);
1242*0Sstevel@tonic-gate 
1243*0Sstevel@tonic-gate 	dmaattr = fdc_dma_attr;
1244*0Sstevel@tonic-gate 	dmaattr.dma_attr_granular = fjp->fj_chars->fdc_sec_size;
1245*0Sstevel@tonic-gate 	if (ddi_dma_alloc_handle(fcp->c_dip, &dmaattr, DDI_DMA_SLEEP,
1246*0Sstevel@tonic-gate 			0, &csb->csb_dmahandle) != DDI_SUCCESS) {
1247*0Sstevel@tonic-gate 		rval = EINVAL;
1248*0Sstevel@tonic-gate 		goto out;
1249*0Sstevel@tonic-gate 	}
1250*0Sstevel@tonic-gate 
1251*0Sstevel@tonic-gate 	rval = ddi_dma_addr_bind_handle(csb->csb_dmahandle, NULL, bufp, len,
1252*0Sstevel@tonic-gate 			dmar_flags, DDI_DMA_SLEEP, 0,
1253*0Sstevel@tonic-gate 			&csb->csb_dmacookie, &csb->csb_dmacookiecnt);
1254*0Sstevel@tonic-gate 
1255*0Sstevel@tonic-gate 	if (rval == DDI_DMA_MAPPED) {
1256*0Sstevel@tonic-gate 		csb->csb_dmawincnt = 1;
1257*0Sstevel@tonic-gate 		csb->csb_handle_bound = 1;
1258*0Sstevel@tonic-gate 	} else if (rval == DDI_DMA_PARTIAL_MAP) {
1259*0Sstevel@tonic-gate 		csb->csb_handle_bound = 1;
1260*0Sstevel@tonic-gate 		if (ddi_dma_numwin(csb->csb_dmahandle, &csb->csb_dmawincnt) !=
1261*0Sstevel@tonic-gate 					DDI_SUCCESS) {
1262*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdrw: dma numwin failed\n");
1263*0Sstevel@tonic-gate 			rval = EINVAL;
1264*0Sstevel@tonic-gate 			goto out;
1265*0Sstevel@tonic-gate 		}
1266*0Sstevel@tonic-gate 	} else {
1267*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
1268*0Sstevel@tonic-gate 			"fdrw: dma addr bind handle failed, rval = %d\n",
1269*0Sstevel@tonic-gate 			rval);
1270*0Sstevel@tonic-gate 		rval = EINVAL;
1271*0Sstevel@tonic-gate 		goto out;
1272*0Sstevel@tonic-gate 	}
1273*0Sstevel@tonic-gate 	rval = fdc_exec(fcp, 1, 1);
1274*0Sstevel@tonic-gate 
1275*0Sstevel@tonic-gate out:
1276*0Sstevel@tonic-gate 	if (csb->csb_dmahandle) {
1277*0Sstevel@tonic-gate 		if (csb->csb_handle_bound) {
1278*0Sstevel@tonic-gate 			if (ddi_dma_unbind_handle(csb->csb_dmahandle) !=
1279*0Sstevel@tonic-gate 			    DDI_SUCCESS)
1280*0Sstevel@tonic-gate 				cmn_err(CE_WARN, "fdrw: "
1281*0Sstevel@tonic-gate 				    "dma unbind handle failed\n");
1282*0Sstevel@tonic-gate 			csb->csb_handle_bound = 0;
1283*0Sstevel@tonic-gate 		}
1284*0Sstevel@tonic-gate 		ddi_dma_free_handle(&csb->csb_dmahandle);
1285*0Sstevel@tonic-gate 		csb->csb_dmahandle = NULL;
1286*0Sstevel@tonic-gate 	}
1287*0Sstevel@tonic-gate 	return (rval);
1288*0Sstevel@tonic-gate }
1289*0Sstevel@tonic-gate 
1290*0Sstevel@tonic-gate 
1291*0Sstevel@tonic-gate int
1292*0Sstevel@tonic-gate fdtrkformat(struct fcu_obj *fjp, int funit, int cyl, int head, int filldata)
1293*0Sstevel@tonic-gate {
1294*0Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
1295*0Sstevel@tonic-gate 	struct fdcsb *csb;
1296*0Sstevel@tonic-gate 	ddi_dma_attr_t dmaattr;
1297*0Sstevel@tonic-gate 	int unit = funit & 3;
1298*0Sstevel@tonic-gate 	int fmdatlen, lsector, lstart;
1299*0Sstevel@tonic-gate 	int interleave, numsctr, offset, psector;
1300*0Sstevel@tonic-gate 	uchar_t *dp;
1301*0Sstevel@tonic-gate 	uchar_t *fmdatp;
1302*0Sstevel@tonic-gate 	int rval;
1303*0Sstevel@tonic-gate 
1304*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L2, FDEM_FORM,
1305*0Sstevel@tonic-gate 	    (CE_NOTE, "fdformattrk unit %d cyl=%d, hd=%d", funit, cyl, head));
1306*0Sstevel@tonic-gate 
1307*0Sstevel@tonic-gate 	csb = &fcp->c_csb;
1308*0Sstevel@tonic-gate 
1309*0Sstevel@tonic-gate 	csb->csb_opflags = CSB_OFDMAWT | CSB_OFINRPT;
1310*0Sstevel@tonic-gate 
1311*0Sstevel@tonic-gate 	*csb->csb_cmd = FO_FRMT | FO_MFM;
1312*0Sstevel@tonic-gate 	csb->csb_cmd[1] = (head << 2) | unit;
1313*0Sstevel@tonic-gate 	encode(sector_size, fjp->fj_chars->fdc_sec_size,
1314*0Sstevel@tonic-gate 	    &csb->csb_cmd[2]);
1315*0Sstevel@tonic-gate 	csb->csb_cmd[3] = numsctr = fjp->fj_chars->fdc_secptrack;
1316*0Sstevel@tonic-gate 	csb->csb_cmd[4] = fjp->fj_attr->fda_gapf;
1317*0Sstevel@tonic-gate 	csb->csb_cmd[5] = (uchar_t)filldata;
1318*0Sstevel@tonic-gate 
1319*0Sstevel@tonic-gate 	csb->csb_npcyl = (uchar_t)(cyl * fjp->fj_chars->fdc_steps);
1320*0Sstevel@tonic-gate 
1321*0Sstevel@tonic-gate 	csb->csb_dmahandle = NULL;
1322*0Sstevel@tonic-gate 	csb->csb_handle_bound = 0;
1323*0Sstevel@tonic-gate 	csb->csb_dmacookiecnt = 0;
1324*0Sstevel@tonic-gate 	csb->csb_dmacurrcookie = 0;
1325*0Sstevel@tonic-gate 	csb->csb_dmawincnt = 0;
1326*0Sstevel@tonic-gate 	csb->csb_dmacurrwin = 0;
1327*0Sstevel@tonic-gate 	csb->csb_ncmds = 6;
1328*0Sstevel@tonic-gate 	csb->csb_nrslts = 7;
1329*0Sstevel@tonic-gate 	csb->csb_timer = 32;
1330*0Sstevel@tonic-gate 	csb->csb_maxretry = rwretry;
1331*0Sstevel@tonic-gate 
1332*0Sstevel@tonic-gate 	/*
1333*0Sstevel@tonic-gate 	 * just kmem_alloc space for format track cmd
1334*0Sstevel@tonic-gate 	 */
1335*0Sstevel@tonic-gate 	/*
1336*0Sstevel@tonic-gate 	 * NOTE: have to add size of fifo also - for dummy format action
1337*0Sstevel@tonic-gate 	 */
1338*0Sstevel@tonic-gate 	fmdatlen = 4 * numsctr;
1339*0Sstevel@tonic-gate 	dp = fmdatp = kmem_alloc(fmdatlen, KM_SLEEP);
1340*0Sstevel@tonic-gate 
1341*0Sstevel@tonic-gate 	interleave = fjp->fj_attr->fda_intrlv;
1342*0Sstevel@tonic-gate 	offset = (numsctr + interleave - 1) / interleave;
1343*0Sstevel@tonic-gate 	for (psector = lstart = 1;
1344*0Sstevel@tonic-gate 	    psector <= numsctr; psector += interleave, lstart++) {
1345*0Sstevel@tonic-gate 		for (lsector = lstart; lsector <= numsctr; lsector += offset) {
1346*0Sstevel@tonic-gate 			*dp++ = (uchar_t)cyl;
1347*0Sstevel@tonic-gate 			*dp++ = (uchar_t)head;
1348*0Sstevel@tonic-gate 			*dp++ = (uchar_t)lsector;
1349*0Sstevel@tonic-gate 			*dp++ = csb->csb_cmd[2];
1350*0Sstevel@tonic-gate 		}
1351*0Sstevel@tonic-gate 	}
1352*0Sstevel@tonic-gate 
1353*0Sstevel@tonic-gate 	dmaattr = fdc_dma_attr;
1354*0Sstevel@tonic-gate 	for (; dmaattr.dma_attr_granular < fmdatlen;
1355*0Sstevel@tonic-gate 	    dmaattr.dma_attr_granular <<= 1);
1356*0Sstevel@tonic-gate 
1357*0Sstevel@tonic-gate 	if (ddi_dma_alloc_handle(fcp->c_dip, &dmaattr, DDI_DMA_SLEEP,
1358*0Sstevel@tonic-gate 			0, &csb->csb_dmahandle) != DDI_SUCCESS) {
1359*0Sstevel@tonic-gate 		rval = EINVAL;
1360*0Sstevel@tonic-gate 		goto out;
1361*0Sstevel@tonic-gate 	}
1362*0Sstevel@tonic-gate 
1363*0Sstevel@tonic-gate 	rval = ddi_dma_addr_bind_handle(csb->csb_dmahandle, NULL,
1364*0Sstevel@tonic-gate 			(caddr_t)fmdatp, fmdatlen,
1365*0Sstevel@tonic-gate 			DDI_DMA_WRITE | DDI_DMA_STREAMING | DDI_DMA_PARTIAL,
1366*0Sstevel@tonic-gate 			DDI_DMA_SLEEP, 0,
1367*0Sstevel@tonic-gate 			&csb->csb_dmacookie, &csb->csb_dmacookiecnt);
1368*0Sstevel@tonic-gate 
1369*0Sstevel@tonic-gate 	if (rval == DDI_DMA_MAPPED) {
1370*0Sstevel@tonic-gate 		csb->csb_dmawincnt = 1;
1371*0Sstevel@tonic-gate 		csb->csb_handle_bound = 1;
1372*0Sstevel@tonic-gate 	} else if (rval == DDI_DMA_PARTIAL_MAP) {
1373*0Sstevel@tonic-gate 		csb->csb_handle_bound = 1;
1374*0Sstevel@tonic-gate 		if (ddi_dma_numwin(csb->csb_dmahandle, &csb->csb_dmawincnt) !=
1375*0Sstevel@tonic-gate 					DDI_SUCCESS) {
1376*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdtrkformat: dma numwin failed\n");
1377*0Sstevel@tonic-gate 			rval = EINVAL;
1378*0Sstevel@tonic-gate 			goto out;
1379*0Sstevel@tonic-gate 		}
1380*0Sstevel@tonic-gate 	} else {
1381*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
1382*0Sstevel@tonic-gate 			"fdtrkformat: dma buf bind handle failed, rval = %d\n",
1383*0Sstevel@tonic-gate 			rval);
1384*0Sstevel@tonic-gate 		rval = EINVAL;
1385*0Sstevel@tonic-gate 		goto out;
1386*0Sstevel@tonic-gate 	}
1387*0Sstevel@tonic-gate 
1388*0Sstevel@tonic-gate 	rval = fdc_exec(fcp, 1, 1);
1389*0Sstevel@tonic-gate out:
1390*0Sstevel@tonic-gate 	if (csb->csb_dmahandle) {
1391*0Sstevel@tonic-gate 		if (csb->csb_handle_bound) {
1392*0Sstevel@tonic-gate 			if (ddi_dma_unbind_handle(csb->csb_dmahandle) !=
1393*0Sstevel@tonic-gate 			    DDI_SUCCESS)
1394*0Sstevel@tonic-gate 				cmn_err(CE_WARN, "fdtrkformat: "
1395*0Sstevel@tonic-gate 				    "dma unbind handle failed\n");
1396*0Sstevel@tonic-gate 			csb->csb_handle_bound = 0;
1397*0Sstevel@tonic-gate 		}
1398*0Sstevel@tonic-gate 		ddi_dma_free_handle(&csb->csb_dmahandle);
1399*0Sstevel@tonic-gate 		csb->csb_dmahandle = NULL;
1400*0Sstevel@tonic-gate 	}
1401*0Sstevel@tonic-gate 	kmem_free(fmdatp, fmdatlen);
1402*0Sstevel@tonic-gate 	return (rval);
1403*0Sstevel@tonic-gate }
1404*0Sstevel@tonic-gate 
1405*0Sstevel@tonic-gate /* ARGSUSED */
1406*0Sstevel@tonic-gate int
1407*0Sstevel@tonic-gate fdrawioctl(struct fcu_obj *fjp, int funit, caddr_t arg)
1408*0Sstevel@tonic-gate {
1409*0Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
1410*0Sstevel@tonic-gate 	struct fd_raw *fdrp = (struct fd_raw *)arg;
1411*0Sstevel@tonic-gate 	struct fdcsb *csb;
1412*0Sstevel@tonic-gate 	ddi_dma_attr_t dmaattr;
1413*0Sstevel@tonic-gate 	uint_t dmar_flags = 0;
1414*0Sstevel@tonic-gate 	int i;
1415*0Sstevel@tonic-gate 	int change = 1;
1416*0Sstevel@tonic-gate 	int sleep = 1;
1417*0Sstevel@tonic-gate 	int rval = 0;
1418*0Sstevel@tonic-gate 	int rval_exec = 0;
1419*0Sstevel@tonic-gate 
1420*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L2, FDEM_RAWI,
1421*0Sstevel@tonic-gate 	    (CE_NOTE, "fdrawioctl: cmd[0]=0x%x", fdrp->fdr_cmd[0]));
1422*0Sstevel@tonic-gate 
1423*0Sstevel@tonic-gate 	csb = &fcp->c_csb;
1424*0Sstevel@tonic-gate 
1425*0Sstevel@tonic-gate 	/* copy cmd bytes into csb */
1426*0Sstevel@tonic-gate 	for (i = 0; i <= fdrp->fdr_cnum; i++)
1427*0Sstevel@tonic-gate 		csb->csb_cmd[i] = fdrp->fdr_cmd[i];
1428*0Sstevel@tonic-gate 	csb->csb_ncmds = (uchar_t)fdrp->fdr_cnum;
1429*0Sstevel@tonic-gate 
1430*0Sstevel@tonic-gate 	csb->csb_maxretry = 0;	/* let the application deal with errors */
1431*0Sstevel@tonic-gate 	csb->csb_opflags = CSB_OFRAWIOCTL;
1432*0Sstevel@tonic-gate 	csb->csb_nrslts = 0;
1433*0Sstevel@tonic-gate 	csb->csb_timer = 50;
1434*0Sstevel@tonic-gate 
1435*0Sstevel@tonic-gate 	switch (fdrp->fdr_cmd[0] & 0x0f) {
1436*0Sstevel@tonic-gate 
1437*0Sstevel@tonic-gate 	case FO_SEEK:
1438*0Sstevel@tonic-gate 		change = 0;
1439*0Sstevel@tonic-gate 		/* FALLTHROUGH */
1440*0Sstevel@tonic-gate 	case FO_RECAL:
1441*0Sstevel@tonic-gate 		csb->csb_opflags |= CSB_OFINRPT;
1442*0Sstevel@tonic-gate 		break;
1443*0Sstevel@tonic-gate 
1444*0Sstevel@tonic-gate 	case FO_FRMT:
1445*0Sstevel@tonic-gate 		csb->csb_npcyl = *(uchar_t *)(fdrp->fdr_addr) *
1446*0Sstevel@tonic-gate 		    fjp->fj_chars->fdc_steps;
1447*0Sstevel@tonic-gate 		/* FALLTHROUGH */
1448*0Sstevel@tonic-gate 	case FO_WRDAT:
1449*0Sstevel@tonic-gate 	case FO_WRDEL:
1450*0Sstevel@tonic-gate 		csb->csb_opflags |= CSB_OFDMAWT | CSB_OFRESLT | CSB_OFINRPT;
1451*0Sstevel@tonic-gate 		csb->csb_nrslts = 7;
1452*0Sstevel@tonic-gate 		if (fdrp->fdr_nbytes == 0)
1453*0Sstevel@tonic-gate 			return (EINVAL);
1454*0Sstevel@tonic-gate 		dmar_flags = DDI_DMA_WRITE;
1455*0Sstevel@tonic-gate 		break;
1456*0Sstevel@tonic-gate 
1457*0Sstevel@tonic-gate 	case FO_RDDAT:
1458*0Sstevel@tonic-gate 	case FO_RDDEL:
1459*0Sstevel@tonic-gate 	case FO_RDTRK:
1460*0Sstevel@tonic-gate 		csb->csb_opflags |= CSB_OFDMARD | CSB_OFRESLT | CSB_OFINRPT;
1461*0Sstevel@tonic-gate 		csb->csb_nrslts = 7;
1462*0Sstevel@tonic-gate 		dmar_flags = DDI_DMA_READ;
1463*0Sstevel@tonic-gate 		break;
1464*0Sstevel@tonic-gate 
1465*0Sstevel@tonic-gate 	case FO_RDID:
1466*0Sstevel@tonic-gate 		csb->csb_opflags |= CSB_OFRESLT | CSB_OFINRPT;
1467*0Sstevel@tonic-gate 		csb->csb_nrslts = 7;
1468*0Sstevel@tonic-gate 		break;
1469*0Sstevel@tonic-gate 
1470*0Sstevel@tonic-gate 	case FO_SDRV:
1471*0Sstevel@tonic-gate 		sleep = 0;
1472*0Sstevel@tonic-gate 		csb->csb_nrslts = 1;
1473*0Sstevel@tonic-gate 		break;
1474*0Sstevel@tonic-gate 
1475*0Sstevel@tonic-gate 	case FO_SINT:
1476*0Sstevel@tonic-gate 		sleep = 0;
1477*0Sstevel@tonic-gate 		change = 0;
1478*0Sstevel@tonic-gate 		csb->csb_nrslts = 2;
1479*0Sstevel@tonic-gate 		break;
1480*0Sstevel@tonic-gate 
1481*0Sstevel@tonic-gate 	case FO_SPEC:
1482*0Sstevel@tonic-gate 		sleep = 0;
1483*0Sstevel@tonic-gate 		change = 0;
1484*0Sstevel@tonic-gate 		break;
1485*0Sstevel@tonic-gate 
1486*0Sstevel@tonic-gate 	default:
1487*0Sstevel@tonic-gate 		return (EINVAL);
1488*0Sstevel@tonic-gate 	}
1489*0Sstevel@tonic-gate 
1490*0Sstevel@tonic-gate 	csb->csb_dmahandle = NULL;
1491*0Sstevel@tonic-gate 	csb->csb_handle_bound = 0;
1492*0Sstevel@tonic-gate 	csb->csb_dmacookiecnt = 0;
1493*0Sstevel@tonic-gate 	csb->csb_dmacurrcookie = 0;
1494*0Sstevel@tonic-gate 	csb->csb_dmawincnt = 0;
1495*0Sstevel@tonic-gate 	csb->csb_dmacurrwin = 0;
1496*0Sstevel@tonic-gate 
1497*0Sstevel@tonic-gate 	if (csb->csb_opflags & (CSB_OFDMARD | CSB_OFDMAWT)) {
1498*0Sstevel@tonic-gate 		dmaattr = fdc_dma_attr;
1499*0Sstevel@tonic-gate 		dmaattr.dma_attr_granular = fjp->fj_chars->fdc_sec_size;
1500*0Sstevel@tonic-gate 
1501*0Sstevel@tonic-gate 		if (ddi_dma_alloc_handle(fcp->c_dip, &dmaattr, DDI_DMA_SLEEP,
1502*0Sstevel@tonic-gate 				0, &csb->csb_dmahandle) != DDI_SUCCESS) {
1503*0Sstevel@tonic-gate 			rval = EINVAL;
1504*0Sstevel@tonic-gate 			goto out;
1505*0Sstevel@tonic-gate 		}
1506*0Sstevel@tonic-gate 
1507*0Sstevel@tonic-gate 		dmar_flags |= (DDI_DMA_STREAMING | DDI_DMA_PARTIAL);
1508*0Sstevel@tonic-gate 		rval = ddi_dma_addr_bind_handle(csb->csb_dmahandle, NULL,
1509*0Sstevel@tonic-gate 				fdrp->fdr_addr, (uint_t)fdrp->fdr_nbytes,
1510*0Sstevel@tonic-gate 				dmar_flags, DDI_DMA_SLEEP, 0,
1511*0Sstevel@tonic-gate 				&csb->csb_dmacookie, &csb->csb_dmacookiecnt);
1512*0Sstevel@tonic-gate 
1513*0Sstevel@tonic-gate 		if (rval == DDI_DMA_MAPPED) {
1514*0Sstevel@tonic-gate 			csb->csb_dmawincnt = 1;
1515*0Sstevel@tonic-gate 			csb->csb_handle_bound = 1;
1516*0Sstevel@tonic-gate 		} else if (rval == DDI_DMA_PARTIAL_MAP) {
1517*0Sstevel@tonic-gate 			csb->csb_handle_bound = 1;
1518*0Sstevel@tonic-gate 			if (ddi_dma_numwin(csb->csb_dmahandle,
1519*0Sstevel@tonic-gate 			    &csb->csb_dmawincnt) != DDI_SUCCESS) {
1520*0Sstevel@tonic-gate 				cmn_err(CE_WARN,
1521*0Sstevel@tonic-gate 				    "fdrawioctl: dma numwin failed\n");
1522*0Sstevel@tonic-gate 				rval = EINVAL;
1523*0Sstevel@tonic-gate 				goto out;
1524*0Sstevel@tonic-gate 			}
1525*0Sstevel@tonic-gate 		} else {
1526*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdrawioctl: "
1527*0Sstevel@tonic-gate 			    "dma buf bind handle failed, rval = %d\n", rval);
1528*0Sstevel@tonic-gate 			rval = EINVAL;
1529*0Sstevel@tonic-gate 			goto out;
1530*0Sstevel@tonic-gate 		}
1531*0Sstevel@tonic-gate 	}
1532*0Sstevel@tonic-gate 
1533*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L1, FDEM_RAWI,
1534*0Sstevel@tonic-gate 	    (CE_CONT, "cmd: %x %x %x %x %x %x %x %x %x %x\n", csb->csb_cmd[0],
1535*0Sstevel@tonic-gate 	    csb->csb_cmd[1], csb->csb_cmd[2], csb->csb_cmd[3],
1536*0Sstevel@tonic-gate 	    csb->csb_cmd[4], csb->csb_cmd[5], csb->csb_cmd[6],
1537*0Sstevel@tonic-gate 	    csb->csb_cmd[7], csb->csb_cmd[8], csb->csb_cmd[9]));
1538*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L1, FDEM_RAWI,
1539*0Sstevel@tonic-gate 	    (CE_CONT, "nbytes: %x, opflags: %x, addr: %p, len: %x\n",
1540*0Sstevel@tonic-gate 	    csb->csb_ncmds, csb->csb_opflags, (void *)fdrp->fdr_addr,
1541*0Sstevel@tonic-gate 	    fdrp->fdr_nbytes));
1542*0Sstevel@tonic-gate 
1543*0Sstevel@tonic-gate 	/*
1544*0Sstevel@tonic-gate 	 * Note that we ignore any error returns from fdexec.
1545*0Sstevel@tonic-gate 	 * This is the way the driver has been, and it may be
1546*0Sstevel@tonic-gate 	 * that the raw ioctl senders simply don't want to
1547*0Sstevel@tonic-gate 	 * see any errors returned in this fashion.
1548*0Sstevel@tonic-gate 	 */
1549*0Sstevel@tonic-gate 
1550*0Sstevel@tonic-gate 	/*
1551*0Sstevel@tonic-gate 	 * VP/ix sense drive ioctl call checks for the error return.
1552*0Sstevel@tonic-gate 	 */
1553*0Sstevel@tonic-gate 
1554*0Sstevel@tonic-gate 	rval_exec = fdc_exec(fcp, sleep, change);
1555*0Sstevel@tonic-gate 
1556*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L1, FDEM_RAWI,
1557*0Sstevel@tonic-gate 	    (CE_CONT, "rslt: %x %x %x %x %x %x %x %x %x %x\n", csb->csb_rslt[0],
1558*0Sstevel@tonic-gate 	    csb->csb_rslt[1], csb->csb_rslt[2], csb->csb_rslt[3],
1559*0Sstevel@tonic-gate 	    csb->csb_rslt[4], csb->csb_rslt[5], csb->csb_rslt[6],
1560*0Sstevel@tonic-gate 	    csb->csb_rslt[7], csb->csb_rslt[8], csb->csb_rslt[9]));
1561*0Sstevel@tonic-gate 
1562*0Sstevel@tonic-gate 	/* copy results into fdr */
1563*0Sstevel@tonic-gate 	for (i = 0; i <= (int)csb->csb_nrslts; i++)
1564*0Sstevel@tonic-gate 		fdrp->fdr_result[i] = csb->csb_rslt[i];
1565*0Sstevel@tonic-gate /*	fdrp->fdr_nbytes = fdc->c_csb.csb_rlen;  return resid */
1566*0Sstevel@tonic-gate 
1567*0Sstevel@tonic-gate out:
1568*0Sstevel@tonic-gate 	if (csb->csb_dmahandle) {
1569*0Sstevel@tonic-gate 		if (csb->csb_handle_bound) {
1570*0Sstevel@tonic-gate 			if (ddi_dma_unbind_handle(csb->csb_dmahandle) !=
1571*0Sstevel@tonic-gate 			    DDI_SUCCESS)
1572*0Sstevel@tonic-gate 				cmn_err(CE_WARN, "fdrawioctl: "
1573*0Sstevel@tonic-gate 				    "dma unbind handle failed\n");
1574*0Sstevel@tonic-gate 			csb->csb_handle_bound = 0;
1575*0Sstevel@tonic-gate 		}
1576*0Sstevel@tonic-gate 		ddi_dma_free_handle(&csb->csb_dmahandle);
1577*0Sstevel@tonic-gate 		csb->csb_dmahandle = NULL;
1578*0Sstevel@tonic-gate 	}
1579*0Sstevel@tonic-gate 	if ((fdrp->fdr_cmd[0] & 0x0f) == FO_SDRV) {
1580*0Sstevel@tonic-gate 		return (rval_exec);
1581*0Sstevel@tonic-gate 	}
1582*0Sstevel@tonic-gate 	return (rval);
1583*0Sstevel@tonic-gate }
1584*0Sstevel@tonic-gate 
1585*0Sstevel@tonic-gate void
1586*0Sstevel@tonic-gate encode(xlate_tbl_t *tablep, int val, uchar_t *rcode)
1587*0Sstevel@tonic-gate {
1588*0Sstevel@tonic-gate 	do {
1589*0Sstevel@tonic-gate 		if (tablep->value >= val) {
1590*0Sstevel@tonic-gate 			*rcode = tablep->code;
1591*0Sstevel@tonic-gate 			return;
1592*0Sstevel@tonic-gate 		}
1593*0Sstevel@tonic-gate 	} while ((++tablep)->value);
1594*0Sstevel@tonic-gate 	*rcode = tablep->code;
1595*0Sstevel@tonic-gate 	cmn_err(CE_WARN, "fdc encode failed, table %p val %x code %x\n",
1596*0Sstevel@tonic-gate 	    (void *)tablep, val, (uint_t)*rcode);
1597*0Sstevel@tonic-gate }
1598*0Sstevel@tonic-gate 
1599*0Sstevel@tonic-gate int
1600*0Sstevel@tonic-gate decode(xlate_tbl_t *tablep, int kode, int *rvalue)
1601*0Sstevel@tonic-gate {
1602*0Sstevel@tonic-gate 	do  {
1603*0Sstevel@tonic-gate 		if (tablep->code == kode) {
1604*0Sstevel@tonic-gate 			*rvalue = tablep->value;
1605*0Sstevel@tonic-gate 			return (0);
1606*0Sstevel@tonic-gate 		}
1607*0Sstevel@tonic-gate 	} while ((++tablep)->value);
1608*0Sstevel@tonic-gate 	return (-1);
1609*0Sstevel@tonic-gate }
1610*0Sstevel@tonic-gate 
1611*0Sstevel@tonic-gate 
1612*0Sstevel@tonic-gate void
1613*0Sstevel@tonic-gate fdcquiesce(struct fdcntlr *fcp)
1614*0Sstevel@tonic-gate {
1615*0Sstevel@tonic-gate 	int unit;
1616*0Sstevel@tonic-gate 
1617*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L2, FDEM_RESE, (CE_NOTE, "fdcquiesce fcp %p",
1618*0Sstevel@tonic-gate 		(void*)fcp));
1619*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fcp->c_lock));
1620*0Sstevel@tonic-gate 
1621*0Sstevel@tonic-gate 	mutex_enter(&fcp->c_dorlock);
1622*0Sstevel@tonic-gate 
1623*0Sstevel@tonic-gate 	if (ddi_dmae_stop(fcp->c_dip, fcp->c_dmachan) != DDI_SUCCESS)
1624*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "fdcquiesce: dmae stop failed, "
1625*0Sstevel@tonic-gate 			"dip %p, dmachan %x\n",
1626*0Sstevel@tonic-gate 			(void*)fcp->c_dip, fcp->c_dmachan);
1627*0Sstevel@tonic-gate 
1628*0Sstevel@tonic-gate 	fcp->c_digout = (fcp->c_digout & (FD_DMTREN | FD_DRSEL)) | FD_ENABLE;
1629*0Sstevel@tonic-gate 	outb(fcp->c_regbase + FCR_DOR, fcp->c_digout);
1630*0Sstevel@tonic-gate 	drv_usecwait(20);
1631*0Sstevel@tonic-gate 	fcp->c_digout |= FD_RSETZ;
1632*0Sstevel@tonic-gate 	outb(fcp->c_regbase + FCR_DOR, fcp->c_digout);
1633*0Sstevel@tonic-gate 
1634*0Sstevel@tonic-gate 	mutex_exit(&fcp->c_dorlock);
1635*0Sstevel@tonic-gate 
1636*0Sstevel@tonic-gate 	/* count resets */
1637*0Sstevel@tonic-gate 	fcp->fdstats.reset++;
1638*0Sstevel@tonic-gate 	fcp->c_curunit = -1;
1639*0Sstevel@tonic-gate 	for (unit = 0; unit < NFDUN; unit++)
1640*0Sstevel@tonic-gate 		fcp->c_curpcyl[unit] = -1;
1641*0Sstevel@tonic-gate 
1642*0Sstevel@tonic-gate 	if (fcp->c_chip >= i82077) {
1643*0Sstevel@tonic-gate 		(void) fdc_docmd(fcp, configurecmd, 4);
1644*0Sstevel@tonic-gate 		/*
1645*0Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_docmd.
1646*0Sstevel@tonic-gate 		 */
1647*0Sstevel@tonic-gate 	}
1648*0Sstevel@tonic-gate }
1649*0Sstevel@tonic-gate 
1650*0Sstevel@tonic-gate void
1651*0Sstevel@tonic-gate fdcreadid(struct fdcntlr *fcp, struct fdcsb *csb)
1652*0Sstevel@tonic-gate {
1653*0Sstevel@tonic-gate 	static uchar_t readidcmd[2] = {FO_RDID | FO_MFM, 0};
1654*0Sstevel@tonic-gate 
1655*0Sstevel@tonic-gate 	readidcmd[1] = csb->csb_cmd[1];
1656*0Sstevel@tonic-gate 	(void) fdc_docmd(fcp, readidcmd, 2);
1657*0Sstevel@tonic-gate }
1658*0Sstevel@tonic-gate 
1659*0Sstevel@tonic-gate int
1660*0Sstevel@tonic-gate fdcseek(struct fdcntlr *fcp, int unit, int cyl)
1661*0Sstevel@tonic-gate {
1662*0Sstevel@tonic-gate 	static uchar_t seekabscmd[3] = {FO_SEEK, 0, 0};
1663*0Sstevel@tonic-gate 
1664*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L0, FDEM_RECA, (CE_CONT, "fdcseek unit %d to cyl %d\n",
1665*0Sstevel@tonic-gate 	    unit, cyl));
1666*0Sstevel@tonic-gate 	seekabscmd[1] = (uchar_t)unit;
1667*0Sstevel@tonic-gate 	seekabscmd[2] = (uchar_t)cyl;
1668*0Sstevel@tonic-gate 	return (fdc_docmd(fcp, seekabscmd, 3));
1669*0Sstevel@tonic-gate }
1670*0Sstevel@tonic-gate 
1671*0Sstevel@tonic-gate /*
1672*0Sstevel@tonic-gate  * Returns status of disk change line of selected drive.
1673*0Sstevel@tonic-gate  *	= 0 means diskette is present
1674*0Sstevel@tonic-gate  *	!= 0 means diskette was removed and current state is unknown
1675*0Sstevel@tonic-gate  */
1676*0Sstevel@tonic-gate int
1677*0Sstevel@tonic-gate fdcsense_chng(struct fdcntlr *fcp, int unit)
1678*0Sstevel@tonic-gate {
1679*0Sstevel@tonic-gate 	int digital_input;
1680*0Sstevel@tonic-gate 
1681*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L0, FDEM_SCHG,
1682*0Sstevel@tonic-gate 	    (CE_CONT, "fdcsense_chng unit %d\n", unit));
1683*0Sstevel@tonic-gate 	digital_input = inb(fcp->c_regbase + FCR_DIR);
1684*0Sstevel@tonic-gate 	if (fcp->c_mode == FDCMODE_30)
1685*0Sstevel@tonic-gate 		digital_input ^= FDI_DKCHG;
1686*0Sstevel@tonic-gate 	return (digital_input & FDI_DKCHG);
1687*0Sstevel@tonic-gate }
1688*0Sstevel@tonic-gate 
1689*0Sstevel@tonic-gate int
1690*0Sstevel@tonic-gate fdcsense_drv(struct fdcntlr *fcp, int unit)
1691*0Sstevel@tonic-gate {
1692*0Sstevel@tonic-gate 	static uchar_t sensedrvcmd[2] = {FO_SDRV, 0};
1693*0Sstevel@tonic-gate 	uchar_t senser;
1694*0Sstevel@tonic-gate 	int rval;
1695*0Sstevel@tonic-gate 
1696*0Sstevel@tonic-gate 	sensedrvcmd[1] = (uchar_t)unit;
1697*0Sstevel@tonic-gate 	(void) fdc_docmd(fcp, sensedrvcmd, 2);
1698*0Sstevel@tonic-gate 	/*
1699*0Sstevel@tonic-gate 	 * Ignored return. If failed, warning was issued by fdc_docmd.
1700*0Sstevel@tonic-gate 	 * fdc_results retrieves the controller/drive status
1701*0Sstevel@tonic-gate 	 */
1702*0Sstevel@tonic-gate 	if (rval = fdc_result(fcp, &senser, 1))
1703*0Sstevel@tonic-gate 		goto done;
1704*0Sstevel@tonic-gate 	if (senser & S3_WPROT)
1705*0Sstevel@tonic-gate 		fcp->c_unit[unit]->fj_flags |= FUNIT_WPROT;
1706*0Sstevel@tonic-gate 	else
1707*0Sstevel@tonic-gate 		fcp->c_unit[unit]->fj_flags &= ~FUNIT_WPROT;
1708*0Sstevel@tonic-gate done:
1709*0Sstevel@tonic-gate 	return (rval);
1710*0Sstevel@tonic-gate }
1711*0Sstevel@tonic-gate 
1712*0Sstevel@tonic-gate int
1713*0Sstevel@tonic-gate fdcsense_int(struct fdcntlr *fcp, int *unitp, int *cylp)
1714*0Sstevel@tonic-gate {
1715*0Sstevel@tonic-gate 	uchar_t senser[2];
1716*0Sstevel@tonic-gate 	int rval;
1717*0Sstevel@tonic-gate 
1718*0Sstevel@tonic-gate 	(void) fdc_docmd(fcp, &senseintcmd, 1);
1719*0Sstevel@tonic-gate 	/*
1720*0Sstevel@tonic-gate 	 * Ignored return. If failed, warning was issued by fdc_docmd.
1721*0Sstevel@tonic-gate 	 * fdc_results retrieves the controller/drive status
1722*0Sstevel@tonic-gate 	 */
1723*0Sstevel@tonic-gate 
1724*0Sstevel@tonic-gate 	if (!(rval = fdc_result(fcp, senser, 2))) {
1725*0Sstevel@tonic-gate 		if ((*senser & (S0_IVCMD | S0_SEKEND | S0_ECHK)) != S0_SEKEND)
1726*0Sstevel@tonic-gate 			rval = 1;
1727*0Sstevel@tonic-gate 		if (unitp)
1728*0Sstevel@tonic-gate 			*unitp = *senser & 3;
1729*0Sstevel@tonic-gate 		if (cylp)
1730*0Sstevel@tonic-gate 			*cylp = senser[1];
1731*0Sstevel@tonic-gate 	}
1732*0Sstevel@tonic-gate 	return (rval);
1733*0Sstevel@tonic-gate }
1734*0Sstevel@tonic-gate 
1735*0Sstevel@tonic-gate int
1736*0Sstevel@tonic-gate fdcspecify(struct fdcntlr *fcp, int xferrate, int steprate, int hlt)
1737*0Sstevel@tonic-gate {
1738*0Sstevel@tonic-gate 	static uchar_t perpindcmd[2] = {FO_PERP, 0};
1739*0Sstevel@tonic-gate 	static uchar_t specifycmd[3] = {FO_SPEC, 0, 0};
1740*0Sstevel@tonic-gate 
1741*0Sstevel@tonic-gate 	encode(drate_mfm, xferrate, &fcp->c_config);
1742*0Sstevel@tonic-gate 	outb(fcp->c_regbase + FCR_CCR, fcp->c_config);
1743*0Sstevel@tonic-gate 
1744*0Sstevel@tonic-gate 	if (fcp->c_chip >= i82077) {
1745*0Sstevel@tonic-gate 		/*
1746*0Sstevel@tonic-gate 		 * Use old style perpendicular mode command of 82077.
1747*0Sstevel@tonic-gate 		 */
1748*0Sstevel@tonic-gate 		if (xferrate == 1000) {
1749*0Sstevel@tonic-gate 			/* Set GAP and WGATE */
1750*0Sstevel@tonic-gate 			perpindcmd[1] = 3;
1751*0Sstevel@tonic-gate 			/* double step rate because xlate table is for 500Kb */
1752*0Sstevel@tonic-gate 			steprate <<= 1;
1753*0Sstevel@tonic-gate 			hlt <<= 1;
1754*0Sstevel@tonic-gate 		} else
1755*0Sstevel@tonic-gate 			perpindcmd[1] = 0;
1756*0Sstevel@tonic-gate 		(void) fdc_docmd(fcp, perpindcmd, 2);
1757*0Sstevel@tonic-gate 		/*
1758*0Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_docmd.
1759*0Sstevel@tonic-gate 		 */
1760*0Sstevel@tonic-gate 	}
1761*0Sstevel@tonic-gate 	encode(step_rate, steprate, &fcp->c_hutsrt);
1762*0Sstevel@tonic-gate 	specifycmd[1] = fcp->c_hutsrt |= 0x0F;	/* use max head unload time */
1763*0Sstevel@tonic-gate 	hlt = (hlt >= 256) ? 0 : (hlt >> 1);	/* encode head load time */
1764*0Sstevel@tonic-gate 	specifycmd[2] = fcp->c_hlt = hlt << 1;	/* make room for DMA bit */
1765*0Sstevel@tonic-gate 	return (fdc_docmd(fcp, specifycmd, 3));
1766*0Sstevel@tonic-gate }
1767*0Sstevel@tonic-gate 
1768*0Sstevel@tonic-gate int
1769*0Sstevel@tonic-gate fdcspdchange(struct fdcntlr *fcp, struct fcu_obj *fjp, int rpm)
1770*0Sstevel@tonic-gate {
1771*0Sstevel@tonic-gate 	int	retcode = 0;
1772*0Sstevel@tonic-gate 	uint_t	ddic;
1773*0Sstevel@tonic-gate 	uchar_t	deselect = 0;
1774*0Sstevel@tonic-gate 	uchar_t	ds_code;
1775*0Sstevel@tonic-gate 	uchar_t	enable_code;
1776*0Sstevel@tonic-gate 	uchar_t	save;
1777*0Sstevel@tonic-gate 
1778*0Sstevel@tonic-gate 	if (((fcp->c_flags & FCFLG_DSOUT) == 0 && rpm <= fjp->fj_rotspd) ||
1779*0Sstevel@tonic-gate 	    ((fcp->c_flags & FCFLG_DSOUT) && (fjp->fj_flags & FUNIT_3DMODE) &&
1780*0Sstevel@tonic-gate 	    rpm > fjp->fj_rotspd)) {
1781*0Sstevel@tonic-gate 		return (0);
1782*0Sstevel@tonic-gate 	}
1783*0Sstevel@tonic-gate 
1784*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L1, FDEM_SCHG,
1785*0Sstevel@tonic-gate 	    (CE_CONT, "fdcspdchange: %d rpm\n", rpm));
1786*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fcp->c_dorlock));
1787*0Sstevel@tonic-gate 
1788*0Sstevel@tonic-gate 	switch (fcp->c_chip) {
1789*0Sstevel@tonic-gate 	default:
1790*0Sstevel@tonic-gate 		break;
1791*0Sstevel@tonic-gate 	case i82077:
1792*0Sstevel@tonic-gate 		break;
1793*0Sstevel@tonic-gate 
1794*0Sstevel@tonic-gate 	case PC87322:
1795*0Sstevel@tonic-gate 		{
1796*0Sstevel@tonic-gate 		uchar_t nscmodecmd[5] = {FO_MODE, 0x02, 0x00, 0xC8, 0x00};
1797*0Sstevel@tonic-gate 
1798*0Sstevel@tonic-gate 		if (rpm > fjp->fj_rotspd) {
1799*0Sstevel@tonic-gate 			nscmodecmd[3] ^= 0xC0;
1800*0Sstevel@tonic-gate 			retcode = (fcp->c_flags ^ FCFLG_DSOUT) ||
1801*0Sstevel@tonic-gate 			    (fjp->fj_flags ^ FUNIT_3DMODE);
1802*0Sstevel@tonic-gate 			fcp->c_flags |= FCFLG_DSOUT;
1803*0Sstevel@tonic-gate 			fjp->fj_flags |= FUNIT_3DMODE;
1804*0Sstevel@tonic-gate 		} else {
1805*0Sstevel@tonic-gate 			/* program DENSEL to default output */
1806*0Sstevel@tonic-gate 			fcp->c_flags &= ~FCFLG_DSOUT;
1807*0Sstevel@tonic-gate 			retcode = fjp->fj_flags & FUNIT_3DMODE;
1808*0Sstevel@tonic-gate 			fjp->fj_flags &= ~FUNIT_3DMODE;
1809*0Sstevel@tonic-gate 		}
1810*0Sstevel@tonic-gate 		if (retcode && (fcp->c_digout & FD_DRSEL) == fcp->c_curunit) {
1811*0Sstevel@tonic-gate 			/* de-select drive while changing speed */
1812*0Sstevel@tonic-gate 			deselect = fcp->c_digout ^ FD_DRSEL;
1813*0Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_DOR, deselect);
1814*0Sstevel@tonic-gate 		}
1815*0Sstevel@tonic-gate 
1816*0Sstevel@tonic-gate 		(void) fdc_docmd(fcp, nscmodecmd, 5);
1817*0Sstevel@tonic-gate 		/*
1818*0Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_docmd.
1819*0Sstevel@tonic-gate 		 */
1820*0Sstevel@tonic-gate 		break;
1821*0Sstevel@tonic-gate 		}
1822*0Sstevel@tonic-gate 
1823*0Sstevel@tonic-gate 	case FDC37C665:
1824*0Sstevel@tonic-gate 		enable_code = FSA_ENA5;
1825*0Sstevel@tonic-gate 		goto SMC_config;
1826*0Sstevel@tonic-gate 
1827*0Sstevel@tonic-gate 	case FDC37C666:
1828*0Sstevel@tonic-gate 		enable_code = FSA_ENA6;
1829*0Sstevel@tonic-gate SMC_config:
1830*0Sstevel@tonic-gate 		if (rpm > fjp->fj_rotspd) {
1831*0Sstevel@tonic-gate 			/* force DENSEL output to active LOW */
1832*0Sstevel@tonic-gate 			ds_code = FSB_DSHI;
1833*0Sstevel@tonic-gate 			retcode = (fcp->c_flags ^ FCFLG_DSOUT) ||
1834*0Sstevel@tonic-gate 			    (fjp->fj_flags ^ FUNIT_3DMODE);
1835*0Sstevel@tonic-gate 			fcp->c_flags |= FCFLG_DSOUT;
1836*0Sstevel@tonic-gate 			fjp->fj_flags |= FUNIT_3DMODE;
1837*0Sstevel@tonic-gate 		} else {
1838*0Sstevel@tonic-gate 			/* program DENSEL to default output */
1839*0Sstevel@tonic-gate 			ds_code = 0;
1840*0Sstevel@tonic-gate 			fcp->c_flags &= ~FCFLG_DSOUT;
1841*0Sstevel@tonic-gate 			retcode = fjp->fj_flags & FUNIT_3DMODE;
1842*0Sstevel@tonic-gate 			fjp->fj_flags &= ~FUNIT_3DMODE;
1843*0Sstevel@tonic-gate 		}
1844*0Sstevel@tonic-gate 		if (retcode && (fcp->c_digout & FD_DRSEL) == fcp->c_curunit) {
1845*0Sstevel@tonic-gate 			/* de-select drive while changing speed */
1846*0Sstevel@tonic-gate 			deselect = fcp->c_digout ^ FD_DRSEL;
1847*0Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_DOR, deselect);
1848*0Sstevel@tonic-gate 		}
1849*0Sstevel@tonic-gate 		save = inb(fcp->c_regbase + FCR_SRA);
1850*0Sstevel@tonic-gate 
1851*0Sstevel@tonic-gate 		/* enter configuration mode */
1852*0Sstevel@tonic-gate 		ddic = ddi_enter_critical();
1853*0Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_SRA, enable_code);
1854*0Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_SRA, enable_code);
1855*0Sstevel@tonic-gate 		ddi_exit_critical(ddic);
1856*0Sstevel@tonic-gate 
1857*0Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_SRA, FSA_CR5);
1858*0Sstevel@tonic-gate 		enable_code = inb(fcp->c_regbase + FCR_SRB) & FSB_DSDEF;
1859*0Sstevel@tonic-gate 		/* update DENSEL mode bits */
1860*0Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_SRB, enable_code | ds_code);
1861*0Sstevel@tonic-gate 
1862*0Sstevel@tonic-gate 		/* exit configuration mode */
1863*0Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_SRA, FSA_DISB);
1864*0Sstevel@tonic-gate 		drv_usecwait(10);
1865*0Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_SRA, save);
1866*0Sstevel@tonic-gate 		break;
1867*0Sstevel@tonic-gate 	}
1868*0Sstevel@tonic-gate 	if (deselect)
1869*0Sstevel@tonic-gate 		/* reselect drive */
1870*0Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_DOR, fcp->c_digout);
1871*0Sstevel@tonic-gate 	return (retcode);
1872*0Sstevel@tonic-gate }
1873*0Sstevel@tonic-gate 
1874*0Sstevel@tonic-gate static int
1875*0Sstevel@tonic-gate fdc_motorsm(struct fcu_obj *fjp, int input, int timeval)
1876*0Sstevel@tonic-gate {
1877*0Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
1878*0Sstevel@tonic-gate 	int unit = fjp->fj_unit & 3;
1879*0Sstevel@tonic-gate 	int old_mstate;
1880*0Sstevel@tonic-gate 	int rval = 0;
1881*0Sstevel@tonic-gate 	uchar_t motorbit;
1882*0Sstevel@tonic-gate 
1883*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fcp->c_dorlock));
1884*0Sstevel@tonic-gate 	old_mstate = fcp->c_mtrstate[unit];
1885*0Sstevel@tonic-gate 	encode(motor_onbits, unit, &motorbit);
1886*0Sstevel@tonic-gate 
1887*0Sstevel@tonic-gate 	switch (input) {
1888*0Sstevel@tonic-gate 	case FMI_TIMER:		/* timer expired */
1889*0Sstevel@tonic-gate 		fcp->c_motort[unit] = 0;
1890*0Sstevel@tonic-gate 		switch (old_mstate) {
1891*0Sstevel@tonic-gate 		case FMS_START:
1892*0Sstevel@tonic-gate 		case FMS_DELAY:
1893*0Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = FMS_ON;
1894*0Sstevel@tonic-gate 			break;
1895*0Sstevel@tonic-gate 		case FMS_KILLST:
1896*0Sstevel@tonic-gate 			fcp->c_motort[unit] = timeout(fdmotort, (void *)fjp,
1897*0Sstevel@tonic-gate 			    drv_usectohz(1000000));
1898*0Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = FMS_IDLE;
1899*0Sstevel@tonic-gate 			break;
1900*0Sstevel@tonic-gate 		case FMS_IDLE:
1901*0Sstevel@tonic-gate 			fcp->c_digout &= ~motorbit;
1902*0Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_DOR, fcp->c_digout);
1903*0Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = FMS_OFF;
1904*0Sstevel@tonic-gate 			fjp->fj_flags &= ~FUNIT_3DMODE;
1905*0Sstevel@tonic-gate 			break;
1906*0Sstevel@tonic-gate 		case 86:
1907*0Sstevel@tonic-gate 			rval = -1;
1908*0Sstevel@tonic-gate 			break;
1909*0Sstevel@tonic-gate 		case FMS_OFF:
1910*0Sstevel@tonic-gate 		case FMS_ON:
1911*0Sstevel@tonic-gate 		default:
1912*0Sstevel@tonic-gate 			rval = -2;
1913*0Sstevel@tonic-gate 		}
1914*0Sstevel@tonic-gate 		break;
1915*0Sstevel@tonic-gate 
1916*0Sstevel@tonic-gate 	case FMI_STARTCMD:	/* start command */
1917*0Sstevel@tonic-gate 		switch (old_mstate) {
1918*0Sstevel@tonic-gate 		case FMS_IDLE:
1919*0Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = 86;
1920*0Sstevel@tonic-gate 			mutex_exit(&fcp->c_dorlock);
1921*0Sstevel@tonic-gate 			(void) untimeout(fcp->c_motort[unit]);
1922*0Sstevel@tonic-gate 			mutex_enter(&fcp->c_dorlock);
1923*0Sstevel@tonic-gate 			fcp->c_motort[unit] = 0;
1924*0Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = FMS_ON;
1925*0Sstevel@tonic-gate 			break;
1926*0Sstevel@tonic-gate 		case FMS_OFF:
1927*0Sstevel@tonic-gate 			fcp->c_digout |= motorbit;
1928*0Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_DOR, fcp->c_digout);
1929*0Sstevel@tonic-gate 
1930*0Sstevel@tonic-gate 			/* start motor_spinup_timer */
1931*0Sstevel@tonic-gate 			ASSERT(timeval > 0);
1932*0Sstevel@tonic-gate 			fcp->c_motort[unit] = timeout(fdmotort,  (void *)fjp,
1933*0Sstevel@tonic-gate 			    drv_usectohz(100000 * timeval));
1934*0Sstevel@tonic-gate 			/* FALLTHROUGH */
1935*0Sstevel@tonic-gate 		case FMS_KILLST:
1936*0Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = FMS_START;
1937*0Sstevel@tonic-gate 			break;
1938*0Sstevel@tonic-gate 		default:
1939*0Sstevel@tonic-gate 			rval = -2;
1940*0Sstevel@tonic-gate 		}
1941*0Sstevel@tonic-gate 		break;
1942*0Sstevel@tonic-gate 
1943*0Sstevel@tonic-gate 	case FMI_RSTARTCMD:	/* restart command */
1944*0Sstevel@tonic-gate 		if (fcp->c_motort[unit] != 0) {
1945*0Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = 86;
1946*0Sstevel@tonic-gate 			mutex_exit(&fcp->c_dorlock);
1947*0Sstevel@tonic-gate 			(void) untimeout(fcp->c_motort[unit]);
1948*0Sstevel@tonic-gate 			mutex_enter(&fcp->c_dorlock);
1949*0Sstevel@tonic-gate 		}
1950*0Sstevel@tonic-gate 		ASSERT(timeval > 0);
1951*0Sstevel@tonic-gate 		fcp->c_motort[unit] = timeout(fdmotort, (void *)fjp,
1952*0Sstevel@tonic-gate 		    drv_usectohz(100000 * timeval));
1953*0Sstevel@tonic-gate 		fcp->c_mtrstate[unit] = FMS_START;
1954*0Sstevel@tonic-gate 		break;
1955*0Sstevel@tonic-gate 
1956*0Sstevel@tonic-gate 	case FMI_DELAYCMD:	/* delay command */
1957*0Sstevel@tonic-gate 		if (fcp->c_motort[unit] == 0)
1958*0Sstevel@tonic-gate 			fcp->c_motort[unit] = timeout(fdmotort,  (void *)fjp,
1959*0Sstevel@tonic-gate 			    drv_usectohz(15000));
1960*0Sstevel@tonic-gate 		fcp->c_mtrstate[unit] = FMS_DELAY;
1961*0Sstevel@tonic-gate 		break;
1962*0Sstevel@tonic-gate 
1963*0Sstevel@tonic-gate 	case FMI_IDLECMD:	/* idle command */
1964*0Sstevel@tonic-gate 		switch (old_mstate) {
1965*0Sstevel@tonic-gate 		case FMS_DELAY:
1966*0Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = 86;
1967*0Sstevel@tonic-gate 			mutex_exit(&fcp->c_dorlock);
1968*0Sstevel@tonic-gate 			(void) untimeout(fcp->c_motort[unit]);
1969*0Sstevel@tonic-gate 			mutex_enter(&fcp->c_dorlock);
1970*0Sstevel@tonic-gate 			/* FALLTHROUGH */
1971*0Sstevel@tonic-gate 		case FMS_ON:
1972*0Sstevel@tonic-gate 			ASSERT(timeval > 0);
1973*0Sstevel@tonic-gate 			fcp->c_motort[unit] = timeout(fdmotort, (void *)fjp,
1974*0Sstevel@tonic-gate 			    drv_usectohz(100000 * timeval));
1975*0Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = FMS_IDLE;
1976*0Sstevel@tonic-gate 			break;
1977*0Sstevel@tonic-gate 		case FMS_START:
1978*0Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = FMS_KILLST;
1979*0Sstevel@tonic-gate 			break;
1980*0Sstevel@tonic-gate 		default:
1981*0Sstevel@tonic-gate 			rval = -2;
1982*0Sstevel@tonic-gate 		}
1983*0Sstevel@tonic-gate 		break;
1984*0Sstevel@tonic-gate 
1985*0Sstevel@tonic-gate 	default:
1986*0Sstevel@tonic-gate 		rval = -3;
1987*0Sstevel@tonic-gate 	}
1988*0Sstevel@tonic-gate 	if (rval) {
1989*0Sstevel@tonic-gate 		FCERRPRINT(FDEP_L4, FDEM_EXEC, (CE_WARN,
1990*0Sstevel@tonic-gate 		    "fdc_motorsm: unit %d  bad input %d or bad state %d",
1991*0Sstevel@tonic-gate 		    (int)fjp->fj_unit, input, old_mstate));
1992*0Sstevel@tonic-gate #if 0
1993*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
1994*0Sstevel@tonic-gate 		    "fdc_motorsm: unit %d  bad input %d or bad state %d\n",
1995*0Sstevel@tonic-gate 		    (int)fjp->fj_unit, input, old_mstate);
1996*0Sstevel@tonic-gate 		fcp->c_mtrstate[unit] = FMS_OFF;
1997*0Sstevel@tonic-gate 		if (fcp->c_motort[unit] != 0) {
1998*0Sstevel@tonic-gate 			mutex_exit(&fcp->c_dorlock);
1999*0Sstevel@tonic-gate 			(void) untimeout(fcp->c_motort[unit]);
2000*0Sstevel@tonic-gate 			mutex_enter(&fcp->c_dorlock);
2001*0Sstevel@tonic-gate 			fcp->c_motort[unit] = 0;
2002*0Sstevel@tonic-gate 		}
2003*0Sstevel@tonic-gate #endif
2004*0Sstevel@tonic-gate 	} else
2005*0Sstevel@tonic-gate 		FCERRPRINT(FDEP_L0, FDEM_EXEC,
2006*0Sstevel@tonic-gate 		    (CE_CONT, "fdc_motorsm unit %d: input %d,  %d -> %d\n",
2007*0Sstevel@tonic-gate 		    (int)fjp->fj_unit, input, old_mstate,
2008*0Sstevel@tonic-gate 		    fcp->c_mtrstate[unit]));
2009*0Sstevel@tonic-gate 	return (rval);
2010*0Sstevel@tonic-gate }
2011*0Sstevel@tonic-gate 
2012*0Sstevel@tonic-gate /*
2013*0Sstevel@tonic-gate  * fdmotort
2014*0Sstevel@tonic-gate  *	is called from timeout() when a motor timer has expired.
2015*0Sstevel@tonic-gate  */
2016*0Sstevel@tonic-gate static void
2017*0Sstevel@tonic-gate fdmotort(void *arg)
2018*0Sstevel@tonic-gate {
2019*0Sstevel@tonic-gate 	struct fcu_obj *fjp = (struct fcu_obj *)arg;
2020*0Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
2021*0Sstevel@tonic-gate 	struct fdcsb *csb = &fcp->c_csb;
2022*0Sstevel@tonic-gate 	int unit = fjp->fj_unit & 3;
2023*0Sstevel@tonic-gate 	int mval;
2024*0Sstevel@tonic-gate 	int newxstate = 0;
2025*0Sstevel@tonic-gate 
2026*0Sstevel@tonic-gate 	mutex_enter(&fcp->c_dorlock);
2027*0Sstevel@tonic-gate 	mval = fdc_motorsm(fjp, FMI_TIMER, 0);
2028*0Sstevel@tonic-gate 	mutex_exit(&fcp->c_dorlock);
2029*0Sstevel@tonic-gate 	if (mval < 0)
2030*0Sstevel@tonic-gate 		return;
2031*0Sstevel@tonic-gate 
2032*0Sstevel@tonic-gate 	mutex_enter(&fcp->c_lock);
2033*0Sstevel@tonic-gate 
2034*0Sstevel@tonic-gate 	if ((fcp->c_flags & FCFLG_WAITING) &&
2035*0Sstevel@tonic-gate 	    fcp->c_mtrstate[unit] == FMS_ON &&
2036*0Sstevel@tonic-gate 	    (csb->csb_xstate == FXS_MTRON || csb->csb_xstate == FXS_HDST ||
2037*0Sstevel@tonic-gate 	    csb->csb_xstate == FXS_DKCHGX))
2038*0Sstevel@tonic-gate 		newxstate = fdc_statemach(fcp);
2039*0Sstevel@tonic-gate 		if (newxstate == -1) {
2040*0Sstevel@tonic-gate 			FCERRPRINT(FDEP_L3, FDEM_EXEC,
2041*0Sstevel@tonic-gate 			    (CE_WARN,
2042*0Sstevel@tonic-gate 			    "fdc_motort unit %d: motor ready but bad xstate",
2043*0Sstevel@tonic-gate 			    (int)fjp->fj_unit));
2044*0Sstevel@tonic-gate 			fcp->c_csb.csb_cmdstat = EIO;
2045*0Sstevel@tonic-gate 		}
2046*0Sstevel@tonic-gate 		if (newxstate == -1 || newxstate == FXS_END) {
2047*0Sstevel@tonic-gate 			fcp->c_flags ^= FCFLG_WAITING;
2048*0Sstevel@tonic-gate 			cv_signal(&fcp->c_iocv);
2049*0Sstevel@tonic-gate 		}
2050*0Sstevel@tonic-gate 	mutex_exit(&fcp->c_lock);
2051*0Sstevel@tonic-gate }
2052*0Sstevel@tonic-gate 
2053*0Sstevel@tonic-gate /*
2054*0Sstevel@tonic-gate  * DMA interrupt service routine
2055*0Sstevel@tonic-gate  *
2056*0Sstevel@tonic-gate  *	Called by EISA dma interrupt service routine when buffer chaining
2057*0Sstevel@tonic-gate  *	is required.
2058*0Sstevel@tonic-gate  */
2059*0Sstevel@tonic-gate 
2060*0Sstevel@tonic-gate ddi_dma_cookie_t *
2061*0Sstevel@tonic-gate fdc_dmae_isr(struct fdcntlr *fcp)
2062*0Sstevel@tonic-gate {
2063*0Sstevel@tonic-gate 	struct fdcsb *csb = &fcp->c_csb;
2064*0Sstevel@tonic-gate 	off_t off;
2065*0Sstevel@tonic-gate 	size_t len;
2066*0Sstevel@tonic-gate 
2067*0Sstevel@tonic-gate 	if (csb->csb_dmahandle && !csb->csb_cmdstat) {
2068*0Sstevel@tonic-gate 		if (++csb->csb_dmacurrcookie < csb->csb_dmacookiecnt) {
2069*0Sstevel@tonic-gate 			ddi_dma_nextcookie(csb->csb_dmahandle,
2070*0Sstevel@tonic-gate 			    &csb->csb_dmacookie);
2071*0Sstevel@tonic-gate 			return (&csb->csb_dmacookie);
2072*0Sstevel@tonic-gate 		} else if (++csb->csb_dmacurrwin < csb->csb_dmawincnt) {
2073*0Sstevel@tonic-gate 			if (ddi_dma_getwin(csb->csb_dmahandle,
2074*0Sstevel@tonic-gate 			    csb->csb_dmacurrwin, &off, &len,
2075*0Sstevel@tonic-gate 			    &csb->csb_dmacookie,
2076*0Sstevel@tonic-gate 			    &csb->csb_dmacookiecnt) != DDI_SUCCESS) {
2077*0Sstevel@tonic-gate 				return (NULL);
2078*0Sstevel@tonic-gate 			}
2079*0Sstevel@tonic-gate 			csb->csb_dmacurrcookie = 0;
2080*0Sstevel@tonic-gate 			return (&csb->csb_dmacookie);
2081*0Sstevel@tonic-gate 		}
2082*0Sstevel@tonic-gate 	} else
2083*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "fdc: unsolicited DMA interrupt\n");
2084*0Sstevel@tonic-gate 	return (NULL);
2085*0Sstevel@tonic-gate }
2086*0Sstevel@tonic-gate 
2087*0Sstevel@tonic-gate 
2088*0Sstevel@tonic-gate /*
2089*0Sstevel@tonic-gate  * returns:
2090*0Sstevel@tonic-gate  *	0 if all ok,
2091*0Sstevel@tonic-gate  *	ENXIO - diskette not in drive
2092*0Sstevel@tonic-gate  *	ETIMEDOUT - for immediate operations that timed out
2093*0Sstevel@tonic-gate  *	EBUSY - if stupid chip is locked busy???
2094*0Sstevel@tonic-gate  *	ENOEXEC - for timeout during sending cmds to chip
2095*0Sstevel@tonic-gate  *
2096*0Sstevel@tonic-gate  * to sleep: set sleep
2097*0Sstevel@tonic-gate  * to check for disk changed: set change
2098*0Sstevel@tonic-gate  */
2099*0Sstevel@tonic-gate static int
2100*0Sstevel@tonic-gate fdc_exec(struct fdcntlr *fcp, int sleep, int change)
2101*0Sstevel@tonic-gate {
2102*0Sstevel@tonic-gate 	struct ddi_dmae_req dmaereq;
2103*0Sstevel@tonic-gate 	struct fcu_obj *fjp;
2104*0Sstevel@tonic-gate 	struct fdcsb *csb;
2105*0Sstevel@tonic-gate 	off_t off;
2106*0Sstevel@tonic-gate 	size_t len;
2107*0Sstevel@tonic-gate 	int unit;
2108*0Sstevel@tonic-gate 
2109*0Sstevel@tonic-gate 	mutex_enter(&fcp->c_lock);
2110*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L0, FDEM_EXEC,
2111*0Sstevel@tonic-gate 	    (CE_CONT, "fdc_exec: sleep %x change %x\n", sleep, change));
2112*0Sstevel@tonic-gate 	csb = &fcp->c_csb;
2113*0Sstevel@tonic-gate 	unit = csb->csb_drive;
2114*0Sstevel@tonic-gate 	fjp = fcp->c_unit[unit];
2115*0Sstevel@tonic-gate 
2116*0Sstevel@tonic-gate 	if (csb->csb_opflags & CSB_OFINRPT) {
2117*0Sstevel@tonic-gate 		if (*csb->csb_cmd == FO_RECAL)
2118*0Sstevel@tonic-gate 			csb->csb_npcyl = 0;
2119*0Sstevel@tonic-gate 		else if ((*csb->csb_cmd & ~FO_MFM) != FO_FRMT)
2120*0Sstevel@tonic-gate 			csb->csb_npcyl =
2121*0Sstevel@tonic-gate 			    csb->csb_cmd[2] * fjp->fj_chars->fdc_steps;
2122*0Sstevel@tonic-gate 		csb->csb_xstate = FXS_START;
2123*0Sstevel@tonic-gate 	} else
2124*0Sstevel@tonic-gate 		csb->csb_xstate = FXS_DOIT;
2125*0Sstevel@tonic-gate 	csb->csb_retrys = 0;
2126*0Sstevel@tonic-gate 	csb->csb_ourtrys = 0;
2127*0Sstevel@tonic-gate 
2128*0Sstevel@tonic-gate 	if (csb->csb_dmahandle) {
2129*0Sstevel@tonic-gate 		/* ensure that entire format xfer is in one cookie */
2130*0Sstevel@tonic-gate 		/*
2131*0Sstevel@tonic-gate 		 * The change from  ddi_dma_buf/addr_setup() to
2132*0Sstevel@tonic-gate 		 * ddi_dma_buf/addr_bind_handle() has already loaded
2133*0Sstevel@tonic-gate 		 * the first DMA window and cookie.
2134*0Sstevel@tonic-gate 		 */
2135*0Sstevel@tonic-gate 		if ((*csb->csb_cmd & ~FO_MFM) == FO_FRMT &&
2136*0Sstevel@tonic-gate 		    (4 * csb->csb_cmd[3]) != csb->csb_dmacookie.dmac_size) {
2137*0Sstevel@tonic-gate 			mutex_exit(&fcp->c_lock);
2138*0Sstevel@tonic-gate 			return (EINVAL);
2139*0Sstevel@tonic-gate 		}
2140*0Sstevel@tonic-gate 	}
2141*0Sstevel@tonic-gate 
2142*0Sstevel@tonic-gate retry:
2143*0Sstevel@tonic-gate 	if (fcp->c_curunit != unit || !(fjp->fj_flags & FUNIT_CHAROK)) {
2144*0Sstevel@tonic-gate 		fcp->c_curunit = unit;
2145*0Sstevel@tonic-gate 		fjp->fj_flags |= FUNIT_CHAROK;
2146*0Sstevel@tonic-gate 		if (fjp->fj_chars->fdc_transfer_rate == 417) {
2147*0Sstevel@tonic-gate 			/* XXX hack for fdformat */
2148*0Sstevel@tonic-gate 			/* fjp->fj_chars->fdc_transfer_rate == 500;	*/
2149*0Sstevel@tonic-gate 			fjp->fj_attr->fda_rotatespd = 360;
2150*0Sstevel@tonic-gate 		}
2151*0Sstevel@tonic-gate 		if (fdcspecify(fcp, fjp->fj_chars->fdc_transfer_rate,
2152*0Sstevel@tonic-gate 		    fjp->fj_drive->fdd_steprate, 40))
2153*0Sstevel@tonic-gate 			cmn_err(CE_WARN,
2154*0Sstevel@tonic-gate 			    "fdc_select: controller setup rejected "
2155*0Sstevel@tonic-gate 			    "fdcntrl %p transfer rate %x step rate %x "
2156*0Sstevel@tonic-gate 			    "head load time 40\n", (void*)fcp,
2157*0Sstevel@tonic-gate 			    fjp->fj_chars->fdc_transfer_rate,
2158*0Sstevel@tonic-gate 			    fjp->fj_drive->fdd_steprate);
2159*0Sstevel@tonic-gate 
2160*0Sstevel@tonic-gate 		mutex_enter(&fcp->c_dorlock);
2161*0Sstevel@tonic-gate 		if (fdcspdchange(fcp, fjp, fjp->fj_attr->fda_rotatespd)) {
2162*0Sstevel@tonic-gate 			/* 3D drive requires 500 ms for speed change */
2163*0Sstevel@tonic-gate 			(void) fdc_motorsm(fjp, FMI_RSTARTCMD, 5);
2164*0Sstevel@tonic-gate 			/*
2165*0Sstevel@tonic-gate 			 * Return value ignored - fdcmotort deals with failure.
2166*0Sstevel@tonic-gate 			 */
2167*0Sstevel@tonic-gate 		}
2168*0Sstevel@tonic-gate 		mutex_exit(&fcp->c_dorlock);
2169*0Sstevel@tonic-gate 	}
2170*0Sstevel@tonic-gate 
2171*0Sstevel@tonic-gate 	/*
2172*0Sstevel@tonic-gate 	 * If checking for disk_change is enabled
2173*0Sstevel@tonic-gate 	 * (i.e. not seeking in fdresetchng),
2174*0Sstevel@tonic-gate 	 * we sample the DSKCHG line to see if the diskette has wandered away.
2175*0Sstevel@tonic-gate 	 */
2176*0Sstevel@tonic-gate 	if (change && fdcsense_chng(fcp, unit)) {
2177*0Sstevel@tonic-gate 		FCERRPRINT(FDEP_L3, FDEM_EXEC,
2178*0Sstevel@tonic-gate 		    (CE_WARN, "diskette %d changed!!!", csb->csb_drive));
2179*0Sstevel@tonic-gate 		fcp->c_unit[unit]->fj_flags |= FUNIT_CHANGED;
2180*0Sstevel@tonic-gate 		/*
2181*0Sstevel@tonic-gate 		 * If the diskette is still gone... so are we, adios!
2182*0Sstevel@tonic-gate 		 */
2183*0Sstevel@tonic-gate 		if (fdcheckdisk(fcp, unit)) {
2184*0Sstevel@tonic-gate 			mutex_exit(&fcp->c_lock);
2185*0Sstevel@tonic-gate 
2186*0Sstevel@tonic-gate 			/* VP/ix expects an EBUSY return here */
2187*0Sstevel@tonic-gate 			if (*csb->csb_cmd == FO_SDRV) {
2188*0Sstevel@tonic-gate 				return (EBUSY);
2189*0Sstevel@tonic-gate 			}
2190*0Sstevel@tonic-gate 			return (ENXIO);
2191*0Sstevel@tonic-gate 		}
2192*0Sstevel@tonic-gate 		/*
2193*0Sstevel@tonic-gate 		 * delay to ensure that new diskette is up to speed
2194*0Sstevel@tonic-gate 		 */
2195*0Sstevel@tonic-gate 		mutex_enter(&fcp->c_dorlock);
2196*0Sstevel@tonic-gate 		(void) fdc_motorsm(fjp, FMI_RSTARTCMD,
2197*0Sstevel@tonic-gate 			fjp->fj_drive->fdd_motoron);
2198*0Sstevel@tonic-gate 		/*
2199*0Sstevel@tonic-gate 		 * Return value ignored - fdcmotort deals with failure.
2200*0Sstevel@tonic-gate 		 */
2201*0Sstevel@tonic-gate 		mutex_exit(&fcp->c_dorlock);
2202*0Sstevel@tonic-gate 	}
2203*0Sstevel@tonic-gate 
2204*0Sstevel@tonic-gate 	/*
2205*0Sstevel@tonic-gate 	 * gather some statistics
2206*0Sstevel@tonic-gate 	 */
2207*0Sstevel@tonic-gate 	switch (csb->csb_cmd[0] & 0x1f) {
2208*0Sstevel@tonic-gate 	case FO_RDDAT:
2209*0Sstevel@tonic-gate 		fcp->fdstats.rd++;
2210*0Sstevel@tonic-gate 		break;
2211*0Sstevel@tonic-gate 	case FO_WRDAT:
2212*0Sstevel@tonic-gate 		fcp->fdstats.wr++;
2213*0Sstevel@tonic-gate 		break;
2214*0Sstevel@tonic-gate 	case FO_RECAL:
2215*0Sstevel@tonic-gate 		fcp->fdstats.recal++;
2216*0Sstevel@tonic-gate 		break;
2217*0Sstevel@tonic-gate 	case FO_FRMT:
2218*0Sstevel@tonic-gate 		fcp->fdstats.form++;
2219*0Sstevel@tonic-gate 		break;
2220*0Sstevel@tonic-gate 	default:
2221*0Sstevel@tonic-gate 		fcp->fdstats.other++;
2222*0Sstevel@tonic-gate 		break;
2223*0Sstevel@tonic-gate 	}
2224*0Sstevel@tonic-gate 
2225*0Sstevel@tonic-gate 	bzero(csb->csb_rslt, 10);
2226*0Sstevel@tonic-gate 	csb->csb_cmdstat = 0;
2227*0Sstevel@tonic-gate 
2228*0Sstevel@tonic-gate 	if (csb->csb_dmahandle) {
2229*0Sstevel@tonic-gate 		bzero(&dmaereq, sizeof (struct ddi_dmae_req));
2230*0Sstevel@tonic-gate 		dmaereq.der_command = (csb->csb_opflags & CSB_OFDMAWT) ?
2231*0Sstevel@tonic-gate 		    DMAE_CMD_WRITE : DMAE_CMD_READ;
2232*0Sstevel@tonic-gate 		/*
2233*0Sstevel@tonic-gate 		 * setup for dma buffer chaining regardless of bus capability
2234*0Sstevel@tonic-gate 		 */
2235*0Sstevel@tonic-gate 		dmaereq.der_bufprocess = DMAE_BUF_CHAIN;
2236*0Sstevel@tonic-gate 		dmaereq.proc = fdc_dmae_isr;
2237*0Sstevel@tonic-gate 		dmaereq.procparms = (void *)fcp;
2238*0Sstevel@tonic-gate 		if (ddi_dmae_prog(fcp->c_dip, &dmaereq, &csb->csb_dmacookie,
2239*0Sstevel@tonic-gate 		    fcp->c_dmachan) != DDI_SUCCESS)
2240*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdc_exec: dmae prog failed, "
2241*0Sstevel@tonic-gate 				"dip %p, dmachan %x\n",
2242*0Sstevel@tonic-gate 				(void*)fcp->c_dip, fcp->c_dmachan);
2243*0Sstevel@tonic-gate 	}
2244*0Sstevel@tonic-gate 
2245*0Sstevel@tonic-gate 	if ((fdc_statemach(fcp) == FXS_DOWT) && !sleep) {
2246*0Sstevel@tonic-gate 		/*
2247*0Sstevel@tonic-gate 		 * If the operation has no results - then just return
2248*0Sstevel@tonic-gate 		 */
2249*0Sstevel@tonic-gate 		if (!csb->csb_nrslts) {
2250*0Sstevel@tonic-gate 			mutex_exit(&fcp->c_lock);
2251*0Sstevel@tonic-gate 			return (0);
2252*0Sstevel@tonic-gate 		}
2253*0Sstevel@tonic-gate 		/*
2254*0Sstevel@tonic-gate 		 * this operation has no interrupt and an immediate result
2255*0Sstevel@tonic-gate 		 * so wait for the results and stuff them into the csb
2256*0Sstevel@tonic-gate 		 */
2257*0Sstevel@tonic-gate 		if (fdc_statemach(fcp) == -1) {
2258*0Sstevel@tonic-gate 			mutex_exit(&fcp->c_lock);
2259*0Sstevel@tonic-gate 			return (EIO);
2260*0Sstevel@tonic-gate 		}
2261*0Sstevel@tonic-gate 	} else {
2262*0Sstevel@tonic-gate 		fcp->c_flags |= FCFLG_WAITING;
2263*0Sstevel@tonic-gate 		/*
2264*0Sstevel@tonic-gate 		 * wait for completion interrupt
2265*0Sstevel@tonic-gate 		 */
2266*0Sstevel@tonic-gate 		while (fcp->c_flags & FCFLG_WAITING) {
2267*0Sstevel@tonic-gate 			cv_wait(&fcp->c_iocv, &fcp->c_lock);
2268*0Sstevel@tonic-gate 		}
2269*0Sstevel@tonic-gate 	}
2270*0Sstevel@tonic-gate 
2271*0Sstevel@tonic-gate 	/*
2272*0Sstevel@tonic-gate 	 * See if there was an error detected, if so, fdrecover()
2273*0Sstevel@tonic-gate 	 * will check it out and say what to do.
2274*0Sstevel@tonic-gate 	 *
2275*0Sstevel@tonic-gate 	 * Don't do this, though, if this was the Sense Drive Status
2276*0Sstevel@tonic-gate 	 * or the Dump Registers command.
2277*0Sstevel@tonic-gate 	 */
2278*0Sstevel@tonic-gate 	if (csb->csb_cmdstat && *csb->csb_cmd != FO_SDRV) {
2279*0Sstevel@tonic-gate 		/* if it can restarted OK, then do so, else return error */
2280*0Sstevel@tonic-gate 		if (fdrecover(fcp)) {
2281*0Sstevel@tonic-gate 			mutex_exit(&fcp->c_lock);
2282*0Sstevel@tonic-gate 			return (EIO);
2283*0Sstevel@tonic-gate 		}
2284*0Sstevel@tonic-gate 		/* ASSUMES that cmd is still intact in csb */
2285*0Sstevel@tonic-gate 		if (csb->csb_xstate == FXS_END)
2286*0Sstevel@tonic-gate 			csb->csb_xstate = FXS_START;
2287*0Sstevel@tonic-gate 		if (fdc_dma_attr.dma_attr_sgllen > 1 && csb->csb_dmahandle) {
2288*0Sstevel@tonic-gate 			/*
2289*0Sstevel@tonic-gate 			 * restarted read/write operation requires
2290*0Sstevel@tonic-gate 			 * first DMA cookie of current window
2291*0Sstevel@tonic-gate 			 */
2292*0Sstevel@tonic-gate 			if (ddi_dma_getwin(csb->csb_dmahandle,
2293*0Sstevel@tonic-gate 			    csb->csb_dmacurrwin, &off, &len,
2294*0Sstevel@tonic-gate 			    &csb->csb_dmacookie,
2295*0Sstevel@tonic-gate 			    &csb->csb_dmacookiecnt) != DDI_SUCCESS) {
2296*0Sstevel@tonic-gate 
2297*0Sstevel@tonic-gate 				mutex_exit(&fcp->c_lock);
2298*0Sstevel@tonic-gate 				return (EIO);
2299*0Sstevel@tonic-gate 			}
2300*0Sstevel@tonic-gate 			csb->csb_dmacurrcookie = 0;
2301*0Sstevel@tonic-gate 		}
2302*0Sstevel@tonic-gate 		goto retry;
2303*0Sstevel@tonic-gate 	}
2304*0Sstevel@tonic-gate 	/* things went ok */
2305*0Sstevel@tonic-gate 	mutex_exit(&fcp->c_lock);
2306*0Sstevel@tonic-gate 	return (0);
2307*0Sstevel@tonic-gate }
2308*0Sstevel@tonic-gate 
2309*0Sstevel@tonic-gate /*
2310*0Sstevel@tonic-gate  * fdcheckdisk
2311*0Sstevel@tonic-gate  *	called by fdc_exec to check if the disk is still there - do a seek
2312*0Sstevel@tonic-gate  *	then see if DSKCHG line went away; if so, diskette is in; else
2313*0Sstevel@tonic-gate  *	it's (still) out.
2314*0Sstevel@tonic-gate  */
2315*0Sstevel@tonic-gate int
2316*0Sstevel@tonic-gate fdcheckdisk(struct fdcntlr *fcp, int unit)
2317*0Sstevel@tonic-gate {
2318*0Sstevel@tonic-gate 	struct fdcsb *csb = &fcp->c_csb;
2319*0Sstevel@tonic-gate 	int newcyl;			/* where to seek for reset of DSKCHG */
2320*0Sstevel@tonic-gate 	int rval;
2321*0Sstevel@tonic-gate 	enum fxstate save_xstate;
2322*0Sstevel@tonic-gate 	uchar_t save_cmd, save_cd1, save_npcyl;
2323*0Sstevel@tonic-gate 
2324*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fcp->c_lock));
2325*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L1, FDEM_CHEK,
2326*0Sstevel@tonic-gate 	    (CE_CONT, "fdcheckdisk unit %d\n", unit));
2327*0Sstevel@tonic-gate 
2328*0Sstevel@tonic-gate 	if (fcp->c_curpcyl[unit])
2329*0Sstevel@tonic-gate 		newcyl = fcp->c_curpcyl[unit] - 1;
2330*0Sstevel@tonic-gate 	else
2331*0Sstevel@tonic-gate 		newcyl = 1;
2332*0Sstevel@tonic-gate 
2333*0Sstevel@tonic-gate 	save_cmd = *csb->csb_cmd;
2334*0Sstevel@tonic-gate 	save_cd1 = csb->csb_cmd[1];
2335*0Sstevel@tonic-gate 	save_npcyl = csb->csb_npcyl;
2336*0Sstevel@tonic-gate 	save_xstate = csb->csb_xstate;
2337*0Sstevel@tonic-gate 
2338*0Sstevel@tonic-gate 	*csb->csb_cmd = FO_SEEK;
2339*0Sstevel@tonic-gate 	csb->csb_cmd[1] = (uchar_t)unit;
2340*0Sstevel@tonic-gate 	csb->csb_npcyl = (uchar_t)newcyl;
2341*0Sstevel@tonic-gate 	fcp->c_flags |= FCFLG_WAITING;
2342*0Sstevel@tonic-gate 
2343*0Sstevel@tonic-gate 	if (fcp->c_mtrstate[unit] != FMS_ON && fcp->c_motort[unit] != 0)
2344*0Sstevel@tonic-gate 		/*
2345*0Sstevel@tonic-gate 		 * wait for motor to get up to speed,
2346*0Sstevel@tonic-gate 		 * and let motor_timer issue seek cmd
2347*0Sstevel@tonic-gate 		 */
2348*0Sstevel@tonic-gate 		csb->csb_xstate = FXS_DKCHGX;
2349*0Sstevel@tonic-gate 	else {
2350*0Sstevel@tonic-gate 		/*
2351*0Sstevel@tonic-gate 		 * motor is up to speed; issue seek cmd now
2352*0Sstevel@tonic-gate 		 */
2353*0Sstevel@tonic-gate 		csb->csb_xstate = FXS_SEEK;
2354*0Sstevel@tonic-gate 		if (rval = fdcseek(fcp, unit, newcyl)) {
2355*0Sstevel@tonic-gate 			/*
2356*0Sstevel@tonic-gate 			 * any recal/seek errors are too serious to attend to
2357*0Sstevel@tonic-gate 			 */
2358*0Sstevel@tonic-gate 			FCERRPRINT(FDEP_L3, FDEM_CHEK,
2359*0Sstevel@tonic-gate 			    (CE_WARN, "fdcheckdisk err %d", rval));
2360*0Sstevel@tonic-gate 			fcp->c_flags ^= FCFLG_WAITING;
2361*0Sstevel@tonic-gate 		}
2362*0Sstevel@tonic-gate 	}
2363*0Sstevel@tonic-gate 	/*
2364*0Sstevel@tonic-gate 	 * wait for completion interrupt
2365*0Sstevel@tonic-gate 	 * XXX This should be backed up with a watchdog timer!
2366*0Sstevel@tonic-gate 	 */
2367*0Sstevel@tonic-gate 	while (fcp->c_flags & FCFLG_WAITING) {
2368*0Sstevel@tonic-gate 		cv_wait(&fcp->c_iocv, &fcp->c_lock);
2369*0Sstevel@tonic-gate 	}
2370*0Sstevel@tonic-gate 
2371*0Sstevel@tonic-gate 	/*
2372*0Sstevel@tonic-gate 	 * if disk change still asserted, no diskette in drive!
2373*0Sstevel@tonic-gate 	 */
2374*0Sstevel@tonic-gate 	if (rval = fdcsense_chng(fcp, unit)) {
2375*0Sstevel@tonic-gate 		FCERRPRINT(FDEP_L3, FDEM_CHEK,
2376*0Sstevel@tonic-gate 		    (CE_WARN, "fdcheckdisk no disk %d", unit));
2377*0Sstevel@tonic-gate 	}
2378*0Sstevel@tonic-gate 
2379*0Sstevel@tonic-gate 	*csb->csb_cmd = save_cmd;
2380*0Sstevel@tonic-gate 	csb->csb_cmd[1] = save_cd1;
2381*0Sstevel@tonic-gate 	csb->csb_npcyl = save_npcyl;
2382*0Sstevel@tonic-gate 	csb->csb_xstate = save_xstate;
2383*0Sstevel@tonic-gate 	return (rval);
2384*0Sstevel@tonic-gate }
2385*0Sstevel@tonic-gate 
2386*0Sstevel@tonic-gate static int
2387*0Sstevel@tonic-gate fdrecover(struct fdcntlr *fcp)
2388*0Sstevel@tonic-gate {
2389*0Sstevel@tonic-gate 	struct fcu_obj *fjp;
2390*0Sstevel@tonic-gate 	struct fdcsb *csb = &fcp->c_csb;
2391*0Sstevel@tonic-gate 	int residual;
2392*0Sstevel@tonic-gate 	int unit;
2393*0Sstevel@tonic-gate 	char *failure;
2394*0Sstevel@tonic-gate 
2395*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L2, FDEM_RECO,
2396*0Sstevel@tonic-gate 	    (CE_NOTE, "fdrecover unit %d", csb->csb_drive));
2397*0Sstevel@tonic-gate 
2398*0Sstevel@tonic-gate 	unit = csb->csb_drive;
2399*0Sstevel@tonic-gate 	fjp = fcp->c_unit[unit];
2400*0Sstevel@tonic-gate 	if (fcp->c_flags & FCFLG_TIMEOUT) {
2401*0Sstevel@tonic-gate 		fcp->c_flags ^= FCFLG_TIMEOUT;
2402*0Sstevel@tonic-gate 		csb->csb_rslt[1] |= 0x08;
2403*0Sstevel@tonic-gate 		FCERRPRINT(FDEP_L3, FDEM_RECO,
2404*0Sstevel@tonic-gate 		    (CE_WARN, "fd unit %d: %s timed out", csb->csb_drive,
2405*0Sstevel@tonic-gate 		    fdcmds[*csb->csb_cmd & 0x1f].cmdname));
2406*0Sstevel@tonic-gate 	}
2407*0Sstevel@tonic-gate 
2408*0Sstevel@tonic-gate 	if (csb->csb_status & S0_SEKEND)
2409*0Sstevel@tonic-gate 		fcp->c_curpcyl[unit] = -1;
2410*0Sstevel@tonic-gate 
2411*0Sstevel@tonic-gate 	switch (csb->csb_oldxs) {
2412*0Sstevel@tonic-gate 	case FXS_RCAL:		/* recalibrate */
2413*0Sstevel@tonic-gate 	case FXS_SEEK:		/* seek */
2414*0Sstevel@tonic-gate 	case FXS_RESET:		/* cntlr reset */
2415*0Sstevel@tonic-gate 		FCERRPRINT(FDEP_L4, FDEM_RECO, (CE_WARN,
2416*0Sstevel@tonic-gate 		    "fd unit %d: %s error: st0=0x%x pcn=%d", csb->csb_drive,
2417*0Sstevel@tonic-gate 		    fdcmds[*csb->csb_cmd & 0x1f].cmdname,
2418*0Sstevel@tonic-gate 		    *csb->csb_rslt, csb->csb_rslt[1]));
2419*0Sstevel@tonic-gate 		if (csb->csb_retrys++ < skretry &&
2420*0Sstevel@tonic-gate 		    !(csb->csb_opflags & CSB_OFRAWIOCTL))
2421*0Sstevel@tonic-gate 			return (0);
2422*0Sstevel@tonic-gate 		break;
2423*0Sstevel@tonic-gate 
2424*0Sstevel@tonic-gate 	case FXS_RDID:		/* read ID */
2425*0Sstevel@tonic-gate 		if (!(csb->csb_status & S0_SEKEND))
2426*0Sstevel@tonic-gate 			csb->csb_xstate = FXS_HDST;
2427*0Sstevel@tonic-gate 		/* FALLTHROUGH */
2428*0Sstevel@tonic-gate 	case FXS_DOIT:		/* original operation */
2429*0Sstevel@tonic-gate 	case FXS_DOWT:		/* waiting on operation */
2430*0Sstevel@tonic-gate 		if (csb->csb_opflags & (CSB_OFDMARD | CSB_OFDMAWT)) {
2431*0Sstevel@tonic-gate 			if (ddi_dmae_getcnt(fcp->c_dip, fcp->c_dmachan,
2432*0Sstevel@tonic-gate 			    &residual) != DDI_SUCCESS)
2433*0Sstevel@tonic-gate 				cmn_err(CE_WARN,
2434*0Sstevel@tonic-gate 				    "fdc_recover: dmae getcnt failed, "
2435*0Sstevel@tonic-gate 				    "dip %p dmachan %x residual %x\n",
2436*0Sstevel@tonic-gate 				    (void*)fcp->c_dip, fcp->c_dmachan,
2437*0Sstevel@tonic-gate 				    residual);
2438*0Sstevel@tonic-gate 			FCERRPRINT(FDEP_L2, FDEM_RECO,
2439*0Sstevel@tonic-gate 	(CE_NOTE, "fd unit %d: %s error: dma count=0x%lx residual=0x%x",
2440*0Sstevel@tonic-gate 			    csb->csb_drive,
2441*0Sstevel@tonic-gate 			    fdcmds[*csb->csb_cmd & 0x1f].cmdname,
2442*0Sstevel@tonic-gate 			    csb->csb_dmacookie.dmac_size, residual));
2443*0Sstevel@tonic-gate 		}
2444*0Sstevel@tonic-gate 		if (csb->csb_rslt[1] == S1_OVRUN)
2445*0Sstevel@tonic-gate 			/*
2446*0Sstevel@tonic-gate 			 * handle retries of over/underrun
2447*0Sstevel@tonic-gate 			 * with a secondary retry counter
2448*0Sstevel@tonic-gate 			 */
2449*0Sstevel@tonic-gate 			if (++csb->csb_ourtrys <= OURUN_TRIES) {
2450*0Sstevel@tonic-gate 				FCERRPRINT(FDEP_L2, FDEM_RECO,
2451*0Sstevel@tonic-gate (CE_NOTE, "fd unit %d: %s error: over/under-run",
2452*0Sstevel@tonic-gate csb->csb_drive, fdcmds[*csb->csb_cmd & 0x1f].cmdname));
2453*0Sstevel@tonic-gate 				return (0);
2454*0Sstevel@tonic-gate 			} else
2455*0Sstevel@tonic-gate 				/*
2456*0Sstevel@tonic-gate 				 * count 1 set of over/underruns
2457*0Sstevel@tonic-gate 				 * as 1 primary retry effort
2458*0Sstevel@tonic-gate 				 */
2459*0Sstevel@tonic-gate 				csb->csb_ourtrys = 0;
2460*0Sstevel@tonic-gate 
2461*0Sstevel@tonic-gate 		if ((fjp->fj_flags & (FUNIT_UNLABELED | FUNIT_LABELOK)) &&
2462*0Sstevel@tonic-gate 		    !(csb->csb_opflags & CSB_OFRAWIOCTL)) {
2463*0Sstevel@tonic-gate 			/*
2464*0Sstevel@tonic-gate 			 * device is open so keep trying and
2465*0Sstevel@tonic-gate 			 * gather statistics on errors
2466*0Sstevel@tonic-gate 			 */
2467*0Sstevel@tonic-gate 			if (csb->csb_rslt[1] & S1_CRCER)
2468*0Sstevel@tonic-gate 				fcp->fdstats.de++;
2469*0Sstevel@tonic-gate 			if (csb->csb_rslt[1] & S1_OVRUN)
2470*0Sstevel@tonic-gate 				fcp->fdstats.run++;
2471*0Sstevel@tonic-gate 			if (csb->csb_rslt[1] & (S1_NODATA | S1_MADMK))
2472*0Sstevel@tonic-gate 				fcp->fdstats.bfmt++;
2473*0Sstevel@tonic-gate 			if (csb->csb_rslt[1] & 0x08)
2474*0Sstevel@tonic-gate 				fcp->fdstats.to++;
2475*0Sstevel@tonic-gate 
2476*0Sstevel@tonic-gate 			/*
2477*0Sstevel@tonic-gate 			 * if we have not run out of retries, return 0
2478*0Sstevel@tonic-gate 			 */
2479*0Sstevel@tonic-gate 			if (csb->csb_retrys++ < csb->csb_maxretry &&
2480*0Sstevel@tonic-gate 			    (*csb->csb_cmd & ~FO_MFM) != FO_FRMT) {
2481*0Sstevel@tonic-gate 				if (csb->csb_opflags &
2482*0Sstevel@tonic-gate 				    (CSB_OFDMARD | CSB_OFDMAWT)) {
2483*0Sstevel@tonic-gate 					FCERRPRINT(FDEP_L4, FDEM_RECO,
2484*0Sstevel@tonic-gate (CE_WARN, "fd unit %d: %s error: st0=0x%x st1=0x%x st2=0x%x",
2485*0Sstevel@tonic-gate 					    csb->csb_drive,
2486*0Sstevel@tonic-gate fdcmds[*csb->csb_cmd & 0x1f].cmdname,
2487*0Sstevel@tonic-gate 					    *csb->csb_rslt, csb->csb_rslt[1],
2488*0Sstevel@tonic-gate 					    csb->csb_rslt[2]));
2489*0Sstevel@tonic-gate 				}
2490*0Sstevel@tonic-gate 				if ((csb->csb_retrys & 1) &&
2491*0Sstevel@tonic-gate 				    csb->csb_xstate == FXS_END)
2492*0Sstevel@tonic-gate 					csb->csb_xstate = FXS_DOIT;
2493*0Sstevel@tonic-gate 				else if (csb->csb_retrys == 3)
2494*0Sstevel@tonic-gate 					csb->csb_xstate = FXS_RESTART;
2495*0Sstevel@tonic-gate 				return (0);
2496*0Sstevel@tonic-gate 			}
2497*0Sstevel@tonic-gate 			if (csb->csb_rslt[1] & S1_CRCER)
2498*0Sstevel@tonic-gate 				failure = "crc error";
2499*0Sstevel@tonic-gate 			else if (csb->csb_rslt[1] & S1_OVRUN)
2500*0Sstevel@tonic-gate 				failure = "over/under-run";
2501*0Sstevel@tonic-gate 			else if (csb->csb_rslt[1] & (S1_NODATA | S1_MADMK))
2502*0Sstevel@tonic-gate 				failure = "bad format";
2503*0Sstevel@tonic-gate 			else if (csb->csb_rslt[1] & 0x08)
2504*0Sstevel@tonic-gate 				failure = "timeout";
2505*0Sstevel@tonic-gate 			else
2506*0Sstevel@tonic-gate 				failure = "failed";
2507*0Sstevel@tonic-gate 			cmn_err(CE_NOTE, "!fd unit %d: %s %s (%x %x %x)",
2508*0Sstevel@tonic-gate 			    csb->csb_drive,
2509*0Sstevel@tonic-gate 			    fdcmds[*csb->csb_cmd & 0x1f].cmdname, failure,
2510*0Sstevel@tonic-gate 			    *csb->csb_rslt, csb->csb_rslt[1], csb->csb_rslt[2]);
2511*0Sstevel@tonic-gate 		} else {
2512*0Sstevel@tonic-gate 			FCERRPRINT(FDEP_L2, FDEM_RECO,
2513*0Sstevel@tonic-gate 			    (CE_NOTE, "fd unit %d: %s failed (%x %x %x)",
2514*0Sstevel@tonic-gate 			    csb->csb_drive,
2515*0Sstevel@tonic-gate 			    fdcmds[*csb->csb_cmd & 0x1f].cmdname,
2516*0Sstevel@tonic-gate 			    *csb->csb_rslt, csb->csb_rslt[1],
2517*0Sstevel@tonic-gate 			    csb->csb_rslt[2]));
2518*0Sstevel@tonic-gate 		}
2519*0Sstevel@tonic-gate 		break;
2520*0Sstevel@tonic-gate 
2521*0Sstevel@tonic-gate 	default:
2522*0Sstevel@tonic-gate 		FCERRPRINT(FDEP_L4, FDEM_RECO, (CE_WARN,
2523*0Sstevel@tonic-gate 		    "fd unit %d: %s failed: st0=0x%x st1=0x%x st2=0x%x",
2524*0Sstevel@tonic-gate 		    csb->csb_drive, fdcmds[*csb->csb_cmd & 0x1f].cmdname,
2525*0Sstevel@tonic-gate 		    *csb->csb_rslt, csb->csb_rslt[1], csb->csb_rslt[2]));
2526*0Sstevel@tonic-gate 		break;
2527*0Sstevel@tonic-gate 	}
2528*0Sstevel@tonic-gate 	return (1);
2529*0Sstevel@tonic-gate }
2530*0Sstevel@tonic-gate 
2531*0Sstevel@tonic-gate 
2532*0Sstevel@tonic-gate /*	Autovector Interrupt Entry Point	*/
2533*0Sstevel@tonic-gate /* ARGSUSED */
2534*0Sstevel@tonic-gate static uint_t
2535*0Sstevel@tonic-gate fdc_intr(caddr_t arg)
2536*0Sstevel@tonic-gate {
2537*0Sstevel@tonic-gate 	struct fdcntlr *fcp = (struct fdcntlr *)arg;
2538*0Sstevel@tonic-gate 	struct fdcsb *csb;
2539*0Sstevel@tonic-gate 	off_t off;
2540*0Sstevel@tonic-gate 	size_t blklen;
2541*0Sstevel@tonic-gate 	int drive;
2542*0Sstevel@tonic-gate 	int newstate;
2543*0Sstevel@tonic-gate 	int pendstate;
2544*0Sstevel@tonic-gate 	int rval = DDI_DMA_DONE;
2545*0Sstevel@tonic-gate 	int state;
2546*0Sstevel@tonic-gate 	int maxspin = 10;
2547*0Sstevel@tonic-gate 
2548*0Sstevel@tonic-gate 	csb = &fcp->c_csb;
2549*0Sstevel@tonic-gate 
2550*0Sstevel@tonic-gate 	mutex_enter(&fcp->c_lock);
2551*0Sstevel@tonic-gate 	/*
2552*0Sstevel@tonic-gate 	 * Wait for the RQM bit to be set, or until we've tested it
2553*0Sstevel@tonic-gate 	 * a bunch of times (which may imply this isn't our interrupt).
2554*0Sstevel@tonic-gate 	 */
2555*0Sstevel@tonic-gate 	state = inb(fcp->c_regbase + FCR_MSR);
2556*0Sstevel@tonic-gate 	pendstate = state & (MS_RQM | MS_DIO | MS_CB);
2557*0Sstevel@tonic-gate 	while (((pendstate & MS_RQM) == 0) && (maxspin-- > 0)) {
2558*0Sstevel@tonic-gate 		/* Small pause in between reading the status port */
2559*0Sstevel@tonic-gate 		drv_usecwait(10);
2560*0Sstevel@tonic-gate 		/* Reread the status port */
2561*0Sstevel@tonic-gate 		state = inb(fcp->c_regbase + FCR_MSR);
2562*0Sstevel@tonic-gate 		pendstate = state & (MS_RQM | MS_DIO | MS_CB);
2563*0Sstevel@tonic-gate 	}
2564*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L0, FDEM_INTR,
2565*0Sstevel@tonic-gate 	    (CE_CONT, "fdc_intr unit %d: xstate=%d MSR=0x%x\n",
2566*0Sstevel@tonic-gate 	    csb->csb_drive, csb->csb_xstate, state));
2567*0Sstevel@tonic-gate 
2568*0Sstevel@tonic-gate 	/*
2569*0Sstevel@tonic-gate 	 * If there is an operation outstanding AND the controller is ready
2570*0Sstevel@tonic-gate 	 * to receive a command or send us the result of a command (OR if the
2571*0Sstevel@tonic-gate 	 * controller is ready to accept a new command), AND if
2572*0Sstevel@tonic-gate 	 * someone has been waiting for a command to finish AND (if no unit
2573*0Sstevel@tonic-gate 	 * is BUSY OR if the unit that we're waiting for is BUSY (i.e. it's in
2574*0Sstevel@tonic-gate 	 * the middle of a seek/recalibrate)) then this interrupt is for us.
2575*0Sstevel@tonic-gate 	 */
2576*0Sstevel@tonic-gate 	if ((pendstate == (MS_RQM | MS_DIO | MS_CB) || pendstate == MS_RQM) &&
2577*0Sstevel@tonic-gate 	    (fcp->c_flags & FCFLG_WAITING) &&
2578*0Sstevel@tonic-gate 	    (!(state & 0x0f) || ((1 << csb->csb_drive) & state))) {
2579*0Sstevel@tonic-gate 		/*
2580*0Sstevel@tonic-gate 		 * Remove one of the conditions for entering this code.
2581*0Sstevel@tonic-gate 		 * The state_machine will release the c_lock if it
2582*0Sstevel@tonic-gate 		 * calls untimeout()
2583*0Sstevel@tonic-gate 		 */
2584*0Sstevel@tonic-gate 		fcp->c_flags ^= FCFLG_WAITING;
2585*0Sstevel@tonic-gate 
2586*0Sstevel@tonic-gate 		if ((newstate = fdc_statemach(fcp)) == -1) {
2587*0Sstevel@tonic-gate 			/* restore waiting flag */
2588*0Sstevel@tonic-gate 			fcp->c_flags |= FCFLG_WAITING;
2589*0Sstevel@tonic-gate 			mutex_exit(&fcp->c_lock);
2590*0Sstevel@tonic-gate 			return (DDI_INTR_CLAIMED);
2591*0Sstevel@tonic-gate 		}
2592*0Sstevel@tonic-gate 
2593*0Sstevel@tonic-gate 		if (fcp->c_intrstat)
2594*0Sstevel@tonic-gate 			KIOIP->intrs[KSTAT_INTR_HARD]++;
2595*0Sstevel@tonic-gate 		if (newstate == FXS_END) {
2596*0Sstevel@tonic-gate 
2597*0Sstevel@tonic-gate 			if (csb->csb_dmahandle && !csb->csb_cmdstat &&
2598*0Sstevel@tonic-gate 				/*
2599*0Sstevel@tonic-gate 				 * read/write operation may have multiple DMA
2600*0Sstevel@tonic-gate 				 * cookies: process next one
2601*0Sstevel@tonic-gate 				 */
2602*0Sstevel@tonic-gate 			    ((csb->csb_dmacurrcookie <
2603*0Sstevel@tonic-gate 			    (csb->csb_dmacookiecnt - 1)) ||
2604*0Sstevel@tonic-gate 			    (csb->csb_dmacurrwin) < (csb->csb_dmawincnt - 1))) {
2605*0Sstevel@tonic-gate 				/*
2606*0Sstevel@tonic-gate 				 * read/write operation requires another
2607*0Sstevel@tonic-gate 				 * DMA cookie: process next one
2608*0Sstevel@tonic-gate 				 */
2609*0Sstevel@tonic-gate 
2610*0Sstevel@tonic-gate 				if (++csb->csb_dmacurrcookie <
2611*0Sstevel@tonic-gate 				    csb->csb_dmacookiecnt) {
2612*0Sstevel@tonic-gate 					ddi_dma_nextcookie(csb->csb_dmahandle,
2613*0Sstevel@tonic-gate 					    &csb->csb_dmacookie);
2614*0Sstevel@tonic-gate 				} else if (++csb->csb_dmacurrwin <
2615*0Sstevel@tonic-gate 				    csb->csb_dmawincnt) {
2616*0Sstevel@tonic-gate 					if (ddi_dma_getwin(csb->csb_dmahandle,
2617*0Sstevel@tonic-gate 					    csb->csb_dmacurrwin, &off, &blklen,
2618*0Sstevel@tonic-gate 					    &csb->csb_dmacookie,
2619*0Sstevel@tonic-gate 					    &csb->csb_dmacookiecnt) !=
2620*0Sstevel@tonic-gate 					    DDI_SUCCESS) {
2621*0Sstevel@tonic-gate 						cmn_err(CE_WARN,
2622*0Sstevel@tonic-gate 						    "fdc_intr: "
2623*0Sstevel@tonic-gate 						    "dma getwin failed\n");
2624*0Sstevel@tonic-gate 					}
2625*0Sstevel@tonic-gate 					csb->csb_dmacurrcookie = 0;
2626*0Sstevel@tonic-gate 				}
2627*0Sstevel@tonic-gate 
2628*0Sstevel@tonic-gate 				if (ddi_dmae_prog(fcp->c_dip, NULL,
2629*0Sstevel@tonic-gate 				    &csb->csb_dmacookie, fcp->c_dmachan) !=
2630*0Sstevel@tonic-gate 				    DDI_SUCCESS)
2631*0Sstevel@tonic-gate 					cmn_err(CE_WARN,
2632*0Sstevel@tonic-gate 					    "fdc_intr: dmae prog failed, "
2633*0Sstevel@tonic-gate 					    "dip %p dmachannel %x\n",
2634*0Sstevel@tonic-gate 					    (void*)fcp->c_dip,
2635*0Sstevel@tonic-gate 					    fcp->c_dmachan);
2636*0Sstevel@tonic-gate 
2637*0Sstevel@tonic-gate 				/*
2638*0Sstevel@tonic-gate 				 * status of last operation has disk
2639*0Sstevel@tonic-gate 				 * address for continuation
2640*0Sstevel@tonic-gate 				 */
2641*0Sstevel@tonic-gate 				csb->csb_cmd[2] = csb->csb_rslt[3];
2642*0Sstevel@tonic-gate 				csb->csb_cmd[3] = csb->csb_rslt[4];
2643*0Sstevel@tonic-gate 				csb->csb_cmd[4] = csb->csb_rslt[5];
2644*0Sstevel@tonic-gate 				csb->csb_cmd[1] = (csb->csb_cmd[1] & ~0x04) |
2645*0Sstevel@tonic-gate 				    (csb->csb_cmd[3] << 2);
2646*0Sstevel@tonic-gate 
2647*0Sstevel@tonic-gate 				csb->csb_xstate = FXS_START;
2648*0Sstevel@tonic-gate 				(void) fdc_statemach(fcp);
2649*0Sstevel@tonic-gate 				/*
2650*0Sstevel@tonic-gate 				 * Ignored return.  If failed, warning already
2651*0Sstevel@tonic-gate 				 * posted.  Returned state irrelevant.
2652*0Sstevel@tonic-gate 				 */
2653*0Sstevel@tonic-gate 				/* restore waiting flag */
2654*0Sstevel@tonic-gate 				fcp->c_flags |= FCFLG_WAITING;
2655*0Sstevel@tonic-gate 				goto fi_exit;
2656*0Sstevel@tonic-gate 			}
2657*0Sstevel@tonic-gate 			if (rval != DDI_DMA_DONE)
2658*0Sstevel@tonic-gate 				csb->csb_cmdstat = EIO;
2659*0Sstevel@tonic-gate 			/*
2660*0Sstevel@tonic-gate 			 * somebody's waiting for completion of fdcntlr/csb,
2661*0Sstevel@tonic-gate 			 * wake them
2662*0Sstevel@tonic-gate 			 */
2663*0Sstevel@tonic-gate 			cv_signal(&fcp->c_iocv);
2664*0Sstevel@tonic-gate 		}
2665*0Sstevel@tonic-gate 		else
2666*0Sstevel@tonic-gate 			/* restore waiting flag */
2667*0Sstevel@tonic-gate 			fcp->c_flags |= FCFLG_WAITING;
2668*0Sstevel@tonic-gate fi_exit:
2669*0Sstevel@tonic-gate 		mutex_exit(&fcp->c_lock);
2670*0Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
2671*0Sstevel@tonic-gate 	}
2672*0Sstevel@tonic-gate 
2673*0Sstevel@tonic-gate 	if (state & MS_RQM) {
2674*0Sstevel@tonic-gate 		(void) fdcsense_int(fcp, &drive, NULL);
2675*0Sstevel@tonic-gate 		/*
2676*0Sstevel@tonic-gate 		 * Ignored return - senser state already saved
2677*0Sstevel@tonic-gate 		 */
2678*0Sstevel@tonic-gate 		FCERRPRINT(FDEP_L4, FDEM_INTR,
2679*0Sstevel@tonic-gate 		    (CE_WARN, "fdc_intr unit %d: nobody sleeping 0x%x",
2680*0Sstevel@tonic-gate 		    drive, state));
2681*0Sstevel@tonic-gate 	} else {
2682*0Sstevel@tonic-gate 		FCERRPRINT(FDEP_L4, FDEM_INTR,
2683*0Sstevel@tonic-gate 		    (CE_WARN, "fdc_intr: nobody sleeping on %d 0x%x",
2684*0Sstevel@tonic-gate 		    csb->csb_drive, state));
2685*0Sstevel@tonic-gate 	}
2686*0Sstevel@tonic-gate 	/*
2687*0Sstevel@tonic-gate 	 * This should probably be protected, but, what the
2688*0Sstevel@tonic-gate 	 * heck...the cost isn't worth the accuracy for this
2689*0Sstevel@tonic-gate 	 * statistic.
2690*0Sstevel@tonic-gate 	 */
2691*0Sstevel@tonic-gate 	if (fcp->c_intrstat)
2692*0Sstevel@tonic-gate 		KIOIP->intrs[KSTAT_INTR_SPURIOUS]++;
2693*0Sstevel@tonic-gate 	mutex_exit(&fcp->c_lock);
2694*0Sstevel@tonic-gate 	return (DDI_INTR_UNCLAIMED);
2695*0Sstevel@tonic-gate }
2696*0Sstevel@tonic-gate 
2697*0Sstevel@tonic-gate /*
2698*0Sstevel@tonic-gate  * fdwatch
2699*0Sstevel@tonic-gate  *	is called from timeout() when a floppy operation timer has expired.
2700*0Sstevel@tonic-gate  */
2701*0Sstevel@tonic-gate static void
2702*0Sstevel@tonic-gate fdwatch(void *arg)
2703*0Sstevel@tonic-gate {
2704*0Sstevel@tonic-gate 	struct fdcntlr *fcp = (struct fdcntlr *)arg;
2705*0Sstevel@tonic-gate 	struct fdcsb *csb;
2706*0Sstevel@tonic-gate 
2707*0Sstevel@tonic-gate 	mutex_enter(&fcp->c_lock);
2708*0Sstevel@tonic-gate 
2709*0Sstevel@tonic-gate 	if (fcp->c_timeid == 0) {
2710*0Sstevel@tonic-gate 		/*
2711*0Sstevel@tonic-gate 		 * fdc_intr got here first, ergo, no timeout condition..
2712*0Sstevel@tonic-gate 		 */
2713*0Sstevel@tonic-gate 		mutex_exit(&fcp->c_lock);
2714*0Sstevel@tonic-gate 		return;
2715*0Sstevel@tonic-gate 	}
2716*0Sstevel@tonic-gate 
2717*0Sstevel@tonic-gate 	if (fcp->c_flags & FCFLG_WAITING) {
2718*0Sstevel@tonic-gate 		if (ddi_dmae_stop(fcp->c_dip, fcp->c_dmachan) != DDI_SUCCESS)
2719*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdwatch: dmae stop failed, "
2720*0Sstevel@tonic-gate 				"dip %p, dmachan %x\n",
2721*0Sstevel@tonic-gate 				(void*)fcp->c_dip, fcp->c_dmachan);
2722*0Sstevel@tonic-gate 		csb = &fcp->c_csb;
2723*0Sstevel@tonic-gate 		FCERRPRINT(FDEP_L3, FDEM_WATC,
2724*0Sstevel@tonic-gate 		    (CE_WARN, "fdcwatch unit %d: xstate = %d",
2725*0Sstevel@tonic-gate 		    csb->csb_drive, csb->csb_xstate));
2726*0Sstevel@tonic-gate 		drv_usecwait(50);
2727*0Sstevel@tonic-gate 
2728*0Sstevel@tonic-gate 		if (inb(fcp->c_regbase + FCR_MSR) != MS_RQM) {
2729*0Sstevel@tonic-gate 			/*
2730*0Sstevel@tonic-gate 			 * cntlr is still busy, so reset it
2731*0Sstevel@tonic-gate 			 */
2732*0Sstevel@tonic-gate 			csb->csb_xstate = FXS_KILL;
2733*0Sstevel@tonic-gate 			(void) fdc_statemach(fcp);
2734*0Sstevel@tonic-gate 			/*
2735*0Sstevel@tonic-gate 			 * Ignored return.  If failed, warning already
2736*0Sstevel@tonic-gate 			 * posted.  Returned state irrelevant.
2737*0Sstevel@tonic-gate 			 */
2738*0Sstevel@tonic-gate 		} else {
2739*0Sstevel@tonic-gate 			csb->csb_xstate = FXS_END;
2740*0Sstevel@tonic-gate 			fcp->c_timeid = 0;
2741*0Sstevel@tonic-gate 			fcp->c_flags ^= FCFLG_WAITING;
2742*0Sstevel@tonic-gate 			cv_signal(&fcp->c_iocv);
2743*0Sstevel@tonic-gate 		}
2744*0Sstevel@tonic-gate 		csb->csb_cmdstat = EIO;
2745*0Sstevel@tonic-gate 		fcp->c_flags |= FCFLG_TIMEOUT;
2746*0Sstevel@tonic-gate 	} else {
2747*0Sstevel@tonic-gate 		FCERRPRINT(FDEP_L4, FDEM_INTR,
2748*0Sstevel@tonic-gate 		    (CE_WARN, "fdcwatch: not sleeping for unit %d",
2749*0Sstevel@tonic-gate 		    fcp->c_csb.csb_drive));
2750*0Sstevel@tonic-gate 	}
2751*0Sstevel@tonic-gate 	if (fcp->c_intrstat)
2752*0Sstevel@tonic-gate 		KIOIP->intrs[KSTAT_INTR_WATCHDOG]++;
2753*0Sstevel@tonic-gate 	mutex_exit(&fcp->c_lock);
2754*0Sstevel@tonic-gate }
2755*0Sstevel@tonic-gate 
2756*0Sstevel@tonic-gate 
2757*0Sstevel@tonic-gate static int
2758*0Sstevel@tonic-gate fdc_statemach(struct fdcntlr *fcp)
2759*0Sstevel@tonic-gate {
2760*0Sstevel@tonic-gate 	struct fcu_obj *fjp;
2761*0Sstevel@tonic-gate 	struct fdcsb *csb = &fcp->c_csb;
2762*0Sstevel@tonic-gate 	int backoff;
2763*0Sstevel@tonic-gate 	clock_t time;
2764*0Sstevel@tonic-gate 	int unit;
2765*0Sstevel@tonic-gate 
2766*0Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fcp->c_lock));
2767*0Sstevel@tonic-gate 
2768*0Sstevel@tonic-gate 	unit = csb->csb_drive;
2769*0Sstevel@tonic-gate 	fjp = fcp->c_unit[unit];
2770*0Sstevel@tonic-gate 
2771*0Sstevel@tonic-gate 	csb->csb_oldxs = csb->csb_xstate;
2772*0Sstevel@tonic-gate 	switch (csb->csb_xstate) {
2773*0Sstevel@tonic-gate 
2774*0Sstevel@tonic-gate 	case FXS_START:		/* start of operation */
2775*0Sstevel@tonic-gate 		ASSERT(fcp->c_timeid == 0);
2776*0Sstevel@tonic-gate 		time = drv_usectohz(100000 * (unsigned int)csb->csb_timer);
2777*0Sstevel@tonic-gate 		if (time == 0)
2778*0Sstevel@tonic-gate 			time = drv_usectohz(2000000);
2779*0Sstevel@tonic-gate 		fcp->c_timeid = timeout(fdwatch, (void *)fcp, time);
2780*0Sstevel@tonic-gate 
2781*0Sstevel@tonic-gate 		if (fcp->c_mtrstate[unit] == FMS_START) {
2782*0Sstevel@tonic-gate 			/*
2783*0Sstevel@tonic-gate 			 * wait for motor to get up to speed
2784*0Sstevel@tonic-gate 			 */
2785*0Sstevel@tonic-gate 			csb->csb_xstate = FXS_MTRON;
2786*0Sstevel@tonic-gate 			break;
2787*0Sstevel@tonic-gate 		}
2788*0Sstevel@tonic-gate 		/* FALLTHROUGH */
2789*0Sstevel@tonic-gate 
2790*0Sstevel@tonic-gate 	case FXS_MTRON:		/* motor is at speed */
2791*0Sstevel@tonic-gate 		if (fcp->c_mtrstate[unit] != FMS_ON) {
2792*0Sstevel@tonic-gate 			/* how did we get here ?? */
2793*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdc: selected but motor off");
2794*0Sstevel@tonic-gate 			return (-1);
2795*0Sstevel@tonic-gate 		}
2796*0Sstevel@tonic-gate 		if (fcp->c_curpcyl[unit] != -1 && *csb->csb_cmd != FO_RECAL)
2797*0Sstevel@tonic-gate 			goto nxs_seek;
2798*0Sstevel@tonic-gate 		recalcmd[1] = (uchar_t)unit;
2799*0Sstevel@tonic-gate 		if (fdc_docmd(fcp, recalcmd, 2) == -1) {
2800*0Sstevel@tonic-gate 			/* cntlr did not accept command bytes */
2801*0Sstevel@tonic-gate 			fdcquiesce(fcp);
2802*0Sstevel@tonic-gate 			csb->csb_cmdstat = EIO;
2803*0Sstevel@tonic-gate 			csb->csb_xstate = FXS_RESET;
2804*0Sstevel@tonic-gate 			break;
2805*0Sstevel@tonic-gate 		}
2806*0Sstevel@tonic-gate 		fcp->c_sekdir[unit] = 0;
2807*0Sstevel@tonic-gate 		csb->csb_xstate = FXS_RCAL;
2808*0Sstevel@tonic-gate 		break;
2809*0Sstevel@tonic-gate 
2810*0Sstevel@tonic-gate 	case FXS_RCAL:		/* forced recalibrate is complete */
2811*0Sstevel@tonic-gate #if 0	/* #ifdef _VPIX */
2812*0Sstevel@tonic-gate 	/* WARNING: this code breaks SPARC compatibility */
2813*0Sstevel@tonic-gate 		if (csb->csb_opflags & CSB_OFRAWIOCTL &&
2814*0Sstevel@tonic-gate 		    *csb->csb_cmd == FO_RECAL) {
2815*0Sstevel@tonic-gate 			fcp->c_curpcyl[unit] = 0;
2816*0Sstevel@tonic-gate 			csb->csb_status = 0;
2817*0Sstevel@tonic-gate 			goto nxs_cmpl;
2818*0Sstevel@tonic-gate 		}
2819*0Sstevel@tonic-gate #endif
2820*0Sstevel@tonic-gate 		(void) fdc_docmd(fcp, &senseintcmd, 1);
2821*0Sstevel@tonic-gate 		/*
2822*0Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_docmd.
2823*0Sstevel@tonic-gate 		 * fdc_results retrieves the controller/drive status
2824*0Sstevel@tonic-gate 		 */
2825*0Sstevel@tonic-gate 		(void) fdc_result(fcp, csb->csb_rslt, 2);
2826*0Sstevel@tonic-gate 		/*
2827*0Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_result.
2828*0Sstevel@tonic-gate 		 * Actual results checked below
2829*0Sstevel@tonic-gate 		 */
2830*0Sstevel@tonic-gate 		if ((csb->csb_status = ((*csb->csb_rslt ^ S0_SEKEND) &
2831*0Sstevel@tonic-gate 		    (S0_ICMASK | S0_SEKEND | S0_ECHK | S0_NOTRDY))) != 0) {
2832*0Sstevel@tonic-gate 			FCERRPRINT(FDEP_L3, FDEM_EXEC,
2833*0Sstevel@tonic-gate 			    (CE_WARN, "fdc_statemach unit %d: recal result %x",
2834*0Sstevel@tonic-gate 			    csb->csb_drive, *csb->csb_rslt));
2835*0Sstevel@tonic-gate 			fdcquiesce(fcp);
2836*0Sstevel@tonic-gate 			csb->csb_cmdstat = EIO;
2837*0Sstevel@tonic-gate 			csb->csb_xstate = FXS_RESET;
2838*0Sstevel@tonic-gate 			break;
2839*0Sstevel@tonic-gate 		}
2840*0Sstevel@tonic-gate 		if (unit != (*csb->csb_rslt & 3) || csb->csb_rslt[1]) {
2841*0Sstevel@tonic-gate 			csb->csb_status = S0_SEKEND;
2842*0Sstevel@tonic-gate 			goto nxs_cmpl;
2843*0Sstevel@tonic-gate 		}
2844*0Sstevel@tonic-gate 		fcp->c_curpcyl[unit] = csb->csb_rslt[1];
2845*0Sstevel@tonic-gate 		if (*csb->csb_cmd == FO_RECAL)
2846*0Sstevel@tonic-gate 			goto nxs_cmpl;
2847*0Sstevel@tonic-gate nxs_seek:
2848*0Sstevel@tonic-gate 		if (*csb->csb_cmd != FO_SEEK &&
2849*0Sstevel@tonic-gate 		    csb->csb_npcyl == fcp->c_curpcyl[unit])
2850*0Sstevel@tonic-gate 			goto nxs_doit;
2851*0Sstevel@tonic-gate 		fcp->c_sekdir[unit] = csb->csb_npcyl - fcp->c_curpcyl[unit];
2852*0Sstevel@tonic-gate 		/* FALLTHROUGH */
2853*0Sstevel@tonic-gate 
2854*0Sstevel@tonic-gate 	case FXS_DKCHGX:	/* reset Disk-Change latch */
2855*0Sstevel@tonic-gate 		(void) fdcseek(fcp, csb->csb_cmd[1], csb->csb_npcyl);
2856*0Sstevel@tonic-gate 		/*
2857*0Sstevel@tonic-gate 		 * Ignored return.  If command rejected, warnig already posted
2858*0Sstevel@tonic-gate 		 * by fdc_docmd().
2859*0Sstevel@tonic-gate 		 */
2860*0Sstevel@tonic-gate 		csb->csb_xstate = FXS_SEEK;
2861*0Sstevel@tonic-gate 		break;
2862*0Sstevel@tonic-gate 
2863*0Sstevel@tonic-gate 	case FXS_RESTART:	/* special restart of read/write operation */
2864*0Sstevel@tonic-gate 		ASSERT(fcp->c_timeid == 0);
2865*0Sstevel@tonic-gate 		time = drv_usectohz(100000 * csb->csb_timer);
2866*0Sstevel@tonic-gate 		if (time == 0)
2867*0Sstevel@tonic-gate 			time = drv_usectohz(2000000);
2868*0Sstevel@tonic-gate 		fcp->c_timeid = timeout(fdwatch, (void *)fcp, time);
2869*0Sstevel@tonic-gate 
2870*0Sstevel@tonic-gate 		if (fcp->c_mtrstate[unit] != FMS_ON) {
2871*0Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdc: selected but motor off");
2872*0Sstevel@tonic-gate 			return (-1);
2873*0Sstevel@tonic-gate 		}
2874*0Sstevel@tonic-gate 		if ((csb->csb_npcyl == 0 || fcp->c_sekdir[unit] >= 0) &&
2875*0Sstevel@tonic-gate 		    (int)csb->csb_cmd[2] < (fjp->fj_chars->fdc_ncyl - 1))
2876*0Sstevel@tonic-gate 			backoff = csb->csb_npcyl + 1;
2877*0Sstevel@tonic-gate 		else
2878*0Sstevel@tonic-gate 			backoff = csb->csb_npcyl - 1;
2879*0Sstevel@tonic-gate 		(void) fdcseek(fcp, csb->csb_cmd[1], backoff);
2880*0Sstevel@tonic-gate 		/*
2881*0Sstevel@tonic-gate 		 * Ignored return.  If command rejected, warnig already posted
2882*0Sstevel@tonic-gate 		 * by fdc_docmd().
2883*0Sstevel@tonic-gate 		 */
2884*0Sstevel@tonic-gate 		csb->csb_xstate = FXS_RESEEK;
2885*0Sstevel@tonic-gate 		break;
2886*0Sstevel@tonic-gate 
2887*0Sstevel@tonic-gate 	case FXS_RESEEK:	/* seek to backoff-cyl complete */
2888*0Sstevel@tonic-gate 		(void) fdc_docmd(fcp, &senseintcmd, 1);
2889*0Sstevel@tonic-gate 		/*
2890*0Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_docmd.
2891*0Sstevel@tonic-gate 		 * fdc_results retrieves the controller/drive status
2892*0Sstevel@tonic-gate 		 */
2893*0Sstevel@tonic-gate 		(void) fdc_result(fcp, csb->csb_rslt, 2);
2894*0Sstevel@tonic-gate 		/*
2895*0Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_result.
2896*0Sstevel@tonic-gate 		 * Actual results checked below
2897*0Sstevel@tonic-gate 		 */
2898*0Sstevel@tonic-gate 		if ((csb->csb_status = ((*csb->csb_rslt ^ S0_SEKEND) &
2899*0Sstevel@tonic-gate 		    (S0_ICMASK | S0_SEKEND | S0_ECHK | S0_NOTRDY))) != 0)
2900*0Sstevel@tonic-gate 			goto nxs_cmpl;
2901*0Sstevel@tonic-gate 		(void) fdcseek(fcp, csb->csb_cmd[1], csb->csb_npcyl);
2902*0Sstevel@tonic-gate 		/*
2903*0Sstevel@tonic-gate 		 * Ignored return.  If command rejected, warnig already posted
2904*0Sstevel@tonic-gate 		 * by fdc_docmd().
2905*0Sstevel@tonic-gate 		 */
2906*0Sstevel@tonic-gate 		csb->csb_xstate = FXS_SEEK;
2907*0Sstevel@tonic-gate 		break;
2908*0Sstevel@tonic-gate 
2909*0Sstevel@tonic-gate 	case FXS_SEEK:		/* seek complete */
2910*0Sstevel@tonic-gate #if 0	/* #ifdef _VPIX */
2911*0Sstevel@tonic-gate 	/* WARNING: this code breaks SPARC compatibility and */
2912*0Sstevel@tonic-gate 	/* rawioctls in fdformat */
2913*0Sstevel@tonic-gate 		if (csb->csb_opflags & CSB_OFRAWIOCTL) {
2914*0Sstevel@tonic-gate 			fcp->c_curpcyl[unit] = csb->csb_npcyl;
2915*0Sstevel@tonic-gate 			csb->csb_status = 0;
2916*0Sstevel@tonic-gate 			goto nxs_cmpl;
2917*0Sstevel@tonic-gate 		}
2918*0Sstevel@tonic-gate #endif
2919*0Sstevel@tonic-gate 		(void) fdc_docmd(fcp, &senseintcmd, 1);
2920*0Sstevel@tonic-gate 		/*
2921*0Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_docmd.
2922*0Sstevel@tonic-gate 		 * fdc_results retrieves the controller/drive status
2923*0Sstevel@tonic-gate 		 */
2924*0Sstevel@tonic-gate 		(void) fdc_result(fcp, csb->csb_rslt, 2);
2925*0Sstevel@tonic-gate 		/*
2926*0Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_result.
2927*0Sstevel@tonic-gate 		 * Actual results checked below
2928*0Sstevel@tonic-gate 		 */
2929*0Sstevel@tonic-gate 		if ((csb->csb_status = ((*csb->csb_rslt ^ S0_SEKEND) &
2930*0Sstevel@tonic-gate 		    (S0_ICMASK | S0_SEKEND | S0_ECHK | S0_NOTRDY))) != 0)
2931*0Sstevel@tonic-gate 			goto nxs_cmpl;
2932*0Sstevel@tonic-gate 		if (unit != (*csb->csb_rslt & 3) ||
2933*0Sstevel@tonic-gate 		    csb->csb_rslt[1] != csb->csb_npcyl) {
2934*0Sstevel@tonic-gate 			csb->csb_status = S0_SEKEND;
2935*0Sstevel@tonic-gate 			goto nxs_cmpl;
2936*0Sstevel@tonic-gate 		};
2937*0Sstevel@tonic-gate 		fcp->c_curpcyl[unit] = csb->csb_rslt[1];
2938*0Sstevel@tonic-gate 		/* use motor_timer to delay for head settle */
2939*0Sstevel@tonic-gate 		mutex_enter(&fcp->c_dorlock);
2940*0Sstevel@tonic-gate 		(void) fdc_motorsm(fjp, FMI_DELAYCMD,
2941*0Sstevel@tonic-gate 		    fjp->fj_drive->fdd_headsettle / 1000);
2942*0Sstevel@tonic-gate 		/*
2943*0Sstevel@tonic-gate 		 * Return value ignored - fdcmotort deals with failure.
2944*0Sstevel@tonic-gate 		 */
2945*0Sstevel@tonic-gate 		mutex_exit(&fcp->c_dorlock);
2946*0Sstevel@tonic-gate 		csb->csb_xstate = FXS_HDST;
2947*0Sstevel@tonic-gate 		break;
2948*0Sstevel@tonic-gate 
2949*0Sstevel@tonic-gate 	case FXS_HDST:		/* head settle */
2950*0Sstevel@tonic-gate 		if (*csb->csb_cmd == FO_SEEK)
2951*0Sstevel@tonic-gate 			goto nxs_cmpl;
2952*0Sstevel@tonic-gate 		if ((*csb->csb_cmd & ~FO_MFM) == FO_FRMT)
2953*0Sstevel@tonic-gate 			goto nxs_doit;
2954*0Sstevel@tonic-gate 		fdcreadid(fcp, csb);
2955*0Sstevel@tonic-gate 		csb->csb_xstate = FXS_RDID;
2956*0Sstevel@tonic-gate 		break;
2957*0Sstevel@tonic-gate 
2958*0Sstevel@tonic-gate 	case FXS_RDID:		/* read ID complete */
2959*0Sstevel@tonic-gate 		(void) fdc_result(fcp, csb->csb_rslt, 7);
2960*0Sstevel@tonic-gate 		/*
2961*0Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_result.
2962*0Sstevel@tonic-gate 		 * Actual results checked below
2963*0Sstevel@tonic-gate 		 */
2964*0Sstevel@tonic-gate 		if ((csb->csb_status = (*csb->csb_rslt &
2965*0Sstevel@tonic-gate 		    (S0_ICMASK | S0_ECHK | S0_NOTRDY))) != 0)
2966*0Sstevel@tonic-gate 			goto nxs_cmpl;
2967*0Sstevel@tonic-gate 		if (csb->csb_cmd[2] != csb->csb_rslt[3]) {
2968*0Sstevel@tonic-gate 			/* at wrong logical cylinder */
2969*0Sstevel@tonic-gate 			csb->csb_status = S0_SEKEND;
2970*0Sstevel@tonic-gate 			goto nxs_cmpl;
2971*0Sstevel@tonic-gate 		};
2972*0Sstevel@tonic-gate 		goto nxs_doit;
2973*0Sstevel@tonic-gate 
2974*0Sstevel@tonic-gate 	case FXS_DOIT:		/* do original operation */
2975*0Sstevel@tonic-gate 		ASSERT(fcp->c_timeid == 0);
2976*0Sstevel@tonic-gate 		time = drv_usectohz(100000 * csb->csb_timer);
2977*0Sstevel@tonic-gate 		if (time == 0)
2978*0Sstevel@tonic-gate 			time = drv_usectohz(2000000);
2979*0Sstevel@tonic-gate 		fcp->c_timeid = timeout(fdwatch, (void *)fcp, time);
2980*0Sstevel@tonic-gate nxs_doit:
2981*0Sstevel@tonic-gate 		if (fdc_docmd(fcp, csb->csb_cmd, csb->csb_ncmds) == -1) {
2982*0Sstevel@tonic-gate 			/* cntlr did not accept command bytes */
2983*0Sstevel@tonic-gate 			fdcquiesce(fcp);
2984*0Sstevel@tonic-gate 			csb->csb_xstate = FXS_RESET;
2985*0Sstevel@tonic-gate 			csb->csb_cmdstat = EIO;
2986*0Sstevel@tonic-gate 			break;
2987*0Sstevel@tonic-gate 		}
2988*0Sstevel@tonic-gate 		csb->csb_xstate = FXS_DOWT;
2989*0Sstevel@tonic-gate 		break;
2990*0Sstevel@tonic-gate 
2991*0Sstevel@tonic-gate 	case FXS_DOWT:		/* operation complete */
2992*0Sstevel@tonic-gate 		(void) fdc_result(fcp, csb->csb_rslt, csb->csb_nrslts);
2993*0Sstevel@tonic-gate 		/*
2994*0Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_result.
2995*0Sstevel@tonic-gate 		 * Actual results checked below.
2996*0Sstevel@tonic-gate 		 */
2997*0Sstevel@tonic-gate 		if (*csb->csb_cmd == FO_SDRV) {
2998*0Sstevel@tonic-gate 			csb->csb_status =
2999*0Sstevel@tonic-gate 			    (*csb->csb_rslt ^ (S3_DRRDY | S3_2SIDE)) &
3000*0Sstevel@tonic-gate 			    ~(S3_HEAD | S3_UNIT);
3001*0Sstevel@tonic-gate 		} else {
3002*0Sstevel@tonic-gate 			csb->csb_status = *csb->csb_rslt &
3003*0Sstevel@tonic-gate 			    (S0_ICMASK | S0_ECHK | S0_NOTRDY);
3004*0Sstevel@tonic-gate 		}
3005*0Sstevel@tonic-gate nxs_cmpl:
3006*0Sstevel@tonic-gate 		if (csb->csb_status)
3007*0Sstevel@tonic-gate 			csb->csb_cmdstat = EIO;
3008*0Sstevel@tonic-gate 		csb->csb_xstate = FXS_END;
3009*0Sstevel@tonic-gate 
3010*0Sstevel@tonic-gate 		/*  remove watchdog timer if armed and not already triggered */
3011*0Sstevel@tonic-gate 		if (fcp->c_timeid != 0) {
3012*0Sstevel@tonic-gate 			timeout_id_t timeid;
3013*0Sstevel@tonic-gate 			timeid = fcp->c_timeid;
3014*0Sstevel@tonic-gate 			fcp->c_timeid = 0;
3015*0Sstevel@tonic-gate 			mutex_exit(&fcp->c_lock);
3016*0Sstevel@tonic-gate 			(void) untimeout(timeid);
3017*0Sstevel@tonic-gate 			mutex_enter(&fcp->c_lock);
3018*0Sstevel@tonic-gate 		}
3019*0Sstevel@tonic-gate 		break;
3020*0Sstevel@tonic-gate 
3021*0Sstevel@tonic-gate 	case FXS_KILL:		/* quiesce cntlr by reset */
3022*0Sstevel@tonic-gate 		fdcquiesce(fcp);
3023*0Sstevel@tonic-gate 		fcp->c_timeid = timeout(fdwatch, (void *)fcp,
3024*0Sstevel@tonic-gate 		    drv_usectohz(2000000));
3025*0Sstevel@tonic-gate 		csb->csb_xstate = FXS_RESET;
3026*0Sstevel@tonic-gate 		break;
3027*0Sstevel@tonic-gate 
3028*0Sstevel@tonic-gate 	case FXS_RESET:		/* int from reset */
3029*0Sstevel@tonic-gate 		for (unit = 0; unit < NFDUN; unit++) {
3030*0Sstevel@tonic-gate 			(void) fdcsense_int(fcp, NULL, NULL);
3031*0Sstevel@tonic-gate 			fcp->c_curpcyl[unit] = -1;
3032*0Sstevel@tonic-gate 		}
3033*0Sstevel@tonic-gate 		if (fcp->c_timeid != 0) {
3034*0Sstevel@tonic-gate 			timeout_id_t timeid;
3035*0Sstevel@tonic-gate 			timeid = fcp->c_timeid;
3036*0Sstevel@tonic-gate 			fcp->c_timeid = 0;
3037*0Sstevel@tonic-gate 			mutex_exit(&fcp->c_lock);
3038*0Sstevel@tonic-gate 			(void) untimeout(timeid);
3039*0Sstevel@tonic-gate 			mutex_enter(&fcp->c_lock);
3040*0Sstevel@tonic-gate 		}
3041*0Sstevel@tonic-gate 		csb->csb_xstate = FXS_END;
3042*0Sstevel@tonic-gate 		break;
3043*0Sstevel@tonic-gate 
3044*0Sstevel@tonic-gate 	default:
3045*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "fdc: statemach, unknown state");
3046*0Sstevel@tonic-gate 		return (-1);
3047*0Sstevel@tonic-gate 	}
3048*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L1, FDEM_EXEC,
3049*0Sstevel@tonic-gate 	    (CE_CONT, "fdc_statemach unit %d: %d -> %d\n",
3050*0Sstevel@tonic-gate 	    csb->csb_drive, csb->csb_oldxs, csb->csb_xstate));
3051*0Sstevel@tonic-gate 	return (csb->csb_xstate);
3052*0Sstevel@tonic-gate }
3053*0Sstevel@tonic-gate 
3054*0Sstevel@tonic-gate 
3055*0Sstevel@tonic-gate /*
3056*0Sstevel@tonic-gate  * routine to program a command into the floppy disk controller.
3057*0Sstevel@tonic-gate  */
3058*0Sstevel@tonic-gate int
3059*0Sstevel@tonic-gate fdc_docmd(struct fdcntlr *fcp, uchar_t *oplistp, uchar_t count)
3060*0Sstevel@tonic-gate {
3061*0Sstevel@tonic-gate 	int ntries;
3062*0Sstevel@tonic-gate 
3063*0Sstevel@tonic-gate 	ASSERT(count >= 1);
3064*0Sstevel@tonic-gate 	FCERRPRINT(FDEP_L0, FDEM_EXEC,
3065*0Sstevel@tonic-gate 	    (CE_CONT, "fdc_docmd: %x %x %x %x %x %x %x %x %x\n",
3066*0Sstevel@tonic-gate 	    oplistp[0], oplistp[1], oplistp[2], oplistp[3], oplistp[4],
3067*0Sstevel@tonic-gate 	    oplistp[5], oplistp[6], oplistp[7], oplistp[8]));
3068*0Sstevel@tonic-gate 
3069*0Sstevel@tonic-gate 	do {
3070*0Sstevel@tonic-gate 		ntries = FDC_RQM_RETRY;
3071*0Sstevel@tonic-gate 		do {
3072*0Sstevel@tonic-gate 			if ((inb(fcp->c_regbase + FCR_MSR) & (MS_RQM|MS_DIO))
3073*0Sstevel@tonic-gate 			    == MS_RQM)
3074*0Sstevel@tonic-gate 				break;
3075*0Sstevel@tonic-gate 			else
3076*0Sstevel@tonic-gate 				drv_usecwait(1);
3077*0Sstevel@tonic-gate 		} while (--ntries);
3078*0Sstevel@tonic-gate 		if (ntries == 0) {
3079*0Sstevel@tonic-gate 			FCERRPRINT(FDEP_L3, FDEM_EXEC,
3080*0Sstevel@tonic-gate 			    (CE_WARN, "fdc_docmd: ctlr not ready"));
3081*0Sstevel@tonic-gate 			return (-1);
3082*0Sstevel@tonic-gate 		}
3083*0Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_DATA, *oplistp++);
3084*0Sstevel@tonic-gate 		drv_usecwait(16);	/* See comment in fdc_result() */
3085*0Sstevel@tonic-gate 	} while (--count);
3086*0Sstevel@tonic-gate 	return (0);
3087*0Sstevel@tonic-gate }
3088*0Sstevel@tonic-gate 
3089*0Sstevel@tonic-gate 
3090*0Sstevel@tonic-gate /*
3091*0Sstevel@tonic-gate  * Routine to return controller/drive status information.
3092*0Sstevel@tonic-gate  * The diskette-controller data-register is read the
3093*0Sstevel@tonic-gate  * requested number of times and the results are placed in
3094*0Sstevel@tonic-gate  * consecutive memory locations starting at the passed
3095*0Sstevel@tonic-gate  * address.
3096*0Sstevel@tonic-gate  */
3097*0Sstevel@tonic-gate int
3098*0Sstevel@tonic-gate fdc_result(struct fdcntlr *fcp, uchar_t *rsltp, uchar_t rcount)
3099*0Sstevel@tonic-gate {
3100*0Sstevel@tonic-gate 	int ntries;
3101*0Sstevel@tonic-gate 	uchar_t *abresultp = rsltp;
3102*0Sstevel@tonic-gate 	uchar_t stat;
3103*0Sstevel@tonic-gate 	int laxative = 7;
3104*0Sstevel@tonic-gate 
3105*0Sstevel@tonic-gate 	ntries = 10 * FDC_RQM_RETRY;
3106*0Sstevel@tonic-gate 	do {
3107*0Sstevel@tonic-gate 		do {
3108*0Sstevel@tonic-gate 			if ((inb(fcp->c_regbase + FCR_MSR) &
3109*0Sstevel@tonic-gate 			    (MS_RQM | MS_DIO)) == (MS_RQM | MS_DIO))
3110*0Sstevel@tonic-gate 				break;
3111*0Sstevel@tonic-gate 			else
3112*0Sstevel@tonic-gate 				drv_usecwait(10);
3113*0Sstevel@tonic-gate 		} while (--ntries);
3114*0Sstevel@tonic-gate 		if (!ntries) {
3115*0Sstevel@tonic-gate 			FCERRPRINT(FDEP_L3, FDEM_EXEC,
3116*0Sstevel@tonic-gate 			    (CE_WARN, "fdc_result: ctlr not ready"));
3117*0Sstevel@tonic-gate 			return (-2);
3118*0Sstevel@tonic-gate 		}
3119*0Sstevel@tonic-gate 		*rsltp++ = inb(fcp->c_regbase + FCR_DATA);
3120*0Sstevel@tonic-gate 
3121*0Sstevel@tonic-gate 		/*
3122*0Sstevel@tonic-gate 		 * The PRM suggests waiting for 14.5 us.
3123*0Sstevel@tonic-gate 		 * Adding a bit more to cover the case of bad calibration
3124*0Sstevel@tonic-gate 		 * of drv_usecwait().
3125*0Sstevel@tonic-gate 		 */
3126*0Sstevel@tonic-gate 		drv_usecwait(16);
3127*0Sstevel@tonic-gate 		ntries = FDC_RQM_RETRY;
3128*0Sstevel@tonic-gate 	} while (--rcount);
3129*0Sstevel@tonic-gate 	while ((inb(fcp->c_regbase + FCR_MSR) & MS_CB) && laxative--) {
3130*0Sstevel@tonic-gate 		FCERRPRINT(FDEP_L3, FDEM_EXEC,
3131*0Sstevel@tonic-gate 		    (CE_WARN, "fdc_result: ctlr still busy"));
3132*0Sstevel@tonic-gate 		/*
3133*0Sstevel@tonic-gate 		 * try to complete Result phase by purging
3134*0Sstevel@tonic-gate 		 * result bytes queued for reading
3135*0Sstevel@tonic-gate 		 */
3136*0Sstevel@tonic-gate 		*abresultp = S0_IVCMD;
3137*0Sstevel@tonic-gate 		do {
3138*0Sstevel@tonic-gate 			stat = inb(fcp->c_regbase + FCR_MSR) &
3139*0Sstevel@tonic-gate 			    (MS_RQM | MS_DIO);
3140*0Sstevel@tonic-gate 			if (stat == MS_RQM) {
3141*0Sstevel@tonic-gate 				/*
3142*0Sstevel@tonic-gate 				 * Result phase is complete
3143*0Sstevel@tonic-gate 				 * but did we get the results corresponding to
3144*0Sstevel@tonic-gate 				 * the command we think we executed?
3145*0Sstevel@tonic-gate 				 */
3146*0Sstevel@tonic-gate 				return (-1);
3147*0Sstevel@tonic-gate 			}
3148*0Sstevel@tonic-gate 			if (stat == (MS_RQM | MS_DIO))
3149*0Sstevel@tonic-gate 				break;
3150*0Sstevel@tonic-gate 			else
3151*0Sstevel@tonic-gate 				drv_usecwait(10);
3152*0Sstevel@tonic-gate 		} while (--ntries);
3153*0Sstevel@tonic-gate 		if (!ntries || !laxative) {
3154*0Sstevel@tonic-gate 			FCERRPRINT(FDEP_L3, FDEM_EXEC,
3155*0Sstevel@tonic-gate 			    (CE_WARN,
3156*0Sstevel@tonic-gate 			    "fdc_result: ctlr still busy and not ready"));
3157*0Sstevel@tonic-gate 			return (-3);
3158*0Sstevel@tonic-gate 		}
3159*0Sstevel@tonic-gate 		(void) inb(fcp->c_regbase + FCR_DATA);
3160*0Sstevel@tonic-gate 
3161*0Sstevel@tonic-gate 		drv_usecwait(16);	/* See comment above */
3162*0Sstevel@tonic-gate 		ntries = FDC_RQM_RETRY;
3163*0Sstevel@tonic-gate 	}
3164*0Sstevel@tonic-gate 	return (0);
3165*0Sstevel@tonic-gate }
3166*0Sstevel@tonic-gate 
3167*0Sstevel@tonic-gate /*
3168*0Sstevel@tonic-gate  *  Function: get_unit()
3169*0Sstevel@tonic-gate  *
3170*0Sstevel@tonic-gate  *  Assumptions:  ioaddr is either 0x3f0 or 0x370
3171*0Sstevel@tonic-gate  */
3172*0Sstevel@tonic-gate static int
3173*0Sstevel@tonic-gate get_unit(dev_info_t *dip, int *cntrl_num)
3174*0Sstevel@tonic-gate {
3175*0Sstevel@tonic-gate 	int ioaddr;
3176*0Sstevel@tonic-gate 
3177*0Sstevel@tonic-gate 	if (get_ioaddr(dip, &ioaddr) != DDI_SUCCESS)
3178*0Sstevel@tonic-gate 		return (DDI_FAILURE);
3179*0Sstevel@tonic-gate 
3180*0Sstevel@tonic-gate 	switch (ioaddr) {
3181*0Sstevel@tonic-gate 	case 0x3f0:
3182*0Sstevel@tonic-gate 		*cntrl_num = 0;
3183*0Sstevel@tonic-gate 		break;
3184*0Sstevel@tonic-gate 
3185*0Sstevel@tonic-gate 	case 0x370:
3186*0Sstevel@tonic-gate 		*cntrl_num = 1;
3187*0Sstevel@tonic-gate 		break;
3188*0Sstevel@tonic-gate 
3189*0Sstevel@tonic-gate 	default:
3190*0Sstevel@tonic-gate 		return (DDI_FAILURE);
3191*0Sstevel@tonic-gate 	}
3192*0Sstevel@tonic-gate 	return (DDI_SUCCESS);
3193*0Sstevel@tonic-gate }
3194*0Sstevel@tonic-gate 
3195*0Sstevel@tonic-gate static int
3196*0Sstevel@tonic-gate get_ioaddr(dev_info_t *dip, int *ioaddr)
3197*0Sstevel@tonic-gate {
3198*0Sstevel@tonic-gate 	int reglen, nregs, i;
3199*0Sstevel@tonic-gate 	int status = DDI_FAILURE;
3200*0Sstevel@tonic-gate 	struct {
3201*0Sstevel@tonic-gate 		int bustype;
3202*0Sstevel@tonic-gate 		int base;
3203*0Sstevel@tonic-gate 		int size;
3204*0Sstevel@tonic-gate 	} *reglist;
3205*0Sstevel@tonic-gate 
3206*0Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
3207*0Sstevel@tonic-gate 		"reg", (caddr_t)&reglist, &reglen) != DDI_PROP_SUCCESS) {
3208*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "fdc: reg property not found");
3209*0Sstevel@tonic-gate 		return (DDI_FAILURE);
3210*0Sstevel@tonic-gate 	}
3211*0Sstevel@tonic-gate 
3212*0Sstevel@tonic-gate 	nregs = reglen / sizeof (*reglist);
3213*0Sstevel@tonic-gate 	for (i = 0; i < nregs; i++) {
3214*0Sstevel@tonic-gate 		if (reglist[i].bustype == 1) {
3215*0Sstevel@tonic-gate 			*ioaddr = reglist[i].base;
3216*0Sstevel@tonic-gate 			status = DDI_SUCCESS;
3217*0Sstevel@tonic-gate 			break;
3218*0Sstevel@tonic-gate 		}
3219*0Sstevel@tonic-gate 	}
3220*0Sstevel@tonic-gate 	kmem_free(reglist, reglen);
3221*0Sstevel@tonic-gate 
3222*0Sstevel@tonic-gate 	if (status == DDI_SUCCESS) {
3223*0Sstevel@tonic-gate 		if (*ioaddr == 0x3f2 || *ioaddr == 0x372) {
3224*0Sstevel@tonic-gate 			/*
3225*0Sstevel@tonic-gate 			 * Some BIOS's (ASUS is one) don't include first
3226*0Sstevel@tonic-gate 			 * two IO ports in the floppy controller resources.
3227*0Sstevel@tonic-gate 			 */
3228*0Sstevel@tonic-gate 
3229*0Sstevel@tonic-gate 			*ioaddr -= 2; /* step back to 0x3f0 or 0x370 */
3230*0Sstevel@tonic-gate 
3231*0Sstevel@tonic-gate 			/*
3232*0Sstevel@tonic-gate 			 * It would be nice to update the regs property as well
3233*0Sstevel@tonic-gate 			 * so device pathname contains 3f0 instead of 3f2, but
3234*0Sstevel@tonic-gate 			 * updating the regs now won't have this effect as that
3235*0Sstevel@tonic-gate 			 * component of the device pathname has already been
3236*0Sstevel@tonic-gate 			 * constructed by the ISA nexus driver.
3237*0Sstevel@tonic-gate 			 *
3238*0Sstevel@tonic-gate 			 * reglist[i].base -= 2;
3239*0Sstevel@tonic-gate 			 * reglist[i].size += 2;
3240*0Sstevel@tonic-gate 			 * dev = makedevice(ddi_driver_major(dip), 0);
3241*0Sstevel@tonic-gate 			 * ddi_prop_update_int_array(dev, dip, "reg",
3242*0Sstevel@tonic-gate 			 *    (int *)reglist, reglen / sizeof (int));
3243*0Sstevel@tonic-gate 			 */
3244*0Sstevel@tonic-gate 		}
3245*0Sstevel@tonic-gate 	}
3246*0Sstevel@tonic-gate 
3247*0Sstevel@tonic-gate 	return (status);
3248*0Sstevel@tonic-gate }
3249