xref: /onnv-gate/usr/src/uts/common/io/fdc.c (revision 5295:a21f2449e5f9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*5295Srandyf  * Common Development and Distribution License (the "License").
6*5295Srandyf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*5295Srandyf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Floppy Disk Controller Driver
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  *   for the standard PC architecture using the Intel 8272A fdc.
320Sstevel@tonic-gate  *   Note that motor control and drive select use a latch external
330Sstevel@tonic-gate  *   to the fdc.
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  *   This driver is EISA capable, and uses DMA buffer chaining if available.
360Sstevel@tonic-gate  *   If this driver is attached to the ISA bus nexus (or if the EISA bus driver
370Sstevel@tonic-gate  *   does not support DMA buffer chaining), then the bus driver must ensure
380Sstevel@tonic-gate  *   that dma mapping (breakup) and dma engine requests are properly degraded.
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * hack for bugid 1160621:
430Sstevel@tonic-gate  * workaround compiler optimization bug by turning on DEBUG
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate #ifndef DEBUG
460Sstevel@tonic-gate #define	DEBUG	1
470Sstevel@tonic-gate #endif
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include <sys/param.h>
500Sstevel@tonic-gate #include <sys/buf.h>
510Sstevel@tonic-gate #include <sys/ioctl.h>
520Sstevel@tonic-gate #include <sys/uio.h>
530Sstevel@tonic-gate #include <sys/open.h>
540Sstevel@tonic-gate #include <sys/conf.h>
550Sstevel@tonic-gate #include <sys/file.h>
560Sstevel@tonic-gate #include <sys/cmn_err.h>
570Sstevel@tonic-gate #include <sys/debug.h>
580Sstevel@tonic-gate #include <sys/kmem.h>
590Sstevel@tonic-gate #include <sys/stat.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #include <sys/autoconf.h>
620Sstevel@tonic-gate #include <sys/dkio.h>
630Sstevel@tonic-gate #include <sys/vtoc.h>
640Sstevel@tonic-gate #include <sys/kstat.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <sys/fdio.h>
670Sstevel@tonic-gate #include <sys/fdc.h>
680Sstevel@tonic-gate #include <sys/i8272A.h>
690Sstevel@tonic-gate #include <sys/fd_debug.h>
700Sstevel@tonic-gate #include <sys/promif.h>
710Sstevel@tonic-gate #include <sys/ddi.h>
720Sstevel@tonic-gate #include <sys/sunddi.h>
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  * bss (uninitialized data)
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate static void *fdc_state_head;		/* opaque handle top of state structs */
780Sstevel@tonic-gate static ddi_dma_attr_t fdc_dma_attr;
79940Smrj static ddi_device_acc_attr_t fdc_accattr = {DDI_DEVICE_ATTR_V0,
80940Smrj 	DDI_STRUCTURE_LE_ACC, DDI_STRICTORDER_ACC};
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate  * Local static data
840Sstevel@tonic-gate  */
850Sstevel@tonic-gate #define	OURUN_TRIES	12
860Sstevel@tonic-gate static uchar_t rwretry = 4;
870Sstevel@tonic-gate static uchar_t skretry = 3;
880Sstevel@tonic-gate static uchar_t configurecmd[4] = {FO_CNFG, 0, 0x0F, 0};
890Sstevel@tonic-gate static uchar_t recalcmd[2] = {FO_RECAL, 0};
900Sstevel@tonic-gate static uchar_t senseintcmd = FO_SINT;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate  * error handling
940Sstevel@tonic-gate  *
950Sstevel@tonic-gate  * for debugging, set rwretry and skretry = 1
960Sstevel@tonic-gate  *		set fcerrlevel to 1
970Sstevel@tonic-gate  *		set fcerrmask  to 224  or 644
980Sstevel@tonic-gate  *
990Sstevel@tonic-gate  * after debug, set rwretry to 4, skretry to 3, and fcerrlevel to 5
1000Sstevel@tonic-gate  * set fcerrmask to FDEM_ALL
1010Sstevel@tonic-gate  * or remove the define DEBUG
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate static uint_t fcerrmask = FDEM_ALL;
1040Sstevel@tonic-gate static int fcerrlevel = 6;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate #define	KIOIP	KSTAT_INTR_PTR(fcp->c_intrstat)
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate static xlate_tbl_t drate_mfm[] = {
1100Sstevel@tonic-gate 	{  250, 2},
1110Sstevel@tonic-gate 	{  300, 1},
1120Sstevel@tonic-gate 	{  417, 0},
1130Sstevel@tonic-gate 	{  500, 0},
1140Sstevel@tonic-gate 	{ 1000, 3},
1150Sstevel@tonic-gate 	{    0, 0}
1160Sstevel@tonic-gate };
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate static xlate_tbl_t sector_size[] = {
1190Sstevel@tonic-gate 	{  256, 1},
1200Sstevel@tonic-gate 	{  512, 2},
1210Sstevel@tonic-gate 	{ 1024, 3},
1220Sstevel@tonic-gate 	{    0, 2}
1230Sstevel@tonic-gate };
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate static xlate_tbl_t motor_onbits[] = {
1260Sstevel@tonic-gate 	{  0, 0x10},
1270Sstevel@tonic-gate 	{  1, 0x20},
1280Sstevel@tonic-gate 	{  2, 0x40},
1290Sstevel@tonic-gate 	{  3, 0x80},
1300Sstevel@tonic-gate 	{  0, 0x80}
1310Sstevel@tonic-gate };
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate static xlate_tbl_t step_rate[] = {
1340Sstevel@tonic-gate 	{  10, 0xF0},		/* for 500K data rate */
1350Sstevel@tonic-gate 	{  20, 0xE0},
1360Sstevel@tonic-gate 	{  30, 0xD0},
1370Sstevel@tonic-gate 	{  40, 0xC0},
1380Sstevel@tonic-gate 	{  50, 0xB0},
1390Sstevel@tonic-gate 	{  60, 0xA0},
1400Sstevel@tonic-gate 	{  70, 0x90},
1410Sstevel@tonic-gate 	{  80, 0x80},
1420Sstevel@tonic-gate 	{  90, 0x70},
1430Sstevel@tonic-gate 	{ 100, 0x60},
1440Sstevel@tonic-gate 	{ 110, 0x50},
1450Sstevel@tonic-gate 	{ 120, 0x40},
1460Sstevel@tonic-gate 	{ 130, 0x30},
1470Sstevel@tonic-gate 	{ 140, 0x20},
1480Sstevel@tonic-gate 	{ 150, 0x10},
1490Sstevel@tonic-gate 	{ 160, 0x00},
1500Sstevel@tonic-gate 	{   0, 0x00}
1510Sstevel@tonic-gate };
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate #ifdef notdef
1540Sstevel@tonic-gate static xlate_tbl_t head_unld[] = {
1550Sstevel@tonic-gate 	{  16, 0x1},		/* for 500K data rate */
1560Sstevel@tonic-gate 	{  32, 0x2},
1570Sstevel@tonic-gate 	{  48, 0x3},
1580Sstevel@tonic-gate 	{  64, 0x4},
1590Sstevel@tonic-gate 	{  80, 0x5},
1600Sstevel@tonic-gate 	{  96, 0x6},
1610Sstevel@tonic-gate 	{ 112, 0x7},
1620Sstevel@tonic-gate 	{ 128, 0x8},
1630Sstevel@tonic-gate 	{ 144, 0x9},
1640Sstevel@tonic-gate 	{ 160, 0xA},
1650Sstevel@tonic-gate 	{ 176, 0xB},
1660Sstevel@tonic-gate 	{ 192, 0xC},
1670Sstevel@tonic-gate 	{ 208, 0xD},
1680Sstevel@tonic-gate 	{ 224, 0xE},
1690Sstevel@tonic-gate 	{ 240, 0xF},
1700Sstevel@tonic-gate 	{ 256, 0x0},
1710Sstevel@tonic-gate 	{   0, 0x0}
1720Sstevel@tonic-gate };
1730Sstevel@tonic-gate #endif
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate static struct fdcmdinfo {
1760Sstevel@tonic-gate 	char *cmdname;		/* command name */
1770Sstevel@tonic-gate 	uchar_t ncmdbytes;	/* number of bytes of command */
1780Sstevel@tonic-gate 	uchar_t nrsltbytes;	/* number of bytes in result */
1790Sstevel@tonic-gate 	uchar_t cmdtype;		/* characteristics */
1800Sstevel@tonic-gate } fdcmds[] = {
1810Sstevel@tonic-gate 	"", 0, 0, 0,			/* - */
1820Sstevel@tonic-gate 	"", 0, 0, 0,			/* - */
1830Sstevel@tonic-gate 	"read_track", 9, 7, 1,		/* 2 */
1840Sstevel@tonic-gate 	"specify", 3, 0, 3,		/* 3 */
1850Sstevel@tonic-gate 	"sense_drv_status", 2, 1, 3,	/* 4 */
1860Sstevel@tonic-gate 	"write", 9, 7, 1,		/* 5 */
1870Sstevel@tonic-gate 	"read", 9, 7, 1,		/* 6 */
1880Sstevel@tonic-gate 	"recalibrate", 2, 0, 2,		/* 7 */
1890Sstevel@tonic-gate 	"sense_int_status", 1, 2, 3,	/* 8 */
1900Sstevel@tonic-gate 	"write_del", 9, 7, 1,		/* 9 */
1910Sstevel@tonic-gate 	"read_id", 2, 7, 2,		/* A */
1920Sstevel@tonic-gate 	"", 0, 0, 0,			/* - */
1930Sstevel@tonic-gate 	"read_del", 9, 7, 1,		/* C */
1940Sstevel@tonic-gate 	"format_track", 10, 7, 1,	/* D */
1950Sstevel@tonic-gate 	"dump_reg", 1, 10, 4,		/* E */
1960Sstevel@tonic-gate 	"seek", 3, 0, 2,		/* F */
1970Sstevel@tonic-gate 	"version", 1, 1, 3,		/* 10 */
1980Sstevel@tonic-gate 	"", 0, 0, 0,			/* - */
1990Sstevel@tonic-gate 	"perp_mode", 2, 0, 3,		/* 12 */
2000Sstevel@tonic-gate 	"configure", 4, 0, 4,		/* 13 */
2010Sstevel@tonic-gate 	/* relative seek */
2020Sstevel@tonic-gate };
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate static int
2060Sstevel@tonic-gate fdc_bus_ctl(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
2070Sstevel@tonic-gate static int get_ioaddr(dev_info_t *dip, int *ioaddr);
2080Sstevel@tonic-gate static int get_unit(dev_info_t *dip, int *cntrl_num);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate struct bus_ops fdc_bus_ops = {
2110Sstevel@tonic-gate 	BUSO_REV,
2120Sstevel@tonic-gate 	nullbusmap,
2130Sstevel@tonic-gate 	0,	/* ddi_intrspec_t (*bus_get_intrspec)(); */
2140Sstevel@tonic-gate 	0,	/* int 	(*bus_add_intrspec)(); */
2150Sstevel@tonic-gate 	0,	/* void (*bus_remove_intrspec)(); */
2160Sstevel@tonic-gate 	i_ddi_map_fault,
2170Sstevel@tonic-gate 	ddi_dma_map,
2180Sstevel@tonic-gate 	ddi_dma_allochdl,
2190Sstevel@tonic-gate 	ddi_dma_freehdl,
2200Sstevel@tonic-gate 	ddi_dma_bindhdl,
2210Sstevel@tonic-gate 	ddi_dma_unbindhdl,
2220Sstevel@tonic-gate 	ddi_dma_flush,
2230Sstevel@tonic-gate 	ddi_dma_win,
2240Sstevel@tonic-gate 	ddi_dma_mctl,
2250Sstevel@tonic-gate 	fdc_bus_ctl,
2260Sstevel@tonic-gate 	ddi_bus_prop_op,
2270Sstevel@tonic-gate };
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate static int fdc_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
2300Sstevel@tonic-gate static int fdc_probe(dev_info_t *);
2310Sstevel@tonic-gate static int fdc_attach(dev_info_t *, ddi_attach_cmd_t);
2320Sstevel@tonic-gate static int fdc_detach(dev_info_t *, ddi_detach_cmd_t);
2330Sstevel@tonic-gate static int fdc_enhance_probe(struct fdcntlr *fcp);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate struct dev_ops	fdc_ops = {
2360Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
2370Sstevel@tonic-gate 	0,			/* refcnt  */
2380Sstevel@tonic-gate 	fdc_getinfo,		/* getinfo */
2390Sstevel@tonic-gate 	nulldev,		/* identify */
2400Sstevel@tonic-gate 	fdc_probe,		/* probe */
2410Sstevel@tonic-gate 	fdc_attach,		/* attach */
2420Sstevel@tonic-gate 	fdc_detach,		/* detach */
2430Sstevel@tonic-gate 	nodev,			/* reset */
2440Sstevel@tonic-gate 	(struct cb_ops *)0,	/* driver operations */
2450Sstevel@tonic-gate 	&fdc_bus_ops		/* bus operations */
2460Sstevel@tonic-gate };
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate  * This is the loadable module wrapper.
2500Sstevel@tonic-gate  */
2510Sstevel@tonic-gate #include <sys/modctl.h>
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate extern struct mod_ops mod_driverops;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate static struct modldrv modldrv = {
2560Sstevel@tonic-gate 	&mod_driverops,		/* Type of module. This one is a driver */
2570Sstevel@tonic-gate 	"Floppy Controller %I%",	/* Name of the module. */
2580Sstevel@tonic-gate 	&fdc_ops,		/* Driver ops vector */
2590Sstevel@tonic-gate };
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate static struct modlinkage modlinkage = {
2620Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
2630Sstevel@tonic-gate };
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate int
2660Sstevel@tonic-gate _init(void)
2670Sstevel@tonic-gate {
2680Sstevel@tonic-gate 	int retval;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	if ((retval = ddi_soft_state_init(&fdc_state_head,
2710Sstevel@tonic-gate 	    sizeof (struct fdcntlr) + NFDUN * sizeof (struct fcu_obj), 0)) != 0)
2720Sstevel@tonic-gate 		return (retval);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	if ((retval = mod_install(&modlinkage)) != 0)
2750Sstevel@tonic-gate 		ddi_soft_state_fini(&fdc_state_head);
2760Sstevel@tonic-gate 	return (retval);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate int
2800Sstevel@tonic-gate _fini(void)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	int retval;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	if ((retval = mod_remove(&modlinkage)) != 0)
2850Sstevel@tonic-gate 		return (retval);
2860Sstevel@tonic-gate 	ddi_soft_state_fini(&fdc_state_head);
2870Sstevel@tonic-gate 	return (retval);
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate int
2910Sstevel@tonic-gate _info(struct modinfo *modinfop)
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate int fdc_start(struct fcu_obj *);
2980Sstevel@tonic-gate int fdc_abort(struct fcu_obj *);
2990Sstevel@tonic-gate int fdc_getcap(struct fcu_obj *, char *, int);
3000Sstevel@tonic-gate int fdc_setcap(struct fcu_obj *, char *, int, int);
3010Sstevel@tonic-gate int fdc_dkinfo(struct fcu_obj *, struct dk_cinfo *);
3020Sstevel@tonic-gate int fdc_select(struct fcu_obj *, int, int);
3030Sstevel@tonic-gate int fdgetchng(struct fcu_obj *, int);
3040Sstevel@tonic-gate int fdresetchng(struct fcu_obj *, int);
3050Sstevel@tonic-gate int fdrecalseek(struct fcu_obj *, int, int, int);
3060Sstevel@tonic-gate int fdrw(struct fcu_obj *, int, int, int, int, int, caddr_t, uint_t);
3070Sstevel@tonic-gate int fdtrkformat(struct fcu_obj *, int, int, int, int);
3080Sstevel@tonic-gate int fdrawioctl(struct fcu_obj *, int, caddr_t);
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate static struct fcobjops fdc_iops = {
3110Sstevel@tonic-gate 		fdc_start,	/* controller start */
3120Sstevel@tonic-gate 		fdc_abort,	/* controller abort */
3130Sstevel@tonic-gate 		fdc_getcap,	/* capability retrieval */
3140Sstevel@tonic-gate 		fdc_setcap,	/* capability establishment */
3150Sstevel@tonic-gate 		fdc_dkinfo,	/* get disk controller info */
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 		fdc_select,	/* select / deselect unit */
3180Sstevel@tonic-gate 		fdgetchng,	/* get media change */
3190Sstevel@tonic-gate 		fdresetchng,	/* reset media change */
3200Sstevel@tonic-gate 		fdrecalseek,	/* recal / seek */
321940Smrj 		NULL,		/* read /write request (UNUSED) */
3220Sstevel@tonic-gate 		fdrw,		/* read /write sector */
3230Sstevel@tonic-gate 		fdtrkformat,	/* format track */
3240Sstevel@tonic-gate 		fdrawioctl	/* raw ioctl */
3250Sstevel@tonic-gate };
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate /*
3290Sstevel@tonic-gate  * Function prototypes
3300Sstevel@tonic-gate  */
3310Sstevel@tonic-gate void encode(xlate_tbl_t *tablep, int val, uchar_t *rcode);
3320Sstevel@tonic-gate int decode(xlate_tbl_t *, int, int *);
3330Sstevel@tonic-gate static int fdc_propinit1(struct fdcntlr *, int);
3340Sstevel@tonic-gate static void fdc_propinit2(struct fdcntlr *, int);
3350Sstevel@tonic-gate void fdcquiesce(struct fdcntlr *);
3360Sstevel@tonic-gate int fdcsense_chng(struct fdcntlr *, int);
3370Sstevel@tonic-gate int fdcsense_drv(struct fdcntlr *, int);
3380Sstevel@tonic-gate int fdcsense_int(struct fdcntlr *, int *, int *);
3390Sstevel@tonic-gate int fdcspecify(struct fdcntlr *, int, int, int);
3400Sstevel@tonic-gate int fdcspdchange(struct fdcntlr *, struct fcu_obj *, int);
3410Sstevel@tonic-gate static int fdc_exec(struct fdcntlr *, int, int);
3420Sstevel@tonic-gate int fdcheckdisk(struct fdcntlr *, int);
3430Sstevel@tonic-gate static uint_t fdc_intr(caddr_t arg);
3440Sstevel@tonic-gate static void fdwatch(void *arg);
3450Sstevel@tonic-gate static void fdmotort(void *arg);
3460Sstevel@tonic-gate static int fdrecover(struct fdcntlr *);
3470Sstevel@tonic-gate static int fdc_motorsm(struct fcu_obj *, int, int);
3480Sstevel@tonic-gate static int fdc_statemach(struct fdcntlr *);
3490Sstevel@tonic-gate int fdc_docmd(struct fdcntlr *, uchar_t *, uchar_t);
3500Sstevel@tonic-gate int fdc_result(struct fdcntlr *, uchar_t *, uchar_t);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate /* ARGSUSED */
3540Sstevel@tonic-gate static int
3550Sstevel@tonic-gate fdc_bus_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop,
3560Sstevel@tonic-gate     void *arg, void *result)
3570Sstevel@tonic-gate {
3580Sstevel@tonic-gate 	struct 	fdcntlr *fcp;
3590Sstevel@tonic-gate 	struct	fcu_obj *fjp;
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	FCERRPRINT(FDEP_L0, FDEM_ATTA,
3620Sstevel@tonic-gate 	    (CE_CONT, "fdc_bus_ctl: cmd= %x\n", ctlop));
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	if ((fcp = ddi_get_driver_private(dip)) == NULL)
3650Sstevel@tonic-gate 		return (DDI_FAILURE);
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	switch (ctlop) {
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
3700Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s%d at %s%d\n",
3710Sstevel@tonic-gate 		    ddi_get_name(rdip), ddi_get_instance(rdip),
3720Sstevel@tonic-gate 		    ddi_get_name(dip), ddi_get_instance(dip));
3730Sstevel@tonic-gate 		FCERRPRINT(FDEP_L3, FDEM_ATTA,
3740Sstevel@tonic-gate 		    (CE_WARN, "fdc_bus_ctl: report %s%d at %s%d",
3750Sstevel@tonic-gate 		    ddi_get_name(rdip), ddi_get_instance(rdip),
3760Sstevel@tonic-gate 		    ddi_get_name(dip), ddi_get_instance(dip)));
3770Sstevel@tonic-gate 		return (DDI_SUCCESS);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
3800Sstevel@tonic-gate 	{
3810Sstevel@tonic-gate 		dev_info_t *udip = (dev_info_t *)arg;
3820Sstevel@tonic-gate 		int cntlr;
3830Sstevel@tonic-gate 		int len;
3840Sstevel@tonic-gate 		int unit;
3850Sstevel@tonic-gate 		char name[MAXNAMELEN];
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 		FCERRPRINT(FDEP_L3, FDEM_ATTA,
3880Sstevel@tonic-gate 		    (CE_WARN, "fdc_bus_ctl: init child 0x%p", (void*)udip));
3890Sstevel@tonic-gate 		cntlr = fcp->c_number;
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 		len = sizeof (unit);
3920Sstevel@tonic-gate 		if (ddi_prop_op(DDI_DEV_T_ANY, udip, PROP_LEN_AND_VAL_BUF,
3930Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "unit", (caddr_t)&unit, &len)
3940Sstevel@tonic-gate 		    != DDI_PROP_SUCCESS ||
3950Sstevel@tonic-gate 		    cntlr != FDCTLR(unit) ||
3960Sstevel@tonic-gate 		    (fcp->c_unit[FDUNIT(unit)])->fj_dip)
3970Sstevel@tonic-gate 			return (DDI_NOT_WELL_FORMED);
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 		(void) sprintf(name, "%d,%d", cntlr, FDUNIT(unit));
4000Sstevel@tonic-gate 		ddi_set_name_addr(udip, name);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 		fjp = fcp->c_unit[FDUNIT(unit)];
4030Sstevel@tonic-gate 		fjp->fj_unit = unit;
4040Sstevel@tonic-gate 		fjp->fj_dip = udip;
4050Sstevel@tonic-gate 		fjp->fj_ops = &fdc_iops;
4060Sstevel@tonic-gate 		fjp->fj_fdc = fcp;
4070Sstevel@tonic-gate 		fjp->fj_iblock = &fcp->c_iblock;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 		ddi_set_driver_private(udip, fjp);
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 		return (DDI_SUCCESS);
4120Sstevel@tonic-gate 	}
4130Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
4140Sstevel@tonic-gate 	{
4150Sstevel@tonic-gate 		dev_info_t *udip = (dev_info_t *)arg;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 		FCERRPRINT(FDEP_L3, FDEM_ATTA,
4180Sstevel@tonic-gate 		    (CE_WARN, "fdc_bus_ctl: uninit child 0x%p", (void *)udip));
4190Sstevel@tonic-gate 		fjp = ddi_get_driver_private(udip);
4200Sstevel@tonic-gate 		ddi_set_driver_private(udip, NULL);
4210Sstevel@tonic-gate 		fjp->fj_dip = NULL;
4220Sstevel@tonic-gate 		ddi_set_name_addr(udip, NULL);
4230Sstevel@tonic-gate 		return (DDI_SUCCESS);
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate 	default:
4260Sstevel@tonic-gate 		return (DDI_FAILURE);
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate /* ARGSUSED */
4310Sstevel@tonic-gate static int
4320Sstevel@tonic-gate fdc_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
4330Sstevel@tonic-gate {
4340Sstevel@tonic-gate 	struct fdcntlr *fcp;
4350Sstevel@tonic-gate 	int rval;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	switch (cmd) {
4380Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
4390Sstevel@tonic-gate 		if (fcp = ddi_get_soft_state(fdc_state_head, (dev_t)arg)) {
4400Sstevel@tonic-gate 			*result = fcp->c_dip;
4410Sstevel@tonic-gate 			rval = DDI_SUCCESS;
4420Sstevel@tonic-gate 			break;
4430Sstevel@tonic-gate 		} else {
4440Sstevel@tonic-gate 			rval = DDI_FAILURE;
4450Sstevel@tonic-gate 			break;
4460Sstevel@tonic-gate 		}
4470Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
4480Sstevel@tonic-gate 		*result = (void *)(uintptr_t)getminor((dev_t)arg);
4490Sstevel@tonic-gate 		rval = DDI_SUCCESS;
4500Sstevel@tonic-gate 		break;
4510Sstevel@tonic-gate 	default:
4520Sstevel@tonic-gate 		rval = DDI_FAILURE;
4530Sstevel@tonic-gate 	}
4540Sstevel@tonic-gate 	return (rval);
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate static int
4580Sstevel@tonic-gate fdc_probe(dev_info_t *dip)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	int	debug[2];
4610Sstevel@tonic-gate 	int ioaddr;
4620Sstevel@tonic-gate 	int	len;
4630Sstevel@tonic-gate 	uchar_t	stat;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	len = sizeof (debug);
4660Sstevel@tonic-gate 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
4670Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "debug", (caddr_t)debug, &len) ==
4680Sstevel@tonic-gate 	    DDI_PROP_SUCCESS) {
4690Sstevel@tonic-gate 		fcerrlevel = debug[0];
4700Sstevel@tonic-gate 		fcerrmask = (uint_t)debug[1];
4710Sstevel@tonic-gate 	}
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	FCERRPRINT(FDEP_L3, FDEM_ATTA, (CE_WARN, "fdc_probe: dip %p",
474*5295Srandyf 	    (void*)dip));
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	if (get_ioaddr(dip, &ioaddr) != DDI_SUCCESS)
4770Sstevel@tonic-gate 		return (DDI_PROBE_FAILURE);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	stat = inb(ioaddr + FCR_MSR);
4800Sstevel@tonic-gate 	if ((stat & (MS_RQM | MS_DIO | MS_CB)) != MS_RQM &&
4810Sstevel@tonic-gate 	    (stat & ~MS_DIO) != MS_CB)
4820Sstevel@tonic-gate 		return (DDI_PROBE_FAILURE);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	return (DDI_PROBE_SUCCESS);
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate /* ARGSUSED */
4880Sstevel@tonic-gate static int
4890Sstevel@tonic-gate fdc_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4900Sstevel@tonic-gate {
4910Sstevel@tonic-gate 	struct fdcntlr *fcp;
4920Sstevel@tonic-gate 	struct fcu_obj *fjp;
4930Sstevel@tonic-gate 	int cntlr_num, ctlr, unit;
4940Sstevel@tonic-gate 	int intr_set = 0;
4950Sstevel@tonic-gate 	int len;
4960Sstevel@tonic-gate 	char name[MAXNAMELEN];
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	FCERRPRINT(FDEP_L3, FDEM_ATTA, (CE_WARN, "fdc_attach: dip %p",
499*5295Srandyf 	    (void*)dip));
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	switch (cmd) {
5020Sstevel@tonic-gate 	case DDI_ATTACH:
5030Sstevel@tonic-gate 		if (ddi_getprop
5040Sstevel@tonic-gate 		    (DDI_DEV_T_ANY, dip, 0, "ignore-hardware-nodes", 0)) {
5050Sstevel@tonic-gate 			len = sizeof (cntlr_num);
5060Sstevel@tonic-gate 			if (ddi_prop_op(DDI_DEV_T_ANY, dip,
5070Sstevel@tonic-gate 			    PROP_LEN_AND_VAL_BUF, DDI_PROP_DONTPASS, "unit",
5080Sstevel@tonic-gate 			    (caddr_t)&cntlr_num, &len) != DDI_PROP_SUCCESS) {
5090Sstevel@tonic-gate 				FCERRPRINT(FDEP_L3, FDEM_ATTA, (CE_WARN,
5100Sstevel@tonic-gate 				    "fdc_attach failed: dip %p", (void*)dip));
5110Sstevel@tonic-gate 				return (DDI_FAILURE);
5120Sstevel@tonic-gate 			}
5130Sstevel@tonic-gate 		} else {
5140Sstevel@tonic-gate 			if (get_unit(dip, &cntlr_num) != DDI_SUCCESS)
5150Sstevel@tonic-gate 				return (DDI_FAILURE);
5160Sstevel@tonic-gate 		}
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 		ctlr = ddi_get_instance(dip);
5190Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(fdc_state_head, ctlr) != 0)
5200Sstevel@tonic-gate 			return (DDI_FAILURE);
5210Sstevel@tonic-gate 		fcp = ddi_get_soft_state(fdc_state_head, ctlr);
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 		for (unit = 0, fjp = (struct fcu_obj *)(fcp+1);
5240Sstevel@tonic-gate 		    unit < NFDUN; unit++) {
5250Sstevel@tonic-gate 			fcp->c_unit[unit] = fjp++;
5260Sstevel@tonic-gate 		}
5270Sstevel@tonic-gate 		fcp->c_dip = dip;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 		if (fdc_propinit1(fcp, cntlr_num) != DDI_SUCCESS)
5300Sstevel@tonic-gate 			goto no_attach;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 		/* get iblock cookie to initialize mutex used in the ISR */
5330Sstevel@tonic-gate 		if (ddi_get_iblock_cookie(dip, (uint_t)0, &fcp->c_iblock) !=
5340Sstevel@tonic-gate 		    DDI_SUCCESS) {
5350Sstevel@tonic-gate 			cmn_err(CE_WARN,
5360Sstevel@tonic-gate 			    "fdc_attach: cannot get iblock cookie");
5370Sstevel@tonic-gate 			goto no_attach;
5380Sstevel@tonic-gate 		}
5390Sstevel@tonic-gate 		mutex_init(&fcp->c_lock, NULL, MUTEX_DRIVER, fcp->c_iblock);
5400Sstevel@tonic-gate 		intr_set = 1;
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 		/* setup interrupt handler */
5430Sstevel@tonic-gate 		if (ddi_add_intr(dip, (uint_t)0, NULL,
5440Sstevel@tonic-gate 		    (ddi_idevice_cookie_t *)0, fdc_intr, (caddr_t)fcp) !=
5450Sstevel@tonic-gate 		    DDI_SUCCESS) {
5460Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdc: cannot add intr\n");
5470Sstevel@tonic-gate 			goto no_attach;
5480Sstevel@tonic-gate 		}
5490Sstevel@tonic-gate 		intr_set++;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 		/*
5520Sstevel@tonic-gate 		 * acquire the DMA channel
5530Sstevel@tonic-gate 		 * this assumes that the chnl is not shared; else allocate
5540Sstevel@tonic-gate 		 * and free the chnl with each fdc request
5550Sstevel@tonic-gate 		 */
5560Sstevel@tonic-gate 		if (ddi_dmae_alloc(dip, fcp->c_dmachan, DDI_DMA_DONTWAIT, NULL)
5570Sstevel@tonic-gate 		    != DDI_SUCCESS) {
5580Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdc: cannot acquire dma%d\n",
5590Sstevel@tonic-gate 			    fcp->c_dmachan);
5600Sstevel@tonic-gate 			goto no_attach;
5610Sstevel@tonic-gate 		}
5620Sstevel@tonic-gate 		(void) ddi_dmae_getattr(dip, &fdc_dma_attr);
563940Smrj 		fdc_dma_attr.dma_attr_align = MMU_PAGESIZE;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 		mutex_init(&fcp->c_dorlock, NULL, MUTEX_DRIVER, fcp->c_iblock);
5660Sstevel@tonic-gate 		cv_init(&fcp->c_iocv, NULL, CV_DRIVER, fcp->c_iblock);
5670Sstevel@tonic-gate 		sema_init(&fcp->c_selsem, 1, NULL, SEMA_DRIVER, NULL);
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 		(void) sprintf(name, "fdc%d", ctlr);
5700Sstevel@tonic-gate 		fcp->c_intrstat = kstat_create("fdc", ctlr, name,
5710Sstevel@tonic-gate 		    "controller", KSTAT_TYPE_INTR, 1, KSTAT_FLAG_PERSISTENT);
5720Sstevel@tonic-gate 		if (fcp->c_intrstat) {
5730Sstevel@tonic-gate 			kstat_install(fcp->c_intrstat);
5740Sstevel@tonic-gate 		}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 		ddi_set_driver_private(dip, fcp);
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 		/*
5790Sstevel@tonic-gate 		 * reset the controller
5800Sstevel@tonic-gate 		 */
5810Sstevel@tonic-gate 		sema_p(&fcp->c_selsem);
5820Sstevel@tonic-gate 		mutex_enter(&fcp->c_lock);
5830Sstevel@tonic-gate 		fcp->c_csb.csb_xstate = FXS_RESET;
5840Sstevel@tonic-gate 		fcp->c_flags |= FCFLG_WAITING;
5850Sstevel@tonic-gate 		fdcquiesce(fcp);
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 		/* first test for mode == Model 30 */
5880Sstevel@tonic-gate 		fcp->c_mode = (inb(fcp->c_regbase + FCR_SRB) & 0x1c) ?
5890Sstevel@tonic-gate 		    FDCMODE_AT : FDCMODE_30;
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 		while (fcp->c_flags & FCFLG_WAITING) {
5920Sstevel@tonic-gate 			cv_wait(&fcp->c_iocv, &fcp->c_lock);
5930Sstevel@tonic-gate 		}
5940Sstevel@tonic-gate 		mutex_exit(&fcp->c_lock);
5950Sstevel@tonic-gate 		sema_v(&fcp->c_selsem);
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 		fdc_propinit2(fcp, cntlr_num);
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 		ddi_report_dev(dip);
6000Sstevel@tonic-gate 		return (DDI_SUCCESS);
6010Sstevel@tonic-gate 
602*5295Srandyf 	case DDI_RESUME:
603*5295Srandyf 		return (DDI_SUCCESS);
604*5295Srandyf 		/* break; */
605*5295Srandyf 
6060Sstevel@tonic-gate 	default:
6070Sstevel@tonic-gate 		return (DDI_FAILURE);
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate no_attach:
6110Sstevel@tonic-gate 	if (intr_set) {
6120Sstevel@tonic-gate 		if (intr_set > 1)
6130Sstevel@tonic-gate 			ddi_remove_intr(dip, 0, fcp->c_iblock);
6140Sstevel@tonic-gate 		mutex_destroy(&fcp->c_lock);
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 	ddi_soft_state_free(fdc_state_head, cntlr_num);
6170Sstevel@tonic-gate 	return (DDI_FAILURE);
6180Sstevel@tonic-gate }
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate static int
6210Sstevel@tonic-gate fdc_propinit1(struct fdcntlr *fcp, int cntlr)
6220Sstevel@tonic-gate {
6230Sstevel@tonic-gate 	dev_info_t *dip;
6240Sstevel@tonic-gate 	int len;
6250Sstevel@tonic-gate 	int value;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	dip = fcp->c_dip;
6280Sstevel@tonic-gate 	len = sizeof (value);
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	if (get_ioaddr(dip, &value) != DDI_SUCCESS)
6310Sstevel@tonic-gate 		return (DDI_FAILURE);
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	fcp->c_regbase = (ushort_t)value;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
6360Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "dma-channels", (caddr_t)&value, &len)
6370Sstevel@tonic-gate 	    != DDI_PROP_SUCCESS) {
6380Sstevel@tonic-gate 			cmn_err(CE_WARN,
6390Sstevel@tonic-gate 			    "fdc_attach: Error, could not find a dma channel");
6400Sstevel@tonic-gate 			return (DDI_FAILURE);
6410Sstevel@tonic-gate 	}
6420Sstevel@tonic-gate 	fcp->c_dmachan = (ushort_t)value;
6430Sstevel@tonic-gate 	fcp->c_number = cntlr;
6440Sstevel@tonic-gate 	return (DDI_SUCCESS);
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate /* ARGSUSED */
6480Sstevel@tonic-gate static void
6490Sstevel@tonic-gate fdc_propinit2(struct fdcntlr *fcp, int cntlr)
6500Sstevel@tonic-gate {
6510Sstevel@tonic-gate 	dev_info_t *dip;
6520Sstevel@tonic-gate 	int ccr;
6530Sstevel@tonic-gate 	int len;
6540Sstevel@tonic-gate 	int value;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	dip = fcp->c_dip;
6570Sstevel@tonic-gate 	len = sizeof (value);
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	if (ddi_prop_op(DDI_DEV_T_ANY, dip, PROP_LEN_AND_VAL_BUF,
6600Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "chip", (caddr_t)&value, &len)
6610Sstevel@tonic-gate 	    == DDI_PROP_SUCCESS)
6620Sstevel@tonic-gate 		fcp->c_chip = value;
6630Sstevel@tonic-gate 	else {
6640Sstevel@tonic-gate 		static uchar_t perpindcmd[2] = {FO_PERP, 0};
6650Sstevel@tonic-gate 		static uchar_t versioncmd = FO_VRSN;
6660Sstevel@tonic-gate 		uchar_t result;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 		fcp->c_chip = i8272A;
6690Sstevel@tonic-gate 		(void) fdc_docmd(fcp, &versioncmd, 1);
6700Sstevel@tonic-gate 		/*
6710Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_docmd.
6720Sstevel@tonic-gate 		 * fdc_results retrieves the controller/drive status
6730Sstevel@tonic-gate 		 */
6740Sstevel@tonic-gate 		if (!fdc_result(fcp, &result, 1) && result == 0x90) {
6750Sstevel@tonic-gate 			/*
6760Sstevel@tonic-gate 			 * try a perpendicular_mode cmd to ensure
6770Sstevel@tonic-gate 			 * that we really have an enhanced controller
6780Sstevel@tonic-gate 			 */
6790Sstevel@tonic-gate 			if (fdc_docmd(fcp, perpindcmd, 2) ||
6800Sstevel@tonic-gate 			    fdc_docmd(fcp, configurecmd, 4))
6810Sstevel@tonic-gate 				/*
6820Sstevel@tonic-gate 				 * perpindicular_mode will be rejected by
6830Sstevel@tonic-gate 				 * older controllers; make sure we don't hang.
6840Sstevel@tonic-gate 				 */
6850Sstevel@tonic-gate 				(void) fdc_result(fcp, &result, 1);
6860Sstevel@tonic-gate 				/*
6870Sstevel@tonic-gate 				 * Ignored return. If failed, warning was
6880Sstevel@tonic-gate 				 * issued by fdc_result.
6890Sstevel@tonic-gate 				 */
6900Sstevel@tonic-gate 			else
6910Sstevel@tonic-gate 				/* enhanced type controller */
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 				if ((fcp->c_chip = fdc_enhance_probe(fcp)) == 0)
6940Sstevel@tonic-gate 					/* default enhanced cntlr */
6950Sstevel@tonic-gate 					fcp->c_chip = i82077;
6960Sstevel@tonic-gate 		}
6970Sstevel@tonic-gate 		(void) ddi_prop_update_int(DDI_DEV_T_NONE, dip,
6980Sstevel@tonic-gate 		    "chip", fcp->c_chip);
6990Sstevel@tonic-gate 		/*
7000Sstevel@tonic-gate 		 * Ignoring return value because, for passed arguments, only
7010Sstevel@tonic-gate 		 * DDI_SUCCESS is returned.
7020Sstevel@tonic-gate 		 */
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 	if (fcp->c_chip >= i82077 && fcp->c_mode == FDCMODE_30 &&
7050Sstevel@tonic-gate 	    (inb(fcp->c_regbase + FCR_DIR) & 0x70) == 0)
7060Sstevel@tonic-gate 		for (ccr = 0; ccr <= (FCC_NOPREC | FCC_DRATE); ccr++) {
7070Sstevel@tonic-gate 			/*
7080Sstevel@tonic-gate 			 * run through all the combinations of NOPREC and
7090Sstevel@tonic-gate 			 * datarate selection, and see if they show up in the
7100Sstevel@tonic-gate 			 * Model 30 DIR
7110Sstevel@tonic-gate 			 */
7120Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_CCR, ccr);
7130Sstevel@tonic-gate 			drv_usecwait(5);
7140Sstevel@tonic-gate 			if ((inb(fcp->c_regbase + FCR_DIR) &
7150Sstevel@tonic-gate 			    (FCC_NOPREC | FCC_DRATE)) != ccr) {
7160Sstevel@tonic-gate 				fcp->c_mode = FDCMODE_AT;
7170Sstevel@tonic-gate 				break;
7180Sstevel@tonic-gate 			}
7190Sstevel@tonic-gate 		}
7200Sstevel@tonic-gate 	else
7210Sstevel@tonic-gate 		fcp->c_mode = FDCMODE_AT;
7220Sstevel@tonic-gate 	outb(fcp->c_regbase + FCR_CCR, 0);
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate /* ARGSUSED */
7260Sstevel@tonic-gate static int
7270Sstevel@tonic-gate fdc_enhance_probe(struct fdcntlr *fcp)
7280Sstevel@tonic-gate {
7290Sstevel@tonic-gate 	static uchar_t nsccmd = FO_NSC;
7300Sstevel@tonic-gate 	uint_t	ddic;
7310Sstevel@tonic-gate 	int	retcode = 0;
7320Sstevel@tonic-gate 	uchar_t	result;
7330Sstevel@tonic-gate 	uchar_t	save;
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	/*
7360Sstevel@tonic-gate 	 * Try to identify the enhanced floppy controller.
7370Sstevel@tonic-gate 	 * This is required so that we can program the DENSEL output to
7380Sstevel@tonic-gate 	 * control 3D mode (1.0 MB, 1.6 MB and 2.0 MB unformatted capacity,
7390Sstevel@tonic-gate 	 * 720 KB, 1.2 MB, and 1.44 MB formatted capacity) 3.5" dual-speed
7400Sstevel@tonic-gate 	 * floppy drives.  Refer to bugid 1195155.
7410Sstevel@tonic-gate 	 */
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	(void) fdc_docmd(fcp, &nsccmd, 1);
7440Sstevel@tonic-gate 	/*
7450Sstevel@tonic-gate 	 * Ignored return. If failed, warning was issued by fdc_docmd.
7460Sstevel@tonic-gate 	 * fdc_results retrieves the controller/drive status
7470Sstevel@tonic-gate 	 */
7480Sstevel@tonic-gate 	if (!fdc_result(fcp, &result, 1) && result != S0_IVCMD) {
7490Sstevel@tonic-gate 		/*
7500Sstevel@tonic-gate 		 * only enhanced National Semi PC8477 core
7510Sstevel@tonic-gate 		 * should respond to this command
7520Sstevel@tonic-gate 		 */
7530Sstevel@tonic-gate 		if ((result & 0xf0) == 0x70) {
7540Sstevel@tonic-gate 			/* low 4 bits may change */
7550Sstevel@tonic-gate 			fcp->c_flags |= FCFLG_3DMODE;
7560Sstevel@tonic-gate 			retcode = PC87322;
7570Sstevel@tonic-gate 		} else
7580Sstevel@tonic-gate 			cmn_err(CE_CONT,
7590Sstevel@tonic-gate "?fdc: unidentified, enhanced, National Semiconductor cntlr %x\n", result);
7600Sstevel@tonic-gate 	} else {
7610Sstevel@tonic-gate 		save = inb(fcp->c_regbase + FCR_SRA);
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 		do {
7640Sstevel@tonic-gate 			/* probe for motherboard version of SMC cntlr */
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 			/* try to enable configuration mode */
7670Sstevel@tonic-gate 			ddic = ddi_enter_critical();
7680Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, FSA_ENA5);
7690Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, FSA_ENA5);
7700Sstevel@tonic-gate 			ddi_exit_critical(ddic);
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, 0x0F);
7730Sstevel@tonic-gate 			if (inb(fcp->c_regbase + FCR_SRB) != 0x00)
7740Sstevel@tonic-gate 				/* always expect 0 from config reg F */
7750Sstevel@tonic-gate 				break;
7760Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, 0x0D);
7770Sstevel@tonic-gate 			if (inb(fcp->c_regbase + FCR_SRB) != 0x65)
7780Sstevel@tonic-gate 				/* expect 0x65 from config reg D */
7790Sstevel@tonic-gate 				break;
7800Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, 0x0E);
7810Sstevel@tonic-gate 			result = inb(fcp->c_regbase + FCR_SRB);
7820Sstevel@tonic-gate 			if (result != 0x02) {
7830Sstevel@tonic-gate 				/* expect revision level 2 from config reg E */
7840Sstevel@tonic-gate 				cmn_err(CE_CONT,
7850Sstevel@tonic-gate "?fdc: unidentified, enhanced, SMC cntlr revision %x\n", result);
7860Sstevel@tonic-gate 				/* break;	*/
7870Sstevel@tonic-gate 			}
7880Sstevel@tonic-gate 			fcp->c_flags |= FCFLG_3DMODE;
7890Sstevel@tonic-gate 			retcode = FDC37C665;
7900Sstevel@tonic-gate 		} while (retcode == 0);
7910Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_SRA, FSA_DISB);
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 		while (retcode == 0) {
7940Sstevel@tonic-gate 			/* probe for adapter version of SMC cntlr */
7950Sstevel@tonic-gate 			ddic = ddi_enter_critical();
7960Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, FSA_ENA6);
7970Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, FSA_ENA6);
7980Sstevel@tonic-gate 			ddi_exit_critical(ddic);
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, 0x0F);
8010Sstevel@tonic-gate 			if (inb(fcp->c_regbase + FCR_SRB) != 0x00)
8020Sstevel@tonic-gate 				/* always expect 0 from config reg F */
8030Sstevel@tonic-gate 				break;
8040Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, 0x0D);
8050Sstevel@tonic-gate 			if (inb(fcp->c_regbase + FCR_SRB) != 0x66)
8060Sstevel@tonic-gate 				/* expect 0x66 from config reg D */
8070Sstevel@tonic-gate 				break;
8080Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_SRA, 0x0E);
8090Sstevel@tonic-gate 			result = inb(fcp->c_regbase + FCR_SRB);
8100Sstevel@tonic-gate 			if (result != 0x02) {
8110Sstevel@tonic-gate 				/* expect revision level 2 from config reg E */
8120Sstevel@tonic-gate 				cmn_err(CE_CONT,
8130Sstevel@tonic-gate "?fdc: unidentified, enhanced, SMC cntlr revision %x\n", result);
8140Sstevel@tonic-gate 				/* break;	*/
8150Sstevel@tonic-gate 			}
8160Sstevel@tonic-gate 			fcp->c_flags |= FCFLG_3DMODE;
8170Sstevel@tonic-gate 			retcode = FDC37C666;
8180Sstevel@tonic-gate 		}
8190Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_SRA, FSA_DISB);
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 		drv_usecwait(10);
8220Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_SRA, save);
8230Sstevel@tonic-gate 	}
8240Sstevel@tonic-gate 	return (retcode);
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate /* ARGSUSED */
8280Sstevel@tonic-gate static int
8290Sstevel@tonic-gate fdc_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
8300Sstevel@tonic-gate {
8310Sstevel@tonic-gate 	struct fdcntlr *fcp;
832*5295Srandyf 	struct fcu_obj *fjp;
8330Sstevel@tonic-gate 	int unit;
8340Sstevel@tonic-gate 	int rval = 0;
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	FCERRPRINT(FDEP_L3, FDEM_ATTA, (CE_WARN, "fdc_detach: dip %p",
837*5295Srandyf 	    (void*)dip));
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	fcp = ddi_get_driver_private(dip);
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	switch (cmd) {
8420Sstevel@tonic-gate 	case DDI_DETACH:
8430Sstevel@tonic-gate 		for (unit = 0; unit < NFDUN; unit++)
8440Sstevel@tonic-gate 			if ((fcp->c_unit[unit])->fj_dip) {
8450Sstevel@tonic-gate 				rval = EBUSY;
8460Sstevel@tonic-gate 				break;
8470Sstevel@tonic-gate 			}
8480Sstevel@tonic-gate 		kstat_delete(fcp->c_intrstat);
8490Sstevel@tonic-gate 		fcp->c_intrstat = NULL;
8500Sstevel@tonic-gate 		ddi_remove_intr(fcp->c_dip, 0, fcp->c_iblock);
8510Sstevel@tonic-gate 		if (ddi_dmae_release(fcp->c_dip, fcp->c_dmachan) !=
8520Sstevel@tonic-gate 		    DDI_SUCCESS)
8530Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdc_detach: dma release failed, "
854*5295Srandyf 			    "dip %p, dmachan %x\n",
855*5295Srandyf 			    (void*)fcp->c_dip, fcp->c_dmachan);
8560Sstevel@tonic-gate 		ddi_prop_remove_all(fcp->c_dip);
8570Sstevel@tonic-gate 		ddi_set_driver_private(fcp->c_dip, NULL);
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 		mutex_destroy(&fcp->c_lock);
8600Sstevel@tonic-gate 		mutex_destroy(&fcp->c_dorlock);
8610Sstevel@tonic-gate 		cv_destroy(&fcp->c_iocv);
8620Sstevel@tonic-gate 		sema_destroy(&fcp->c_selsem);
8630Sstevel@tonic-gate 		ddi_soft_state_free(fdc_state_head, ddi_get_instance(dip));
8640Sstevel@tonic-gate 		break;
865*5295Srandyf 
866*5295Srandyf 	case DDI_SUSPEND:
867*5295Srandyf 		/*
868*5295Srandyf 		 * Following code causes the fdc (floppy controller)
869*5295Srandyf 		 * to suspend as long as there are no floppy drives
870*5295Srandyf 		 * attached to it.
871*5295Srandyf 		 * At present the floppy driver does not support
872*5295Srandyf 		 * SUSPEND/RESUME.
873*5295Srandyf 		 *
874*5295Srandyf 		 * Check if any FD units are attached
875*5295Srandyf 		 *
876*5295Srandyf 		 * For now, SUSPEND/RESUME is not supported
877*5295Srandyf 		 * if a floppy drive is present.
878*5295Srandyf 		 * So if any FD unit is attached return DDI_FAILURE
879*5295Srandyf 		 */
880*5295Srandyf 		for (unit = 0; unit < NFDUN; unit++) {
881*5295Srandyf 			fjp = fcp->c_unit[unit];
882*5295Srandyf 			if (fjp->fj_flags & FUNIT_DRVATCH) {
883*5295Srandyf 				cmn_err(CE_WARN,
884*5295Srandyf 				    "fdc_detach: fd attached, failing SUSPEND");
885*5295Srandyf 				return (DDI_FAILURE);
886*5295Srandyf 			}
887*5295Srandyf 		}
888*5295Srandyf 
889*5295Srandyf 		cmn_err(CE_NOTE, "fdc_detach: SUSPEND fdc");
890*5295Srandyf 
891*5295Srandyf 		rval = DDI_SUCCESS;
892*5295Srandyf 		break;
893*5295Srandyf 
8940Sstevel@tonic-gate 	default:
8950Sstevel@tonic-gate 		rval = EINVAL;
8960Sstevel@tonic-gate 		break;
8970Sstevel@tonic-gate 	}
8980Sstevel@tonic-gate 	return (rval);
8990Sstevel@tonic-gate }
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate /* ARGSUSED */
9030Sstevel@tonic-gate int
9040Sstevel@tonic-gate fdc_start(struct fcu_obj *fjp)
9050Sstevel@tonic-gate {
9060Sstevel@tonic-gate 	return (ENOSYS);
9070Sstevel@tonic-gate }
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate int
9100Sstevel@tonic-gate fdc_abort(struct fcu_obj *fjp)
9110Sstevel@tonic-gate {
9120Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
9130Sstevel@tonic-gate 	int unit = fjp->fj_unit & 3;
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	FCERRPRINT(FDEP_L3, FDEM_RESE, (CE_WARN, "fdc_abort"));
9160Sstevel@tonic-gate 	if (fcp->c_curunit == unit) {
9170Sstevel@tonic-gate 		mutex_enter(&fcp->c_lock);
9180Sstevel@tonic-gate 		if (fcp->c_flags & FCFLG_WAITING) {
9190Sstevel@tonic-gate 			/*
9200Sstevel@tonic-gate 			 * this can cause data corruption !
9210Sstevel@tonic-gate 			 */
9220Sstevel@tonic-gate 			fdcquiesce(fcp);
9230Sstevel@tonic-gate 			fcp->c_csb.csb_xstate = FXS_RESET;
9240Sstevel@tonic-gate 			fcp->c_flags |= FCFLG_TIMEOUT;
9250Sstevel@tonic-gate 			if (ddi_dmae_stop(fcp->c_dip, fcp->c_dmachan) !=
9260Sstevel@tonic-gate 			    DDI_SUCCESS)
9270Sstevel@tonic-gate 				cmn_err(CE_WARN,
928*5295Srandyf 				    "fdc_detach: dma release failed, "
929*5295Srandyf 				    "dip %p, dmachan %x\n",
930*5295Srandyf 				    (void*)fcp->c_dip, fcp->c_dmachan);
9310Sstevel@tonic-gate 		}
9320Sstevel@tonic-gate 		mutex_exit(&fcp->c_lock);
9330Sstevel@tonic-gate 		drv_usecwait(500);
9340Sstevel@tonic-gate 		return (DDI_SUCCESS);
9350Sstevel@tonic-gate 	}
9360Sstevel@tonic-gate 	return (DDI_FAILURE);
9370Sstevel@tonic-gate }
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate /* ARGSUSED */
9400Sstevel@tonic-gate int
9410Sstevel@tonic-gate fdc_getcap(struct fcu_obj *fjp, char *a, int i)
9420Sstevel@tonic-gate {
9430Sstevel@tonic-gate 	return (ENOSYS);
9440Sstevel@tonic-gate }
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate /* ARGSUSED */
9470Sstevel@tonic-gate int
9480Sstevel@tonic-gate fdc_setcap(struct fcu_obj *fjp, char *a, int i, int j)
9490Sstevel@tonic-gate {
9500Sstevel@tonic-gate 	return (ENOSYS);
9510Sstevel@tonic-gate }
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate int
9540Sstevel@tonic-gate fdc_dkinfo(struct fcu_obj *fjp, struct dk_cinfo *dcp)
9550Sstevel@tonic-gate {
9560Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	(void) strncpy((char *)&dcp->dki_cname, ddi_get_name(fcp->c_dip),
959*5295Srandyf 	    DK_DEVLEN);
9600Sstevel@tonic-gate 	dcp->dki_ctype = DKC_UNKNOWN; /* no code for generic PC/AT fdc */
9610Sstevel@tonic-gate 	dcp->dki_flags = DKI_FMTTRK;
9620Sstevel@tonic-gate 	dcp->dki_addr = fcp->c_regbase;
9630Sstevel@tonic-gate 	dcp->dki_space = 0;
9640Sstevel@tonic-gate 	dcp->dki_prio = fcp->c_intprio;
9650Sstevel@tonic-gate 	dcp->dki_vec = fcp->c_intvec;
9660Sstevel@tonic-gate 	(void) strncpy((char *)&dcp->dki_dname, ddi_driver_name(fjp->fj_dip),
967*5295Srandyf 	    DK_DEVLEN);
9680Sstevel@tonic-gate 	dcp->dki_slave = fjp->fj_unit & 3;
9690Sstevel@tonic-gate 	dcp->dki_maxtransfer = maxphys / DEV_BSIZE;
9700Sstevel@tonic-gate 	return (DDI_SUCCESS);
9710Sstevel@tonic-gate }
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate /*
9740Sstevel@tonic-gate  * on=> non-zero = select, 0 = de-select
9750Sstevel@tonic-gate  */
9760Sstevel@tonic-gate /* ARGSUSED */
9770Sstevel@tonic-gate int
9780Sstevel@tonic-gate fdc_select(struct fcu_obj *fjp, int funit, int on)
9790Sstevel@tonic-gate {
9800Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
9810Sstevel@tonic-gate 	int unit = funit & 3;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	if (on) {
9840Sstevel@tonic-gate 		/* possess controller */
9850Sstevel@tonic-gate 		sema_p(&fcp->c_selsem);
9860Sstevel@tonic-gate 		FCERRPRINT(FDEP_L2, FDEM_DSEL,
9870Sstevel@tonic-gate 		    (CE_NOTE, "fdc_select unit %d: on", funit));
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 		if (fcp->c_curunit != unit || !(fjp->fj_flags & FUNIT_CHAROK)) {
9900Sstevel@tonic-gate 			fcp->c_curunit = unit;
9910Sstevel@tonic-gate 			fjp->fj_flags |= FUNIT_CHAROK;
9920Sstevel@tonic-gate 			if (fdcspecify(fcp,
9930Sstevel@tonic-gate 			    fjp->fj_chars->fdc_transfer_rate,
9940Sstevel@tonic-gate 			    fjp->fj_drive->fdd_steprate, 40))
9950Sstevel@tonic-gate 				cmn_err(CE_WARN,
9960Sstevel@tonic-gate 				    "fdc_select: controller setup rejected "
9970Sstevel@tonic-gate 				    "fdcntrl %p transfer rate %x step rate %x"
9980Sstevel@tonic-gate 				    " head load time 40\n", (void*)fcp,
9990Sstevel@tonic-gate 				    fjp->fj_chars->fdc_transfer_rate,
10000Sstevel@tonic-gate 				    fjp->fj_drive->fdd_steprate);
10010Sstevel@tonic-gate 		}
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 		mutex_enter(&fcp->c_dorlock);
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 		/* make sure drive is not selected in case we change speed */
10060Sstevel@tonic-gate 		fcp->c_digout = (fcp->c_digout & ~FD_DRSEL) |
10070Sstevel@tonic-gate 		    (~unit & FD_DRSEL);
10080Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_DOR, fcp->c_digout);
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 		(void) fdc_motorsm(fjp, FMI_STARTCMD,
10110Sstevel@tonic-gate 		    fjp->fj_drive->fdd_motoron);
10120Sstevel@tonic-gate 		/*
10130Sstevel@tonic-gate 		 * Return value ignored - fdcmotort deals with failure.
10140Sstevel@tonic-gate 		 */
10150Sstevel@tonic-gate 		if (fdcspdchange(fcp, fjp, fjp->fj_attr->fda_rotatespd)) {
10160Sstevel@tonic-gate 			/* 3D drive requires 500 ms for speed change */
10170Sstevel@tonic-gate 			(void) fdc_motorsm(fjp, FMI_RSTARTCMD, 5);
10180Sstevel@tonic-gate 			/*
10190Sstevel@tonic-gate 			 * Return value ignored - fdcmotort deals with failure.
10200Sstevel@tonic-gate 			 */
10210Sstevel@tonic-gate 		}
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 		fcp->c_digout = (fcp->c_digout & ~FD_DRSEL) | (unit & FD_DRSEL);
10240Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_DOR, fcp->c_digout);
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 		mutex_exit(&fcp->c_dorlock);
10270Sstevel@tonic-gate 		fcp->c_csb.csb_drive = (uchar_t)unit;
10280Sstevel@tonic-gate 	} else {
10290Sstevel@tonic-gate 		FCERRPRINT(FDEP_L2, FDEM_DSEL,
10300Sstevel@tonic-gate 		    (CE_NOTE, "fdc_select unit %d: off", funit));
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 		mutex_enter(&fcp->c_dorlock);
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 		fcp->c_digout |= FD_DRSEL;
10350Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_DOR, fcp->c_digout);
10360Sstevel@tonic-gate 		(void) fdc_motorsm(fjp, FMI_IDLECMD,
10370Sstevel@tonic-gate 		    fjp->fj_drive->fdd_motoroff);
10380Sstevel@tonic-gate 		/*
10390Sstevel@tonic-gate 		 * Return value ignored - fdcmotort deals with failure.
10400Sstevel@tonic-gate 		 */
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 		mutex_exit(&fcp->c_dorlock);
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 		/* give up controller */
10450Sstevel@tonic-gate 		sema_v(&fcp->c_selsem);
10460Sstevel@tonic-gate 	}
10470Sstevel@tonic-gate 	return (0);
10480Sstevel@tonic-gate }
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate int
10520Sstevel@tonic-gate fdgetchng(struct fcu_obj *fjp, int funit)
10530Sstevel@tonic-gate {
10540Sstevel@tonic-gate 	if (fdcsense_drv(fjp->fj_fdc, funit & 3))
10550Sstevel@tonic-gate 		cmn_err(CE_WARN, "fdgetchng: write protect check failed\n");
10560Sstevel@tonic-gate 	return (fdcsense_chng(fjp->fj_fdc, funit & 3));
10570Sstevel@tonic-gate }
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate int
10610Sstevel@tonic-gate fdresetchng(struct fcu_obj *fjp, int funit)
10620Sstevel@tonic-gate {
10630Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
10640Sstevel@tonic-gate 	int unit = funit & 3;
10650Sstevel@tonic-gate 	int newcyl;			/* where to seek for reset of DSKCHG */
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	FCERRPRINT(FDEP_L2, FDEM_CHEK, (CE_NOTE, "fdmediachng unit %d", funit));
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 	if (fcp->c_curpcyl[unit])
10700Sstevel@tonic-gate 		newcyl = fcp->c_curpcyl[unit] - 1;
10710Sstevel@tonic-gate 	else
10720Sstevel@tonic-gate 		newcyl = 1;
10730Sstevel@tonic-gate 	return (fdrecalseek(fjp, funit, newcyl, 0));
10740Sstevel@tonic-gate }
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate /*
10780Sstevel@tonic-gate  * fdrecalseek
10790Sstevel@tonic-gate  */
10800Sstevel@tonic-gate int
10810Sstevel@tonic-gate fdrecalseek(struct fcu_obj *fjp, int funit, int arg, int execflg)
10820Sstevel@tonic-gate {
10830Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
10840Sstevel@tonic-gate 	struct fdcsb *csb;
10850Sstevel@tonic-gate 	int unit = funit & 3;
10860Sstevel@tonic-gate 	int rval;
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	FCERRPRINT(FDEP_L2, FDEM_RECA, (CE_NOTE, "fdrecalseek unit %d to %d",
10890Sstevel@tonic-gate 	    funit, arg));
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	csb = &fcp->c_csb;
10920Sstevel@tonic-gate 	csb->csb_cmd[1] = (uchar_t)unit;
10930Sstevel@tonic-gate 	if (arg < 0) {			/* is recal... */
10940Sstevel@tonic-gate 		*csb->csb_cmd = FO_RECAL;
10950Sstevel@tonic-gate 		csb->csb_ncmds = 2;
10960Sstevel@tonic-gate 		csb->csb_timer = 28;
10970Sstevel@tonic-gate 	} else {
10980Sstevel@tonic-gate 		*csb->csb_cmd = FO_SEEK;
10990Sstevel@tonic-gate 		csb->csb_cmd[2] = (uchar_t)arg;
11000Sstevel@tonic-gate 		csb->csb_ncmds = 3;
11010Sstevel@tonic-gate 		csb->csb_timer = 10;
11020Sstevel@tonic-gate 	}
11030Sstevel@tonic-gate 	csb->csb_nrslts = 2;	/* 2 for SENSE INTERRUPTS */
11040Sstevel@tonic-gate 	csb->csb_opflags = CSB_OFINRPT;
11050Sstevel@tonic-gate 	csb->csb_maxretry = skretry;
11060Sstevel@tonic-gate 	csb->csb_dmahandle = NULL;
11070Sstevel@tonic-gate 	csb->csb_handle_bound = 0;
11080Sstevel@tonic-gate 	csb->csb_dmacookiecnt = 0;
11090Sstevel@tonic-gate 	csb->csb_dmacurrcookie = 0;
11100Sstevel@tonic-gate 	csb->csb_dmawincnt = 0;
11110Sstevel@tonic-gate 	csb->csb_dmacurrwin = 0;
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	/* send cmd off to fdc_exec */
11140Sstevel@tonic-gate 	if (rval = fdc_exec(fcp, 1, execflg))
11150Sstevel@tonic-gate 		goto out;
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	if (!(*csb->csb_rslt & S0_SEKEND) ||
11180Sstevel@tonic-gate 	    (*csb->csb_rslt & S0_ICMASK) ||
11190Sstevel@tonic-gate 	    ((*csb->csb_rslt & S0_ECHK) && arg < 0) ||
11200Sstevel@tonic-gate 	    csb->csb_cmdstat)
11210Sstevel@tonic-gate 		rval = ENODEV;
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	if (fdcsense_drv(fcp, unit))
11240Sstevel@tonic-gate 		cmn_err(CE_WARN, "fdgetchng: write protect check failed\n");
11250Sstevel@tonic-gate out:
11260Sstevel@tonic-gate 	return (rval);
11270Sstevel@tonic-gate }
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate /*
11310Sstevel@tonic-gate  * fdrw- used only for read/writing sectors into/from kernel buffers.
11320Sstevel@tonic-gate  */
11330Sstevel@tonic-gate int
11340Sstevel@tonic-gate fdrw(struct fcu_obj *fjp, int funit, int rw, int cyl, int head,
11350Sstevel@tonic-gate     int sector, caddr_t bufp, uint_t len)
11360Sstevel@tonic-gate {
11370Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
11380Sstevel@tonic-gate 	struct fdcsb *csb;
11390Sstevel@tonic-gate 	uint_t dmar_flags = 0;
11400Sstevel@tonic-gate 	int unit = funit & 3;
11410Sstevel@tonic-gate 	int rval;
1142940Smrj 	ddi_acc_handle_t mem_handle = NULL;
1143940Smrj 	caddr_t aligned_buf;
1144940Smrj 	size_t real_size;
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	FCERRPRINT(FDEP_L1, FDEM_RW, (CE_CONT, "fdrw unit %d\n", funit));
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	csb = &fcp->c_csb;
11490Sstevel@tonic-gate 	if (rw) {
11500Sstevel@tonic-gate 		dmar_flags = DDI_DMA_READ;
11510Sstevel@tonic-gate 		csb->csb_opflags = CSB_OFDMARD | CSB_OFINRPT;
11520Sstevel@tonic-gate 		*csb->csb_cmd = FO_MT | FO_MFM | FO_SK | FO_RDDAT;
11530Sstevel@tonic-gate 	} else { /* write */
11540Sstevel@tonic-gate 		dmar_flags = DDI_DMA_WRITE;
11550Sstevel@tonic-gate 		csb->csb_opflags = CSB_OFDMAWT | CSB_OFINRPT;
11560Sstevel@tonic-gate 		*csb->csb_cmd = FO_MT | FO_MFM | FO_WRDAT;
11570Sstevel@tonic-gate 	}
11580Sstevel@tonic-gate 	csb->csb_cmd[1] = (uchar_t)(unit | ((head & 0x1) << 2));
11590Sstevel@tonic-gate 	csb->csb_cmd[2] = (uchar_t)cyl;
11600Sstevel@tonic-gate 	csb->csb_cmd[3] = (uchar_t)head;
11610Sstevel@tonic-gate 	csb->csb_cmd[4] = (uchar_t)sector;
11620Sstevel@tonic-gate 	encode(sector_size, fjp->fj_chars->fdc_sec_size,
11630Sstevel@tonic-gate 	    &csb->csb_cmd[5]);
11640Sstevel@tonic-gate 	csb->csb_cmd[6] = (uchar_t)max(fjp->fj_chars->fdc_secptrack, sector);
11650Sstevel@tonic-gate 	csb->csb_cmd[7] = fjp->fj_attr->fda_gapl;
11660Sstevel@tonic-gate 	csb->csb_cmd[8] = 0xFF;
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	csb->csb_ncmds = 9;
11690Sstevel@tonic-gate 	csb->csb_nrslts = 7;
11700Sstevel@tonic-gate 	csb->csb_timer = 36;
11710Sstevel@tonic-gate 	if (rw == FDRDONE)
11720Sstevel@tonic-gate 		csb->csb_maxretry = 1;
11730Sstevel@tonic-gate 	else
11740Sstevel@tonic-gate 		csb->csb_maxretry = rwretry;
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 	csb->csb_dmahandle = NULL;
11770Sstevel@tonic-gate 	csb->csb_handle_bound = 0;
11780Sstevel@tonic-gate 	csb->csb_dmacookiecnt = 0;
11790Sstevel@tonic-gate 	csb->csb_dmacurrcookie = 0;
11800Sstevel@tonic-gate 	csb->csb_dmawincnt = 0;
11810Sstevel@tonic-gate 	csb->csb_dmacurrwin = 0;
11820Sstevel@tonic-gate 	dmar_flags |= (DDI_DMA_STREAMING | DDI_DMA_PARTIAL);
11830Sstevel@tonic-gate 
1184940Smrj 	if (ddi_dma_alloc_handle(fcp->c_dip, &fdc_dma_attr, DDI_DMA_SLEEP,
1185*5295Srandyf 	    0, &csb->csb_dmahandle) != DDI_SUCCESS) {
11860Sstevel@tonic-gate 		rval = EINVAL;
11870Sstevel@tonic-gate 		goto out;
11880Sstevel@tonic-gate 	}
11890Sstevel@tonic-gate 
1190940Smrj 	/*
1191940Smrj 	 * allocate a page aligned buffer to dma to/from. This way we can
1192940Smrj 	 * ensure the cookie is a whole multiple of granularity and avoids
1193940Smrj 	 * any alignment issues.
1194940Smrj 	 */
1195940Smrj 	rval = ddi_dma_mem_alloc(csb->csb_dmahandle, len, &fdc_accattr,
1196940Smrj 	    DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL, &aligned_buf,
1197940Smrj 	    &real_size, &mem_handle);
1198940Smrj 	if (rval != DDI_SUCCESS) {
1199940Smrj 		rval = EINVAL;
1200940Smrj 		goto out;
1201940Smrj 	}
1202940Smrj 
1203940Smrj 	if (dmar_flags & DDI_DMA_WRITE) {
1204940Smrj 		bcopy(bufp, aligned_buf, len);
1205940Smrj 	}
1206940Smrj 
1207940Smrj 	rval = ddi_dma_addr_bind_handle(csb->csb_dmahandle, NULL, aligned_buf,
1208940Smrj 	    len, dmar_flags, DDI_DMA_SLEEP, 0, &csb->csb_dmacookie,
1209940Smrj 	    &csb->csb_dmacookiecnt);
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	if (rval == DDI_DMA_MAPPED) {
12120Sstevel@tonic-gate 		csb->csb_dmawincnt = 1;
12130Sstevel@tonic-gate 		csb->csb_handle_bound = 1;
12140Sstevel@tonic-gate 	} else if (rval == DDI_DMA_PARTIAL_MAP) {
12150Sstevel@tonic-gate 		csb->csb_handle_bound = 1;
12160Sstevel@tonic-gate 		if (ddi_dma_numwin(csb->csb_dmahandle, &csb->csb_dmawincnt) !=
1217*5295Srandyf 		    DDI_SUCCESS) {
12180Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdrw: dma numwin failed\n");
12190Sstevel@tonic-gate 			rval = EINVAL;
12200Sstevel@tonic-gate 			goto out;
12210Sstevel@tonic-gate 		}
12220Sstevel@tonic-gate 	} else {
12230Sstevel@tonic-gate 		cmn_err(CE_WARN,
1224*5295Srandyf 		    "fdrw: dma addr bind handle failed, rval = %d\n", rval);
12250Sstevel@tonic-gate 		rval = EINVAL;
12260Sstevel@tonic-gate 		goto out;
12270Sstevel@tonic-gate 	}
12280Sstevel@tonic-gate 	rval = fdc_exec(fcp, 1, 1);
12290Sstevel@tonic-gate 
1230940Smrj 	if (dmar_flags & DDI_DMA_READ) {
1231940Smrj 		bcopy(aligned_buf, bufp, len);
1232940Smrj 	}
1233940Smrj 
12340Sstevel@tonic-gate out:
12350Sstevel@tonic-gate 	if (csb->csb_dmahandle) {
12360Sstevel@tonic-gate 		if (csb->csb_handle_bound) {
12370Sstevel@tonic-gate 			if (ddi_dma_unbind_handle(csb->csb_dmahandle) !=
12380Sstevel@tonic-gate 			    DDI_SUCCESS)
12390Sstevel@tonic-gate 				cmn_err(CE_WARN, "fdrw: "
12400Sstevel@tonic-gate 				    "dma unbind handle failed\n");
12410Sstevel@tonic-gate 			csb->csb_handle_bound = 0;
12420Sstevel@tonic-gate 		}
1243940Smrj 		if (mem_handle != NULL) {
1244940Smrj 			ddi_dma_mem_free(&mem_handle);
1245940Smrj 		}
12460Sstevel@tonic-gate 		ddi_dma_free_handle(&csb->csb_dmahandle);
12470Sstevel@tonic-gate 		csb->csb_dmahandle = NULL;
12480Sstevel@tonic-gate 	}
12490Sstevel@tonic-gate 	return (rval);
12500Sstevel@tonic-gate }
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate int
12540Sstevel@tonic-gate fdtrkformat(struct fcu_obj *fjp, int funit, int cyl, int head, int filldata)
12550Sstevel@tonic-gate {
12560Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
12570Sstevel@tonic-gate 	struct fdcsb *csb;
12580Sstevel@tonic-gate 	int unit = funit & 3;
12590Sstevel@tonic-gate 	int fmdatlen, lsector, lstart;
12600Sstevel@tonic-gate 	int interleave, numsctr, offset, psector;
12610Sstevel@tonic-gate 	uchar_t *dp;
12620Sstevel@tonic-gate 	int rval;
1263940Smrj 	ddi_acc_handle_t mem_handle = NULL;
1264940Smrj 	caddr_t aligned_buf;
1265940Smrj 	size_t real_size;
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	FCERRPRINT(FDEP_L2, FDEM_FORM,
12680Sstevel@tonic-gate 	    (CE_NOTE, "fdformattrk unit %d cyl=%d, hd=%d", funit, cyl, head));
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	csb = &fcp->c_csb;
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 	csb->csb_opflags = CSB_OFDMAWT | CSB_OFINRPT;
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 	*csb->csb_cmd = FO_FRMT | FO_MFM;
12750Sstevel@tonic-gate 	csb->csb_cmd[1] = (head << 2) | unit;
12760Sstevel@tonic-gate 	encode(sector_size, fjp->fj_chars->fdc_sec_size,
12770Sstevel@tonic-gate 	    &csb->csb_cmd[2]);
12780Sstevel@tonic-gate 	csb->csb_cmd[3] = numsctr = fjp->fj_chars->fdc_secptrack;
12790Sstevel@tonic-gate 	csb->csb_cmd[4] = fjp->fj_attr->fda_gapf;
12800Sstevel@tonic-gate 	csb->csb_cmd[5] = (uchar_t)filldata;
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	csb->csb_npcyl = (uchar_t)(cyl * fjp->fj_chars->fdc_steps);
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	csb->csb_dmahandle = NULL;
12850Sstevel@tonic-gate 	csb->csb_handle_bound = 0;
12860Sstevel@tonic-gate 	csb->csb_dmacookiecnt = 0;
12870Sstevel@tonic-gate 	csb->csb_dmacurrcookie = 0;
12880Sstevel@tonic-gate 	csb->csb_dmawincnt = 0;
12890Sstevel@tonic-gate 	csb->csb_dmacurrwin = 0;
12900Sstevel@tonic-gate 	csb->csb_ncmds = 6;
12910Sstevel@tonic-gate 	csb->csb_nrslts = 7;
12920Sstevel@tonic-gate 	csb->csb_timer = 32;
12930Sstevel@tonic-gate 	csb->csb_maxretry = rwretry;
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 	/*
1296940Smrj 	 * alloc space for format track cmd
12970Sstevel@tonic-gate 	 */
12980Sstevel@tonic-gate 	/*
12990Sstevel@tonic-gate 	 * NOTE: have to add size of fifo also - for dummy format action
13000Sstevel@tonic-gate 	 */
13010Sstevel@tonic-gate 	fmdatlen = 4 * numsctr;
1302940Smrj 
1303940Smrj 	if (ddi_dma_alloc_handle(fcp->c_dip, &fdc_dma_attr, DDI_DMA_SLEEP,
1304*5295Srandyf 	    0, &csb->csb_dmahandle) != DDI_SUCCESS) {
1305940Smrj 		rval = EINVAL;
1306940Smrj 		goto out;
1307940Smrj 	}
1308940Smrj 
1309940Smrj 	/*
1310940Smrj 	 * allocate a page aligned buffer to dma to/from. This way we can
1311940Smrj 	 * ensure the cookie is a whole multiple of granularity and avoids
1312940Smrj 	 * any alignment issues.
1313940Smrj 	 */
1314940Smrj 	rval = ddi_dma_mem_alloc(csb->csb_dmahandle, fmdatlen, &fdc_accattr,
1315940Smrj 	    DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL, &aligned_buf,
1316940Smrj 	    &real_size, &mem_handle);
1317940Smrj 	if (rval != DDI_SUCCESS) {
1318940Smrj 		rval = EINVAL;
1319940Smrj 		goto out;
1320940Smrj 	}
1321940Smrj 	dp = (uchar_t *)aligned_buf;
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	interleave = fjp->fj_attr->fda_intrlv;
13240Sstevel@tonic-gate 	offset = (numsctr + interleave - 1) / interleave;
13250Sstevel@tonic-gate 	for (psector = lstart = 1;
13260Sstevel@tonic-gate 	    psector <= numsctr; psector += interleave, lstart++) {
13270Sstevel@tonic-gate 		for (lsector = lstart; lsector <= numsctr; lsector += offset) {
13280Sstevel@tonic-gate 			*dp++ = (uchar_t)cyl;
13290Sstevel@tonic-gate 			*dp++ = (uchar_t)head;
13300Sstevel@tonic-gate 			*dp++ = (uchar_t)lsector;
13310Sstevel@tonic-gate 			*dp++ = csb->csb_cmd[2];
13320Sstevel@tonic-gate 		}
13330Sstevel@tonic-gate 	}
13340Sstevel@tonic-gate 
1335940Smrj 	rval = ddi_dma_addr_bind_handle(csb->csb_dmahandle, NULL, aligned_buf,
1336940Smrj 	    fmdatlen, DDI_DMA_WRITE | DDI_DMA_STREAMING | DDI_DMA_PARTIAL,
1337940Smrj 	    DDI_DMA_SLEEP, 0, &csb->csb_dmacookie, &csb->csb_dmacookiecnt);
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	if (rval == DDI_DMA_MAPPED) {
13400Sstevel@tonic-gate 		csb->csb_dmawincnt = 1;
13410Sstevel@tonic-gate 		csb->csb_handle_bound = 1;
13420Sstevel@tonic-gate 	} else if (rval == DDI_DMA_PARTIAL_MAP) {
13430Sstevel@tonic-gate 		csb->csb_handle_bound = 1;
13440Sstevel@tonic-gate 		if (ddi_dma_numwin(csb->csb_dmahandle, &csb->csb_dmawincnt) !=
1345*5295Srandyf 		    DDI_SUCCESS) {
13460Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdtrkformat: dma numwin failed\n");
13470Sstevel@tonic-gate 			rval = EINVAL;
13480Sstevel@tonic-gate 			goto out;
13490Sstevel@tonic-gate 		}
13500Sstevel@tonic-gate 	} else {
13510Sstevel@tonic-gate 		cmn_err(CE_WARN,
1352*5295Srandyf 		    "fdtrkformat: dma buf bind handle failed, rval = %d\n",
1353*5295Srandyf 		    rval);
13540Sstevel@tonic-gate 		rval = EINVAL;
13550Sstevel@tonic-gate 		goto out;
13560Sstevel@tonic-gate 	}
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 	rval = fdc_exec(fcp, 1, 1);
13590Sstevel@tonic-gate out:
13600Sstevel@tonic-gate 	if (csb->csb_dmahandle) {
13610Sstevel@tonic-gate 		if (csb->csb_handle_bound) {
13620Sstevel@tonic-gate 			if (ddi_dma_unbind_handle(csb->csb_dmahandle) !=
13630Sstevel@tonic-gate 			    DDI_SUCCESS)
13640Sstevel@tonic-gate 				cmn_err(CE_WARN, "fdtrkformat: "
13650Sstevel@tonic-gate 				    "dma unbind handle failed\n");
13660Sstevel@tonic-gate 			csb->csb_handle_bound = 0;
13670Sstevel@tonic-gate 		}
1368940Smrj 		if (mem_handle != NULL) {
1369940Smrj 			ddi_dma_mem_free(&mem_handle);
1370940Smrj 		}
13710Sstevel@tonic-gate 		ddi_dma_free_handle(&csb->csb_dmahandle);
13720Sstevel@tonic-gate 		csb->csb_dmahandle = NULL;
13730Sstevel@tonic-gate 	}
13740Sstevel@tonic-gate 	return (rval);
13750Sstevel@tonic-gate }
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate /* ARGSUSED */
13780Sstevel@tonic-gate int
13790Sstevel@tonic-gate fdrawioctl(struct fcu_obj *fjp, int funit, caddr_t arg)
13800Sstevel@tonic-gate {
13810Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
13820Sstevel@tonic-gate 	struct fd_raw *fdrp = (struct fd_raw *)arg;
13830Sstevel@tonic-gate 	struct fdcsb *csb;
13840Sstevel@tonic-gate 	uint_t dmar_flags = 0;
13850Sstevel@tonic-gate 	int i;
13860Sstevel@tonic-gate 	int change = 1;
13870Sstevel@tonic-gate 	int sleep = 1;
13880Sstevel@tonic-gate 	int rval = 0;
13890Sstevel@tonic-gate 	int rval_exec = 0;
1390940Smrj 	ddi_acc_handle_t mem_handle = NULL;
1391940Smrj 	caddr_t aligned_buf;
1392940Smrj 	size_t real_size;
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	FCERRPRINT(FDEP_L2, FDEM_RAWI,
13950Sstevel@tonic-gate 	    (CE_NOTE, "fdrawioctl: cmd[0]=0x%x", fdrp->fdr_cmd[0]));
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	csb = &fcp->c_csb;
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	/* copy cmd bytes into csb */
14000Sstevel@tonic-gate 	for (i = 0; i <= fdrp->fdr_cnum; i++)
14010Sstevel@tonic-gate 		csb->csb_cmd[i] = fdrp->fdr_cmd[i];
14020Sstevel@tonic-gate 	csb->csb_ncmds = (uchar_t)fdrp->fdr_cnum;
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 	csb->csb_maxretry = 0;	/* let the application deal with errors */
14050Sstevel@tonic-gate 	csb->csb_opflags = CSB_OFRAWIOCTL;
14060Sstevel@tonic-gate 	csb->csb_nrslts = 0;
14070Sstevel@tonic-gate 	csb->csb_timer = 50;
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 	switch (fdrp->fdr_cmd[0] & 0x0f) {
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	case FO_SEEK:
14120Sstevel@tonic-gate 		change = 0;
14130Sstevel@tonic-gate 		/* FALLTHROUGH */
14140Sstevel@tonic-gate 	case FO_RECAL:
14150Sstevel@tonic-gate 		csb->csb_opflags |= CSB_OFINRPT;
14160Sstevel@tonic-gate 		break;
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 	case FO_FRMT:
14190Sstevel@tonic-gate 		csb->csb_npcyl = *(uchar_t *)(fdrp->fdr_addr) *
14200Sstevel@tonic-gate 		    fjp->fj_chars->fdc_steps;
14210Sstevel@tonic-gate 		/* FALLTHROUGH */
14220Sstevel@tonic-gate 	case FO_WRDAT:
14230Sstevel@tonic-gate 	case FO_WRDEL:
14240Sstevel@tonic-gate 		csb->csb_opflags |= CSB_OFDMAWT | CSB_OFRESLT | CSB_OFINRPT;
14250Sstevel@tonic-gate 		csb->csb_nrslts = 7;
14260Sstevel@tonic-gate 		if (fdrp->fdr_nbytes == 0)
14270Sstevel@tonic-gate 			return (EINVAL);
14280Sstevel@tonic-gate 		dmar_flags = DDI_DMA_WRITE;
14290Sstevel@tonic-gate 		break;
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 	case FO_RDDAT:
14320Sstevel@tonic-gate 	case FO_RDDEL:
14330Sstevel@tonic-gate 	case FO_RDTRK:
14340Sstevel@tonic-gate 		csb->csb_opflags |= CSB_OFDMARD | CSB_OFRESLT | CSB_OFINRPT;
14350Sstevel@tonic-gate 		csb->csb_nrslts = 7;
14360Sstevel@tonic-gate 		dmar_flags = DDI_DMA_READ;
14370Sstevel@tonic-gate 		break;
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 	case FO_RDID:
14400Sstevel@tonic-gate 		csb->csb_opflags |= CSB_OFRESLT | CSB_OFINRPT;
14410Sstevel@tonic-gate 		csb->csb_nrslts = 7;
14420Sstevel@tonic-gate 		break;
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 	case FO_SDRV:
14450Sstevel@tonic-gate 		sleep = 0;
14460Sstevel@tonic-gate 		csb->csb_nrslts = 1;
14470Sstevel@tonic-gate 		break;
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	case FO_SINT:
14500Sstevel@tonic-gate 		sleep = 0;
14510Sstevel@tonic-gate 		change = 0;
14520Sstevel@tonic-gate 		csb->csb_nrslts = 2;
14530Sstevel@tonic-gate 		break;
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 	case FO_SPEC:
14560Sstevel@tonic-gate 		sleep = 0;
14570Sstevel@tonic-gate 		change = 0;
14580Sstevel@tonic-gate 		break;
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	default:
14610Sstevel@tonic-gate 		return (EINVAL);
14620Sstevel@tonic-gate 	}
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 	csb->csb_dmahandle = NULL;
14650Sstevel@tonic-gate 	csb->csb_handle_bound = 0;
14660Sstevel@tonic-gate 	csb->csb_dmacookiecnt = 0;
14670Sstevel@tonic-gate 	csb->csb_dmacurrcookie = 0;
14680Sstevel@tonic-gate 	csb->csb_dmawincnt = 0;
14690Sstevel@tonic-gate 	csb->csb_dmacurrwin = 0;
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	if (csb->csb_opflags & (CSB_OFDMARD | CSB_OFDMAWT)) {
1472940Smrj 		if (ddi_dma_alloc_handle(fcp->c_dip, &fdc_dma_attr,
1473940Smrj 		    DDI_DMA_SLEEP, 0, &csb->csb_dmahandle) != DDI_SUCCESS) {
14740Sstevel@tonic-gate 			rval = EINVAL;
14750Sstevel@tonic-gate 			goto out;
14760Sstevel@tonic-gate 		}
14770Sstevel@tonic-gate 
1478940Smrj 		/*
1479940Smrj 		 * allocate a page aligned buffer to dma to/from. This way we
1480940Smrj 		 * can ensure the cookie is a whole multiple of granularity and
1481940Smrj 		 * avoids any alignment issues.
1482940Smrj 		 */
1483940Smrj 		rval = ddi_dma_mem_alloc(csb->csb_dmahandle,
1484940Smrj 		    (uint_t)fdrp->fdr_nbytes, &fdc_accattr, DDI_DMA_CONSISTENT,
1485940Smrj 		    DDI_DMA_SLEEP, NULL, &aligned_buf, &real_size, &mem_handle);
1486940Smrj 		if (rval != DDI_SUCCESS) {
1487940Smrj 			rval = EINVAL;
1488940Smrj 			goto out;
1489940Smrj 		}
1490940Smrj 
1491940Smrj 		if (dmar_flags & DDI_DMA_WRITE) {
1492940Smrj 			bcopy(fdrp->fdr_addr, aligned_buf,
1493940Smrj 			    (uint_t)fdrp->fdr_nbytes);
1494940Smrj 		}
1495940Smrj 
14960Sstevel@tonic-gate 		dmar_flags |= (DDI_DMA_STREAMING | DDI_DMA_PARTIAL);
14970Sstevel@tonic-gate 		rval = ddi_dma_addr_bind_handle(csb->csb_dmahandle, NULL,
1498940Smrj 		    aligned_buf, (uint_t)fdrp->fdr_nbytes, dmar_flags,
1499940Smrj 		    DDI_DMA_SLEEP, 0, &csb->csb_dmacookie,
1500940Smrj 		    &csb->csb_dmacookiecnt);
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 		if (rval == DDI_DMA_MAPPED) {
15030Sstevel@tonic-gate 			csb->csb_dmawincnt = 1;
15040Sstevel@tonic-gate 			csb->csb_handle_bound = 1;
15050Sstevel@tonic-gate 		} else if (rval == DDI_DMA_PARTIAL_MAP) {
15060Sstevel@tonic-gate 			csb->csb_handle_bound = 1;
15070Sstevel@tonic-gate 			if (ddi_dma_numwin(csb->csb_dmahandle,
15080Sstevel@tonic-gate 			    &csb->csb_dmawincnt) != DDI_SUCCESS) {
15090Sstevel@tonic-gate 				cmn_err(CE_WARN,
15100Sstevel@tonic-gate 				    "fdrawioctl: dma numwin failed\n");
15110Sstevel@tonic-gate 				rval = EINVAL;
15120Sstevel@tonic-gate 				goto out;
15130Sstevel@tonic-gate 			}
15140Sstevel@tonic-gate 		} else {
15150Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdrawioctl: "
15160Sstevel@tonic-gate 			    "dma buf bind handle failed, rval = %d\n", rval);
15170Sstevel@tonic-gate 			rval = EINVAL;
15180Sstevel@tonic-gate 			goto out;
15190Sstevel@tonic-gate 		}
15200Sstevel@tonic-gate 	}
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 	FCERRPRINT(FDEP_L1, FDEM_RAWI,
15230Sstevel@tonic-gate 	    (CE_CONT, "cmd: %x %x %x %x %x %x %x %x %x %x\n", csb->csb_cmd[0],
15240Sstevel@tonic-gate 	    csb->csb_cmd[1], csb->csb_cmd[2], csb->csb_cmd[3],
15250Sstevel@tonic-gate 	    csb->csb_cmd[4], csb->csb_cmd[5], csb->csb_cmd[6],
15260Sstevel@tonic-gate 	    csb->csb_cmd[7], csb->csb_cmd[8], csb->csb_cmd[9]));
15270Sstevel@tonic-gate 	FCERRPRINT(FDEP_L1, FDEM_RAWI,
15280Sstevel@tonic-gate 	    (CE_CONT, "nbytes: %x, opflags: %x, addr: %p, len: %x\n",
15290Sstevel@tonic-gate 	    csb->csb_ncmds, csb->csb_opflags, (void *)fdrp->fdr_addr,
15300Sstevel@tonic-gate 	    fdrp->fdr_nbytes));
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	/*
15330Sstevel@tonic-gate 	 * Note that we ignore any error returns from fdexec.
15340Sstevel@tonic-gate 	 * This is the way the driver has been, and it may be
15350Sstevel@tonic-gate 	 * that the raw ioctl senders simply don't want to
15360Sstevel@tonic-gate 	 * see any errors returned in this fashion.
15370Sstevel@tonic-gate 	 */
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	/*
15400Sstevel@tonic-gate 	 * VP/ix sense drive ioctl call checks for the error return.
15410Sstevel@tonic-gate 	 */
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	rval_exec = fdc_exec(fcp, sleep, change);
15440Sstevel@tonic-gate 
1545940Smrj 	if (dmar_flags & DDI_DMA_READ) {
1546940Smrj 		bcopy(aligned_buf, fdrp->fdr_addr, (uint_t)fdrp->fdr_nbytes);
1547940Smrj 	}
1548940Smrj 
15490Sstevel@tonic-gate 	FCERRPRINT(FDEP_L1, FDEM_RAWI,
15500Sstevel@tonic-gate 	    (CE_CONT, "rslt: %x %x %x %x %x %x %x %x %x %x\n", csb->csb_rslt[0],
15510Sstevel@tonic-gate 	    csb->csb_rslt[1], csb->csb_rslt[2], csb->csb_rslt[3],
15520Sstevel@tonic-gate 	    csb->csb_rslt[4], csb->csb_rslt[5], csb->csb_rslt[6],
15530Sstevel@tonic-gate 	    csb->csb_rslt[7], csb->csb_rslt[8], csb->csb_rslt[9]));
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	/* copy results into fdr */
15560Sstevel@tonic-gate 	for (i = 0; i <= (int)csb->csb_nrslts; i++)
15570Sstevel@tonic-gate 		fdrp->fdr_result[i] = csb->csb_rslt[i];
15580Sstevel@tonic-gate /*	fdrp->fdr_nbytes = fdc->c_csb.csb_rlen;  return resid */
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate out:
15610Sstevel@tonic-gate 	if (csb->csb_dmahandle) {
15620Sstevel@tonic-gate 		if (csb->csb_handle_bound) {
15630Sstevel@tonic-gate 			if (ddi_dma_unbind_handle(csb->csb_dmahandle) !=
15640Sstevel@tonic-gate 			    DDI_SUCCESS)
15650Sstevel@tonic-gate 				cmn_err(CE_WARN, "fdrawioctl: "
15660Sstevel@tonic-gate 				    "dma unbind handle failed\n");
15670Sstevel@tonic-gate 			csb->csb_handle_bound = 0;
15680Sstevel@tonic-gate 		}
1569940Smrj 		if (mem_handle != NULL) {
1570940Smrj 			ddi_dma_mem_free(&mem_handle);
1571940Smrj 		}
15720Sstevel@tonic-gate 		ddi_dma_free_handle(&csb->csb_dmahandle);
15730Sstevel@tonic-gate 		csb->csb_dmahandle = NULL;
15740Sstevel@tonic-gate 	}
15750Sstevel@tonic-gate 	if ((fdrp->fdr_cmd[0] & 0x0f) == FO_SDRV) {
15760Sstevel@tonic-gate 		return (rval_exec);
15770Sstevel@tonic-gate 	}
15780Sstevel@tonic-gate 	return (rval);
15790Sstevel@tonic-gate }
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate void
15820Sstevel@tonic-gate encode(xlate_tbl_t *tablep, int val, uchar_t *rcode)
15830Sstevel@tonic-gate {
15840Sstevel@tonic-gate 	do {
15850Sstevel@tonic-gate 		if (tablep->value >= val) {
15860Sstevel@tonic-gate 			*rcode = tablep->code;
15870Sstevel@tonic-gate 			return;
15880Sstevel@tonic-gate 		}
15890Sstevel@tonic-gate 	} while ((++tablep)->value);
15900Sstevel@tonic-gate 	*rcode = tablep->code;
15910Sstevel@tonic-gate 	cmn_err(CE_WARN, "fdc encode failed, table %p val %x code %x\n",
15920Sstevel@tonic-gate 	    (void *)tablep, val, (uint_t)*rcode);
15930Sstevel@tonic-gate }
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate int
15960Sstevel@tonic-gate decode(xlate_tbl_t *tablep, int kode, int *rvalue)
15970Sstevel@tonic-gate {
15980Sstevel@tonic-gate 	do  {
15990Sstevel@tonic-gate 		if (tablep->code == kode) {
16000Sstevel@tonic-gate 			*rvalue = tablep->value;
16010Sstevel@tonic-gate 			return (0);
16020Sstevel@tonic-gate 		}
16030Sstevel@tonic-gate 	} while ((++tablep)->value);
16040Sstevel@tonic-gate 	return (-1);
16050Sstevel@tonic-gate }
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate void
16090Sstevel@tonic-gate fdcquiesce(struct fdcntlr *fcp)
16100Sstevel@tonic-gate {
16110Sstevel@tonic-gate 	int unit;
16120Sstevel@tonic-gate 
16130Sstevel@tonic-gate 	FCERRPRINT(FDEP_L2, FDEM_RESE, (CE_NOTE, "fdcquiesce fcp %p",
1614*5295Srandyf 	    (void*)fcp));
16150Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fcp->c_lock));
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	mutex_enter(&fcp->c_dorlock);
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	if (ddi_dmae_stop(fcp->c_dip, fcp->c_dmachan) != DDI_SUCCESS)
16200Sstevel@tonic-gate 		cmn_err(CE_WARN, "fdcquiesce: dmae stop failed, "
1621*5295Srandyf 		    "dip %p, dmachan %x\n",
1622*5295Srandyf 		    (void*)fcp->c_dip, fcp->c_dmachan);
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 	fcp->c_digout = (fcp->c_digout & (FD_DMTREN | FD_DRSEL)) | FD_ENABLE;
16250Sstevel@tonic-gate 	outb(fcp->c_regbase + FCR_DOR, fcp->c_digout);
16260Sstevel@tonic-gate 	drv_usecwait(20);
16270Sstevel@tonic-gate 	fcp->c_digout |= FD_RSETZ;
16280Sstevel@tonic-gate 	outb(fcp->c_regbase + FCR_DOR, fcp->c_digout);
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	mutex_exit(&fcp->c_dorlock);
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	/* count resets */
16330Sstevel@tonic-gate 	fcp->fdstats.reset++;
16340Sstevel@tonic-gate 	fcp->c_curunit = -1;
16350Sstevel@tonic-gate 	for (unit = 0; unit < NFDUN; unit++)
16360Sstevel@tonic-gate 		fcp->c_curpcyl[unit] = -1;
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate 	if (fcp->c_chip >= i82077) {
16390Sstevel@tonic-gate 		(void) fdc_docmd(fcp, configurecmd, 4);
16400Sstevel@tonic-gate 		/*
16410Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_docmd.
16420Sstevel@tonic-gate 		 */
16430Sstevel@tonic-gate 	}
16440Sstevel@tonic-gate }
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate void
16470Sstevel@tonic-gate fdcreadid(struct fdcntlr *fcp, struct fdcsb *csb)
16480Sstevel@tonic-gate {
16490Sstevel@tonic-gate 	static uchar_t readidcmd[2] = {FO_RDID | FO_MFM, 0};
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate 	readidcmd[1] = csb->csb_cmd[1];
16520Sstevel@tonic-gate 	(void) fdc_docmd(fcp, readidcmd, 2);
16530Sstevel@tonic-gate }
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate int
16560Sstevel@tonic-gate fdcseek(struct fdcntlr *fcp, int unit, int cyl)
16570Sstevel@tonic-gate {
16580Sstevel@tonic-gate 	static uchar_t seekabscmd[3] = {FO_SEEK, 0, 0};
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 	FCERRPRINT(FDEP_L0, FDEM_RECA, (CE_CONT, "fdcseek unit %d to cyl %d\n",
16610Sstevel@tonic-gate 	    unit, cyl));
16620Sstevel@tonic-gate 	seekabscmd[1] = (uchar_t)unit;
16630Sstevel@tonic-gate 	seekabscmd[2] = (uchar_t)cyl;
16640Sstevel@tonic-gate 	return (fdc_docmd(fcp, seekabscmd, 3));
16650Sstevel@tonic-gate }
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate /*
16680Sstevel@tonic-gate  * Returns status of disk change line of selected drive.
16690Sstevel@tonic-gate  *	= 0 means diskette is present
16700Sstevel@tonic-gate  *	!= 0 means diskette was removed and current state is unknown
16710Sstevel@tonic-gate  */
16720Sstevel@tonic-gate int
16730Sstevel@tonic-gate fdcsense_chng(struct fdcntlr *fcp, int unit)
16740Sstevel@tonic-gate {
16750Sstevel@tonic-gate 	int digital_input;
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 	FCERRPRINT(FDEP_L0, FDEM_SCHG,
16780Sstevel@tonic-gate 	    (CE_CONT, "fdcsense_chng unit %d\n", unit));
16790Sstevel@tonic-gate 	digital_input = inb(fcp->c_regbase + FCR_DIR);
16800Sstevel@tonic-gate 	if (fcp->c_mode == FDCMODE_30)
16810Sstevel@tonic-gate 		digital_input ^= FDI_DKCHG;
16820Sstevel@tonic-gate 	return (digital_input & FDI_DKCHG);
16830Sstevel@tonic-gate }
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate int
16860Sstevel@tonic-gate fdcsense_drv(struct fdcntlr *fcp, int unit)
16870Sstevel@tonic-gate {
16880Sstevel@tonic-gate 	static uchar_t sensedrvcmd[2] = {FO_SDRV, 0};
16890Sstevel@tonic-gate 	uchar_t senser;
16900Sstevel@tonic-gate 	int rval;
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 	sensedrvcmd[1] = (uchar_t)unit;
16930Sstevel@tonic-gate 	(void) fdc_docmd(fcp, sensedrvcmd, 2);
16940Sstevel@tonic-gate 	/*
16950Sstevel@tonic-gate 	 * Ignored return. If failed, warning was issued by fdc_docmd.
16960Sstevel@tonic-gate 	 * fdc_results retrieves the controller/drive status
16970Sstevel@tonic-gate 	 */
16980Sstevel@tonic-gate 	if (rval = fdc_result(fcp, &senser, 1))
16990Sstevel@tonic-gate 		goto done;
17000Sstevel@tonic-gate 	if (senser & S3_WPROT)
17010Sstevel@tonic-gate 		fcp->c_unit[unit]->fj_flags |= FUNIT_WPROT;
17020Sstevel@tonic-gate 	else
17030Sstevel@tonic-gate 		fcp->c_unit[unit]->fj_flags &= ~FUNIT_WPROT;
17040Sstevel@tonic-gate done:
17050Sstevel@tonic-gate 	return (rval);
17060Sstevel@tonic-gate }
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate int
17090Sstevel@tonic-gate fdcsense_int(struct fdcntlr *fcp, int *unitp, int *cylp)
17100Sstevel@tonic-gate {
17110Sstevel@tonic-gate 	uchar_t senser[2];
17120Sstevel@tonic-gate 	int rval;
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	(void) fdc_docmd(fcp, &senseintcmd, 1);
17150Sstevel@tonic-gate 	/*
17160Sstevel@tonic-gate 	 * Ignored return. If failed, warning was issued by fdc_docmd.
17170Sstevel@tonic-gate 	 * fdc_results retrieves the controller/drive status
17180Sstevel@tonic-gate 	 */
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 	if (!(rval = fdc_result(fcp, senser, 2))) {
17210Sstevel@tonic-gate 		if ((*senser & (S0_IVCMD | S0_SEKEND | S0_ECHK)) != S0_SEKEND)
17220Sstevel@tonic-gate 			rval = 1;
17230Sstevel@tonic-gate 		if (unitp)
17240Sstevel@tonic-gate 			*unitp = *senser & 3;
17250Sstevel@tonic-gate 		if (cylp)
17260Sstevel@tonic-gate 			*cylp = senser[1];
17270Sstevel@tonic-gate 	}
17280Sstevel@tonic-gate 	return (rval);
17290Sstevel@tonic-gate }
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate int
17320Sstevel@tonic-gate fdcspecify(struct fdcntlr *fcp, int xferrate, int steprate, int hlt)
17330Sstevel@tonic-gate {
17340Sstevel@tonic-gate 	static uchar_t perpindcmd[2] = {FO_PERP, 0};
17350Sstevel@tonic-gate 	static uchar_t specifycmd[3] = {FO_SPEC, 0, 0};
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 	encode(drate_mfm, xferrate, &fcp->c_config);
17380Sstevel@tonic-gate 	outb(fcp->c_regbase + FCR_CCR, fcp->c_config);
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 	if (fcp->c_chip >= i82077) {
17410Sstevel@tonic-gate 		/*
17420Sstevel@tonic-gate 		 * Use old style perpendicular mode command of 82077.
17430Sstevel@tonic-gate 		 */
17440Sstevel@tonic-gate 		if (xferrate == 1000) {
17450Sstevel@tonic-gate 			/* Set GAP and WGATE */
17460Sstevel@tonic-gate 			perpindcmd[1] = 3;
17470Sstevel@tonic-gate 			/* double step rate because xlate table is for 500Kb */
17480Sstevel@tonic-gate 			steprate <<= 1;
17490Sstevel@tonic-gate 			hlt <<= 1;
17500Sstevel@tonic-gate 		} else
17510Sstevel@tonic-gate 			perpindcmd[1] = 0;
17520Sstevel@tonic-gate 		(void) fdc_docmd(fcp, perpindcmd, 2);
17530Sstevel@tonic-gate 		/*
17540Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_docmd.
17550Sstevel@tonic-gate 		 */
17560Sstevel@tonic-gate 	}
17570Sstevel@tonic-gate 	encode(step_rate, steprate, &fcp->c_hutsrt);
17580Sstevel@tonic-gate 	specifycmd[1] = fcp->c_hutsrt |= 0x0F;	/* use max head unload time */
17590Sstevel@tonic-gate 	hlt = (hlt >= 256) ? 0 : (hlt >> 1);	/* encode head load time */
17600Sstevel@tonic-gate 	specifycmd[2] = fcp->c_hlt = hlt << 1;	/* make room for DMA bit */
17610Sstevel@tonic-gate 	return (fdc_docmd(fcp, specifycmd, 3));
17620Sstevel@tonic-gate }
17630Sstevel@tonic-gate 
17640Sstevel@tonic-gate int
17650Sstevel@tonic-gate fdcspdchange(struct fdcntlr *fcp, struct fcu_obj *fjp, int rpm)
17660Sstevel@tonic-gate {
17670Sstevel@tonic-gate 	int	retcode = 0;
17680Sstevel@tonic-gate 	uint_t	ddic;
17690Sstevel@tonic-gate 	uchar_t	deselect = 0;
17700Sstevel@tonic-gate 	uchar_t	ds_code;
17710Sstevel@tonic-gate 	uchar_t	enable_code;
17720Sstevel@tonic-gate 	uchar_t	save;
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate 	if (((fcp->c_flags & FCFLG_DSOUT) == 0 && rpm <= fjp->fj_rotspd) ||
17750Sstevel@tonic-gate 	    ((fcp->c_flags & FCFLG_DSOUT) && (fjp->fj_flags & FUNIT_3DMODE) &&
17760Sstevel@tonic-gate 	    rpm > fjp->fj_rotspd)) {
17770Sstevel@tonic-gate 		return (0);
17780Sstevel@tonic-gate 	}
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 	FCERRPRINT(FDEP_L1, FDEM_SCHG,
17810Sstevel@tonic-gate 	    (CE_CONT, "fdcspdchange: %d rpm\n", rpm));
17820Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fcp->c_dorlock));
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 	switch (fcp->c_chip) {
17850Sstevel@tonic-gate 	default:
17860Sstevel@tonic-gate 		break;
17870Sstevel@tonic-gate 	case i82077:
17880Sstevel@tonic-gate 		break;
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 	case PC87322:
17910Sstevel@tonic-gate 		{
17920Sstevel@tonic-gate 		uchar_t nscmodecmd[5] = {FO_MODE, 0x02, 0x00, 0xC8, 0x00};
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 		if (rpm > fjp->fj_rotspd) {
17950Sstevel@tonic-gate 			nscmodecmd[3] ^= 0xC0;
17960Sstevel@tonic-gate 			retcode = (fcp->c_flags ^ FCFLG_DSOUT) ||
17970Sstevel@tonic-gate 			    (fjp->fj_flags ^ FUNIT_3DMODE);
17980Sstevel@tonic-gate 			fcp->c_flags |= FCFLG_DSOUT;
17990Sstevel@tonic-gate 			fjp->fj_flags |= FUNIT_3DMODE;
18000Sstevel@tonic-gate 		} else {
18010Sstevel@tonic-gate 			/* program DENSEL to default output */
18020Sstevel@tonic-gate 			fcp->c_flags &= ~FCFLG_DSOUT;
18030Sstevel@tonic-gate 			retcode = fjp->fj_flags & FUNIT_3DMODE;
18040Sstevel@tonic-gate 			fjp->fj_flags &= ~FUNIT_3DMODE;
18050Sstevel@tonic-gate 		}
18060Sstevel@tonic-gate 		if (retcode && (fcp->c_digout & FD_DRSEL) == fcp->c_curunit) {
18070Sstevel@tonic-gate 			/* de-select drive while changing speed */
18080Sstevel@tonic-gate 			deselect = fcp->c_digout ^ FD_DRSEL;
18090Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_DOR, deselect);
18100Sstevel@tonic-gate 		}
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate 		(void) fdc_docmd(fcp, nscmodecmd, 5);
18130Sstevel@tonic-gate 		/*
18140Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_docmd.
18150Sstevel@tonic-gate 		 */
18160Sstevel@tonic-gate 		break;
18170Sstevel@tonic-gate 		}
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate 	case FDC37C665:
18200Sstevel@tonic-gate 		enable_code = FSA_ENA5;
18210Sstevel@tonic-gate 		goto SMC_config;
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate 	case FDC37C666:
18240Sstevel@tonic-gate 		enable_code = FSA_ENA6;
18250Sstevel@tonic-gate SMC_config:
18260Sstevel@tonic-gate 		if (rpm > fjp->fj_rotspd) {
18270Sstevel@tonic-gate 			/* force DENSEL output to active LOW */
18280Sstevel@tonic-gate 			ds_code = FSB_DSHI;
18290Sstevel@tonic-gate 			retcode = (fcp->c_flags ^ FCFLG_DSOUT) ||
18300Sstevel@tonic-gate 			    (fjp->fj_flags ^ FUNIT_3DMODE);
18310Sstevel@tonic-gate 			fcp->c_flags |= FCFLG_DSOUT;
18320Sstevel@tonic-gate 			fjp->fj_flags |= FUNIT_3DMODE;
18330Sstevel@tonic-gate 		} else {
18340Sstevel@tonic-gate 			/* program DENSEL to default output */
18350Sstevel@tonic-gate 			ds_code = 0;
18360Sstevel@tonic-gate 			fcp->c_flags &= ~FCFLG_DSOUT;
18370Sstevel@tonic-gate 			retcode = fjp->fj_flags & FUNIT_3DMODE;
18380Sstevel@tonic-gate 			fjp->fj_flags &= ~FUNIT_3DMODE;
18390Sstevel@tonic-gate 		}
18400Sstevel@tonic-gate 		if (retcode && (fcp->c_digout & FD_DRSEL) == fcp->c_curunit) {
18410Sstevel@tonic-gate 			/* de-select drive while changing speed */
18420Sstevel@tonic-gate 			deselect = fcp->c_digout ^ FD_DRSEL;
18430Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_DOR, deselect);
18440Sstevel@tonic-gate 		}
18450Sstevel@tonic-gate 		save = inb(fcp->c_regbase + FCR_SRA);
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate 		/* enter configuration mode */
18480Sstevel@tonic-gate 		ddic = ddi_enter_critical();
18490Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_SRA, enable_code);
18500Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_SRA, enable_code);
18510Sstevel@tonic-gate 		ddi_exit_critical(ddic);
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_SRA, FSA_CR5);
18540Sstevel@tonic-gate 		enable_code = inb(fcp->c_regbase + FCR_SRB) & FSB_DSDEF;
18550Sstevel@tonic-gate 		/* update DENSEL mode bits */
18560Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_SRB, enable_code | ds_code);
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 		/* exit configuration mode */
18590Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_SRA, FSA_DISB);
18600Sstevel@tonic-gate 		drv_usecwait(10);
18610Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_SRA, save);
18620Sstevel@tonic-gate 		break;
18630Sstevel@tonic-gate 	}
18640Sstevel@tonic-gate 	if (deselect)
18650Sstevel@tonic-gate 		/* reselect drive */
18660Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_DOR, fcp->c_digout);
18670Sstevel@tonic-gate 	return (retcode);
18680Sstevel@tonic-gate }
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate static int
18710Sstevel@tonic-gate fdc_motorsm(struct fcu_obj *fjp, int input, int timeval)
18720Sstevel@tonic-gate {
18730Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
18740Sstevel@tonic-gate 	int unit = fjp->fj_unit & 3;
18750Sstevel@tonic-gate 	int old_mstate;
18760Sstevel@tonic-gate 	int rval = 0;
18770Sstevel@tonic-gate 	uchar_t motorbit;
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fcp->c_dorlock));
18800Sstevel@tonic-gate 	old_mstate = fcp->c_mtrstate[unit];
18810Sstevel@tonic-gate 	encode(motor_onbits, unit, &motorbit);
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate 	switch (input) {
18840Sstevel@tonic-gate 	case FMI_TIMER:		/* timer expired */
18850Sstevel@tonic-gate 		fcp->c_motort[unit] = 0;
18860Sstevel@tonic-gate 		switch (old_mstate) {
18870Sstevel@tonic-gate 		case FMS_START:
18880Sstevel@tonic-gate 		case FMS_DELAY:
18890Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = FMS_ON;
18900Sstevel@tonic-gate 			break;
18910Sstevel@tonic-gate 		case FMS_KILLST:
18920Sstevel@tonic-gate 			fcp->c_motort[unit] = timeout(fdmotort, (void *)fjp,
18930Sstevel@tonic-gate 			    drv_usectohz(1000000));
18940Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = FMS_IDLE;
18950Sstevel@tonic-gate 			break;
18960Sstevel@tonic-gate 		case FMS_IDLE:
18970Sstevel@tonic-gate 			fcp->c_digout &= ~motorbit;
18980Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_DOR, fcp->c_digout);
18990Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = FMS_OFF;
19000Sstevel@tonic-gate 			fjp->fj_flags &= ~FUNIT_3DMODE;
19010Sstevel@tonic-gate 			break;
19020Sstevel@tonic-gate 		case 86:
19030Sstevel@tonic-gate 			rval = -1;
19040Sstevel@tonic-gate 			break;
19050Sstevel@tonic-gate 		case FMS_OFF:
19060Sstevel@tonic-gate 		case FMS_ON:
19070Sstevel@tonic-gate 		default:
19080Sstevel@tonic-gate 			rval = -2;
19090Sstevel@tonic-gate 		}
19100Sstevel@tonic-gate 		break;
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	case FMI_STARTCMD:	/* start command */
19130Sstevel@tonic-gate 		switch (old_mstate) {
19140Sstevel@tonic-gate 		case FMS_IDLE:
19150Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = 86;
19160Sstevel@tonic-gate 			mutex_exit(&fcp->c_dorlock);
19170Sstevel@tonic-gate 			(void) untimeout(fcp->c_motort[unit]);
19180Sstevel@tonic-gate 			mutex_enter(&fcp->c_dorlock);
19190Sstevel@tonic-gate 			fcp->c_motort[unit] = 0;
19200Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = FMS_ON;
19210Sstevel@tonic-gate 			break;
19220Sstevel@tonic-gate 		case FMS_OFF:
19230Sstevel@tonic-gate 			fcp->c_digout |= motorbit;
19240Sstevel@tonic-gate 			outb(fcp->c_regbase + FCR_DOR, fcp->c_digout);
19250Sstevel@tonic-gate 
19260Sstevel@tonic-gate 			/* start motor_spinup_timer */
19270Sstevel@tonic-gate 			ASSERT(timeval > 0);
19280Sstevel@tonic-gate 			fcp->c_motort[unit] = timeout(fdmotort,  (void *)fjp,
19290Sstevel@tonic-gate 			    drv_usectohz(100000 * timeval));
19300Sstevel@tonic-gate 			/* FALLTHROUGH */
19310Sstevel@tonic-gate 		case FMS_KILLST:
19320Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = FMS_START;
19330Sstevel@tonic-gate 			break;
19340Sstevel@tonic-gate 		default:
19350Sstevel@tonic-gate 			rval = -2;
19360Sstevel@tonic-gate 		}
19370Sstevel@tonic-gate 		break;
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 	case FMI_RSTARTCMD:	/* restart command */
19400Sstevel@tonic-gate 		if (fcp->c_motort[unit] != 0) {
19410Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = 86;
19420Sstevel@tonic-gate 			mutex_exit(&fcp->c_dorlock);
19430Sstevel@tonic-gate 			(void) untimeout(fcp->c_motort[unit]);
19440Sstevel@tonic-gate 			mutex_enter(&fcp->c_dorlock);
19450Sstevel@tonic-gate 		}
19460Sstevel@tonic-gate 		ASSERT(timeval > 0);
19470Sstevel@tonic-gate 		fcp->c_motort[unit] = timeout(fdmotort, (void *)fjp,
19480Sstevel@tonic-gate 		    drv_usectohz(100000 * timeval));
19490Sstevel@tonic-gate 		fcp->c_mtrstate[unit] = FMS_START;
19500Sstevel@tonic-gate 		break;
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 	case FMI_DELAYCMD:	/* delay command */
19530Sstevel@tonic-gate 		if (fcp->c_motort[unit] == 0)
19540Sstevel@tonic-gate 			fcp->c_motort[unit] = timeout(fdmotort,  (void *)fjp,
19550Sstevel@tonic-gate 			    drv_usectohz(15000));
19560Sstevel@tonic-gate 		fcp->c_mtrstate[unit] = FMS_DELAY;
19570Sstevel@tonic-gate 		break;
19580Sstevel@tonic-gate 
19590Sstevel@tonic-gate 	case FMI_IDLECMD:	/* idle command */
19600Sstevel@tonic-gate 		switch (old_mstate) {
19610Sstevel@tonic-gate 		case FMS_DELAY:
19620Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = 86;
19630Sstevel@tonic-gate 			mutex_exit(&fcp->c_dorlock);
19640Sstevel@tonic-gate 			(void) untimeout(fcp->c_motort[unit]);
19650Sstevel@tonic-gate 			mutex_enter(&fcp->c_dorlock);
19660Sstevel@tonic-gate 			/* FALLTHROUGH */
19670Sstevel@tonic-gate 		case FMS_ON:
19680Sstevel@tonic-gate 			ASSERT(timeval > 0);
19690Sstevel@tonic-gate 			fcp->c_motort[unit] = timeout(fdmotort, (void *)fjp,
19700Sstevel@tonic-gate 			    drv_usectohz(100000 * timeval));
19710Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = FMS_IDLE;
19720Sstevel@tonic-gate 			break;
19730Sstevel@tonic-gate 		case FMS_START:
19740Sstevel@tonic-gate 			fcp->c_mtrstate[unit] = FMS_KILLST;
19750Sstevel@tonic-gate 			break;
19760Sstevel@tonic-gate 		default:
19770Sstevel@tonic-gate 			rval = -2;
19780Sstevel@tonic-gate 		}
19790Sstevel@tonic-gate 		break;
19800Sstevel@tonic-gate 
19810Sstevel@tonic-gate 	default:
19820Sstevel@tonic-gate 		rval = -3;
19830Sstevel@tonic-gate 	}
19840Sstevel@tonic-gate 	if (rval) {
19850Sstevel@tonic-gate 		FCERRPRINT(FDEP_L4, FDEM_EXEC, (CE_WARN,
19860Sstevel@tonic-gate 		    "fdc_motorsm: unit %d  bad input %d or bad state %d",
19870Sstevel@tonic-gate 		    (int)fjp->fj_unit, input, old_mstate));
19880Sstevel@tonic-gate #if 0
19890Sstevel@tonic-gate 		cmn_err(CE_WARN,
19900Sstevel@tonic-gate 		    "fdc_motorsm: unit %d  bad input %d or bad state %d\n",
19910Sstevel@tonic-gate 		    (int)fjp->fj_unit, input, old_mstate);
19920Sstevel@tonic-gate 		fcp->c_mtrstate[unit] = FMS_OFF;
19930Sstevel@tonic-gate 		if (fcp->c_motort[unit] != 0) {
19940Sstevel@tonic-gate 			mutex_exit(&fcp->c_dorlock);
19950Sstevel@tonic-gate 			(void) untimeout(fcp->c_motort[unit]);
19960Sstevel@tonic-gate 			mutex_enter(&fcp->c_dorlock);
19970Sstevel@tonic-gate 			fcp->c_motort[unit] = 0;
19980Sstevel@tonic-gate 		}
19990Sstevel@tonic-gate #endif
20000Sstevel@tonic-gate 	} else
20010Sstevel@tonic-gate 		FCERRPRINT(FDEP_L0, FDEM_EXEC,
20020Sstevel@tonic-gate 		    (CE_CONT, "fdc_motorsm unit %d: input %d,  %d -> %d\n",
20030Sstevel@tonic-gate 		    (int)fjp->fj_unit, input, old_mstate,
20040Sstevel@tonic-gate 		    fcp->c_mtrstate[unit]));
20050Sstevel@tonic-gate 	return (rval);
20060Sstevel@tonic-gate }
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate /*
20090Sstevel@tonic-gate  * fdmotort
20100Sstevel@tonic-gate  *	is called from timeout() when a motor timer has expired.
20110Sstevel@tonic-gate  */
20120Sstevel@tonic-gate static void
20130Sstevel@tonic-gate fdmotort(void *arg)
20140Sstevel@tonic-gate {
20150Sstevel@tonic-gate 	struct fcu_obj *fjp = (struct fcu_obj *)arg;
20160Sstevel@tonic-gate 	struct fdcntlr *fcp = fjp->fj_fdc;
20170Sstevel@tonic-gate 	struct fdcsb *csb = &fcp->c_csb;
20180Sstevel@tonic-gate 	int unit = fjp->fj_unit & 3;
20190Sstevel@tonic-gate 	int mval;
20200Sstevel@tonic-gate 	int newxstate = 0;
20210Sstevel@tonic-gate 
20220Sstevel@tonic-gate 	mutex_enter(&fcp->c_dorlock);
20230Sstevel@tonic-gate 	mval = fdc_motorsm(fjp, FMI_TIMER, 0);
20240Sstevel@tonic-gate 	mutex_exit(&fcp->c_dorlock);
20250Sstevel@tonic-gate 	if (mval < 0)
20260Sstevel@tonic-gate 		return;
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 	mutex_enter(&fcp->c_lock);
20290Sstevel@tonic-gate 
20300Sstevel@tonic-gate 	if ((fcp->c_flags & FCFLG_WAITING) &&
20310Sstevel@tonic-gate 	    fcp->c_mtrstate[unit] == FMS_ON &&
20320Sstevel@tonic-gate 	    (csb->csb_xstate == FXS_MTRON || csb->csb_xstate == FXS_HDST ||
20330Sstevel@tonic-gate 	    csb->csb_xstate == FXS_DKCHGX))
20340Sstevel@tonic-gate 		newxstate = fdc_statemach(fcp);
20350Sstevel@tonic-gate 		if (newxstate == -1) {
20360Sstevel@tonic-gate 			FCERRPRINT(FDEP_L3, FDEM_EXEC,
20370Sstevel@tonic-gate 			    (CE_WARN,
20380Sstevel@tonic-gate 			    "fdc_motort unit %d: motor ready but bad xstate",
20390Sstevel@tonic-gate 			    (int)fjp->fj_unit));
20400Sstevel@tonic-gate 			fcp->c_csb.csb_cmdstat = EIO;
20410Sstevel@tonic-gate 		}
20420Sstevel@tonic-gate 		if (newxstate == -1 || newxstate == FXS_END) {
20430Sstevel@tonic-gate 			fcp->c_flags ^= FCFLG_WAITING;
20440Sstevel@tonic-gate 			cv_signal(&fcp->c_iocv);
20450Sstevel@tonic-gate 		}
20460Sstevel@tonic-gate 	mutex_exit(&fcp->c_lock);
20470Sstevel@tonic-gate }
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate /*
20500Sstevel@tonic-gate  * DMA interrupt service routine
20510Sstevel@tonic-gate  *
20520Sstevel@tonic-gate  *	Called by EISA dma interrupt service routine when buffer chaining
20530Sstevel@tonic-gate  *	is required.
20540Sstevel@tonic-gate  */
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate ddi_dma_cookie_t *
20570Sstevel@tonic-gate fdc_dmae_isr(struct fdcntlr *fcp)
20580Sstevel@tonic-gate {
20590Sstevel@tonic-gate 	struct fdcsb *csb = &fcp->c_csb;
20600Sstevel@tonic-gate 	off_t off;
20610Sstevel@tonic-gate 	size_t len;
20620Sstevel@tonic-gate 
20630Sstevel@tonic-gate 	if (csb->csb_dmahandle && !csb->csb_cmdstat) {
20640Sstevel@tonic-gate 		if (++csb->csb_dmacurrcookie < csb->csb_dmacookiecnt) {
20650Sstevel@tonic-gate 			ddi_dma_nextcookie(csb->csb_dmahandle,
20660Sstevel@tonic-gate 			    &csb->csb_dmacookie);
20670Sstevel@tonic-gate 			return (&csb->csb_dmacookie);
20680Sstevel@tonic-gate 		} else if (++csb->csb_dmacurrwin < csb->csb_dmawincnt) {
20690Sstevel@tonic-gate 			if (ddi_dma_getwin(csb->csb_dmahandle,
20700Sstevel@tonic-gate 			    csb->csb_dmacurrwin, &off, &len,
20710Sstevel@tonic-gate 			    &csb->csb_dmacookie,
20720Sstevel@tonic-gate 			    &csb->csb_dmacookiecnt) != DDI_SUCCESS) {
20730Sstevel@tonic-gate 				return (NULL);
20740Sstevel@tonic-gate 			}
20750Sstevel@tonic-gate 			csb->csb_dmacurrcookie = 0;
20760Sstevel@tonic-gate 			return (&csb->csb_dmacookie);
20770Sstevel@tonic-gate 		}
20780Sstevel@tonic-gate 	} else
20790Sstevel@tonic-gate 		cmn_err(CE_WARN, "fdc: unsolicited DMA interrupt\n");
20800Sstevel@tonic-gate 	return (NULL);
20810Sstevel@tonic-gate }
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate /*
20850Sstevel@tonic-gate  * returns:
20860Sstevel@tonic-gate  *	0 if all ok,
20870Sstevel@tonic-gate  *	ENXIO - diskette not in drive
20880Sstevel@tonic-gate  *	ETIMEDOUT - for immediate operations that timed out
20890Sstevel@tonic-gate  *	EBUSY - if stupid chip is locked busy???
20900Sstevel@tonic-gate  *	ENOEXEC - for timeout during sending cmds to chip
20910Sstevel@tonic-gate  *
20920Sstevel@tonic-gate  * to sleep: set sleep
20930Sstevel@tonic-gate  * to check for disk changed: set change
20940Sstevel@tonic-gate  */
20950Sstevel@tonic-gate static int
20960Sstevel@tonic-gate fdc_exec(struct fdcntlr *fcp, int sleep, int change)
20970Sstevel@tonic-gate {
20980Sstevel@tonic-gate 	struct ddi_dmae_req dmaereq;
20990Sstevel@tonic-gate 	struct fcu_obj *fjp;
21000Sstevel@tonic-gate 	struct fdcsb *csb;
21010Sstevel@tonic-gate 	off_t off;
21020Sstevel@tonic-gate 	size_t len;
21030Sstevel@tonic-gate 	int unit;
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 	mutex_enter(&fcp->c_lock);
21060Sstevel@tonic-gate 	FCERRPRINT(FDEP_L0, FDEM_EXEC,
21070Sstevel@tonic-gate 	    (CE_CONT, "fdc_exec: sleep %x change %x\n", sleep, change));
21080Sstevel@tonic-gate 	csb = &fcp->c_csb;
21090Sstevel@tonic-gate 	unit = csb->csb_drive;
21100Sstevel@tonic-gate 	fjp = fcp->c_unit[unit];
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 	if (csb->csb_opflags & CSB_OFINRPT) {
21130Sstevel@tonic-gate 		if (*csb->csb_cmd == FO_RECAL)
21140Sstevel@tonic-gate 			csb->csb_npcyl = 0;
21150Sstevel@tonic-gate 		else if ((*csb->csb_cmd & ~FO_MFM) != FO_FRMT)
21160Sstevel@tonic-gate 			csb->csb_npcyl =
21170Sstevel@tonic-gate 			    csb->csb_cmd[2] * fjp->fj_chars->fdc_steps;
21180Sstevel@tonic-gate 		csb->csb_xstate = FXS_START;
21190Sstevel@tonic-gate 	} else
21200Sstevel@tonic-gate 		csb->csb_xstate = FXS_DOIT;
21210Sstevel@tonic-gate 	csb->csb_retrys = 0;
21220Sstevel@tonic-gate 	csb->csb_ourtrys = 0;
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 	if (csb->csb_dmahandle) {
21250Sstevel@tonic-gate 		/* ensure that entire format xfer is in one cookie */
21260Sstevel@tonic-gate 		/*
21270Sstevel@tonic-gate 		 * The change from  ddi_dma_buf/addr_setup() to
21280Sstevel@tonic-gate 		 * ddi_dma_buf/addr_bind_handle() has already loaded
21290Sstevel@tonic-gate 		 * the first DMA window and cookie.
21300Sstevel@tonic-gate 		 */
21310Sstevel@tonic-gate 		if ((*csb->csb_cmd & ~FO_MFM) == FO_FRMT &&
21320Sstevel@tonic-gate 		    (4 * csb->csb_cmd[3]) != csb->csb_dmacookie.dmac_size) {
21330Sstevel@tonic-gate 			mutex_exit(&fcp->c_lock);
21340Sstevel@tonic-gate 			return (EINVAL);
21350Sstevel@tonic-gate 		}
21360Sstevel@tonic-gate 	}
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate retry:
21390Sstevel@tonic-gate 	if (fcp->c_curunit != unit || !(fjp->fj_flags & FUNIT_CHAROK)) {
21400Sstevel@tonic-gate 		fcp->c_curunit = unit;
21410Sstevel@tonic-gate 		fjp->fj_flags |= FUNIT_CHAROK;
21420Sstevel@tonic-gate 		if (fjp->fj_chars->fdc_transfer_rate == 417) {
21430Sstevel@tonic-gate 			/* XXX hack for fdformat */
21440Sstevel@tonic-gate 			/* fjp->fj_chars->fdc_transfer_rate == 500;	*/
21450Sstevel@tonic-gate 			fjp->fj_attr->fda_rotatespd = 360;
21460Sstevel@tonic-gate 		}
21470Sstevel@tonic-gate 		if (fdcspecify(fcp, fjp->fj_chars->fdc_transfer_rate,
21480Sstevel@tonic-gate 		    fjp->fj_drive->fdd_steprate, 40))
21490Sstevel@tonic-gate 			cmn_err(CE_WARN,
21500Sstevel@tonic-gate 			    "fdc_select: controller setup rejected "
21510Sstevel@tonic-gate 			    "fdcntrl %p transfer rate %x step rate %x "
21520Sstevel@tonic-gate 			    "head load time 40\n", (void*)fcp,
21530Sstevel@tonic-gate 			    fjp->fj_chars->fdc_transfer_rate,
21540Sstevel@tonic-gate 			    fjp->fj_drive->fdd_steprate);
21550Sstevel@tonic-gate 
21560Sstevel@tonic-gate 		mutex_enter(&fcp->c_dorlock);
21570Sstevel@tonic-gate 		if (fdcspdchange(fcp, fjp, fjp->fj_attr->fda_rotatespd)) {
21580Sstevel@tonic-gate 			/* 3D drive requires 500 ms for speed change */
21590Sstevel@tonic-gate 			(void) fdc_motorsm(fjp, FMI_RSTARTCMD, 5);
21600Sstevel@tonic-gate 			/*
21610Sstevel@tonic-gate 			 * Return value ignored - fdcmotort deals with failure.
21620Sstevel@tonic-gate 			 */
21630Sstevel@tonic-gate 		}
21640Sstevel@tonic-gate 		mutex_exit(&fcp->c_dorlock);
21650Sstevel@tonic-gate 	}
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate 	/*
21680Sstevel@tonic-gate 	 * If checking for disk_change is enabled
21690Sstevel@tonic-gate 	 * (i.e. not seeking in fdresetchng),
21700Sstevel@tonic-gate 	 * we sample the DSKCHG line to see if the diskette has wandered away.
21710Sstevel@tonic-gate 	 */
21720Sstevel@tonic-gate 	if (change && fdcsense_chng(fcp, unit)) {
21730Sstevel@tonic-gate 		FCERRPRINT(FDEP_L3, FDEM_EXEC,
21740Sstevel@tonic-gate 		    (CE_WARN, "diskette %d changed!!!", csb->csb_drive));
21750Sstevel@tonic-gate 		fcp->c_unit[unit]->fj_flags |= FUNIT_CHANGED;
21760Sstevel@tonic-gate 		/*
21770Sstevel@tonic-gate 		 * If the diskette is still gone... so are we, adios!
21780Sstevel@tonic-gate 		 */
21790Sstevel@tonic-gate 		if (fdcheckdisk(fcp, unit)) {
21800Sstevel@tonic-gate 			mutex_exit(&fcp->c_lock);
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate 			/* VP/ix expects an EBUSY return here */
21830Sstevel@tonic-gate 			if (*csb->csb_cmd == FO_SDRV) {
21840Sstevel@tonic-gate 				return (EBUSY);
21850Sstevel@tonic-gate 			}
21860Sstevel@tonic-gate 			return (ENXIO);
21870Sstevel@tonic-gate 		}
21880Sstevel@tonic-gate 		/*
21890Sstevel@tonic-gate 		 * delay to ensure that new diskette is up to speed
21900Sstevel@tonic-gate 		 */
21910Sstevel@tonic-gate 		mutex_enter(&fcp->c_dorlock);
21920Sstevel@tonic-gate 		(void) fdc_motorsm(fjp, FMI_RSTARTCMD,
2193*5295Srandyf 		    fjp->fj_drive->fdd_motoron);
21940Sstevel@tonic-gate 		/*
21950Sstevel@tonic-gate 		 * Return value ignored - fdcmotort deals with failure.
21960Sstevel@tonic-gate 		 */
21970Sstevel@tonic-gate 		mutex_exit(&fcp->c_dorlock);
21980Sstevel@tonic-gate 	}
21990Sstevel@tonic-gate 
22000Sstevel@tonic-gate 	/*
22010Sstevel@tonic-gate 	 * gather some statistics
22020Sstevel@tonic-gate 	 */
22030Sstevel@tonic-gate 	switch (csb->csb_cmd[0] & 0x1f) {
22040Sstevel@tonic-gate 	case FO_RDDAT:
22050Sstevel@tonic-gate 		fcp->fdstats.rd++;
22060Sstevel@tonic-gate 		break;
22070Sstevel@tonic-gate 	case FO_WRDAT:
22080Sstevel@tonic-gate 		fcp->fdstats.wr++;
22090Sstevel@tonic-gate 		break;
22100Sstevel@tonic-gate 	case FO_RECAL:
22110Sstevel@tonic-gate 		fcp->fdstats.recal++;
22120Sstevel@tonic-gate 		break;
22130Sstevel@tonic-gate 	case FO_FRMT:
22140Sstevel@tonic-gate 		fcp->fdstats.form++;
22150Sstevel@tonic-gate 		break;
22160Sstevel@tonic-gate 	default:
22170Sstevel@tonic-gate 		fcp->fdstats.other++;
22180Sstevel@tonic-gate 		break;
22190Sstevel@tonic-gate 	}
22200Sstevel@tonic-gate 
22210Sstevel@tonic-gate 	bzero(csb->csb_rslt, 10);
22220Sstevel@tonic-gate 	csb->csb_cmdstat = 0;
22230Sstevel@tonic-gate 
22240Sstevel@tonic-gate 	if (csb->csb_dmahandle) {
22250Sstevel@tonic-gate 		bzero(&dmaereq, sizeof (struct ddi_dmae_req));
22260Sstevel@tonic-gate 		dmaereq.der_command = (csb->csb_opflags & CSB_OFDMAWT) ?
22270Sstevel@tonic-gate 		    DMAE_CMD_WRITE : DMAE_CMD_READ;
22280Sstevel@tonic-gate 		/*
22290Sstevel@tonic-gate 		 * setup for dma buffer chaining regardless of bus capability
22300Sstevel@tonic-gate 		 */
22310Sstevel@tonic-gate 		dmaereq.der_bufprocess = DMAE_BUF_CHAIN;
22320Sstevel@tonic-gate 		dmaereq.proc = fdc_dmae_isr;
22330Sstevel@tonic-gate 		dmaereq.procparms = (void *)fcp;
22340Sstevel@tonic-gate 		if (ddi_dmae_prog(fcp->c_dip, &dmaereq, &csb->csb_dmacookie,
22350Sstevel@tonic-gate 		    fcp->c_dmachan) != DDI_SUCCESS)
22360Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdc_exec: dmae prog failed, "
2237*5295Srandyf 			    "dip %p, dmachan %x\n",
2238*5295Srandyf 			    (void*)fcp->c_dip, fcp->c_dmachan);
22390Sstevel@tonic-gate 	}
22400Sstevel@tonic-gate 
22410Sstevel@tonic-gate 	if ((fdc_statemach(fcp) == FXS_DOWT) && !sleep) {
22420Sstevel@tonic-gate 		/*
22430Sstevel@tonic-gate 		 * If the operation has no results - then just return
22440Sstevel@tonic-gate 		 */
22450Sstevel@tonic-gate 		if (!csb->csb_nrslts) {
22460Sstevel@tonic-gate 			mutex_exit(&fcp->c_lock);
22470Sstevel@tonic-gate 			return (0);
22480Sstevel@tonic-gate 		}
22490Sstevel@tonic-gate 		/*
22500Sstevel@tonic-gate 		 * this operation has no interrupt and an immediate result
22510Sstevel@tonic-gate 		 * so wait for the results and stuff them into the csb
22520Sstevel@tonic-gate 		 */
22530Sstevel@tonic-gate 		if (fdc_statemach(fcp) == -1) {
22540Sstevel@tonic-gate 			mutex_exit(&fcp->c_lock);
22550Sstevel@tonic-gate 			return (EIO);
22560Sstevel@tonic-gate 		}
22570Sstevel@tonic-gate 	} else {
22580Sstevel@tonic-gate 		fcp->c_flags |= FCFLG_WAITING;
22590Sstevel@tonic-gate 		/*
22600Sstevel@tonic-gate 		 * wait for completion interrupt
22610Sstevel@tonic-gate 		 */
22620Sstevel@tonic-gate 		while (fcp->c_flags & FCFLG_WAITING) {
22630Sstevel@tonic-gate 			cv_wait(&fcp->c_iocv, &fcp->c_lock);
22640Sstevel@tonic-gate 		}
22650Sstevel@tonic-gate 	}
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate 	/*
22680Sstevel@tonic-gate 	 * See if there was an error detected, if so, fdrecover()
22690Sstevel@tonic-gate 	 * will check it out and say what to do.
22700Sstevel@tonic-gate 	 *
22710Sstevel@tonic-gate 	 * Don't do this, though, if this was the Sense Drive Status
22720Sstevel@tonic-gate 	 * or the Dump Registers command.
22730Sstevel@tonic-gate 	 */
22740Sstevel@tonic-gate 	if (csb->csb_cmdstat && *csb->csb_cmd != FO_SDRV) {
22750Sstevel@tonic-gate 		/* if it can restarted OK, then do so, else return error */
22760Sstevel@tonic-gate 		if (fdrecover(fcp)) {
22770Sstevel@tonic-gate 			mutex_exit(&fcp->c_lock);
22780Sstevel@tonic-gate 			return (EIO);
22790Sstevel@tonic-gate 		}
22800Sstevel@tonic-gate 		/* ASSUMES that cmd is still intact in csb */
22810Sstevel@tonic-gate 		if (csb->csb_xstate == FXS_END)
22820Sstevel@tonic-gate 			csb->csb_xstate = FXS_START;
22830Sstevel@tonic-gate 		if (fdc_dma_attr.dma_attr_sgllen > 1 && csb->csb_dmahandle) {
22840Sstevel@tonic-gate 			/*
22850Sstevel@tonic-gate 			 * restarted read/write operation requires
22860Sstevel@tonic-gate 			 * first DMA cookie of current window
22870Sstevel@tonic-gate 			 */
22880Sstevel@tonic-gate 			if (ddi_dma_getwin(csb->csb_dmahandle,
22890Sstevel@tonic-gate 			    csb->csb_dmacurrwin, &off, &len,
22900Sstevel@tonic-gate 			    &csb->csb_dmacookie,
22910Sstevel@tonic-gate 			    &csb->csb_dmacookiecnt) != DDI_SUCCESS) {
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate 				mutex_exit(&fcp->c_lock);
22940Sstevel@tonic-gate 				return (EIO);
22950Sstevel@tonic-gate 			}
22960Sstevel@tonic-gate 			csb->csb_dmacurrcookie = 0;
22970Sstevel@tonic-gate 		}
22980Sstevel@tonic-gate 		goto retry;
22990Sstevel@tonic-gate 	}
23000Sstevel@tonic-gate 	/* things went ok */
23010Sstevel@tonic-gate 	mutex_exit(&fcp->c_lock);
23020Sstevel@tonic-gate 	return (0);
23030Sstevel@tonic-gate }
23040Sstevel@tonic-gate 
23050Sstevel@tonic-gate /*
23060Sstevel@tonic-gate  * fdcheckdisk
23070Sstevel@tonic-gate  *	called by fdc_exec to check if the disk is still there - do a seek
23080Sstevel@tonic-gate  *	then see if DSKCHG line went away; if so, diskette is in; else
23090Sstevel@tonic-gate  *	it's (still) out.
23100Sstevel@tonic-gate  */
23110Sstevel@tonic-gate int
23120Sstevel@tonic-gate fdcheckdisk(struct fdcntlr *fcp, int unit)
23130Sstevel@tonic-gate {
23140Sstevel@tonic-gate 	struct fdcsb *csb = &fcp->c_csb;
23150Sstevel@tonic-gate 	int newcyl;			/* where to seek for reset of DSKCHG */
23160Sstevel@tonic-gate 	int rval;
23170Sstevel@tonic-gate 	enum fxstate save_xstate;
23180Sstevel@tonic-gate 	uchar_t save_cmd, save_cd1, save_npcyl;
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fcp->c_lock));
23210Sstevel@tonic-gate 	FCERRPRINT(FDEP_L1, FDEM_CHEK,
23220Sstevel@tonic-gate 	    (CE_CONT, "fdcheckdisk unit %d\n", unit));
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate 	if (fcp->c_curpcyl[unit])
23250Sstevel@tonic-gate 		newcyl = fcp->c_curpcyl[unit] - 1;
23260Sstevel@tonic-gate 	else
23270Sstevel@tonic-gate 		newcyl = 1;
23280Sstevel@tonic-gate 
23290Sstevel@tonic-gate 	save_cmd = *csb->csb_cmd;
23300Sstevel@tonic-gate 	save_cd1 = csb->csb_cmd[1];
23310Sstevel@tonic-gate 	save_npcyl = csb->csb_npcyl;
23320Sstevel@tonic-gate 	save_xstate = csb->csb_xstate;
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate 	*csb->csb_cmd = FO_SEEK;
23350Sstevel@tonic-gate 	csb->csb_cmd[1] = (uchar_t)unit;
23360Sstevel@tonic-gate 	csb->csb_npcyl = (uchar_t)newcyl;
23370Sstevel@tonic-gate 	fcp->c_flags |= FCFLG_WAITING;
23380Sstevel@tonic-gate 
23390Sstevel@tonic-gate 	if (fcp->c_mtrstate[unit] != FMS_ON && fcp->c_motort[unit] != 0)
23400Sstevel@tonic-gate 		/*
23410Sstevel@tonic-gate 		 * wait for motor to get up to speed,
23420Sstevel@tonic-gate 		 * and let motor_timer issue seek cmd
23430Sstevel@tonic-gate 		 */
23440Sstevel@tonic-gate 		csb->csb_xstate = FXS_DKCHGX;
23450Sstevel@tonic-gate 	else {
23460Sstevel@tonic-gate 		/*
23470Sstevel@tonic-gate 		 * motor is up to speed; issue seek cmd now
23480Sstevel@tonic-gate 		 */
23490Sstevel@tonic-gate 		csb->csb_xstate = FXS_SEEK;
23500Sstevel@tonic-gate 		if (rval = fdcseek(fcp, unit, newcyl)) {
23510Sstevel@tonic-gate 			/*
23520Sstevel@tonic-gate 			 * any recal/seek errors are too serious to attend to
23530Sstevel@tonic-gate 			 */
23540Sstevel@tonic-gate 			FCERRPRINT(FDEP_L3, FDEM_CHEK,
23550Sstevel@tonic-gate 			    (CE_WARN, "fdcheckdisk err %d", rval));
23560Sstevel@tonic-gate 			fcp->c_flags ^= FCFLG_WAITING;
23570Sstevel@tonic-gate 		}
23580Sstevel@tonic-gate 	}
23590Sstevel@tonic-gate 	/*
23600Sstevel@tonic-gate 	 * wait for completion interrupt
23610Sstevel@tonic-gate 	 * XXX This should be backed up with a watchdog timer!
23620Sstevel@tonic-gate 	 */
23630Sstevel@tonic-gate 	while (fcp->c_flags & FCFLG_WAITING) {
23640Sstevel@tonic-gate 		cv_wait(&fcp->c_iocv, &fcp->c_lock);
23650Sstevel@tonic-gate 	}
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 	/*
23680Sstevel@tonic-gate 	 * if disk change still asserted, no diskette in drive!
23690Sstevel@tonic-gate 	 */
23700Sstevel@tonic-gate 	if (rval = fdcsense_chng(fcp, unit)) {
23710Sstevel@tonic-gate 		FCERRPRINT(FDEP_L3, FDEM_CHEK,
23720Sstevel@tonic-gate 		    (CE_WARN, "fdcheckdisk no disk %d", unit));
23730Sstevel@tonic-gate 	}
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate 	*csb->csb_cmd = save_cmd;
23760Sstevel@tonic-gate 	csb->csb_cmd[1] = save_cd1;
23770Sstevel@tonic-gate 	csb->csb_npcyl = save_npcyl;
23780Sstevel@tonic-gate 	csb->csb_xstate = save_xstate;
23790Sstevel@tonic-gate 	return (rval);
23800Sstevel@tonic-gate }
23810Sstevel@tonic-gate 
23820Sstevel@tonic-gate static int
23830Sstevel@tonic-gate fdrecover(struct fdcntlr *fcp)
23840Sstevel@tonic-gate {
23850Sstevel@tonic-gate 	struct fcu_obj *fjp;
23860Sstevel@tonic-gate 	struct fdcsb *csb = &fcp->c_csb;
23870Sstevel@tonic-gate 	int residual;
23880Sstevel@tonic-gate 	int unit;
23890Sstevel@tonic-gate 	char *failure;
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate 	FCERRPRINT(FDEP_L2, FDEM_RECO,
23920Sstevel@tonic-gate 	    (CE_NOTE, "fdrecover unit %d", csb->csb_drive));
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 	unit = csb->csb_drive;
23950Sstevel@tonic-gate 	fjp = fcp->c_unit[unit];
23960Sstevel@tonic-gate 	if (fcp->c_flags & FCFLG_TIMEOUT) {
23970Sstevel@tonic-gate 		fcp->c_flags ^= FCFLG_TIMEOUT;
23980Sstevel@tonic-gate 		csb->csb_rslt[1] |= 0x08;
23990Sstevel@tonic-gate 		FCERRPRINT(FDEP_L3, FDEM_RECO,
24000Sstevel@tonic-gate 		    (CE_WARN, "fd unit %d: %s timed out", csb->csb_drive,
24010Sstevel@tonic-gate 		    fdcmds[*csb->csb_cmd & 0x1f].cmdname));
24020Sstevel@tonic-gate 	}
24030Sstevel@tonic-gate 
24040Sstevel@tonic-gate 	if (csb->csb_status & S0_SEKEND)
24050Sstevel@tonic-gate 		fcp->c_curpcyl[unit] = -1;
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 	switch (csb->csb_oldxs) {
24080Sstevel@tonic-gate 	case FXS_RCAL:		/* recalibrate */
24090Sstevel@tonic-gate 	case FXS_SEEK:		/* seek */
24100Sstevel@tonic-gate 	case FXS_RESET:		/* cntlr reset */
24110Sstevel@tonic-gate 		FCERRPRINT(FDEP_L4, FDEM_RECO, (CE_WARN,
24120Sstevel@tonic-gate 		    "fd unit %d: %s error: st0=0x%x pcn=%d", csb->csb_drive,
24130Sstevel@tonic-gate 		    fdcmds[*csb->csb_cmd & 0x1f].cmdname,
24140Sstevel@tonic-gate 		    *csb->csb_rslt, csb->csb_rslt[1]));
24150Sstevel@tonic-gate 		if (csb->csb_retrys++ < skretry &&
24160Sstevel@tonic-gate 		    !(csb->csb_opflags & CSB_OFRAWIOCTL))
24170Sstevel@tonic-gate 			return (0);
24180Sstevel@tonic-gate 		break;
24190Sstevel@tonic-gate 
24200Sstevel@tonic-gate 	case FXS_RDID:		/* read ID */
24210Sstevel@tonic-gate 		if (!(csb->csb_status & S0_SEKEND))
24220Sstevel@tonic-gate 			csb->csb_xstate = FXS_HDST;
24230Sstevel@tonic-gate 		/* FALLTHROUGH */
24240Sstevel@tonic-gate 	case FXS_DOIT:		/* original operation */
24250Sstevel@tonic-gate 	case FXS_DOWT:		/* waiting on operation */
24260Sstevel@tonic-gate 		if (csb->csb_opflags & (CSB_OFDMARD | CSB_OFDMAWT)) {
24270Sstevel@tonic-gate 			if (ddi_dmae_getcnt(fcp->c_dip, fcp->c_dmachan,
24280Sstevel@tonic-gate 			    &residual) != DDI_SUCCESS)
24290Sstevel@tonic-gate 				cmn_err(CE_WARN,
24300Sstevel@tonic-gate 				    "fdc_recover: dmae getcnt failed, "
24310Sstevel@tonic-gate 				    "dip %p dmachan %x residual %x\n",
24320Sstevel@tonic-gate 				    (void*)fcp->c_dip, fcp->c_dmachan,
24330Sstevel@tonic-gate 				    residual);
24340Sstevel@tonic-gate 			FCERRPRINT(FDEP_L2, FDEM_RECO,
2435*5295Srandyf 			    (CE_NOTE,
2436*5295Srandyf 			    "fd unit %d: %s error: "
2437*5295Srandyf 			    "dma count=0x%lx residual=0x%x",
24380Sstevel@tonic-gate 			    csb->csb_drive,
24390Sstevel@tonic-gate 			    fdcmds[*csb->csb_cmd & 0x1f].cmdname,
24400Sstevel@tonic-gate 			    csb->csb_dmacookie.dmac_size, residual));
24410Sstevel@tonic-gate 		}
24420Sstevel@tonic-gate 		if (csb->csb_rslt[1] == S1_OVRUN)
24430Sstevel@tonic-gate 			/*
24440Sstevel@tonic-gate 			 * handle retries of over/underrun
24450Sstevel@tonic-gate 			 * with a secondary retry counter
24460Sstevel@tonic-gate 			 */
24470Sstevel@tonic-gate 			if (++csb->csb_ourtrys <= OURUN_TRIES) {
24480Sstevel@tonic-gate 				FCERRPRINT(FDEP_L2, FDEM_RECO,
2449*5295Srandyf 				    (CE_NOTE,
2450*5295Srandyf 				    "fd unit %d: %s error: over/under-run",
2451*5295Srandyf 				    csb->csb_drive,
2452*5295Srandyf 				    fdcmds[*csb->csb_cmd & 0x1f].cmdname));
24530Sstevel@tonic-gate 				return (0);
24540Sstevel@tonic-gate 			} else
24550Sstevel@tonic-gate 				/*
24560Sstevel@tonic-gate 				 * count 1 set of over/underruns
24570Sstevel@tonic-gate 				 * as 1 primary retry effort
24580Sstevel@tonic-gate 				 */
24590Sstevel@tonic-gate 				csb->csb_ourtrys = 0;
24600Sstevel@tonic-gate 
24610Sstevel@tonic-gate 		if ((fjp->fj_flags & (FUNIT_UNLABELED | FUNIT_LABELOK)) &&
24620Sstevel@tonic-gate 		    !(csb->csb_opflags & CSB_OFRAWIOCTL)) {
24630Sstevel@tonic-gate 			/*
24640Sstevel@tonic-gate 			 * device is open so keep trying and
24650Sstevel@tonic-gate 			 * gather statistics on errors
24660Sstevel@tonic-gate 			 */
24670Sstevel@tonic-gate 			if (csb->csb_rslt[1] & S1_CRCER)
24680Sstevel@tonic-gate 				fcp->fdstats.de++;
24690Sstevel@tonic-gate 			if (csb->csb_rslt[1] & S1_OVRUN)
24700Sstevel@tonic-gate 				fcp->fdstats.run++;
24710Sstevel@tonic-gate 			if (csb->csb_rslt[1] & (S1_NODATA | S1_MADMK))
24720Sstevel@tonic-gate 				fcp->fdstats.bfmt++;
24730Sstevel@tonic-gate 			if (csb->csb_rslt[1] & 0x08)
24740Sstevel@tonic-gate 				fcp->fdstats.to++;
24750Sstevel@tonic-gate 
24760Sstevel@tonic-gate 			/*
24770Sstevel@tonic-gate 			 * if we have not run out of retries, return 0
24780Sstevel@tonic-gate 			 */
24790Sstevel@tonic-gate 			if (csb->csb_retrys++ < csb->csb_maxretry &&
24800Sstevel@tonic-gate 			    (*csb->csb_cmd & ~FO_MFM) != FO_FRMT) {
24810Sstevel@tonic-gate 				if (csb->csb_opflags &
24820Sstevel@tonic-gate 				    (CSB_OFDMARD | CSB_OFDMAWT)) {
24830Sstevel@tonic-gate 					FCERRPRINT(FDEP_L4, FDEM_RECO,
2484*5295Srandyf 					    (CE_WARN,
2485*5295Srandyf 					    "fd unit %d: %s error: "
2486*5295Srandyf 					    "st0=0x%x st1=0x%x st2=0x%x",
24870Sstevel@tonic-gate 					    csb->csb_drive,
2488*5295Srandyf 					    fdcmds[*csb->csb_cmd &
2489*5295Srandyf 					    0x1f].cmdname,
24900Sstevel@tonic-gate 					    *csb->csb_rslt, csb->csb_rslt[1],
24910Sstevel@tonic-gate 					    csb->csb_rslt[2]));
24920Sstevel@tonic-gate 				}
24930Sstevel@tonic-gate 				if ((csb->csb_retrys & 1) &&
24940Sstevel@tonic-gate 				    csb->csb_xstate == FXS_END)
24950Sstevel@tonic-gate 					csb->csb_xstate = FXS_DOIT;
24960Sstevel@tonic-gate 				else if (csb->csb_retrys == 3)
24970Sstevel@tonic-gate 					csb->csb_xstate = FXS_RESTART;
24980Sstevel@tonic-gate 				return (0);
24990Sstevel@tonic-gate 			}
25000Sstevel@tonic-gate 			if (csb->csb_rslt[1] & S1_CRCER)
25010Sstevel@tonic-gate 				failure = "crc error";
25020Sstevel@tonic-gate 			else if (csb->csb_rslt[1] & S1_OVRUN)
25030Sstevel@tonic-gate 				failure = "over/under-run";
25040Sstevel@tonic-gate 			else if (csb->csb_rslt[1] & (S1_NODATA | S1_MADMK))
25050Sstevel@tonic-gate 				failure = "bad format";
25060Sstevel@tonic-gate 			else if (csb->csb_rslt[1] & 0x08)
25070Sstevel@tonic-gate 				failure = "timeout";
25080Sstevel@tonic-gate 			else
25090Sstevel@tonic-gate 				failure = "failed";
25100Sstevel@tonic-gate 			cmn_err(CE_NOTE, "!fd unit %d: %s %s (%x %x %x)",
25110Sstevel@tonic-gate 			    csb->csb_drive,
25120Sstevel@tonic-gate 			    fdcmds[*csb->csb_cmd & 0x1f].cmdname, failure,
25130Sstevel@tonic-gate 			    *csb->csb_rslt, csb->csb_rslt[1], csb->csb_rslt[2]);
25140Sstevel@tonic-gate 		} else {
25150Sstevel@tonic-gate 			FCERRPRINT(FDEP_L2, FDEM_RECO,
25160Sstevel@tonic-gate 			    (CE_NOTE, "fd unit %d: %s failed (%x %x %x)",
25170Sstevel@tonic-gate 			    csb->csb_drive,
25180Sstevel@tonic-gate 			    fdcmds[*csb->csb_cmd & 0x1f].cmdname,
25190Sstevel@tonic-gate 			    *csb->csb_rslt, csb->csb_rslt[1],
25200Sstevel@tonic-gate 			    csb->csb_rslt[2]));
25210Sstevel@tonic-gate 		}
25220Sstevel@tonic-gate 		break;
25230Sstevel@tonic-gate 
25240Sstevel@tonic-gate 	default:
25250Sstevel@tonic-gate 		FCERRPRINT(FDEP_L4, FDEM_RECO, (CE_WARN,
25260Sstevel@tonic-gate 		    "fd unit %d: %s failed: st0=0x%x st1=0x%x st2=0x%x",
25270Sstevel@tonic-gate 		    csb->csb_drive, fdcmds[*csb->csb_cmd & 0x1f].cmdname,
25280Sstevel@tonic-gate 		    *csb->csb_rslt, csb->csb_rslt[1], csb->csb_rslt[2]));
25290Sstevel@tonic-gate 		break;
25300Sstevel@tonic-gate 	}
25310Sstevel@tonic-gate 	return (1);
25320Sstevel@tonic-gate }
25330Sstevel@tonic-gate 
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate /*	Autovector Interrupt Entry Point	*/
25360Sstevel@tonic-gate /* ARGSUSED */
25370Sstevel@tonic-gate static uint_t
25380Sstevel@tonic-gate fdc_intr(caddr_t arg)
25390Sstevel@tonic-gate {
25400Sstevel@tonic-gate 	struct fdcntlr *fcp = (struct fdcntlr *)arg;
25410Sstevel@tonic-gate 	struct fdcsb *csb;
25420Sstevel@tonic-gate 	off_t off;
25430Sstevel@tonic-gate 	size_t blklen;
25440Sstevel@tonic-gate 	int drive;
25450Sstevel@tonic-gate 	int newstate;
25460Sstevel@tonic-gate 	int pendstate;
25470Sstevel@tonic-gate 	int rval = DDI_DMA_DONE;
25480Sstevel@tonic-gate 	int state;
25490Sstevel@tonic-gate 	int maxspin = 10;
25500Sstevel@tonic-gate 
25510Sstevel@tonic-gate 	csb = &fcp->c_csb;
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate 	mutex_enter(&fcp->c_lock);
25540Sstevel@tonic-gate 	/*
25550Sstevel@tonic-gate 	 * Wait for the RQM bit to be set, or until we've tested it
25560Sstevel@tonic-gate 	 * a bunch of times (which may imply this isn't our interrupt).
25570Sstevel@tonic-gate 	 */
25580Sstevel@tonic-gate 	state = inb(fcp->c_regbase + FCR_MSR);
25590Sstevel@tonic-gate 	pendstate = state & (MS_RQM | MS_DIO | MS_CB);
25600Sstevel@tonic-gate 	while (((pendstate & MS_RQM) == 0) && (maxspin-- > 0)) {
25610Sstevel@tonic-gate 		/* Small pause in between reading the status port */
25620Sstevel@tonic-gate 		drv_usecwait(10);
25630Sstevel@tonic-gate 		/* Reread the status port */
25640Sstevel@tonic-gate 		state = inb(fcp->c_regbase + FCR_MSR);
25650Sstevel@tonic-gate 		pendstate = state & (MS_RQM | MS_DIO | MS_CB);
25660Sstevel@tonic-gate 	}
25670Sstevel@tonic-gate 	FCERRPRINT(FDEP_L0, FDEM_INTR,
25680Sstevel@tonic-gate 	    (CE_CONT, "fdc_intr unit %d: xstate=%d MSR=0x%x\n",
25690Sstevel@tonic-gate 	    csb->csb_drive, csb->csb_xstate, state));
25700Sstevel@tonic-gate 
25710Sstevel@tonic-gate 	/*
25720Sstevel@tonic-gate 	 * If there is an operation outstanding AND the controller is ready
25730Sstevel@tonic-gate 	 * to receive a command or send us the result of a command (OR if the
25740Sstevel@tonic-gate 	 * controller is ready to accept a new command), AND if
25750Sstevel@tonic-gate 	 * someone has been waiting for a command to finish AND (if no unit
25760Sstevel@tonic-gate 	 * is BUSY OR if the unit that we're waiting for is BUSY (i.e. it's in
25770Sstevel@tonic-gate 	 * the middle of a seek/recalibrate)) then this interrupt is for us.
25780Sstevel@tonic-gate 	 */
25790Sstevel@tonic-gate 	if ((pendstate == (MS_RQM | MS_DIO | MS_CB) || pendstate == MS_RQM) &&
25800Sstevel@tonic-gate 	    (fcp->c_flags & FCFLG_WAITING) &&
25810Sstevel@tonic-gate 	    (!(state & 0x0f) || ((1 << csb->csb_drive) & state))) {
25820Sstevel@tonic-gate 		/*
25830Sstevel@tonic-gate 		 * Remove one of the conditions for entering this code.
25840Sstevel@tonic-gate 		 * The state_machine will release the c_lock if it
25850Sstevel@tonic-gate 		 * calls untimeout()
25860Sstevel@tonic-gate 		 */
25870Sstevel@tonic-gate 		fcp->c_flags ^= FCFLG_WAITING;
25880Sstevel@tonic-gate 
25890Sstevel@tonic-gate 		if ((newstate = fdc_statemach(fcp)) == -1) {
25900Sstevel@tonic-gate 			/* restore waiting flag */
25910Sstevel@tonic-gate 			fcp->c_flags |= FCFLG_WAITING;
25920Sstevel@tonic-gate 			mutex_exit(&fcp->c_lock);
25930Sstevel@tonic-gate 			return (DDI_INTR_CLAIMED);
25940Sstevel@tonic-gate 		}
25950Sstevel@tonic-gate 
25960Sstevel@tonic-gate 		if (fcp->c_intrstat)
25970Sstevel@tonic-gate 			KIOIP->intrs[KSTAT_INTR_HARD]++;
25980Sstevel@tonic-gate 		if (newstate == FXS_END) {
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate 			if (csb->csb_dmahandle && !csb->csb_cmdstat &&
26010Sstevel@tonic-gate 				/*
26020Sstevel@tonic-gate 				 * read/write operation may have multiple DMA
26030Sstevel@tonic-gate 				 * cookies: process next one
26040Sstevel@tonic-gate 				 */
26050Sstevel@tonic-gate 			    ((csb->csb_dmacurrcookie <
26060Sstevel@tonic-gate 			    (csb->csb_dmacookiecnt - 1)) ||
26070Sstevel@tonic-gate 			    (csb->csb_dmacurrwin) < (csb->csb_dmawincnt - 1))) {
26080Sstevel@tonic-gate 				/*
26090Sstevel@tonic-gate 				 * read/write operation requires another
26100Sstevel@tonic-gate 				 * DMA cookie: process next one
26110Sstevel@tonic-gate 				 */
26120Sstevel@tonic-gate 
26130Sstevel@tonic-gate 				if (++csb->csb_dmacurrcookie <
26140Sstevel@tonic-gate 				    csb->csb_dmacookiecnt) {
26150Sstevel@tonic-gate 					ddi_dma_nextcookie(csb->csb_dmahandle,
26160Sstevel@tonic-gate 					    &csb->csb_dmacookie);
26170Sstevel@tonic-gate 				} else if (++csb->csb_dmacurrwin <
26180Sstevel@tonic-gate 				    csb->csb_dmawincnt) {
26190Sstevel@tonic-gate 					if (ddi_dma_getwin(csb->csb_dmahandle,
26200Sstevel@tonic-gate 					    csb->csb_dmacurrwin, &off, &blklen,
26210Sstevel@tonic-gate 					    &csb->csb_dmacookie,
26220Sstevel@tonic-gate 					    &csb->csb_dmacookiecnt) !=
26230Sstevel@tonic-gate 					    DDI_SUCCESS) {
26240Sstevel@tonic-gate 						cmn_err(CE_WARN,
26250Sstevel@tonic-gate 						    "fdc_intr: "
26260Sstevel@tonic-gate 						    "dma getwin failed\n");
26270Sstevel@tonic-gate 					}
26280Sstevel@tonic-gate 					csb->csb_dmacurrcookie = 0;
26290Sstevel@tonic-gate 				}
26300Sstevel@tonic-gate 
26310Sstevel@tonic-gate 				if (ddi_dmae_prog(fcp->c_dip, NULL,
26320Sstevel@tonic-gate 				    &csb->csb_dmacookie, fcp->c_dmachan) !=
26330Sstevel@tonic-gate 				    DDI_SUCCESS)
26340Sstevel@tonic-gate 					cmn_err(CE_WARN,
26350Sstevel@tonic-gate 					    "fdc_intr: dmae prog failed, "
26360Sstevel@tonic-gate 					    "dip %p dmachannel %x\n",
26370Sstevel@tonic-gate 					    (void*)fcp->c_dip,
26380Sstevel@tonic-gate 					    fcp->c_dmachan);
26390Sstevel@tonic-gate 
26400Sstevel@tonic-gate 				/*
26410Sstevel@tonic-gate 				 * status of last operation has disk
26420Sstevel@tonic-gate 				 * address for continuation
26430Sstevel@tonic-gate 				 */
26440Sstevel@tonic-gate 				csb->csb_cmd[2] = csb->csb_rslt[3];
26450Sstevel@tonic-gate 				csb->csb_cmd[3] = csb->csb_rslt[4];
26460Sstevel@tonic-gate 				csb->csb_cmd[4] = csb->csb_rslt[5];
26470Sstevel@tonic-gate 				csb->csb_cmd[1] = (csb->csb_cmd[1] & ~0x04) |
26480Sstevel@tonic-gate 				    (csb->csb_cmd[3] << 2);
26490Sstevel@tonic-gate 
26500Sstevel@tonic-gate 				csb->csb_xstate = FXS_START;
26510Sstevel@tonic-gate 				(void) fdc_statemach(fcp);
26520Sstevel@tonic-gate 				/*
26530Sstevel@tonic-gate 				 * Ignored return.  If failed, warning already
26540Sstevel@tonic-gate 				 * posted.  Returned state irrelevant.
26550Sstevel@tonic-gate 				 */
26560Sstevel@tonic-gate 				/* restore waiting flag */
26570Sstevel@tonic-gate 				fcp->c_flags |= FCFLG_WAITING;
26580Sstevel@tonic-gate 				goto fi_exit;
26590Sstevel@tonic-gate 			}
26600Sstevel@tonic-gate 			if (rval != DDI_DMA_DONE)
26610Sstevel@tonic-gate 				csb->csb_cmdstat = EIO;
26620Sstevel@tonic-gate 			/*
26630Sstevel@tonic-gate 			 * somebody's waiting for completion of fdcntlr/csb,
26640Sstevel@tonic-gate 			 * wake them
26650Sstevel@tonic-gate 			 */
26660Sstevel@tonic-gate 			cv_signal(&fcp->c_iocv);
26670Sstevel@tonic-gate 		}
26680Sstevel@tonic-gate 		else
26690Sstevel@tonic-gate 			/* restore waiting flag */
26700Sstevel@tonic-gate 			fcp->c_flags |= FCFLG_WAITING;
26710Sstevel@tonic-gate fi_exit:
26720Sstevel@tonic-gate 		mutex_exit(&fcp->c_lock);
26730Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
26740Sstevel@tonic-gate 	}
26750Sstevel@tonic-gate 
26760Sstevel@tonic-gate 	if (state & MS_RQM) {
26770Sstevel@tonic-gate 		(void) fdcsense_int(fcp, &drive, NULL);
26780Sstevel@tonic-gate 		/*
26790Sstevel@tonic-gate 		 * Ignored return - senser state already saved
26800Sstevel@tonic-gate 		 */
26810Sstevel@tonic-gate 		FCERRPRINT(FDEP_L4, FDEM_INTR,
26820Sstevel@tonic-gate 		    (CE_WARN, "fdc_intr unit %d: nobody sleeping 0x%x",
26830Sstevel@tonic-gate 		    drive, state));
26840Sstevel@tonic-gate 	} else {
26850Sstevel@tonic-gate 		FCERRPRINT(FDEP_L4, FDEM_INTR,
26860Sstevel@tonic-gate 		    (CE_WARN, "fdc_intr: nobody sleeping on %d 0x%x",
26870Sstevel@tonic-gate 		    csb->csb_drive, state));
26880Sstevel@tonic-gate 	}
26890Sstevel@tonic-gate 	/*
26900Sstevel@tonic-gate 	 * This should probably be protected, but, what the
26910Sstevel@tonic-gate 	 * heck...the cost isn't worth the accuracy for this
26920Sstevel@tonic-gate 	 * statistic.
26930Sstevel@tonic-gate 	 */
26940Sstevel@tonic-gate 	if (fcp->c_intrstat)
26950Sstevel@tonic-gate 		KIOIP->intrs[KSTAT_INTR_SPURIOUS]++;
26960Sstevel@tonic-gate 	mutex_exit(&fcp->c_lock);
26970Sstevel@tonic-gate 	return (DDI_INTR_UNCLAIMED);
26980Sstevel@tonic-gate }
26990Sstevel@tonic-gate 
27000Sstevel@tonic-gate /*
27010Sstevel@tonic-gate  * fdwatch
27020Sstevel@tonic-gate  *	is called from timeout() when a floppy operation timer has expired.
27030Sstevel@tonic-gate  */
27040Sstevel@tonic-gate static void
27050Sstevel@tonic-gate fdwatch(void *arg)
27060Sstevel@tonic-gate {
27070Sstevel@tonic-gate 	struct fdcntlr *fcp = (struct fdcntlr *)arg;
27080Sstevel@tonic-gate 	struct fdcsb *csb;
27090Sstevel@tonic-gate 
27100Sstevel@tonic-gate 	mutex_enter(&fcp->c_lock);
27110Sstevel@tonic-gate 
27120Sstevel@tonic-gate 	if (fcp->c_timeid == 0) {
27130Sstevel@tonic-gate 		/*
27140Sstevel@tonic-gate 		 * fdc_intr got here first, ergo, no timeout condition..
27150Sstevel@tonic-gate 		 */
27160Sstevel@tonic-gate 		mutex_exit(&fcp->c_lock);
27170Sstevel@tonic-gate 		return;
27180Sstevel@tonic-gate 	}
27190Sstevel@tonic-gate 
27200Sstevel@tonic-gate 	if (fcp->c_flags & FCFLG_WAITING) {
27210Sstevel@tonic-gate 		if (ddi_dmae_stop(fcp->c_dip, fcp->c_dmachan) != DDI_SUCCESS)
27220Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdwatch: dmae stop failed, "
2723*5295Srandyf 			    "dip %p, dmachan %x\n",
2724*5295Srandyf 			    (void*)fcp->c_dip, fcp->c_dmachan);
27250Sstevel@tonic-gate 		csb = &fcp->c_csb;
27260Sstevel@tonic-gate 		FCERRPRINT(FDEP_L3, FDEM_WATC,
27270Sstevel@tonic-gate 		    (CE_WARN, "fdcwatch unit %d: xstate = %d",
27280Sstevel@tonic-gate 		    csb->csb_drive, csb->csb_xstate));
27290Sstevel@tonic-gate 		drv_usecwait(50);
27300Sstevel@tonic-gate 
27310Sstevel@tonic-gate 		if (inb(fcp->c_regbase + FCR_MSR) != MS_RQM) {
27320Sstevel@tonic-gate 			/*
27330Sstevel@tonic-gate 			 * cntlr is still busy, so reset it
27340Sstevel@tonic-gate 			 */
27350Sstevel@tonic-gate 			csb->csb_xstate = FXS_KILL;
27360Sstevel@tonic-gate 			(void) fdc_statemach(fcp);
27370Sstevel@tonic-gate 			/*
27380Sstevel@tonic-gate 			 * Ignored return.  If failed, warning already
27390Sstevel@tonic-gate 			 * posted.  Returned state irrelevant.
27400Sstevel@tonic-gate 			 */
27410Sstevel@tonic-gate 		} else {
27420Sstevel@tonic-gate 			csb->csb_xstate = FXS_END;
27430Sstevel@tonic-gate 			fcp->c_timeid = 0;
27440Sstevel@tonic-gate 			fcp->c_flags ^= FCFLG_WAITING;
27450Sstevel@tonic-gate 			cv_signal(&fcp->c_iocv);
27460Sstevel@tonic-gate 		}
27470Sstevel@tonic-gate 		csb->csb_cmdstat = EIO;
27480Sstevel@tonic-gate 		fcp->c_flags |= FCFLG_TIMEOUT;
27490Sstevel@tonic-gate 	} else {
27500Sstevel@tonic-gate 		FCERRPRINT(FDEP_L4, FDEM_INTR,
27510Sstevel@tonic-gate 		    (CE_WARN, "fdcwatch: not sleeping for unit %d",
27520Sstevel@tonic-gate 		    fcp->c_csb.csb_drive));
27530Sstevel@tonic-gate 	}
27540Sstevel@tonic-gate 	if (fcp->c_intrstat)
27550Sstevel@tonic-gate 		KIOIP->intrs[KSTAT_INTR_WATCHDOG]++;
27560Sstevel@tonic-gate 	mutex_exit(&fcp->c_lock);
27570Sstevel@tonic-gate }
27580Sstevel@tonic-gate 
27590Sstevel@tonic-gate 
27600Sstevel@tonic-gate static int
27610Sstevel@tonic-gate fdc_statemach(struct fdcntlr *fcp)
27620Sstevel@tonic-gate {
27630Sstevel@tonic-gate 	struct fcu_obj *fjp;
27640Sstevel@tonic-gate 	struct fdcsb *csb = &fcp->c_csb;
27650Sstevel@tonic-gate 	int backoff;
27660Sstevel@tonic-gate 	clock_t time;
27670Sstevel@tonic-gate 	int unit;
27680Sstevel@tonic-gate 
27690Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&fcp->c_lock));
27700Sstevel@tonic-gate 
27710Sstevel@tonic-gate 	unit = csb->csb_drive;
27720Sstevel@tonic-gate 	fjp = fcp->c_unit[unit];
27730Sstevel@tonic-gate 
27740Sstevel@tonic-gate 	csb->csb_oldxs = csb->csb_xstate;
27750Sstevel@tonic-gate 	switch (csb->csb_xstate) {
27760Sstevel@tonic-gate 
27770Sstevel@tonic-gate 	case FXS_START:		/* start of operation */
27780Sstevel@tonic-gate 		ASSERT(fcp->c_timeid == 0);
27790Sstevel@tonic-gate 		time = drv_usectohz(100000 * (unsigned int)csb->csb_timer);
27800Sstevel@tonic-gate 		if (time == 0)
27810Sstevel@tonic-gate 			time = drv_usectohz(2000000);
27820Sstevel@tonic-gate 		fcp->c_timeid = timeout(fdwatch, (void *)fcp, time);
27830Sstevel@tonic-gate 
27840Sstevel@tonic-gate 		if (fcp->c_mtrstate[unit] == FMS_START) {
27850Sstevel@tonic-gate 			/*
27860Sstevel@tonic-gate 			 * wait for motor to get up to speed
27870Sstevel@tonic-gate 			 */
27880Sstevel@tonic-gate 			csb->csb_xstate = FXS_MTRON;
27890Sstevel@tonic-gate 			break;
27900Sstevel@tonic-gate 		}
27910Sstevel@tonic-gate 		/* FALLTHROUGH */
27920Sstevel@tonic-gate 
27930Sstevel@tonic-gate 	case FXS_MTRON:		/* motor is at speed */
27940Sstevel@tonic-gate 		if (fcp->c_mtrstate[unit] != FMS_ON) {
27950Sstevel@tonic-gate 			/* how did we get here ?? */
27960Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdc: selected but motor off");
27970Sstevel@tonic-gate 			return (-1);
27980Sstevel@tonic-gate 		}
27990Sstevel@tonic-gate 		if (fcp->c_curpcyl[unit] != -1 && *csb->csb_cmd != FO_RECAL)
28000Sstevel@tonic-gate 			goto nxs_seek;
28010Sstevel@tonic-gate 		recalcmd[1] = (uchar_t)unit;
28020Sstevel@tonic-gate 		if (fdc_docmd(fcp, recalcmd, 2) == -1) {
28030Sstevel@tonic-gate 			/* cntlr did not accept command bytes */
28040Sstevel@tonic-gate 			fdcquiesce(fcp);
28050Sstevel@tonic-gate 			csb->csb_cmdstat = EIO;
28060Sstevel@tonic-gate 			csb->csb_xstate = FXS_RESET;
28070Sstevel@tonic-gate 			break;
28080Sstevel@tonic-gate 		}
28090Sstevel@tonic-gate 		fcp->c_sekdir[unit] = 0;
28100Sstevel@tonic-gate 		csb->csb_xstate = FXS_RCAL;
28110Sstevel@tonic-gate 		break;
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate 	case FXS_RCAL:		/* forced recalibrate is complete */
28140Sstevel@tonic-gate #if 0	/* #ifdef _VPIX */
28150Sstevel@tonic-gate 	/* WARNING: this code breaks SPARC compatibility */
28160Sstevel@tonic-gate 		if (csb->csb_opflags & CSB_OFRAWIOCTL &&
28170Sstevel@tonic-gate 		    *csb->csb_cmd == FO_RECAL) {
28180Sstevel@tonic-gate 			fcp->c_curpcyl[unit] = 0;
28190Sstevel@tonic-gate 			csb->csb_status = 0;
28200Sstevel@tonic-gate 			goto nxs_cmpl;
28210Sstevel@tonic-gate 		}
28220Sstevel@tonic-gate #endif
28230Sstevel@tonic-gate 		(void) fdc_docmd(fcp, &senseintcmd, 1);
28240Sstevel@tonic-gate 		/*
28250Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_docmd.
28260Sstevel@tonic-gate 		 * fdc_results retrieves the controller/drive status
28270Sstevel@tonic-gate 		 */
28280Sstevel@tonic-gate 		(void) fdc_result(fcp, csb->csb_rslt, 2);
28290Sstevel@tonic-gate 		/*
28300Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_result.
28310Sstevel@tonic-gate 		 * Actual results checked below
28320Sstevel@tonic-gate 		 */
28330Sstevel@tonic-gate 		if ((csb->csb_status = ((*csb->csb_rslt ^ S0_SEKEND) &
28340Sstevel@tonic-gate 		    (S0_ICMASK | S0_SEKEND | S0_ECHK | S0_NOTRDY))) != 0) {
28350Sstevel@tonic-gate 			FCERRPRINT(FDEP_L3, FDEM_EXEC,
28360Sstevel@tonic-gate 			    (CE_WARN, "fdc_statemach unit %d: recal result %x",
28370Sstevel@tonic-gate 			    csb->csb_drive, *csb->csb_rslt));
28380Sstevel@tonic-gate 			fdcquiesce(fcp);
28390Sstevel@tonic-gate 			csb->csb_cmdstat = EIO;
28400Sstevel@tonic-gate 			csb->csb_xstate = FXS_RESET;
28410Sstevel@tonic-gate 			break;
28420Sstevel@tonic-gate 		}
28430Sstevel@tonic-gate 		if (unit != (*csb->csb_rslt & 3) || csb->csb_rslt[1]) {
28440Sstevel@tonic-gate 			csb->csb_status = S0_SEKEND;
28450Sstevel@tonic-gate 			goto nxs_cmpl;
28460Sstevel@tonic-gate 		}
28470Sstevel@tonic-gate 		fcp->c_curpcyl[unit] = csb->csb_rslt[1];
28480Sstevel@tonic-gate 		if (*csb->csb_cmd == FO_RECAL)
28490Sstevel@tonic-gate 			goto nxs_cmpl;
28500Sstevel@tonic-gate nxs_seek:
28510Sstevel@tonic-gate 		if (*csb->csb_cmd != FO_SEEK &&
28520Sstevel@tonic-gate 		    csb->csb_npcyl == fcp->c_curpcyl[unit])
28530Sstevel@tonic-gate 			goto nxs_doit;
28540Sstevel@tonic-gate 		fcp->c_sekdir[unit] = csb->csb_npcyl - fcp->c_curpcyl[unit];
28550Sstevel@tonic-gate 		/* FALLTHROUGH */
28560Sstevel@tonic-gate 
28570Sstevel@tonic-gate 	case FXS_DKCHGX:	/* reset Disk-Change latch */
28580Sstevel@tonic-gate 		(void) fdcseek(fcp, csb->csb_cmd[1], csb->csb_npcyl);
28590Sstevel@tonic-gate 		/*
28600Sstevel@tonic-gate 		 * Ignored return.  If command rejected, warnig already posted
28610Sstevel@tonic-gate 		 * by fdc_docmd().
28620Sstevel@tonic-gate 		 */
28630Sstevel@tonic-gate 		csb->csb_xstate = FXS_SEEK;
28640Sstevel@tonic-gate 		break;
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate 	case FXS_RESTART:	/* special restart of read/write operation */
28670Sstevel@tonic-gate 		ASSERT(fcp->c_timeid == 0);
28680Sstevel@tonic-gate 		time = drv_usectohz(100000 * csb->csb_timer);
28690Sstevel@tonic-gate 		if (time == 0)
28700Sstevel@tonic-gate 			time = drv_usectohz(2000000);
28710Sstevel@tonic-gate 		fcp->c_timeid = timeout(fdwatch, (void *)fcp, time);
28720Sstevel@tonic-gate 
28730Sstevel@tonic-gate 		if (fcp->c_mtrstate[unit] != FMS_ON) {
28740Sstevel@tonic-gate 			cmn_err(CE_WARN, "fdc: selected but motor off");
28750Sstevel@tonic-gate 			return (-1);
28760Sstevel@tonic-gate 		}
28770Sstevel@tonic-gate 		if ((csb->csb_npcyl == 0 || fcp->c_sekdir[unit] >= 0) &&
28780Sstevel@tonic-gate 		    (int)csb->csb_cmd[2] < (fjp->fj_chars->fdc_ncyl - 1))
28790Sstevel@tonic-gate 			backoff = csb->csb_npcyl + 1;
28800Sstevel@tonic-gate 		else
28810Sstevel@tonic-gate 			backoff = csb->csb_npcyl - 1;
28820Sstevel@tonic-gate 		(void) fdcseek(fcp, csb->csb_cmd[1], backoff);
28830Sstevel@tonic-gate 		/*
28840Sstevel@tonic-gate 		 * Ignored return.  If command rejected, warnig already posted
28850Sstevel@tonic-gate 		 * by fdc_docmd().
28860Sstevel@tonic-gate 		 */
28870Sstevel@tonic-gate 		csb->csb_xstate = FXS_RESEEK;
28880Sstevel@tonic-gate 		break;
28890Sstevel@tonic-gate 
28900Sstevel@tonic-gate 	case FXS_RESEEK:	/* seek to backoff-cyl complete */
28910Sstevel@tonic-gate 		(void) fdc_docmd(fcp, &senseintcmd, 1);
28920Sstevel@tonic-gate 		/*
28930Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_docmd.
28940Sstevel@tonic-gate 		 * fdc_results retrieves the controller/drive status
28950Sstevel@tonic-gate 		 */
28960Sstevel@tonic-gate 		(void) fdc_result(fcp, csb->csb_rslt, 2);
28970Sstevel@tonic-gate 		/*
28980Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_result.
28990Sstevel@tonic-gate 		 * Actual results checked below
29000Sstevel@tonic-gate 		 */
29010Sstevel@tonic-gate 		if ((csb->csb_status = ((*csb->csb_rslt ^ S0_SEKEND) &
29020Sstevel@tonic-gate 		    (S0_ICMASK | S0_SEKEND | S0_ECHK | S0_NOTRDY))) != 0)
29030Sstevel@tonic-gate 			goto nxs_cmpl;
29040Sstevel@tonic-gate 		(void) fdcseek(fcp, csb->csb_cmd[1], csb->csb_npcyl);
29050Sstevel@tonic-gate 		/*
29060Sstevel@tonic-gate 		 * Ignored return.  If command rejected, warnig already posted
29070Sstevel@tonic-gate 		 * by fdc_docmd().
29080Sstevel@tonic-gate 		 */
29090Sstevel@tonic-gate 		csb->csb_xstate = FXS_SEEK;
29100Sstevel@tonic-gate 		break;
29110Sstevel@tonic-gate 
29120Sstevel@tonic-gate 	case FXS_SEEK:		/* seek complete */
29130Sstevel@tonic-gate #if 0	/* #ifdef _VPIX */
29140Sstevel@tonic-gate 	/* WARNING: this code breaks SPARC compatibility and */
29150Sstevel@tonic-gate 	/* rawioctls in fdformat */
29160Sstevel@tonic-gate 		if (csb->csb_opflags & CSB_OFRAWIOCTL) {
29170Sstevel@tonic-gate 			fcp->c_curpcyl[unit] = csb->csb_npcyl;
29180Sstevel@tonic-gate 			csb->csb_status = 0;
29190Sstevel@tonic-gate 			goto nxs_cmpl;
29200Sstevel@tonic-gate 		}
29210Sstevel@tonic-gate #endif
29220Sstevel@tonic-gate 		(void) fdc_docmd(fcp, &senseintcmd, 1);
29230Sstevel@tonic-gate 		/*
29240Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_docmd.
29250Sstevel@tonic-gate 		 * fdc_results retrieves the controller/drive status
29260Sstevel@tonic-gate 		 */
29270Sstevel@tonic-gate 		(void) fdc_result(fcp, csb->csb_rslt, 2);
29280Sstevel@tonic-gate 		/*
29290Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_result.
29300Sstevel@tonic-gate 		 * Actual results checked below
29310Sstevel@tonic-gate 		 */
29320Sstevel@tonic-gate 		if ((csb->csb_status = ((*csb->csb_rslt ^ S0_SEKEND) &
29330Sstevel@tonic-gate 		    (S0_ICMASK | S0_SEKEND | S0_ECHK | S0_NOTRDY))) != 0)
29340Sstevel@tonic-gate 			goto nxs_cmpl;
29350Sstevel@tonic-gate 		if (unit != (*csb->csb_rslt & 3) ||
29360Sstevel@tonic-gate 		    csb->csb_rslt[1] != csb->csb_npcyl) {
29370Sstevel@tonic-gate 			csb->csb_status = S0_SEKEND;
29380Sstevel@tonic-gate 			goto nxs_cmpl;
29390Sstevel@tonic-gate 		};
29400Sstevel@tonic-gate 		fcp->c_curpcyl[unit] = csb->csb_rslt[1];
29410Sstevel@tonic-gate 		/* use motor_timer to delay for head settle */
29420Sstevel@tonic-gate 		mutex_enter(&fcp->c_dorlock);
29430Sstevel@tonic-gate 		(void) fdc_motorsm(fjp, FMI_DELAYCMD,
29440Sstevel@tonic-gate 		    fjp->fj_drive->fdd_headsettle / 1000);
29450Sstevel@tonic-gate 		/*
29460Sstevel@tonic-gate 		 * Return value ignored - fdcmotort deals with failure.
29470Sstevel@tonic-gate 		 */
29480Sstevel@tonic-gate 		mutex_exit(&fcp->c_dorlock);
29490Sstevel@tonic-gate 		csb->csb_xstate = FXS_HDST;
29500Sstevel@tonic-gate 		break;
29510Sstevel@tonic-gate 
29520Sstevel@tonic-gate 	case FXS_HDST:		/* head settle */
29530Sstevel@tonic-gate 		if (*csb->csb_cmd == FO_SEEK)
29540Sstevel@tonic-gate 			goto nxs_cmpl;
29550Sstevel@tonic-gate 		if ((*csb->csb_cmd & ~FO_MFM) == FO_FRMT)
29560Sstevel@tonic-gate 			goto nxs_doit;
29570Sstevel@tonic-gate 		fdcreadid(fcp, csb);
29580Sstevel@tonic-gate 		csb->csb_xstate = FXS_RDID;
29590Sstevel@tonic-gate 		break;
29600Sstevel@tonic-gate 
29610Sstevel@tonic-gate 	case FXS_RDID:		/* read ID complete */
29620Sstevel@tonic-gate 		(void) fdc_result(fcp, csb->csb_rslt, 7);
29630Sstevel@tonic-gate 		/*
29640Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_result.
29650Sstevel@tonic-gate 		 * Actual results checked below
29660Sstevel@tonic-gate 		 */
29670Sstevel@tonic-gate 		if ((csb->csb_status = (*csb->csb_rslt &
29680Sstevel@tonic-gate 		    (S0_ICMASK | S0_ECHK | S0_NOTRDY))) != 0)
29690Sstevel@tonic-gate 			goto nxs_cmpl;
29700Sstevel@tonic-gate 		if (csb->csb_cmd[2] != csb->csb_rslt[3]) {
29710Sstevel@tonic-gate 			/* at wrong logical cylinder */
29720Sstevel@tonic-gate 			csb->csb_status = S0_SEKEND;
29730Sstevel@tonic-gate 			goto nxs_cmpl;
29740Sstevel@tonic-gate 		};
29750Sstevel@tonic-gate 		goto nxs_doit;
29760Sstevel@tonic-gate 
29770Sstevel@tonic-gate 	case FXS_DOIT:		/* do original operation */
29780Sstevel@tonic-gate 		ASSERT(fcp->c_timeid == 0);
29790Sstevel@tonic-gate 		time = drv_usectohz(100000 * csb->csb_timer);
29800Sstevel@tonic-gate 		if (time == 0)
29810Sstevel@tonic-gate 			time = drv_usectohz(2000000);
29820Sstevel@tonic-gate 		fcp->c_timeid = timeout(fdwatch, (void *)fcp, time);
29830Sstevel@tonic-gate nxs_doit:
29840Sstevel@tonic-gate 		if (fdc_docmd(fcp, csb->csb_cmd, csb->csb_ncmds) == -1) {
29850Sstevel@tonic-gate 			/* cntlr did not accept command bytes */
29860Sstevel@tonic-gate 			fdcquiesce(fcp);
29870Sstevel@tonic-gate 			csb->csb_xstate = FXS_RESET;
29880Sstevel@tonic-gate 			csb->csb_cmdstat = EIO;
29890Sstevel@tonic-gate 			break;
29900Sstevel@tonic-gate 		}
29910Sstevel@tonic-gate 		csb->csb_xstate = FXS_DOWT;
29920Sstevel@tonic-gate 		break;
29930Sstevel@tonic-gate 
29940Sstevel@tonic-gate 	case FXS_DOWT:		/* operation complete */
29950Sstevel@tonic-gate 		(void) fdc_result(fcp, csb->csb_rslt, csb->csb_nrslts);
29960Sstevel@tonic-gate 		/*
29970Sstevel@tonic-gate 		 * Ignored return. If failed, warning was issued by fdc_result.
29980Sstevel@tonic-gate 		 * Actual results checked below.
29990Sstevel@tonic-gate 		 */
30000Sstevel@tonic-gate 		if (*csb->csb_cmd == FO_SDRV) {
30010Sstevel@tonic-gate 			csb->csb_status =
30020Sstevel@tonic-gate 			    (*csb->csb_rslt ^ (S3_DRRDY | S3_2SIDE)) &
30030Sstevel@tonic-gate 			    ~(S3_HEAD | S3_UNIT);
30040Sstevel@tonic-gate 		} else {
30050Sstevel@tonic-gate 			csb->csb_status = *csb->csb_rslt &
30060Sstevel@tonic-gate 			    (S0_ICMASK | S0_ECHK | S0_NOTRDY);
30070Sstevel@tonic-gate 		}
30080Sstevel@tonic-gate nxs_cmpl:
30090Sstevel@tonic-gate 		if (csb->csb_status)
30100Sstevel@tonic-gate 			csb->csb_cmdstat = EIO;
30110Sstevel@tonic-gate 		csb->csb_xstate = FXS_END;
30120Sstevel@tonic-gate 
30130Sstevel@tonic-gate 		/*  remove watchdog timer if armed and not already triggered */
30140Sstevel@tonic-gate 		if (fcp->c_timeid != 0) {
30150Sstevel@tonic-gate 			timeout_id_t timeid;
30160Sstevel@tonic-gate 			timeid = fcp->c_timeid;
30170Sstevel@tonic-gate 			fcp->c_timeid = 0;
30180Sstevel@tonic-gate 			mutex_exit(&fcp->c_lock);
30190Sstevel@tonic-gate 			(void) untimeout(timeid);
30200Sstevel@tonic-gate 			mutex_enter(&fcp->c_lock);
30210Sstevel@tonic-gate 		}
30220Sstevel@tonic-gate 		break;
30230Sstevel@tonic-gate 
30240Sstevel@tonic-gate 	case FXS_KILL:		/* quiesce cntlr by reset */
30250Sstevel@tonic-gate 		fdcquiesce(fcp);
30260Sstevel@tonic-gate 		fcp->c_timeid = timeout(fdwatch, (void *)fcp,
30270Sstevel@tonic-gate 		    drv_usectohz(2000000));
30280Sstevel@tonic-gate 		csb->csb_xstate = FXS_RESET;
30290Sstevel@tonic-gate 		break;
30300Sstevel@tonic-gate 
30310Sstevel@tonic-gate 	case FXS_RESET:		/* int from reset */
30320Sstevel@tonic-gate 		for (unit = 0; unit < NFDUN; unit++) {
30330Sstevel@tonic-gate 			(void) fdcsense_int(fcp, NULL, NULL);
30340Sstevel@tonic-gate 			fcp->c_curpcyl[unit] = -1;
30350Sstevel@tonic-gate 		}
30360Sstevel@tonic-gate 		if (fcp->c_timeid != 0) {
30370Sstevel@tonic-gate 			timeout_id_t timeid;
30380Sstevel@tonic-gate 			timeid = fcp->c_timeid;
30390Sstevel@tonic-gate 			fcp->c_timeid = 0;
30400Sstevel@tonic-gate 			mutex_exit(&fcp->c_lock);
30410Sstevel@tonic-gate 			(void) untimeout(timeid);
30420Sstevel@tonic-gate 			mutex_enter(&fcp->c_lock);
30430Sstevel@tonic-gate 		}
30440Sstevel@tonic-gate 		csb->csb_xstate = FXS_END;
30450Sstevel@tonic-gate 		break;
30460Sstevel@tonic-gate 
30470Sstevel@tonic-gate 	default:
30480Sstevel@tonic-gate 		cmn_err(CE_WARN, "fdc: statemach, unknown state");
30490Sstevel@tonic-gate 		return (-1);
30500Sstevel@tonic-gate 	}
30510Sstevel@tonic-gate 	FCERRPRINT(FDEP_L1, FDEM_EXEC,
30520Sstevel@tonic-gate 	    (CE_CONT, "fdc_statemach unit %d: %d -> %d\n",
30530Sstevel@tonic-gate 	    csb->csb_drive, csb->csb_oldxs, csb->csb_xstate));
30540Sstevel@tonic-gate 	return (csb->csb_xstate);
30550Sstevel@tonic-gate }
30560Sstevel@tonic-gate 
30570Sstevel@tonic-gate 
30580Sstevel@tonic-gate /*
30590Sstevel@tonic-gate  * routine to program a command into the floppy disk controller.
30600Sstevel@tonic-gate  */
30610Sstevel@tonic-gate int
30620Sstevel@tonic-gate fdc_docmd(struct fdcntlr *fcp, uchar_t *oplistp, uchar_t count)
30630Sstevel@tonic-gate {
30640Sstevel@tonic-gate 	int ntries;
30650Sstevel@tonic-gate 
30660Sstevel@tonic-gate 	ASSERT(count >= 1);
30670Sstevel@tonic-gate 	FCERRPRINT(FDEP_L0, FDEM_EXEC,
30680Sstevel@tonic-gate 	    (CE_CONT, "fdc_docmd: %x %x %x %x %x %x %x %x %x\n",
30690Sstevel@tonic-gate 	    oplistp[0], oplistp[1], oplistp[2], oplistp[3], oplistp[4],
30700Sstevel@tonic-gate 	    oplistp[5], oplistp[6], oplistp[7], oplistp[8]));
30710Sstevel@tonic-gate 
30720Sstevel@tonic-gate 	do {
30730Sstevel@tonic-gate 		ntries = FDC_RQM_RETRY;
30740Sstevel@tonic-gate 		do {
30750Sstevel@tonic-gate 			if ((inb(fcp->c_regbase + FCR_MSR) & (MS_RQM|MS_DIO))
30760Sstevel@tonic-gate 			    == MS_RQM)
30770Sstevel@tonic-gate 				break;
30780Sstevel@tonic-gate 			else
30790Sstevel@tonic-gate 				drv_usecwait(1);
30800Sstevel@tonic-gate 		} while (--ntries);
30810Sstevel@tonic-gate 		if (ntries == 0) {
30820Sstevel@tonic-gate 			FCERRPRINT(FDEP_L3, FDEM_EXEC,
30830Sstevel@tonic-gate 			    (CE_WARN, "fdc_docmd: ctlr not ready"));
30840Sstevel@tonic-gate 			return (-1);
30850Sstevel@tonic-gate 		}
30860Sstevel@tonic-gate 		outb(fcp->c_regbase + FCR_DATA, *oplistp++);
30870Sstevel@tonic-gate 		drv_usecwait(16);	/* See comment in fdc_result() */
30880Sstevel@tonic-gate 	} while (--count);
30890Sstevel@tonic-gate 	return (0);
30900Sstevel@tonic-gate }
30910Sstevel@tonic-gate 
30920Sstevel@tonic-gate 
30930Sstevel@tonic-gate /*
30940Sstevel@tonic-gate  * Routine to return controller/drive status information.
30950Sstevel@tonic-gate  * The diskette-controller data-register is read the
30960Sstevel@tonic-gate  * requested number of times and the results are placed in
30970Sstevel@tonic-gate  * consecutive memory locations starting at the passed
30980Sstevel@tonic-gate  * address.
30990Sstevel@tonic-gate  */
31000Sstevel@tonic-gate int
31010Sstevel@tonic-gate fdc_result(struct fdcntlr *fcp, uchar_t *rsltp, uchar_t rcount)
31020Sstevel@tonic-gate {
31030Sstevel@tonic-gate 	int ntries;
31040Sstevel@tonic-gate 	uchar_t *abresultp = rsltp;
31050Sstevel@tonic-gate 	uchar_t stat;
31060Sstevel@tonic-gate 	int laxative = 7;
31070Sstevel@tonic-gate 
31080Sstevel@tonic-gate 	ntries = 10 * FDC_RQM_RETRY;
31090Sstevel@tonic-gate 	do {
31100Sstevel@tonic-gate 		do {
31110Sstevel@tonic-gate 			if ((inb(fcp->c_regbase + FCR_MSR) &
31120Sstevel@tonic-gate 			    (MS_RQM | MS_DIO)) == (MS_RQM | MS_DIO))
31130Sstevel@tonic-gate 				break;
31140Sstevel@tonic-gate 			else
31150Sstevel@tonic-gate 				drv_usecwait(10);
31160Sstevel@tonic-gate 		} while (--ntries);
31170Sstevel@tonic-gate 		if (!ntries) {
31180Sstevel@tonic-gate 			FCERRPRINT(FDEP_L3, FDEM_EXEC,
31190Sstevel@tonic-gate 			    (CE_WARN, "fdc_result: ctlr not ready"));
31200Sstevel@tonic-gate 			return (-2);
31210Sstevel@tonic-gate 		}
31220Sstevel@tonic-gate 		*rsltp++ = inb(fcp->c_regbase + FCR_DATA);
31230Sstevel@tonic-gate 
31240Sstevel@tonic-gate 		/*
31250Sstevel@tonic-gate 		 * The PRM suggests waiting for 14.5 us.
31260Sstevel@tonic-gate 		 * Adding a bit more to cover the case of bad calibration
31270Sstevel@tonic-gate 		 * of drv_usecwait().
31280Sstevel@tonic-gate 		 */
31290Sstevel@tonic-gate 		drv_usecwait(16);
31300Sstevel@tonic-gate 		ntries = FDC_RQM_RETRY;
31310Sstevel@tonic-gate 	} while (--rcount);
31320Sstevel@tonic-gate 	while ((inb(fcp->c_regbase + FCR_MSR) & MS_CB) && laxative--) {
31330Sstevel@tonic-gate 		FCERRPRINT(FDEP_L3, FDEM_EXEC,
31340Sstevel@tonic-gate 		    (CE_WARN, "fdc_result: ctlr still busy"));
31350Sstevel@tonic-gate 		/*
31360Sstevel@tonic-gate 		 * try to complete Result phase by purging
31370Sstevel@tonic-gate 		 * result bytes queued for reading
31380Sstevel@tonic-gate 		 */
31390Sstevel@tonic-gate 		*abresultp = S0_IVCMD;
31400Sstevel@tonic-gate 		do {
31410Sstevel@tonic-gate 			stat = inb(fcp->c_regbase + FCR_MSR) &
31420Sstevel@tonic-gate 			    (MS_RQM | MS_DIO);
31430Sstevel@tonic-gate 			if (stat == MS_RQM) {
31440Sstevel@tonic-gate 				/*
31450Sstevel@tonic-gate 				 * Result phase is complete
31460Sstevel@tonic-gate 				 * but did we get the results corresponding to
31470Sstevel@tonic-gate 				 * the command we think we executed?
31480Sstevel@tonic-gate 				 */
31490Sstevel@tonic-gate 				return (-1);
31500Sstevel@tonic-gate 			}
31510Sstevel@tonic-gate 			if (stat == (MS_RQM | MS_DIO))
31520Sstevel@tonic-gate 				break;
31530Sstevel@tonic-gate 			else
31540Sstevel@tonic-gate 				drv_usecwait(10);
31550Sstevel@tonic-gate 		} while (--ntries);
31560Sstevel@tonic-gate 		if (!ntries || !laxative) {
31570Sstevel@tonic-gate 			FCERRPRINT(FDEP_L3, FDEM_EXEC,
31580Sstevel@tonic-gate 			    (CE_WARN,
31590Sstevel@tonic-gate 			    "fdc_result: ctlr still busy and not ready"));
31600Sstevel@tonic-gate 			return (-3);
31610Sstevel@tonic-gate 		}
31620Sstevel@tonic-gate 		(void) inb(fcp->c_regbase + FCR_DATA);
31630Sstevel@tonic-gate 
31640Sstevel@tonic-gate 		drv_usecwait(16);	/* See comment above */
31650Sstevel@tonic-gate 		ntries = FDC_RQM_RETRY;
31660Sstevel@tonic-gate 	}
31670Sstevel@tonic-gate 	return (0);
31680Sstevel@tonic-gate }
31690Sstevel@tonic-gate 
31700Sstevel@tonic-gate /*
31710Sstevel@tonic-gate  *  Function: get_unit()
31720Sstevel@tonic-gate  *
31730Sstevel@tonic-gate  *  Assumptions:  ioaddr is either 0x3f0 or 0x370
31740Sstevel@tonic-gate  */
31750Sstevel@tonic-gate static int
31760Sstevel@tonic-gate get_unit(dev_info_t *dip, int *cntrl_num)
31770Sstevel@tonic-gate {
31780Sstevel@tonic-gate 	int ioaddr;
31790Sstevel@tonic-gate 
31800Sstevel@tonic-gate 	if (get_ioaddr(dip, &ioaddr) != DDI_SUCCESS)
31810Sstevel@tonic-gate 		return (DDI_FAILURE);
31820Sstevel@tonic-gate 
31830Sstevel@tonic-gate 	switch (ioaddr) {
31840Sstevel@tonic-gate 	case 0x3f0:
31850Sstevel@tonic-gate 		*cntrl_num = 0;
31860Sstevel@tonic-gate 		break;
31870Sstevel@tonic-gate 
31880Sstevel@tonic-gate 	case 0x370:
31890Sstevel@tonic-gate 		*cntrl_num = 1;
31900Sstevel@tonic-gate 		break;
31910Sstevel@tonic-gate 
31920Sstevel@tonic-gate 	default:
31930Sstevel@tonic-gate 		return (DDI_FAILURE);
31940Sstevel@tonic-gate 	}
31950Sstevel@tonic-gate 	return (DDI_SUCCESS);
31960Sstevel@tonic-gate }
31970Sstevel@tonic-gate 
31980Sstevel@tonic-gate static int
31990Sstevel@tonic-gate get_ioaddr(dev_info_t *dip, int *ioaddr)
32000Sstevel@tonic-gate {
32010Sstevel@tonic-gate 	int reglen, nregs, i;
32020Sstevel@tonic-gate 	int status = DDI_FAILURE;
32030Sstevel@tonic-gate 	struct {
32040Sstevel@tonic-gate 		int bustype;
32050Sstevel@tonic-gate 		int base;
32060Sstevel@tonic-gate 		int size;
32070Sstevel@tonic-gate 	} *reglist;
32080Sstevel@tonic-gate 
32090Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
3210*5295Srandyf 	    "reg", (caddr_t)&reglist, &reglen) != DDI_PROP_SUCCESS) {
32110Sstevel@tonic-gate 		cmn_err(CE_WARN, "fdc: reg property not found");
32120Sstevel@tonic-gate 		return (DDI_FAILURE);
32130Sstevel@tonic-gate 	}
32140Sstevel@tonic-gate 
32150Sstevel@tonic-gate 	nregs = reglen / sizeof (*reglist);
32160Sstevel@tonic-gate 	for (i = 0; i < nregs; i++) {
32170Sstevel@tonic-gate 		if (reglist[i].bustype == 1) {
32180Sstevel@tonic-gate 			*ioaddr = reglist[i].base;
32190Sstevel@tonic-gate 			status = DDI_SUCCESS;
32200Sstevel@tonic-gate 			break;
32210Sstevel@tonic-gate 		}
32220Sstevel@tonic-gate 	}
32230Sstevel@tonic-gate 	kmem_free(reglist, reglen);
32240Sstevel@tonic-gate 
32250Sstevel@tonic-gate 	if (status == DDI_SUCCESS) {
32260Sstevel@tonic-gate 		if (*ioaddr == 0x3f2 || *ioaddr == 0x372) {
32270Sstevel@tonic-gate 			/*
32280Sstevel@tonic-gate 			 * Some BIOS's (ASUS is one) don't include first
32290Sstevel@tonic-gate 			 * two IO ports in the floppy controller resources.
32300Sstevel@tonic-gate 			 */
32310Sstevel@tonic-gate 
32320Sstevel@tonic-gate 			*ioaddr -= 2; /* step back to 0x3f0 or 0x370 */
32330Sstevel@tonic-gate 
32340Sstevel@tonic-gate 			/*
32350Sstevel@tonic-gate 			 * It would be nice to update the regs property as well
32360Sstevel@tonic-gate 			 * so device pathname contains 3f0 instead of 3f2, but
32370Sstevel@tonic-gate 			 * updating the regs now won't have this effect as that
32380Sstevel@tonic-gate 			 * component of the device pathname has already been
32390Sstevel@tonic-gate 			 * constructed by the ISA nexus driver.
32400Sstevel@tonic-gate 			 *
32410Sstevel@tonic-gate 			 * reglist[i].base -= 2;
32420Sstevel@tonic-gate 			 * reglist[i].size += 2;
32430Sstevel@tonic-gate 			 * dev = makedevice(ddi_driver_major(dip), 0);
32440Sstevel@tonic-gate 			 * ddi_prop_update_int_array(dev, dip, "reg",
32450Sstevel@tonic-gate 			 *    (int *)reglist, reglen / sizeof (int));
32460Sstevel@tonic-gate 			 */
32470Sstevel@tonic-gate 		}
32480Sstevel@tonic-gate 	}
32490Sstevel@tonic-gate 
32500Sstevel@tonic-gate 	return (status);
32510Sstevel@tonic-gate }
3252