xref: /onnv-gate/usr/src/uts/sun/io/fd.c (revision 7656:2621e50fdf4a)
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*7656SSherry.Moore@Sun.COM  * Common Development and Distribution License (the "License").
6*7656SSherry.Moore@Sun.COM  * 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*7656SSherry.Moore@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * Intel 82077 Floppy Disk Driver
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * Notes
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  *	0. The driver supports two flavors of hardware design:
350Sstevel@tonic-gate  *		"SUNW,fdtwo"	- sun4m	- 82077 with sun4m style Auxio
360Sstevel@tonic-gate  *		"fdthree"  - sun4u - 82077 with DMA
370Sstevel@tonic-gate  *	   In addition it supports an apparent bug in some versions of
380Sstevel@tonic-gate  *	   the 82077 controller.
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  *	1. The driver is mostly set up for multiple controllers, multiple
410Sstevel@tonic-gate  *	drives. However- we *do* assume the use of the AUXIO register, and
420Sstevel@tonic-gate  *	if we ever have > 1 fdc, we'll have to see what that means. This
430Sstevel@tonic-gate  *	is all intrinsically machine specific, but there isn't much we
440Sstevel@tonic-gate  *	can do about it.
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  *	2. The driver also is structured to deal with one drive active at
470Sstevel@tonic-gate  *	a time. This is because the 82072 chip (no longer supported) was
480Sstevel@tonic-gate  *	known to be buggy with respect to overlapped seeks.
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  *	3. The high level interrupt code is in assembler, and runs in a
510Sstevel@tonic-gate  *	sparc trap window. It acts as a pseudo-dma engine as well as
520Sstevel@tonic-gate  *	handles a couple of other interrupts. When it gets its job done,
530Sstevel@tonic-gate  *	it schedules a second stage interrupt (soft interrupt) which
540Sstevel@tonic-gate  *	is then fielded here in fd_lointr.  When DMA is used, the fdintr_dma
550Sstevel@tonic-gate  *	interrupt handler is used.
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  *	4. Nearly all locking is done on a lower level MUTEX_DRIVER
580Sstevel@tonic-gate  *	mutex. The locking is quite conservative, and is generally
590Sstevel@tonic-gate  *	established very close to any of the entries into the driver.
600Sstevel@tonic-gate  *	There is nearly no locking done of the high level MUTEX_DRIVER
610Sstevel@tonic-gate  *	mutex (which generally is a SPIN mutex because the floppy usually
620Sstevel@tonic-gate  *	interrupts above LOCK_LEVEL). The assembler high level interrupt
630Sstevel@tonic-gate  *	handler grabs the high level mutex, but the code in the driver
640Sstevel@tonic-gate  *	here is especially structured to not need to do this.
650Sstevel@tonic-gate  *
660Sstevel@tonic-gate  *	5. Fdrawioctl commands that pass data are not optimized for
670Sstevel@tonic-gate  *	speed. If they need to be faster, the driver structure will
680Sstevel@tonic-gate  *	have to be redone such that fdrawioctl calls physio after
690Sstevel@tonic-gate  *	cons'ing up a uio structure and that fdstart will be able
700Sstevel@tonic-gate  *	to detect that a particular buffer is a 'special' buffer.
710Sstevel@tonic-gate  *
720Sstevel@tonic-gate  *	6. Removable media support is not complete.
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  */
750Sstevel@tonic-gate 
760Sstevel@tonic-gate #include <sys/param.h>
770Sstevel@tonic-gate #include <sys/buf.h>
780Sstevel@tonic-gate #include <sys/ioctl.h>
790Sstevel@tonic-gate #include <sys/uio.h>
800Sstevel@tonic-gate #include <sys/open.h>
810Sstevel@tonic-gate #include <sys/conf.h>
820Sstevel@tonic-gate #include <sys/file.h>
830Sstevel@tonic-gate #include <sys/cmn_err.h>
840Sstevel@tonic-gate #include <sys/debug.h>
850Sstevel@tonic-gate #include <sys/kmem.h>
860Sstevel@tonic-gate #include <sys/stat.h>
870Sstevel@tonic-gate #include <sys/autoconf.h>
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #include <sys/dklabel.h>
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #include <sys/vtoc.h>
920Sstevel@tonic-gate #include <sys/dkio.h>
930Sstevel@tonic-gate #include <sys/fdio.h>
940Sstevel@tonic-gate 
950Sstevel@tonic-gate #include <sys/ddi.h>
960Sstevel@tonic-gate #include <sys/sunddi.h>
970Sstevel@tonic-gate #include <sys/kstat.h>
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * included to check for ELC or SLC which report floppy controller that
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate #include <sys/cpu.h>
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate #include "sys/fdvar.h"
1050Sstevel@tonic-gate #include "sys/fdreg.h"
1060Sstevel@tonic-gate #include "sys/dma_i8237A.h"
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate  * Defines
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate #define	KIOSP	KSTAT_IO_PTR(un->un_iostat)
1120Sstevel@tonic-gate #define	KIOIP	KSTAT_INTR_PTR(fdc->c_intrstat)
1130Sstevel@tonic-gate #define	MEDIUM_DENSITY	0x40
1140Sstevel@tonic-gate #define	SEC_SIZE_CODE	(fdctlr.c_csb->csb_unit]->un_chars->medium ? 3 : 2)
1150Sstevel@tonic-gate #define	CMD_READ	(MT + SK + FDRAW_RDCMD + MFM)
1160Sstevel@tonic-gate #define	CMD_WRITE	(MT + FDRAW_WRCMD + MFM)
1170Sstevel@tonic-gate #define	C		CE_CONT
1180Sstevel@tonic-gate #define	FD_POLLABLE_PROP	"pollable"	/* prom property */
1190Sstevel@tonic-gate #define	FD_MANUAL_EJECT		"manual"	/* prom property */
1200Sstevel@tonic-gate #define	FD_UNIT			"unit"		/* prom property */
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /*
1230Sstevel@tonic-gate  * Sony MP-F17W-50D Drive Parameters
1240Sstevel@tonic-gate  *				High Capacity
1250Sstevel@tonic-gate  *	Capacity unformatted	2Mb
1260Sstevel@tonic-gate  *	Capacity formatted	1.47Mb
1270Sstevel@tonic-gate  *	Encoding method	 MFM
1280Sstevel@tonic-gate  *	Recording density	17434 bpi
1290Sstevel@tonic-gate  *	Track density		135 tpi
1300Sstevel@tonic-gate  *	Cylinders		80
1310Sstevel@tonic-gate  *	Heads			2
1320Sstevel@tonic-gate  *	Tracks			160
1330Sstevel@tonic-gate  *	Rotational speed	300 rpm
1340Sstevel@tonic-gate  *	Transfer rate		250/500 kbps
1350Sstevel@tonic-gate  *	Latency (average)	100 ms
1360Sstevel@tonic-gate  *	Access time
1370Sstevel@tonic-gate  *		Average		95 ms
1380Sstevel@tonic-gate  *		Track to track	3 ms
1390Sstevel@tonic-gate  *	Head settling time	15 ms
1400Sstevel@tonic-gate  *	Motor start time	500 ms
1410Sstevel@tonic-gate  *	Head load time		? ms
1420Sstevel@tonic-gate  */
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  * The max_fd_dma_len is used only when southbridge is present.
1460Sstevel@tonic-gate  * It has been observed that when IFB tests are run the floppy dma could get
1470Sstevel@tonic-gate  * starved and result in underrun errors. After experimenting it was found that
1480Sstevel@tonic-gate  * doing dma in chunks of 2048 works OK.
1490Sstevel@tonic-gate  * The reason for making this a global variable is that there could be
1500Sstevel@tonic-gate  * situations under which the customer would like to get full performance
1510Sstevel@tonic-gate  * from floppy. He may not be having IFB boards that cause underrun errors.
1520Sstevel@tonic-gate  * Under those conditions we could set this value to a much higher value
1530Sstevel@tonic-gate  * by editing /etc/system file.
1540Sstevel@tonic-gate  */
1550Sstevel@tonic-gate int	max_fd_dma_len = 2048;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate static void quiesce_fd_interrupt(struct fdctlr *);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate /*
1600Sstevel@tonic-gate  * Character/block entry points function prototypes
1610Sstevel@tonic-gate  */
1620Sstevel@tonic-gate static int fd_open(dev_t *, int, int, cred_t *);
1630Sstevel@tonic-gate static int fd_close(dev_t, int, int, cred_t *);
1640Sstevel@tonic-gate static int fd_strategy(struct buf *);
1650Sstevel@tonic-gate static int fd_read(dev_t, struct uio *, cred_t *);
1660Sstevel@tonic-gate static int fd_write(dev_t, struct uio *, cred_t *);
1670Sstevel@tonic-gate static int fd_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1680Sstevel@tonic-gate static int
1690Sstevel@tonic-gate fd_prop_op(dev_t, dev_info_t *, ddi_prop_op_t, int, char *, caddr_t, int *);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  * Device operations (dev_ops) entries function prototypes
1730Sstevel@tonic-gate  */
1740Sstevel@tonic-gate static int fd_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1750Sstevel@tonic-gate 		void **result);
1760Sstevel@tonic-gate static int fd_attach(dev_info_t *, ddi_attach_cmd_t);
1770Sstevel@tonic-gate static int fd_detach(dev_info_t *, ddi_detach_cmd_t);
1780Sstevel@tonic-gate static int fd_power(dev_info_t *dip, int component, int level);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate  * Internal functions
1820Sstevel@tonic-gate  */
1830Sstevel@tonic-gate static int fd_attach_check_drive(struct fdctlr *fdc);
1840Sstevel@tonic-gate static int fd_attach_det_ctlr(dev_info_t *dip, struct fdctlr *fdc);
1850Sstevel@tonic-gate static int fd_attach_map_regs(dev_info_t *dip, struct fdctlr *fdc);
1860Sstevel@tonic-gate static int fd_attach_register_interrupts(dev_info_t *dip, struct fdctlr *fdc,
1870Sstevel@tonic-gate     int *hard);
1880Sstevel@tonic-gate static int fd_build_label_vtoc(struct fdunit *, struct vtoc *);
1890Sstevel@tonic-gate static void fd_build_user_vtoc(struct fdunit *, struct vtoc *);
1900Sstevel@tonic-gate static int fdcheckdisk(struct fdctlr *fdc, int unit);
1910Sstevel@tonic-gate static int fd_check_media(dev_t dev, enum dkio_state state);
1920Sstevel@tonic-gate static void fd_cleanup(dev_info_t *dip, struct fdctlr *fdc, int hard,
1930Sstevel@tonic-gate     int locks);
1940Sstevel@tonic-gate static void fdeject(struct fdctlr *, int unit);
1950Sstevel@tonic-gate static int fdexec(struct fdctlr *fdc, int flags);
1960Sstevel@tonic-gate static void fdexec_turn_on_motor(struct fdctlr *fdc, int flags, uint_t unit);
1970Sstevel@tonic-gate static int fdformat(struct fdctlr *fdc, int unit, int cyl, int hd);
1980Sstevel@tonic-gate static caddr_t fd_getauxiova();
1990Sstevel@tonic-gate static struct fdctlr *fd_getctlr(dev_t);
2000Sstevel@tonic-gate static void fdgetcsb(struct fdctlr *);
2010Sstevel@tonic-gate static int fdgetlabel(struct fdctlr *fdc, int unit);
2020Sstevel@tonic-gate enum dkio_state fd_get_media_state(struct fdctlr *, int);
2030Sstevel@tonic-gate static uint_t fdintr_dma();
2040Sstevel@tonic-gate static int fd_isauxiodip(dev_info_t *);
2050Sstevel@tonic-gate static uint_t  fd_lointr(caddr_t arg);
2060Sstevel@tonic-gate static void fd_media_watch(void *);
2070Sstevel@tonic-gate static void fdmotoff(void *);
2080Sstevel@tonic-gate static int fd_part_is_open(struct fdunit *un, int part);
2090Sstevel@tonic-gate static int fdrawioctl(struct fdctlr *, int, intptr_t, int);
2100Sstevel@tonic-gate static int fdrecalseek(struct fdctlr *fdc, int unit, int arg, int execflg);
2110Sstevel@tonic-gate static int fdrecover(struct fdctlr *);
2120Sstevel@tonic-gate static void fdretcsb(struct fdctlr *);
2130Sstevel@tonic-gate static int fdreset(struct fdctlr *);
2140Sstevel@tonic-gate static int fdrw(struct fdctlr *fdc, int, int, int, int, int, caddr_t, uint_t);
2150Sstevel@tonic-gate static void fdselect(struct fdctlr *fdc, int unit, int onoff);
2160Sstevel@tonic-gate static int fdsensedrv(struct fdctlr *fdc, int unit);
2170Sstevel@tonic-gate static int fdsense_chng(struct fdctlr *, int unit);
2180Sstevel@tonic-gate static void fdstart(struct fdctlr *);
2190Sstevel@tonic-gate static int fdstart_dma(register struct fdctlr *fdc, caddr_t addr, uint_t len);
2200Sstevel@tonic-gate static int fd_unit_is_open(struct fdunit *);
2210Sstevel@tonic-gate static void fdunpacklabel(struct packed_label *, struct dk_label *);
2220Sstevel@tonic-gate static int fd_unbind_handle(struct fdctlr *);
2230Sstevel@tonic-gate static void fdwatch(void *);
2240Sstevel@tonic-gate static void set_rotational_speed(struct fdctlr *, int);
2250Sstevel@tonic-gate static int fd_get_media_info(struct fdunit *un, caddr_t buf, int flag);
2260Sstevel@tonic-gate static int fd_pm_lower_power(struct fdctlr *fdc);
2270Sstevel@tonic-gate static int fd_pm_raise_power(struct fdctlr *fdc);
2280Sstevel@tonic-gate static void create_pm_components(dev_info_t *dip);
2290Sstevel@tonic-gate static void set_data_count_register(struct fdctlr *fdc, uint32_t count);
2300Sstevel@tonic-gate static uint32_t get_data_count_register(struct fdctlr *fdc);
2310Sstevel@tonic-gate static void reset_dma_controller(struct fdctlr *fdc);
2320Sstevel@tonic-gate static void set_data_address_register(struct fdctlr *fdc, uint32_t address);
2330Sstevel@tonic-gate static uint32_t get_dma_control_register(struct fdctlr *fdc);
2340Sstevel@tonic-gate static void set_dma_mode(struct fdctlr *fdc, int val);
2350Sstevel@tonic-gate static void set_dma_control_register(struct fdctlr *fdc, uint32_t val);
2360Sstevel@tonic-gate static void release_sb_dma(struct fdctlr *fdc);
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate /*
2390Sstevel@tonic-gate  * External functions
2400Sstevel@tonic-gate  */
2410Sstevel@tonic-gate extern uint_t fd_intr(caddr_t);	/* defined in fd_asm.s */
2420Sstevel@tonic-gate extern void set_auxioreg();
2430Sstevel@tonic-gate extern void call_debug();
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate  * The following macro checks whether the device in a SUSPENDED state.
2490Sstevel@tonic-gate  * As per WDD guide lines the I/O requests to a suspended device should
2500Sstevel@tonic-gate  * be blocked until the device is resumed.
2510Sstevel@tonic-gate  * Here we cv_wait on c_suspend_cv, and there is a cv_broadcast() in
2520Sstevel@tonic-gate  * DDI_RESUME to wake up this thread.
2530Sstevel@tonic-gate  *
2540Sstevel@tonic-gate  * NOTE: This code is not tested because the kernel threads are suspended
2550Sstevel@tonic-gate  * before the device is suspended. So there can not be any I/O requests on
2560Sstevel@tonic-gate  * a suspended device until the cpr implementation changes..
2570Sstevel@tonic-gate  */
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate #define	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc) 	\
2600Sstevel@tonic-gate 		{\
2610Sstevel@tonic-gate 			while (fdc->c_un->un_state == FD_STATE_SUSPENDED) {\
2620Sstevel@tonic-gate 				cv_wait(&fdc->c_suspend_cv, \
2630Sstevel@tonic-gate 							&fdc->c_lolock);\
2640Sstevel@tonic-gate 			}\
2650Sstevel@tonic-gate 		}
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate  * bss (uninitialized data)
2690Sstevel@tonic-gate  */
2700Sstevel@tonic-gate struct	fdctlr	*fdctlrs;	/* linked list of controllers */
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate /*
2730Sstevel@tonic-gate  * initialized data
2740Sstevel@tonic-gate  */
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate static int fd_check_media_time = 5000000;	/* 5 second state check */
2770Sstevel@tonic-gate static int fd_pollable = 0;
2780Sstevel@tonic-gate static uchar_t rwretry = 10;
2790Sstevel@tonic-gate static uchar_t skretry = 5;
2800Sstevel@tonic-gate /* This variable allows the dynamic change of the burst size */
2810Sstevel@tonic-gate static int fd_burstsize = DCSR_BURST_0 | DCSR_BURST_1;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate static struct driver_minor_data {
2840Sstevel@tonic-gate 	char	*name;
2850Sstevel@tonic-gate 	int	minor;
2860Sstevel@tonic-gate 	int	type;
2870Sstevel@tonic-gate } fd_minor [] = {
2880Sstevel@tonic-gate 	{ "a", 0, S_IFBLK},
2890Sstevel@tonic-gate 	{ "b", 1, S_IFBLK},
2900Sstevel@tonic-gate 	{ "c", 2, S_IFBLK},
2910Sstevel@tonic-gate 	{ "a,raw", 0, S_IFCHR},
2920Sstevel@tonic-gate 	{ "b,raw", 1, S_IFCHR},
2930Sstevel@tonic-gate 	{ "c,raw", 2, S_IFCHR},
2940Sstevel@tonic-gate 	{0}
2950Sstevel@tonic-gate };
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /*
2980Sstevel@tonic-gate  * If the interrupt handler is invoked and no controllers expect an
2990Sstevel@tonic-gate  * interrupt, the kernel panics.  The following message is printed out.
3000Sstevel@tonic-gate  */
3010Sstevel@tonic-gate char *panic_msg = "fd_intr: unexpected interrupt\n";
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate  * Specify/Configure cmd parameters
3050Sstevel@tonic-gate  */
3060Sstevel@tonic-gate static uchar_t fdspec[2] = { 0xc2, 0x33 };	/*  "specify" parameters */
3070Sstevel@tonic-gate static uchar_t fdconf[3] = { 0x64, 0x58, 0x00 }; /*  "configure" parameters */
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate /* When DMA is used, set the ND bit to 0 */
3100Sstevel@tonic-gate #define	SPEC_DMA_MODE	0x32
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate  * default characteristics
3140Sstevel@tonic-gate  */
3150Sstevel@tonic-gate static struct fd_char fdtypes[] = {
3160Sstevel@tonic-gate 	{	/* struct fd_char fdchar_1.7MB density */
3170Sstevel@tonic-gate 		0,		/* medium */
3180Sstevel@tonic-gate 		500,		/* transfer rate */
3190Sstevel@tonic-gate 		80,		/* number of cylinders */
3200Sstevel@tonic-gate 		2,		/* number of heads */
3210Sstevel@tonic-gate 		512,		/* sector size */
3220Sstevel@tonic-gate 		21,		/* sectors per track */
3230Sstevel@tonic-gate 		-1,		/* (NA) # steps per data track */
3240Sstevel@tonic-gate 	},
3250Sstevel@tonic-gate 	{	/* struct fd_char fdchar_highdens */
3260Sstevel@tonic-gate 		0, 		/* medium */
3270Sstevel@tonic-gate 		500, 		/* transfer rate */
3280Sstevel@tonic-gate 		80, 		/* number of cylinders */
3290Sstevel@tonic-gate 		2, 		/* number of heads */
3300Sstevel@tonic-gate 		512, 		/* sector size */
3310Sstevel@tonic-gate 		18, 		/* sectors per track */
3320Sstevel@tonic-gate 		-1, 		/* (NA) # steps per data track */
3330Sstevel@tonic-gate 	},
3340Sstevel@tonic-gate 	{	/* struct fd_char fdchar_meddens */
3350Sstevel@tonic-gate 		1, 		/* medium */
3360Sstevel@tonic-gate 		500, 		/* transfer rate */
3370Sstevel@tonic-gate 		77, 		/* number of cylinders */
3380Sstevel@tonic-gate 		2, 		/* number of heads */
3390Sstevel@tonic-gate 		1024, 		/* sector size */
3400Sstevel@tonic-gate 		8, 		/* sectors per track */
3410Sstevel@tonic-gate 		-1, 		/* (NA) # steps per data track */
3420Sstevel@tonic-gate 	},
3430Sstevel@tonic-gate 	{	/* struct fd_char fdchar_lowdens  */
3440Sstevel@tonic-gate 		0, 		/* medium */
3450Sstevel@tonic-gate 		250, 		/* transfer rate */
3460Sstevel@tonic-gate 		80, 		/* number of cylinders */
3470Sstevel@tonic-gate 		2, 		/* number of heads */
3480Sstevel@tonic-gate 		512, 		/* sector size */
3490Sstevel@tonic-gate 		9, 		/* sectors per track */
3500Sstevel@tonic-gate 		-1, 		/* (NA) # steps per data track */
3510Sstevel@tonic-gate 	}
3520Sstevel@tonic-gate };
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate static int nfdtypes = sizeof (fdtypes) / sizeof (fdtypes[0]);
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate /*
3590Sstevel@tonic-gate  * Default Label & partition maps
3600Sstevel@tonic-gate  */
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate static struct packed_label fdlbl_high_21 = {
3630Sstevel@tonic-gate 	{ "3.5\" floppy cyl 80 alt 0 hd 2 sec 21" },
3640Sstevel@tonic-gate 	300,				/* rotations per minute */
3650Sstevel@tonic-gate 	80,				/* # physical cylinders */
3660Sstevel@tonic-gate 	0,				/* alternates per cylinder */
3670Sstevel@tonic-gate 	1,				/* interleave factor */
3680Sstevel@tonic-gate 	80,				/* # of data cylinders */
3690Sstevel@tonic-gate 	0,				/* # of alternate cylinders */
3700Sstevel@tonic-gate 	2,				/* # of heads in this partition */
3710Sstevel@tonic-gate 	21,				/* # of 512 byte sectors per track */
3720Sstevel@tonic-gate 	{
3730Sstevel@tonic-gate 		{ 0, 79 * 2 * 21 },	/* part 0 - all but last cyl */
3740Sstevel@tonic-gate 		{ 79, 1 * 2 * 21 },	/* part 1 - just the last cyl */
3750Sstevel@tonic-gate 		{ 0, 80 * 2 * 21 },	/* part 2 - "the whole thing" */
3760Sstevel@tonic-gate 	},
3770Sstevel@tonic-gate 	{	0,			/* version */
3780Sstevel@tonic-gate 		"",			/* volume label */
3790Sstevel@tonic-gate 		3,			/* no. of partitions */
3800Sstevel@tonic-gate 		{ 0 },			/* partition hdrs, sec 2 */
3810Sstevel@tonic-gate 		{ 0 },			/* mboot info.  unsupported */
3820Sstevel@tonic-gate 		VTOC_SANE,		/* verify vtoc sanity */
3830Sstevel@tonic-gate 		{ 0 },			/* reserved space */
3840Sstevel@tonic-gate 		0,			/* timestamp */
3850Sstevel@tonic-gate 	},
3860Sstevel@tonic-gate };
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate static struct packed_label fdlbl_high_80 = {
3890Sstevel@tonic-gate 	{ "3.5\" floppy cyl 80 alt 0 hd 2 sec 18" },
3900Sstevel@tonic-gate 	300, 				/* rotations per minute */
3910Sstevel@tonic-gate 	80, 				/* # physical cylinders */
3920Sstevel@tonic-gate 	0, 				/* alternates per cylinder */
3930Sstevel@tonic-gate 	1, 				/* interleave factor */
3940Sstevel@tonic-gate 	80, 				/* # of data cylinders */
3950Sstevel@tonic-gate 	0, 				/* # of alternate cylinders */
3960Sstevel@tonic-gate 	2, 				/* # of heads in this partition */
3970Sstevel@tonic-gate 	18, 				/* # of 512 byte sectors per track */
3980Sstevel@tonic-gate 	{
3990Sstevel@tonic-gate 		{ 0, 79 * 2 * 18 }, 	/* part 0 - all but last cyl */
4000Sstevel@tonic-gate 		{ 79, 1 * 2 * 18 }, 	/* part 1 - just the last cyl */
4010Sstevel@tonic-gate 		{ 0, 80 * 2 * 18 }, 	/* part 2 - "the whole thing" */
4020Sstevel@tonic-gate 	},
4030Sstevel@tonic-gate 	{	0,			/* version */
4040Sstevel@tonic-gate 		"",			/* volume label */
4050Sstevel@tonic-gate 		3,			/* no. of partitions */
4060Sstevel@tonic-gate 		{ 0 },			/* partition hdrs, sec 2 */
4070Sstevel@tonic-gate 		{ 0 },			/* mboot info.  unsupported */
4080Sstevel@tonic-gate 		VTOC_SANE,		/* verify vtoc sanity */
4090Sstevel@tonic-gate 		{ 0 },			/* reserved space */
4100Sstevel@tonic-gate 		0,			/* timestamp */
4110Sstevel@tonic-gate 	},
4120Sstevel@tonic-gate };
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate /*
4150Sstevel@tonic-gate  * A medium density diskette has 1024 byte sectors.  The dk_label structure
4160Sstevel@tonic-gate  * assumes a sector is DEVBSIZE (512) bytes.
4170Sstevel@tonic-gate  */
4180Sstevel@tonic-gate static struct packed_label fdlbl_medium_80 = {
4190Sstevel@tonic-gate 	{ "3.5\" floppy cyl 77 alt 0 hd 2 sec 8" },
4200Sstevel@tonic-gate 	360, 				/* rotations per minute */
4210Sstevel@tonic-gate 	77, 				/* # physical cylinders */
4220Sstevel@tonic-gate 	0, 				/* alternates per cylinder */
4230Sstevel@tonic-gate 	1, 				/* interleave factor */
4240Sstevel@tonic-gate 	77, 				/* # of data cylinders */
4250Sstevel@tonic-gate 	0, 				/* # of alternate cylinders */
4260Sstevel@tonic-gate 	2, 				/* # of heads in this partition */
4270Sstevel@tonic-gate 	16, 				/* # of 512 byte sectors per track */
4280Sstevel@tonic-gate 	{
4290Sstevel@tonic-gate 		{ 0, 76 * 2 * 8 * 2 },  /* part 0 - all but last cyl */
4300Sstevel@tonic-gate 		{ 76, 1 * 2 * 8 * 2 },  /* part 1 - just the last cyl */
4310Sstevel@tonic-gate 		{ 0, 77 * 2 * 8 * 2 },  /* part 2 - "the whole thing" */
4320Sstevel@tonic-gate 	},
4330Sstevel@tonic-gate 	{	0,			/* version */
4340Sstevel@tonic-gate 		"",			/* volume label */
4350Sstevel@tonic-gate 		3,			/* no. of partitions */
4360Sstevel@tonic-gate 		{ 0 },			/* partition hdrs, sec 2 */
4370Sstevel@tonic-gate 		{ 0 },			/* mboot info.  unsupported */
4380Sstevel@tonic-gate 		VTOC_SANE,		/* verify vtoc sanity */
4390Sstevel@tonic-gate 		{ 0 },			/* reserved space */
4400Sstevel@tonic-gate 		0,			/* timestamp */
4410Sstevel@tonic-gate 	},
4420Sstevel@tonic-gate };
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate static struct packed_label fdlbl_low_80 = {
4450Sstevel@tonic-gate 	{ "3.5\" floppy cyl 80 alt 0 hd 2 sec 9" },
4460Sstevel@tonic-gate 	300, 				/* rotations per minute */
4470Sstevel@tonic-gate 	80, 				/* # physical cylinders */
4480Sstevel@tonic-gate 	0, 				/* alternates per cylinder */
4490Sstevel@tonic-gate 	1, 				/* interleave factor */
4500Sstevel@tonic-gate 	80, 				/* # of data cylinders */
4510Sstevel@tonic-gate 	0, 				/* # of alternate cylinders */
4520Sstevel@tonic-gate 	2, 				/* # of heads in this partition */
4530Sstevel@tonic-gate 	9, 				/* # of 512 byte sectors per track */
4540Sstevel@tonic-gate 	{
4550Sstevel@tonic-gate 		{ 0, 79 * 2 * 9 }, 	/* part 0 - all but last cyl */
4560Sstevel@tonic-gate 		{ 79, 1 * 2 * 9 }, 	/* part 1 - just the last cyl */
4570Sstevel@tonic-gate 		{ 0, 80 * 2 * 9 }, 	/* part 2 - "the whole thing" */
4580Sstevel@tonic-gate 	},
4590Sstevel@tonic-gate 	{	0,			/* version */
4600Sstevel@tonic-gate 		"",			/* volume label */
4610Sstevel@tonic-gate 		3,			/* no. of partitions */
4620Sstevel@tonic-gate 		{ 0 },			/* partition hdrs, sec 2 */
4630Sstevel@tonic-gate 		{ 0 },			/* mboot info.  unsupported */
4640Sstevel@tonic-gate 		VTOC_SANE,		/* verify vtoc sanity */
4650Sstevel@tonic-gate 		{ 0 },			/* reserved space */
4660Sstevel@tonic-gate 		0,			/* timestamp */
4670Sstevel@tonic-gate 	},
4680Sstevel@tonic-gate };
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate static struct fdcmdinfo {
4710Sstevel@tonic-gate 	char *cmdname;		/* command name */
4720Sstevel@tonic-gate 	uchar_t ncmdbytes;	/* number of bytes of command */
4730Sstevel@tonic-gate 	uchar_t nrsltbytes;	/* number of bytes in result */
4740Sstevel@tonic-gate 	uchar_t cmdtype;		/* characteristics */
4750Sstevel@tonic-gate } fdcmds[] = {
4760Sstevel@tonic-gate 	"", 0, 0, 0, 			/* - */
4770Sstevel@tonic-gate 	"", 0, 0, 0, 			/* - */
4780Sstevel@tonic-gate 	"read_track", 9, 7, 1, 		/* 2 */
4790Sstevel@tonic-gate 	"specify", 3, 0, 3, 		/* 3 */
4800Sstevel@tonic-gate 	"sense_drv_status", 2, 1, 3, 	/* 4 */
4810Sstevel@tonic-gate 	"write", 9, 7, 1, 		/* 5 */
4820Sstevel@tonic-gate 	"read", 9, 7, 1, 		/* 6 */
4830Sstevel@tonic-gate 	"recalibrate", 2, 0, 2, 		/* 7 */
4840Sstevel@tonic-gate 	"sense_int_status", 1, 2, 3, 	/* 8 */
4850Sstevel@tonic-gate 	"write_del", 9, 7, 1, 		/* 9 */
4860Sstevel@tonic-gate 	"read_id", 2, 7, 2, 		/* A */
4870Sstevel@tonic-gate 	"motor_on/off", 1, 0, 4, 	/* B */
4880Sstevel@tonic-gate 	"read_del", 9, 7, 1, 		/* C */
4890Sstevel@tonic-gate 	"format_track", 10, 7, 1, 	/* D */
4900Sstevel@tonic-gate 	"dump_reg", 1, 10, 4, 		/* E */
4910Sstevel@tonic-gate 	"seek", 3, 0, 2, 		/* F */
4920Sstevel@tonic-gate 	"", 0, 0, 0, 			/* - */
4930Sstevel@tonic-gate 	"", 0, 0, 0, 			/* - */
4940Sstevel@tonic-gate 	"", 0, 0, 0, 			/* - */
4950Sstevel@tonic-gate 	"configure", 4, 0, 4, 		/* 13 */
4960Sstevel@tonic-gate 	/* relative seek */
4970Sstevel@tonic-gate };
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate static struct cb_ops fd_cb_ops = {
5000Sstevel@tonic-gate 	fd_open, 		/* open */
5010Sstevel@tonic-gate 	fd_close, 		/* close */
5020Sstevel@tonic-gate 	fd_strategy, 		/* strategy */
5030Sstevel@tonic-gate 	nodev, 			/* print */
5040Sstevel@tonic-gate 	nodev, 			/* dump */
5050Sstevel@tonic-gate 	fd_read, 		/* read */
5060Sstevel@tonic-gate 	fd_write, 		/* write */
5070Sstevel@tonic-gate 	fd_ioctl, 		/* ioctl */
5080Sstevel@tonic-gate 	nodev, 			/* devmap */
5090Sstevel@tonic-gate 	nodev, 			/* mmap */
5100Sstevel@tonic-gate 	nodev, 			/* segmap */
5110Sstevel@tonic-gate 	nochpoll, 		/* poll */
5120Sstevel@tonic-gate 	fd_prop_op, 		/* cb_prop_op */
5130Sstevel@tonic-gate 	0, 			/* streamtab  */
5140Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
5150Sstevel@tonic-gate };
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate static struct dev_ops	fd_ops = {
5180Sstevel@tonic-gate 	DEVO_REV, 		/* devo_rev, */
5190Sstevel@tonic-gate 	0, 			/* refcnt  */
5200Sstevel@tonic-gate 	fd_info, 		/* info */
5210Sstevel@tonic-gate 	nulldev, 		/* identify */
5220Sstevel@tonic-gate 	nulldev, 		/* probe */
5230Sstevel@tonic-gate 	fd_attach, 		/* attach */
5240Sstevel@tonic-gate 	fd_detach, 		/* detach */
5250Sstevel@tonic-gate 	nodev, 			/* reset */
5260Sstevel@tonic-gate 	&fd_cb_ops, 		/* driver operations */
5270Sstevel@tonic-gate 	(struct bus_ops *)0,	/* bus operations */
528*7656SSherry.Moore@Sun.COM 	fd_power,		/* power */
529*7656SSherry.Moore@Sun.COM 	ddi_quiesce_not_supported,	/* devo_quiesce */
5300Sstevel@tonic-gate };
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate /*
5340Sstevel@tonic-gate  * error handling
5350Sstevel@tonic-gate  *
5360Sstevel@tonic-gate  * for debugging, set rwretry and skretry = 1
5370Sstevel@tonic-gate  *		set fderrlevel to 1
5380Sstevel@tonic-gate  *		set fderrmask  to 224  or 100644
5390Sstevel@tonic-gate  *
5400Sstevel@tonic-gate  * after debug set rwretry to 10, skretry to 5, and fderrlevel to 3
5410Sstevel@tonic-gate  * set fderrmask to FDEM_ALL
5420Sstevel@tonic-gate  * remove the define FD_DEBUG
5430Sstevel@tonic-gate  *
5440Sstevel@tonic-gate  */
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate static unsigned int fderrmask = (unsigned int)FDEM_ALL;
5470Sstevel@tonic-gate static int fderrlevel = 3;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate static int tosec = 16;  /* long timeouts for sundiag for now */
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate /*
5520Sstevel@tonic-gate  * loadable module support
5530Sstevel@tonic-gate  */
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate #include <sys/modctl.h>
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate extern struct mod_ops mod_driverops;
5580Sstevel@tonic-gate static struct modldrv modldrv = {
559*7656SSherry.Moore@Sun.COM 	&mod_driverops,		/* Type of module. driver here */
560*7656SSherry.Moore@Sun.COM 	"Floppy Driver", 	/* Name of the module. */
5610Sstevel@tonic-gate 	&fd_ops, 		/* Driver ops vector */
5620Sstevel@tonic-gate };
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate static struct modlinkage modlinkage = {
5650Sstevel@tonic-gate 	MODREV_1,
5660Sstevel@tonic-gate 	&modldrv,
5670Sstevel@tonic-gate 	NULL
5680Sstevel@tonic-gate };
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate int
5710Sstevel@tonic-gate _init(void)
5720Sstevel@tonic-gate {
5730Sstevel@tonic-gate 	return (mod_install(&modlinkage));
5740Sstevel@tonic-gate }
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate int
5770Sstevel@tonic-gate _info(struct modinfo *modinfop)
5780Sstevel@tonic-gate {
5790Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
5800Sstevel@tonic-gate }
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate int
5830Sstevel@tonic-gate _fini(void)
5840Sstevel@tonic-gate {
5850Sstevel@tonic-gate 	int e;
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) != 0)
5880Sstevel@tonic-gate 		return (e);
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	/* ddi_soft_state_fini() */
5910Sstevel@tonic-gate 	return (0);
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate /* ARGSUSED */
5950Sstevel@tonic-gate static int
5960Sstevel@tonic-gate fd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5970Sstevel@tonic-gate {
5980Sstevel@tonic-gate 	struct 			fdctlr *fdc;
5990Sstevel@tonic-gate 	struct 			driver_minor_data *dmdp;
6000Sstevel@tonic-gate 	int			instance = ddi_get_instance(dip);
6010Sstevel@tonic-gate 	int			hard_intr_set = 0;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_attach: start\n"));
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	switch (cmd) {
6060Sstevel@tonic-gate 		case DDI_ATTACH:
6070Sstevel@tonic-gate 			break;
6080Sstevel@tonic-gate 		case DDI_RESUME:
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 			if (!(fdc = fd_getctlr(instance << FDINSTSHIFT))) {
6110Sstevel@tonic-gate 				return (DDI_FAILURE);
6120Sstevel@tonic-gate 			}
6130Sstevel@tonic-gate 			quiesce_fd_interrupt(fdc);
6140Sstevel@tonic-gate 			if (fdc->c_fdtype & FDCTYPE_SB)
615*7656SSherry.Moore@Sun.COM 				if (ddi_add_intr(dip, 0, &fdc->c_block, 0,
616*7656SSherry.Moore@Sun.COM 				    fdintr_dma, (caddr_t)0) != DDI_SUCCESS) {
6170Sstevel@tonic-gate 				return (DDI_FAILURE);
6180Sstevel@tonic-gate 			}
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 			(void) pm_raise_power(dip, 0, PM_LEVEL_ON);
6210Sstevel@tonic-gate 			mutex_enter(&fdc->c_lolock);
6220Sstevel@tonic-gate 			/*
6230Sstevel@tonic-gate 			 * Wake up any thread blocked due to I/O requests
6240Sstevel@tonic-gate 			 * while the device was suspended.
6250Sstevel@tonic-gate 			 */
6260Sstevel@tonic-gate 			cv_broadcast(&fdc->c_suspend_cv);
6270Sstevel@tonic-gate 			mutex_exit(&fdc->c_lolock);
6280Sstevel@tonic-gate 			return (DDI_SUCCESS);
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 		default:
6310Sstevel@tonic-gate 			return (DDI_FAILURE);
6320Sstevel@tonic-gate 	}
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	/*
6360Sstevel@tonic-gate 	 * Check for the pollable property
6370Sstevel@tonic-gate 	 * A pollable floppy drive currently only exists on the
6380Sstevel@tonic-gate 	 * Sparcstation Voyager.  This drive does not need to
6390Sstevel@tonic-gate 	 * be turned on in order to sense whether or not a diskette
6400Sstevel@tonic-gate 	 * is present.
6410Sstevel@tonic-gate 	 */
6420Sstevel@tonic-gate 	if (ddi_getprop(DDI_DEV_T_ANY, dip,
6430Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, FD_POLLABLE_PROP, 0))
6440Sstevel@tonic-gate 		fd_pollable = 1;
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	fdc = kmem_zalloc(sizeof (*fdc), KM_SLEEP);
6470Sstevel@tonic-gate 	fdc->c_dip = dip;
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	fdc->c_next = fdctlrs;
6510Sstevel@tonic-gate 	fdctlrs = fdc;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	/* Determine which type of controller is present and initialize it */
6540Sstevel@tonic-gate 	if (fd_attach_det_ctlr(dip, fdc) == DDI_FAILURE) {
6550Sstevel@tonic-gate 		fd_cleanup(dip, fdc, hard_intr_set, 0);
6560Sstevel@tonic-gate 		return (DDI_FAILURE);
6570Sstevel@tonic-gate 	}
6580Sstevel@tonic-gate 	/* Finish mapping the device registers & setting up structures */
6590Sstevel@tonic-gate 	if (fd_attach_map_regs(dip, fdc) == DDI_FAILURE) {
6600Sstevel@tonic-gate 		fd_cleanup(dip, fdc, hard_intr_set, 0);
6610Sstevel@tonic-gate 		return (DDI_FAILURE);
6620Sstevel@tonic-gate 	}
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	/*
6650Sstevel@tonic-gate 	 * Initialize the DMA limit structures if it's being used.
6660Sstevel@tonic-gate 	 */
6670Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_DMA) {
6680Sstevel@tonic-gate 		fdc->c_fd_dma_lim.dma_attr_version = DMA_ATTR_V0;
6690Sstevel@tonic-gate 		fdc->c_fd_dma_lim.dma_attr_addr_lo = 0x00000000ull;
6700Sstevel@tonic-gate 		fdc->c_fd_dma_lim.dma_attr_addr_hi = 0xfffffffeull;
6710Sstevel@tonic-gate 		fdc->c_fd_dma_lim.dma_attr_count_max = 0xffffff;
6720Sstevel@tonic-gate 		if (fdc->c_fdtype & FDCTYPE_SB) {
6730Sstevel@tonic-gate 			fdc->c_fd_dma_lim.dma_attr_align = FD_SB_DMA_ALIGN;
6740Sstevel@tonic-gate 		} else {
6750Sstevel@tonic-gate 			fdc->c_fd_dma_lim.dma_attr_align = 1;
6760Sstevel@tonic-gate 		}
6770Sstevel@tonic-gate 		fdc->c_fd_dma_lim.dma_attr_burstsizes = 0x0;
6780Sstevel@tonic-gate 		fdc->c_fd_dma_lim.dma_attr_minxfer = 1;
6790Sstevel@tonic-gate 		fdc->c_fd_dma_lim.dma_attr_maxxfer = 0xffff;
6800Sstevel@tonic-gate 		fdc->c_fd_dma_lim.dma_attr_seg = 0xffff;
6810Sstevel@tonic-gate 		fdc->c_fd_dma_lim.dma_attr_sgllen = 1;
6820Sstevel@tonic-gate 		fdc->c_fd_dma_lim.dma_attr_granular = 512;
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 		if (ddi_dma_alloc_handle(dip, &fdc->c_fd_dma_lim,
6850Sstevel@tonic-gate 		    DDI_DMA_DONTWAIT, 0, &fdc->c_dmahandle) != DDI_SUCCESS) {
6860Sstevel@tonic-gate 			fd_cleanup(dip, fdc, hard_intr_set, 0);
6870Sstevel@tonic-gate 			return (DDI_FAILURE);
6880Sstevel@tonic-gate 		}
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 		if (fdc->c_fdtype & FDCTYPE_SB) {
6910Sstevel@tonic-gate 			ddi_device_acc_attr_t dev_attr;
6920Sstevel@tonic-gate 			size_t	rlen;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 			dev_attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
6950Sstevel@tonic-gate 			dev_attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
6960Sstevel@tonic-gate 			dev_attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 			if (ddi_dma_mem_alloc(fdc->c_dmahandle,
6990Sstevel@tonic-gate 			    (size_t)(32*1024), &dev_attr, DDI_DMA_CONSISTENT,
7000Sstevel@tonic-gate 			    DDI_DMA_SLEEP, NULL, (caddr_t *)&fdc->dma_buf,
7010Sstevel@tonic-gate 			    &rlen, &fdc->c_dma_buf_handle) != DDI_SUCCESS) {
702*7656SSherry.Moore@Sun.COM 				fd_cleanup(dip, fdc, hard_intr_set, 0);
7030Sstevel@tonic-gate 				return (DDI_FAILURE);
7040Sstevel@tonic-gate 			}
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 		}
7070Sstevel@tonic-gate 	}
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	/* Register the interrupts */
7110Sstevel@tonic-gate 	if (fd_attach_register_interrupts(dip, fdc,
7120Sstevel@tonic-gate 	    &hard_intr_set) == DDI_FAILURE) {
7130Sstevel@tonic-gate 		fd_cleanup(dip, fdc, hard_intr_set, 0);
7140Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
7150Sstevel@tonic-gate 		    (C, "fd_attach: registering interrupts failed\n"));
7160Sstevel@tonic-gate 		return (DDI_FAILURE);
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	/*
7210Sstevel@tonic-gate 	 * set initial controller/drive/disk "characteristics/geometry"
7220Sstevel@tonic-gate 	 *
7230Sstevel@tonic-gate 	 * NOTE:  The driver only supports one floppy drive.  The hardware
7240Sstevel@tonic-gate 	 * only supports one drive because there is only one auxio register
7250Sstevel@tonic-gate 	 * for one drive.
7260Sstevel@tonic-gate 	 */
7270Sstevel@tonic-gate 	fdc->c_un = kmem_zalloc(sizeof (struct fdunit), KM_SLEEP);
7280Sstevel@tonic-gate 	fdc->c_un->un_chars = kmem_alloc(sizeof (struct fd_char), KM_SLEEP);
7290Sstevel@tonic-gate 	fdc->c_un->un_iostat = kstat_create("fd", 0, "fd0", "disk",
7300Sstevel@tonic-gate 	    KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
7310Sstevel@tonic-gate 	if (fdc->c_un->un_iostat) {
7320Sstevel@tonic-gate 		fdc->c_un->un_iostat->ks_lock = &fdc->c_lolock;
7330Sstevel@tonic-gate 		kstat_install(fdc->c_un->un_iostat);
7340Sstevel@tonic-gate 	}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	fdc->c_un->un_drive = kmem_zalloc(sizeof (struct fd_drive), KM_SLEEP);
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	/* check for the manual eject property */
7390Sstevel@tonic-gate 	if (ddi_getprop(DDI_DEV_T_ANY, dip,
7400Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, FD_MANUAL_EJECT, 0)) {
7410Sstevel@tonic-gate 		fdc->c_un->un_drive->fdd_ejectable = 0;
7420Sstevel@tonic-gate 	} else {
7430Sstevel@tonic-gate 		/* an absence of the property indicates auto eject */
7440Sstevel@tonic-gate 		fdc->c_un->un_drive->fdd_ejectable = -1;
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_attach: ejectable? %d\n",
7480Sstevel@tonic-gate 	    fdc->c_un->un_drive->fdd_ejectable));
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	/*
7510Sstevel@tonic-gate 	 * Check for the drive id.  If the drive id property doesn't exist
7520Sstevel@tonic-gate 	 * then the drive id is set to 0
7530Sstevel@tonic-gate 	 */
7540Sstevel@tonic-gate 	fdc->c_un->un_unit_no = ddi_getprop(DDI_DEV_T_ANY, dip,
7550Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, FD_UNIT, 0);
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_SB) {
7590Sstevel@tonic-gate 		fdc->sb_dma_channel = ddi_getprop(DDI_DEV_T_ANY, dip,
760*7656SSherry.Moore@Sun.COM 		    DDI_PROP_DONTPASS, "dma-channel", 0);
7610Sstevel@tonic-gate 	}
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_attach: unit %d\n",
7650Sstevel@tonic-gate 	    fdc->c_un->un_unit_no));
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	/* Initially set the characteristics to high density */
7680Sstevel@tonic-gate 	fdc->c_un->un_curfdtype = 1;
7690Sstevel@tonic-gate 	*fdc->c_un->un_chars = fdtypes[fdc->c_un->un_curfdtype];
7700Sstevel@tonic-gate 	fdunpacklabel(&fdlbl_high_80, &fdc->c_un->un_label);
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	/* Make sure drive is present */
7730Sstevel@tonic-gate 	if (fd_attach_check_drive(fdc) == DDI_FAILURE) {
7740Sstevel@tonic-gate 		fd_cleanup(dip, fdc, hard_intr_set, 1);
7750Sstevel@tonic-gate 		return (DDI_FAILURE);
7760Sstevel@tonic-gate 	}
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	for (dmdp = fd_minor; dmdp->name != NULL; dmdp++) {
7790Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, dmdp->name, dmdp->type,
780*7656SSherry.Moore@Sun.COM 		    (instance << FDINSTSHIFT) | dmdp->minor,
781*7656SSherry.Moore@Sun.COM 		    DDI_NT_FD, 0) == DDI_FAILURE) {
7820Sstevel@tonic-gate 			fd_cleanup(dip, fdc, hard_intr_set, 1);
7830Sstevel@tonic-gate 			return (DDI_FAILURE);
7840Sstevel@tonic-gate 		}
7850Sstevel@tonic-gate 	}
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	create_pm_components(dip);
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	/*
7900Sstevel@tonic-gate 	 * Add a zero-length attribute to tell the world we support
7910Sstevel@tonic-gate 	 * kernel ioctls (for layered drivers)
7920Sstevel@tonic-gate 	 */
7930Sstevel@tonic-gate 	(void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
7940Sstevel@tonic-gate 	    DDI_KERNEL_IOCTL, NULL, 0);
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	ddi_report_dev(dip);
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_ATTA,
7990Sstevel@tonic-gate 	    (C, "attached 0x%x\n", ddi_get_instance(dip)));
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	return (DDI_SUCCESS);
8020Sstevel@tonic-gate }
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate /*
8050Sstevel@tonic-gate  * Finish mapping the registers and initializing structures
8060Sstevel@tonic-gate  */
8070Sstevel@tonic-gate static int
8080Sstevel@tonic-gate fd_attach_map_regs(dev_info_t *dip, struct fdctlr *fdc)
8090Sstevel@tonic-gate {
8100Sstevel@tonic-gate 	ddi_device_acc_attr_t attr;
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
8130Sstevel@tonic-gate 	attr.devacc_attr_endian_flags  = DDI_STRUCTURE_LE_ACC;
8140Sstevel@tonic-gate 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	/* Map the DMA registers of the platform supports DMA */
8170Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_SB) {
8180Sstevel@tonic-gate 		if (ddi_regs_map_setup(dip, 1, (caddr_t *)&fdc->c_dma_regs,
819*7656SSherry.Moore@Sun.COM 		    0, sizeof (struct sb_dma_reg), &attr,
820*7656SSherry.Moore@Sun.COM 		    &fdc->c_handlep_dma)) {
8210Sstevel@tonic-gate 			return (DDI_FAILURE);
8220Sstevel@tonic-gate 		}
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	} else if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
8260Sstevel@tonic-gate 		if (ddi_regs_map_setup(dip, 1, (caddr_t *)&fdc->c_dma_regs,
827*7656SSherry.Moore@Sun.COM 		    0, sizeof (struct cheerio_dma_reg), &attr,
828*7656SSherry.Moore@Sun.COM 		    &fdc->c_handlep_dma)) {
8290Sstevel@tonic-gate 			return (DDI_FAILURE);
8300Sstevel@tonic-gate 		}
8310Sstevel@tonic-gate 	}
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	/* Reset the DMA engine and enable floppy interrupts */
8340Sstevel@tonic-gate 	reset_dma_controller(fdc);
8350Sstevel@tonic-gate 	set_dma_control_register(fdc, DCSR_INIT_BITS);
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	/* Finish initializing structures associated with the device regs */
8380Sstevel@tonic-gate 	switch (fdc->c_fdtype & FDCTYPE_CTRLMASK) {
8390Sstevel@tonic-gate 	case FDCTYPE_82077:
8400Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "type is 82077\n"));
8410Sstevel@tonic-gate 		/*
8420Sstevel@tonic-gate 		 * Initialize addrs of key registers
8430Sstevel@tonic-gate 		 */
8440Sstevel@tonic-gate 		fdc->c_control =
8450Sstevel@tonic-gate 		    (uchar_t *)&fdc->c_reg->fdc_82077_reg.fdc_control;
8460Sstevel@tonic-gate 		fdc->c_fifo = (uchar_t *)&fdc->c_reg->fdc_82077_reg.fdc_fifo;
8470Sstevel@tonic-gate 		fdc->c_dor = (uchar_t *)&fdc->c_reg->fdc_82077_reg.fdc_dor;
8480Sstevel@tonic-gate 		fdc->c_dir = (uchar_t *)&fdc->c_reg->fdc_82077_reg.fdc_dir;
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_ATTA, ((int)C,
852*7656SSherry.Moore@Sun.COM 		    (char *)"fdattach: msr/dsr at %p\n",
853*7656SSherry.Moore@Sun.COM 		    (void *)fdc->c_control));
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 		/*
8560Sstevel@tonic-gate 		 * The 82077 doesn't use the first configuration parameter
8570Sstevel@tonic-gate 		 * so let's adjust that while we know we're an 82077.
8580Sstevel@tonic-gate 		 */
8590Sstevel@tonic-gate 		fdconf[0] = 0;
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 		quiesce_fd_interrupt(fdc);
8620Sstevel@tonic-gate 		break;
8630Sstevel@tonic-gate 	default:
8640Sstevel@tonic-gate 		break;
8650Sstevel@tonic-gate 	}
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	return (0);
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate /*
8710Sstevel@tonic-gate  * Determine which type of floppy controller is present and
8720Sstevel@tonic-gate  * initialize the registers accordingly
8730Sstevel@tonic-gate  */
8740Sstevel@tonic-gate static int
8750Sstevel@tonic-gate fd_attach_det_ctlr(dev_info_t *dip, struct fdctlr *fdc)
8760Sstevel@tonic-gate {
8770Sstevel@tonic-gate 	ddi_device_acc_attr_t attr;
8780Sstevel@tonic-gate 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
8790Sstevel@tonic-gate 	/* DDI_NEVERSWAP_ACC since the controller has a byte interface. */
8800Sstevel@tonic-gate 	attr.devacc_attr_endian_flags  = DDI_STRUCTURE_LE_ACC;
8810Sstevel@tonic-gate 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_ATTA,
884*7656SSherry.Moore@Sun.COM 	    (C, "fdattach_det_cltr: start \n"));
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	/*
8870Sstevel@tonic-gate 	 * First, map in the controller's registers
8880Sstevel@tonic-gate 	 * The controller has an 8-bit interface, so byte
8890Sstevel@tonic-gate 	 * swapping isn't needed
8900Sstevel@tonic-gate 	 */
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	if (ddi_regs_map_setup(dip, 0, (caddr_t *)&fdc->c_reg,
893*7656SSherry.Moore@Sun.COM 	    0, sizeof (union fdcreg),
894*7656SSherry.Moore@Sun.COM 	    &attr,
895*7656SSherry.Moore@Sun.COM 	    &fdc->c_handlep_cont)) {
896*7656SSherry.Moore@Sun.COM 		return (DDI_FAILURE);
8970Sstevel@tonic-gate 	}
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_ATTA,
900*7656SSherry.Moore@Sun.COM 	    (C, "fdattach_det_cltr: mapped floppy regs\n"));
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	/*
9040Sstevel@tonic-gate 	 * Set platform specific characteristics based on the device-tree
9050Sstevel@tonic-gate 	 * node name.
9060Sstevel@tonic-gate 	 */
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	if (strcmp(ddi_get_name(dip), "SUNW,fdtwo") == 0) {
9100Sstevel@tonic-gate 		fdc->c_fdtype |= FDCTYPE_SLAVIO;
9110Sstevel@tonic-gate 		fdc->c_fdtype |= FDCTYPE_82077;
9120Sstevel@tonic-gate 		fdc->c_auxiova = fd_getauxiova(dip);
9130Sstevel@tonic-gate 		fdc->c_auxiodata = (uchar_t)(AUX_MBO4M|AUX_TC4M);
9140Sstevel@tonic-gate 		fdc->c_auxiodata2 = (uchar_t)AUX_TC4M;
9150Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
916*7656SSherry.Moore@Sun.COM 		    (C, "fdattach: slavio will be used!\n"));
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate /*
9200Sstevel@tonic-gate  * Check the binding name to identify whether it is a South bridge based
9210Sstevel@tonic-gate  * system or not.
9220Sstevel@tonic-gate  */
9230Sstevel@tonic-gate 	} else if (strcmp(ddi_get_name(dip), "pnpALI,1533,0") == 0) {
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 		fdc->c_fdtype |= FDCTYPE_SB;
9260Sstevel@tonic-gate 		fdc->c_fdtype |= FDCTYPE_82077;
9270Sstevel@tonic-gate 		fdc->c_fdtype |= FDCTYPE_DMA;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
930*7656SSherry.Moore@Sun.COM 		    (C, "fdattach: southbridge will be used!\n"));
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		/*
9330Sstevel@tonic-gate 		 * The driver assumes high density characteristics until
9340Sstevel@tonic-gate 		 * the diskette is looked at.
9350Sstevel@tonic-gate 		 */
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 		fdc->c_fdtype |= FDCTYPE_DMA8237;
9380Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_attach: DMA used\n"));
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	} else if (strcmp(ddi_get_name(dip), "fdthree") == 0) {
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 		fdc->c_fdtype |= FDCTYPE_CHEERIO;
9440Sstevel@tonic-gate 		fdc->c_fdtype |= FDCTYPE_82077;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
947*7656SSherry.Moore@Sun.COM 		    (C, "fdattach: cheerio will be used!\n"));
9480Sstevel@tonic-gate 		/*
9490Sstevel@tonic-gate 		 * The cheerio auxio register should be memory mapped.  The
9500Sstevel@tonic-gate 		 * auxio register on other platforms is shared and mapped
9510Sstevel@tonic-gate 		 * elsewhere in the kernel
9520Sstevel@tonic-gate 		 */
9530Sstevel@tonic-gate 		if (ddi_regs_map_setup(dip, 2, (caddr_t *)&fdc->c_auxio_reg,
9540Sstevel@tonic-gate 		    0, sizeof (uint_t), &attr, &fdc->c_handlep_aux)) {
9550Sstevel@tonic-gate 			return (DDI_FAILURE);
9560Sstevel@tonic-gate 		}
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 		/*
9590Sstevel@tonic-gate 		 * The driver assumes high density characteristics until
9600Sstevel@tonic-gate 		 * the diskette is looked at.
9610Sstevel@tonic-gate 		 */
9620Sstevel@tonic-gate 		Set_auxio(fdc, AUX_HIGH_DENSITY);
9630Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
964*7656SSherry.Moore@Sun.COM 		    (C, "fdattach: auxio register 0x%x\n",
965*7656SSherry.Moore@Sun.COM 		    *fdc->c_auxio_reg));
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 		fdc->c_fdtype |= FDCTYPE_DMA;
9680Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_attach: DMA used\n"));
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	}
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	if (fdc->c_fdtype == 0) {
9730Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
974*7656SSherry.Moore@Sun.COM 		    (C, "fdattach: no controller!\n"));
9750Sstevel@tonic-gate 		return (DDI_FAILURE);
9760Sstevel@tonic-gate 	} else {
9770Sstevel@tonic-gate 		return (0);
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate }
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate /*
9830Sstevel@tonic-gate  * Register the floppy interrupts
9840Sstevel@tonic-gate  */
9850Sstevel@tonic-gate static int
9860Sstevel@tonic-gate fd_attach_register_interrupts(dev_info_t *dip, struct fdctlr *fdc, int *hard)
9870Sstevel@tonic-gate {
9880Sstevel@tonic-gate 	ddi_iblock_cookie_t  iblock_cookie_soft;
9890Sstevel@tonic-gate 	int status;
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	/*
9920Sstevel@tonic-gate 	 * First call ddi_get_iblock_cookie() to retrieve the
9930Sstevel@tonic-gate 	 * the interrupt block cookie so that the mutexes may
9940Sstevel@tonic-gate 	 * be initialized before adding the interrupt.  If the
9950Sstevel@tonic-gate 	 * mutexes are initialized after adding the interrupt, there
9960Sstevel@tonic-gate 	 * could be a race condition.
9970Sstevel@tonic-gate 	 */
9980Sstevel@tonic-gate 	if (ddi_get_iblock_cookie(dip, 0, &fdc->c_block) != DDI_SUCCESS) {
9990Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
1000*7656SSherry.Moore@Sun.COM 		    (C, "fdattach: ddi_get_iblock_cookie failed\n"));
10010Sstevel@tonic-gate 		return (DDI_FAILURE);
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	}
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	/* Initialize high level mutex */
10060Sstevel@tonic-gate 	mutex_init(&fdc->c_hilock, NULL, MUTEX_DRIVER, fdc->c_block);
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	/*
10090Sstevel@tonic-gate 	 * Try to register fast trap handler, if unable try standard
10100Sstevel@tonic-gate 	 * interrupt handler, else bad
10110Sstevel@tonic-gate 	 */
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_DMA) {
10140Sstevel@tonic-gate 		if (ddi_add_intr(dip, 0, &fdc->c_block, 0,
1015*7656SSherry.Moore@Sun.COM 		    fdintr_dma, (caddr_t)0) == DDI_SUCCESS) {
1016*7656SSherry.Moore@Sun.COM 			FDERRPRINT(FDEP_L1, FDEM_ATTA,
1017*7656SSherry.Moore@Sun.COM 			    (C, "fdattach: standard intr\n"));
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 				/*
10200Sstevel@tonic-gate 				 * When DMA is used, the low level lock
10210Sstevel@tonic-gate 				 * is used in the hard interrupt handler.
10220Sstevel@tonic-gate 				 */
10230Sstevel@tonic-gate 				mutex_init(&fdc->c_lolock, NULL,
1024*7656SSherry.Moore@Sun.COM 				    MUTEX_DRIVER, fdc->c_block);
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 				*hard = 1;
10270Sstevel@tonic-gate 		} else {
10280Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_ATTA,
1029*7656SSherry.Moore@Sun.COM 			    (C, "fdattach: can't add dma intr\n"));
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 			mutex_destroy(&fdc->c_hilock);
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 			return (DDI_FAILURE);
10340Sstevel@tonic-gate 		}
10350Sstevel@tonic-gate 	} else {
10360Sstevel@tonic-gate 		/*
10370Sstevel@tonic-gate 		 * Platforms that don't support DMA have both hard
10380Sstevel@tonic-gate 		 * and soft interrupts.
10390Sstevel@tonic-gate 		 */
10400Sstevel@tonic-gate 		if (ddi_add_intr(dip, 0, &fdc->c_block, 0,
1041*7656SSherry.Moore@Sun.COM 		    fd_intr, (caddr_t)0) == DDI_SUCCESS) {
1042*7656SSherry.Moore@Sun.COM 			FDERRPRINT(FDEP_L1, FDEM_ATTA,
1043*7656SSherry.Moore@Sun.COM 			    (C, "fdattach: standard intr\n"));
1044*7656SSherry.Moore@Sun.COM 			*hard = 1;
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 			/* fast traps are not enabled */
10470Sstevel@tonic-gate 			fdc->c_fasttrap = 0;
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 		} else {
10500Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_ATTA,
1051*7656SSherry.Moore@Sun.COM 			    (C, "fdattach: can't add intr\n"));
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 			mutex_destroy(&fdc->c_hilock);
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 			return (DDI_FAILURE);
10560Sstevel@tonic-gate 		}
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 		/*
10600Sstevel@tonic-gate 		 * Initialize the soft interrupt handler.  First call
10610Sstevel@tonic-gate 		 * ddi_get_soft_iblock_cookie() so that the mutex may
10620Sstevel@tonic-gate 		 * be initialized before the handler is added.
10630Sstevel@tonic-gate 		 */
10640Sstevel@tonic-gate 		status = ddi_get_soft_iblock_cookie(dip, DDI_SOFTINT_LOW,
1065*7656SSherry.Moore@Sun.COM 		    &iblock_cookie_soft);
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 		if (status != DDI_SUCCESS) {
10690Sstevel@tonic-gate 			mutex_destroy(&fdc->c_hilock);
10700Sstevel@tonic-gate 			return (DDI_FAILURE);
10710Sstevel@tonic-gate 		}
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 		/*
10740Sstevel@tonic-gate 		 * Initialize low level mutex which is used in the soft
10750Sstevel@tonic-gate 		 * interrupt handler
10760Sstevel@tonic-gate 		 */
10770Sstevel@tonic-gate 		mutex_init(&fdc->c_lolock, NULL, MUTEX_DRIVER,
1078*7656SSherry.Moore@Sun.COM 		    iblock_cookie_soft);
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 		if (ddi_add_softintr(dip, DDI_SOFTINT_LOW, &fdc->c_softid,
1081*7656SSherry.Moore@Sun.COM 		    NULL, NULL,
1082*7656SSherry.Moore@Sun.COM 		    fd_lointr,
1083*7656SSherry.Moore@Sun.COM 		    (caddr_t)fdc) != DDI_SUCCESS) {
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 			mutex_destroy(&fdc->c_hilock);
10860Sstevel@tonic-gate 			mutex_destroy(&fdc->c_lolock);
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 			return (DDI_FAILURE);
10890Sstevel@tonic-gate 		}
10900Sstevel@tonic-gate 	}
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 	fdc->c_intrstat = kstat_create("fd", 0, "fdc0", "controller",
1093*7656SSherry.Moore@Sun.COM 	    KSTAT_TYPE_INTR, 1, KSTAT_FLAG_PERSISTENT);
10940Sstevel@tonic-gate 	if (fdc->c_intrstat) {
10950Sstevel@tonic-gate 		fdc->c_hiintct = &KIOIP->intrs[KSTAT_INTR_HARD];
10960Sstevel@tonic-gate 		kstat_install(fdc->c_intrstat);
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	/* condition variable to wait on while an io transaction occurs */
11000Sstevel@tonic-gate 	cv_init(&fdc->c_iocv, NULL, CV_DRIVER, NULL);
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	/* condition variable for the csb */
11030Sstevel@tonic-gate 	cv_init(&fdc->c_csbcv, NULL, CV_DRIVER, NULL);
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 	/* condition variable for motor on waiting period */
11060Sstevel@tonic-gate 	cv_init(&fdc->c_motoncv, NULL, CV_DRIVER, NULL);
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	/* semaphore to serialize opens and closes */
11090Sstevel@tonic-gate 	sema_init(&fdc->c_ocsem, 1, NULL, SEMA_DRIVER, NULL);
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	/* condition variable to wait on suspended floppy controller. */
11120Sstevel@tonic-gate 	cv_init(&fdc->c_suspend_cv, NULL, CV_DRIVER, NULL);
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	return (0);
11150Sstevel@tonic-gate }
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate /*
11180Sstevel@tonic-gate  * Make sure the drive is present
11190Sstevel@tonic-gate  * 	- acquires the low level lock
11200Sstevel@tonic-gate  */
11210Sstevel@tonic-gate static int
11220Sstevel@tonic-gate fd_attach_check_drive(struct fdctlr *fdc)
11230Sstevel@tonic-gate {
11240Sstevel@tonic-gate 	int tmp_fderrlevel;
11250Sstevel@tonic-gate 	int unit = fdc->c_un->un_unit_no;
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_ATTA,
1128*7656SSherry.Moore@Sun.COM 	    (C, "fd_attach_check_drive\n"));
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	mutex_enter(&fdc->c_lolock);
11320Sstevel@tonic-gate 	switch (fdc->c_fdtype & FDCTYPE_CTRLMASK) {
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	/* insure that the eject line is reset */
11350Sstevel@tonic-gate 	case FDCTYPE_82077:
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 		/*
11380Sstevel@tonic-gate 		 * Everything but the motor enable, drive select,
11390Sstevel@tonic-gate 		 * and reset bits are turned off.  These three
11400Sstevel@tonic-gate 		 * bits remain as they are.
11410Sstevel@tonic-gate 		 */
11420Sstevel@tonic-gate 		/* LINTED */
11430Sstevel@tonic-gate 		Set_dor(fdc, ~((MOTEN(unit))|DRVSEL|RESET), 0);
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
1146*7656SSherry.Moore@Sun.COM 		    (C, "fdattach: Dor 0x%x\n", Dor(fdc)));
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 		drv_usecwait(5);
11490Sstevel@tonic-gate 		if (unit == 0) {
11500Sstevel@tonic-gate 			/* LINTED */
11510Sstevel@tonic-gate 			Set_dor(fdc, RESET|DRVSEL, 1);
11520Sstevel@tonic-gate 		} else {
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 			/* LINTED */
11550Sstevel@tonic-gate 			Set_dor(fdc, DRVSEL, 0);
11560Sstevel@tonic-gate 			/* LINTED */
11570Sstevel@tonic-gate 			Set_dor(fdc, RESET, 1);
11580Sstevel@tonic-gate 		}
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 		drv_usecwait(5);
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
1163*7656SSherry.Moore@Sun.COM 		    (C, "fdattach: Dor 0x%x\n", Dor(fdc)));
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 		if (!((fdc->c_fdtype & FDCTYPE_CHEERIO) ||
1166*7656SSherry.Moore@Sun.COM 		    (fdc->c_fdtype & FDCTYPE_SB))) {
11670Sstevel@tonic-gate 			set_auxioreg(AUX_TC4M, 0);
11680Sstevel@tonic-gate 		}
11690Sstevel@tonic-gate 		break;
11700Sstevel@tonic-gate 	default:
11710Sstevel@tonic-gate 		break;
11720Sstevel@tonic-gate 	}
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	fdgetcsb(fdc);
11760Sstevel@tonic-gate 	if (fdreset(fdc) != 0) {
11770Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
11780Sstevel@tonic-gate 		return (DDI_FAILURE);
11790Sstevel@tonic-gate 	}
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	/* check for drive present */
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	tmp_fderrlevel = fderrlevel;
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	fderrlevel = FDEP_LMAX;
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_ATTA,
1190*7656SSherry.Moore@Sun.COM 	    (C, "fdattach: call fdrecalseek\n"));
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	/* Make sure the drive is present */
11930Sstevel@tonic-gate 	if (fdrecalseek(fdc, unit, -1, 0) != 0) {
11940Sstevel@tonic-gate 		timeout_id_t timeid = fdc->c_mtimeid;
11950Sstevel@tonic-gate 		fderrlevel = tmp_fderrlevel;
11960Sstevel@tonic-gate 		fdc->c_mtimeid = 0;
11970Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 		/* Do not hold the mutex over the call to untimeout */
12010Sstevel@tonic-gate 		if (timeid) {
12020Sstevel@tonic-gate 			(void) untimeout(timeid);
12030Sstevel@tonic-gate 		}
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 		FDERRPRINT(FDEP_L2, FDEM_ATTA,
12060Sstevel@tonic-gate 		    (C, "fd_attach: no drive?\n"));
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 		return (DDI_FAILURE);
12090Sstevel@tonic-gate 	}
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	fderrlevel = tmp_fderrlevel;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	fdselect(fdc, unit, 0);    /* deselect drive zero (used in fdreset) */
12140Sstevel@tonic-gate 	fdretcsb(fdc);
12150Sstevel@tonic-gate 	mutex_exit(&fdc->c_lolock);
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 	return (0);
12180Sstevel@tonic-gate }
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate /*
12210Sstevel@tonic-gate  * Clean up routine used by fd_detach and fd_attach
12220Sstevel@tonic-gate  *
12230Sstevel@tonic-gate  * Note: if the soft id is non-zero, then ddi_add_softintr() completed
12240Sstevel@tonic-gate  * successfully.  I can not make the same assumption about the iblock_cookie
12250Sstevel@tonic-gate  * for the high level interrupt handler.  So, the hard parameter indicates
12260Sstevel@tonic-gate  * whether or not a high level interrupt handler has been added.
12270Sstevel@tonic-gate  *
12280Sstevel@tonic-gate  * If the locks parameter is nonzero, then all mutexes, semaphores and
12290Sstevel@tonic-gate  * condition variables will be destroyed.
12300Sstevel@tonic-gate  *
12310Sstevel@tonic-gate  * Does not assume the low level mutex is held.
12320Sstevel@tonic-gate  *
12330Sstevel@tonic-gate  */
12340Sstevel@tonic-gate static void
12350Sstevel@tonic-gate fd_cleanup(dev_info_t *dip, struct fdctlr *fdc, int hard, int locks)
12360Sstevel@tonic-gate {
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_ATTA,
1240*7656SSherry.Moore@Sun.COM 	    (C, "fd_cleanup instance: %d ctlr: 0x%p\n",
1241*7656SSherry.Moore@Sun.COM 	    ddi_get_instance(dip), (void *)fdc));
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	if (fdc == NULL) {
12450Sstevel@tonic-gate 		return;
12460Sstevel@tonic-gate 	}
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	/*
12490Sstevel@tonic-gate 	 * Remove interrupt handlers first before anything else
12500Sstevel@tonic-gate 	 * is deallocated.
12510Sstevel@tonic-gate 	 */
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	/* Remove hard interrupt if one is registered */
12540Sstevel@tonic-gate 	if (hard) {
12550Sstevel@tonic-gate 		ddi_remove_intr(dip, (uint_t)0, fdc->c_block);
12560Sstevel@tonic-gate 	}
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	/* Remove soft interrupt if one is registered */
12590Sstevel@tonic-gate 	if (fdc->c_softid != NULL)
12600Sstevel@tonic-gate 		ddi_remove_softintr(fdc->c_softid);
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	/* Remove timers */
12640Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_82077) {
12650Sstevel@tonic-gate 		if (fdc->c_mtimeid)
12660Sstevel@tonic-gate 			(void) untimeout(fdc->c_mtimeid);
12670Sstevel@tonic-gate 		/*
12680Sstevel@tonic-gate 		 * Need to turn off motor (includes select/LED for South Bridge
12690Sstevel@tonic-gate 		 * chipset) just in case it was on when timer was removed
12700Sstevel@tonic-gate 		 */
12710Sstevel@tonic-gate 		fdmotoff(fdc);
12720Sstevel@tonic-gate 	}
12730Sstevel@tonic-gate 	if (fdc->c_timeid)
12740Sstevel@tonic-gate 		(void) untimeout(fdc->c_timeid);
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	/* Remove memory handles */
12780Sstevel@tonic-gate 	if (fdc->c_handlep_cont)
12790Sstevel@tonic-gate 		ddi_regs_map_free(&fdc->c_handlep_cont);
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 	if (fdc->c_handlep_aux)
12820Sstevel@tonic-gate 		ddi_regs_map_free(&fdc->c_handlep_aux);
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	if (fdc->c_handlep_dma)
12850Sstevel@tonic-gate 		ddi_regs_map_free(&fdc->c_handlep_dma);
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 	if (fdc->c_dma_buf_handle != NULL)
12880Sstevel@tonic-gate 		ddi_dma_mem_free(&fdc->c_dma_buf_handle);
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 	if (fdc->c_dmahandle != NULL)
12910Sstevel@tonic-gate 		ddi_dma_free_handle(&fdc->c_dmahandle);
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 	/* Remove all minor nodes */
12950Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	/* Remove unit structure if one exists */
13000Sstevel@tonic-gate 	if (fdc->c_un != (struct fdunit *)NULL) {
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 		ASSERT(!mutex_owned(&fdc->c_lolock));
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 		if (fdc->c_un->un_iostat)
13050Sstevel@tonic-gate 			kstat_delete(fdc->c_un->un_iostat);
13060Sstevel@tonic-gate 		fdc->c_un->un_iostat = NULL;
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 		if (fdc->c_un->un_chars)
13090Sstevel@tonic-gate 			kmem_free(fdc->c_un->un_chars, sizeof (struct fd_char));
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 		if (fdc->c_un->un_drive)
13120Sstevel@tonic-gate 			kmem_free(fdc->c_un->un_drive,
13130Sstevel@tonic-gate 			    sizeof (struct fd_drive));
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 		kmem_free((caddr_t)fdc->c_un, sizeof (struct fdunit));
13160Sstevel@tonic-gate 	}
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 	if (fdc->c_intrstat) {
13190Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
1320*7656SSherry.Moore@Sun.COM 		    (C, "fd_cleanup: delete intrstat\n"));
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 		kstat_delete(fdc->c_intrstat);
13230Sstevel@tonic-gate 	}
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	fdc->c_intrstat = NULL;
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	if (locks) {
13280Sstevel@tonic-gate 		cv_destroy(&fdc->c_iocv);
13290Sstevel@tonic-gate 		cv_destroy(&fdc->c_csbcv);
13300Sstevel@tonic-gate 		cv_destroy(&fdc->c_motoncv);
13310Sstevel@tonic-gate 		cv_destroy(&fdc->c_suspend_cv);
13320Sstevel@tonic-gate 		sema_destroy(&fdc->c_ocsem);
13330Sstevel@tonic-gate 		mutex_destroy(&fdc->c_hilock);
13340Sstevel@tonic-gate 		mutex_destroy(&fdc->c_lolock);
13350Sstevel@tonic-gate 	}
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 	fdctlrs = fdc->c_next;
13390Sstevel@tonic-gate 	kmem_free(fdc, sizeof (*fdc));
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate }
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate static int
13460Sstevel@tonic-gate fd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
13470Sstevel@tonic-gate {
13480Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
13490Sstevel@tonic-gate 	struct fdctlr *fdc = fd_getctlr(instance << FDINSTSHIFT);
13500Sstevel@tonic-gate 	timeout_id_t c_mtimeid;
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_detach\n"));
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	switch (cmd) {
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	case DDI_DETACH:
13570Sstevel@tonic-gate 		/*
13580Sstevel@tonic-gate 		 * The hard parameter is set to 1.  If detach is called, then
13590Sstevel@tonic-gate 		 * attach must have passed meaning that the high level
13600Sstevel@tonic-gate 		 * interrupt handler was successfully added.
13610Sstevel@tonic-gate 		 * Similarly, the locks parameter is also set to 1.
13620Sstevel@tonic-gate 		 */
13630Sstevel@tonic-gate 		fd_cleanup(dip, fdc, 1, 1);
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 		ddi_prop_remove_all(dip);
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 		return (DDI_SUCCESS);
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 	case DDI_SUSPEND:
13700Sstevel@tonic-gate 		if (!fdc)
13710Sstevel@tonic-gate 			return (DDI_FAILURE);
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 		mutex_enter(&fdc->c_lolock);
13750Sstevel@tonic-gate 		fdgetcsb(fdc);	/* Wait for I/O to finish */
13760Sstevel@tonic-gate 		c_mtimeid = fdc->c_mtimeid;
13770Sstevel@tonic-gate 		fdretcsb(fdc);
13780Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 		(void) untimeout(c_mtimeid);
13810Sstevel@tonic-gate 		/*
13820Sstevel@tonic-gate 		 * After suspend, the system could be powered off.
13830Sstevel@tonic-gate 		 * When it is later powered on the southbridge floppy
13840Sstevel@tonic-gate 		 * controller will tristate the interrupt line causing
13850Sstevel@tonic-gate 		 * continuous dma interrupts.
13860Sstevel@tonic-gate 		 * To avoid getting continuous fd interrupts we will remove the
13870Sstevel@tonic-gate 		 * dma interrupt handler installed. We will re-install the
13880Sstevel@tonic-gate 		 * handler when we RESUME.
13890Sstevel@tonic-gate 		 */
13900Sstevel@tonic-gate 		if (fdc->c_fdtype & FDCTYPE_SB)
13910Sstevel@tonic-gate 			ddi_remove_intr(dip, 0, fdc->c_block);
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 		fdc->c_un->un_state = FD_STATE_SUSPENDED;
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 		return (DDI_SUCCESS);
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	default:
13980Sstevel@tonic-gate 		return (DDI_FAILURE);
13990Sstevel@tonic-gate 	}
14000Sstevel@tonic-gate }
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate /* ARGSUSED */
14030Sstevel@tonic-gate static int
14040Sstevel@tonic-gate fd_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
14050Sstevel@tonic-gate {
14060Sstevel@tonic-gate 	register struct fdctlr *fdc;
14070Sstevel@tonic-gate 	register int error;
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 	switch (infocmd) {
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
14120Sstevel@tonic-gate 		if ((fdc = fd_getctlr((dev_t)arg)) == NULL) {
14130Sstevel@tonic-gate 			error = DDI_FAILURE;
14140Sstevel@tonic-gate 		} else {
14150Sstevel@tonic-gate 			*result = fdc->c_dip;
14160Sstevel@tonic-gate 			error = DDI_SUCCESS;
14170Sstevel@tonic-gate 		}
14180Sstevel@tonic-gate 		break;
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
14210Sstevel@tonic-gate 		*result = 0;
14220Sstevel@tonic-gate 		error = DDI_SUCCESS;
14230Sstevel@tonic-gate 		break;
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 	default:
14260Sstevel@tonic-gate 		error = DDI_FAILURE;
14270Sstevel@tonic-gate 	}
14280Sstevel@tonic-gate 	return (error);
14290Sstevel@tonic-gate }
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate /*
14320Sstevel@tonic-gate  * property operation routine.  return the number of blocks for the partition
14330Sstevel@tonic-gate  * in question or forward the request to the property facilities.
14340Sstevel@tonic-gate  */
14350Sstevel@tonic-gate static int
14360Sstevel@tonic-gate fd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
14370Sstevel@tonic-gate     char *name, caddr_t valuep, int *lengthp)
14380Sstevel@tonic-gate {
14390Sstevel@tonic-gate 	struct fdunit	*un;
14400Sstevel@tonic-gate 	struct fdctlr	*fdc;
14410Sstevel@tonic-gate 	uint64_t	nblocks64;
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 	/*
14440Sstevel@tonic-gate 	 * Our dynamic properties are all device specific and size oriented.
14450Sstevel@tonic-gate 	 * Requests issued under conditions where size is valid are passed
14460Sstevel@tonic-gate 	 * to ddi_prop_op_nblocks with the size information, otherwise the
14470Sstevel@tonic-gate 	 * request is passed to ddi_prop_op.
14480Sstevel@tonic-gate 	 */
14490Sstevel@tonic-gate 	if (dev == DDI_DEV_T_ANY) {
14500Sstevel@tonic-gate pass:  		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
14510Sstevel@tonic-gate 		    name, valuep, lengthp));
14520Sstevel@tonic-gate 	} else {
14530Sstevel@tonic-gate 		fdc = fd_getctlr(dev);
14540Sstevel@tonic-gate 		if (fdc == NULL)
14550Sstevel@tonic-gate 			goto pass;
14560Sstevel@tonic-gate 
14570Sstevel@tonic-gate 		/* we have size if diskette opened and label read */
14580Sstevel@tonic-gate 		un = fdc->c_un;
14590Sstevel@tonic-gate 		if ((un == NULL) || !fd_unit_is_open(fdc->c_un))
14600Sstevel@tonic-gate 			goto pass;
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 		/* get nblocks value */
14630Sstevel@tonic-gate 		nblocks64 = (ulong_t)
14640Sstevel@tonic-gate 		    un->un_label.dkl_map[FDPARTITION(dev)].dkl_nblk;
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate 		return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags,
14670Sstevel@tonic-gate 		    name, valuep, lengthp, nblocks64));
14680Sstevel@tonic-gate 	}
14690Sstevel@tonic-gate }
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate /* ARGSUSED3 */
14720Sstevel@tonic-gate static int
14730Sstevel@tonic-gate fd_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
14740Sstevel@tonic-gate {
14750Sstevel@tonic-gate 	dev_t dev;
14760Sstevel@tonic-gate 	int  part;
14770Sstevel@tonic-gate 	struct fdctlr *fdc;
14780Sstevel@tonic-gate 	struct fdunit *un;
14790Sstevel@tonic-gate 	struct dk_map32 *dkm;
14800Sstevel@tonic-gate 	uchar_t	pbit;
14810Sstevel@tonic-gate 	int	err, part_is_open;
14820Sstevel@tonic-gate 	int 	unit;
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 	dev = *devp;
14850Sstevel@tonic-gate 	fdc = fd_getctlr(dev);
14860Sstevel@tonic-gate 	if ((fdc == NULL) || ((un = fdc->c_un) == NULL)) {
14870Sstevel@tonic-gate 		return (ENXIO);
14880Sstevel@tonic-gate 	}
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 	unit = fdc->c_un->un_unit_no;
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 	/*
14930Sstevel@tonic-gate 	 * Serialize opens/closes
14940Sstevel@tonic-gate 	 */
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	sema_p(&fdc->c_ocsem);
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	/* check partition */
14990Sstevel@tonic-gate 	part = FDPARTITION(dev);
15000Sstevel@tonic-gate 	pbit = 1 << part;
15010Sstevel@tonic-gate 	dkm = &un->un_label.dkl_map[part];
15020Sstevel@tonic-gate 	if (dkm->dkl_nblk == 0) {
15030Sstevel@tonic-gate 		sema_v(&fdc->c_ocsem);
15040Sstevel@tonic-gate 		return (ENXIO);
15050Sstevel@tonic-gate 	}
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_OPEN,
15080Sstevel@tonic-gate 	    (C, "fdopen: ctlr %d unit %d part %d\n",
15090Sstevel@tonic-gate 	    ddi_get_instance(fdc->c_dip), unit, part));
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_OPEN,
15120Sstevel@tonic-gate 	    (C, "fdopen: flag 0x%x", flag));
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	/*
15160Sstevel@tonic-gate 	 * Insure that drive is present with a recalibrate on first open.
15170Sstevel@tonic-gate 	 */
15180Sstevel@tonic-gate 	(void) pm_busy_component(fdc->c_dip, 0);
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	mutex_enter(&fdc->c_lolock);
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 	if (fdc->c_un->un_state == FD_STATE_STOPPED) {
15250Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
15260Sstevel@tonic-gate 		if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
1527*7656SSherry.Moore@Sun.COM 		    != DDI_SUCCESS) {
15280Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power change \
1529*7656SSherry.Moore@Sun.COM 			    failed. \n"));
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate 				sema_v(&fdc->c_ocsem);
15320Sstevel@tonic-gate 				(void) pm_idle_component(fdc->c_dip, 0);
15330Sstevel@tonic-gate 				return (EIO);
15340Sstevel@tonic-gate 		}
15350Sstevel@tonic-gate 		mutex_enter(&fdc->c_lolock);
15360Sstevel@tonic-gate 	}
15370Sstevel@tonic-gate 	if (fd_unit_is_open(un) == 0) {
15380Sstevel@tonic-gate 		fdgetcsb(fdc);
15390Sstevel@tonic-gate 		/*
15400Sstevel@tonic-gate 		 * no check changed!
15410Sstevel@tonic-gate 		 */
15420Sstevel@tonic-gate 		err = fdrecalseek(fdc, unit, -1, 0);
15430Sstevel@tonic-gate 		fdretcsb(fdc);
15440Sstevel@tonic-gate 		if (err) {
15450Sstevel@tonic-gate 			FDERRPRINT(FDEP_L3, FDEM_OPEN,
15460Sstevel@tonic-gate 			    (C, "fd%d: drive not ready\n", 0));
15470Sstevel@tonic-gate 			/* deselect drv on last close */
15480Sstevel@tonic-gate 			fdselect(fdc, unit, 0);
15490Sstevel@tonic-gate 			mutex_exit(&fdc->c_lolock);
15500Sstevel@tonic-gate 			sema_v(&fdc->c_ocsem);
15510Sstevel@tonic-gate 			(void) pm_idle_component(fdc->c_dip, 0);
15520Sstevel@tonic-gate 			return (EIO);
15530Sstevel@tonic-gate 		}
15540Sstevel@tonic-gate 	}
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 	/*
15570Sstevel@tonic-gate 	 * Check for previous exclusive open, or trying to exclusive open
15580Sstevel@tonic-gate 	 */
15590Sstevel@tonic-gate 	if (otyp == OTYP_LYR) {
15600Sstevel@tonic-gate 		part_is_open = (un->un_lyropen[part] != 0);
15610Sstevel@tonic-gate 	} else {
15620Sstevel@tonic-gate 		part_is_open = fd_part_is_open(un, part);
15630Sstevel@tonic-gate 	}
15640Sstevel@tonic-gate 	if ((un->un_exclmask & pbit) || ((flag & FEXCL) && part_is_open)) {
15650Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
15660Sstevel@tonic-gate 		sema_v(&fdc->c_ocsem);
15670Sstevel@tonic-gate 		FDERRPRINT(FDEP_L2, FDEM_OPEN, (C, "fd:just return\n"));
15680Sstevel@tonic-gate 		(void) pm_idle_component(fdc->c_dip, 0);
15690Sstevel@tonic-gate 		return (EBUSY);
15700Sstevel@tonic-gate 	}
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	/* don't attempt access, just return successfully */
15730Sstevel@tonic-gate 	if (flag & (FNDELAY | FNONBLOCK)) {
15740Sstevel@tonic-gate 		FDERRPRINT(FDEP_L2, FDEM_OPEN,
15750Sstevel@tonic-gate 		    (C, "fd: return busy..\n"));
15760Sstevel@tonic-gate 		goto out;
15770Sstevel@tonic-gate 	}
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 	fdc->c_csb.csb_unit = (uchar_t)unit;
15800Sstevel@tonic-gate 	if (fdgetlabel(fdc, unit)) {
15810Sstevel@tonic-gate 		/* didn't find label (couldn't read anything) */
15820Sstevel@tonic-gate 		FDERRPRINT(FDEP_L3, FDEM_OPEN,
15830Sstevel@tonic-gate 		    (C,
15840Sstevel@tonic-gate 		    "fd%d: unformatted diskette or no diskette in the drive\n",
15850Sstevel@tonic-gate 		    0));
15860Sstevel@tonic-gate 		if (fd_unit_is_open(un) == 0) {
15870Sstevel@tonic-gate 			/* deselect drv on last close */
15880Sstevel@tonic-gate 			fdselect(fdc, unit, 0);
15890Sstevel@tonic-gate 		}
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
15920Sstevel@tonic-gate 		sema_v(&fdc->c_ocsem);
15930Sstevel@tonic-gate 		(void) pm_idle_component(fdc->c_dip, 0);
15940Sstevel@tonic-gate 		return (EIO);
15950Sstevel@tonic-gate 	}
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	/*
15980Sstevel@tonic-gate 	 * if opening for writing, check write protect on diskette
15990Sstevel@tonic-gate 	 */
16000Sstevel@tonic-gate 	if (flag & FWRITE) {
16010Sstevel@tonic-gate 		fdgetcsb(fdc);
16020Sstevel@tonic-gate 		err = fdsensedrv(fdc, unit) & WP_SR3;
16030Sstevel@tonic-gate 		fdretcsb(fdc);
16040Sstevel@tonic-gate 		if (err) {
16050Sstevel@tonic-gate 			if (fd_unit_is_open(un) == 0)
16060Sstevel@tonic-gate 				fdselect(fdc, unit, 0);
16070Sstevel@tonic-gate 			mutex_exit(&fdc->c_lolock);
16080Sstevel@tonic-gate 			sema_v(&fdc->c_ocsem);
16090Sstevel@tonic-gate 			(void) pm_idle_component(fdc->c_dip, 0);
16100Sstevel@tonic-gate 			return (EROFS);
16110Sstevel@tonic-gate 		}
16120Sstevel@tonic-gate 	}
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate out:
16150Sstevel@tonic-gate 	/*
16160Sstevel@tonic-gate 	 * mark open as having succeeded
16170Sstevel@tonic-gate 	 */
16180Sstevel@tonic-gate 	if (flag & FEXCL) {
16190Sstevel@tonic-gate 		un->un_exclmask |= pbit;
16200Sstevel@tonic-gate 	}
16210Sstevel@tonic-gate 	if (otyp == OTYP_LYR) {
16220Sstevel@tonic-gate 		un->un_lyropen[part]++;
16230Sstevel@tonic-gate 	} else {
16240Sstevel@tonic-gate 		un->un_regopen[otyp] |= pbit;
16250Sstevel@tonic-gate 	}
16260Sstevel@tonic-gate 	mutex_exit(&fdc->c_lolock);
16270Sstevel@tonic-gate 	sema_v(&fdc->c_ocsem);
16280Sstevel@tonic-gate 	(void) pm_idle_component(fdc->c_dip, 0);
16290Sstevel@tonic-gate 	return (0);
16300Sstevel@tonic-gate }
16310Sstevel@tonic-gate /*
16320Sstevel@tonic-gate  * fd_part_is_open
16330Sstevel@tonic-gate  *	return 1 if the partition is open
16340Sstevel@tonic-gate  *	return 0 otherwise
16350Sstevel@tonic-gate  */
16360Sstevel@tonic-gate static int
16370Sstevel@tonic-gate fd_part_is_open(struct fdunit *un, int part)
16380Sstevel@tonic-gate {
16390Sstevel@tonic-gate 	int i;
16400Sstevel@tonic-gate 	for (i = 0; i < OTYPCNT - 1; i++)
16410Sstevel@tonic-gate 		if (un->un_regopen[i] & (1 << part))
16420Sstevel@tonic-gate 			return (1);
16430Sstevel@tonic-gate 	return (0);
16440Sstevel@tonic-gate }
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate /* ARGSUSED */
16480Sstevel@tonic-gate static int
16490Sstevel@tonic-gate fd_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
16500Sstevel@tonic-gate {
16510Sstevel@tonic-gate 	int unit, part_is_closed, part;
16520Sstevel@tonic-gate 	register struct fdctlr *fdc;
16530Sstevel@tonic-gate 	register struct fdunit *un;
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate 	fdc = fd_getctlr(dev);
16560Sstevel@tonic-gate 	if (!fdc || !(un = fdc->c_un))
16570Sstevel@tonic-gate 		return (ENXIO);
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 	unit = fdc->c_un->un_unit_no;
16610Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_CLOS, (C, "fd_close\n"));
16620Sstevel@tonic-gate 	part = FDPARTITION(dev);
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 	sema_p(&fdc->c_ocsem);
16650Sstevel@tonic-gate 	mutex_enter(&fdc->c_lolock);
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 	if (otyp == OTYP_LYR) {
16680Sstevel@tonic-gate 		un->un_lyropen[part]--;
16690Sstevel@tonic-gate 		part_is_closed = (un->un_lyropen[part] == 0);
16700Sstevel@tonic-gate 	} else {
16710Sstevel@tonic-gate 		un->un_regopen[otyp] &= ~(1<<part);
16720Sstevel@tonic-gate 		part_is_closed = 1;
16730Sstevel@tonic-gate 	}
16740Sstevel@tonic-gate 	if (part_is_closed)
16750Sstevel@tonic-gate 		un->un_exclmask &= ~(1<<part);
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 	if (fd_unit_is_open(un) == 0) {
16780Sstevel@tonic-gate 		/* deselect drive on last close */
16790Sstevel@tonic-gate 		fdselect(fdc, unit, 0);
16800Sstevel@tonic-gate 		un->un_flags &= ~FDUNIT_CHANGED;
16810Sstevel@tonic-gate 	}
16820Sstevel@tonic-gate 	mutex_exit(&fdc->c_lolock);
16830Sstevel@tonic-gate 	sema_v(&fdc->c_ocsem);
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate 	return (0);
16860Sstevel@tonic-gate }
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate /*
16890Sstevel@tonic-gate  * fd_strategy
16900Sstevel@tonic-gate  *	checks operation, hangs buf struct off fdctlr, calls fdstart
16910Sstevel@tonic-gate  *	if not already busy.  Note that if we call start, then the operation
16920Sstevel@tonic-gate  *	will already be done on return (start sleeps).
16930Sstevel@tonic-gate  */
16940Sstevel@tonic-gate static int
16950Sstevel@tonic-gate fd_strategy(register struct buf *bp)
16960Sstevel@tonic-gate {
16970Sstevel@tonic-gate 	struct fdctlr *fdc;
16980Sstevel@tonic-gate 	struct fdunit *un;
16990Sstevel@tonic-gate 	uint_t	phys_blkno;
17000Sstevel@tonic-gate 	struct dk_map32 *dkm;
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_STRA,
17030Sstevel@tonic-gate 	    (C, "fd_strategy: bp = 0x%p, dev = 0x%lx\n",
17040Sstevel@tonic-gate 	    (void *)bp, bp->b_edev));
17050Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_STRA,
17060Sstevel@tonic-gate 	    (C, "b_blkno=%x b_flags=%x b_count=%x\n",
17070Sstevel@tonic-gate 	    (int)bp->b_blkno, bp->b_flags, (int)bp->b_bcount));
17080Sstevel@tonic-gate 	fdc = fd_getctlr(bp->b_edev);
17090Sstevel@tonic-gate 	un = fdc->c_un;
17100Sstevel@tonic-gate 	dkm = &un->un_label.dkl_map[FDPARTITION(bp->b_edev)];
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 	/*
17130Sstevel@tonic-gate 	 * If it's medium density and the block no. isn't a multiple
17140Sstevel@tonic-gate 	 * of 1K, then return an error.
17150Sstevel@tonic-gate 	 */
17160Sstevel@tonic-gate 	if (un->un_chars->fdc_medium) {
17170Sstevel@tonic-gate 		phys_blkno = (uint_t)bp->b_blkno >> 1;
17180Sstevel@tonic-gate 		if (bp->b_blkno & 1) {
17190Sstevel@tonic-gate 			FDERRPRINT(FDEP_L3, FDEM_STRA,
17200Sstevel@tonic-gate 			    (C, "b_blkno=0x%lx is not 1k aligned\n",
17210Sstevel@tonic-gate 			    (long)bp->b_blkno));
17220Sstevel@tonic-gate 			bp->b_error = EINVAL;
17230Sstevel@tonic-gate 			bp->b_resid = bp->b_bcount;
17240Sstevel@tonic-gate 			bp->b_flags |= B_ERROR;
17250Sstevel@tonic-gate 			biodone(bp);
17260Sstevel@tonic-gate 			return (0);
17270Sstevel@tonic-gate 		}
17280Sstevel@tonic-gate 	} else {
17290Sstevel@tonic-gate 		phys_blkno = (uint_t)bp->b_blkno;
17300Sstevel@tonic-gate 	}
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 	/* If the block number is past the end, return an error */
17340Sstevel@tonic-gate 	if ((phys_blkno > dkm->dkl_nblk)) {
17350Sstevel@tonic-gate 		FDERRPRINT(FDEP_L3, FDEM_STRA,
17360Sstevel@tonic-gate 		    (C, "fd%d: block %ld is past the end! (nblk=%d)\n",
17370Sstevel@tonic-gate 		    0, (long)bp->b_blkno, dkm->dkl_nblk));
17380Sstevel@tonic-gate 		bp->b_error = ENOSPC;
17390Sstevel@tonic-gate 		bp->b_resid = bp->b_bcount;
17400Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
17410Sstevel@tonic-gate 		biodone(bp);
17420Sstevel@tonic-gate 		return (0);
17430Sstevel@tonic-gate 	}
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 	/* if at end of file, skip out now */
17460Sstevel@tonic-gate 	if (phys_blkno == dkm->dkl_nblk) {
17470Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_STRA,
17480Sstevel@tonic-gate 		    (C, "b_blkno is at the end!\n"));
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 		if ((bp->b_flags & B_READ) == 0) {
17510Sstevel@tonic-gate 			/* a write needs to get an error! */
17520Sstevel@tonic-gate 			bp->b_error = ENOSPC;
17530Sstevel@tonic-gate 			bp->b_flags |= B_ERROR;
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_STRA,
17560Sstevel@tonic-gate 			    (C, "block is at end and this is a write\n"));
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate 		}
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 		bp->b_resid = bp->b_bcount;
17610Sstevel@tonic-gate 		biodone(bp);
17620Sstevel@tonic-gate 		return (0);
17630Sstevel@tonic-gate 	}
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 	/* if operation not a multiple of sector size, is error! */
17660Sstevel@tonic-gate 	if (bp->b_bcount % un->un_chars->fdc_sec_size)	{
17670Sstevel@tonic-gate 		FDERRPRINT(FDEP_L3, FDEM_STRA,
17680Sstevel@tonic-gate 		    (C, "fd%d: requested transfer size(0x%lx) is not"
1769*7656SSherry.Moore@Sun.COM 		    " multiple of sector size(0x%x)\n", 0,
1770*7656SSherry.Moore@Sun.COM 		    bp->b_bcount, un->un_chars->fdc_sec_size));
17710Sstevel@tonic-gate 		FDERRPRINT(FDEP_L3, FDEM_STRA,
17720Sstevel@tonic-gate 		    (C, "	b_blkno=0x%lx b_flags=0x%x\n",
17730Sstevel@tonic-gate 		    (long)bp->b_blkno, bp->b_flags));
17740Sstevel@tonic-gate 		bp->b_error = EINVAL;
17750Sstevel@tonic-gate 		bp->b_resid = bp->b_bcount;
17760Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
17770Sstevel@tonic-gate 		biodone(bp);
17780Sstevel@tonic-gate 		return (0);
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 	}
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 	/*
17830Sstevel@tonic-gate 	 * Put the buf request in the controller's queue, FIFO.
17840Sstevel@tonic-gate 	 */
17850Sstevel@tonic-gate 	bp->av_forw = 0;
17860Sstevel@tonic-gate 	sema_p(&fdc->c_ocsem);
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate 	(void) pm_busy_component(fdc->c_dip, 0);
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 	mutex_enter(&fdc->c_lolock);
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 	if (fdc->c_un->un_state == FD_STATE_STOPPED) {
17950Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
17960Sstevel@tonic-gate 		if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
1797*7656SSherry.Moore@Sun.COM 		    != DDI_SUCCESS) {
1798*7656SSherry.Moore@Sun.COM 			sema_v(&fdc->c_ocsem);
1799*7656SSherry.Moore@Sun.COM 			(void) pm_idle_component(fdc->c_dip, 0);
1800*7656SSherry.Moore@Sun.COM 			bp->b_error = EIO;
1801*7656SSherry.Moore@Sun.COM 			bp->b_resid = bp->b_bcount;
1802*7656SSherry.Moore@Sun.COM 			bp->b_flags |= B_ERROR;
1803*7656SSherry.Moore@Sun.COM 			biodone(bp);
1804*7656SSherry.Moore@Sun.COM 			return (0);
18050Sstevel@tonic-gate 		} else {
18060Sstevel@tonic-gate 			mutex_enter(&fdc->c_lolock);
18070Sstevel@tonic-gate 		}
18080Sstevel@tonic-gate 	}
18090Sstevel@tonic-gate 	if (un->un_iostat) {
18100Sstevel@tonic-gate 		kstat_waitq_enter(KIOSP);
18110Sstevel@tonic-gate 	}
18120Sstevel@tonic-gate 	if (fdc->c_actf)
18130Sstevel@tonic-gate 		fdc->c_actl->av_forw = bp;
18140Sstevel@tonic-gate 	else
18150Sstevel@tonic-gate 		fdc->c_actf = bp;
18160Sstevel@tonic-gate 	fdc->c_actl = bp;
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate 	/* call fdstart to start the transfer */
18200Sstevel@tonic-gate 	fdstart(fdc);
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 	mutex_exit(&fdc->c_lolock);
18230Sstevel@tonic-gate 	sema_v(&fdc->c_ocsem);
18240Sstevel@tonic-gate 	(void) pm_idle_component(fdc->c_dip, 0);
18250Sstevel@tonic-gate 	return (0);
18260Sstevel@tonic-gate }
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate /* ARGSUSED2 */
18290Sstevel@tonic-gate static int
18300Sstevel@tonic-gate fd_read(dev_t dev, struct uio *uio, cred_t *cred_p)
18310Sstevel@tonic-gate {
18320Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_RDWR, (C, "fd_read\n"));
18330Sstevel@tonic-gate 	return (physio(fd_strategy, NULL, dev, B_READ, minphys, uio));
18340Sstevel@tonic-gate }
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate /* ARGSUSED2 */
18370Sstevel@tonic-gate static int
18380Sstevel@tonic-gate fd_write(dev_t dev, struct uio *uio, cred_t *cred_p)
18390Sstevel@tonic-gate {
18400Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_RDWR, (C, "fd_write\n"));
18410Sstevel@tonic-gate 	return (physio(fd_strategy, NULL, dev, B_WRITE, minphys, uio));
18420Sstevel@tonic-gate }
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate static void
18450Sstevel@tonic-gate fdmotoff(void *arg)
18460Sstevel@tonic-gate {
18470Sstevel@tonic-gate 	struct fdctlr *fdc = arg;
18480Sstevel@tonic-gate 	int unit = fdc->c_un->un_unit_no;
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 	mutex_enter(&fdc->c_lolock);
18510Sstevel@tonic-gate 
18520Sstevel@tonic-gate 	/* Just return if we're about to call untimeout */
18530Sstevel@tonic-gate 	if (fdc->c_mtimeid == 0) {
18540Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
18550Sstevel@tonic-gate 		return;
18560Sstevel@tonic-gate 	}
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_MOFF, (C, "fdmotoff\n"));
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 	fdc->c_mtimeid = 0;
18610Sstevel@tonic-gate 
18620Sstevel@tonic-gate 	if (!(Msr(fdc) & CB) && (Dor(fdc) & (MOTEN(unit)))) {
18630Sstevel@tonic-gate 		/* LINTED */
18640Sstevel@tonic-gate 		Set_dor(fdc, MOTEN(unit), 0);
18650Sstevel@tonic-gate 	}
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 	mutex_exit(&fdc->c_lolock);
18680Sstevel@tonic-gate }
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate /* ARGSUSED */
18710Sstevel@tonic-gate static int
18720Sstevel@tonic-gate fd_ioctl(dev_t dev, int cmd, intptr_t arg, int flag,
18730Sstevel@tonic-gate 	cred_t *cred_p, int *rval_p)
18740Sstevel@tonic-gate {
18750Sstevel@tonic-gate 	union {
18760Sstevel@tonic-gate 		struct dk_cinfo dki;
18770Sstevel@tonic-gate 		struct dk_geom dkg;
18780Sstevel@tonic-gate 		struct dk_allmap32 dka;
18790Sstevel@tonic-gate 		struct fd_char fdchar;
18800Sstevel@tonic-gate 		struct fd_drive drvchar;
18810Sstevel@tonic-gate 		int	temp;
18820Sstevel@tonic-gate 	} cpy;
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate 	struct vtoc	vtoc;
18850Sstevel@tonic-gate 	struct fdunit *un;
18860Sstevel@tonic-gate 	struct fdctlr *fdc;
18870Sstevel@tonic-gate 	int unit, dkunit;
18880Sstevel@tonic-gate 	int err = 0;
18890Sstevel@tonic-gate 	uint_t	sec_size;
18900Sstevel@tonic-gate 	enum dkio_state state;
18910Sstevel@tonic-gate 	int	transfer_rate;
18920Sstevel@tonic-gate 
18930Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_IOCT,
18940Sstevel@tonic-gate 	    (C, "fd_ioctl: cmd 0x%x, arg 0x%lx\n", cmd, (long)arg));
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 	/* The minor number should always be 0 */
18970Sstevel@tonic-gate 	if (FDUNIT(dev) != 0)
18980Sstevel@tonic-gate 		return (ENXIO);
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate 	fdc = fd_getctlr(dev);
19010Sstevel@tonic-gate 	unit = fdc->c_un->un_unit_no;
19020Sstevel@tonic-gate 	un = fdc->c_un;
19030Sstevel@tonic-gate 	sec_size = un->un_chars->fdc_sec_size;
19040Sstevel@tonic-gate 	bzero(&cpy, sizeof (cpy));
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 	switch (cmd) {
19070Sstevel@tonic-gate 	case DKIOCINFO:
19080Sstevel@tonic-gate 		cpy.dki.dki_addr = 0;
19090Sstevel@tonic-gate 
19100Sstevel@tonic-gate 		/*
19110Sstevel@tonic-gate 		 * The meaning of the dki_slave and dki_unit fields
19120Sstevel@tonic-gate 		 * is unclear.  The sparc floppy driver follows the same
19130Sstevel@tonic-gate 		 * convention as sd.c in that the instance number is
19140Sstevel@tonic-gate 		 * returned in the dki_cnum field.  The dki_slave field is
19150Sstevel@tonic-gate 		 * ignored.
19160Sstevel@tonic-gate 		 *
19170Sstevel@tonic-gate 		 * The dki_cnum contains the controller instance
19180Sstevel@tonic-gate 		 * and its value can be any positive number. Even
19190Sstevel@tonic-gate 		 * though currently Sparc platforms only support
19200Sstevel@tonic-gate 		 * one controller, the controller instance number
19210Sstevel@tonic-gate 		 * can be any number since it is assigned by the
19220Sstevel@tonic-gate 		 * system depending on the device properties.
19230Sstevel@tonic-gate 		 */
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate 		cpy.dki.dki_cnum = FDCTLR(dev);
19260Sstevel@tonic-gate 
19270Sstevel@tonic-gate 		/*
19280Sstevel@tonic-gate 		 * Sparc platforms support only one floppy drive.
19290Sstevel@tonic-gate 		 * The device node for the controller is the same as
19300Sstevel@tonic-gate 		 * the device node for the drive.  The x86 driver is
19310Sstevel@tonic-gate 		 * different in that it has a node for the controller
19320Sstevel@tonic-gate 		 * and a child node for each drive. Since Sparc supports
19330Sstevel@tonic-gate 		 * only one drive, the unit number will always be zero.
19340Sstevel@tonic-gate 		 */
19350Sstevel@tonic-gate 
19360Sstevel@tonic-gate 		cpy.dki.dki_unit = FDUNIT(dev);
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate 		/*
19390Sstevel@tonic-gate 		 * The meaning of the dki_slave field is unclear.
19400Sstevel@tonic-gate 		 * So, I will leave it set to 0.
19410Sstevel@tonic-gate 		 */
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 		cpy.dki.dki_slave = 0;
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 		cpy.dki.dki_ctype = (ushort_t)-1;
19460Sstevel@tonic-gate 		if (fdc->c_fdtype & FDCTYPE_82077)
19470Sstevel@tonic-gate 			cpy.dki.dki_ctype = DKC_INTEL82077;
19480Sstevel@tonic-gate 		cpy.dki.dki_flags = DKI_FMTTRK;
19490Sstevel@tonic-gate 		cpy.dki.dki_partition = FDPARTITION(dev);
19500Sstevel@tonic-gate 		cpy.dki.dki_maxtransfer = maxphys / DEV_BSIZE;
19510Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&cpy.dki, (caddr_t)arg,
1952*7656SSherry.Moore@Sun.COM 		    sizeof (cpy.dki), flag))
19530Sstevel@tonic-gate 			err = EFAULT;
19540Sstevel@tonic-gate 		break;
19550Sstevel@tonic-gate 	case DKIOCGGEOM:
19560Sstevel@tonic-gate 		cpy.dkg.dkg_ncyl = un->un_chars->fdc_ncyl;
19570Sstevel@tonic-gate 		cpy.dkg.dkg_nhead = un->un_chars->fdc_nhead;
19580Sstevel@tonic-gate 		cpy.dkg.dkg_nsect = un->un_chars->fdc_secptrack;
19590Sstevel@tonic-gate 		cpy.dkg.dkg_intrlv = un->un_label.dkl_intrlv;
19600Sstevel@tonic-gate 		cpy.dkg.dkg_rpm = un->un_label.dkl_rpm;
19610Sstevel@tonic-gate 		cpy.dkg.dkg_pcyl = un->un_chars->fdc_ncyl;
19620Sstevel@tonic-gate 		cpy.dkg.dkg_read_reinstruct =
19630Sstevel@tonic-gate 		    (int)(cpy.dkg.dkg_nsect * cpy.dkg.dkg_rpm * 4) / 60000;
19640Sstevel@tonic-gate 		cpy.dkg.dkg_write_reinstruct = cpy.dkg.dkg_read_reinstruct;
19650Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&cpy.dkg, (caddr_t)arg,
1966*7656SSherry.Moore@Sun.COM 		    sizeof (cpy.dkg), flag))
19670Sstevel@tonic-gate 			err = EFAULT;
19680Sstevel@tonic-gate 		break;
19690Sstevel@tonic-gate 	case DKIOCSGEOM:
19700Sstevel@tonic-gate 		FDERRPRINT(FDEP_L3, FDEM_IOCT,
19710Sstevel@tonic-gate 		    (C, "fd_ioctl: DKIOCSGEOM not supported\n"));
19720Sstevel@tonic-gate 		err = ENOTTY;
19730Sstevel@tonic-gate 		break;
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate 	/*
19760Sstevel@tonic-gate 	 * return the map of all logical partitions
19770Sstevel@tonic-gate 	 */
19780Sstevel@tonic-gate 	case DKIOCGAPART:
19790Sstevel@tonic-gate 		/*
19800Sstevel@tonic-gate 		 * We don't have anything to do if the application is ILP32
19810Sstevel@tonic-gate 		 * because the label map has a 32-bit format. Otherwise
19820Sstevel@tonic-gate 		 * convert.
19830Sstevel@tonic-gate 		 */
19840Sstevel@tonic-gate 		if ((flag & DATAMODEL_MASK) == DATAMODEL_ILP32) {
19850Sstevel@tonic-gate 			if (ddi_copyout(&un->un_label.dkl_map,
1986*7656SSherry.Moore@Sun.COM 			    (void *)arg, sizeof (struct dk_allmap32), flag))
19870Sstevel@tonic-gate 				err = EFAULT;
19880Sstevel@tonic-gate 		}
19890Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
19900Sstevel@tonic-gate 		else {
19910Sstevel@tonic-gate 			struct dk_allmap dk_allmap;
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 			ASSERT((flag & DATAMODEL_MASK) == DATAMODEL_LP64);
19940Sstevel@tonic-gate 			for (dkunit = 0; dkunit < NDKMAP; dkunit++) {
19950Sstevel@tonic-gate 				dk_allmap.dka_map[dkunit].dkl_cylno =
19960Sstevel@tonic-gate 				    un->un_label.dkl_map[dkunit].dkl_cylno;
19970Sstevel@tonic-gate 				dk_allmap.dka_map[dkunit].dkl_nblk =
19980Sstevel@tonic-gate 				    un->un_label.dkl_map[dkunit].dkl_nblk;
19990Sstevel@tonic-gate 			}
20000Sstevel@tonic-gate 			if (ddi_copyout(&dk_allmap, (void *)arg,
2001*7656SSherry.Moore@Sun.COM 			    sizeof (struct dk_allmap), flag))
20020Sstevel@tonic-gate 				err = EFAULT;
20030Sstevel@tonic-gate 		}
20040Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
20050Sstevel@tonic-gate 		break;
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate 	/*
20080Sstevel@tonic-gate 	 * Set the map of all logical partitions
20090Sstevel@tonic-gate 	 */
20100Sstevel@tonic-gate 	case DKIOCSAPART:
20110Sstevel@tonic-gate 		if ((flag & DATAMODEL_MASK) == DATAMODEL_ILP32) {
20120Sstevel@tonic-gate 			if (ddi_copyin((const void *)arg, &cpy.dka,
20130Sstevel@tonic-gate 			    sizeof (cpy.dka), flag))
20140Sstevel@tonic-gate 				return (EFAULT);
20150Sstevel@tonic-gate 			else {
20160Sstevel@tonic-gate 				mutex_enter(&fdc->c_lolock);
20170Sstevel@tonic-gate 				for (dkunit = 0; dkunit < NDKMAP; dkunit++) {
20180Sstevel@tonic-gate 					un->un_label.dkl_map[dkunit] =
2019*7656SSherry.Moore@Sun.COM 					    cpy.dka.dka_map[dkunit];
20200Sstevel@tonic-gate 				}
20210Sstevel@tonic-gate 				mutex_exit(&fdc->c_lolock);
20220Sstevel@tonic-gate 			}
20230Sstevel@tonic-gate 		}
20240Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
20250Sstevel@tonic-gate 		else {
20260Sstevel@tonic-gate 			struct dk_allmap dk_allmap;
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 			ASSERT((flag & DATAMODEL_MASK) == DATAMODEL_LP64);
20290Sstevel@tonic-gate 			if (ddi_copyin((const void *)arg, &dk_allmap,
20300Sstevel@tonic-gate 			    sizeof (dk_allmap), flag))
20310Sstevel@tonic-gate 				return (EFAULT);
20320Sstevel@tonic-gate 			else {
20330Sstevel@tonic-gate 				mutex_enter(&fdc->c_lolock);
20340Sstevel@tonic-gate 				for (dkunit = 0; dkunit < NDKMAP; dkunit++) {
20350Sstevel@tonic-gate 					un->un_label.dkl_map[dkunit].dkl_cylno =
20360Sstevel@tonic-gate 					    dk_allmap.dka_map[dkunit].dkl_cylno;
20370Sstevel@tonic-gate 					un->un_label.dkl_map[dkunit].dkl_nblk =
20380Sstevel@tonic-gate 					    dk_allmap.dka_map[dkunit].dkl_nblk;
20390Sstevel@tonic-gate 				}
20400Sstevel@tonic-gate 				mutex_exit(&fdc->c_lolock);
20410Sstevel@tonic-gate 			}
20420Sstevel@tonic-gate 		}
20430Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
20440Sstevel@tonic-gate 		break;
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 	case DKIOCGVTOC:
20470Sstevel@tonic-gate 		mutex_enter(&fdc->c_lolock);
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 		/*
20500Sstevel@tonic-gate 		 * Exit if the diskette has no label.
20510Sstevel@tonic-gate 		 * Also, get the label to make sure the
20520Sstevel@tonic-gate 		 * correct one is being used since the diskette
20530Sstevel@tonic-gate 		 * may have changed
20540Sstevel@tonic-gate 		 */
20550Sstevel@tonic-gate 		if (fdgetlabel(fdc, unit)) {
20560Sstevel@tonic-gate 			mutex_exit(&fdc->c_lolock);
20570Sstevel@tonic-gate 			err = EINVAL;
20580Sstevel@tonic-gate 			break;
20590Sstevel@tonic-gate 		}
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 		/* Build a vtoc from the diskette's label */
20620Sstevel@tonic-gate 		fd_build_user_vtoc(un, &vtoc);
20630Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
20660Sstevel@tonic-gate 		switch (ddi_model_convert_from(flag & FMODELS)) {
20670Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
20680Sstevel@tonic-gate 			struct vtoc32 vtoc32;
20690Sstevel@tonic-gate 
20700Sstevel@tonic-gate 			vtoctovtoc32(vtoc, vtoc32);
20710Sstevel@tonic-gate 			if (ddi_copyout(&vtoc32, (void *)arg,
20720Sstevel@tonic-gate 			    sizeof (struct vtoc32), flag))
20730Sstevel@tonic-gate 				return (EFAULT);
20740Sstevel@tonic-gate 			break;
20750Sstevel@tonic-gate 		}
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 		case DDI_MODEL_NONE:
20780Sstevel@tonic-gate 			if (ddi_copyout(&vtoc, (void *)arg,
20790Sstevel@tonic-gate 			    sizeof (vtoc), flag))
20800Sstevel@tonic-gate 				return (EFAULT);
20810Sstevel@tonic-gate 			break;
20820Sstevel@tonic-gate 		}
20830Sstevel@tonic-gate #else /* ! _MULTI_DATAMODEL */
20840Sstevel@tonic-gate 		if (ddi_copyout(&vtoc, (void *)arg, sizeof (vtoc), flag))
20850Sstevel@tonic-gate 			return (EFAULT);
20860Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
20870Sstevel@tonic-gate 		break;
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 	case DKIOCSVTOC:
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
20920Sstevel@tonic-gate 		switch (ddi_model_convert_from(flag & FMODELS)) {
20930Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
20940Sstevel@tonic-gate 			struct vtoc32 vtoc32;
20950Sstevel@tonic-gate 
20960Sstevel@tonic-gate 			if (ddi_copyin((const void *)arg, &vtoc32,
20970Sstevel@tonic-gate 			    sizeof (struct vtoc32), flag)) {
20980Sstevel@tonic-gate 				return (EFAULT);
20990Sstevel@tonic-gate 			}
21000Sstevel@tonic-gate 			vtoc32tovtoc(vtoc32, vtoc);
21010Sstevel@tonic-gate 			break;
21020Sstevel@tonic-gate 		}
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 		case DDI_MODEL_NONE:
21050Sstevel@tonic-gate 			if (ddi_copyin((const void *)arg, &vtoc,
21060Sstevel@tonic-gate 			    sizeof (vtoc), flag)) {
21070Sstevel@tonic-gate 				return (EFAULT);
21080Sstevel@tonic-gate 			}
21090Sstevel@tonic-gate 			break;
21100Sstevel@tonic-gate 		}
21110Sstevel@tonic-gate #else /* ! _MULTI_DATAMODEL */
21120Sstevel@tonic-gate 		if (ddi_copyin((const void *)arg, &vtoc, sizeof (vtoc), flag))
21130Sstevel@tonic-gate 			return (EFAULT);
21140Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate 		mutex_enter(&fdc->c_lolock);
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate 		/*
21190Sstevel@tonic-gate 		 * The characteristics structure must be filled in because
21200Sstevel@tonic-gate 		 * it helps build the vtoc.
21210Sstevel@tonic-gate 		 */
21220Sstevel@tonic-gate 		if ((un->un_chars->fdc_ncyl == 0) ||
2123*7656SSherry.Moore@Sun.COM 		    (un->un_chars->fdc_nhead == 0) ||
2124*7656SSherry.Moore@Sun.COM 		    (un->un_chars->fdc_secptrack == 0)) {
21250Sstevel@tonic-gate 			mutex_exit(&fdc->c_lolock);
21260Sstevel@tonic-gate 			err = EINVAL;
21270Sstevel@tonic-gate 			break;
21280Sstevel@tonic-gate 		}
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 		if ((err = fd_build_label_vtoc(un, &vtoc)) != 0) {
21310Sstevel@tonic-gate 			mutex_exit(&fdc->c_lolock);
21320Sstevel@tonic-gate 			break;
21330Sstevel@tonic-gate 		}
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate 		(void) pm_busy_component(fdc->c_dip, 0);
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate 		err = fdrw(fdc, unit, FDWRITE, 0, 0, 1,
21380Sstevel@tonic-gate 		    (caddr_t)&un->un_label, sizeof (struct dk_label));
21390Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
21400Sstevel@tonic-gate 		(void) pm_idle_component(fdc->c_dip, 0);
21410Sstevel@tonic-gate 		break;
21420Sstevel@tonic-gate 
21430Sstevel@tonic-gate 	case DKIOCSTATE:
21440Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&state,
2145*7656SSherry.Moore@Sun.COM 		    sizeof (int), flag)) {
21460Sstevel@tonic-gate 			err = EFAULT;
21470Sstevel@tonic-gate 			break;
21480Sstevel@tonic-gate 		}
21490Sstevel@tonic-gate 		(void) pm_busy_component(fdc->c_dip, 0);
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate 		err = fd_check_media(dev, state);
21520Sstevel@tonic-gate 		(void) pm_idle_component(fdc->c_dip, 0);
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&un->un_media_state,
2155*7656SSherry.Moore@Sun.COM 		    (caddr_t)arg, sizeof (int), flag))
21560Sstevel@tonic-gate 			err = EFAULT;
21570Sstevel@tonic-gate 		break;
21580Sstevel@tonic-gate 
21590Sstevel@tonic-gate 	case FDIOGCHAR:
21600Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)un->un_chars, (caddr_t)arg,
2161*7656SSherry.Moore@Sun.COM 		    sizeof (struct fd_char), flag))
21620Sstevel@tonic-gate 			err = EFAULT;
21630Sstevel@tonic-gate 		break;
21640Sstevel@tonic-gate 
21650Sstevel@tonic-gate 	case FDIOSCHAR:
21660Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&cpy.fdchar,
21670Sstevel@tonic-gate 				sizeof (struct fd_char), flag)) {
21680Sstevel@tonic-gate 			err = EFAULT;
21690Sstevel@tonic-gate 			break;
21700Sstevel@tonic-gate 		}
21710Sstevel@tonic-gate 
21720Sstevel@tonic-gate 		/*
21730Sstevel@tonic-gate 		 * Check the fields in the fdchar structure that are either
21740Sstevel@tonic-gate 		 * driver or controller dependent.
21750Sstevel@tonic-gate 		 */
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate 		transfer_rate = cpy.fdchar.fdc_transfer_rate;
21780Sstevel@tonic-gate 		if ((transfer_rate != 500) && (transfer_rate != 300) &&
21790Sstevel@tonic-gate 		    (transfer_rate != 250) && (transfer_rate != 1000)) {
21800Sstevel@tonic-gate 			FDERRPRINT(FDEP_L3, FDEM_IOCT,
2181*7656SSherry.Moore@Sun.COM 			    (C, "fd_ioctl: FDIOSCHAR odd transfer rate %d\n",
21820Sstevel@tonic-gate 			    cpy.fdchar.fdc_transfer_rate));
21830Sstevel@tonic-gate 			err = EINVAL;
21840Sstevel@tonic-gate 			break;
21850Sstevel@tonic-gate 		}
21860Sstevel@tonic-gate 
21870Sstevel@tonic-gate 		if ((cpy.fdchar.fdc_nhead < 1) ||
2188*7656SSherry.Moore@Sun.COM 		    (cpy.fdchar.fdc_nhead > 2)) {
21890Sstevel@tonic-gate 			FDERRPRINT(FDEP_L3, FDEM_IOCT,
2190*7656SSherry.Moore@Sun.COM 			    (C, "fd_ioctl: FDIOSCHAR bad no. of heads %d\n",
21910Sstevel@tonic-gate 			    cpy.fdchar.fdc_nhead));
21920Sstevel@tonic-gate 			err = EINVAL;
21930Sstevel@tonic-gate 			break;
21940Sstevel@tonic-gate 		}
21950Sstevel@tonic-gate 
21960Sstevel@tonic-gate 		/*
21970Sstevel@tonic-gate 		 * The number of cylinders must be between 0 and 255
21980Sstevel@tonic-gate 		 */
21990Sstevel@tonic-gate 		if ((cpy.fdchar.fdc_ncyl < 0) || (cpy.fdchar.fdc_ncyl > 255)) {
22000Sstevel@tonic-gate 			FDERRPRINT(FDEP_L3, FDEM_IOCT,
2201*7656SSherry.Moore@Sun.COM 			    (C, "fd_ioctl: FDIOSCHAR bad cyl no %d\n",
22020Sstevel@tonic-gate 			    cpy.fdchar.fdc_ncyl));
22030Sstevel@tonic-gate 			err = EINVAL;
22040Sstevel@tonic-gate 			break;
22050Sstevel@tonic-gate 		}
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate 		/* Copy the fdchar structure */
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate 		mutex_enter(&fdc->c_lolock);
22100Sstevel@tonic-gate 		*(un->un_chars) = cpy.fdchar;
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 		un->un_curfdtype = -1;
22130Sstevel@tonic-gate 
22140Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 		break;
22170Sstevel@tonic-gate 	case FDEJECT:  /* eject disk */
22180Sstevel@tonic-gate 	case DKIOCEJECT:
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate 		/*
22210Sstevel@tonic-gate 		 * Fail the ioctl if auto-eject isn't supported
22220Sstevel@tonic-gate 		 */
22230Sstevel@tonic-gate 		if (fdc->c_un->un_drive->fdd_ejectable == 0) {
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate 			err = ENOSYS;
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate 		} else {
22280Sstevel@tonic-gate 			(void) pm_busy_component(fdc->c_dip, 0);
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate 			mutex_enter(&fdc->c_lolock);
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate 			CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
22330Sstevel@tonic-gate 
22340Sstevel@tonic-gate 			if (fdc->c_un->un_state == FD_STATE_STOPPED) {
22350Sstevel@tonic-gate 				mutex_exit(&fdc->c_lolock);
22360Sstevel@tonic-gate 				if ((pm_raise_power(fdc->c_dip, 0,
2237*7656SSherry.Moore@Sun.COM 				    PM_LEVEL_ON)) != DDI_SUCCESS) {
22380Sstevel@tonic-gate 					(void) pm_idle_component(fdc->c_dip, 0);
22390Sstevel@tonic-gate 					err = EIO;
22400Sstevel@tonic-gate 				}
22410Sstevel@tonic-gate 				mutex_enter(&fdc->c_lolock);
22420Sstevel@tonic-gate 			}
22430Sstevel@tonic-gate 		}
22440Sstevel@tonic-gate 		if (err == 0) {
22450Sstevel@tonic-gate 			fdselect(fdc, unit, 1);
22460Sstevel@tonic-gate 			fdeject(fdc, unit);
22470Sstevel@tonic-gate 			mutex_exit(&fdc->c_lolock);
22480Sstevel@tonic-gate 		}
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate 		(void) pm_idle_component(fdc->c_dip, 0);
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate 		/*
22530Sstevel@tonic-gate 		 * Make sure the drive is turned off
22540Sstevel@tonic-gate 		 */
22550Sstevel@tonic-gate 		if (fdc->c_fdtype & FDCTYPE_82077) {
22560Sstevel@tonic-gate 			if (fdc->c_mtimeid == 0) {
22570Sstevel@tonic-gate 				fdc->c_mtimeid = timeout(fdmotoff, fdc,
2258*7656SSherry.Moore@Sun.COM 				    Motoff_delay);
22590Sstevel@tonic-gate 			}
22600Sstevel@tonic-gate 		}
22610Sstevel@tonic-gate 
22620Sstevel@tonic-gate 		break;
22630Sstevel@tonic-gate 	case FDGETCHANGE: /* disk changed */
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&cpy.temp,
2266*7656SSherry.Moore@Sun.COM 		    sizeof (int), flag)) {
22670Sstevel@tonic-gate 			err = EFAULT;
22680Sstevel@tonic-gate 			break;
22690Sstevel@tonic-gate 		}
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 		/* zero out the user's parameter */
22720Sstevel@tonic-gate 		cpy.temp = 0;
22730Sstevel@tonic-gate 
22740Sstevel@tonic-gate 		(void) pm_busy_component(fdc->c_dip, 0);
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 		mutex_enter(&fdc->c_lolock);
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 		CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
22790Sstevel@tonic-gate 
22800Sstevel@tonic-gate 		if (fdc->c_un->un_state == FD_STATE_STOPPED) {
22810Sstevel@tonic-gate 			mutex_exit(&fdc->c_lolock);
22820Sstevel@tonic-gate 			if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
2283*7656SSherry.Moore@Sun.COM 			    != DDI_SUCCESS) {
22840Sstevel@tonic-gate 				FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power \
2285*7656SSherry.Moore@Sun.COM 				    change failed. \n"));
22860Sstevel@tonic-gate 				(void) pm_idle_component(fdc->c_dip, 0);
22870Sstevel@tonic-gate 				return (EIO);
22880Sstevel@tonic-gate 			}
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate 			mutex_enter(&fdc->c_lolock);
22910Sstevel@tonic-gate 		}
22920Sstevel@tonic-gate 		if (un->un_flags & FDUNIT_CHANGED)
22930Sstevel@tonic-gate 			cpy.temp |= FDGC_HISTORY;
22940Sstevel@tonic-gate 		else
22950Sstevel@tonic-gate 			cpy.temp &= ~FDGC_HISTORY;
22960Sstevel@tonic-gate 		un->un_flags &= ~FDUNIT_CHANGED;
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate 		if (fd_pollable) {
22990Sstevel@tonic-gate 			/*
23000Sstevel@tonic-gate 			 * If it's a "pollable" floppy, then we don't
23010Sstevel@tonic-gate 			 * have to do all the fdcheckdisk nastyness to
23020Sstevel@tonic-gate 			 * figure out if the thing is still there.
23030Sstevel@tonic-gate 			 */
23040Sstevel@tonic-gate 			if (fdsense_chng(fdc, unit)) {
23050Sstevel@tonic-gate 				cpy.temp |= FDGC_CURRENT;
23060Sstevel@tonic-gate 			} else {
23070Sstevel@tonic-gate 				cpy.temp &= ~FDGC_CURRENT;
23080Sstevel@tonic-gate 			}
23090Sstevel@tonic-gate 		} else {
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate 			if (fdsense_chng(fdc, unit)) {
23120Sstevel@tonic-gate 				/*
23130Sstevel@tonic-gate 				 * check disk change signal is asserted.
23140Sstevel@tonic-gate 				 * Now find out if the floppy is
23150Sstevel@tonic-gate 				 * inserted
23160Sstevel@tonic-gate 				 */
23170Sstevel@tonic-gate 				if (fdcheckdisk(fdc, unit)) {
23180Sstevel@tonic-gate 					cpy.temp |= FDGC_CURRENT;
23190Sstevel@tonic-gate 				} else {
23200Sstevel@tonic-gate 					/*
23210Sstevel@tonic-gate 					 * Yes, the floppy was
23220Sstevel@tonic-gate 					 * reinserted. Implies
23230Sstevel@tonic-gate 					 * floppy change.
23240Sstevel@tonic-gate 					 */
23250Sstevel@tonic-gate 					cpy.temp &= ~FDGC_CURRENT;
23260Sstevel@tonic-gate 					cpy.temp |= FDGC_HISTORY;
23270Sstevel@tonic-gate 				}
23280Sstevel@tonic-gate 			} else {
23290Sstevel@tonic-gate 				cpy.temp &= ~FDGC_CURRENT;
23300Sstevel@tonic-gate 			}
23310Sstevel@tonic-gate 		}
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 		/*
23340Sstevel@tonic-gate 		 * For a pollable floppy, the floppy_change signal
23350Sstevel@tonic-gate 		 * reflects whether the floppy is in there or not.
23360Sstevel@tonic-gate 		 * We can not detect a floppy change if we don't poll
23370Sstevel@tonic-gate 		 * this signal when the floppy is being changed.
23380Sstevel@tonic-gate 		 * Because as soon as the floppy is put back, the
23390Sstevel@tonic-gate 		 * signal is reset.
23400Sstevel@tonic-gate 		 * BUT the pollable floppies are available only on
23410Sstevel@tonic-gate 		 * Sparcstation Voyager Voyagers (Gypsy) only and
23420Sstevel@tonic-gate 		 * those are motorized floppies. For motorized floppies,
23430Sstevel@tonic-gate 		 * the floppy can only (assuming the user doesn't use a
23440Sstevel@tonic-gate 		 * pin to take out the floppy) be taken out by
23450Sstevel@tonic-gate 		 * issuing 'eject' command which sets the
23460Sstevel@tonic-gate 		 * un->un_ejected flag. So, if the following
23470Sstevel@tonic-gate 		 * condition is true, we can assume there
23480Sstevel@tonic-gate 		 * was a floppy change.
23490Sstevel@tonic-gate 		 */
23500Sstevel@tonic-gate 		if (un->un_ejected && !(cpy.temp & FDGC_CURRENT)) {
23510Sstevel@tonic-gate 			cpy.temp |= FDGC_HISTORY;
23520Sstevel@tonic-gate 		}
23530Sstevel@tonic-gate 		un->un_ejected = 0;
23540Sstevel@tonic-gate 
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate 		/* return the write-protection status */
23570Sstevel@tonic-gate 		fdgetcsb(fdc);
23580Sstevel@tonic-gate 		if (fdsensedrv(fdc, unit) & WP_SR3) {
23590Sstevel@tonic-gate 			cpy.temp |= FDGC_CURWPROT;
23600Sstevel@tonic-gate 		}
23610Sstevel@tonic-gate 		fdretcsb(fdc);
23620Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
23630Sstevel@tonic-gate 
23640Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&cpy.temp, (caddr_t)arg,
2365*7656SSherry.Moore@Sun.COM 		    sizeof (int), flag))
23660Sstevel@tonic-gate 			err = EFAULT;
23670Sstevel@tonic-gate 		(void) pm_idle_component(fdc->c_dip, 0);
23680Sstevel@tonic-gate 		break;
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate 	case FDGETDRIVECHAR:
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&cpy.drvchar,
23730Sstevel@tonic-gate 				sizeof (struct fd_drive), flag)) {
23740Sstevel@tonic-gate 			err = EFAULT;
23750Sstevel@tonic-gate 			break;
23760Sstevel@tonic-gate 		}
23770Sstevel@tonic-gate 
23780Sstevel@tonic-gate 		/*
23790Sstevel@tonic-gate 		 * Return the ejectable value based on the FD_MANUAL_EJECT
23800Sstevel@tonic-gate 		 * property
23810Sstevel@tonic-gate 		 */
23820Sstevel@tonic-gate 		cpy.drvchar.fdd_ejectable = fdc->c_un->un_drive->fdd_ejectable;
23830Sstevel@tonic-gate 		cpy.drvchar.fdd_maxsearch = nfdtypes; /* 3 - hi m lo density */
23840Sstevel@tonic-gate 		if (fd_pollable)	/* pollable device */
23850Sstevel@tonic-gate 			cpy.drvchar.fdd_flags |= FDD_POLLABLE;
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate 		/* the rest of the fd_drive struct is meaningless to us */
23880Sstevel@tonic-gate 
23890Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&cpy.drvchar, (caddr_t)arg,
2390*7656SSherry.Moore@Sun.COM 		    sizeof (struct fd_drive), flag))
23910Sstevel@tonic-gate 			err = EFAULT;
23920Sstevel@tonic-gate 		break;
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 	case FDSETDRIVECHAR:
23950Sstevel@tonic-gate 		FDERRPRINT(FDEP_L3, FDEM_IOCT,
23960Sstevel@tonic-gate 		    (C, "fd_ioctl: FDSETDRIVECHAR not supportedn\n"));
23970Sstevel@tonic-gate 		err = ENOTTY;
23980Sstevel@tonic-gate 		break;
23990Sstevel@tonic-gate 
24000Sstevel@tonic-gate 	case DKIOCREMOVABLE: {
24010Sstevel@tonic-gate 		int	i = 1;
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate 		/* no brainer: floppies are always removable */
24040Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&i, (caddr_t)arg, sizeof (int),
24050Sstevel@tonic-gate 		    flag)) {
24060Sstevel@tonic-gate 			err = EFAULT;
24070Sstevel@tonic-gate 		}
24080Sstevel@tonic-gate 		break;
24090Sstevel@tonic-gate 	}
24100Sstevel@tonic-gate 	case DKIOCGMEDIAINFO:
24110Sstevel@tonic-gate 		err = fd_get_media_info(un, (caddr_t)arg, flag);
24120Sstevel@tonic-gate 		break;
24130Sstevel@tonic-gate 
24140Sstevel@tonic-gate 
24150Sstevel@tonic-gate 	case FDIOCMD:
24160Sstevel@tonic-gate 	{
24170Sstevel@tonic-gate 		struct fd_cmd fc;
24180Sstevel@tonic-gate 		int cyl, hd, spc, spt;
24190Sstevel@tonic-gate 		int nblks; /* total no. of blocks */
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
24220Sstevel@tonic-gate 		switch (ddi_model_convert_from(flag & FMODELS)) {
24230Sstevel@tonic-gate 		case DDI_MODEL_ILP32: {
24240Sstevel@tonic-gate 			struct fd_cmd32 fc32;
24250Sstevel@tonic-gate 
24260Sstevel@tonic-gate 			if (ddi_copyin((const void *)arg, &fc32,
24270Sstevel@tonic-gate 			    sizeof (fc32), flag)) {
24280Sstevel@tonic-gate 				return (EFAULT);
24290Sstevel@tonic-gate 			}
24300Sstevel@tonic-gate 			fc.fdc_cmd	= fc32.fdc_cmd;
24310Sstevel@tonic-gate 			fc.fdc_flags	= fc32.fdc_flags;
24320Sstevel@tonic-gate 			fc.fdc_blkno	= (daddr_t)fc32.fdc_blkno;
24330Sstevel@tonic-gate 			fc.fdc_secnt	= fc32.fdc_secnt;
2434483Spc157239 			fc.fdc_bufaddr	= (caddr_t)(uintptr_t)fc32.fdc_bufaddr;
24350Sstevel@tonic-gate 			fc.fdc_buflen	= fc32.fdc_buflen;
24360Sstevel@tonic-gate 			fc.fdc_cmd	= fc32.fdc_cmd;
24370Sstevel@tonic-gate 
24380Sstevel@tonic-gate 			break;
24390Sstevel@tonic-gate 		}
24400Sstevel@tonic-gate 
24410Sstevel@tonic-gate 		case DDI_MODEL_NONE:
24420Sstevel@tonic-gate 			if (ddi_copyin((const void *)arg, &fc,
24430Sstevel@tonic-gate 			    sizeof (fc), flag)) {
24440Sstevel@tonic-gate 				return (EFAULT);
24450Sstevel@tonic-gate 			}
24460Sstevel@tonic-gate 			break;
24470Sstevel@tonic-gate 		}
24480Sstevel@tonic-gate #else /* ! _MULTI_DATAMODEL */
24490Sstevel@tonic-gate 		if (ddi_copyin((const void *)arg, &fc, sizeof (fc), flag)) {
24500Sstevel@tonic-gate 			return (EFAULT);
24510Sstevel@tonic-gate 		}
24520Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
24530Sstevel@tonic-gate 
24540Sstevel@tonic-gate 		if (fc.fdc_cmd == FDCMD_READ || fc.fdc_cmd == FDCMD_WRITE) {
24550Sstevel@tonic-gate 			auto struct iovec aiov;
24560Sstevel@tonic-gate 			auto struct uio auio;
24570Sstevel@tonic-gate 			struct uio *uio = &auio;
24580Sstevel@tonic-gate 
24590Sstevel@tonic-gate 			spc = (fc.fdc_cmd == FDCMD_READ)? B_READ: B_WRITE;
24600Sstevel@tonic-gate 
24610Sstevel@tonic-gate 			bzero(&auio, sizeof (struct uio));
24620Sstevel@tonic-gate 			bzero(&aiov, sizeof (struct iovec));
24630Sstevel@tonic-gate 			aiov.iov_base = fc.fdc_bufaddr;
24640Sstevel@tonic-gate 			aiov.iov_len = (uint_t)fc.fdc_secnt * sec_size;
24650Sstevel@tonic-gate 			uio->uio_iov = &aiov;
24660Sstevel@tonic-gate 
24670Sstevel@tonic-gate 			uio->uio_iovcnt = 1;
24680Sstevel@tonic-gate 			uio->uio_resid = aiov.iov_len;
24690Sstevel@tonic-gate 			uio->uio_segflg = UIO_USERSPACE;
24700Sstevel@tonic-gate 			FDERRPRINT(FDEP_L2, FDEM_IOCT,
24710Sstevel@tonic-gate 			    (C, "fd_ioctl: call physio\n"));
24720Sstevel@tonic-gate 			err = physio(fd_strategy, NULL, dev,
24730Sstevel@tonic-gate 			    spc, minphys, uio);
24740Sstevel@tonic-gate 			break;
24750Sstevel@tonic-gate 		} else if (fc.fdc_cmd != FDCMD_FORMAT_TRACK) {
24760Sstevel@tonic-gate 
24770Sstevel@tonic-gate 			/*
24780Sstevel@tonic-gate 			 * The manpage states that only the FDCMD_WRITE,
24790Sstevel@tonic-gate 			 * FDCMD_READ, and the FDCMD_FORMAT_TR are available.
24800Sstevel@tonic-gate 			 */
24810Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_IOCT,
24820Sstevel@tonic-gate 			    (C, "fd_ioctl: FDIOCMD invalid command\n"));
24830Sstevel@tonic-gate 			err = EINVAL;
24840Sstevel@tonic-gate 			break;
24850Sstevel@tonic-gate 		}
24860Sstevel@tonic-gate 
24870Sstevel@tonic-gate 		/* The command is FDCMD_FORMAT_TRACK */
24880Sstevel@tonic-gate 
24890Sstevel@tonic-gate 		spt = un->un_chars->fdc_secptrack;	/* sec/trk */
24900Sstevel@tonic-gate 		spc = un->un_chars->fdc_nhead * spt;	/* sec/cyl */
24910Sstevel@tonic-gate 		cyl = fc.fdc_blkno / spc;
24920Sstevel@tonic-gate 		hd = (fc.fdc_blkno % spc) / spt;
24930Sstevel@tonic-gate 
24940Sstevel@tonic-gate 		/*
24950Sstevel@tonic-gate 		 * Make sure the specified block number is in the correct
24960Sstevel@tonic-gate 		 * range. (block numbers start at 0)
24970Sstevel@tonic-gate 		 */
24980Sstevel@tonic-gate 		nblks = spc * un->un_chars->fdc_ncyl;
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 		if (fc.fdc_blkno < 0 || fc.fdc_blkno > (nblks - 1)) {
25010Sstevel@tonic-gate 			err = EINVAL;
25020Sstevel@tonic-gate 			break;
25030Sstevel@tonic-gate 		}
25040Sstevel@tonic-gate 
25050Sstevel@tonic-gate 		(void) pm_busy_component(fdc->c_dip, 0);
25060Sstevel@tonic-gate 
25070Sstevel@tonic-gate 		mutex_enter(&fdc->c_lolock);
25080Sstevel@tonic-gate 		CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
25090Sstevel@tonic-gate 		if (fdc->c_un->un_state == FD_STATE_STOPPED) {
25100Sstevel@tonic-gate 			mutex_exit(&fdc->c_lolock);
25110Sstevel@tonic-gate 			if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
2512*7656SSherry.Moore@Sun.COM 			    != DDI_SUCCESS) {
25130Sstevel@tonic-gate 				FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power \
2514*7656SSherry.Moore@Sun.COM 				    change failed. \n"));
25150Sstevel@tonic-gate 				(void) pm_idle_component(fdc->c_dip, 0);
25160Sstevel@tonic-gate 				return (EIO);
25170Sstevel@tonic-gate 			}
25180Sstevel@tonic-gate 
25190Sstevel@tonic-gate 			mutex_enter(&fdc->c_lolock);
25200Sstevel@tonic-gate 		}
25210Sstevel@tonic-gate 
25220Sstevel@tonic-gate 		if (fdformat(fdc, unit, cyl, hd))
25230Sstevel@tonic-gate 			err = EIO;
25240Sstevel@tonic-gate 
25250Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
25260Sstevel@tonic-gate 		(void) pm_idle_component(fdc->c_dip, 0);
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 		break;
25290Sstevel@tonic-gate 	}
25300Sstevel@tonic-gate 
25310Sstevel@tonic-gate 	case FDRAW:
25320Sstevel@tonic-gate 
25330Sstevel@tonic-gate 		(void) pm_busy_component(fdc->c_dip, 0);
25340Sstevel@tonic-gate 		err = fdrawioctl(fdc, unit, arg, flag);
25350Sstevel@tonic-gate 
25360Sstevel@tonic-gate 		(void) pm_idle_component(fdc->c_dip, 0);
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate 		break;
25390Sstevel@tonic-gate #ifdef FD_DEBUG
25400Sstevel@tonic-gate 	case IOCTL_DEBUG:
25410Sstevel@tonic-gate 		fderrlevel--;
25420Sstevel@tonic-gate 		if (fderrlevel < 0)
25430Sstevel@tonic-gate 			fderrlevel = 3;
25440Sstevel@tonic-gate 		cmn_err(C, "fdioctl: CHANGING debug to %d", fderrlevel);
25450Sstevel@tonic-gate 		return (0);
25460Sstevel@tonic-gate #endif /* FD_DEBUG */
25470Sstevel@tonic-gate 	default:
25480Sstevel@tonic-gate 		FDERRPRINT(FDEP_L2, FDEM_IOCT,
25490Sstevel@tonic-gate 		    (C, "fd_ioctl: invalid ioctl 0x%x\n", cmd));
25500Sstevel@tonic-gate 		err = ENOTTY;
25510Sstevel@tonic-gate 		break;
25520Sstevel@tonic-gate 	}
25530Sstevel@tonic-gate 
25540Sstevel@tonic-gate 	return (err);
25550Sstevel@tonic-gate }
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate /*
25580Sstevel@tonic-gate  * fdrawioctl
25590Sstevel@tonic-gate  *
25600Sstevel@tonic-gate  * 	- acquires the low level lock
25610Sstevel@tonic-gate  */
25620Sstevel@tonic-gate 
25630Sstevel@tonic-gate static int
25640Sstevel@tonic-gate fdrawioctl(struct fdctlr *fdc, int unit, intptr_t arg, int mode)
25650Sstevel@tonic-gate {
25660Sstevel@tonic-gate 	struct fd_raw fdr;
25670Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
25680Sstevel@tonic-gate 	struct fd_raw32 fdr32;
25690Sstevel@tonic-gate #endif
25700Sstevel@tonic-gate 	struct fdcsb *csb;
25710Sstevel@tonic-gate 	int i, err, flag;
25720Sstevel@tonic-gate 	caddr_t fa;
25730Sstevel@tonic-gate 	uint_t	fc;
25740Sstevel@tonic-gate 	size_t	real_length;
25750Sstevel@tonic-gate 	int	res;
25760Sstevel@tonic-gate 	ddi_device_acc_attr_t attr;
25770Sstevel@tonic-gate 	ddi_acc_handle_t	mem_handle;
25780Sstevel@tonic-gate 
25790Sstevel@tonic-gate 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
25800Sstevel@tonic-gate 	attr.devacc_attr_endian_flags  = DDI_STRUCTURE_BE_ACC;
25810Sstevel@tonic-gate 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
25820Sstevel@tonic-gate 
25830Sstevel@tonic-gate 	ASSERT(fdc->c_un->un_unit_no == unit);
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_RAWI,
25860Sstevel@tonic-gate 	    (C, "fdrawioctl: cmd[0]=0x%x\n", fdr.fdr_cmd[0]));
25870Sstevel@tonic-gate 
25880Sstevel@tonic-gate 	flag = B_READ;
25890Sstevel@tonic-gate 	err = 0;
25900Sstevel@tonic-gate 	fa = NULL;
25910Sstevel@tonic-gate 	fc = (uint_t)0;
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate 	/* Copy in the arguments */
25940Sstevel@tonic-gate 	switch (ddi_model_convert_from(mode)) {
25950Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
25960Sstevel@tonic-gate 	case DDI_MODEL_ILP32:
25970Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&fdr32,
25980Sstevel@tonic-gate 		    sizeof (fdr32), mode)) {
25990Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2600*7656SSherry.Moore@Sun.COM 			    (C, "fdrawioctl: copyin error, args32\n"));
26010Sstevel@tonic-gate 			return (EFAULT);
26020Sstevel@tonic-gate 		}
26030Sstevel@tonic-gate 		bcopy(fdr32.fdr_cmd, fdr.fdr_cmd, sizeof (fdr.fdr_cmd));
26040Sstevel@tonic-gate 		fdr.fdr_cnum = fdr32.fdr_cnum;
26050Sstevel@tonic-gate 		bcopy(fdr32.fdr_result, fdr.fdr_result,
26060Sstevel@tonic-gate 		    sizeof (fdr.fdr_result));
26070Sstevel@tonic-gate 		fdr.fdr_nbytes = fdr32.fdr_nbytes;
2608483Spc157239 		fdr.fdr_addr = (caddr_t)(uintptr_t)fdr32.fdr_addr;
26090Sstevel@tonic-gate 		break;
26100Sstevel@tonic-gate #endif
26110Sstevel@tonic-gate 	default:
26120Sstevel@tonic-gate 	case DDI_MODEL_NONE:
26130Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&fdr,
26140Sstevel@tonic-gate 		    sizeof (fdr), mode)) {
26150Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2616*7656SSherry.Moore@Sun.COM 			    (C, "fdrawioctl: copyin error, args\n"));
26170Sstevel@tonic-gate 			return (EFAULT);
26180Sstevel@tonic-gate 		}
26190Sstevel@tonic-gate 		break;
26200Sstevel@tonic-gate 	}
26210Sstevel@tonic-gate 
26220Sstevel@tonic-gate 	mutex_enter(&fdc->c_lolock);
26230Sstevel@tonic-gate 
26240Sstevel@tonic-gate 	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
26250Sstevel@tonic-gate 
26260Sstevel@tonic-gate 	if (fdc->c_un->un_state == FD_STATE_STOPPED) {
26270Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
26280Sstevel@tonic-gate 		if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
2629*7656SSherry.Moore@Sun.COM 		    != DDI_SUCCESS) {
26300Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power change \
2631*7656SSherry.Moore@Sun.COM 			    failed. \n"));
26320Sstevel@tonic-gate 
26330Sstevel@tonic-gate 			(void) pm_idle_component(fdc->c_dip, 0);
26340Sstevel@tonic-gate 			return (EIO);
26350Sstevel@tonic-gate 		}
26360Sstevel@tonic-gate 		mutex_enter(&fdc->c_lolock);
26370Sstevel@tonic-gate 	}
26380Sstevel@tonic-gate 
26390Sstevel@tonic-gate 	fdgetcsb(fdc);
26400Sstevel@tonic-gate 	csb = &fdc->c_csb;
26410Sstevel@tonic-gate 	csb->csb_unit = (uchar_t)unit;
26420Sstevel@tonic-gate 
26430Sstevel@tonic-gate 	/* copy cmd bytes into csb */
26440Sstevel@tonic-gate 	for (i = 0; i <= fdr.fdr_cnum; i++)
26450Sstevel@tonic-gate 		csb->csb_cmds[i] = fdr.fdr_cmd[i];
26460Sstevel@tonic-gate 	csb->csb_ncmds = (uchar_t)fdr.fdr_cnum;
26470Sstevel@tonic-gate 
26480Sstevel@tonic-gate 	csb->csb_maxretry = 0;	/* let the application deal with errors */
26490Sstevel@tonic-gate 	csb->csb_retrys = 0;
26500Sstevel@tonic-gate 
26510Sstevel@tonic-gate 	switch (fdr.fdr_cmd[0] & 0x0f) {
26520Sstevel@tonic-gate 
26530Sstevel@tonic-gate 	case FDRAW_SPECIFY:
26540Sstevel@tonic-gate 		/*
26550Sstevel@tonic-gate 		 * Ensure that the right DMA mode is selected.  There is
26560Sstevel@tonic-gate 		 * currently no way for the user to tell if DMA is
26570Sstevel@tonic-gate 		 * happening so set the value for the user.
26580Sstevel@tonic-gate 		 */
26590Sstevel@tonic-gate 
26600Sstevel@tonic-gate 		if (fdc->c_fdtype & FDCTYPE_DMA)
26610Sstevel@tonic-gate 			csb->csb_cmds[2] = csb->csb_cmds[2] & 0xFE;
26620Sstevel@tonic-gate 		else
26630Sstevel@tonic-gate 			csb->csb_cmds[2] = csb->csb_cmds[2] | 0x1;
26640Sstevel@tonic-gate 
26650Sstevel@tonic-gate 		csb->csb_opflags = CSB_OFNORESULTS;
26660Sstevel@tonic-gate 		csb->csb_nrslts = 0;
26670Sstevel@tonic-gate 		break;
26680Sstevel@tonic-gate 
26690Sstevel@tonic-gate 	case FDRAW_SENSE_DRV:
26700Sstevel@tonic-gate 		/* Insert the appropriate drive number */
26710Sstevel@tonic-gate 		csb->csb_cmds[1] = csb->csb_cmds[1] | (unit & DRV_MASK);
26720Sstevel@tonic-gate 		csb->csb_opflags = CSB_OFIMMEDIATE;
26730Sstevel@tonic-gate 		csb->csb_nrslts = 1;
26740Sstevel@tonic-gate 		break;
26750Sstevel@tonic-gate 
26760Sstevel@tonic-gate 	case FDRAW_REZERO:
26770Sstevel@tonic-gate 	case FDRAW_SEEK:
26780Sstevel@tonic-gate 		/* Insert the appropriate drive number */
26790Sstevel@tonic-gate 		csb->csb_cmds[1] = csb->csb_cmds[1] | (unit & DRV_MASK);
26800Sstevel@tonic-gate 		csb->csb_opflags = CSB_OFSEEKOPS + CSB_OFTIMEIT;
26810Sstevel@tonic-gate 		csb->csb_nrslts = 2;
26820Sstevel@tonic-gate 		break;
26830Sstevel@tonic-gate 
26840Sstevel@tonic-gate 	case FDRAW_FORMAT:
26850Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_RAWI,
2686*7656SSherry.Moore@Sun.COM 		    (C, "fdrawioctl: cmd is fdfraw format\n"));
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate 		/* Insert the appropriate drive number */
26890Sstevel@tonic-gate 		csb->csb_cmds[1] = csb->csb_cmds[1] | (unit & DRV_MASK);
26900Sstevel@tonic-gate 		csb->csb_opflags = CSB_OFXFEROPS + CSB_OFTIMEIT;
26910Sstevel@tonic-gate 		csb->csb_nrslts = NRBRW;
26920Sstevel@tonic-gate 		flag = B_WRITE;
26930Sstevel@tonic-gate 
26940Sstevel@tonic-gate 		/*
26950Sstevel@tonic-gate 		 * Allocate memory for the command.
26960Sstevel@tonic-gate 		 * If PIO is being used, then add an extra 16 bytes
26970Sstevel@tonic-gate 		 */
26980Sstevel@tonic-gate 		if (fdc->c_fdtype & FDCTYPE_DMA) {
26990Sstevel@tonic-gate 
27000Sstevel@tonic-gate 			fc = (uint_t)(fdr.fdr_nbytes);
27010Sstevel@tonic-gate 			mutex_enter(&fdc->c_hilock);
27020Sstevel@tonic-gate 
27030Sstevel@tonic-gate 			res = ddi_dma_mem_alloc(fdc->c_dmahandle, fc,
2704*7656SSherry.Moore@Sun.COM 			    &attr, DDI_DMA_STREAMING,
2705*7656SSherry.Moore@Sun.COM 			    DDI_DMA_DONTWAIT, 0, &fa, &real_length,
2706*7656SSherry.Moore@Sun.COM 			    &mem_handle);
27070Sstevel@tonic-gate 
27080Sstevel@tonic-gate 			if (res != DDI_SUCCESS) {
27090Sstevel@tonic-gate 				fdretcsb(fdc);
27100Sstevel@tonic-gate 				mutex_exit(&fdc->c_lolock);
27110Sstevel@tonic-gate 				mutex_exit(&fdc->c_hilock);
27120Sstevel@tonic-gate 				return (EIO);
27130Sstevel@tonic-gate 			}
27140Sstevel@tonic-gate 
27150Sstevel@tonic-gate 			fdc->c_csb.csb_read = CSB_WRITE;
27160Sstevel@tonic-gate 			if (fdstart_dma(fdc, fa, fc) != 0) {
27170Sstevel@tonic-gate 				ddi_dma_mem_free(&mem_handle);
27180Sstevel@tonic-gate 				fdretcsb(fdc);
27190Sstevel@tonic-gate 				mutex_exit(&fdc->c_lolock);
27200Sstevel@tonic-gate 				mutex_exit(&fdc->c_hilock);
27210Sstevel@tonic-gate 				return (EIO);
27220Sstevel@tonic-gate 			}
27230Sstevel@tonic-gate 			mutex_exit(&fdc->c_hilock);
27240Sstevel@tonic-gate 
27250Sstevel@tonic-gate 		} else {
27260Sstevel@tonic-gate 			fc = (uint_t)(fdr.fdr_nbytes + 16);
27270Sstevel@tonic-gate 			fa = kmem_zalloc(fc, KM_SLEEP);
27280Sstevel@tonic-gate 		}
27290Sstevel@tonic-gate 
27300Sstevel@tonic-gate 		/* copy in the user's command bytes */
27310Sstevel@tonic-gate 		if (ddi_copyin(fdr.fdr_addr, fa,
2732*7656SSherry.Moore@Sun.COM 		    (uint_t)fdr.fdr_nbytes, mode)) {
27330Sstevel@tonic-gate 			fdretcsb(fdc);
27340Sstevel@tonic-gate 			mutex_exit(&fdc->c_lolock);
27350Sstevel@tonic-gate 
27360Sstevel@tonic-gate 			if (fdc->c_fdtype & FDCTYPE_DMA) {
27370Sstevel@tonic-gate 				ddi_dma_mem_free(&mem_handle);
27380Sstevel@tonic-gate 				FDERRPRINT(FDEP_L1, FDEM_RAWI,
2739*7656SSherry.Moore@Sun.COM 				    (C, "fdrawioctl: (err)free dma memory\n"));
27400Sstevel@tonic-gate 			} else {
27410Sstevel@tonic-gate 				kmem_free(fa, fc);
27420Sstevel@tonic-gate 			}
27430Sstevel@tonic-gate 
27440Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2745*7656SSherry.Moore@Sun.COM 			    (C, "fdrawioctl: ddi_copyin error\n"));
27460Sstevel@tonic-gate 			return (EFAULT);
27470Sstevel@tonic-gate 		}
27480Sstevel@tonic-gate 
27490Sstevel@tonic-gate 		break;
27500Sstevel@tonic-gate 	case FDRAW_WRCMD:
27510Sstevel@tonic-gate 	case FDRAW_WRITEDEL:
27520Sstevel@tonic-gate 		flag = B_WRITE;
27530Sstevel@tonic-gate 		/* FALLTHROUGH */
27540Sstevel@tonic-gate 	case FDRAW_RDCMD:
27550Sstevel@tonic-gate 	case FDRAW_READDEL:
27560Sstevel@tonic-gate 	case FDRAW_READTRACK:
27570Sstevel@tonic-gate 		/* Insert the appropriate drive number */
27580Sstevel@tonic-gate 		csb->csb_cmds[1] = csb->csb_cmds[1] | (unit & DRV_MASK);
27590Sstevel@tonic-gate 		if (fdc->c_fdtype & FDCTYPE_SB)
27600Sstevel@tonic-gate 			csb->csb_cmds[1] |= IPS;
27610Sstevel@tonic-gate 		csb->csb_opflags = CSB_OFXFEROPS + CSB_OFTIMEIT;
27620Sstevel@tonic-gate 		csb->csb_nrslts = NRBRW;
27630Sstevel@tonic-gate 		break;
27640Sstevel@tonic-gate 
27650Sstevel@tonic-gate 	default:
27660Sstevel@tonic-gate 		fdretcsb(fdc);
27670Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
27680Sstevel@tonic-gate 		return (EINVAL);
27690Sstevel@tonic-gate 	}
27700Sstevel@tonic-gate 
27710Sstevel@tonic-gate 	if ((csb->csb_opflags & CSB_OFXFEROPS) && (fdr.fdr_nbytes == 0)) {
27720Sstevel@tonic-gate 		fdretcsb(fdc);
27730Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
27740Sstevel@tonic-gate 		return (EINVAL);
27750Sstevel@tonic-gate 	}
27760Sstevel@tonic-gate 	csb->csb_opflags |= CSB_OFRAWIOCTL;
27770Sstevel@tonic-gate 
27780Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_RAWI,
2779*7656SSherry.Moore@Sun.COM 	    (C, "fdrawioctl: nbytes = %u\n", fdr.fdr_nbytes));
27800Sstevel@tonic-gate 
27810Sstevel@tonic-gate 	if ((fdr.fdr_cmd[0] & 0x0f) != FDRAW_FORMAT) {
27820Sstevel@tonic-gate 		if ((fc = (uint_t)fdr.fdr_nbytes) > 0) {
27830Sstevel@tonic-gate 			/*
27840Sstevel@tonic-gate 			 * In SunOS 4.X, we used to as_fault things in.
27850Sstevel@tonic-gate 			 * We really cannot do this in 5.0/SVr4. Unless
27860Sstevel@tonic-gate 			 * someone really believes that speed is of the
27870Sstevel@tonic-gate 			 * essence here, it is just much simpler to do
27880Sstevel@tonic-gate 			 * this in kernel space and use copyin/copyout.
27890Sstevel@tonic-gate 			 */
27900Sstevel@tonic-gate 			if (fdc->c_fdtype & FDCTYPE_DMA) {
27910Sstevel@tonic-gate 				mutex_enter(&fdc->c_hilock);
27920Sstevel@tonic-gate 				res = ddi_dma_mem_alloc(fdc->c_dmahandle, fc,
2793*7656SSherry.Moore@Sun.COM 				    &attr, DDI_DMA_STREAMING,
2794*7656SSherry.Moore@Sun.COM 				    DDI_DMA_DONTWAIT, 0, &fa, &real_length,
2795*7656SSherry.Moore@Sun.COM 				    &mem_handle);
27960Sstevel@tonic-gate 
27970Sstevel@tonic-gate 				if (res != DDI_SUCCESS) {
27980Sstevel@tonic-gate 					fdretcsb(fdc);
27990Sstevel@tonic-gate 					mutex_exit(&fdc->c_lolock);
28000Sstevel@tonic-gate 					mutex_exit(&fdc->c_hilock);
28010Sstevel@tonic-gate 					return (EIO);
28020Sstevel@tonic-gate 				}
28030Sstevel@tonic-gate 
28040Sstevel@tonic-gate 				if (flag == B_WRITE)
28050Sstevel@tonic-gate 					fdc->c_csb.csb_read = CSB_WRITE;
28060Sstevel@tonic-gate 				else
28070Sstevel@tonic-gate 					fdc->c_csb.csb_read = CSB_READ;
28080Sstevel@tonic-gate 
28090Sstevel@tonic-gate 				if (fdstart_dma(fdc, fa, fc) != 0) {
28100Sstevel@tonic-gate 					ddi_dma_mem_free(&mem_handle);
28110Sstevel@tonic-gate 					fdretcsb(fdc);
28120Sstevel@tonic-gate 					mutex_exit(&fdc->c_lolock);
28130Sstevel@tonic-gate 					mutex_exit(&fdc->c_hilock);
28140Sstevel@tonic-gate 					return (EIO);
28150Sstevel@tonic-gate 				}
28160Sstevel@tonic-gate 				mutex_exit(&fdc->c_hilock);
28170Sstevel@tonic-gate 
28180Sstevel@tonic-gate 			} else {
28190Sstevel@tonic-gate 				fa = kmem_zalloc(fc, KM_SLEEP);
28200Sstevel@tonic-gate 			}
28210Sstevel@tonic-gate 
28220Sstevel@tonic-gate 			if (flag == B_WRITE) {
28230Sstevel@tonic-gate 				if (ddi_copyin(fdr.fdr_addr, fa, fc, mode)) {
28240Sstevel@tonic-gate 					if (fdc->c_fdtype & FDCTYPE_DMA)
28250Sstevel@tonic-gate 						ddi_dma_mem_free(&mem_handle);
28260Sstevel@tonic-gate 					else
28270Sstevel@tonic-gate 						kmem_free(fa, fc);
28280Sstevel@tonic-gate 					fdretcsb(fdc);
28290Sstevel@tonic-gate 					mutex_exit(&fdc->c_lolock);
2830*7656SSherry.Moore@Sun.COM 					FDERRPRINT(FDEP_L1, FDEM_RAWI, (C,
2831*7656SSherry.Moore@Sun.COM 					    "fdrawioctl: can't copy data\n"));
28320Sstevel@tonic-gate 
28330Sstevel@tonic-gate 					return (EFAULT);
28340Sstevel@tonic-gate 				}
28350Sstevel@tonic-gate 			}
28360Sstevel@tonic-gate 			csb->csb_addr = fa;
28370Sstevel@tonic-gate 			csb->csb_len = fc;
28380Sstevel@tonic-gate 		} else {
28390Sstevel@tonic-gate 			csb->csb_addr = 0;
28400Sstevel@tonic-gate 			csb->csb_len = 0;
28410Sstevel@tonic-gate 		}
28420Sstevel@tonic-gate 	} else {
28430Sstevel@tonic-gate 		csb->csb_addr = fa;
28440Sstevel@tonic-gate 		csb->csb_len = fc;
28450Sstevel@tonic-gate 	}
28460Sstevel@tonic-gate 
28470Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_RAWI,
28480Sstevel@tonic-gate 	    (C, "cmd: %x %x %x %x %x %x %x %x %x %x\n", csb->csb_cmds[0],
28490Sstevel@tonic-gate 	    csb->csb_cmds[1], csb->csb_cmds[2], csb->csb_cmds[3],
28500Sstevel@tonic-gate 	    csb->csb_cmds[4], csb->csb_cmds[5], csb->csb_cmds[6],
28510Sstevel@tonic-gate 	    csb->csb_cmds[7], csb->csb_cmds[8], csb->csb_cmds[9]));
28520Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_RAWI,
28530Sstevel@tonic-gate 	    (C, "nbytes: %x, opflags: %x, addr: %p, len: %x\n",
28540Sstevel@tonic-gate 	    csb->csb_ncmds, csb->csb_opflags, (void *)csb->csb_addr,
28550Sstevel@tonic-gate 	    csb->csb_len));
28560Sstevel@tonic-gate 
28570Sstevel@tonic-gate 
28580Sstevel@tonic-gate 	/*
28590Sstevel@tonic-gate 	 * Note that we ignore any error return s from fdexec.
28600Sstevel@tonic-gate 	 * This is the way the driver has been, and it may be
28610Sstevel@tonic-gate 	 * that the raw ioctl senders simply don't want to
28620Sstevel@tonic-gate 	 * see any errors returned in this fashion.
28630Sstevel@tonic-gate 	 */
28640Sstevel@tonic-gate 
28650Sstevel@tonic-gate 	if ((csb->csb_opflags & CSB_OFNORESULTS) ||
28660Sstevel@tonic-gate 	    (csb->csb_opflags & CSB_OFIMMEDIATE)) {
28670Sstevel@tonic-gate 		(void) fdexec(fdc, 0); /* don't sleep, don't check change */
28680Sstevel@tonic-gate 	} else {
28690Sstevel@tonic-gate 		(void) fdexec(fdc, FDXC_SLEEP | FDXC_CHECKCHG);
28700Sstevel@tonic-gate 	}
28710Sstevel@tonic-gate 
28720Sstevel@tonic-gate 
28730Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_RAWI,
28740Sstevel@tonic-gate 	    (C, "rslt: %x %x %x %x %x %x %x %x %x %x\n", csb->csb_rslt[0],
28750Sstevel@tonic-gate 	    csb->csb_rslt[1], csb->csb_rslt[2], csb->csb_rslt[3],
28760Sstevel@tonic-gate 	    csb->csb_rslt[4], csb->csb_rslt[5], csb->csb_rslt[6],
28770Sstevel@tonic-gate 	    csb->csb_rslt[7], csb->csb_rslt[8], csb->csb_rslt[9]));
28780Sstevel@tonic-gate 
28790Sstevel@tonic-gate 	if ((fdr.fdr_cmd[0] & 0x0f) != FDRAW_FORMAT && fc &&
28800Sstevel@tonic-gate 	    flag == B_READ && err == 0) {
28810Sstevel@tonic-gate 		if (ddi_copyout(fa, fdr.fdr_addr, fc, mode)) {
28820Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2883*7656SSherry.Moore@Sun.COM 			    (C, "fdrawioctl: can't copy read data\n"));
28840Sstevel@tonic-gate 
28850Sstevel@tonic-gate 			err = EFAULT;
28860Sstevel@tonic-gate 		}
28870Sstevel@tonic-gate 	}
28880Sstevel@tonic-gate 
28890Sstevel@tonic-gate 
28900Sstevel@tonic-gate 	if (fc) {
28910Sstevel@tonic-gate 		if (fdc->c_fdtype & FDCTYPE_DMA) {
28920Sstevel@tonic-gate 			ddi_dma_mem_free(&mem_handle);
28930Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2894*7656SSherry.Moore@Sun.COM 			    (C, "fdrawioctl: free dma memory\n"));
28950Sstevel@tonic-gate 		} else {
28960Sstevel@tonic-gate 			kmem_free(fa, fc);
28970Sstevel@tonic-gate 		}
28980Sstevel@tonic-gate 	}
28990Sstevel@tonic-gate 
29000Sstevel@tonic-gate 
29010Sstevel@tonic-gate 	/* copy cmd results into fdr */
29020Sstevel@tonic-gate 	for (i = 0; (int)i <= (int)csb->csb_nrslts; i++)
29030Sstevel@tonic-gate 		fdr.fdr_result[i] = csb->csb_rslt[i];
29040Sstevel@tonic-gate 	fdr.fdr_nbytes = fdc->c_csb.csb_rlen; /* return resid */
29050Sstevel@tonic-gate 
29060Sstevel@tonic-gate 	switch (ddi_model_convert_from(mode)) {
29070Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
29080Sstevel@tonic-gate 	case DDI_MODEL_ILP32:
29090Sstevel@tonic-gate 		bcopy(fdr.fdr_cmd, fdr32.fdr_cmd, sizeof (fdr32.fdr_cmd));
29100Sstevel@tonic-gate 		fdr32.fdr_cnum = fdr.fdr_cnum;
29110Sstevel@tonic-gate 		bcopy(fdr.fdr_result, fdr32.fdr_result,
29120Sstevel@tonic-gate 		    sizeof (fdr32.fdr_result));
29130Sstevel@tonic-gate 		fdr32.fdr_nbytes = fdr.fdr_nbytes;
2914483Spc157239 		fdr32.fdr_addr = (caddr32_t)(uintptr_t)fdr.fdr_addr;
29150Sstevel@tonic-gate 		if (ddi_copyout(&fdr32, (caddr_t)arg, sizeof (fdr32), mode)) {
29160Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2917*7656SSherry.Moore@Sun.COM 			    (C, "fdrawioctl: can't copy results32\n"));
29180Sstevel@tonic-gate 			err = EFAULT;
29190Sstevel@tonic-gate 		}
29200Sstevel@tonic-gate 		break;
29210Sstevel@tonic-gate #endif
29220Sstevel@tonic-gate 	case DDI_MODEL_NONE:
29230Sstevel@tonic-gate 	default:
29240Sstevel@tonic-gate 		if (ddi_copyout(&fdr, (caddr_t)arg, sizeof (fdr), mode)) {
29250Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2926*7656SSherry.Moore@Sun.COM 			    (C, "fdrawioctl: can't copy results\n"));
29270Sstevel@tonic-gate 			err = EFAULT;
29280Sstevel@tonic-gate 		}
29290Sstevel@tonic-gate 		break;
29300Sstevel@tonic-gate 	}
29310Sstevel@tonic-gate 
29320Sstevel@tonic-gate 	fdretcsb(fdc);
29330Sstevel@tonic-gate 	mutex_exit(&fdc->c_lolock);
29340Sstevel@tonic-gate 	return (0);
29350Sstevel@tonic-gate }
29360Sstevel@tonic-gate 
29370Sstevel@tonic-gate /*
29380Sstevel@tonic-gate  * fdformat
29390Sstevel@tonic-gate  *	format a track
29400Sstevel@tonic-gate  * For PIO, builds a table of sector data values with 16 bytes
29410Sstevel@tonic-gate  * (sizeof fdc's fifo) of dummy on end.	 This is so than when fdc->c_len
29420Sstevel@tonic-gate  * goes to 0 and fd_intr sends a TC that all the real formatting will
29430Sstevel@tonic-gate  * have already been done.
29440Sstevel@tonic-gate  *
29450Sstevel@tonic-gate  *	- called with the low level lock held
29460Sstevel@tonic-gate  */
29470Sstevel@tonic-gate static int
29480Sstevel@tonic-gate fdformat(struct fdctlr *fdc, int unit, int cyl, int hd)
29490Sstevel@tonic-gate {
29500Sstevel@tonic-gate 	struct fdcsb *csb;
29510Sstevel@tonic-gate 	struct fdunit *un;
29520Sstevel@tonic-gate 	struct fd_char *ch;
29530Sstevel@tonic-gate 	int	cmdresult;
29540Sstevel@tonic-gate 	uchar_t	*fmthdrs;
29550Sstevel@tonic-gate 	caddr_t fd;
29560Sstevel@tonic-gate 	int	i;
29570Sstevel@tonic-gate 	size_t	real_length;
29580Sstevel@tonic-gate 	ddi_device_acc_attr_t attr;
29590Sstevel@tonic-gate 	ddi_acc_handle_t mem_handle;
29600Sstevel@tonic-gate 
29610Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_FORM,
29620Sstevel@tonic-gate 	    (C, "fdformat cyl %d, hd %d\n", cyl, hd));
29630Sstevel@tonic-gate 	fdgetcsb(fdc);
29640Sstevel@tonic-gate 
29650Sstevel@tonic-gate 	ASSERT(fdc->c_un->un_unit_no == unit);
29660Sstevel@tonic-gate 
29670Sstevel@tonic-gate 	csb = &fdc->c_csb;
29680Sstevel@tonic-gate 	un = fdc->c_un;
29690Sstevel@tonic-gate 	ch = un->un_chars;
29700Sstevel@tonic-gate 
29710Sstevel@tonic-gate 	/* setup common things in csb */
29720Sstevel@tonic-gate 	csb->csb_unit = (uchar_t)unit;
29730Sstevel@tonic-gate 
29740Sstevel@tonic-gate 	/*
29750Sstevel@tonic-gate 	 * The controller needs to do a seek before
29760Sstevel@tonic-gate 	 * each format to get to right cylinder.
29770Sstevel@tonic-gate 	 */
29780Sstevel@tonic-gate 	if (fdrecalseek(fdc, unit, cyl, FDXC_CHECKCHG)) {
29790Sstevel@tonic-gate 		fdretcsb(fdc);
29800Sstevel@tonic-gate 		return (EIO);
29810Sstevel@tonic-gate 	}
29820Sstevel@tonic-gate 
29830Sstevel@tonic-gate 	/*
29840Sstevel@tonic-gate 	 * now do the format itself
29850Sstevel@tonic-gate 	 */
29860Sstevel@tonic-gate 	csb->csb_nrslts = NRBRW;
29870Sstevel@tonic-gate 	csb->csb_opflags = CSB_OFXFEROPS | CSB_OFTIMEIT;
29880Sstevel@tonic-gate 
29890Sstevel@tonic-gate 	csb->csb_cmds[0] = FDRAW_FORMAT;
29900Sstevel@tonic-gate 	/* always or in MFM bit */
29910Sstevel@tonic-gate 	csb->csb_cmds[0] |= MFM;
29920Sstevel@tonic-gate 	csb->csb_cmds[1] = (hd << 2) | (unit & 0x03);
29930Sstevel@tonic-gate 	csb->csb_cmds[2] = ch->fdc_medium ? 3 : 2;
29940Sstevel@tonic-gate 	csb->csb_cmds[3] = ch->fdc_secptrack;
29950Sstevel@tonic-gate 	csb->csb_cmds[4] = GPLF;
29960Sstevel@tonic-gate 	csb->csb_cmds[5] = FDATA;
29970Sstevel@tonic-gate 	csb->csb_ncmds = 6;
29980Sstevel@tonic-gate 	csb->csb_maxretry = rwretry;
29990Sstevel@tonic-gate 	csb->csb_retrys = 0;
30000Sstevel@tonic-gate 
30010Sstevel@tonic-gate 	/*
30020Sstevel@tonic-gate 	 * NOTE: have to add size of fifo also - for dummy format action
30030Sstevel@tonic-gate 	 * if PIO is being used.
30040Sstevel@tonic-gate 	 */
30050Sstevel@tonic-gate 
30060Sstevel@tonic-gate 
30070Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_DMA) {
30080Sstevel@tonic-gate 
30090Sstevel@tonic-gate 		csb->csb_len = (uint_t)4 * ch->fdc_secptrack;
30100Sstevel@tonic-gate 
30110Sstevel@tonic-gate 		attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
30120Sstevel@tonic-gate 		attr.devacc_attr_endian_flags  = DDI_STRUCTURE_BE_ACC;
30130Sstevel@tonic-gate 		attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate 		mutex_enter(&fdc->c_hilock);
30160Sstevel@tonic-gate 
30170Sstevel@tonic-gate 		cmdresult = ddi_dma_mem_alloc(fdc->c_dmahandle, csb->csb_len,
3018*7656SSherry.Moore@Sun.COM 		    &attr, DDI_DMA_STREAMING,
3019*7656SSherry.Moore@Sun.COM 		    DDI_DMA_DONTWAIT, 0, &fd, &real_length,
3020*7656SSherry.Moore@Sun.COM 		    &mem_handle);
30210Sstevel@tonic-gate 
30220Sstevel@tonic-gate 		if (cmdresult != DDI_SUCCESS) {
30230Sstevel@tonic-gate 			mutex_exit(&fdc->c_hilock);
30240Sstevel@tonic-gate 			return (cmdresult);
30250Sstevel@tonic-gate 		}
30260Sstevel@tonic-gate 
30270Sstevel@tonic-gate 		fdc->c_csb.csb_read = CSB_WRITE;
30280Sstevel@tonic-gate 		if (fdstart_dma(fdc, fd,  csb->csb_len) != 0) {
30290Sstevel@tonic-gate 			ddi_dma_mem_free(&mem_handle);
30300Sstevel@tonic-gate 			mutex_exit(&fdc->c_hilock);
30310Sstevel@tonic-gate 			return (-1);
30320Sstevel@tonic-gate 		}
30330Sstevel@tonic-gate 		mutex_exit(&fdc->c_hilock);
30340Sstevel@tonic-gate 
30350Sstevel@tonic-gate 
30360Sstevel@tonic-gate 	} else {
30370Sstevel@tonic-gate 		csb->csb_len = (uint_t)4 * ch->fdc_secptrack + 16;
30380Sstevel@tonic-gate 		fd = kmem_zalloc(csb->csb_len, KM_SLEEP);
30390Sstevel@tonic-gate 		fmthdrs = (uchar_t *)fd;
30400Sstevel@tonic-gate 	}
30410Sstevel@tonic-gate 
30420Sstevel@tonic-gate 	csb->csb_addr = (caddr_t)fd;
30430Sstevel@tonic-gate 
30440Sstevel@tonic-gate 	for (i = 1; i <= ch->fdc_secptrack; i++) {
30450Sstevel@tonic-gate 		*fd++ = (uchar_t)cyl;		/* cylinder */
30460Sstevel@tonic-gate 		*fd++ = (uchar_t)hd;		/* head */
30470Sstevel@tonic-gate 		*fd++ = (uchar_t)i;	/* sector number */
30480Sstevel@tonic-gate 		*fd++ = ch->fdc_medium ? 3 : 2; /* sec_size code */
30490Sstevel@tonic-gate 	}
30500Sstevel@tonic-gate 
30510Sstevel@tonic-gate 	if ((cmdresult = fdexec(fdc, FDXC_SLEEP | FDXC_CHECKCHG)) == 0) {
30520Sstevel@tonic-gate 		if (csb->csb_cmdstat)
30530Sstevel@tonic-gate 			cmdresult = EIO;	/* XXX TBD NYD for now */
30540Sstevel@tonic-gate 	}
30550Sstevel@tonic-gate 
30560Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_DMA) {
30570Sstevel@tonic-gate 		ddi_dma_mem_free(&mem_handle);
30580Sstevel@tonic-gate 	} else {
30590Sstevel@tonic-gate 		kmem_free((caddr_t)fmthdrs, csb->csb_len);
30600Sstevel@tonic-gate 	}
30610Sstevel@tonic-gate 
30620Sstevel@tonic-gate 	fdretcsb(fdc);
30630Sstevel@tonic-gate 
30640Sstevel@tonic-gate 	return (cmdresult);
30650Sstevel@tonic-gate }
30660Sstevel@tonic-gate 
30670Sstevel@tonic-gate /*
30680Sstevel@tonic-gate  * fdstart
30690Sstevel@tonic-gate  *	called from fd_strategy() or from fdXXXX() to setup and
30700Sstevel@tonic-gate  *	start operations of read or write only (using buf structs).
30710Sstevel@tonic-gate  *	Because the chip doesn't handle crossing cylinder boundaries on
30720Sstevel@tonic-gate  *	the fly, this takes care of those boundary conditions.	Note that
30730Sstevel@tonic-gate  *	it sleeps until the operation is done *within fdstart* - so that
30740Sstevel@tonic-gate  *	when fdstart returns, the operation is already done.
30750Sstevel@tonic-gate  *
30760Sstevel@tonic-gate  *	- called with the low level lock held
30770Sstevel@tonic-gate  *
30780Sstevel@tonic-gate  */
30790Sstevel@tonic-gate 
30800Sstevel@tonic-gate static int slavio_index_pulse_work_around = 0;
30810Sstevel@tonic-gate 
30820Sstevel@tonic-gate static void
30830Sstevel@tonic-gate fdstart(struct fdctlr *fdc)
30840Sstevel@tonic-gate {
30850Sstevel@tonic-gate 	struct buf *bp;
30860Sstevel@tonic-gate 	struct fdcsb *csb;
30870Sstevel@tonic-gate 	struct fdunit *un;
30880Sstevel@tonic-gate 	struct fd_char *ch;
30890Sstevel@tonic-gate 	struct dk_map32 *dkm;
30900Sstevel@tonic-gate 	uint_t	part;		/* partition number for the transfer */
30910Sstevel@tonic-gate 	uint_t	start_part;	/* starting block of the partition */
30920Sstevel@tonic-gate 	uint_t	last_part;	/* last block of the partition */
30930Sstevel@tonic-gate 	uint_t	blk;		/* starting block of transfer on diskette */
30940Sstevel@tonic-gate 	uint_t	sect;		/* starting block's offset into track */
30950Sstevel@tonic-gate 	uint_t	cyl;		/* starting cylinder of the transfer */
30960Sstevel@tonic-gate 	uint_t	bincyl;		/* starting blocks's offset into cylinder */
30970Sstevel@tonic-gate 	uint_t	secpcyl;	/* number of sectors per cylinder */
30980Sstevel@tonic-gate 	uint_t	phys_blkno;	/* no. of blocks on the diskette */
30990Sstevel@tonic-gate 	uint_t	head;		/* one of two diskette heads */
31000Sstevel@tonic-gate 	uint_t	unit;
31010Sstevel@tonic-gate 	uint_t	len, tlen;
31020Sstevel@tonic-gate 	caddr_t addr;
31030Sstevel@tonic-gate 	caddr_t temp_addr;
31040Sstevel@tonic-gate 	uint_t	partial_read = 0;
31050Sstevel@tonic-gate 	int sb_temp_buf_used = 0;
31060Sstevel@tonic-gate 
31070Sstevel@tonic-gate 	bp = fdc->c_actf;
31080Sstevel@tonic-gate 
31090Sstevel@tonic-gate 	while (bp != NULL) {
31100Sstevel@tonic-gate 
31110Sstevel@tonic-gate 		fdc->c_actf = bp->av_forw;
31120Sstevel@tonic-gate 		fdc->c_current = bp;
31130Sstevel@tonic-gate 
31140Sstevel@tonic-gate 		/*
31150Sstevel@tonic-gate 		 * Initialize the buf structure.  The residual count is
31160Sstevel@tonic-gate 		 * initially the number of bytes to be read or written
31170Sstevel@tonic-gate 		 */
31180Sstevel@tonic-gate 		bp->b_flags &= ~B_ERROR;
31190Sstevel@tonic-gate 		bp->b_error = 0;
31200Sstevel@tonic-gate 		bp->b_resid = bp->b_bcount;
31210Sstevel@tonic-gate 		bp_mapin(bp);			/* map in buffers */
31220Sstevel@tonic-gate 
31230Sstevel@tonic-gate 		addr = bp->b_un.b_addr;		/* assign buffer address */
31240Sstevel@tonic-gate 
31250Sstevel@tonic-gate 		/*
31260Sstevel@tonic-gate 		 * Find the unit and partition numbers.
31270Sstevel@tonic-gate 		 */
31280Sstevel@tonic-gate 		unit = fdc->c_un->un_unit_no;
31290Sstevel@tonic-gate 		un = fdc->c_un;
31300Sstevel@tonic-gate 		ch = un->un_chars;
31310Sstevel@tonic-gate 		part = FDPARTITION(bp->b_edev);
31320Sstevel@tonic-gate 		dkm = &un->un_label.dkl_map[part];
31330Sstevel@tonic-gate 
31340Sstevel@tonic-gate 		if (un->un_chars->fdc_medium) {
31350Sstevel@tonic-gate 			phys_blkno = bp->b_blkno >> 1;
31360Sstevel@tonic-gate 		} else {
31370Sstevel@tonic-gate 			phys_blkno = bp->b_blkno;
31380Sstevel@tonic-gate 		}
31390Sstevel@tonic-gate 
31400Sstevel@tonic-gate 		if (un->un_iostat) {
31410Sstevel@tonic-gate 			kstat_waitq_to_runq(KIOSP);
31420Sstevel@tonic-gate 		}
31430Sstevel@tonic-gate 
31440Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_STRT,
31450Sstevel@tonic-gate 		    (C, "fdstart: bp=0x%p blkno=0x%x bcount=0x%x\n",
31460Sstevel@tonic-gate 		    (void *)bp, (int)bp->b_blkno, (int)bp->b_bcount));
31470Sstevel@tonic-gate 
31480Sstevel@tonic-gate 		/*
31490Sstevel@tonic-gate 		 * Get the csb and initialize the values that are the same
31500Sstevel@tonic-gate 		 * for DMA and PIO.
31510Sstevel@tonic-gate 		 */
31520Sstevel@tonic-gate 		fdgetcsb(fdc);		/* get csb (maybe wait for it) */
31530Sstevel@tonic-gate 		csb = &fdc->c_csb;
31540Sstevel@tonic-gate 		csb->csb_unit = unit;		/* floppy unit number */
31550Sstevel@tonic-gate 
31560Sstevel@tonic-gate 
31570Sstevel@tonic-gate 		/*
31580Sstevel@tonic-gate 		 * bugID:4133425 : If the controller is SLAVIO, and
31590Sstevel@tonic-gate 		 * the read does not reach end of track, then modify
31600Sstevel@tonic-gate 		 * the tlen to read until the end of track to a temp
31610Sstevel@tonic-gate 		 * buffer and disable MT. After the read is over,
31620Sstevel@tonic-gate 		 * copy the useful portion of the data to 'addr'.
31630Sstevel@tonic-gate 		 * Enable this feature only when
31640Sstevel@tonic-gate 		 * slavio_index_pulse_work_aound variable is
31650Sstevel@tonic-gate 		 * set in /etc/system.
31660Sstevel@tonic-gate 		 */
31670Sstevel@tonic-gate 
31680Sstevel@tonic-gate 
31690Sstevel@tonic-gate 		if (bp->b_flags & B_READ) {
31700Sstevel@tonic-gate 			if (((fdc->c_fdtype & FDCTYPE_SLAVIO) &&
3171*7656SSherry.Moore@Sun.COM 			    slavio_index_pulse_work_around) ||
3172*7656SSherry.Moore@Sun.COM 			    (fdc->c_fdtype & FDCTYPE_TCBUG))
31730Sstevel@tonic-gate 				csb->csb_cmds[0] = SK | FDRAW_RDCMD | MFM;
31740Sstevel@tonic-gate 			else
31750Sstevel@tonic-gate 				csb->csb_cmds[0] = MT | SK | FDRAW_RDCMD | MFM;
31760Sstevel@tonic-gate 		} else {
31770Sstevel@tonic-gate 			if (fdc->c_fdtype & FDCTYPE_TCBUG)
31780Sstevel@tonic-gate 				csb->csb_cmds[0] = FDRAW_WRCMD | MFM;
31790Sstevel@tonic-gate 			else
31800Sstevel@tonic-gate 				csb->csb_cmds[0] = MT | FDRAW_WRCMD | MFM;
31810Sstevel@tonic-gate 		}
31820Sstevel@tonic-gate 
31830Sstevel@tonic-gate 
31840Sstevel@tonic-gate 		if (bp->b_flags & B_READ)
31850Sstevel@tonic-gate 			fdc->c_csb.csb_read = CSB_READ;
31860Sstevel@tonic-gate 		else
31870Sstevel@tonic-gate 			fdc->c_csb.csb_read = CSB_WRITE;
31880Sstevel@tonic-gate 
31890Sstevel@tonic-gate 
31900Sstevel@tonic-gate 		csb->csb_cmds[5] = ch->fdc_medium ? 3 : 2; /* sector size  */
31910Sstevel@tonic-gate 		csb->csb_cmds[6] = ch->fdc_secptrack; /* EOT-# of sectors/trk */
31920Sstevel@tonic-gate 		csb->csb_cmds[7] = GPLN;	/* GPL - gap 3 size code */
31930Sstevel@tonic-gate 		csb->csb_cmds[8] = SSSDTL;	/* DTL - be 0xFF if N != 0 */
31940Sstevel@tonic-gate 
31950Sstevel@tonic-gate 		csb->csb_ncmds = NCBRW;		/* number of command bytes */
31960Sstevel@tonic-gate 		csb->csb_nrslts = NRBRW;	/* number of result bytes */
31970Sstevel@tonic-gate 
31980Sstevel@tonic-gate 
31990Sstevel@tonic-gate 		/*
32000Sstevel@tonic-gate 		 * opflags for interrupt handler, et.al.
32010Sstevel@tonic-gate 		 */
32020Sstevel@tonic-gate 		csb->csb_opflags = CSB_OFXFEROPS | CSB_OFTIMEIT;
32030Sstevel@tonic-gate 
32040Sstevel@tonic-gate 
32050Sstevel@tonic-gate 		/*
32060Sstevel@tonic-gate 		 * Make sure the transfer does not go off the end
32070Sstevel@tonic-gate 		 * of the partition.  Limit the actual amount transferred
32080Sstevel@tonic-gate 		 * to fit the partition.
32090Sstevel@tonic-gate 		 */
32100Sstevel@tonic-gate 
32110Sstevel@tonic-gate 		blk = phys_blkno;
32120Sstevel@tonic-gate 		start_part = (dkm->dkl_cylno * ch->fdc_secptrack
3213*7656SSherry.Moore@Sun.COM 		    * ch->fdc_nhead);
32140Sstevel@tonic-gate 		blk = blk + start_part;
32150Sstevel@tonic-gate 		last_part = start_part + dkm->dkl_nblk;
32160Sstevel@tonic-gate 
32170Sstevel@tonic-gate 		if ((blk + (bp->b_bcount / ch->fdc_sec_size)) > last_part)
32180Sstevel@tonic-gate 			len = (last_part - blk) * ch->fdc_sec_size;
32190Sstevel@tonic-gate 		else
32200Sstevel@tonic-gate 			len = (uint_t)bp->b_bcount;
32210Sstevel@tonic-gate 
32220Sstevel@tonic-gate 		/*
32230Sstevel@tonic-gate 		 * now we have the real start blk,
32240Sstevel@tonic-gate 		 * addr and len for xfer op
32250Sstevel@tonic-gate 		 * sectors per cylinder
32260Sstevel@tonic-gate 		 */
32270Sstevel@tonic-gate 		secpcyl = ch->fdc_nhead * ch->fdc_secptrack;
32280Sstevel@tonic-gate 
32290Sstevel@tonic-gate 		/*
32300Sstevel@tonic-gate 		 * The controller can transfer up to a cylinder at a time.
32310Sstevel@tonic-gate 		 * Early revs of the 82077 have a bug that causes the chip to
32320Sstevel@tonic-gate 		 * fail to respond to the Terminal Count signal.  Due to this
32330Sstevel@tonic-gate 		 * bug, controllers with type FDCTYPE_TCBUG, only transfer up
32340Sstevel@tonic-gate 		 * to a track at a time.
32350Sstevel@tonic-gate 		 * See earlier comment for bugID:4133425 for index pulse
32360Sstevel@tonic-gate 		 * work around.
32370Sstevel@tonic-gate 		 */
32380Sstevel@tonic-gate 
32390Sstevel@tonic-gate 		while (len != 0) {
32400Sstevel@tonic-gate 
32410Sstevel@tonic-gate 			cyl = blk / secpcyl;	/* cylinder of transfer */
32420Sstevel@tonic-gate 			bincyl = blk % secpcyl;	/* blk within cylinder */
32430Sstevel@tonic-gate 			head = bincyl / ch->fdc_secptrack;
32440Sstevel@tonic-gate 			sect = (bincyl % ch->fdc_secptrack) + 1;
32450Sstevel@tonic-gate 						/* sect w/in track */
32460Sstevel@tonic-gate 
32470Sstevel@tonic-gate 			/*
32480Sstevel@tonic-gate 			 * If the desired block and length will go beyond the
32490Sstevel@tonic-gate 			 * cylinder end, limit it to the cylinder end.
32500Sstevel@tonic-gate 			 */
32510Sstevel@tonic-gate 
32520Sstevel@tonic-gate 			if ((fdc->c_fdtype & FDCTYPE_SLAVIO) &&
3253*7656SSherry.Moore@Sun.COM 			    slavio_index_pulse_work_around &&
3254*7656SSherry.Moore@Sun.COM 			    (fdc->c_csb.csb_read == CSB_READ)) {
32550Sstevel@tonic-gate 
32560Sstevel@tonic-gate 				tlen = (ch->fdc_secptrack - sect + 1) *
3257*7656SSherry.Moore@Sun.COM 				    ch->fdc_sec_size;
32580Sstevel@tonic-gate 				if (len < tlen) {
32590Sstevel@tonic-gate 					partial_read = 1;
32600Sstevel@tonic-gate 					temp_addr = (caddr_t)kmem_alloc(tlen,
3261*7656SSherry.Moore@Sun.COM 					    KM_SLEEP);
32620Sstevel@tonic-gate 				}
32630Sstevel@tonic-gate 
32640Sstevel@tonic-gate 			} else if (fdc->c_fdtype & FDCTYPE_TCBUG) {
32650Sstevel@tonic-gate 				tlen = len;
32660Sstevel@tonic-gate 				if (len > ((ch->fdc_secptrack - sect + 1) *
3267*7656SSherry.Moore@Sun.COM 				    ch->fdc_sec_size))
32680Sstevel@tonic-gate 					tlen = (ch->fdc_secptrack - sect + 1)
3269*7656SSherry.Moore@Sun.COM 					    * ch->fdc_sec_size;
32700Sstevel@tonic-gate 			} else {
32710Sstevel@tonic-gate 				if (len > ((secpcyl - bincyl)
3272*7656SSherry.Moore@Sun.COM 				    * ch->fdc_sec_size))
32730Sstevel@tonic-gate 					tlen = (secpcyl - bincyl)
3274*7656SSherry.Moore@Sun.COM 					    * ch->fdc_sec_size;
32750Sstevel@tonic-gate 
32760Sstevel@tonic-gate 				else
32770Sstevel@tonic-gate 					tlen = len;
32780Sstevel@tonic-gate 			}
32790Sstevel@tonic-gate 			if (fdc->c_fdtype & FDCTYPE_SB) {
32800Sstevel@tonic-gate 				/*
32810Sstevel@tonic-gate 				 * To avoid underrun errors during IFB activity.
32820Sstevel@tonic-gate 				 */
32830Sstevel@tonic-gate 				if (tlen > max_fd_dma_len)
32840Sstevel@tonic-gate 					tlen = max_fd_dma_len;
32850Sstevel@tonic-gate 			}
32860Sstevel@tonic-gate 
32870Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_STRT,
32880Sstevel@tonic-gate 			    (C, "	blk 0x%x, addr 0x%p, len 0x%x\n",
32890Sstevel@tonic-gate 			    blk, (void *)addr, len));
32900Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_STRT,
32910Sstevel@tonic-gate 			    (C, "cyl:%x, head:%x, sec:%x\n",
32920Sstevel@tonic-gate 			    cyl, head, sect));
32930Sstevel@tonic-gate 
32940Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_STRT,
32950Sstevel@tonic-gate 			    (C, "	resid 0x%lx, tlen %d\n",
32960Sstevel@tonic-gate 			    bp->b_resid, tlen));
32970Sstevel@tonic-gate 
32980Sstevel@tonic-gate 			/*
32990Sstevel@tonic-gate 			 * Finish programming the command
33000Sstevel@tonic-gate 			 */
33010Sstevel@tonic-gate 			csb->csb_cmds[1] = (head << 2) | unit;
33020Sstevel@tonic-gate 			if (fdc->c_fdtype & FDCTYPE_SB)
33030Sstevel@tonic-gate 				csb->csb_cmds[1] |= IPS;
33040Sstevel@tonic-gate 
33050Sstevel@tonic-gate 			csb->csb_cmds[2] = cyl;	/* C - cylinder address */
33060Sstevel@tonic-gate 			csb->csb_cmds[3] = head;	/* H - head number */
33070Sstevel@tonic-gate 			csb->csb_cmds[4] = sect;	/* R - sector number */
33080Sstevel@tonic-gate 			if (fdc->c_fdtype & FDCTYPE_TCBUG)
33090Sstevel@tonic-gate 				csb->csb_cmds[6] = sect +
3310*7656SSherry.Moore@Sun.COM 				    (tlen / ch->fdc_sec_size) - 1;
33110Sstevel@tonic-gate 
33120Sstevel@tonic-gate 			csb->csb_len = tlen;
33130Sstevel@tonic-gate 			if (partial_read)
33140Sstevel@tonic-gate 				csb->csb_addr = temp_addr;
33150Sstevel@tonic-gate 			else
33160Sstevel@tonic-gate 				csb->csb_addr = addr;
33170Sstevel@tonic-gate 
33180Sstevel@tonic-gate 			/* retry this many times max */
33190Sstevel@tonic-gate 			csb->csb_maxretry = rwretry;
33200Sstevel@tonic-gate 			csb->csb_retrys = 0;
33210Sstevel@tonic-gate 
33220Sstevel@tonic-gate 			/* If platform supports DMA, set up DMA resources */
33230Sstevel@tonic-gate 			if (fdc->c_fdtype & FDCTYPE_DMA) {
33240Sstevel@tonic-gate 				if ((fdc->c_fdtype & FDCTYPE_SB) &&
3325483Spc157239 				    (((uint32_t)(uintptr_t)addr & 0xFFFF0000) !=
3326483Spc157239 				    (((uint32_t)(uintptr_t)addr + tlen) &
3327483Spc157239 				    0xFFFF0000))) {
33280Sstevel@tonic-gate 					csb->csb_addr = fdc->dma_buf;
33290Sstevel@tonic-gate 					sb_temp_buf_used = 1;
33300Sstevel@tonic-gate 					if (csb->csb_read != CSB_READ) {
33310Sstevel@tonic-gate 						bcopy(addr, fdc->dma_buf, tlen);
33320Sstevel@tonic-gate 				}
33330Sstevel@tonic-gate 			}
33340Sstevel@tonic-gate 				mutex_enter(&fdc->c_hilock);
33350Sstevel@tonic-gate 
33360Sstevel@tonic-gate 				if (fdstart_dma(fdc, csb->csb_addr,
3337*7656SSherry.Moore@Sun.COM 				    tlen) != 0) {
33380Sstevel@tonic-gate 
33390Sstevel@tonic-gate 					bp->b_flags |= B_ERROR;
33400Sstevel@tonic-gate 					bp->b_error = EAGAIN;
33410Sstevel@tonic-gate 
33420Sstevel@tonic-gate 					mutex_exit(&fdc->c_hilock);
33430Sstevel@tonic-gate 					FDERRPRINT(FDEP_L1, FDEM_STRT,
3344*7656SSherry.Moore@Sun.COM 					    (C, "fdstart: no dma resources\n"));
33450Sstevel@tonic-gate 
33460Sstevel@tonic-gate 					break;
33470Sstevel@tonic-gate 				}
33480Sstevel@tonic-gate 				mutex_exit(&fdc->c_hilock);
33490Sstevel@tonic-gate 
33500Sstevel@tonic-gate 			}
33510Sstevel@tonic-gate 
33520Sstevel@tonic-gate 			bp->b_error = fdexec(fdc, FDXC_SLEEP|FDXC_CHECKCHG);
33530Sstevel@tonic-gate 			if (bp->b_error != 0) {
33540Sstevel@tonic-gate 				/*
33550Sstevel@tonic-gate 				 * error in fdexec
33560Sstevel@tonic-gate 				 */
33570Sstevel@tonic-gate 				FDERRPRINT(FDEP_L1, FDEM_STRT, (C,
33580Sstevel@tonic-gate 				    "fdstart: bad exec of bp: 0x%p, err %d\n",
33590Sstevel@tonic-gate 				    (void *)bp, bp->b_error));
33600Sstevel@tonic-gate 
33610Sstevel@tonic-gate 				bp->b_flags |= B_ERROR;
33620Sstevel@tonic-gate 				if (partial_read) {
33630Sstevel@tonic-gate 					partial_read = 0;
33640Sstevel@tonic-gate 					kmem_free(temp_addr, tlen);
33650Sstevel@tonic-gate 				}
33660Sstevel@tonic-gate 				break;
33670Sstevel@tonic-gate 			}
33680Sstevel@tonic-gate 
33690Sstevel@tonic-gate 			/*
33700Sstevel@tonic-gate 			 * If it was a partial read, copy the useful
33710Sstevel@tonic-gate 			 * portion of data to 'addr'.
33720Sstevel@tonic-gate 			 */
33730Sstevel@tonic-gate 			if (partial_read) {
33740Sstevel@tonic-gate 				partial_read = 0;
33750Sstevel@tonic-gate 				bcopy(temp_addr, addr, len);
33760Sstevel@tonic-gate 				kmem_free(temp_addr, tlen);
33770Sstevel@tonic-gate 				tlen = len;
33780Sstevel@tonic-gate 			}
33790Sstevel@tonic-gate 			if ((fdc->c_fdtype & FDCTYPE_SB) &&
3380*7656SSherry.Moore@Sun.COM 			    (csb->csb_read == CSB_READ)) {
33810Sstevel@tonic-gate 				if (sb_temp_buf_used) {
33820Sstevel@tonic-gate 					bcopy(fdc->dma_buf, addr, tlen);
33830Sstevel@tonic-gate 					sb_temp_buf_used = 0;
33840Sstevel@tonic-gate 				}
33850Sstevel@tonic-gate 			}
33860Sstevel@tonic-gate 
33870Sstevel@tonic-gate 			blk += tlen / ch->fdc_sec_size;
33880Sstevel@tonic-gate 			len -= tlen;
33890Sstevel@tonic-gate 			addr += tlen;
33900Sstevel@tonic-gate 			bp->b_resid -= tlen;
33910Sstevel@tonic-gate 
33920Sstevel@tonic-gate 		}
33930Sstevel@tonic-gate 
33940Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_STRT,
33950Sstevel@tonic-gate 		    (C, "fdstart done: b_resid %lu, b_count %lu, csb_rlen %d\n",
33960Sstevel@tonic-gate 		    bp->b_resid, bp->b_bcount, fdc->c_csb.csb_rlen));
33970Sstevel@tonic-gate 
33980Sstevel@tonic-gate 		fdc->c_current = 0;
33990Sstevel@tonic-gate 		fdretcsb(fdc);
34000Sstevel@tonic-gate 		if (un->un_iostat) {
34010Sstevel@tonic-gate 			if (bp->b_flags & B_READ) {
34020Sstevel@tonic-gate 				KIOSP->reads++;
34030Sstevel@tonic-gate 				KIOSP->nread +=
3404*7656SSherry.Moore@Sun.COM 				    (bp->b_bcount - bp->b_resid);
34050Sstevel@tonic-gate 			} else {
34060Sstevel@tonic-gate 				KIOSP->writes++;
34070Sstevel@tonic-gate 				KIOSP->nwritten += (bp->b_bcount - bp->b_resid);
34080Sstevel@tonic-gate 			}
34090Sstevel@tonic-gate 			kstat_runq_exit(KIOSP);
34100Sstevel@tonic-gate 		}
34110Sstevel@tonic-gate 		biodone(bp);
34120Sstevel@tonic-gate 
34130Sstevel@tonic-gate 		/*
34140Sstevel@tonic-gate 		 * Look at the next buffer
34150Sstevel@tonic-gate 		 */
34160Sstevel@tonic-gate 		bp = fdc->c_actf;
34170Sstevel@tonic-gate 
34180Sstevel@tonic-gate 	}
34190Sstevel@tonic-gate }
34200Sstevel@tonic-gate 
34210Sstevel@tonic-gate /*
34220Sstevel@tonic-gate  * Set up DMA resources
34230Sstevel@tonic-gate  * The DMA handle was initialized in fd_attach()
34240Sstevel@tonic-gate  * Assumes the handle has already been allocated by fd_attach()
34250Sstevel@tonic-gate  */
34260Sstevel@tonic-gate static int
34270Sstevel@tonic-gate fdstart_dma(struct fdctlr *fdc, caddr_t addr, uint_t len)
34280Sstevel@tonic-gate {
34290Sstevel@tonic-gate 	int		flags;		/* flags for setting up resources */
34300Sstevel@tonic-gate 	int		res;
34310Sstevel@tonic-gate 
34320Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_SDMA, (C, "fdstart_dma: start\n"));
34330Sstevel@tonic-gate 
34340Sstevel@tonic-gate 	if (fdc->c_csb.csb_read == CSB_READ) {
34350Sstevel@tonic-gate 		flags = DDI_DMA_READ;
34360Sstevel@tonic-gate 	} else {
34370Sstevel@tonic-gate 		flags = DDI_DMA_WRITE;
34380Sstevel@tonic-gate 	}
34390Sstevel@tonic-gate 
34400Sstevel@tonic-gate 
34410Sstevel@tonic-gate 	/* allow partial mapping to maximize the portability of the driver */
34420Sstevel@tonic-gate 	flags = flags | DDI_DMA_PARTIAL;
34430Sstevel@tonic-gate 
34440Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_SDMA, (C, "fdstart_dma: amt. asked for %d\n",
3445*7656SSherry.Moore@Sun.COM 	    len));
34460Sstevel@tonic-gate 
34470Sstevel@tonic-gate 	/*
34480Sstevel@tonic-gate 	 * Zero out the current cookie.  This is done to ensure that
34490Sstevel@tonic-gate 	 * the previous transfers cookie information can in no way be
34500Sstevel@tonic-gate 	 * used.
34510Sstevel@tonic-gate 	 */
34520Sstevel@tonic-gate 	bzero((char *)&fdc->c_csb.csb_dmacookie,
3453*7656SSherry.Moore@Sun.COM 	    sizeof (fdc->c_csb.csb_dmacookie));
34540Sstevel@tonic-gate 	fdc->c_csb.csb_nwin = 0;
34550Sstevel@tonic-gate 	fdc->c_csb.csb_windex = 0;
34560Sstevel@tonic-gate 	fdc->c_csb.csb_ccount = 0;
34570Sstevel@tonic-gate 
34580Sstevel@tonic-gate 	res = ddi_dma_addr_bind_handle(fdc->c_dmahandle, NULL, addr, len,
3459*7656SSherry.Moore@Sun.COM 	    flags, DDI_DMA_DONTWAIT, 0,  &fdc->c_csb.csb_dmacookie,
3460*7656SSherry.Moore@Sun.COM 	    &fdc->c_csb.csb_ccount);
34610Sstevel@tonic-gate 
34620Sstevel@tonic-gate 	switch (res) {
34630Sstevel@tonic-gate 		case DDI_DMA_MAPPED:
34640Sstevel@tonic-gate 			/*
34650Sstevel@tonic-gate 			 * There is one window. csb_windex is the index
34660Sstevel@tonic-gate 			 * into the array of windows. If there are n
34670Sstevel@tonic-gate 			 * windows then, (0 <= windex <= n-1).  csb_windex
34680Sstevel@tonic-gate 			 * represents the index of the next window
34690Sstevel@tonic-gate 			 * to be processed.
34700Sstevel@tonic-gate 			 */
34710Sstevel@tonic-gate 			fdc->c_csb.csb_nwin = 1;
34720Sstevel@tonic-gate 			fdc->c_csb.csb_windex = 1;
34730Sstevel@tonic-gate 
34740Sstevel@tonic-gate 
34750Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3476*7656SSherry.Moore@Sun.COM 			    (C, "fdstart_dma: DDI_DMA_MAPPED\n"));
34770Sstevel@tonic-gate 
34780Sstevel@tonic-gate 			break;
34790Sstevel@tonic-gate 		case DDI_DMA_PARTIAL_MAP:
34800Sstevel@tonic-gate 
34810Sstevel@tonic-gate 			/*
34820Sstevel@tonic-gate 			 * obtain the number of DMA windows
34830Sstevel@tonic-gate 			 */
34840Sstevel@tonic-gate 			if (ddi_dma_numwin(fdc->c_dmahandle,
3485*7656SSherry.Moore@Sun.COM 			    &fdc->c_csb.csb_nwin) != DDI_SUCCESS) {
34860Sstevel@tonic-gate 				return (-1);
34870Sstevel@tonic-gate 			}
34880Sstevel@tonic-gate 
34890Sstevel@tonic-gate 
34900Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3491*7656SSherry.Moore@Sun.COM 			    (C, "fdstart_dma: partially mapped %d windows\n",
3492*7656SSherry.Moore@Sun.COM 			    fdc->c_csb.csb_nwin));
34930Sstevel@tonic-gate 
34940Sstevel@tonic-gate 			/*
34950Sstevel@tonic-gate 			 * The DMA window currently in use is window number
34960Sstevel@tonic-gate 			 * one.
34970Sstevel@tonic-gate 			 */
34980Sstevel@tonic-gate 			fdc->c_csb.csb_windex = 1;
34990Sstevel@tonic-gate 
35000Sstevel@tonic-gate 			break;
35010Sstevel@tonic-gate 		case DDI_DMA_NORESOURCES:
35020Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3503*7656SSherry.Moore@Sun.COM 			    (C, "fdstart_dma: no resources\n"));
35040Sstevel@tonic-gate 			return (-1);
35050Sstevel@tonic-gate 		case DDI_DMA_NOMAPPING:
35060Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3507*7656SSherry.Moore@Sun.COM 			    (C, "fdstart_dma: no mapping\n"));
35080Sstevel@tonic-gate 			return (-1);
35090Sstevel@tonic-gate 		case DDI_DMA_TOOBIG:
35100Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3511*7656SSherry.Moore@Sun.COM 			    (C, "fdstart_dma: too big\n"));
35120Sstevel@tonic-gate 			return (-1);
35130Sstevel@tonic-gate 
35140Sstevel@tonic-gate 		case DDI_DMA_INUSE:
35150Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3516*7656SSherry.Moore@Sun.COM 			    (C, "fdstart_dma: dma inuse\n"));
35170Sstevel@tonic-gate 			return (-1);
35180Sstevel@tonic-gate 		default:
35190Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3520*7656SSherry.Moore@Sun.COM 			    (C, "fdstart_dma: result is 0x%x\n", res));
35210Sstevel@tonic-gate 			return (-1);
35220Sstevel@tonic-gate 
35230Sstevel@tonic-gate 	};
35240Sstevel@tonic-gate 
35250Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_SDMA,
3526*7656SSherry.Moore@Sun.COM 	    (C, "fdstart_dma: bound the handle\n"));
35270Sstevel@tonic-gate 
35280Sstevel@tonic-gate 	ASSERT(fdc->c_csb.csb_dmacookie.dmac_size);
35290Sstevel@tonic-gate 
35300Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_SDMA, (C, "fdstart_dma: done\n"));
35310Sstevel@tonic-gate 	return (0);
35320Sstevel@tonic-gate }
35330Sstevel@tonic-gate 
35340Sstevel@tonic-gate 
35350Sstevel@tonic-gate /*
35360Sstevel@tonic-gate  * fd_unbind_handle: unbind a dma handle if one exists
35370Sstevel@tonic-gate  *		return EIO if unbind failes
35380Sstevel@tonic-gate  */
35390Sstevel@tonic-gate static int
35400Sstevel@tonic-gate fd_unbind_handle(struct fdctlr *fdc)
35410Sstevel@tonic-gate {
35420Sstevel@tonic-gate 	if ((fdc->c_fdtype & FDCTYPE_DMA) &&
3543*7656SSherry.Moore@Sun.COM 	    ((fdc->c_csb.csb_read == CSB_READ) ||
3544*7656SSherry.Moore@Sun.COM 	    (fdc->c_csb.csb_read == CSB_WRITE))) {
35450Sstevel@tonic-gate 		mutex_enter(&fdc->c_hilock);
35460Sstevel@tonic-gate 
35470Sstevel@tonic-gate 		if (fdc->c_fdtype & FDCTYPE_SB) {
35480Sstevel@tonic-gate 			if (fdc->sb_dma_lock) {
35490Sstevel@tonic-gate 				release_sb_dma(fdc);
35500Sstevel@tonic-gate 			}
35510Sstevel@tonic-gate 		}
35520Sstevel@tonic-gate 
35530Sstevel@tonic-gate 		/*
35540Sstevel@tonic-gate 		 * If the byte count isn't zero, then the DMA engine is
35550Sstevel@tonic-gate 		 * still doing a transfer.  If the byte count is nonzero,
35560Sstevel@tonic-gate 		 * reset the DMA engine to cause it to drain.
35570Sstevel@tonic-gate 		 */
35580Sstevel@tonic-gate 
35590Sstevel@tonic-gate 		if (get_data_count_register(fdc) != 0) {
3560*7656SSherry.Moore@Sun.COM 			FDERRPRINT(FDEP_L1, FDEM_EXEC,
3561*7656SSherry.Moore@Sun.COM 			    (C, "unbind & byte count isn't zero\n"));
3562*7656SSherry.Moore@Sun.COM 
3563*7656SSherry.Moore@Sun.COM 			reset_dma_controller(fdc);
3564*7656SSherry.Moore@Sun.COM 			set_dma_control_register(fdc, DCSR_INIT_BITS);
35650Sstevel@tonic-gate 		}
35660Sstevel@tonic-gate 
35670Sstevel@tonic-gate 		if (ddi_dma_unbind_handle(fdc->c_dmahandle) != DDI_SUCCESS) {
35680Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_EXEC,
3569*7656SSherry.Moore@Sun.COM 			    (C, "problem unbinding the handle\n"));
35700Sstevel@tonic-gate 			mutex_exit(&fdc->c_hilock);
35710Sstevel@tonic-gate 			return (EIO);
35720Sstevel@tonic-gate 		}
35730Sstevel@tonic-gate 		mutex_exit(&fdc->c_hilock);
35740Sstevel@tonic-gate 	}
35750Sstevel@tonic-gate 	return (0);
35760Sstevel@tonic-gate }
35770Sstevel@tonic-gate 
35780Sstevel@tonic-gate /*
35790Sstevel@tonic-gate  * fdexec
35800Sstevel@tonic-gate  *	all commands go through here.  Assumes the command block
35810Sstevel@tonic-gate  *	fdctlr.c_csb is filled in.  The bytes are sent to the
35820Sstevel@tonic-gate  *	controller and then we do whatever else the csb says -
35830Sstevel@tonic-gate  *	like wait for immediate results, etc.
35840Sstevel@tonic-gate  *
35850Sstevel@tonic-gate  *	All waiting for operations done is in here - to allow retrys
35860Sstevel@tonic-gate  *	and checking for disk changed - so we don't have to worry
35870Sstevel@tonic-gate  *	about sleeping at interrupt level.
35880Sstevel@tonic-gate  *
35890Sstevel@tonic-gate  * RETURNS: 0 if all ok,
35900Sstevel@tonic-gate  *	ENXIO - diskette not in drive
35910Sstevel@tonic-gate  *	EBUSY - if chip is locked or busy
35920Sstevel@tonic-gate  *	EIO - for timeout during sending cmds to chip
35930Sstevel@tonic-gate  *
35940Sstevel@tonic-gate  * to sleep: set FDXC_SLEEP, to check for disk
35950Sstevel@tonic-gate  * changed: set FDXC_CHECKCHG
35960Sstevel@tonic-gate  *
35970Sstevel@tonic-gate  *	- called with the lock held
35980Sstevel@tonic-gate  */
35990Sstevel@tonic-gate static int
36000Sstevel@tonic-gate fdexec(struct fdctlr *fdc, int flags)
36010Sstevel@tonic-gate {
36020Sstevel@tonic-gate 	struct fdcsb *csb;
36030Sstevel@tonic-gate 	int	i;
36040Sstevel@tonic-gate 	int	to, unit;
36050Sstevel@tonic-gate 	uchar_t	tmp;
36060Sstevel@tonic-gate 	caddr_t a = (caddr_t)fdc;
36070Sstevel@tonic-gate 
36080Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: flags:%x\n", flags));
36090Sstevel@tonic-gate 
36100Sstevel@tonic-gate 	ASSERT(mutex_owned(&fdc->c_lolock));
36110Sstevel@tonic-gate 
36120Sstevel@tonic-gate 	csb = &fdc->c_csb;
36130Sstevel@tonic-gate 	unit = csb->csb_unit;
36140Sstevel@tonic-gate 
36150Sstevel@tonic-gate 
36160Sstevel@tonic-gate 	ASSERT(unit == fdc->c_un->un_unit_no);
36170Sstevel@tonic-gate 
36180Sstevel@tonic-gate retry:
36190Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: cmd is %s\n",
3620*7656SSherry.Moore@Sun.COM 	    fdcmds[csb->csb_cmds[0] & 0x1f].cmdname));
36210Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: transfer rate = %d\n",
36220Sstevel@tonic-gate 	    fdc->c_un->un_chars->fdc_transfer_rate));
36230Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: sec size = %d\n",
36240Sstevel@tonic-gate 	    fdc->c_un->un_chars->fdc_sec_size));
36250Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: nblocks (512) = %d\n",
36260Sstevel@tonic-gate 	    fdc->c_un->un_label.dkl_map[2].dkl_nblk));
36270Sstevel@tonic-gate 
36280Sstevel@tonic-gate 	if ((fdc->c_fdtype & FDCTYPE_CTRLMASK) == FDCTYPE_82077) {
36290Sstevel@tonic-gate 		fdexec_turn_on_motor(fdc, flags, unit);
36300Sstevel@tonic-gate 	}
36310Sstevel@tonic-gate 
36320Sstevel@tonic-gate 
36330Sstevel@tonic-gate 	fdselect(fdc, unit, 1);	/* select drive */
36340Sstevel@tonic-gate 
36350Sstevel@tonic-gate 	/*
36360Sstevel@tonic-gate 	 * select data rate for this unit/command
36370Sstevel@tonic-gate 	 */
36380Sstevel@tonic-gate 	switch (fdc->c_un->un_chars->fdc_transfer_rate) {
36390Sstevel@tonic-gate 	case 500:
36400Sstevel@tonic-gate 		Dsr(fdc, 0);
36410Sstevel@tonic-gate 		break;
36420Sstevel@tonic-gate 	case 300:
36430Sstevel@tonic-gate 		Dsr(fdc, 1);
36440Sstevel@tonic-gate 		break;
36450Sstevel@tonic-gate 	case 250:
36460Sstevel@tonic-gate 		Dsr(fdc, 2);
36470Sstevel@tonic-gate 		break;
36480Sstevel@tonic-gate 	}
36490Sstevel@tonic-gate 	drv_usecwait(2);
36500Sstevel@tonic-gate 
36510Sstevel@tonic-gate 
36520Sstevel@tonic-gate 	/*
36530Sstevel@tonic-gate 	 * If checking for changed is enabled (i.e., not seeking in checkdisk),
36540Sstevel@tonic-gate 	 * we sample the DSKCHG line to see if the diskette has wandered away.
36550Sstevel@tonic-gate 	 */
36560Sstevel@tonic-gate 	if ((flags & FDXC_CHECKCHG) && fdsense_chng(fdc, unit)) {
36570Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "diskette changed\n"));
36580Sstevel@tonic-gate 		fdc->c_un->un_flags |= FDUNIT_CHANGED;
36590Sstevel@tonic-gate 
36600Sstevel@tonic-gate 		if (fdcheckdisk(fdc, unit)) {
36610Sstevel@tonic-gate 
36620Sstevel@tonic-gate 			(void) fd_unbind_handle(fdc);
36630Sstevel@tonic-gate 			return (ENXIO);
36640Sstevel@tonic-gate 
36650Sstevel@tonic-gate 		}
36660Sstevel@tonic-gate 	}
36670Sstevel@tonic-gate 
36680Sstevel@tonic-gate 	/*
36690Sstevel@tonic-gate 	 * gather some statistics
36700Sstevel@tonic-gate 	 */
36710Sstevel@tonic-gate 	switch (csb->csb_cmds[0] & 0x1f) {
36720Sstevel@tonic-gate 	case FDRAW_RDCMD:
36730Sstevel@tonic-gate 		fdc->fdstats.rd++;
36740Sstevel@tonic-gate 		break;
36750Sstevel@tonic-gate 	case FDRAW_WRCMD:
36760Sstevel@tonic-gate 		fdc->fdstats.wr++;
36770Sstevel@tonic-gate 		break;
36780Sstevel@tonic-gate 	case FDRAW_REZERO:
36790Sstevel@tonic-gate 		fdc->fdstats.recal++;
36800Sstevel@tonic-gate 		break;
36810Sstevel@tonic-gate 	case FDRAW_FORMAT:
36820Sstevel@tonic-gate 		fdc->fdstats.form++;
36830Sstevel@tonic-gate 		break;
36840Sstevel@tonic-gate 	default:
36850Sstevel@tonic-gate 		fdc->fdstats.other++;
36860Sstevel@tonic-gate 		break;
36870Sstevel@tonic-gate 	}
36880Sstevel@tonic-gate 
36890Sstevel@tonic-gate 	/*
36900Sstevel@tonic-gate 	 * Always set the opmode *prior* to poking the chip.
36910Sstevel@tonic-gate 	 * This way we don't have to do any locking at high level.
36920Sstevel@tonic-gate 	 */
36930Sstevel@tonic-gate 	csb->csb_raddr = 0;
36940Sstevel@tonic-gate 	csb->csb_rlen = 0;
36950Sstevel@tonic-gate 	if (csb->csb_opflags & CSB_OFSEEKOPS) {
36960Sstevel@tonic-gate 		csb->csb_opmode = 2;
36970Sstevel@tonic-gate 	} else if (csb->csb_opflags & CSB_OFIMMEDIATE) {
36980Sstevel@tonic-gate 		csb->csb_opmode = 0;
36990Sstevel@tonic-gate 	} else {
37000Sstevel@tonic-gate 		csb->csb_opmode = 1;	/* normal data xfer commands */
37010Sstevel@tonic-gate 		csb->csb_raddr = csb->csb_addr;
37020Sstevel@tonic-gate 		csb->csb_rlen = csb->csb_len;
37030Sstevel@tonic-gate 	}
37040Sstevel@tonic-gate 
37050Sstevel@tonic-gate 	bzero((caddr_t)csb->csb_rslt, 10);
37060Sstevel@tonic-gate 	csb->csb_status = 0;
37070Sstevel@tonic-gate 	csb->csb_cmdstat = 0;
37080Sstevel@tonic-gate 
37090Sstevel@tonic-gate 
37100Sstevel@tonic-gate 	/*
37110Sstevel@tonic-gate 	 * Program the DMA engine with the length and address of the transfer
37120Sstevel@tonic-gate 	 * (DMA is only used on a read or a write)
37130Sstevel@tonic-gate 	 */
37140Sstevel@tonic-gate 	if ((fdc->c_fdtype & FDCTYPE_DMA) &&
3715*7656SSherry.Moore@Sun.COM 	    ((fdc->c_csb.csb_read == CSB_READ) ||
3716*7656SSherry.Moore@Sun.COM 	    (fdc->c_csb.csb_read == CSB_WRITE)))  {
37170Sstevel@tonic-gate 		mutex_enter(&fdc->c_hilock);
37180Sstevel@tonic-gate 
37190Sstevel@tonic-gate 		/* Reset the dcsr to clear it of all errors */
37200Sstevel@tonic-gate 
37210Sstevel@tonic-gate 		reset_dma_controller(fdc);
37220Sstevel@tonic-gate 
37230Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "cookie addr 0x%p\n",
37240Sstevel@tonic-gate 		    (void *)fdc->c_csb.csb_dmacookie.dmac_laddress));
37250Sstevel@tonic-gate 
37260Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "cookie length %ld\n",
3727*7656SSherry.Moore@Sun.COM 		    fdc->c_csb.csb_dmacookie.dmac_size));
37280Sstevel@tonic-gate 		ASSERT(fdc->c_csb.csb_dmacookie.dmac_size);
37290Sstevel@tonic-gate 
37300Sstevel@tonic-gate 		set_data_count_register(fdc,
3731*7656SSherry.Moore@Sun.COM 		    fdc->c_csb.csb_dmacookie.dmac_size);
37320Sstevel@tonic-gate 		set_data_address_register(fdc,
3733*7656SSherry.Moore@Sun.COM 		    fdc->c_csb.csb_dmacookie.dmac_laddress);
37340Sstevel@tonic-gate 
37350Sstevel@tonic-gate 		/* Program the DCSR */
37360Sstevel@tonic-gate 
37370Sstevel@tonic-gate 		if (fdc->c_csb.csb_read == CSB_READ)
37380Sstevel@tonic-gate 			set_dma_mode(fdc, CSB_READ);
37390Sstevel@tonic-gate 		else
37400Sstevel@tonic-gate 			set_dma_mode(fdc, CSB_WRITE);
37410Sstevel@tonic-gate 		mutex_exit(&fdc->c_hilock);
37420Sstevel@tonic-gate 	}
37430Sstevel@tonic-gate 
37440Sstevel@tonic-gate 	/*
37450Sstevel@tonic-gate 	 * I saw this (chip unexpectedly busy) happen when i shoved the
37460Sstevel@tonic-gate 	 * floppy into the drive while
37470Sstevel@tonic-gate 	 * running a dd if= /dev/rfd0c.	so it *is* possible for this to happen.
37480Sstevel@tonic-gate 	 * we need to do a ctlr reset ...
37490Sstevel@tonic-gate 	 */
37500Sstevel@tonic-gate 
37510Sstevel@tonic-gate 	if (Msr(fdc) & CB) {
37520Sstevel@tonic-gate 		/* tried to give command to chip when it is busy! */
37530Sstevel@tonic-gate 		FDERRPRINT(FDEP_L3, FDEM_EXEC,
37540Sstevel@tonic-gate 		    (C, "fdc: unexpectedly busy-stat 0x%x\n", Msr(fdc)));
37550Sstevel@tonic-gate 		csb->csb_cmdstat = 1;	/* XXX TBD ERRS NYD for now */
37560Sstevel@tonic-gate 
37570Sstevel@tonic-gate 		(void) fd_unbind_handle(fdc);
37580Sstevel@tonic-gate 		return (EBUSY);
37590Sstevel@tonic-gate 	}
37600Sstevel@tonic-gate 
37610Sstevel@tonic-gate 	/* Give command to the controller */
37620Sstevel@tonic-gate 	for (i = 0; i < (int)csb->csb_ncmds; i++) {
37630Sstevel@tonic-gate 
37640Sstevel@tonic-gate 		/* Test the readiness of the controller to receive the cmd */
37650Sstevel@tonic-gate 		for (to = FD_CRETRY; to; to--) {
37660Sstevel@tonic-gate 			if ((Msr(fdc) & (DIO|RQM)) == RQM)
37670Sstevel@tonic-gate 				break;
37680Sstevel@tonic-gate 		}
37690Sstevel@tonic-gate 		if (to == 0) {
37700Sstevel@tonic-gate 			FDERRPRINT(FDEP_L2, FDEM_EXEC,
37710Sstevel@tonic-gate 			    (C, "fdc: no RQM - stat 0x%x\n", Msr(fdc)));
37720Sstevel@tonic-gate 			csb->csb_cmdstat = 1;
37730Sstevel@tonic-gate 
37740Sstevel@tonic-gate 			(void) fd_unbind_handle(fdc);
37750Sstevel@tonic-gate 			return (EIO);
37760Sstevel@tonic-gate 		}
37770Sstevel@tonic-gate 
37780Sstevel@tonic-gate 		Set_Fifo(fdc, csb->csb_cmds[i]);
37790Sstevel@tonic-gate 
37800Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_EXEC,
37810Sstevel@tonic-gate 		    (C, "fdexec: sent 0x%x, Msr 0x%x\n", csb->csb_cmds[i],
37820Sstevel@tonic-gate 		    Msr(fdc)));
37830Sstevel@tonic-gate 
37840Sstevel@tonic-gate 	}
37850Sstevel@tonic-gate 
37860Sstevel@tonic-gate 
37870Sstevel@tonic-gate 	/*
37880Sstevel@tonic-gate 	 * Start watchdog timer on data transfer type commands - required
37890Sstevel@tonic-gate 	 * in case a diskette is not present or is unformatted
37900Sstevel@tonic-gate 	 */
37910Sstevel@tonic-gate 	if (csb->csb_opflags & CSB_OFTIMEIT) {
37920Sstevel@tonic-gate 		fdc->c_timeid = timeout(fdwatch, a,
37930Sstevel@tonic-gate 		    tosec * drv_usectohz(1000000));
37940Sstevel@tonic-gate 	}
37950Sstevel@tonic-gate 
37960Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_EXEC,
37970Sstevel@tonic-gate 	    (C, "fdexec: cmd sent, Msr 0x%x\n", Msr(fdc)));
37980Sstevel@tonic-gate 
37990Sstevel@tonic-gate 	/* If the operation has no results - then just return */
38000Sstevel@tonic-gate 	if (csb->csb_opflags & CSB_OFNORESULTS) {
38010Sstevel@tonic-gate 		if (fdc->c_fdtype & FDCTYPE_82077) {
38020Sstevel@tonic-gate 			if (fdc->c_mtimeid == 0) {
38030Sstevel@tonic-gate 				fdc->c_mtimeid = timeout(fdmotoff, a,
3804*7656SSherry.Moore@Sun.COM 				    Motoff_delay);
38050Sstevel@tonic-gate 			}
38060Sstevel@tonic-gate 		}
38070Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: O K ..\n"));
38080Sstevel@tonic-gate 
38090Sstevel@tonic-gate 		/*
38100Sstevel@tonic-gate 		 * Make sure the last byte is received well by the
38110Sstevel@tonic-gate 		 * controller. On faster CPU, it may still be busy
38120Sstevel@tonic-gate 		 * by the time another command comes here.
38130Sstevel@tonic-gate 		 */
38140Sstevel@tonic-gate 		for (to = FD_CRETRY; to; to--) {
38150Sstevel@tonic-gate 			if ((Msr(fdc) & (DIO|RQM)) == RQM)
38160Sstevel@tonic-gate 				break;
38170Sstevel@tonic-gate 			}
38180Sstevel@tonic-gate 		if (to == 0) {
38190Sstevel@tonic-gate 			csb->csb_cmdstat = 1;
38200Sstevel@tonic-gate 			return (EIO);
38210Sstevel@tonic-gate 		}
38220Sstevel@tonic-gate 
38230Sstevel@tonic-gate 		/*
38240Sstevel@tonic-gate 		 * An operation that has no results isn't doing DMA so,
38250Sstevel@tonic-gate 		 * there is no reason to try to unbind a handle
38260Sstevel@tonic-gate 		 */
38270Sstevel@tonic-gate 		return (0);
38280Sstevel@tonic-gate 	}
38290Sstevel@tonic-gate 
38300Sstevel@tonic-gate 	/*
38310Sstevel@tonic-gate 	 * If this operation has no interrupt AND an immediate result
38320Sstevel@tonic-gate 	 * then we just busy wait for the results and stuff them into
38330Sstevel@tonic-gate 	 * the csb
38340Sstevel@tonic-gate 	 */
38350Sstevel@tonic-gate 	if (csb->csb_opflags & CSB_OFIMMEDIATE) {
38360Sstevel@tonic-gate 		to = FD_RRETRY;
38370Sstevel@tonic-gate 		csb->csb_nrslts = 0;
38380Sstevel@tonic-gate 		/*
38390Sstevel@tonic-gate 		 * Wait while this command is still going on.
38400Sstevel@tonic-gate 		 */
38410Sstevel@tonic-gate 		while ((tmp = Msr(fdc)) & CB) {
38420Sstevel@tonic-gate 			/*
38430Sstevel@tonic-gate 			 * If RQM + DIO, then a result byte is at hand.
38440Sstevel@tonic-gate 			 */
38450Sstevel@tonic-gate 			if ((tmp & (RQM|DIO|CB)) == (RQM|DIO|CB)) {
38460Sstevel@tonic-gate 				csb->csb_rslt[csb->csb_nrslts++] =
3847*7656SSherry.Moore@Sun.COM 				    Fifo(fdc);
38480Sstevel@tonic-gate 				/*
38490Sstevel@tonic-gate 				 * FDERRPRINT(FDEP_L4, FDEM_EXEC,
38500Sstevel@tonic-gate 				 *    (C, "fdexec: got result 0x%x\n",
38510Sstevel@tonic-gate 				 *    csb->csb_nrslts));
38520Sstevel@tonic-gate 				 */
38530Sstevel@tonic-gate 			} else if (--to == 0) {
38540Sstevel@tonic-gate 				FDERRPRINT(FDEP_L4, FDEM_EXEC,
38550Sstevel@tonic-gate 				    (C, "fdexec: timeout, Msr%x, nr%x\n",
38560Sstevel@tonic-gate 				    Msr(fdc), csb->csb_nrslts));
38570Sstevel@tonic-gate 
38580Sstevel@tonic-gate 				csb->csb_status = 2;
38590Sstevel@tonic-gate 				if (fdc->c_fdtype & FDCTYPE_82077) {
38600Sstevel@tonic-gate 					if (fdc->c_mtimeid == 0) {
38610Sstevel@tonic-gate 						fdc->c_mtimeid = timeout(
3862*7656SSherry.Moore@Sun.COM 						    fdmotoff, a, Motoff_delay);
38630Sstevel@tonic-gate 					}
38640Sstevel@tonic-gate 				}
38650Sstevel@tonic-gate 				/*
38660Sstevel@tonic-gate 				 * There is no DMA happening.  No need to
38670Sstevel@tonic-gate 				 * try freeing a handle.
38680Sstevel@tonic-gate 				 */
38690Sstevel@tonic-gate 
38700Sstevel@tonic-gate 				return (EIO);
38710Sstevel@tonic-gate 			}
38720Sstevel@tonic-gate 		}
38730Sstevel@tonic-gate 	}
38740Sstevel@tonic-gate 
38750Sstevel@tonic-gate 	/*
38760Sstevel@tonic-gate 	 * If told to sleep here, well then sleep!
38770Sstevel@tonic-gate 	 */
38780Sstevel@tonic-gate 
38790Sstevel@tonic-gate 	if (flags & FDXC_SLEEP) {
38800Sstevel@tonic-gate 		fdc->c_flags |= FDCFLG_WAITING;
38810Sstevel@tonic-gate 		while (fdc->c_flags & FDCFLG_WAITING) {
38820Sstevel@tonic-gate 			cv_wait(&fdc->c_iocv, &fdc->c_lolock);
38830Sstevel@tonic-gate 		}
38840Sstevel@tonic-gate 	}
38850Sstevel@tonic-gate 
38860Sstevel@tonic-gate 	/*
38870Sstevel@tonic-gate 	 * kludge for end-of-cylinder error which must be ignored!!!
38880Sstevel@tonic-gate 	 */
38890Sstevel@tonic-gate 
38900Sstevel@tonic-gate 	if ((fdc->c_fdtype & FDCTYPE_TCBUG) &&
38910Sstevel@tonic-gate 	    ((csb->csb_rslt[0] & IC_SR0) == 0x40) &&
38920Sstevel@tonic-gate 	    (csb->csb_rslt[1] & EN_SR1))
38930Sstevel@tonic-gate 		csb->csb_rslt[0] &= ~IC_SR0;
38940Sstevel@tonic-gate 
38950Sstevel@tonic-gate 	/*
38960Sstevel@tonic-gate 	 * See if there was an error detected, if so, fdrecover()
38970Sstevel@tonic-gate 	 * will check it out and say what to do.
38980Sstevel@tonic-gate 	 *
38990Sstevel@tonic-gate 	 * Don't do this, though, if this was the Sense Drive Status
39000Sstevel@tonic-gate 	 * or the Dump Registers command.
39010Sstevel@tonic-gate 	 */
39020Sstevel@tonic-gate 	if (((csb->csb_rslt[0] & IC_SR0) || (fdc->c_csb.csb_dcsr_rslt) ||
3903*7656SSherry.Moore@Sun.COM 	    (csb->csb_status)) &&
3904*7656SSherry.Moore@Sun.COM 	    ((csb->csb_cmds[0] != FDRAW_SENSE_DRV) &&
3905*7656SSherry.Moore@Sun.COM 	    (csb->csb_cmds[0] != DUMPREG))) {
39060Sstevel@tonic-gate 		/* if it can restarted OK, then do so, else return error */
39070Sstevel@tonic-gate 		if (fdrecover(fdc) != 0) {
39080Sstevel@tonic-gate 			if (fdc->c_fdtype & FDCTYPE_82077) {
39090Sstevel@tonic-gate 				if (fdc->c_mtimeid == 0) {
39100Sstevel@tonic-gate 					fdc->c_mtimeid = timeout(fdmotoff,
3911*7656SSherry.Moore@Sun.COM 					    a, Motoff_delay);
39120Sstevel@tonic-gate 				}
39130Sstevel@tonic-gate 			}
39140Sstevel@tonic-gate 
39150Sstevel@tonic-gate 			/*
39160Sstevel@tonic-gate 			 * If this was a dma transfer, unbind the handle so
39170Sstevel@tonic-gate 			 * that other transfers may use it.
39180Sstevel@tonic-gate 			 */
39190Sstevel@tonic-gate 
39200Sstevel@tonic-gate 			(void) fd_unbind_handle(fdc);
39210Sstevel@tonic-gate 			return (EIO);
39220Sstevel@tonic-gate 		} else {
39230Sstevel@tonic-gate 			/* ASSUMES that cmd is still intact in csb */
39240Sstevel@tonic-gate 			goto retry;
39250Sstevel@tonic-gate 		}
39260Sstevel@tonic-gate 	}
39270Sstevel@tonic-gate 
39280Sstevel@tonic-gate 	/* things went ok */
39290Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_82077) {
39300Sstevel@tonic-gate 		if (fdc->c_mtimeid == 0) {
39310Sstevel@tonic-gate 			fdc->c_mtimeid = timeout(fdmotoff, a, Motoff_delay);
39320Sstevel@tonic-gate 		}
39330Sstevel@tonic-gate 	}
39340Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: O K ..........\n"));
39350Sstevel@tonic-gate 
39360Sstevel@tonic-gate 	if (fd_unbind_handle(fdc))
39370Sstevel@tonic-gate 		return (EIO);
39380Sstevel@tonic-gate 
39390Sstevel@tonic-gate 	return (0);
39400Sstevel@tonic-gate }
39410Sstevel@tonic-gate 
39420Sstevel@tonic-gate /*
39430Sstevel@tonic-gate  * Turn on the drive's motor
39440Sstevel@tonic-gate  *
39450Sstevel@tonic-gate  *	- called with the low level lock held
39460Sstevel@tonic-gate  */
39470Sstevel@tonic-gate static void
39480Sstevel@tonic-gate fdexec_turn_on_motor(struct fdctlr *fdc, int flags,  uint_t unit)
39490Sstevel@tonic-gate {
39500Sstevel@tonic-gate 	clock_t local_lbolt;
39510Sstevel@tonic-gate 	timeout_id_t timeid;
39520Sstevel@tonic-gate 
39530Sstevel@tonic-gate 	/*
39540Sstevel@tonic-gate 	 * The low level mutex may not be held over the call to
39550Sstevel@tonic-gate 	 * untimeout().  See the manpage for details.
39560Sstevel@tonic-gate 	 */
39570Sstevel@tonic-gate 	timeid = fdc->c_mtimeid;
39580Sstevel@tonic-gate 	fdc->c_mtimeid = 0;
39590Sstevel@tonic-gate 	if (timeid) {
39600Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
39610Sstevel@tonic-gate 		(void) untimeout(timeid);
39620Sstevel@tonic-gate 		mutex_enter(&fdc->c_lolock);
39630Sstevel@tonic-gate 	}
39640Sstevel@tonic-gate 
39650Sstevel@tonic-gate 	ASSERT(fdc->c_un->un_unit_no == unit);
39660Sstevel@tonic-gate 
39670Sstevel@tonic-gate 
39680Sstevel@tonic-gate 	set_rotational_speed(fdc, unit);
39690Sstevel@tonic-gate 
39700Sstevel@tonic-gate 	if (!(Dor(fdc) & (MOTEN(unit)))) {
39710Sstevel@tonic-gate 		/*
39720Sstevel@tonic-gate 		 * Turn on the motor
39730Sstevel@tonic-gate 		 */
39740Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_EXEC,
3975*7656SSherry.Moore@Sun.COM 		    (C, "fdexec: turning on motor\n"));
39760Sstevel@tonic-gate 
39770Sstevel@tonic-gate 		/* LINTED */
39780Sstevel@tonic-gate 		Set_dor(fdc, (MOTEN(unit)), 1);
39790Sstevel@tonic-gate 
39800Sstevel@tonic-gate 		if (flags & FDXC_SLEEP) {
39810Sstevel@tonic-gate 			local_lbolt = ddi_get_lbolt();
39820Sstevel@tonic-gate 			(void) cv_timedwait(&fdc->c_motoncv,
39830Sstevel@tonic-gate 			    &fdc->c_lolock, local_lbolt + Moton_delay);
39840Sstevel@tonic-gate 		} else {
39850Sstevel@tonic-gate 			drv_usecwait(1000000);
39860Sstevel@tonic-gate 		}
39870Sstevel@tonic-gate 	}
39880Sstevel@tonic-gate 
39890Sstevel@tonic-gate }
39900Sstevel@tonic-gate 
39910Sstevel@tonic-gate /*
39920Sstevel@tonic-gate  * fdrecover
39930Sstevel@tonic-gate  *	see if possible to retry an operation.
39940Sstevel@tonic-gate  *	All we can do is restart the operation.	 If we are out of allowed
39950Sstevel@tonic-gate  *	retries - return non-zero so that the higher levels will be notified.
39960Sstevel@tonic-gate  *
39970Sstevel@tonic-gate  * RETURNS: 0 if ok to restart, !0 if can't or out of retries
39980Sstevel@tonic-gate  *	- called with the low level lock held
39990Sstevel@tonic-gate  */
40000Sstevel@tonic-gate static int
40010Sstevel@tonic-gate fdrecover(struct fdctlr *fdc)
40020Sstevel@tonic-gate {
40030Sstevel@tonic-gate 	struct fdcsb *csb;
40040Sstevel@tonic-gate 
40050Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_RECO, (C, "fdrecover\n"));
40060Sstevel@tonic-gate 	csb = &fdc->c_csb;
40070Sstevel@tonic-gate 
40080Sstevel@tonic-gate 	if (fdc->c_flags & FDCFLG_TIMEDOUT) {
40090Sstevel@tonic-gate 		struct fdcsb savecsb;
40100Sstevel@tonic-gate 
40110Sstevel@tonic-gate 		fdc->c_flags ^= FDCFLG_TIMEDOUT;
40120Sstevel@tonic-gate 		csb->csb_rslt[1] |= TO_SR1;
40130Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_RECO,
40140Sstevel@tonic-gate 		    (C, "fd%d: %s timed out\n", csb->csb_unit,
40150Sstevel@tonic-gate 		    fdcmds[csb->csb_cmds[0] & 0x1f].cmdname));
40160Sstevel@tonic-gate 
40170Sstevel@tonic-gate 		/* use private csb */
40180Sstevel@tonic-gate 		savecsb = fdc->c_csb;
40190Sstevel@tonic-gate 		bzero(&fdc->c_csb, sizeof (struct fdcsb));
40200Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_RECO, (C, "fdc: resetting\n"));
40210Sstevel@tonic-gate 
40220Sstevel@tonic-gate 		(void) fdreset(fdc);
40230Sstevel@tonic-gate 
40240Sstevel@tonic-gate 		if (fdc->c_fdtype & FDCTYPE_DMA) {
40250Sstevel@tonic-gate 			mutex_enter(&fdc->c_hilock);
40260Sstevel@tonic-gate 			/* Reset the DMA engine as well */
40270Sstevel@tonic-gate 			reset_dma_controller(fdc);
40280Sstevel@tonic-gate 			set_dma_control_register(fdc, DCSR_INIT_BITS);
40290Sstevel@tonic-gate 			mutex_exit(&fdc->c_hilock);
40300Sstevel@tonic-gate 		}
40310Sstevel@tonic-gate 
40320Sstevel@tonic-gate 
40330Sstevel@tonic-gate 		/* check change first?? */
40340Sstevel@tonic-gate 		/* don't ckchg in fdexec, too convoluted */
40350Sstevel@tonic-gate 		(void) fdrecalseek(fdc, savecsb.csb_unit, -1, 0);
40360Sstevel@tonic-gate 		fdc->c_csb = savecsb; /* restore original csb */
40370Sstevel@tonic-gate 	}
40380Sstevel@tonic-gate 
40390Sstevel@tonic-gate 	/*
40400Sstevel@tonic-gate 	 * gather statistics on errors
40410Sstevel@tonic-gate 	 */
40420Sstevel@tonic-gate 	if (csb->csb_rslt[1] & DE_SR1) {
40430Sstevel@tonic-gate 		fdc->fdstats.de++;
40440Sstevel@tonic-gate 	}
40450Sstevel@tonic-gate 	if (csb->csb_rslt[1] & OR_SR1) {
40460Sstevel@tonic-gate 		fdc->fdstats.run++;
40470Sstevel@tonic-gate 	}
40480Sstevel@tonic-gate 	if (csb->csb_rslt[1] & (ND_SR1+MA_SR1)) {
40490Sstevel@tonic-gate 		fdc->fdstats.bfmt++;
40500Sstevel@tonic-gate 	}
40510Sstevel@tonic-gate 	if (csb->csb_rslt[1] & TO_SR1) {
40520Sstevel@tonic-gate 		fdc->fdstats.to++;
40530Sstevel@tonic-gate 	}
40540Sstevel@tonic-gate 
40550Sstevel@tonic-gate 	/*
40560Sstevel@tonic-gate 	 * If raw ioctl don't examine results just pass status
40570Sstevel@tonic-gate 	 * back via fdraw. Raw commands are timed too, so put this
40580Sstevel@tonic-gate 	 * after the above check.
40590Sstevel@tonic-gate 	 */
40600Sstevel@tonic-gate 	if (csb->csb_opflags & CSB_OFRAWIOCTL) {
40610Sstevel@tonic-gate 		return (1);
40620Sstevel@tonic-gate 	}
40630Sstevel@tonic-gate 
40640Sstevel@tonic-gate 
40650Sstevel@tonic-gate 	/*
40660Sstevel@tonic-gate 	 * if there was a pci bus error, do not retry
40670Sstevel@tonic-gate 	 */
40680Sstevel@tonic-gate 
4069*7656SSherry.Moore@Sun.COM 		if (csb->csb_dcsr_rslt == 1) {
40700Sstevel@tonic-gate 			FDERRPRINT(FDEP_L3, FDEM_RECO,
40710Sstevel@tonic-gate 			    (C, "fd%d: host bus error\n", 0));
40720Sstevel@tonic-gate 		return (1);
4073*7656SSherry.Moore@Sun.COM 		}
40740Sstevel@tonic-gate 
40750Sstevel@tonic-gate 	/*
40760Sstevel@tonic-gate 	 * If there was an error with the DMA functions, do not retry
40770Sstevel@tonic-gate 	 */
40780Sstevel@tonic-gate 	if (csb->csb_dma_rslt == 1) {
40790Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_RECO,
40800Sstevel@tonic-gate 			    (C, "fd%d: DMA interface error\n", csb->csb_unit));
40810Sstevel@tonic-gate 		return (1);
40820Sstevel@tonic-gate 	}
40830Sstevel@tonic-gate 
40840Sstevel@tonic-gate 
40850Sstevel@tonic-gate 	/*
40860Sstevel@tonic-gate 	 * if we have run out of retries, return an error
40870Sstevel@tonic-gate 	 * XXX need better status interp
40880Sstevel@tonic-gate 	 */
40890Sstevel@tonic-gate 
40900Sstevel@tonic-gate 	csb->csb_retrys++;
40910Sstevel@tonic-gate 	if (csb->csb_retrys > csb->csb_maxretry) {
40920Sstevel@tonic-gate 		FDERRPRINT(FDEP_L3, FDEM_RECO,
40930Sstevel@tonic-gate 		    (C, "fd%d: %s failed (%x %x %x)\n",
40940Sstevel@tonic-gate 		    0, fdcmds[csb->csb_cmds[0] & 0x1f].cmdname,
40950Sstevel@tonic-gate 		    csb->csb_rslt[0], csb->csb_rslt[1], csb->csb_rslt[2]));
40960Sstevel@tonic-gate 		if (csb->csb_rslt[1] & NW_SR1) {
40970Sstevel@tonic-gate 			FDERRPRINT(FDEP_L3, FDEM_RECO,
40980Sstevel@tonic-gate 			    (C, "fd%d: not writable\n", 0));
40990Sstevel@tonic-gate 		}
41000Sstevel@tonic-gate 		if (csb->csb_rslt[1] & DE_SR1) {
41010Sstevel@tonic-gate 			FDERRPRINT(FDEP_L3, FDEM_RECO,
41020Sstevel@tonic-gate 			    (C, "fd%d: crc error blk %d\n", 0,
41030Sstevel@tonic-gate 			    (int)fdc->c_current->b_blkno));
41040Sstevel@tonic-gate 		}
41050Sstevel@tonic-gate 		if (csb->csb_rslt[1] & OR_SR1) {
41060Sstevel@tonic-gate 			if (fdc->c_fdtype & FDCTYPE_SB) {
41070Sstevel@tonic-gate 				/*
41080Sstevel@tonic-gate 				 * When using southbridge chip we need to
41090Sstevel@tonic-gate 				 * retry atleast 10 times to shake off the
41100Sstevel@tonic-gate 				 * underrun err.
41110Sstevel@tonic-gate 				 */
41120Sstevel@tonic-gate 				if (csb->csb_retrys <= rwretry)
41130Sstevel@tonic-gate 					return (0);
41140Sstevel@tonic-gate 			}
41150Sstevel@tonic-gate 			FDERRPRINT(FDEP_L3, FDEM_RECO,
41160Sstevel@tonic-gate 			    (C, "fd%d: over/underrun\n", 0));
41170Sstevel@tonic-gate 		}
41180Sstevel@tonic-gate 
41190Sstevel@tonic-gate 		if (csb->csb_rslt[1] & (ND_SR1+MA_SR1)) {
41200Sstevel@tonic-gate 			FDERRPRINT(FDEP_L3, FDEM_RECO,
41210Sstevel@tonic-gate 			    (C, "fd%d: bad format\n", 0));
41220Sstevel@tonic-gate 		}
41230Sstevel@tonic-gate 
41240Sstevel@tonic-gate 		if (csb->csb_rslt[1] & TO_SR1) {
41250Sstevel@tonic-gate 			FDERRPRINT(FDEP_L3, FDEM_RECO,
41260Sstevel@tonic-gate 			    (C, "fd%d: timeout\n", 0));
41270Sstevel@tonic-gate 		}
41280Sstevel@tonic-gate 
41290Sstevel@tonic-gate 		csb->csb_cmdstat = 1; /* failed - give up */
41300Sstevel@tonic-gate 		return (1);
41310Sstevel@tonic-gate 	}
41320Sstevel@tonic-gate 
41330Sstevel@tonic-gate 	if (csb->csb_opflags & CSB_OFSEEKOPS) {
41340Sstevel@tonic-gate 		/* seek, recal type commands - just look at st0 */
41350Sstevel@tonic-gate 		FDERRPRINT(FDEP_L2, FDEM_RECO,
41360Sstevel@tonic-gate 		    (C, "fd%d: %s error : st0 0x%x\n", csb->csb_unit,
41370Sstevel@tonic-gate 		    fdcmds[csb->csb_cmds[0] & 0x1f].cmdname,
41380Sstevel@tonic-gate 		    csb->csb_rslt[0]));
41390Sstevel@tonic-gate 	}
41400Sstevel@tonic-gate 	if (csb->csb_opflags & CSB_OFXFEROPS) {
41410Sstevel@tonic-gate 		/* rd, wr, fmt type commands - look at st0, st1, st2 */
41420Sstevel@tonic-gate 		FDERRPRINT(FDEP_L2, FDEM_RECO,
41430Sstevel@tonic-gate 		    (C, "fd%d: %s error : st0=0x%x st1=0x%x st2=0x%x\n",
41440Sstevel@tonic-gate 		    csb->csb_unit, fdcmds[csb->csb_cmds[0] & 0x1f].cmdname,
41450Sstevel@tonic-gate 		    csb->csb_rslt[0], csb->csb_rslt[1], csb->csb_rslt[2]));
41460Sstevel@tonic-gate 	}
41470Sstevel@tonic-gate 
41480Sstevel@tonic-gate 	return (0);	/* tell fdexec to retry */
41490Sstevel@tonic-gate }
41500Sstevel@tonic-gate 
41510Sstevel@tonic-gate /*
41520Sstevel@tonic-gate  * Interrupt handle for DMA
41530Sstevel@tonic-gate  */
41540Sstevel@tonic-gate 
41550Sstevel@tonic-gate static uint_t
41560Sstevel@tonic-gate fdintr_dma()
41570Sstevel@tonic-gate {
41580Sstevel@tonic-gate 	struct fdctlr   *fdc;
41590Sstevel@tonic-gate 	off_t		off;
41600Sstevel@tonic-gate 	size_t		len;
41610Sstevel@tonic-gate 	uint_t		ccount;
41620Sstevel@tonic-gate 	uint_t		windex;
41630Sstevel@tonic-gate 	uint_t		done = 0;
41640Sstevel@tonic-gate 	int		tmp_dcsr;
41650Sstevel@tonic-gate 	int		to;
41660Sstevel@tonic-gate 	uchar_t		tmp;
41670Sstevel@tonic-gate 	int		i = 0;
41680Sstevel@tonic-gate 	int		res = DDI_INTR_UNCLAIMED;
41690Sstevel@tonic-gate 	int		not_cheerio = 1;
41700Sstevel@tonic-gate 
41710Sstevel@tonic-gate 	/* search for a controller that's expecting an interrupt */
41720Sstevel@tonic-gate 	fdc = fdctlrs;
41730Sstevel@tonic-gate 
41740Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
41750Sstevel@tonic-gate 		tmp_dcsr = get_dma_control_register(fdc);
41760Sstevel@tonic-gate 		if (!(tmp_dcsr & DCSR_INT_PEND) && !(DCSR_ERR_PEND & tmp_dcsr))
41770Sstevel@tonic-gate 			return (res);
41780Sstevel@tonic-gate 		not_cheerio = 0;
41790Sstevel@tonic-gate 	}
41800Sstevel@tonic-gate 
41810Sstevel@tonic-gate 	mutex_enter(&fdc->c_hilock);
41820Sstevel@tonic-gate 
41830Sstevel@tonic-gate 	if (fdc->c_csb.csb_opmode == 0x0) {
41840Sstevel@tonic-gate 		fdc->c_csb.csb_opmode = 2;
41850Sstevel@tonic-gate 	}
41860Sstevel@tonic-gate 	if (fdc->sb_dma_lock) {
41870Sstevel@tonic-gate 		release_sb_dma(fdc);
41880Sstevel@tonic-gate 	}
41890Sstevel@tonic-gate 
41900Sstevel@tonic-gate 	/*
41910Sstevel@tonic-gate 	 * An interrupt can come from either the floppy controller or
41920Sstevel@tonic-gate 	 * or the DMA engine.  The DMA engine will only issue an
41930Sstevel@tonic-gate 	 * interrupt if there was an error.
41940Sstevel@tonic-gate 	 */
41950Sstevel@tonic-gate 
41960Sstevel@tonic-gate 	switch (fdc->c_csb.csb_opmode) {
41970Sstevel@tonic-gate 		case 0x1:
41980Sstevel@tonic-gate 			/* read/write/format data-xfer case */
41990Sstevel@tonic-gate 
42000Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_INTR,
4201*7656SSherry.Moore@Sun.COM 			    (C, "fdintr_dma: opmode 1\n"));
42020Sstevel@tonic-gate 
42030Sstevel@tonic-gate 			/*
42040Sstevel@tonic-gate 			 * See if the interrupt is from the floppy
42050Sstevel@tonic-gate 			 * controller.  If there is, take out the status bytes.
42060Sstevel@tonic-gate 			 */
42070Sstevel@tonic-gate 
42080Sstevel@tonic-gate 			if (not_cheerio || (tmp_dcsr & DCSR_INT_PEND)) {
42090Sstevel@tonic-gate 
42100Sstevel@tonic-gate 				FDERRPRINT(FDEP_L1, FDEM_INTR,
4211*7656SSherry.Moore@Sun.COM 				    (C, "fdintr_dma: INT_PEND \n"));
42120Sstevel@tonic-gate 
42130Sstevel@tonic-gate 				res = DDI_INTR_CLAIMED;
42140Sstevel@tonic-gate 
42150Sstevel@tonic-gate 				to = FD_RRETRY;
42160Sstevel@tonic-gate 				fdc->c_csb.csb_nrslts = 0;
42170Sstevel@tonic-gate 
42180Sstevel@tonic-gate 				/* check status */
42190Sstevel@tonic-gate 				i = 0;
42200Sstevel@tonic-gate 
42210Sstevel@tonic-gate 				/*
42220Sstevel@tonic-gate 				 * CB turns off once all the result bytes are
42230Sstevel@tonic-gate 				 *  read.
42240Sstevel@tonic-gate 				 *
42250Sstevel@tonic-gate 				 * NOTE: the counters are there so that the
42260Sstevel@tonic-gate 				 * handler will never get stuck in a loop.
42270Sstevel@tonic-gate 				 * If the counters do reach their maximum
42280Sstevel@tonic-gate 				 * values, then a catastrophic error has
42290Sstevel@tonic-gate 				 * occurred.  This should never be the case.
42300Sstevel@tonic-gate 				 * The counters only came into play during
42310Sstevel@tonic-gate 				 * development.
42320Sstevel@tonic-gate 				 */
42330Sstevel@tonic-gate 				while (((tmp = Msr(fdc)) & CB) &&
4234*7656SSherry.Moore@Sun.COM 				    (i < 1000001)) {
42350Sstevel@tonic-gate 
42360Sstevel@tonic-gate 					/*
42370Sstevel@tonic-gate 					 * If RQM + DIO, then a result byte
42380Sstevel@tonic-gate 					 * is at hand.
42390Sstevel@tonic-gate 					 */
42400Sstevel@tonic-gate 					if ((tmp & (RQM|DIO|CB)) ==
4241*7656SSherry.Moore@Sun.COM 					    (RQM|DIO|CB)) {
42420Sstevel@tonic-gate 						fdc->c_csb.csb_rslt
4243*7656SSherry.Moore@Sun.COM 						    [fdc->c_csb.csb_nrslts++]
4244*7656SSherry.Moore@Sun.COM 						    = Fifo(fdc);
42450Sstevel@tonic-gate 
42460Sstevel@tonic-gate 						FDERRPRINT(FDEP_L1, FDEM_INTR,
4247*7656SSherry.Moore@Sun.COM 						    (C,
4248*7656SSherry.Moore@Sun.COM 						    "fdintr_dma: res 0x%x\n",
4249*7656SSherry.Moore@Sun.COM 						    fdc->c_csb.csb_rslt
4250*7656SSherry.Moore@Sun.COM 						    [fdc->c_csb.csb_nrslts
4251*7656SSherry.Moore@Sun.COM 						    - 1]));
42520Sstevel@tonic-gate 
42530Sstevel@tonic-gate 					} else if (--to == 0) {
42540Sstevel@tonic-gate 						/*
42550Sstevel@tonic-gate 						 * controller was never
42560Sstevel@tonic-gate 						 * ready to give results
42570Sstevel@tonic-gate 						 */
42580Sstevel@tonic-gate 						fdc->c_csb.csb_status = 2;
42590Sstevel@tonic-gate 						break;
42600Sstevel@tonic-gate 					}
4261*7656SSherry.Moore@Sun.COM 					i++;
42620Sstevel@tonic-gate 				}
42630Sstevel@tonic-gate 				if (i == 10000) {
42640Sstevel@tonic-gate 					FDERRPRINT(FDEP_L1, FDEM_INTR,
4265*7656SSherry.Moore@Sun.COM 					    (C, "First loop overran\n"));
42660Sstevel@tonic-gate 				}
42670Sstevel@tonic-gate 			}
42680Sstevel@tonic-gate 
42690Sstevel@tonic-gate 			/*
42700Sstevel@tonic-gate 			 * See if the interrupt is from the DMA engine,
42710Sstevel@tonic-gate 			 * which will only interrupt on an error
42720Sstevel@tonic-gate 			 */
42730Sstevel@tonic-gate 			if ((!not_cheerio) && (tmp_dcsr & DCSR_ERR_PEND)) {
42740Sstevel@tonic-gate 
42750Sstevel@tonic-gate 				res = DDI_INTR_CLAIMED;
42760Sstevel@tonic-gate 
42770Sstevel@tonic-gate 				done = 1;
42780Sstevel@tonic-gate 				fdc->c_csb.csb_dcsr_rslt = 1;
42790Sstevel@tonic-gate 				FDERRPRINT(FDEP_L1, FDEM_INTR,
4280*7656SSherry.Moore@Sun.COM 				    (C, "fdintr_dma: Error pending\n"));
42810Sstevel@tonic-gate 				reset_dma_controller(fdc);
42820Sstevel@tonic-gate 				set_dma_control_register(fdc, DCSR_INIT_BITS);
42830Sstevel@tonic-gate 				break;
42840Sstevel@tonic-gate 			}
42850Sstevel@tonic-gate 
42860Sstevel@tonic-gate 			/* TCBUG kludge */
42870Sstevel@tonic-gate 			if ((fdc->c_fdtype & FDCTYPE_TCBUG) &&
4288*7656SSherry.Moore@Sun.COM 			    ((fdc->c_csb.csb_rslt[0] & IC_SR0) == 0x40) &&
4289*7656SSherry.Moore@Sun.COM 			    (fdc->c_csb.csb_rslt[1] & EN_SR1)) {
42900Sstevel@tonic-gate 
42910Sstevel@tonic-gate 				fdc->c_csb.csb_rslt[0] &= ~IC_SR0;
42920Sstevel@tonic-gate 
42930Sstevel@tonic-gate 				fdc->c_csb.csb_rslt[1] &= ~EN_SR1;
42940Sstevel@tonic-gate 
42950Sstevel@tonic-gate 
42960Sstevel@tonic-gate 			}
42970Sstevel@tonic-gate 
42980Sstevel@tonic-gate 
42990Sstevel@tonic-gate 			/* Exit if there were errors in the DMA */
43000Sstevel@tonic-gate 			if (((fdc->c_csb.csb_rslt[0] & IC_SR0) != 0) ||
43010Sstevel@tonic-gate 			    (fdc->c_csb.csb_rslt[1] != 0) ||
43020Sstevel@tonic-gate 			    (fdc->c_csb.csb_rslt[2] != 0)) {
43030Sstevel@tonic-gate 				done = 1;
43040Sstevel@tonic-gate 				FDERRPRINT(FDEP_L1, FDEM_INTR,
4305*7656SSherry.Moore@Sun.COM 				    (C, "fdintr_dma: errors in command\n"));
43060Sstevel@tonic-gate 
43070Sstevel@tonic-gate 
43080Sstevel@tonic-gate 				break;
43090Sstevel@tonic-gate 			}
43100Sstevel@tonic-gate 
43110Sstevel@tonic-gate 
43120Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_INTR,
4313*7656SSherry.Moore@Sun.COM 			    (C, "fdintr_dma: dbcr 0x%x\n",
4314*7656SSherry.Moore@Sun.COM 			    get_data_count_register(fdc)));
43150Sstevel@tonic-gate 			/*
43160Sstevel@tonic-gate 			 * The csb_ccount is the number of cookies that still
43170Sstevel@tonic-gate 			 * need to be processed.  A cookie was just processed
43180Sstevel@tonic-gate 			 * so decrement the cookie counter.
43190Sstevel@tonic-gate 			 */
43200Sstevel@tonic-gate 			if (fdc->c_csb.csb_ccount == 0) {
43210Sstevel@tonic-gate 				done = 1;
43220Sstevel@tonic-gate 				break;
43230Sstevel@tonic-gate 			}
43240Sstevel@tonic-gate 			fdc->c_csb.csb_ccount--;
43250Sstevel@tonic-gate 			ccount = fdc->c_csb.csb_ccount;
43260Sstevel@tonic-gate 
43270Sstevel@tonic-gate 			windex = fdc->c_csb.csb_windex;
43280Sstevel@tonic-gate 
43290Sstevel@tonic-gate 			/*
43300Sstevel@tonic-gate 			 * If there are no more cookies and all the windows
43310Sstevel@tonic-gate 			 * have been DMA'd, then DMA is done.
43320Sstevel@tonic-gate 			 *
43330Sstevel@tonic-gate 			 */
43340Sstevel@tonic-gate 			if ((ccount == 0) && (windex == fdc->c_csb.csb_nwin)) {
43350Sstevel@tonic-gate 
43360Sstevel@tonic-gate 				done = 1;
43370Sstevel@tonic-gate 
43380Sstevel@tonic-gate 				/*
43390Sstevel@tonic-gate 				 * The handle is unbound in fdexec
43400Sstevel@tonic-gate 				 */
43410Sstevel@tonic-gate 
43420Sstevel@tonic-gate 				break;
43430Sstevel@tonic-gate 			}
43440Sstevel@tonic-gate 
43450Sstevel@tonic-gate 			if (ccount != 0) {
43460Sstevel@tonic-gate 				/* process the next cookie */
43470Sstevel@tonic-gate 				ddi_dma_nextcookie(fdc->c_dmahandle,
43480Sstevel@tonic-gate 				    &fdc->c_csb.csb_dmacookie);
43490Sstevel@tonic-gate 
43500Sstevel@tonic-gate 				FDERRPRINT(FDEP_L1, FDEM_INTR,
43510Sstevel@tonic-gate 				    (C, "cookie addr 0x%" PRIx64 "\n",
43520Sstevel@tonic-gate 				    fdc->c_csb.csb_dmacookie.dmac_laddress));
43530Sstevel@tonic-gate 
43540Sstevel@tonic-gate 				FDERRPRINT(FDEP_L1, FDEM_INTR,
43550Sstevel@tonic-gate 				    (C, "cookie length %lu\n",
43560Sstevel@tonic-gate 				    fdc->c_csb.csb_dmacookie.dmac_size));
43570Sstevel@tonic-gate 
43580Sstevel@tonic-gate 			} else {
43590Sstevel@tonic-gate 
43600Sstevel@tonic-gate 				(void) ddi_dma_getwin(fdc->c_dmahandle,
43610Sstevel@tonic-gate 				    fdc->c_csb.csb_windex,
43620Sstevel@tonic-gate 				    &off, &len,
43630Sstevel@tonic-gate 				    &fdc->c_csb.csb_dmacookie,
43640Sstevel@tonic-gate 				    &fdc->c_csb.csb_ccount);
43650Sstevel@tonic-gate 				fdc->c_csb.csb_windex++;
43660Sstevel@tonic-gate 
43670Sstevel@tonic-gate 				FDERRPRINT(FDEP_L1, FDEM_INTR,
43680Sstevel@tonic-gate 				    (C, "fdintr_dma: process %d window\n",
43690Sstevel@tonic-gate 				    fdc->c_csb.csb_windex));
43700Sstevel@tonic-gate 
43710Sstevel@tonic-gate 				FDERRPRINT(FDEP_L1, FDEM_INTR,
43720Sstevel@tonic-gate 				    (C, "fdintr_dma: process no. cookies %d\n",
43730Sstevel@tonic-gate 				    fdc->c_csb.csb_ccount));
43740Sstevel@tonic-gate 
43750Sstevel@tonic-gate 				FDERRPRINT(FDEP_L1, FDEM_INTR,
43760Sstevel@tonic-gate 				    (C, "cookie addr 0x%" PRIx64 "\n",
43770Sstevel@tonic-gate 				    fdc->c_csb.csb_dmacookie.dmac_laddress));
43780Sstevel@tonic-gate 
43790Sstevel@tonic-gate 				FDERRPRINT(FDEP_L1, FDEM_INTR,
43800Sstevel@tonic-gate 				    (C, "cookie length %lu\n",
43810Sstevel@tonic-gate 				    fdc->c_csb.csb_dmacookie.dmac_size));
43820Sstevel@tonic-gate 			}
43830Sstevel@tonic-gate 
43840Sstevel@tonic-gate 			/*
43850Sstevel@tonic-gate 			 * Program the DMA engine with the length and
43860Sstevel@tonic-gate 			 * the address of the transfer
43870Sstevel@tonic-gate 			 */
43880Sstevel@tonic-gate 
43890Sstevel@tonic-gate 			ASSERT(fdc->c_csb.csb_dmacookie.dmac_size);
43900Sstevel@tonic-gate 
43910Sstevel@tonic-gate 			set_data_count_register(fdc,
4392*7656SSherry.Moore@Sun.COM 			    fdc->c_csb.csb_dmacookie.dmac_size);
43930Sstevel@tonic-gate 			set_data_address_register(fdc,
4394*7656SSherry.Moore@Sun.COM 			    fdc->c_csb.csb_dmacookie.dmac_laddress);
43950Sstevel@tonic-gate 
43960Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_INTR, (C,
43970Sstevel@tonic-gate 			    "fdintr_dma: size 0x%lx\n",
43980Sstevel@tonic-gate 			    fdc->c_csb.csb_dmacookie.dmac_size));
43990Sstevel@tonic-gate 
44000Sstevel@tonic-gate 
44010Sstevel@tonic-gate 			/* reprogram the controller */
44020Sstevel@tonic-gate 			fdc->c_csb.csb_cmds[2] = fdc->c_csb.csb_rslt[3];
44030Sstevel@tonic-gate 			fdc->c_csb.csb_cmds[3] = fdc->c_csb.csb_rslt[4];
44040Sstevel@tonic-gate 			fdc->c_csb.csb_cmds[4] = fdc->c_csb.csb_rslt[5];
44050Sstevel@tonic-gate 			fdc->c_csb.csb_cmds[1] = (fdc->c_csb.csb_cmds[1]
4406*7656SSherry.Moore@Sun.COM 			    & ~0x04) | (fdc->c_csb.csb_rslt[4] << 2);
44070Sstevel@tonic-gate 
44080Sstevel@tonic-gate 			for (i = 0; i < (int)fdc->c_csb.csb_ncmds; i++) {
44090Sstevel@tonic-gate 
44100Sstevel@tonic-gate 				/*
44110Sstevel@tonic-gate 				 * Test the readiness of the controller
44120Sstevel@tonic-gate 				 * to receive the cmd
44130Sstevel@tonic-gate 				 */
44140Sstevel@tonic-gate 				for (to = FD_CRETRY; to; to--) {
44150Sstevel@tonic-gate 					if ((Msr(fdc) & (DIO|RQM)) == RQM)
44160Sstevel@tonic-gate 						break;
44170Sstevel@tonic-gate 				}
44180Sstevel@tonic-gate 				if (to == 0) {
44190Sstevel@tonic-gate 					FDERRPRINT(FDEP_L2, FDEM_EXEC,
4420*7656SSherry.Moore@Sun.COM 					    (C,
4421*7656SSherry.Moore@Sun.COM 					    "fdc: no RQM - stat 0x%x\n",
4422*7656SSherry.Moore@Sun.COM 					    Msr(fdc)));
44230Sstevel@tonic-gate 					/* stop the DMA from happening */
44240Sstevel@tonic-gate 					fdc->c_csb.csb_status = 2;
44250Sstevel@tonic-gate 					done = 1;
44260Sstevel@tonic-gate 					break;
44270Sstevel@tonic-gate 				}
44280Sstevel@tonic-gate 
44290Sstevel@tonic-gate 				Set_Fifo(fdc, fdc->c_csb.csb_cmds[i]);
44300Sstevel@tonic-gate 
44310Sstevel@tonic-gate 				FDERRPRINT(FDEP_L1, FDEM_INTR,
4432*7656SSherry.Moore@Sun.COM 				    (C,
4433*7656SSherry.Moore@Sun.COM 				    "fdintr_dma: sent 0x%x, Msr 0x%x\n",
4434*7656SSherry.Moore@Sun.COM 				    fdc->c_csb.csb_cmds[i], Msr(fdc)));
44350Sstevel@tonic-gate 			}
44360Sstevel@tonic-gate 
44370Sstevel@tonic-gate 			/* reenable DMA */
44380Sstevel@tonic-gate 			if ((!not_cheerio) && (!done))
44390Sstevel@tonic-gate 				set_dma_control_register(fdc, tmp_dcsr |
4440*7656SSherry.Moore@Sun.COM 				    DCSR_EN_DMA);
44410Sstevel@tonic-gate 			break;
44420Sstevel@tonic-gate 
44430Sstevel@tonic-gate 		case 0x2:
44440Sstevel@tonic-gate 		/* seek/recal type cmd */
44450Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_INTR,
4446*7656SSherry.Moore@Sun.COM 			    (C, "fintr_dma: opmode 2\n"));
44470Sstevel@tonic-gate 
44480Sstevel@tonic-gate 			/*
44490Sstevel@tonic-gate 			 *  See if the interrupt is from the DMA engine,
44500Sstevel@tonic-gate 			 *  which will only interrupt if there was an error.
44510Sstevel@tonic-gate 			 */
44520Sstevel@tonic-gate 			if ((!not_cheerio) && (tmp_dcsr & DCSR_ERR_PEND)) {
44530Sstevel@tonic-gate 				res = DDI_INTR_CLAIMED;
44540Sstevel@tonic-gate 				done = 1;
44550Sstevel@tonic-gate 				fdc->c_csb.csb_dcsr_rslt = 1;
44560Sstevel@tonic-gate 				reset_dma_controller(fdc);
44570Sstevel@tonic-gate 				set_dma_control_register(fdc, DCSR_INIT_BITS);
44580Sstevel@tonic-gate 
44590Sstevel@tonic-gate 				break;
44600Sstevel@tonic-gate 			}
44610Sstevel@tonic-gate 
44620Sstevel@tonic-gate 
44630Sstevel@tonic-gate 			/* See if the interrupt is from the floppy controller */
44640Sstevel@tonic-gate 			if (not_cheerio || (tmp_dcsr & DCSR_INT_PEND)) {
44650Sstevel@tonic-gate 
44660Sstevel@tonic-gate 				res = DDI_INTR_CLAIMED;
44670Sstevel@tonic-gate 
44680Sstevel@tonic-gate 
44690Sstevel@tonic-gate 				/*
44700Sstevel@tonic-gate 				 * Wait until there's no longer a command
44710Sstevel@tonic-gate 				 * in progress
44720Sstevel@tonic-gate 				 */
44730Sstevel@tonic-gate 
44740Sstevel@tonic-gate 				FDERRPRINT(FDEP_L1, FDEM_INTR,
4475*7656SSherry.Moore@Sun.COM 				    (C, "fdintr_dma: interrupt pending\n"));
44760Sstevel@tonic-gate 				i = 0;
44770Sstevel@tonic-gate 				while (((Msr(fdc) & CB)) && (i < 10000)) {
44780Sstevel@tonic-gate 					i++;
44790Sstevel@tonic-gate 				}
44800Sstevel@tonic-gate 
44810Sstevel@tonic-gate 				if (i == 10000)
44820Sstevel@tonic-gate 					FDERRPRINT(FDEP_L1, FDEM_INTR,
4483*7656SSherry.Moore@Sun.COM 					    (C, "2nd loop overran !!!\n"));
44840Sstevel@tonic-gate 
44850Sstevel@tonic-gate 				/*
44860Sstevel@tonic-gate 				 * Check the RQM bit to see if the controller is
44870Sstevel@tonic-gate 				 * ready to transfer status of the command.
44880Sstevel@tonic-gate 				 */
44890Sstevel@tonic-gate 				i = 0;
44900Sstevel@tonic-gate 				while ((!(Msr(fdc) & RQM)) && (i < 10000)) {
44910Sstevel@tonic-gate 					i++;
44920Sstevel@tonic-gate 				}
44930Sstevel@tonic-gate 
44940Sstevel@tonic-gate 				if (i == 10000)
44950Sstevel@tonic-gate 					FDERRPRINT(FDEP_L1, FDEM_INTR,
44960Sstevel@tonic-gate 					    (C, "3rd loop overran !!!\n"));
44970Sstevel@tonic-gate 
44980Sstevel@tonic-gate 				/*
44990Sstevel@tonic-gate 				 * Issue the Sense Interrupt Status Command
45000Sstevel@tonic-gate 				 */
45010Sstevel@tonic-gate 				Set_Fifo(fdc, SNSISTAT);
45020Sstevel@tonic-gate 
45030Sstevel@tonic-gate 				i = 0;
45040Sstevel@tonic-gate 				while ((!(Msr(fdc) & RQM)) && (i < 10000)) {
45050Sstevel@tonic-gate 					i++;
45060Sstevel@tonic-gate 				}
45070Sstevel@tonic-gate 				if (i == 10000)
45080Sstevel@tonic-gate 					FDERRPRINT(FDEP_L1, FDEM_INTR,
4509*7656SSherry.Moore@Sun.COM 					    (C, "4th loop overran !!!\n"));
45100Sstevel@tonic-gate 
45110Sstevel@tonic-gate 				/* Store the first result byte */
45120Sstevel@tonic-gate 				fdc->c_csb.csb_rslt[0] = Fifo(fdc);
45130Sstevel@tonic-gate 
45140Sstevel@tonic-gate 				i = 0;
45150Sstevel@tonic-gate 				while ((!(Msr(fdc) & RQM)) && (i < 10000)) {
45160Sstevel@tonic-gate 					i++;
45170Sstevel@tonic-gate 				}
45180Sstevel@tonic-gate 				if (i == 10000)
45190Sstevel@tonic-gate 					FDERRPRINT(FDEP_L1, FDEM_INTR,
4520*7656SSherry.Moore@Sun.COM 					    (C, "5th loop overran !!!\n"));
45210Sstevel@tonic-gate 
45220Sstevel@tonic-gate 				/* Store the second  result byte */
45230Sstevel@tonic-gate 				fdc->c_csb.csb_rslt[1] = Fifo(fdc);
45240Sstevel@tonic-gate 
45250Sstevel@tonic-gate 				done = 1;
45260Sstevel@tonic-gate 			}
45270Sstevel@tonic-gate 
45280Sstevel@tonic-gate 		}
45290Sstevel@tonic-gate 
45300Sstevel@tonic-gate 	/*
45310Sstevel@tonic-gate 	 * We are done with the actual interrupt handling here.
45320Sstevel@tonic-gate 	 * The portion below should be actually be done by fd_lointr().
45330Sstevel@tonic-gate 	 * We should be triggering the fd_lointr here and exiting.
45340Sstevel@tonic-gate 	 * However for want of time this will be done in the next FIX.
45350Sstevel@tonic-gate 	 *
45360Sstevel@tonic-gate 	 * Hence for now we will release hilock only and keep the remaining
45370Sstevel@tonic-gate 	 * code as it is.
45380Sstevel@tonic-gate 	 * Releasing of hilock ensures that we don't hold on to the
45390Sstevel@tonic-gate 	 * lolock and hilock at the same time.
45400Sstevel@tonic-gate 	 * hilock is acquired each time dma related  registers are accessed.
45410Sstevel@tonic-gate 	 */
45420Sstevel@tonic-gate 	mutex_exit(&fdc->c_hilock);
45430Sstevel@tonic-gate 	/* Make signal and get out of interrupt handler */
45440Sstevel@tonic-gate 	if (done) {
45450Sstevel@tonic-gate 		mutex_enter(&fdc->c_lolock);
45460Sstevel@tonic-gate 
45470Sstevel@tonic-gate 		fdc->c_csb.csb_opmode = 0;
45480Sstevel@tonic-gate 
45490Sstevel@tonic-gate 		/*  reset watchdog timer if armed and not already triggered */
45500Sstevel@tonic-gate 
45510Sstevel@tonic-gate 
45520Sstevel@tonic-gate 		if (fdc->c_timeid) {
45530Sstevel@tonic-gate 			timeout_id_t timeid = fdc->c_timeid;
45540Sstevel@tonic-gate 			fdc->c_timeid = 0;
45550Sstevel@tonic-gate 			mutex_exit(&fdc->c_lolock);
45560Sstevel@tonic-gate 			(void) untimeout(timeid);
45570Sstevel@tonic-gate 			mutex_enter(&fdc->c_lolock);
45580Sstevel@tonic-gate 		}
45590Sstevel@tonic-gate 
45600Sstevel@tonic-gate 
45610Sstevel@tonic-gate 		if (fdc->c_flags & FDCFLG_WAITING) {
45620Sstevel@tonic-gate 			/*
45630Sstevel@tonic-gate 			 * somebody's waiting on finish of fdctlr/csb,
45640Sstevel@tonic-gate 			 * wake them
45650Sstevel@tonic-gate 			 */
45660Sstevel@tonic-gate 
45670Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_INTR,
4568*7656SSherry.Moore@Sun.COM 			    (C, "fdintr_dma: signal the waiter\n"));
45690Sstevel@tonic-gate 
45700Sstevel@tonic-gate 			fdc->c_flags ^= FDCFLG_WAITING;
45710Sstevel@tonic-gate 			cv_signal(&fdc->c_iocv);
45720Sstevel@tonic-gate 
45730Sstevel@tonic-gate 			/*
45740Sstevel@tonic-gate 			 * FDCFLG_BUSY is NOT cleared, NOR is the csb given
45750Sstevel@tonic-gate 			 * back; the operation just finished can look at the csb
45760Sstevel@tonic-gate 			 */
45770Sstevel@tonic-gate 		} else {
45780Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_INTR,
4579*7656SSherry.Moore@Sun.COM 			    (C, "fdintr_dma: nobody sleeping (%x %x %x)\n",
4580*7656SSherry.Moore@Sun.COM 			    fdc->c_csb.csb_rslt[0], fdc->c_csb.csb_rslt[1],
4581*7656SSherry.Moore@Sun.COM 			    fdc->c_csb.csb_rslt[2]));
45820Sstevel@tonic-gate 		}
45830Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
45840Sstevel@tonic-gate 	}
45850Sstevel@tonic-gate 	/* update high level interrupt counter */
45860Sstevel@tonic-gate 	if (fdc->c_intrstat)
4587*7656SSherry.Moore@Sun.COM 		KIOIP->intrs[KSTAT_INTR_HARD]++;
45880Sstevel@tonic-gate 
45890Sstevel@tonic-gate 
45900Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_INTR, (C, "fdintr_dma: done\n"));
45910Sstevel@tonic-gate 	return (res);
45920Sstevel@tonic-gate }
45930Sstevel@tonic-gate 
45940Sstevel@tonic-gate /*
45950Sstevel@tonic-gate  * fd_lointr
45960Sstevel@tonic-gate  *	This is the low level SW interrupt handler triggered by the high
45970Sstevel@tonic-gate  *	level interrupt handler (or by fdwatch).
45980Sstevel@tonic-gate  */
45990Sstevel@tonic-gate static uint_t
46000Sstevel@tonic-gate fd_lointr(caddr_t arg)
46010Sstevel@tonic-gate {
46020Sstevel@tonic-gate 	struct fdctlr *fdc = (struct fdctlr *)arg;
46030Sstevel@tonic-gate 	struct fdcsb *csb;
46040Sstevel@tonic-gate 
46050Sstevel@tonic-gate 	csb = &fdc->c_csb;
46060Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_INTR, (C, "fdintr: opmode %d\n",
46070Sstevel@tonic-gate 	    csb->csb_opmode));
46080Sstevel@tonic-gate 	/*
46090Sstevel@tonic-gate 	 * Check that lowlevel interrupt really meant to trigger us.
46100Sstevel@tonic-gate 	 */
46110Sstevel@tonic-gate 	if (csb->csb_opmode != 4) {
46120Sstevel@tonic-gate 		/*
46130Sstevel@tonic-gate 		 * This should probably be protected, but, what the
46140Sstevel@tonic-gate 		 * heck...the cost isn't worth the accuracy for this
46150Sstevel@tonic-gate 		 * statistic.
46160Sstevel@tonic-gate 		 */
46170Sstevel@tonic-gate 		if (fdc->c_intrstat)
46180Sstevel@tonic-gate 			KIOIP->intrs[KSTAT_INTR_SPURIOUS]++;
46190Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
46200Sstevel@tonic-gate 	}
46210Sstevel@tonic-gate 
46220Sstevel@tonic-gate 	mutex_enter(&fdc->c_lolock);
46230Sstevel@tonic-gate 	csb->csb_opmode = 0;
46240Sstevel@tonic-gate 
46250Sstevel@tonic-gate 	/*  reset watchdog timer if armed and not already triggered */
46260Sstevel@tonic-gate 	if (fdc->c_timeid) {
46270Sstevel@tonic-gate 		timeout_id_t timeid = fdc->c_timeid;
46280Sstevel@tonic-gate 		fdc->c_timeid = 0;
46290Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
46300Sstevel@tonic-gate 		(void) untimeout(timeid);
46310Sstevel@tonic-gate 		mutex_enter(&fdc->c_lolock);
46320Sstevel@tonic-gate 
46330Sstevel@tonic-gate 	}
46340Sstevel@tonic-gate 
46350Sstevel@tonic-gate 	if (fdc->c_flags & FDCFLG_WAITING) {
46360Sstevel@tonic-gate 		/*
46370Sstevel@tonic-gate 		 * somebody's waiting on finish of fdctlr/csb, wake them
46380Sstevel@tonic-gate 		 */
46390Sstevel@tonic-gate 		fdc->c_flags ^= FDCFLG_WAITING;
46400Sstevel@tonic-gate 		cv_signal(&fdc->c_iocv);
46410Sstevel@tonic-gate 
46420Sstevel@tonic-gate 		/*
46430Sstevel@tonic-gate 		 * FDCFLG_BUSY is NOT cleared, NOR is the csb given back; so
46440Sstevel@tonic-gate 		 * the operation just finished can look at the csb
46450Sstevel@tonic-gate 		 */
46460Sstevel@tonic-gate 	} else {
46470Sstevel@tonic-gate 		FDERRPRINT(FDEP_L3, FDEM_INTR,
46480Sstevel@tonic-gate 		    (C, "fdintr: nobody sleeping (%x %x %x)\n",
46490Sstevel@tonic-gate 		    csb->csb_rslt[0], csb->csb_rslt[1], csb->csb_rslt[2]));
46500Sstevel@tonic-gate 	}
46510Sstevel@tonic-gate 	if (fdc->c_intrstat)
46520Sstevel@tonic-gate 		KIOIP->intrs[KSTAT_INTR_SOFT]++;
46530Sstevel@tonic-gate 	mutex_exit(&fdc->c_lolock);
46540Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
46550Sstevel@tonic-gate }
46560Sstevel@tonic-gate 
46570Sstevel@tonic-gate /*
46580Sstevel@tonic-gate  * fdwatch
46590Sstevel@tonic-gate  *	is called from timein() when a floppy operation has expired.
46600Sstevel@tonic-gate  */
46610Sstevel@tonic-gate static void
46620Sstevel@tonic-gate fdwatch(void *arg)
46630Sstevel@tonic-gate {
46640Sstevel@tonic-gate 	struct fdctlr *fdc = arg;
46650Sstevel@tonic-gate 	int old_opmode;
46660Sstevel@tonic-gate 	struct fdcsb *csb;
46670Sstevel@tonic-gate 
46680Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_WATC, (C, "fdwatch\n"));
46690Sstevel@tonic-gate 
46700Sstevel@tonic-gate 	mutex_enter(&fdc->c_lolock);
46710Sstevel@tonic-gate 	if (fdc->c_timeid == 0) {
46720Sstevel@tonic-gate 		/*
46730Sstevel@tonic-gate 		 * fdintr got here first, ergo, no timeout condition..
46740Sstevel@tonic-gate 		 */
46750Sstevel@tonic-gate 
46760Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_WATC,
4677*7656SSherry.Moore@Sun.COM 		    (C, "fdwatch: no timeout\n"));
46780Sstevel@tonic-gate 
46790Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
46800Sstevel@tonic-gate 		return;
46810Sstevel@tonic-gate 	}
46820Sstevel@tonic-gate 	fdc->c_timeid = 0;
46830Sstevel@tonic-gate 	csb = &fdc->c_csb;
46840Sstevel@tonic-gate 
46850Sstevel@tonic-gate 	mutex_enter(&fdc->c_hilock);
46860Sstevel@tonic-gate 	/*
46870Sstevel@tonic-gate 	 * XXXX: We should probably reset the bloody chip
46880Sstevel@tonic-gate 	 */
46890Sstevel@tonic-gate 	old_opmode = csb->csb_opmode;
46900Sstevel@tonic-gate 
46910Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_WATC,
46920Sstevel@tonic-gate 	    (C, "fd%d: timeout, opmode:%d\n", csb->csb_unit, old_opmode));
46930Sstevel@tonic-gate 
46940Sstevel@tonic-gate 	csb->csb_opmode = 4;
46950Sstevel@tonic-gate 	mutex_exit(&fdc->c_hilock);
46960Sstevel@tonic-gate 
46970Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_WATC, (C, "fdwatch: cmd %s timed out\n",
4698*7656SSherry.Moore@Sun.COM 	    fdcmds[csb->csb_cmds[0] & 0x1f].cmdname));
46990Sstevel@tonic-gate 	fdc->c_flags |= FDCFLG_TIMEDOUT;
47000Sstevel@tonic-gate 	csb->csb_status = CSB_CMDTO;
47010Sstevel@tonic-gate 
47020Sstevel@tonic-gate 	if ((fdc->c_fdtype & FDCTYPE_DMA) == 0) {
47030Sstevel@tonic-gate 		ddi_trigger_softintr(fdc->c_softid);
47040Sstevel@tonic-gate 		KIOIP->intrs[KSTAT_INTR_WATCHDOG]++;
47050Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
47060Sstevel@tonic-gate 	} else {
47070Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
47080Sstevel@tonic-gate 		(void) fd_lointr((caddr_t)fdctlrs);
47090Sstevel@tonic-gate 	}
47100Sstevel@tonic-gate }
47110Sstevel@tonic-gate 
47120Sstevel@tonic-gate /*
47130Sstevel@tonic-gate  * fdgetcsb
47140Sstevel@tonic-gate  *	wait until the csb is free
47150Sstevel@tonic-gate  */
47160Sstevel@tonic-gate static void
47170Sstevel@tonic-gate fdgetcsb(struct fdctlr *fdc)
47180Sstevel@tonic-gate {
47190Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_GETC, (C, "fdgetcsb\n"));
47200Sstevel@tonic-gate 	ASSERT(mutex_owned(&fdc->c_lolock));
47210Sstevel@tonic-gate 	while (fdc->c_flags & FDCFLG_BUSY) {
47220Sstevel@tonic-gate 		fdc->c_flags |= FDCFLG_WANT;
47230Sstevel@tonic-gate 		cv_wait(&fdc->c_csbcv, &fdc->c_lolock);
47240Sstevel@tonic-gate 	}
47250Sstevel@tonic-gate 	fdc->c_flags |= FDCFLG_BUSY; /* got it! */
47260Sstevel@tonic-gate }
47270Sstevel@tonic-gate 
47280Sstevel@tonic-gate /*
47290Sstevel@tonic-gate  * fdretcsb
47300Sstevel@tonic-gate  *	return csb
47310Sstevel@tonic-gate  */
47320Sstevel@tonic-gate static void
47330Sstevel@tonic-gate fdretcsb(struct fdctlr *fdc)
47340Sstevel@tonic-gate {
47350Sstevel@tonic-gate 
47360Sstevel@tonic-gate 	ASSERT(mutex_owned(&fdc->c_lolock));
47370Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_RETC, (C, "fdretcsb\n"));
47380Sstevel@tonic-gate 	fdc->c_flags &= ~FDCFLG_BUSY; /* let go */
47390Sstevel@tonic-gate 
47400Sstevel@tonic-gate 	fdc->c_csb.csb_read = 0;
47410Sstevel@tonic-gate 
47420Sstevel@tonic-gate 	if (fdc->c_flags & FDCFLG_WANT) {
47430Sstevel@tonic-gate 		fdc->c_flags ^= FDCFLG_WANT;
47440Sstevel@tonic-gate 		/*
47450Sstevel@tonic-gate 		 * broadcast the signal.  One thread will wake up and
47460Sstevel@tonic-gate 		 * set the flags to FDCFLG_BUSY.  If more than one thread is
47470Sstevel@tonic-gate 		 * waiting then each thread will wake up in turn.  The first
47480Sstevel@tonic-gate 		 * thread to wake-up will set the FDCFLG_BUSY flag and the
47490Sstevel@tonic-gate 		 * subsequent threads will will wake-up, but reset the
47500Sstevel@tonic-gate 		 * flag to FDCFLG_WANT because the FDCFLG_BUSY bit is set.
47510Sstevel@tonic-gate 		 */
47520Sstevel@tonic-gate 		cv_broadcast(&fdc->c_csbcv);
47530Sstevel@tonic-gate 	}
47540Sstevel@tonic-gate }
47550Sstevel@tonic-gate 
47560Sstevel@tonic-gate 
47570Sstevel@tonic-gate /*
47580Sstevel@tonic-gate  * fdreset
47590Sstevel@tonic-gate  *	reset THE controller, and configure it to be
47600Sstevel@tonic-gate  *	the way it ought to be
47610Sstevel@tonic-gate  * ASSUMES: that it already owns the csb/fdctlr!
47620Sstevel@tonic-gate  *
47630Sstevel@tonic-gate  *	- called with the low level lock held
47640Sstevel@tonic-gate  */
47650Sstevel@tonic-gate static int
47660Sstevel@tonic-gate fdreset(struct fdctlr *fdc)
47670Sstevel@tonic-gate {
47680Sstevel@tonic-gate 	struct fdcsb *csb;
47690Sstevel@tonic-gate 	clock_t local_lbolt = 0;
47700Sstevel@tonic-gate 	timeout_id_t timeid;
47710Sstevel@tonic-gate 
47720Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_RESE, (C, "fdreset\n"));
47730Sstevel@tonic-gate 
47740Sstevel@tonic-gate 	ASSERT(mutex_owned(&fdc->c_lolock));
47750Sstevel@tonic-gate 
47760Sstevel@tonic-gate 	/* count resets */
47770Sstevel@tonic-gate 	fdc->fdstats.reset++;
47780Sstevel@tonic-gate 
47790Sstevel@tonic-gate 	/*
47800Sstevel@tonic-gate 	 * On the 82077, the DSR will clear itself after a reset.  Upon exiting
47810Sstevel@tonic-gate 	 * the reset, a polling interrupt will be generated.  If the floppy
47820Sstevel@tonic-gate 	 * interrupt is enabled, it's possible for cv_signal() to be called
47830Sstevel@tonic-gate 	 * before cv_wait().  This will cause the system to hang.  Turn off
47840Sstevel@tonic-gate 	 * the floppy interrupt to avoid this race condition
47850Sstevel@tonic-gate 	 */
47860Sstevel@tonic-gate 	if ((fdc->c_fdtype & FDCTYPE_CTRLMASK) == FDCTYPE_82077) {
47870Sstevel@tonic-gate 		/*
47880Sstevel@tonic-gate 		 * We need to perform any timeouts before we Reset the
47890Sstevel@tonic-gate 		 * controller. We cannot afford to drop the c_lolock mutex after
47900Sstevel@tonic-gate 		 * Resetting the controller. The reason is that we get a spate
47910Sstevel@tonic-gate 		 * of interrupts until we take the controller out of reset.
47920Sstevel@tonic-gate 		 * The way we avoid this spate of continuous interrupts is by
47930Sstevel@tonic-gate 		 * holding on to the c_lolock and forcing the fdintr_dma routine
47940Sstevel@tonic-gate 		 * to go to sleep waiting for this mutex.
47950Sstevel@tonic-gate 		 */
47960Sstevel@tonic-gate 		/* Do not hold the mutex across the untimeout call */
47970Sstevel@tonic-gate 		timeid = fdc->c_mtimeid;
47980Sstevel@tonic-gate 		fdc->c_mtimeid = 0;
47990Sstevel@tonic-gate 		if (timeid) {
48000Sstevel@tonic-gate 			mutex_exit(&fdc->c_lolock);
48010Sstevel@tonic-gate 			(void) untimeout(timeid);
48020Sstevel@tonic-gate 			mutex_enter(&fdc->c_lolock);
48030Sstevel@tonic-gate 		}
48040Sstevel@tonic-gate 		/* LINTED */
48050Sstevel@tonic-gate 		Set_dor(fdc, DMAGATE, 0);
48060Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_RESE, (C, "fdreset: set dor\n"));
48070Sstevel@tonic-gate 	}
48080Sstevel@tonic-gate 
48090Sstevel@tonic-gate 	/* toggle software reset */
48100Sstevel@tonic-gate 	Dsr(fdc, SWR);
48110Sstevel@tonic-gate 
48120Sstevel@tonic-gate 	drv_usecwait(5);
48130Sstevel@tonic-gate 
48140Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_RESE,
4815*7656SSherry.Moore@Sun.COM 	    (C, "fdreset: toggled software reset\n"));
48160Sstevel@tonic-gate 
48170Sstevel@tonic-gate 	/*
48180Sstevel@tonic-gate 	 * This sets the data rate to 500Kbps (for high density)
48190Sstevel@tonic-gate 	 * XXX should use current characteristics instead XXX
48200Sstevel@tonic-gate 	 */
48210Sstevel@tonic-gate 	Dsr(fdc, 0);
48220Sstevel@tonic-gate 	drv_usecwait(5);
48230Sstevel@tonic-gate 	switch (fdc->c_fdtype & FDCTYPE_CTRLMASK) {
48240Sstevel@tonic-gate 	case FDCTYPE_82077:
48250Sstevel@tonic-gate 		/*
48260Sstevel@tonic-gate 		 * when we bring the controller out of reset it will generate
48270Sstevel@tonic-gate 		 * a polling interrupt. fdintr() will field it and schedule
48280Sstevel@tonic-gate 		 * fd_lointr(). There will be no one sleeping but we are
48290Sstevel@tonic-gate 		 * expecting an interrupt so....
48300Sstevel@tonic-gate 		 */
48310Sstevel@tonic-gate 		fdc->c_flags |= FDCFLG_WAITING;
48320Sstevel@tonic-gate 
48330Sstevel@tonic-gate 		/*
48340Sstevel@tonic-gate 		 * The reset bit must be cleared to take the 077 out of
48350Sstevel@tonic-gate 		 * reset state and the DMAGATE bit must be high to enable
48360Sstevel@tonic-gate 		 * interrupts.
48370Sstevel@tonic-gate 		 */
48380Sstevel@tonic-gate 		/* LINTED */
48390Sstevel@tonic-gate 		Set_dor(fdc, DMAGATE|RESET, 1);
48400Sstevel@tonic-gate 
48410Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
4842*7656SSherry.Moore@Sun.COM 		    (C, "fdattach: Dor 0x%x\n", Dor(fdc)));
48430Sstevel@tonic-gate 
48440Sstevel@tonic-gate 		local_lbolt = ddi_get_lbolt();
48450Sstevel@tonic-gate 		if (cv_timedwait(&fdc->c_iocv, &fdc->c_lolock,
4846*7656SSherry.Moore@Sun.COM 		    local_lbolt + drv_usectohz(1000000)) == -1) {
48470Sstevel@tonic-gate 			return (-1);
48480Sstevel@tonic-gate 		}
48490Sstevel@tonic-gate 		break;
48500Sstevel@tonic-gate 
48510Sstevel@tonic-gate 	default:
48520Sstevel@tonic-gate 		fdc->c_flags |= FDCFLG_WAITING;
48530Sstevel@tonic-gate 
48540Sstevel@tonic-gate 		/*
48550Sstevel@tonic-gate 		 * A timed wait is not used because it's possible for the timer
48560Sstevel@tonic-gate 		 * to go off before the controller has a chance to interrupt.
48570Sstevel@tonic-gate 		 */
48580Sstevel@tonic-gate 		cv_wait(&fdc->c_iocv, &fdc->c_lolock);
48590Sstevel@tonic-gate 		break;
48600Sstevel@tonic-gate 	}
48610Sstevel@tonic-gate 	csb = &fdc->c_csb;
48620Sstevel@tonic-gate 
48630Sstevel@tonic-gate 	/* setup common things in csb */
48640Sstevel@tonic-gate 	csb->csb_unit = fdc->c_un->un_unit_no;
48650Sstevel@tonic-gate 	csb->csb_nrslts = 0;
48660Sstevel@tonic-gate 	csb->csb_opflags = CSB_OFNORESULTS;
48670Sstevel@tonic-gate 	csb->csb_maxretry = 0;
48680Sstevel@tonic-gate 	csb->csb_retrys = 0;
48690Sstevel@tonic-gate 
48700Sstevel@tonic-gate 	csb->csb_read = CSB_NULL;
48710Sstevel@tonic-gate 
48720Sstevel@tonic-gate 	/* send SPECIFY command to fdc */
48730Sstevel@tonic-gate 	/* csb->unit is don't care */
48740Sstevel@tonic-gate 	csb->csb_cmds[0] = FDRAW_SPECIFY;
48750Sstevel@tonic-gate 	csb->csb_cmds[1] = fdspec[0]; /* step rate, head unload time */
48760Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_DMA)
48770Sstevel@tonic-gate 		csb->csb_cmds[2] =  SPEC_DMA_MODE;
48780Sstevel@tonic-gate 	else
48790Sstevel@tonic-gate 		csb->csb_cmds[2] = fdspec[1];  /* head load time, DMA mode */
48800Sstevel@tonic-gate 
48810Sstevel@tonic-gate 	csb->csb_ncmds = 3;
48820Sstevel@tonic-gate 
48830Sstevel@tonic-gate 	/* XXX for now ignore errors, they "CAN'T HAPPEN" */
48840Sstevel@tonic-gate 	(void) fdexec(fdc, 0);	/* no FDXC_CHECKCHG, ... */
48850Sstevel@tonic-gate 	/* no results */
48860Sstevel@tonic-gate 
48870Sstevel@tonic-gate 	/* send CONFIGURE command to fdc */
48880Sstevel@tonic-gate 	/* csb->unit is don't care */
48890Sstevel@tonic-gate 	csb->csb_cmds[0] = CONFIGURE;
48900Sstevel@tonic-gate 	csb->csb_cmds[1] = fdconf[0]; /* motor info, motor delays */
48910Sstevel@tonic-gate 	csb->csb_cmds[2] = fdconf[1]; /* enaimplsk, disapoll, fifothru */
48920Sstevel@tonic-gate 	csb->csb_cmds[3] = fdconf[2]; /* track precomp */
48930Sstevel@tonic-gate 	csb->csb_ncmds = 4;
48940Sstevel@tonic-gate 
48950Sstevel@tonic-gate 	csb->csb_read = CSB_NULL;
48960Sstevel@tonic-gate 
48970Sstevel@tonic-gate 	csb->csb_retrys = 0;
48980Sstevel@tonic-gate 
48990Sstevel@tonic-gate 	/* XXX for now ignore errors, they "CAN'T HAPPEN" */
49000Sstevel@tonic-gate 	(void) fdexec(fdc, 0); /* no FDXC_CHECKCHG, ... */
49010Sstevel@tonic-gate 	return (0);
49020Sstevel@tonic-gate }
49030Sstevel@tonic-gate 
49040Sstevel@tonic-gate /*
49050Sstevel@tonic-gate  * fdrecalseek
49060Sstevel@tonic-gate  *	performs recalibrates or seeks if the "arg" is -1 does a
49070Sstevel@tonic-gate  *	recalibrate on a drive, else it seeks to the cylinder of
49080Sstevel@tonic-gate  *	the drive.  The recalibrate is also used to find a drive,
49090Sstevel@tonic-gate  *	ie if the drive is not there, the controller says "error"
49100Sstevel@tonic-gate  *	on the operation
49110Sstevel@tonic-gate  * NOTE: that there is special handling of this operation in the hardware
49120Sstevel@tonic-gate  * interrupt routine - it causes the operation to appear to have results;
49130Sstevel@tonic-gate  * ie the results of the SENSE INTERRUPT STATUS that the hardware interrupt
49140Sstevel@tonic-gate  * function did for us.
49150Sstevel@tonic-gate  * NOTE: because it uses sleep/wakeup it must be protected in a critical
49160Sstevel@tonic-gate  * section so create one before calling it!
49170Sstevel@tonic-gate  *
49180Sstevel@tonic-gate  * RETURNS: 0 for ok,
49190Sstevel@tonic-gate  *	else	errno from fdexec,
49200Sstevel@tonic-gate  *	or	ENODEV if error (infers hardware type error)
49210Sstevel@tonic-gate  *
49220Sstevel@tonic-gate  *	- called with the low level lock held
49230Sstevel@tonic-gate  */
49240Sstevel@tonic-gate static int
49250Sstevel@tonic-gate fdrecalseek(struct fdctlr *fdc, int unit, int arg, int execflg)
49260Sstevel@tonic-gate {
49270Sstevel@tonic-gate 	struct fdcsb *csb;
49280Sstevel@tonic-gate 	int result;
49290Sstevel@tonic-gate 
49300Sstevel@tonic-gate 	ASSERT(fdc->c_un->un_unit_no == unit);
49310Sstevel@tonic-gate 
49320Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_RECA, (C, "fdrecalseek to %d\n", arg));
49330Sstevel@tonic-gate 
49340Sstevel@tonic-gate 	/* XXX TODO: check see argument for <= num cyls OR < 256 */
49350Sstevel@tonic-gate 
49360Sstevel@tonic-gate 	csb = &fdc->c_csb;
49370Sstevel@tonic-gate 	csb->csb_unit = (uchar_t)unit;
49380Sstevel@tonic-gate 	csb->csb_cmds[1] = unit & 0x03;
49390Sstevel@tonic-gate 
49400Sstevel@tonic-gate 	if (arg == -1) {			/* is recal... */
49410Sstevel@tonic-gate 		csb->csb_cmds[0] = FDRAW_REZERO;
49420Sstevel@tonic-gate 		csb->csb_ncmds = 2;
49430Sstevel@tonic-gate 	} else {
49440Sstevel@tonic-gate 		csb->csb_cmds[0] = FDRAW_SEEK;
49450Sstevel@tonic-gate 		csb->csb_cmds[2] = (uchar_t)arg;
49460Sstevel@tonic-gate 		csb->csb_ncmds = 3;
49470Sstevel@tonic-gate 	}
49480Sstevel@tonic-gate 	csb->csb_nrslts = 2;	/* 2 for SENSE INTERRUPTS */
49490Sstevel@tonic-gate 	csb->csb_opflags = CSB_OFSEEKOPS | CSB_OFTIMEIT;
49500Sstevel@tonic-gate 	/*
49510Sstevel@tonic-gate 	 * MAYBE NYD need to set retries to different values? - depending on
49520Sstevel@tonic-gate 	 * drive characteristics - if we get to high capacity drives
49530Sstevel@tonic-gate 	 */
49540Sstevel@tonic-gate 	csb->csb_maxretry = skretry;
49550Sstevel@tonic-gate 	csb->csb_retrys = 0;
49560Sstevel@tonic-gate 
49570Sstevel@tonic-gate 	/* send cmd off to fdexec */
49580Sstevel@tonic-gate 	if (result = fdexec(fdc, FDXC_SLEEP | execflg)) {
49590Sstevel@tonic-gate 		goto out;
49600Sstevel@tonic-gate 	}
49610Sstevel@tonic-gate 
49620Sstevel@tonic-gate 	/*
49630Sstevel@tonic-gate 	 * if recal, test for equipment check error
49640Sstevel@tonic-gate 	 * ASSUMES result = 0 from above call
49650Sstevel@tonic-gate 	 */
49660Sstevel@tonic-gate 	if (arg == -1) {
49670Sstevel@tonic-gate 		result = 0;
49680Sstevel@tonic-gate 	} else {
49690Sstevel@tonic-gate 		/* for seeks, any old error will do */
49700Sstevel@tonic-gate 		if ((csb->csb_rslt[0] & IC_SR0) || csb->csb_cmdstat)
49710Sstevel@tonic-gate 			result = ENODEV;
49720Sstevel@tonic-gate 	}
49730Sstevel@tonic-gate 
49740Sstevel@tonic-gate out:
49750Sstevel@tonic-gate 	return (result);
49760Sstevel@tonic-gate }
49770Sstevel@tonic-gate 
49780Sstevel@tonic-gate /*
49790Sstevel@tonic-gate  * fdsensedrv
49800Sstevel@tonic-gate  *	do a sense_drive command.  used by fdopen and fdcheckdisk.
49810Sstevel@tonic-gate  *
49820Sstevel@tonic-gate  *	- called with the lock held
49830Sstevel@tonic-gate  */
49840Sstevel@tonic-gate static int
49850Sstevel@tonic-gate fdsensedrv(struct fdctlr *fdc, int unit)
49860Sstevel@tonic-gate {
49870Sstevel@tonic-gate 	struct fdcsb *csb;
49880Sstevel@tonic-gate 
49890Sstevel@tonic-gate 	ASSERT(fdc->c_un->un_unit_no == unit);
49900Sstevel@tonic-gate 
49910Sstevel@tonic-gate 	csb = &fdc->c_csb;
49920Sstevel@tonic-gate 
49930Sstevel@tonic-gate 	/* setup common things in csb */
49940Sstevel@tonic-gate 	csb->csb_unit = (uchar_t)unit;
49950Sstevel@tonic-gate 	csb->csb_opflags = CSB_OFIMMEDIATE;
49960Sstevel@tonic-gate 	csb->csb_cmds[0] = FDRAW_SENSE_DRV;
49970Sstevel@tonic-gate 	/* MOT bit set means don't delay */
49980Sstevel@tonic-gate 	csb->csb_cmds[1] = MOT | (unit & 0x03);
49990Sstevel@tonic-gate 	csb->csb_ncmds = 2;
50000Sstevel@tonic-gate 	csb->csb_nrslts = 1;
50010Sstevel@tonic-gate 	csb->csb_maxretry = skretry;
50020Sstevel@tonic-gate 	csb->csb_retrys = 0;
50030Sstevel@tonic-gate 
50040Sstevel@tonic-gate 	/* XXX for now ignore errors, they "CAN'T HAPPEN" */
50050Sstevel@tonic-gate 	(void) fdexec(fdc, 0);	/* DON't check changed!, no sleep */
50060Sstevel@tonic-gate 
50070Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_CHEK,
5008*7656SSherry.Moore@Sun.COM 	    (C, "fdsensedrv: result 0x%x", csb->csb_rslt[0]));
50090Sstevel@tonic-gate 
50100Sstevel@tonic-gate 	return (csb->csb_rslt[0]); /* return status byte 3 */
50110Sstevel@tonic-gate }
50120Sstevel@tonic-gate 
50130Sstevel@tonic-gate /*
50140Sstevel@tonic-gate  * fdcheckdisk
50150Sstevel@tonic-gate  *	check to see if the disk is still there - do a recalibrate,
50160Sstevel@tonic-gate  *	then see if DSKCHG line went away, if so, diskette is in; else
50170Sstevel@tonic-gate  *	it's (still) out.
50180Sstevel@tonic-gate  */
50190Sstevel@tonic-gate 
50200Sstevel@tonic-gate static int
50210Sstevel@tonic-gate fdcheckdisk(struct fdctlr *fdc, int unit)
50220Sstevel@tonic-gate {
50230Sstevel@tonic-gate 	auto struct fdcsb savecsb;
50240Sstevel@tonic-gate 	struct fdcsb *csb;
50250Sstevel@tonic-gate 	int	err, st3;
50260Sstevel@tonic-gate 	int	seekto;			/* where to seek for reset of DSKCHG */
50270Sstevel@tonic-gate 
50280Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_CHEK,
50290Sstevel@tonic-gate 	    (C, "fdcheckdisk, unit %d\n", unit));
50300Sstevel@tonic-gate 
50310Sstevel@tonic-gate 	ASSERT(fdc->c_un->un_unit_no == unit);
50320Sstevel@tonic-gate 
50330Sstevel@tonic-gate 	/*
50340Sstevel@tonic-gate 	 * save old csb
50350Sstevel@tonic-gate 	 */
50360Sstevel@tonic-gate 
50370Sstevel@tonic-gate 	csb = &fdc->c_csb;
50380Sstevel@tonic-gate 	savecsb = fdc->c_csb;
50390Sstevel@tonic-gate 	bzero((caddr_t)csb, sizeof (*csb));
50400Sstevel@tonic-gate 
50410Sstevel@tonic-gate 	/*
50420Sstevel@tonic-gate 	 * Read drive status to see if at TRK0, if so, seek to cyl 1,
50430Sstevel@tonic-gate 	 * else seek to cyl 0.	We do this because the controller is
50440Sstevel@tonic-gate 	 * "smart" enough to not send any step pulses (which are how
50450Sstevel@tonic-gate 	 * the DSKCHG line gets reset) if it sees TRK0 'cause it
50460Sstevel@tonic-gate 	 * knows the drive is already recalibrated.
50470Sstevel@tonic-gate 	 */
50480Sstevel@tonic-gate 	st3 = fdsensedrv(fdc, unit);
50490Sstevel@tonic-gate 
50500Sstevel@tonic-gate 	/* check TRK0 bit in status */
50510Sstevel@tonic-gate 	if (st3 & T0_SR3)
50520Sstevel@tonic-gate 		seekto = 1;	/* at TRK0, seek out */
50530Sstevel@tonic-gate 	else
50540Sstevel@tonic-gate 		seekto = 0;
50550Sstevel@tonic-gate 
50560Sstevel@tonic-gate 	/*
50570Sstevel@tonic-gate 	 * DON'T recurse check changed
50580Sstevel@tonic-gate 	 */
50590Sstevel@tonic-gate 	err = fdrecalseek(fdc, unit, seekto, 0);
50600Sstevel@tonic-gate 
50610Sstevel@tonic-gate 	/* "restore" old csb, check change state */
50620Sstevel@tonic-gate 	fdc->c_csb = savecsb;
50630Sstevel@tonic-gate 
50640Sstevel@tonic-gate 	/* any recal/seek errors are too serious to attend to */
50650Sstevel@tonic-gate 	if (err) {
50660Sstevel@tonic-gate 		FDERRPRINT(FDEP_L2, FDEM_CHEK,
50670Sstevel@tonic-gate 		    (C, "fdcheckdisk err %d\n", err));
50680Sstevel@tonic-gate 		return (err);
50690Sstevel@tonic-gate 	}
50700Sstevel@tonic-gate 
50710Sstevel@tonic-gate 	/*
50720Sstevel@tonic-gate 	 * if disk change still asserted, no diskette in drive!
50730Sstevel@tonic-gate 	 */
50740Sstevel@tonic-gate 	if (fdsense_chng(fdc, csb->csb_unit)) {
50750Sstevel@tonic-gate 		FDERRPRINT(FDEP_L2, FDEM_CHEK,
50760Sstevel@tonic-gate 		    (C, "fdcheckdisk no disk\n"));
50770Sstevel@tonic-gate 		return (1);
50780Sstevel@tonic-gate 	}
50790Sstevel@tonic-gate 	return (0);
50800Sstevel@tonic-gate }
50810Sstevel@tonic-gate 
50820Sstevel@tonic-gate /*
50830Sstevel@tonic-gate  *	fdselect() - select drive, needed for external to chip select logic
50840Sstevel@tonic-gate  *	fdeject() - ejects drive, must be previously selected
50850Sstevel@tonic-gate  *	fdsense_chng() - sense disk changed line from previously selected drive
50860Sstevel@tonic-gate  *		return s 1 is signal asserted, else 0
50870Sstevel@tonic-gate  */
50880Sstevel@tonic-gate /* ARGSUSED */
50890Sstevel@tonic-gate static void
50900Sstevel@tonic-gate fdselect(struct fdctlr *fdc, int unit, int on)
50910Sstevel@tonic-gate {
50920Sstevel@tonic-gate 
50930Sstevel@tonic-gate 	ASSERT(fdc->c_un->un_unit_no == unit);
50940Sstevel@tonic-gate 
50950Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_DSEL,
50960Sstevel@tonic-gate 	    (C, "fdselect, unit %d, on = %d\n", unit, on));
50970Sstevel@tonic-gate 
50980Sstevel@tonic-gate 	switch (fdc->c_fdtype & FDCTYPE_AUXIOMASK) {
50990Sstevel@tonic-gate 	case FDCTYPE_MACHIO:
51000Sstevel@tonic-gate 		set_auxioreg(AUX_DRVSELECT, on);
51010Sstevel@tonic-gate 		break;
51020Sstevel@tonic-gate 
51030Sstevel@tonic-gate 	case FDCTYPE_SLAVIO:
51040Sstevel@tonic-gate 	case FDCTYPE_CHEERIO:
51050Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
5106*7656SSherry.Moore@Sun.COM 		    (C, "fdselect: (before) Dor 0x%x\n", Dor(fdc)));
51070Sstevel@tonic-gate 
51080Sstevel@tonic-gate 		if (unit == 0) {
51090Sstevel@tonic-gate 			Set_dor(fdc, DRVSEL, !on);
51100Sstevel@tonic-gate 		} else {
51110Sstevel@tonic-gate 			Set_dor(fdc, DRVSEL, on);
51120Sstevel@tonic-gate 		}
51130Sstevel@tonic-gate 
51140Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
5115*7656SSherry.Moore@Sun.COM 		    (C, "fdselect: Dor 0x%x\n", Dor(fdc)));
51160Sstevel@tonic-gate 
51170Sstevel@tonic-gate 		break;
51180Sstevel@tonic-gate 
51190Sstevel@tonic-gate 	default:
51200Sstevel@tonic-gate 		break;
51210Sstevel@tonic-gate 	}
51220Sstevel@tonic-gate }
51230Sstevel@tonic-gate 
51240Sstevel@tonic-gate /* ARGSUSED */
51250Sstevel@tonic-gate static void
51260Sstevel@tonic-gate fdeject(struct fdctlr *fdc, int unit)
51270Sstevel@tonic-gate {
51280Sstevel@tonic-gate 	struct fdunit *un;
51290Sstevel@tonic-gate 
51300Sstevel@tonic-gate 	ASSERT(fdc->c_un->un_unit_no == unit);
51310Sstevel@tonic-gate 
51320Sstevel@tonic-gate 	un = fdc->c_un;
51330Sstevel@tonic-gate 
51340Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_EJEC, (C, "fdeject\n"));
51350Sstevel@tonic-gate 	/*
51360Sstevel@tonic-gate 	 * assume delay of function calling sufficient settling time
51370Sstevel@tonic-gate 	 * eject line is NOT driven by inverter so it is true low
51380Sstevel@tonic-gate 	 */
51390Sstevel@tonic-gate 	switch (fdc->c_fdtype & FDCTYPE_AUXIOMASK) {
51400Sstevel@tonic-gate 	case FDCTYPE_MACHIO:
51410Sstevel@tonic-gate 		set_auxioreg(AUX_EJECT, 0);
51420Sstevel@tonic-gate 		drv_usecwait(2);
51430Sstevel@tonic-gate 		set_auxioreg(AUX_EJECT, 1);
51440Sstevel@tonic-gate 		break;
51450Sstevel@tonic-gate 
51460Sstevel@tonic-gate 	case FDCTYPE_SLAVIO:
51470Sstevel@tonic-gate 		if (!(Dor(fdc) & MOTEN(unit))) {
51480Sstevel@tonic-gate 			/* LINTED */
51490Sstevel@tonic-gate 			Set_dor(fdc, MOTEN(unit), 1);
51500Sstevel@tonic-gate 		}
51510Sstevel@tonic-gate 		drv_usecwait(2);	/* just to settle */
51520Sstevel@tonic-gate 		/* LINTED */
51530Sstevel@tonic-gate 		Set_dor(fdc, EJECT, 1);
51540Sstevel@tonic-gate 		drv_usecwait(2);
51550Sstevel@tonic-gate 		/* LINTED */
51560Sstevel@tonic-gate 		Set_dor(fdc, EJECT, 0);
51570Sstevel@tonic-gate 		break;
51580Sstevel@tonic-gate 	case FDCTYPE_CHEERIO:
51590Sstevel@tonic-gate 		if (!(Dor(fdc) & MOTEN(unit))) {
51600Sstevel@tonic-gate 			/* LINTED */
51610Sstevel@tonic-gate 			Set_dor(fdc, MOTEN(unit), 1);
51620Sstevel@tonic-gate 		}
51630Sstevel@tonic-gate 		drv_usecwait(2);	/* just to settle */
51640Sstevel@tonic-gate 		/* LINTED */
51650Sstevel@tonic-gate 		Set_dor(fdc, EJECT_DMA, 1);
51660Sstevel@tonic-gate 		drv_usecwait(2);
51670Sstevel@tonic-gate 		/* LINTED */
51680Sstevel@tonic-gate 		Set_dor(fdc, EJECT_DMA, 0);
51690Sstevel@tonic-gate 		break;
51700Sstevel@tonic-gate 	}
51710Sstevel@tonic-gate 	/*
51720Sstevel@tonic-gate 	 * XXX set ejected state?
51730Sstevel@tonic-gate 	 */
51740Sstevel@tonic-gate 	un->un_ejected = 1;
51750Sstevel@tonic-gate }
51760Sstevel@tonic-gate 
51770Sstevel@tonic-gate /* ARGSUSED */
51780Sstevel@tonic-gate static int
51790Sstevel@tonic-gate fdsense_chng(struct fdctlr *fdc, int unit)
51800Sstevel@tonic-gate {
51810Sstevel@tonic-gate 	int changed = 0;
51820Sstevel@tonic-gate 
51830Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_SCHG, (C, "fdsense_chng:start\n"));
51840Sstevel@tonic-gate 
51850Sstevel@tonic-gate 	ASSERT(fdc->c_un->un_unit_no == unit);
51860Sstevel@tonic-gate 
51870Sstevel@tonic-gate 	/*
51880Sstevel@tonic-gate 	 * Do not turn on the motor of a pollable drive
51890Sstevel@tonic-gate 	 */
51900Sstevel@tonic-gate 	if (fd_pollable) {
51910Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_SCHG, (C, "pollable: don't turn on motor\n"));
51920Sstevel@tonic-gate 		/*
51930Sstevel@tonic-gate 		 * Invert the sense of the DSKCHG for pollable drives
51940Sstevel@tonic-gate 		 */
51950Sstevel@tonic-gate 		if (Dir(fdc) & DSKCHG)
51960Sstevel@tonic-gate 			changed = 0;
51970Sstevel@tonic-gate 		else
51980Sstevel@tonic-gate 			changed = 1;
51990Sstevel@tonic-gate 
52000Sstevel@tonic-gate 		return (changed);
52010Sstevel@tonic-gate 	}
52020Sstevel@tonic-gate 
52030Sstevel@tonic-gate 	switch (fdc->c_fdtype & FDCTYPE_AUXIOMASK) {
52040Sstevel@tonic-gate 	case FDCTYPE_MACHIO:
52050Sstevel@tonic-gate 		if (*fdc->c_auxiova & AUX_DISKCHG)
52060Sstevel@tonic-gate 			changed = 1;
52070Sstevel@tonic-gate 		break;
52080Sstevel@tonic-gate 
52090Sstevel@tonic-gate 	case FDCTYPE_SB:
52100Sstevel@tonic-gate 	case FDCTYPE_SLAVIO:
52110Sstevel@tonic-gate 	case FDCTYPE_CHEERIO:
52120Sstevel@tonic-gate 		if (!(Dor(fdc) & MOTEN(unit))) {
52130Sstevel@tonic-gate 			/* LINTED */
52140Sstevel@tonic-gate 			Set_dor(fdc, MOTEN(unit), 1);
52150Sstevel@tonic-gate 		}
52160Sstevel@tonic-gate 		drv_usecwait(2);	/* just to settle */
52170Sstevel@tonic-gate 		if (Dir(fdc) & DSKCHG)
52180Sstevel@tonic-gate 			changed = 1;
52190Sstevel@tonic-gate 		break;
52200Sstevel@tonic-gate 	}
52210Sstevel@tonic-gate 
52220Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_SCHG, (C, "fdsense_chng:end\n"));
52230Sstevel@tonic-gate 
52240Sstevel@tonic-gate 	return (changed);
52250Sstevel@tonic-gate }
52260Sstevel@tonic-gate 
52270Sstevel@tonic-gate /*
52280Sstevel@tonic-gate  *	if it can read a valid label it does so, else it will use a
52290Sstevel@tonic-gate  *	default.  If it can`t read the diskette - that is an error.
52300Sstevel@tonic-gate  *
52310Sstevel@tonic-gate  * RETURNS: 0 for ok - meaning that it could at least read the device,
52320Sstevel@tonic-gate  *	!0 for error XXX TBD NYD error codes
52330Sstevel@tonic-gate  *
52340Sstevel@tonic-gate  *	- called with the low level lock held
52350Sstevel@tonic-gate  */
52360Sstevel@tonic-gate static int
52370Sstevel@tonic-gate fdgetlabel(struct fdctlr *fdc, int unit)
52380Sstevel@tonic-gate {
52390Sstevel@tonic-gate 	struct dk_label *label = NULL;
52400Sstevel@tonic-gate 	struct fdunit *un;
52410Sstevel@tonic-gate 	short *sp;
52420Sstevel@tonic-gate 	short count;
52430Sstevel@tonic-gate 	short xsum;			/* checksum */
52440Sstevel@tonic-gate 	int	i, tries;
52450Sstevel@tonic-gate 	int	err = 0;
52460Sstevel@tonic-gate 	short	oldlvl;
52470Sstevel@tonic-gate 
52480Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_GETL,
52490Sstevel@tonic-gate 	    (C, "fdgetlabel: unit %d\n", unit));
52500Sstevel@tonic-gate 
52510Sstevel@tonic-gate 	un = fdc->c_un;
52520Sstevel@tonic-gate 	un->un_flags &= ~(FDUNIT_UNLABELED);
52530Sstevel@tonic-gate 
52540Sstevel@tonic-gate 	ASSERT(fdc->c_un->un_unit_no == unit);
52550Sstevel@tonic-gate 
52560Sstevel@tonic-gate 	/* Do not print errors since this is a private cmd */
52570Sstevel@tonic-gate 
52580Sstevel@tonic-gate 	oldlvl = fderrlevel;
52590Sstevel@tonic-gate 
52600Sstevel@tonic-gate 
52610Sstevel@tonic-gate 	fderrlevel = FDEP_L4;
52620Sstevel@tonic-gate 
52630Sstevel@tonic-gate 	label = (struct dk_label *)
5264*7656SSherry.Moore@Sun.COM 	    kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);
52650Sstevel@tonic-gate 
52660Sstevel@tonic-gate 	/*
52670Sstevel@tonic-gate 	 * try different characteristics (ie densities) by attempting to read
52680Sstevel@tonic-gate 	 * from the diskette.  The diskette may not be present or
52690Sstevel@tonic-gate 	 * is unformatted.
52700Sstevel@tonic-gate 	 *
52710Sstevel@tonic-gate 	 * First, the last sector of the first track is read.  If this
52720Sstevel@tonic-gate 	 * passes, attempt to read the last sector + 1 of the first track.
52730Sstevel@tonic-gate 	 * For example, for a high density diskette, sector 18 is read.  If
52740Sstevel@tonic-gate 	 * the diskette is high density, this will pass.  Next, try to
52750Sstevel@tonic-gate 	 * read sector 19 of the first track.  This should fail.  If it
52760Sstevel@tonic-gate 	 * passes, this is not a high density diskette.  Finally, read
52770Sstevel@tonic-gate 	 * the first sector which should contain a label.
52780Sstevel@tonic-gate 	 *
52790Sstevel@tonic-gate 	 * if un->un_curfdtype is -1 then the current characteristics
52800Sstevel@tonic-gate 	 * were set by FDIOSCHAR and need to try it as well as everything
52810Sstevel@tonic-gate 	 * in the table
52820Sstevel@tonic-gate 	 */
52830Sstevel@tonic-gate 	if (un->un_curfdtype == -1) {
52840Sstevel@tonic-gate 		tries = nfdtypes+1;
52850Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_GETL,
52860Sstevel@tonic-gate 		    (C, "fdgetl: un_curfdtype is -1\n"));
52870Sstevel@tonic-gate 
5288*7656SSherry.Moore@Sun.COM 	} else {
52890Sstevel@tonic-gate 		tries = nfdtypes;
52900Sstevel@tonic-gate 
52910Sstevel@tonic-gate 		/* Always start with the highest density (1.7MB) */
52920Sstevel@tonic-gate 		un->un_curfdtype = 0;
52930Sstevel@tonic-gate 		*(un->un_chars) = fdtypes[un->un_curfdtype];
52940Sstevel@tonic-gate 	}
52950Sstevel@tonic-gate 
52960Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_GETL,
5297*7656SSherry.Moore@Sun.COM 	    (C, "fdgetl: no. of tries %d\n", tries));
52980Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_GETL,
5299*7656SSherry.Moore@Sun.COM 	    (C, "fdgetl: no. of curfdtype %d\n", un->un_curfdtype));
53000Sstevel@tonic-gate 
53010Sstevel@tonic-gate 	for (i = 0; i < tries; i++) {
53020Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_GETL,
53030Sstevel@tonic-gate 		    (C, "fdgetl: trying %d\n", i));
53040Sstevel@tonic-gate 
53050Sstevel@tonic-gate 		if (!(err = fdrw(fdc, unit, FDREAD, 0, 0,
5306*7656SSherry.Moore@Sun.COM 		    un->un_chars->fdc_secptrack, (caddr_t)label,
5307*7656SSherry.Moore@Sun.COM 		    sizeof (struct dk_label))) &&
53080Sstevel@tonic-gate 
53090Sstevel@tonic-gate 		    fdrw(fdc, unit, FDREAD, 0, 0,
5310*7656SSherry.Moore@Sun.COM 		    un->un_chars->fdc_secptrack + 1,
5311*7656SSherry.Moore@Sun.COM 		    (caddr_t)label, sizeof (struct dk_label)) &&
53120Sstevel@tonic-gate 
53130Sstevel@tonic-gate 		    !(err = fdrw(fdc, unit, FDREAD, 0, 0, 1, (caddr_t)label,
5314*7656SSherry.Moore@Sun.COM 		    sizeof (struct dk_label)))) {
53150Sstevel@tonic-gate 
53160Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_GETL,
53170Sstevel@tonic-gate 				(C, "fdgetl: succeeded\n"));
53180Sstevel@tonic-gate 
53190Sstevel@tonic-gate 			break;
53200Sstevel@tonic-gate 		}
53210Sstevel@tonic-gate 
53220Sstevel@tonic-gate 		/*
53230Sstevel@tonic-gate 		 * try the next entry in the characteristics tbl
53240Sstevel@tonic-gate 		 * If curfdtype is -1, the nxt entry in tbl is 0 (the first).
53250Sstevel@tonic-gate 		 */
53260Sstevel@tonic-gate 
53270Sstevel@tonic-gate 		un->un_curfdtype = (un->un_curfdtype + 1) % nfdtypes;
53280Sstevel@tonic-gate 		*(un->un_chars) = fdtypes[un->un_curfdtype];
53290Sstevel@tonic-gate 
53300Sstevel@tonic-gate 
53310Sstevel@tonic-gate 	}
53320Sstevel@tonic-gate 
53330Sstevel@tonic-gate 	/* print errors again */
53340Sstevel@tonic-gate 	fderrlevel = oldlvl;
53350Sstevel@tonic-gate 
53360Sstevel@tonic-gate 	/* Couldn't read anything */
53370Sstevel@tonic-gate 	if (err) {
53380Sstevel@tonic-gate 
53390Sstevel@tonic-gate 		/* The default characteristics are high density (1.4MB) */
53400Sstevel@tonic-gate 		un->un_curfdtype = 1;
53410Sstevel@tonic-gate 		*(un->un_chars) = fdtypes[un->un_curfdtype];
53420Sstevel@tonic-gate 
53430Sstevel@tonic-gate 		fdunpacklabel(&fdlbl_high_80, &un->un_label);
53440Sstevel@tonic-gate 
53450Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_GETL,
5346*7656SSherry.Moore@Sun.COM 		    (C, "fdgetl: Can't autosense diskette\n"));
53470Sstevel@tonic-gate 
53480Sstevel@tonic-gate 		goto out;
53490Sstevel@tonic-gate 	}
53500Sstevel@tonic-gate 
53510Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_GETL,
53520Sstevel@tonic-gate 	    (C, "fdgetl: fdtype=%d !!!\n", un->un_curfdtype));
53530Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_GETL,
53540Sstevel@tonic-gate 	    (C, "fdgetl: rate=%d ssize=%d !!!\n",
53550Sstevel@tonic-gate 	    un->un_chars->fdc_transfer_rate, un->un_chars->fdc_sec_size));
53560Sstevel@tonic-gate 
53570Sstevel@tonic-gate 	/*
53580Sstevel@tonic-gate 	 * _something_ was read	 -  look for unixtype label
53590Sstevel@tonic-gate 	 */
53600Sstevel@tonic-gate 	if (label->dkl_magic != DKL_MAGIC) {
53610Sstevel@tonic-gate 
53620Sstevel@tonic-gate 		/*
53630Sstevel@tonic-gate 		 * The label isn't a unix label.  However, the diskette
53640Sstevel@tonic-gate 		 * is formatted because we were able to read the first
53650Sstevel@tonic-gate 		 * cylinder.
53660Sstevel@tonic-gate 		 */
53670Sstevel@tonic-gate 
53680Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_GETL,
53690Sstevel@tonic-gate 		    (C, "fdgetl: not unix label\n"));
53700Sstevel@tonic-gate 
53710Sstevel@tonic-gate 		goto nolabel;
53720Sstevel@tonic-gate 	}
53730Sstevel@tonic-gate 
53740Sstevel@tonic-gate 	/*
53750Sstevel@tonic-gate 	 * Checksum the label
53760Sstevel@tonic-gate 	 */
53770Sstevel@tonic-gate 	count = sizeof (struct dk_label)/sizeof (short);
53780Sstevel@tonic-gate 	sp = (short *)label;
53790Sstevel@tonic-gate 	xsum = 0;
53800Sstevel@tonic-gate 	while (count--)
53810Sstevel@tonic-gate 		xsum ^= *sp++;	/* should add up to 0 */
53820Sstevel@tonic-gate 	if (xsum) {
53830Sstevel@tonic-gate 
53840Sstevel@tonic-gate 		/*
53850Sstevel@tonic-gate 		 * The checksum fails.  However, the diskette is formatted
53860Sstevel@tonic-gate 		 * because we were able to read the first cylinder
53870Sstevel@tonic-gate 		 */
53880Sstevel@tonic-gate 
53890Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_GETL,
53900Sstevel@tonic-gate 		    (C, "fdgetl: bad cksum\n"));
53910Sstevel@tonic-gate 
53920Sstevel@tonic-gate 		goto nolabel;
53930Sstevel@tonic-gate 	}
53940Sstevel@tonic-gate 
53950Sstevel@tonic-gate 	/*
53960Sstevel@tonic-gate 	 * The diskette has a unix label with a correct checksum.
53970Sstevel@tonic-gate 	 * Copy the label into the unit structure
53980Sstevel@tonic-gate 	 */
53990Sstevel@tonic-gate 	un->un_label = *label;
54000Sstevel@tonic-gate 
54010Sstevel@tonic-gate 	goto out;
54020Sstevel@tonic-gate 
54030Sstevel@tonic-gate nolabel:
54040Sstevel@tonic-gate 	/*
54050Sstevel@tonic-gate 	 * The diskette doesn't have a correct unix label, but it is formatted.
54060Sstevel@tonic-gate 	 * Use a default label according to the diskette's density
54070Sstevel@tonic-gate 	 * (mark default used)
54080Sstevel@tonic-gate 	 */
54090Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_GETL,
54100Sstevel@tonic-gate 	    (C, "fdgetlabel: unit %d\n", unit));
54110Sstevel@tonic-gate 	un->un_flags |= FDUNIT_UNLABELED;
54120Sstevel@tonic-gate 	switch (un->un_chars->fdc_secptrack) {
54130Sstevel@tonic-gate 	case 9:
54140Sstevel@tonic-gate 		fdunpacklabel(&fdlbl_low_80, &un->un_label);
54150Sstevel@tonic-gate 		break;
54160Sstevel@tonic-gate 	case 8:
54170Sstevel@tonic-gate 		fdunpacklabel(&fdlbl_medium_80, &un->un_label);
54180Sstevel@tonic-gate 		break;
54190Sstevel@tonic-gate 	case 18:
54200Sstevel@tonic-gate 		fdunpacklabel(&fdlbl_high_80, &un->un_label);
54210Sstevel@tonic-gate 		break;
54220Sstevel@tonic-gate 	case 21:
54230Sstevel@tonic-gate 		fdunpacklabel(&fdlbl_high_21, &un->un_label);
54240Sstevel@tonic-gate 		break;
54250Sstevel@tonic-gate 	default:
54260Sstevel@tonic-gate 		fdunpacklabel(&fdlbl_high_80, &un->un_label);
54270Sstevel@tonic-gate 		break;
54280Sstevel@tonic-gate 	}
54290Sstevel@tonic-gate 
54300Sstevel@tonic-gate out:
54310Sstevel@tonic-gate 	if (label != NULL)
54320Sstevel@tonic-gate 		kmem_free((caddr_t)label, sizeof (struct dk_label));
54330Sstevel@tonic-gate 	return (err);
54340Sstevel@tonic-gate }
54350Sstevel@tonic-gate 
54360Sstevel@tonic-gate /*
54370Sstevel@tonic-gate  * fdrw- used only for reading labels  and for DKIOCSVTOC ioctl
54380Sstevel@tonic-gate  *	 which reads the 1 sector.
54390Sstevel@tonic-gate  */
54400Sstevel@tonic-gate static int
54410Sstevel@tonic-gate fdrw(struct fdctlr *fdc, int unit, int rw, int cyl, int head,
54420Sstevel@tonic-gate     int sector, caddr_t bufp, uint_t len)
54430Sstevel@tonic-gate {
54440Sstevel@tonic-gate 	struct fdcsb *csb;
54450Sstevel@tonic-gate 	struct	fd_char *ch;
54460Sstevel@tonic-gate 	int	cmdresult = 0;
54470Sstevel@tonic-gate 	caddr_t dma_addr;
54480Sstevel@tonic-gate 	size_t	real_length;
54490Sstevel@tonic-gate 	int	res;
54500Sstevel@tonic-gate 	ddi_device_acc_attr_t attr;
54510Sstevel@tonic-gate 	ddi_acc_handle_t	mem_handle = NULL;
54520Sstevel@tonic-gate 
54530Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_RW, (C, "fdrw\n"));
54540Sstevel@tonic-gate 
54550Sstevel@tonic-gate 	ASSERT(fdc->c_un->un_unit_no == unit);
54560Sstevel@tonic-gate 
54570Sstevel@tonic-gate 	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
54580Sstevel@tonic-gate 
54590Sstevel@tonic-gate 	if (fdc->c_un->un_state == FD_STATE_STOPPED) {
54600Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
54610Sstevel@tonic-gate 		if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
5462*7656SSherry.Moore@Sun.COM 		    != DDI_SUCCESS) {
54630Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power change \
5464*7656SSherry.Moore@Sun.COM 			    failed. \n"));
54650Sstevel@tonic-gate 			mutex_enter(&fdc->c_lolock);
54660Sstevel@tonic-gate 			return (EIO);
54670Sstevel@tonic-gate 		}
54680Sstevel@tonic-gate 
54690Sstevel@tonic-gate 		mutex_enter(&fdc->c_lolock);
54700Sstevel@tonic-gate 	}
54710Sstevel@tonic-gate 
54720Sstevel@tonic-gate 	fdgetcsb(fdc);
54730Sstevel@tonic-gate 	csb = &fdc->c_csb;
54740Sstevel@tonic-gate 	ch = fdc->c_un->un_chars;
54750Sstevel@tonic-gate 	if (rw == FDREAD) {
54760Sstevel@tonic-gate 		if (fdc->c_fdtype & FDCTYPE_TCBUG) {
54770Sstevel@tonic-gate 			/*
54780Sstevel@tonic-gate 			 * kludge for lack of Multitrack functionality
54790Sstevel@tonic-gate 			 */
54800Sstevel@tonic-gate 			csb->csb_cmds[0] = SK + FDRAW_RDCMD;
54810Sstevel@tonic-gate 		} else
54820Sstevel@tonic-gate 			csb->csb_cmds[0] = MT + SK + FDRAW_RDCMD;
54830Sstevel@tonic-gate 	} else { /* write */
54840Sstevel@tonic-gate 		if (fdc->c_fdtype & FDCTYPE_TCBUG) {
54850Sstevel@tonic-gate 			/*
54860Sstevel@tonic-gate 			 * kludge for lack of Multitrack functionality
54870Sstevel@tonic-gate 			 */
54880Sstevel@tonic-gate 			csb->csb_cmds[0] = FDRAW_WRCMD;
54890Sstevel@tonic-gate 		} else
54900Sstevel@tonic-gate 			csb->csb_cmds[0] = MT + FDRAW_WRCMD;
54910Sstevel@tonic-gate 	}
54920Sstevel@tonic-gate 
54930Sstevel@tonic-gate 	if (rw == FDREAD)
54940Sstevel@tonic-gate 		fdc->c_csb.csb_read = CSB_READ;
54950Sstevel@tonic-gate 	else
54960Sstevel@tonic-gate 		fdc->c_csb.csb_read = CSB_WRITE;
54970Sstevel@tonic-gate 
54980Sstevel@tonic-gate 	/* always or in MFM bit */
54990Sstevel@tonic-gate 	csb->csb_cmds[0] |= MFM;
55000Sstevel@tonic-gate 	csb->csb_cmds[1] = (uchar_t)(unit | ((head & 0x1) << 2));
55010Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_SB)
55020Sstevel@tonic-gate 		csb->csb_cmds[1] |= IPS;
55030Sstevel@tonic-gate 	csb->csb_cmds[2] = (uchar_t)cyl;
55040Sstevel@tonic-gate 	csb->csb_cmds[3] = (uchar_t)head;
55050Sstevel@tonic-gate 	csb->csb_cmds[4] = (uchar_t)sector;
55060Sstevel@tonic-gate 	csb->csb_cmds[5] = ch->fdc_medium ? 3 : 2; /* sector size code */
55070Sstevel@tonic-gate 	/*
55080Sstevel@tonic-gate 	 * kludge for end-of-cylinder error.
55090Sstevel@tonic-gate 	 */
55100Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_TCBUG)
55110Sstevel@tonic-gate 		csb->csb_cmds[6] = sector + (len / ch->fdc_sec_size) - 1;
55120Sstevel@tonic-gate 	else
55130Sstevel@tonic-gate 		csb->csb_cmds[6] =
55140Sstevel@tonic-gate 		    (uchar_t)max(fdc->c_un->un_chars->fdc_secptrack, sector);
55150Sstevel@tonic-gate 	csb->csb_len = len;
55160Sstevel@tonic-gate 	csb->csb_cmds[7] = GPLN;
55170Sstevel@tonic-gate 	csb->csb_cmds[8] = SSSDTL;
55180Sstevel@tonic-gate 	csb->csb_ncmds = NCBRW;
55190Sstevel@tonic-gate 	csb->csb_len = len;
55200Sstevel@tonic-gate 	csb->csb_maxretry = 2;
55210Sstevel@tonic-gate 	csb->csb_retrys = 0;
55220Sstevel@tonic-gate 	bzero(csb->csb_rslt, NRBRW);
55230Sstevel@tonic-gate 	csb->csb_nrslts = NRBRW;
55240Sstevel@tonic-gate 	csb->csb_opflags = CSB_OFXFEROPS | CSB_OFTIMEIT;
55250Sstevel@tonic-gate 
55260Sstevel@tonic-gate 	/* If platform supports DMA, set up DMA resources */
55270Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_DMA) {
55280Sstevel@tonic-gate 
55290Sstevel@tonic-gate 		mutex_enter(&fdc->c_hilock);
55300Sstevel@tonic-gate 
55310Sstevel@tonic-gate 		attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
55320Sstevel@tonic-gate 		attr.devacc_attr_endian_flags  = DDI_STRUCTURE_BE_ACC;
55330Sstevel@tonic-gate 		attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
55340Sstevel@tonic-gate 
55350Sstevel@tonic-gate 		res = ddi_dma_mem_alloc(fdc->c_dmahandle, len,
5536*7656SSherry.Moore@Sun.COM 		    &attr, DDI_DMA_STREAMING,
5537*7656SSherry.Moore@Sun.COM 		    DDI_DMA_DONTWAIT, 0, &dma_addr, &real_length,
5538*7656SSherry.Moore@Sun.COM 		    &mem_handle);
55390Sstevel@tonic-gate 
55400Sstevel@tonic-gate 		if (res != DDI_SUCCESS) {
55410Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_RW,
5542*7656SSherry.Moore@Sun.COM 			    (C, "fdrw: dma mem alloc failed\n"));
55430Sstevel@tonic-gate 
55440Sstevel@tonic-gate 			fdretcsb(fdc);
55450Sstevel@tonic-gate 			mutex_exit(&fdc->c_hilock);
55460Sstevel@tonic-gate 			return (EIO);
55470Sstevel@tonic-gate 		}
55480Sstevel@tonic-gate 
55490Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_RW, (C, "fdrw: allocated memory"));
55500Sstevel@tonic-gate 
55510Sstevel@tonic-gate 		if (fdstart_dma(fdc, dma_addr, len) != 0) {
55520Sstevel@tonic-gate 			fdretcsb(fdc);
55530Sstevel@tonic-gate 			ddi_dma_mem_free(&mem_handle);
55540Sstevel@tonic-gate 			mutex_exit(&fdc->c_hilock);
55550Sstevel@tonic-gate 			return (-1);
55560Sstevel@tonic-gate 
55570Sstevel@tonic-gate 		}
55580Sstevel@tonic-gate 
55590Sstevel@tonic-gate 		/*
55600Sstevel@tonic-gate 		 * If the command is a write, copy the data to be written to
55610Sstevel@tonic-gate 		 * dma_addr.
55620Sstevel@tonic-gate 		 */
55630Sstevel@tonic-gate 
55640Sstevel@tonic-gate 		if (fdc->c_csb.csb_read == CSB_WRITE) {
55650Sstevel@tonic-gate 			bcopy((char *)bufp, (char *)dma_addr, len);
55660Sstevel@tonic-gate 		}
55670Sstevel@tonic-gate 
55680Sstevel@tonic-gate 		csb->csb_addr = dma_addr;
55690Sstevel@tonic-gate 		mutex_exit(&fdc->c_hilock);
55700Sstevel@tonic-gate 	} else {
55710Sstevel@tonic-gate 		csb->csb_addr = bufp;
55720Sstevel@tonic-gate 	}
55730Sstevel@tonic-gate 
55740Sstevel@tonic-gate 
55750Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_RW, (C, "fdrw: call fdexec\n"));
55760Sstevel@tonic-gate 
55770Sstevel@tonic-gate 	if (fdexec(fdc, FDXC_SLEEP | FDXC_CHECKCHG) != 0) {
55780Sstevel@tonic-gate 		fdretcsb(fdc);
55790Sstevel@tonic-gate 
55800Sstevel@tonic-gate 		if (mem_handle)
55810Sstevel@tonic-gate 			ddi_dma_mem_free(&mem_handle);
55820Sstevel@tonic-gate 
55830Sstevel@tonic-gate 		return (EIO);
55840Sstevel@tonic-gate 
55850Sstevel@tonic-gate 	}
55860Sstevel@tonic-gate 
55870Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_RW, (C, "fdrw: fdexec returned\n"));
55880Sstevel@tonic-gate 
55890Sstevel@tonic-gate 	/*
55900Sstevel@tonic-gate 	 * if DMA was used and the command was a read
55910Sstevel@tonic-gate 	 * copy the results into bufp
55920Sstevel@tonic-gate 	 */
55930Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_DMA) {
55940Sstevel@tonic-gate 		if (fdc->c_csb.csb_read == CSB_READ) {
55950Sstevel@tonic-gate 			bcopy((char *)dma_addr, (char *)bufp, len);
55960Sstevel@tonic-gate 		}
55970Sstevel@tonic-gate 		ddi_dma_mem_free(&mem_handle);
55980Sstevel@tonic-gate 	}
55990Sstevel@tonic-gate 
56000Sstevel@tonic-gate 	if (csb->csb_cmdstat)
56010Sstevel@tonic-gate 		cmdresult = EIO;	/* XXX TBD NYD for now */
56020Sstevel@tonic-gate 
56030Sstevel@tonic-gate 	fdretcsb(fdc);
56040Sstevel@tonic-gate 	return (cmdresult);
56050Sstevel@tonic-gate }
56060Sstevel@tonic-gate 
56070Sstevel@tonic-gate /*
56080Sstevel@tonic-gate  * fdunpacklabel
56090Sstevel@tonic-gate  *	this unpacks a (packed) struct dk_label into a standard dk_label.
56100Sstevel@tonic-gate  */
56110Sstevel@tonic-gate static void
56120Sstevel@tonic-gate fdunpacklabel(struct packed_label *from, struct dk_label *to)
56130Sstevel@tonic-gate {
56140Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_PACK, (C, "fdpacklabel\n"));
56150Sstevel@tonic-gate 	bzero((caddr_t)to, sizeof (*to));
56160Sstevel@tonic-gate 	bcopy((caddr_t)&from->dkl_vname, (caddr_t)to->dkl_asciilabel,
56170Sstevel@tonic-gate 	    sizeof (to->dkl_asciilabel));
56180Sstevel@tonic-gate 	to->dkl_rpm = from->dkl_rpm;	/* rotations per minute */
56190Sstevel@tonic-gate 	to->dkl_pcyl = from->dkl_pcyl;	/* # physical cylinders */
56200Sstevel@tonic-gate 	to->dkl_apc = from->dkl_apc;	/* alternates per cylinder */
56210Sstevel@tonic-gate 	to->dkl_intrlv = from->dkl_intrlv;	/* interleave factor */
56220Sstevel@tonic-gate 	to->dkl_ncyl = from->dkl_ncyl;	/* # of data cylinders */
56230Sstevel@tonic-gate 	to->dkl_acyl = from->dkl_acyl;	/* # of alternate cylinders */
56240Sstevel@tonic-gate 	to->dkl_nhead = from->dkl_nhead; /* # of heads in this partition */
56250Sstevel@tonic-gate 	to->dkl_nsect = from->dkl_nsect; /* # of 512 byte sectors per track */
56260Sstevel@tonic-gate 	/* logical partitions */
56270Sstevel@tonic-gate 	bcopy((caddr_t)from->dkl_map, (caddr_t)to->dkl_map,
56280Sstevel@tonic-gate 	    sizeof (struct dk_map32) * NDKMAP);
56290Sstevel@tonic-gate 	to->dkl_vtoc = from->dkl_vtoc;
56300Sstevel@tonic-gate }
56310Sstevel@tonic-gate 
56320Sstevel@tonic-gate static struct fdctlr *
56330Sstevel@tonic-gate fd_getctlr(dev_t dev)
56340Sstevel@tonic-gate {
56350Sstevel@tonic-gate 
56360Sstevel@tonic-gate 	struct fdctlr *fdc = fdctlrs;
56370Sstevel@tonic-gate 	int ctlr = FDCTLR(dev);
56380Sstevel@tonic-gate 
56390Sstevel@tonic-gate 	while (fdc) {
56400Sstevel@tonic-gate 		if (ddi_get_instance(fdc->c_dip) == ctlr)
56410Sstevel@tonic-gate 			return (fdc);
56420Sstevel@tonic-gate 		fdc = fdc->c_next;
56430Sstevel@tonic-gate 	}
56440Sstevel@tonic-gate 	return (fdc);
56450Sstevel@tonic-gate }
56460Sstevel@tonic-gate 
56470Sstevel@tonic-gate static int
56480Sstevel@tonic-gate fd_unit_is_open(struct fdunit *un)
56490Sstevel@tonic-gate {
56500Sstevel@tonic-gate 	int i;
56510Sstevel@tonic-gate 	for (i = 0; i < NDKMAP; i++)
56520Sstevel@tonic-gate 		if (un->un_lyropen[i])
56530Sstevel@tonic-gate 			return (1);
56540Sstevel@tonic-gate 	for (i = 0; i < OTYPCNT - 1; i++)
56550Sstevel@tonic-gate 		if (un->un_regopen[i])
56560Sstevel@tonic-gate 			return (1);
56570Sstevel@tonic-gate 	return (0);
56580Sstevel@tonic-gate }
56590Sstevel@tonic-gate 
56600Sstevel@tonic-gate /*
56610Sstevel@tonic-gate  * Return the a vtoc structure in *vtoc.
56620Sstevel@tonic-gate  * The vtoc is built from information in
56630Sstevel@tonic-gate  * the diskette's label.
56640Sstevel@tonic-gate  */
56650Sstevel@tonic-gate static void
56660Sstevel@tonic-gate fd_build_user_vtoc(struct fdunit *un, struct vtoc *vtoc)
56670Sstevel@tonic-gate {
56680Sstevel@tonic-gate 	int i;
56690Sstevel@tonic-gate 	int nblks;			/* DEV_BSIZE sectors per cylinder */
56700Sstevel@tonic-gate 	struct dk_map2 *lpart;
56710Sstevel@tonic-gate 	struct dk_map32	*lmap;
56720Sstevel@tonic-gate 	struct partition *vpart;
56730Sstevel@tonic-gate 
56740Sstevel@tonic-gate 	bzero(vtoc, sizeof (struct vtoc));
56750Sstevel@tonic-gate 
56760Sstevel@tonic-gate 	/* Initialize info. needed by mboot.  (unsupported) */
56770Sstevel@tonic-gate 	vtoc->v_bootinfo[0] = un->un_label.dkl_vtoc.v_bootinfo[0];
56780Sstevel@tonic-gate 	vtoc->v_bootinfo[1] = un->un_label.dkl_vtoc.v_bootinfo[1];
56790Sstevel@tonic-gate 	vtoc->v_bootinfo[2] = un->un_label.dkl_vtoc.v_bootinfo[2];
56800Sstevel@tonic-gate 
56810Sstevel@tonic-gate 	/* Fill in vtoc sanity and version information */
56820Sstevel@tonic-gate 	vtoc->v_sanity		= un->un_label.dkl_vtoc.v_sanity;
56830Sstevel@tonic-gate 	vtoc->v_version		= un->un_label.dkl_vtoc.v_version;
56840Sstevel@tonic-gate 
56850Sstevel@tonic-gate 	/* Copy the volume name */
56860Sstevel@tonic-gate 	bcopy(un->un_label.dkl_vtoc.v_volume,
56870Sstevel@tonic-gate 	    vtoc->v_volume, LEN_DKL_VVOL);
56880Sstevel@tonic-gate 
56890Sstevel@tonic-gate 	/*
56900Sstevel@tonic-gate 	 * The dk_map32 structure is based on DEV_BSIZE byte blocks.
56910Sstevel@tonic-gate 	 * However, medium density diskettes have 1024 byte blocks.
56920Sstevel@tonic-gate 	 * The number of sectors per partition listed in the dk_map32 structure
56930Sstevel@tonic-gate 	 * accounts for this by multiplying the number of 1024 byte
56940Sstevel@tonic-gate 	 * blocks by 2.  (See the packed_label initializations.)  The
56950Sstevel@tonic-gate 	 * 1024 byte block size can not be listed for medium density
56960Sstevel@tonic-gate 	 * diskettes because the kernel is hard coded for DEV_BSIZE
56970Sstevel@tonic-gate 	 * blocks.
56980Sstevel@tonic-gate 	 */
56990Sstevel@tonic-gate 	vtoc->v_sectorsz = DEV_BSIZE;
57000Sstevel@tonic-gate 	vtoc->v_nparts = un->un_label.dkl_vtoc.v_nparts;
57010Sstevel@tonic-gate 
57020Sstevel@tonic-gate 	/* Copy the reserved space */
57030Sstevel@tonic-gate 	bcopy(un->un_label.dkl_vtoc.v_reserved,
57040Sstevel@tonic-gate 	    vtoc->v_reserved, sizeof (un->un_label.dkl_vtoc.v_reserved));
57050Sstevel@tonic-gate 	/*
57060Sstevel@tonic-gate 	 * Convert partitioning information.
57070Sstevel@tonic-gate 	 *
57080Sstevel@tonic-gate 	 * Note the conversion from starting cylinder number
57090Sstevel@tonic-gate 	 * to starting sector number.
57100Sstevel@tonic-gate 	 */
57110Sstevel@tonic-gate 	lmap = un->un_label.dkl_map;
57120Sstevel@tonic-gate 	lpart = un->un_label.dkl_vtoc.v_part;
57130Sstevel@tonic-gate 	vpart = vtoc->v_part;
57140Sstevel@tonic-gate 
57150Sstevel@tonic-gate 	nblks = (un->un_chars->fdc_nhead * un->un_chars->fdc_secptrack *
5716*7656SSherry.Moore@Sun.COM 	    un->un_chars->fdc_sec_size) / DEV_BSIZE;
57170Sstevel@tonic-gate 
57180Sstevel@tonic-gate 	for (i = 0; i < V_NUMPAR; i++) {
57190Sstevel@tonic-gate 		vpart->p_tag	= lpart->p_tag;
57200Sstevel@tonic-gate 		vpart->p_flag	= lpart->p_flag;
57210Sstevel@tonic-gate 		vpart->p_start	= lmap->dkl_cylno * nblks;
57220Sstevel@tonic-gate 		vpart->p_size	= lmap->dkl_nblk;
57230Sstevel@tonic-gate 
57240Sstevel@tonic-gate 		lmap++;
57250Sstevel@tonic-gate 		lpart++;
57260Sstevel@tonic-gate 		vpart++;
57270Sstevel@tonic-gate 	}
57280Sstevel@tonic-gate 
57290Sstevel@tonic-gate 	/* Initialize timestamp and label */
57300Sstevel@tonic-gate 	bcopy(un->un_label.dkl_vtoc.v_timestamp,
57310Sstevel@tonic-gate 	    vtoc->timestamp, sizeof (vtoc->timestamp));
57320Sstevel@tonic-gate 
57330Sstevel@tonic-gate 	bcopy(un->un_label.dkl_asciilabel,
57340Sstevel@tonic-gate 	    vtoc->v_asciilabel, LEN_DKL_ASCII);
57350Sstevel@tonic-gate }
57360Sstevel@tonic-gate 
57370Sstevel@tonic-gate /*
57380Sstevel@tonic-gate  * Build a label out of a vtoc structure.
57390Sstevel@tonic-gate  */
57400Sstevel@tonic-gate static int
57410Sstevel@tonic-gate fd_build_label_vtoc(struct fdunit *un, struct vtoc *vtoc)
57420Sstevel@tonic-gate {
57430Sstevel@tonic-gate 	struct dk_map32		*lmap;
57440Sstevel@tonic-gate 	struct dk_map2		*lpart;
57450Sstevel@tonic-gate 	struct partition	*vpart;
57460Sstevel@tonic-gate 	int			nblks;	/* no. blocks per cylinder */
57470Sstevel@tonic-gate 	int			ncyl;
57480Sstevel@tonic-gate 	int			i;
57490Sstevel@tonic-gate 	short	 sum, *sp;
57500Sstevel@tonic-gate 
57510Sstevel@tonic-gate 	/* Sanity-check the vtoc */
57520Sstevel@tonic-gate 	if ((vtoc->v_sanity != VTOC_SANE) ||
5753*7656SSherry.Moore@Sun.COM 	    (vtoc->v_nparts > NDKMAP) || (vtoc->v_nparts <= 0)) {
57540Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_IOCT,
57550Sstevel@tonic-gate 		    (C, "fd_build_label:  sanity check on vtoc failed\n"));
57560Sstevel@tonic-gate 		return (EINVAL);
57570Sstevel@tonic-gate 	}
57580Sstevel@tonic-gate 
57590Sstevel@tonic-gate 	nblks = (un->un_chars->fdc_nhead * un->un_chars->fdc_secptrack *
5760*7656SSherry.Moore@Sun.COM 	    un->un_chars->fdc_sec_size) / DEV_BSIZE;
57610Sstevel@tonic-gate 
57620Sstevel@tonic-gate 	vpart = vtoc->v_part;
57630Sstevel@tonic-gate 
57640Sstevel@tonic-gate 	/*
57650Sstevel@tonic-gate 	 * Check the partition information in the vtoc.  The starting sectors
57660Sstevel@tonic-gate 	 * must lie along partition boundaries. (NDKMAP entries are checked
57670Sstevel@tonic-gate 	 * to ensure that the unused entries are set to 0 if vtoc->v_nparts
57680Sstevel@tonic-gate 	 * is less than NDKMAP)
57690Sstevel@tonic-gate 	 */
57700Sstevel@tonic-gate 
57710Sstevel@tonic-gate 	for (i = 0; i < NDKMAP; i++) {
57720Sstevel@tonic-gate 		if ((vpart->p_start % nblks) != 0) {
57730Sstevel@tonic-gate 			return (EINVAL);
57740Sstevel@tonic-gate 		}
57750Sstevel@tonic-gate 		ncyl = vpart->p_start % nblks;
57760Sstevel@tonic-gate 		ncyl += vpart->p_size % nblks;
57770Sstevel@tonic-gate 		if ((vpart->p_size % nblks) != 0)
57780Sstevel@tonic-gate 			ncyl++;
57790Sstevel@tonic-gate 		if (ncyl > un->un_chars->fdc_ncyl) {
57800Sstevel@tonic-gate 			return (EINVAL);
57810Sstevel@tonic-gate 		}
57820Sstevel@tonic-gate 		vpart++;
57830Sstevel@tonic-gate 	}
57840Sstevel@tonic-gate 
57850Sstevel@tonic-gate 	/*
57860Sstevel@tonic-gate 	 * reinitialize the existing label
57870Sstevel@tonic-gate 	 */
57880Sstevel@tonic-gate 	bzero(&un->un_label, sizeof (un->un_label));
57890Sstevel@tonic-gate 
57900Sstevel@tonic-gate 	/* Put appropriate vtoc structure fields into the disk label */
57910Sstevel@tonic-gate 	un->un_label.dkl_vtoc.v_bootinfo[0] = (uint32_t)vtoc->v_bootinfo[0];
57920Sstevel@tonic-gate 	un->un_label.dkl_vtoc.v_bootinfo[1] = (uint32_t)vtoc->v_bootinfo[1];
57930Sstevel@tonic-gate 	un->un_label.dkl_vtoc.v_bootinfo[2] = (uint32_t)vtoc->v_bootinfo[2];
57940Sstevel@tonic-gate 
57950Sstevel@tonic-gate 	un->un_label.dkl_vtoc.v_sanity = vtoc->v_sanity;
57960Sstevel@tonic-gate 	un->un_label.dkl_vtoc.v_version = vtoc->v_version;
57970Sstevel@tonic-gate 
57980Sstevel@tonic-gate 	bcopy(vtoc->v_volume, un->un_label.dkl_vtoc.v_volume, LEN_DKL_VVOL);
57990Sstevel@tonic-gate 
58000Sstevel@tonic-gate 	un->un_label.dkl_vtoc.v_nparts = vtoc->v_nparts;
58010Sstevel@tonic-gate 
58020Sstevel@tonic-gate 	bcopy(vtoc->v_reserved, un->un_label.dkl_vtoc.v_reserved,
58030Sstevel@tonic-gate 	    sizeof (un->un_label.dkl_vtoc.v_reserved));
58040Sstevel@tonic-gate 
58050Sstevel@tonic-gate 	/*
58060Sstevel@tonic-gate 	 * Initialize cylinder information in the label.
58070Sstevel@tonic-gate 	 * Note the conversion from starting sector number
58080Sstevel@tonic-gate 	 * to starting cylinder number.
58090Sstevel@tonic-gate 	 * Return error if division results in a remainder.
58100Sstevel@tonic-gate 	 */
58110Sstevel@tonic-gate 	lmap = un->un_label.dkl_map;
58120Sstevel@tonic-gate 	lpart = un->un_label.dkl_vtoc.v_part;
58130Sstevel@tonic-gate 	vpart = vtoc->v_part;
58140Sstevel@tonic-gate 
58150Sstevel@tonic-gate 	for (i = 0; i < (int)vtoc->v_nparts; i++) {
58160Sstevel@tonic-gate 		lpart->p_tag  = vtoc->v_part[i].p_tag;
58170Sstevel@tonic-gate 		lpart->p_flag = vtoc->v_part[i].p_flag;
58180Sstevel@tonic-gate 		lmap->dkl_cylno = vpart->p_start / nblks;
58190Sstevel@tonic-gate 		lmap->dkl_nblk = vpart->p_size;
58200Sstevel@tonic-gate 
58210Sstevel@tonic-gate 		lmap++;
58220Sstevel@tonic-gate 		lpart++;
58230Sstevel@tonic-gate 		vpart++;
58240Sstevel@tonic-gate 	}
58250Sstevel@tonic-gate 
58260Sstevel@tonic-gate 	/* Copy the timestamp and ascii label */
58270Sstevel@tonic-gate 	for (i = 0; i < NDKMAP; i++) {
58280Sstevel@tonic-gate 		un->un_label.dkl_vtoc.v_timestamp[i] = vtoc->timestamp[i];
58290Sstevel@tonic-gate 	}
58300Sstevel@tonic-gate 
58310Sstevel@tonic-gate 
58320Sstevel@tonic-gate 	bcopy(vtoc->v_asciilabel, un->un_label.dkl_asciilabel, LEN_DKL_ASCII);
58330Sstevel@tonic-gate 
58340Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_IOCT,
5835*7656SSherry.Moore@Sun.COM 	    (C, "fd_build_label: asciilabel %s\n",
5836*7656SSherry.Moore@Sun.COM 	    un->un_label.dkl_asciilabel));
58370Sstevel@tonic-gate 
58380Sstevel@tonic-gate 	/* Initialize the magic number */
58390Sstevel@tonic-gate 	un->un_label.dkl_magic = DKL_MAGIC;
58400Sstevel@tonic-gate 
58410Sstevel@tonic-gate 	un->un_label.dkl_pcyl = un->un_chars->fdc_ncyl;
58420Sstevel@tonic-gate 
58430Sstevel@tonic-gate 	/*
58440Sstevel@tonic-gate 	 * The fdc_secptrack filed of the fd_char structure is the number
58450Sstevel@tonic-gate 	 * of sectors per track where the sectors are fdc_sec_size.  The
58460Sstevel@tonic-gate 	 * dkl_nsect field of the dk_label structure is the number of
58470Sstevel@tonic-gate 	 * 512 (DEVBSIZE) byte sectors per track.
58480Sstevel@tonic-gate 	 */
58490Sstevel@tonic-gate 	un->un_label.dkl_nsect = (un->un_chars->fdc_secptrack *
5850*7656SSherry.Moore@Sun.COM 	    un->un_chars->fdc_sec_size) / DEV_BSIZE;
58510Sstevel@tonic-gate 
58520Sstevel@tonic-gate 
58530Sstevel@tonic-gate 	un->un_label.dkl_ncyl = un->un_label.dkl_pcyl;
58540Sstevel@tonic-gate 	un->un_label.dkl_nhead = un->un_chars->fdc_nhead;
58550Sstevel@tonic-gate 	un->un_label.dkl_rpm = un->un_chars->fdc_medium ? 360 : 300;
58560Sstevel@tonic-gate 	un->un_label.dkl_intrlv = 1;
58570Sstevel@tonic-gate 
58580Sstevel@tonic-gate 	/* Create the checksum */
58590Sstevel@tonic-gate 	sum = 0;
58600Sstevel@tonic-gate 	un->un_label.dkl_cksum = 0;
58610Sstevel@tonic-gate 	sp = (short *)&un->un_label;
58620Sstevel@tonic-gate 	i = sizeof (struct dk_label)/sizeof (short);
58630Sstevel@tonic-gate 	while (i--) {
58640Sstevel@tonic-gate 		sum ^= *sp++;
58650Sstevel@tonic-gate 	}
58660Sstevel@tonic-gate 	un->un_label.dkl_cksum = sum;
58670Sstevel@tonic-gate 
58680Sstevel@tonic-gate 	return (0);
58690Sstevel@tonic-gate }
58700Sstevel@tonic-gate 
58710Sstevel@tonic-gate /*
58720Sstevel@tonic-gate  * Check for auxio register node
58730Sstevel@tonic-gate  */
58740Sstevel@tonic-gate 
58750Sstevel@tonic-gate int
58760Sstevel@tonic-gate fd_isauxiodip(dev_info_t *dip)
58770Sstevel@tonic-gate {
58780Sstevel@tonic-gate 	if (strcmp(ddi_get_name(dip), "auxio") == 0 ||
58790Sstevel@tonic-gate 	    strcmp(ddi_get_name(dip), "auxiliary-io") == 0) {
58800Sstevel@tonic-gate 		return (1);
58810Sstevel@tonic-gate 	}
58820Sstevel@tonic-gate 	return (0);
58830Sstevel@tonic-gate }
58840Sstevel@tonic-gate 
58850Sstevel@tonic-gate /*
58860Sstevel@tonic-gate  * Search for auxio register node, then for address property
58870Sstevel@tonic-gate  */
58880Sstevel@tonic-gate 
58890Sstevel@tonic-gate caddr_t
58900Sstevel@tonic-gate fd_getauxiova(dev_info_t *dip)
58910Sstevel@tonic-gate {
58920Sstevel@tonic-gate 	dev_info_t *auxdip;
58930Sstevel@tonic-gate 	caddr_t addr;
58940Sstevel@tonic-gate 
58950Sstevel@tonic-gate 	/*
58960Sstevel@tonic-gate 	 * Search sibling list, which happens to be safe inside attach
58970Sstevel@tonic-gate 	 */
58980Sstevel@tonic-gate 	auxdip = ddi_get_child(ddi_get_parent(dip));
58990Sstevel@tonic-gate 	while (auxdip) {
59000Sstevel@tonic-gate 		if (fd_isauxiodip(auxdip))
59010Sstevel@tonic-gate 			break;
59020Sstevel@tonic-gate 		auxdip = ddi_get_next_sibling(auxdip);
59030Sstevel@tonic-gate 	}
59040Sstevel@tonic-gate 
59050Sstevel@tonic-gate 	if (auxdip == NULL)
59060Sstevel@tonic-gate 		return (NULL);
59070Sstevel@tonic-gate 
5908483Spc157239 	addr = (caddr_t)(uintptr_t)(caddr32_t)ddi_getprop(DDI_DEV_T_ANY,
5909*7656SSherry.Moore@Sun.COM 	    auxdip, DDI_PROP_DONTPASS, "address", 0);
59100Sstevel@tonic-gate 
59110Sstevel@tonic-gate 	return (addr);
59120Sstevel@tonic-gate }
59130Sstevel@tonic-gate 
59140Sstevel@tonic-gate 
59150Sstevel@tonic-gate /*
59160Sstevel@tonic-gate  * set_rotational speed
59170Sstevel@tonic-gate  * 300 rpm for high and low density.
59180Sstevel@tonic-gate  * 360 rpm for medium density.
59190Sstevel@tonic-gate  * for now, we assume that 3rd density is supported only for Sun4M,
59200Sstevel@tonic-gate  * not for Clones. (else we would have to check for 82077, and do
59210Sstevel@tonic-gate  * specific things for the MEDIUM_DENSITY BIT for clones.
59220Sstevel@tonic-gate  * this code should not break CLONES.
59230Sstevel@tonic-gate  *
59240Sstevel@tonic-gate  * REMARK: there is a SOny requirement, to deselect the drive then
59250Sstevel@tonic-gate  * select it again after the medium density change, since the
59260Sstevel@tonic-gate  * leading edge of the select line latches the rotational Speed.
59270Sstevel@tonic-gate  * then after that, we have to wait 500 ms for the rotation to
59280Sstevel@tonic-gate  * stabilize.
59290Sstevel@tonic-gate  *
59300Sstevel@tonic-gate  */
59310Sstevel@tonic-gate static void
59320Sstevel@tonic-gate set_rotational_speed(struct fdctlr *fdc, int unit)
59330Sstevel@tonic-gate {
59340Sstevel@tonic-gate 	int check;
59350Sstevel@tonic-gate 	int is_medium;
59360Sstevel@tonic-gate 
59370Sstevel@tonic-gate 	ASSERT(fdc->c_un->un_unit_no == unit);
59380Sstevel@tonic-gate 
59390Sstevel@tonic-gate 	/*
59400Sstevel@tonic-gate 	 * if we do not have a Sun4m, medium density is not supported.
59410Sstevel@tonic-gate 	 */
59420Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_MACHIO)
59430Sstevel@tonic-gate 		return;
59440Sstevel@tonic-gate 
59450Sstevel@tonic-gate 	/*
59460Sstevel@tonic-gate 	 * if FDUNIT_SET_SPEED is set, set the speed.
59470Sstevel@tonic-gate 	 * else,
59480Sstevel@tonic-gate 	 *	if there is a change, do it, if not leave it alone.
59490Sstevel@tonic-gate 	 *	there is a change if un->un_chars->fdc_medium does not match
59500Sstevel@tonic-gate 	 *	un->un_flags & FDUNIT_MEDIUM
59510Sstevel@tonic-gate 	 *	un->un_flags & FDUNIT_MEDIUM specifies the last setting.
59520Sstevel@tonic-gate 	 *	un->un_chars->fdc_medium specifies next setting.
59530Sstevel@tonic-gate 	 *	if there is a change, wait 500ms according to Sony spec.
59540Sstevel@tonic-gate 	 */
59550Sstevel@tonic-gate 
59560Sstevel@tonic-gate 	is_medium = fdc->c_un->un_chars->fdc_medium;
59570Sstevel@tonic-gate 
59580Sstevel@tonic-gate 	if (fdc->c_un->un_flags & FDUNIT_SET_SPEED) {
59590Sstevel@tonic-gate 		check = 1;
59600Sstevel@tonic-gate 	} else {
59610Sstevel@tonic-gate 		check = is_medium ^
5962*7656SSherry.Moore@Sun.COM 		    ((fdc->c_un->un_flags & FDUNIT_MEDIUM) ? 1 : 0);
59630Sstevel@tonic-gate 
59640Sstevel@tonic-gate 		/* Set the un_flags if necessary */
59650Sstevel@tonic-gate 
59660Sstevel@tonic-gate 		if (check)
59670Sstevel@tonic-gate 			fdc->c_un->un_flags ^= FDUNIT_MEDIUM;
59680Sstevel@tonic-gate 	}
59690Sstevel@tonic-gate 
59700Sstevel@tonic-gate 	fdc->c_un->un_flags &= ~FDUNIT_SET_SPEED;
59710Sstevel@tonic-gate 
59720Sstevel@tonic-gate 
59730Sstevel@tonic-gate 	if (check) {
59740Sstevel@tonic-gate 
59750Sstevel@tonic-gate 		fdselect(fdc, unit, 0);
59760Sstevel@tonic-gate 		drv_usecwait(5);
59770Sstevel@tonic-gate 
59780Sstevel@tonic-gate 		if ((fdc->c_fdtype & FDCTYPE_AUXIOMASK) == FDCTYPE_SLAVIO) {
59790Sstevel@tonic-gate 			Set_dor(fdc, MEDIUM_DENSITY, is_medium);
59800Sstevel@tonic-gate 		}
59810Sstevel@tonic-gate 
59820Sstevel@tonic-gate 		if ((fdc->c_fdtype & FDCTYPE_AUXIOMASK) == FDCTYPE_CHEERIO) {
59830Sstevel@tonic-gate 			if (is_medium) {
59840Sstevel@tonic-gate 				Set_auxio(fdc, AUX_MEDIUM_DENSITY);
59850Sstevel@tonic-gate 			} else {
59860Sstevel@tonic-gate 				Set_auxio(fdc, AUX_HIGH_DENSITY);
59870Sstevel@tonic-gate 			}
59880Sstevel@tonic-gate 
59890Sstevel@tonic-gate 		}
59900Sstevel@tonic-gate 
59910Sstevel@tonic-gate 		if (is_medium) {
59920Sstevel@tonic-gate 			drv_usecwait(5);
59930Sstevel@tonic-gate 		}
59940Sstevel@tonic-gate 
59950Sstevel@tonic-gate 		fdselect(fdc, unit, 1);	/* Sony requirement */
59960Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "rotation:medium\n"));
59970Sstevel@tonic-gate 		drv_usecwait(500000);
59980Sstevel@tonic-gate 	}
59990Sstevel@tonic-gate }
60000Sstevel@tonic-gate 
60010Sstevel@tonic-gate static void
60020Sstevel@tonic-gate fd_media_watch(void *arg)
60030Sstevel@tonic-gate {
60040Sstevel@tonic-gate 	dev_t		dev;
60050Sstevel@tonic-gate 	struct fdunit *un;
60060Sstevel@tonic-gate 	struct fdctlr *fdc;
60070Sstevel@tonic-gate 	int		unit;
60080Sstevel@tonic-gate 
60090Sstevel@tonic-gate 	dev = (dev_t)arg;
60100Sstevel@tonic-gate 	fdc = fd_getctlr(dev);
60110Sstevel@tonic-gate 	unit = fdc->c_un->un_unit_no;
60120Sstevel@tonic-gate 	un = fdc->c_un;
60130Sstevel@tonic-gate 
60140Sstevel@tonic-gate 	mutex_enter(&fdc->c_lolock);
60150Sstevel@tonic-gate 
60160Sstevel@tonic-gate 	if (un->un_media_timeout_id == 0) {
60170Sstevel@tonic-gate 		/*
60180Sstevel@tonic-gate 		 * Untimeout is about to be called.
60190Sstevel@tonic-gate 		 * Don't call fd_get_media_state again
60200Sstevel@tonic-gate 		 */
60210Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
60220Sstevel@tonic-gate 		return;
60230Sstevel@tonic-gate 	}
60240Sstevel@tonic-gate 
60250Sstevel@tonic-gate 
60260Sstevel@tonic-gate 	un->un_media_state = fd_get_media_state(fdc, unit);
60270Sstevel@tonic-gate 	cv_broadcast(&fdc->c_statecv);
60280Sstevel@tonic-gate 
60290Sstevel@tonic-gate 	mutex_exit(&fdc->c_lolock);
60300Sstevel@tonic-gate 
60310Sstevel@tonic-gate 	if (un->un_media_timeout) {
60320Sstevel@tonic-gate 		un->un_media_timeout_id = timeout(fd_media_watch,
6033*7656SSherry.Moore@Sun.COM 		    (void *)(ulong_t)dev, un->un_media_timeout);
60340Sstevel@tonic-gate 	}
60350Sstevel@tonic-gate }
60360Sstevel@tonic-gate 
60370Sstevel@tonic-gate enum dkio_state
60380Sstevel@tonic-gate fd_get_media_state(struct fdctlr *fdc, int unit)
60390Sstevel@tonic-gate {
60400Sstevel@tonic-gate 	enum dkio_state state;
60410Sstevel@tonic-gate 
60420Sstevel@tonic-gate 	ASSERT(fdc->c_un->un_unit_no == unit);
60430Sstevel@tonic-gate 
60440Sstevel@tonic-gate 	if (fdsense_chng(fdc, unit)) {
60450Sstevel@tonic-gate 		/* check disk only if DSKCHG "high" */
60460Sstevel@tonic-gate 		if (fdcheckdisk(fdc, unit)) {
60470Sstevel@tonic-gate 			state = DKIO_EJECTED;
60480Sstevel@tonic-gate 		} else {
60490Sstevel@tonic-gate 			state = DKIO_INSERTED;
60500Sstevel@tonic-gate 		}
60510Sstevel@tonic-gate 	} else {
60520Sstevel@tonic-gate 		state = DKIO_INSERTED;
60530Sstevel@tonic-gate 	}
60540Sstevel@tonic-gate 	return (state);
60550Sstevel@tonic-gate }
60560Sstevel@tonic-gate 
60570Sstevel@tonic-gate static int
60580Sstevel@tonic-gate fd_check_media(dev_t dev, enum dkio_state state)
60590Sstevel@tonic-gate {
60600Sstevel@tonic-gate 	struct fdunit *un;
60610Sstevel@tonic-gate 	struct fdctlr *fdc;
60620Sstevel@tonic-gate 	int		unit;
60630Sstevel@tonic-gate 
60640Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_RW, (C, "fd_check_media: start\n"));
60650Sstevel@tonic-gate 
60660Sstevel@tonic-gate 	fdc = fd_getctlr(dev);
60670Sstevel@tonic-gate 	unit = fdc->c_un->un_unit_no;
60680Sstevel@tonic-gate 	un = fdc->c_un;
60690Sstevel@tonic-gate 
60700Sstevel@tonic-gate 	mutex_enter(&fdc->c_lolock);
60710Sstevel@tonic-gate 
60720Sstevel@tonic-gate 	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
60730Sstevel@tonic-gate 
60740Sstevel@tonic-gate 	if (fdc->c_un->un_state == FD_STATE_STOPPED) {
60750Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
60760Sstevel@tonic-gate 		if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
6077*7656SSherry.Moore@Sun.COM 		    != DDI_SUCCESS) {
60780Sstevel@tonic-gate 			FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power change \
6079*7656SSherry.Moore@Sun.COM 			    failed. \n"));
60800Sstevel@tonic-gate 
60810Sstevel@tonic-gate 			(void) pm_idle_component(fdc->c_dip, 0);
60820Sstevel@tonic-gate 			return (EIO);
60830Sstevel@tonic-gate 		}
60840Sstevel@tonic-gate 
60850Sstevel@tonic-gate 		mutex_enter(&fdc->c_lolock);
60860Sstevel@tonic-gate 	}
60870Sstevel@tonic-gate 
60880Sstevel@tonic-gate 	un->un_media_state = fd_get_media_state(fdc, unit);
60890Sstevel@tonic-gate 
60900Sstevel@tonic-gate 	/* turn on timeout */
60910Sstevel@tonic-gate 	un->un_media_timeout = drv_usectohz(fd_check_media_time);
60920Sstevel@tonic-gate 	un->un_media_timeout_id = timeout(fd_media_watch,
6093*7656SSherry.Moore@Sun.COM 	    (void *)(ulong_t)dev, un->un_media_timeout);
60940Sstevel@tonic-gate 
60950Sstevel@tonic-gate 	while (un->un_media_state == state) {
60960Sstevel@tonic-gate 		if (cv_wait_sig(&fdc->c_statecv, &fdc->c_lolock) == 0) {
60970Sstevel@tonic-gate 			un->un_media_timeout = 0;
60980Sstevel@tonic-gate 			mutex_exit(&fdc->c_lolock);
60990Sstevel@tonic-gate 			return (EINTR);
61000Sstevel@tonic-gate 		}
61010Sstevel@tonic-gate 	}
61020Sstevel@tonic-gate 
61030Sstevel@tonic-gate 	if (un->un_media_timeout_id) {
61040Sstevel@tonic-gate 		timeout_id_t timeid = un->un_media_timeout_id;
61050Sstevel@tonic-gate 		un->un_media_timeout_id = 0;
61060Sstevel@tonic-gate 
61070Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
61080Sstevel@tonic-gate 		(void) untimeout(timeid);
61090Sstevel@tonic-gate 		mutex_enter(&fdc->c_lolock);
61100Sstevel@tonic-gate 	}
61110Sstevel@tonic-gate 
61120Sstevel@tonic-gate 	if (un->un_media_state == DKIO_INSERTED) {
61130Sstevel@tonic-gate 		if (fdgetlabel(fdc, unit)) {
61140Sstevel@tonic-gate 			mutex_exit(&fdc->c_lolock);
61150Sstevel@tonic-gate 			return (EIO);
61160Sstevel@tonic-gate 		}
61170Sstevel@tonic-gate 	}
61180Sstevel@tonic-gate 	mutex_exit(&fdc->c_lolock);
61190Sstevel@tonic-gate 
61200Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_RW, (C, "fd_check_media: end\n"));
61210Sstevel@tonic-gate 	return (0);
61220Sstevel@tonic-gate }
61230Sstevel@tonic-gate 
61240Sstevel@tonic-gate /*
61250Sstevel@tonic-gate  * fd_get_media_info :
61260Sstevel@tonic-gate  * 	Collects medium information for
61270Sstevel@tonic-gate  *	DKIOCGMEDIAINFO ioctl.
61280Sstevel@tonic-gate  */
61290Sstevel@tonic-gate 
61300Sstevel@tonic-gate static int
61310Sstevel@tonic-gate fd_get_media_info(struct fdunit *un, caddr_t buf, int flag)
61320Sstevel@tonic-gate {
61330Sstevel@tonic-gate 	struct dk_minfo media_info;
61340Sstevel@tonic-gate 	int err = 0;
61350Sstevel@tonic-gate 
61360Sstevel@tonic-gate 	media_info.dki_media_type = DK_FLOPPY;
61370Sstevel@tonic-gate 	media_info.dki_lbsize = un->un_chars->fdc_sec_size;
61380Sstevel@tonic-gate 	media_info.dki_capacity = un->un_chars->fdc_ncyl *
6139*7656SSherry.Moore@Sun.COM 	    un->un_chars->fdc_secptrack * un->un_chars->fdc_nhead;
61400Sstevel@tonic-gate 
61410Sstevel@tonic-gate 	if (ddi_copyout((caddr_t)&media_info, buf,
6142*7656SSherry.Moore@Sun.COM 	    sizeof (struct dk_minfo), flag))
6143*7656SSherry.Moore@Sun.COM 		err = EFAULT;
61440Sstevel@tonic-gate 	return (err);
61450Sstevel@tonic-gate }
61460Sstevel@tonic-gate 
61470Sstevel@tonic-gate /*
61480Sstevel@tonic-gate  * fd_power :
61490Sstevel@tonic-gate  *	Power entry point of fd driver.
61500Sstevel@tonic-gate  */
61510Sstevel@tonic-gate 
61520Sstevel@tonic-gate static int
61530Sstevel@tonic-gate fd_power(dev_info_t *dip, int component, int level)
61540Sstevel@tonic-gate {
61550Sstevel@tonic-gate 
61560Sstevel@tonic-gate 	struct fdctlr *fdc;
61570Sstevel@tonic-gate 	int instance;
61580Sstevel@tonic-gate 	int rval;
61590Sstevel@tonic-gate 
61600Sstevel@tonic-gate 	if ((level < PM_LEVEL_OFF) || (level > PM_LEVEL_ON) ||
6161*7656SSherry.Moore@Sun.COM 	    (component != 0)) {
61620Sstevel@tonic-gate 		return (DDI_FAILURE);
61630Sstevel@tonic-gate 	}
61640Sstevel@tonic-gate 
61650Sstevel@tonic-gate 	instance = ddi_get_instance(dip);
61660Sstevel@tonic-gate 	fdc = fd_getctlr(instance << FDINSTSHIFT);
61670Sstevel@tonic-gate 	if (fdc->c_un == NULL)
61680Sstevel@tonic-gate 		return (DDI_FAILURE);
61690Sstevel@tonic-gate 
61700Sstevel@tonic-gate 	if (level == PM_LEVEL_OFF) {
61710Sstevel@tonic-gate 		rval = fd_pm_lower_power(fdc);
61720Sstevel@tonic-gate 	}
61730Sstevel@tonic-gate 	if (level == PM_LEVEL_ON) {
61740Sstevel@tonic-gate 		rval = fd_pm_raise_power(fdc);
61750Sstevel@tonic-gate 	}
61760Sstevel@tonic-gate 	return (rval);
61770Sstevel@tonic-gate }
61780Sstevel@tonic-gate 
61790Sstevel@tonic-gate /*
61800Sstevel@tonic-gate  * fd_pm_lower_power :
61810Sstevel@tonic-gate  *	This function is called only during pm suspend. At this point,
61820Sstevel@tonic-gate  *	the power management framework thinks the device is idle for
61830Sstevel@tonic-gate  *	long enough to go to a low power mode. If the device is busy,
61840Sstevel@tonic-gate  *	then this function returns DDI_FAILURE.
61850Sstevel@tonic-gate  */
61860Sstevel@tonic-gate 
61870Sstevel@tonic-gate static int
61880Sstevel@tonic-gate fd_pm_lower_power(struct fdctlr *fdc)
61890Sstevel@tonic-gate {
61900Sstevel@tonic-gate 
61910Sstevel@tonic-gate 	mutex_enter(&fdc->c_lolock);
61920Sstevel@tonic-gate 
61930Sstevel@tonic-gate 	if ((fdc->c_un->un_state == FD_STATE_SUSPENDED) ||
6194*7656SSherry.Moore@Sun.COM 	    (fdc->c_un->un_state == FD_STATE_STOPPED)) {
61950Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
61960Sstevel@tonic-gate 		return (DDI_SUCCESS);
61970Sstevel@tonic-gate 	}
61980Sstevel@tonic-gate 
61990Sstevel@tonic-gate 
62000Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "fd_pm_lower_power called\n"));
62010Sstevel@tonic-gate 
62020Sstevel@tonic-gate 	/* if the device is busy then we fail the lower power request */
62030Sstevel@tonic-gate 	if (fdc->c_flags & FDCFLG_BUSY) {
62040Sstevel@tonic-gate 		FDERRPRINT(FDEP_L2, FDEM_PWR, (C, "fd_pm_lower_power : \
62050Sstevel@tonic-gate controller is busy.\n"));
62060Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
62070Sstevel@tonic-gate 		return (DDI_FAILURE);
62080Sstevel@tonic-gate 	}
62090Sstevel@tonic-gate 
62100Sstevel@tonic-gate 	fdc->c_un->un_state = FD_STATE_STOPPED;
62110Sstevel@tonic-gate 
62120Sstevel@tonic-gate 	mutex_exit(&fdc->c_lolock);
62130Sstevel@tonic-gate 	return (DDI_SUCCESS);
62140Sstevel@tonic-gate }
62150Sstevel@tonic-gate 
62160Sstevel@tonic-gate /*
62170Sstevel@tonic-gate  * fd_pm_raise_power :
62180Sstevel@tonic-gate  *	This function performs the necessary steps for resuming a
62190Sstevel@tonic-gate  *	device, either from pm suspend or CPR. Here the controller
62200Sstevel@tonic-gate  *	is reset, initialized and the state is set to FD_STATE_NORMAL.
62210Sstevel@tonic-gate  */
62220Sstevel@tonic-gate 
62230Sstevel@tonic-gate static int
62240Sstevel@tonic-gate fd_pm_raise_power(struct fdctlr *fdc)
62250Sstevel@tonic-gate {
62260Sstevel@tonic-gate 
62270Sstevel@tonic-gate 	struct fdunit *un = fdc->c_un;
62280Sstevel@tonic-gate 	int unit;
62290Sstevel@tonic-gate 
62300Sstevel@tonic-gate 	FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "fd_pm_raise_power called\n"));
62310Sstevel@tonic-gate 	mutex_enter(&fdc->c_lolock);
62320Sstevel@tonic-gate 	fdgetcsb(fdc);
62330Sstevel@tonic-gate 
62340Sstevel@tonic-gate 	/* Reset the dma engine */
62350Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_DMA) {
62360Sstevel@tonic-gate 		mutex_enter(&fdc->c_hilock);
62370Sstevel@tonic-gate 		reset_dma_controller(fdc);
62380Sstevel@tonic-gate 		set_dma_control_register(fdc, DCSR_INIT_BITS);
62390Sstevel@tonic-gate 		mutex_exit(&fdc->c_hilock);
62400Sstevel@tonic-gate 	}
62410Sstevel@tonic-gate 
62420Sstevel@tonic-gate 	/*
62430Sstevel@tonic-gate 	 * Force a rotational speed set in the next
62440Sstevel@tonic-gate 	 * call to set_rotational_speed().
62450Sstevel@tonic-gate 	 */
62460Sstevel@tonic-gate 
62470Sstevel@tonic-gate 	fdc->c_un->un_flags |= FDUNIT_SET_SPEED;
62480Sstevel@tonic-gate 
62490Sstevel@tonic-gate 	/* Reset and configure the controller */
62500Sstevel@tonic-gate 	(void) fdreset(fdc);
62510Sstevel@tonic-gate 
62520Sstevel@tonic-gate 	unit = fdc->c_un->un_unit_no;
62530Sstevel@tonic-gate 
62540Sstevel@tonic-gate 	/* Recalibrate the drive */
62550Sstevel@tonic-gate 	if (fdrecalseek(fdc, unit, -1, 0) != 0) {
62560Sstevel@tonic-gate 		FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "raise_power : recalibrate \
62570Sstevel@tonic-gate failed\n"));
62580Sstevel@tonic-gate 		fdretcsb(fdc);
62590Sstevel@tonic-gate 		mutex_exit(&fdc->c_lolock);
62600Sstevel@tonic-gate 		return (DDI_FAILURE);
62610Sstevel@tonic-gate 	}
62620Sstevel@tonic-gate 
62630Sstevel@tonic-gate 	/* Select the drive through the AUXIO registers */
62640Sstevel@tonic-gate 	fdselect(fdc, unit, 0);
62650Sstevel@tonic-gate 	un->un_state = FD_STATE_NORMAL;
62660Sstevel@tonic-gate 	fdretcsb(fdc);
62670Sstevel@tonic-gate 	mutex_exit(&fdc->c_lolock);
62680Sstevel@tonic-gate 	return (DDI_SUCCESS);
62690Sstevel@tonic-gate }
62700Sstevel@tonic-gate 
62710Sstevel@tonic-gate /*
62720Sstevel@tonic-gate  * create_pm_components :
62730Sstevel@tonic-gate  *	creates the power management components for auto pm framework.
62740Sstevel@tonic-gate  */
62750Sstevel@tonic-gate 
62760Sstevel@tonic-gate static void
62770Sstevel@tonic-gate create_pm_components(dev_info_t *dip)
62780Sstevel@tonic-gate {
62790Sstevel@tonic-gate 	char	*un_pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on"};
62800Sstevel@tonic-gate 
62810Sstevel@tonic-gate 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, dip,
6282*7656SSherry.Moore@Sun.COM 	    "pm-components", un_pm_comp, 3) == DDI_PROP_SUCCESS) {
62830Sstevel@tonic-gate 
62840Sstevel@tonic-gate 		(void) pm_raise_power(dip, 0, PM_LEVEL_ON);
62850Sstevel@tonic-gate 	}
62860Sstevel@tonic-gate }
62870Sstevel@tonic-gate 
62880Sstevel@tonic-gate /*
62890Sstevel@tonic-gate  * set_data_count_register(struct fdctlr *fdc, uint32_t count)
62900Sstevel@tonic-gate  * 	Set the data count in appropriate dma register.
62910Sstevel@tonic-gate  */
62920Sstevel@tonic-gate 
62930Sstevel@tonic-gate static void
62940Sstevel@tonic-gate set_data_count_register(struct fdctlr *fdc, uint32_t count)
62950Sstevel@tonic-gate {
62960Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
62970Sstevel@tonic-gate 		struct cheerio_dma_reg *dma_reg;
62980Sstevel@tonic-gate 		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
62990Sstevel@tonic-gate 		ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dbcr, count);
63000Sstevel@tonic-gate 	} else if (fdc->c_fdtype & FDCTYPE_SB) {
63010Sstevel@tonic-gate 		struct sb_dma_reg *dma_reg;
63020Sstevel@tonic-gate 		count = count - 1; /* 8237 needs it */
63030Sstevel@tonic-gate 		dma_reg = (struct sb_dma_reg *)fdc->c_dma_regs;
63040Sstevel@tonic-gate 		switch (fdc->sb_dma_channel) {
6305*7656SSherry.Moore@Sun.COM 		case 0 :
6306*7656SSherry.Moore@Sun.COM 			ddi_put16(fdc->c_handlep_dma,
6307*7656SSherry.Moore@Sun.COM 			    (ushort_t *)&dma_reg->sb_dma_regs[DMA_0WCNT],
6308*7656SSherry.Moore@Sun.COM 			    count & 0xFFFF);
6309*7656SSherry.Moore@Sun.COM 			break;
6310*7656SSherry.Moore@Sun.COM 		case 1 :
6311*7656SSherry.Moore@Sun.COM 			ddi_put16(fdc->c_handlep_dma,
6312*7656SSherry.Moore@Sun.COM 			    (ushort_t *)&dma_reg->sb_dma_regs[DMA_1WCNT],
6313*7656SSherry.Moore@Sun.COM 			    count & 0xFFFF);
6314*7656SSherry.Moore@Sun.COM 			break;
6315*7656SSherry.Moore@Sun.COM 		case 2 :
6316*7656SSherry.Moore@Sun.COM 			ddi_put16(fdc->c_handlep_dma,
6317*7656SSherry.Moore@Sun.COM 			    (ushort_t *)&dma_reg->sb_dma_regs[DMA_2WCNT],
6318*7656SSherry.Moore@Sun.COM 			    count & 0xFFFF);
6319*7656SSherry.Moore@Sun.COM 			break;
6320*7656SSherry.Moore@Sun.COM 		case 3 :
6321*7656SSherry.Moore@Sun.COM 			ddi_put16(fdc->c_handlep_dma,
6322*7656SSherry.Moore@Sun.COM 			    (ushort_t *)&dma_reg->sb_dma_regs[DMA_3WCNT],
6323*7656SSherry.Moore@Sun.COM 			    count & 0xFFFF);
6324*7656SSherry.Moore@Sun.COM 			break;
6325*7656SSherry.Moore@Sun.COM 		default :
6326*7656SSherry.Moore@Sun.COM 			FDERRPRINT(FDEP_L3, FDEM_SDMA,
6327*7656SSherry.Moore@Sun.COM 			    (C, "set_data_count: wrong channel %x\n",
6328*7656SSherry.Moore@Sun.COM 			    fdc->sb_dma_channel));
6329*7656SSherry.Moore@Sun.COM 			break;
63300Sstevel@tonic-gate 		}
63310Sstevel@tonic-gate 	}
63320Sstevel@tonic-gate }
63330Sstevel@tonic-gate 
63340Sstevel@tonic-gate /*
63350Sstevel@tonic-gate  * get_data_count_register(struct fdctlr *fdc)
63360Sstevel@tonic-gate  * 	Read the data count from appropriate dma register.
63370Sstevel@tonic-gate  */
63380Sstevel@tonic-gate 
63390Sstevel@tonic-gate static uint32_t
63400Sstevel@tonic-gate get_data_count_register(struct fdctlr *fdc)
63410Sstevel@tonic-gate {
63420Sstevel@tonic-gate 	uint32_t retval = 0;
63430Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
63440Sstevel@tonic-gate 		struct cheerio_dma_reg *dma_reg;
63450Sstevel@tonic-gate 		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
63460Sstevel@tonic-gate 		retval = ddi_get32(fdc->c_handlep_dma, &dma_reg->fdc_dbcr);
63470Sstevel@tonic-gate 	} else if (fdc->c_fdtype & FDCTYPE_SB) {
63480Sstevel@tonic-gate 		struct sb_dma_reg *dma_reg;
63490Sstevel@tonic-gate 		dma_reg = (struct sb_dma_reg *)fdc->c_dma_regs;
63500Sstevel@tonic-gate 		switch (fdc->sb_dma_channel) {
6351*7656SSherry.Moore@Sun.COM 		case 0 :
6352*7656SSherry.Moore@Sun.COM 			retval = ddi_get16(fdc->c_handlep_dma,
6353*7656SSherry.Moore@Sun.COM 			    (ushort_t *)&dma_reg->sb_dma_regs[DMA_0WCNT]);
6354*7656SSherry.Moore@Sun.COM 			break;
6355*7656SSherry.Moore@Sun.COM 		case 1 :
6356*7656SSherry.Moore@Sun.COM 			retval = ddi_get16(fdc->c_handlep_dma,
6357*7656SSherry.Moore@Sun.COM 			    (ushort_t *)&dma_reg->sb_dma_regs[DMA_1WCNT]);
6358*7656SSherry.Moore@Sun.COM 			break;
6359*7656SSherry.Moore@Sun.COM 		case 2 :
6360*7656SSherry.Moore@Sun.COM 			retval = ddi_get16(fdc->c_handlep_dma,
6361*7656SSherry.Moore@Sun.COM 			    (ushort_t *)&dma_reg->sb_dma_regs[DMA_2WCNT]);
6362*7656SSherry.Moore@Sun.COM 			break;
6363*7656SSherry.Moore@Sun.COM 		case 3 :
6364*7656SSherry.Moore@Sun.COM 			retval = ddi_get16(fdc->c_handlep_dma,
6365*7656SSherry.Moore@Sun.COM 			    (ushort_t *)&dma_reg->sb_dma_regs[DMA_3WCNT]);
6366*7656SSherry.Moore@Sun.COM 			break;
6367*7656SSherry.Moore@Sun.COM 		default :
6368*7656SSherry.Moore@Sun.COM 			FDERRPRINT(FDEP_L3, FDEM_SDMA,
6369*7656SSherry.Moore@Sun.COM 			    (C, "get_data_count: wrong channel %x\n",
6370*7656SSherry.Moore@Sun.COM 			    fdc->sb_dma_channel));
6371*7656SSherry.Moore@Sun.COM 			break;
63720Sstevel@tonic-gate 		}
63730Sstevel@tonic-gate 		retval = (uint32_t)((uint16_t)(retval +1));
63740Sstevel@tonic-gate 	}
63750Sstevel@tonic-gate 
63760Sstevel@tonic-gate 	return (retval);
63770Sstevel@tonic-gate 
63780Sstevel@tonic-gate }
63790Sstevel@tonic-gate 
63800Sstevel@tonic-gate /*
63810Sstevel@tonic-gate  * reset_dma_controller(struct fdctlr *fdc)
63820Sstevel@tonic-gate  * 	Reset and initialize the dma controller.
63830Sstevel@tonic-gate  */
63840Sstevel@tonic-gate 
63850Sstevel@tonic-gate static void
63860Sstevel@tonic-gate reset_dma_controller(struct fdctlr *fdc)
63870Sstevel@tonic-gate {
63880Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
63890Sstevel@tonic-gate 		struct cheerio_dma_reg *dma_reg;
63900Sstevel@tonic-gate 		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
63910Sstevel@tonic-gate 		ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dcsr, DCSR_RESET);
6392*7656SSherry.Moore@Sun.COM 		while (get_dma_control_register(fdc) & DCSR_CYC_PEND)
6393*7656SSherry.Moore@Sun.COM 			;
63940Sstevel@tonic-gate 		ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dcsr, 0);
63950Sstevel@tonic-gate 	} else if (fdc->c_fdtype & FDCTYPE_SB) {
63960Sstevel@tonic-gate 		struct sb_dma_reg *dma_reg;
63970Sstevel@tonic-gate 		dma_reg = (struct sb_dma_reg *)fdc->c_dma_regs;
63980Sstevel@tonic-gate 		ddi_put8(fdc->c_handlep_dma, &dma_reg->sb_dma_regs[DMAC1_MASK],
63990Sstevel@tonic-gate 		    (fdc->sb_dma_channel & 0x3));
64000Sstevel@tonic-gate 
64010Sstevel@tonic-gate 	}
64020Sstevel@tonic-gate }
64030Sstevel@tonic-gate 
64040Sstevel@tonic-gate /*
64050Sstevel@tonic-gate  * Get the DMA control register for CHEERIO.
64060Sstevel@tonic-gate  * For SouthBridge 8237 DMA controller, this register is not valid.
64070Sstevel@tonic-gate  * So, just return 0.
64080Sstevel@tonic-gate  */
64090Sstevel@tonic-gate static uint32_t
64100Sstevel@tonic-gate get_dma_control_register(struct fdctlr *fdc)
64110Sstevel@tonic-gate {
64120Sstevel@tonic-gate 	uint32_t retval = 0;
64130Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
64140Sstevel@tonic-gate 		struct cheerio_dma_reg *dma_reg;
64150Sstevel@tonic-gate 		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
64160Sstevel@tonic-gate 		retval = ddi_get32(fdc->c_handlep_dma, &dma_reg->fdc_dcsr);
64170Sstevel@tonic-gate 	}
64180Sstevel@tonic-gate 
64190Sstevel@tonic-gate 	return (retval);
64200Sstevel@tonic-gate }
64210Sstevel@tonic-gate 
64220Sstevel@tonic-gate 
64230Sstevel@tonic-gate /*
64240Sstevel@tonic-gate  * set_data_address_register(struct fdctlr *fdc)
64250Sstevel@tonic-gate  * 	Set the data address in appropriate dma register.
64260Sstevel@tonic-gate  */
64270Sstevel@tonic-gate static void
64280Sstevel@tonic-gate set_data_address_register(struct fdctlr *fdc, uint32_t address)
64290Sstevel@tonic-gate {
64300Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
64310Sstevel@tonic-gate 		struct cheerio_dma_reg *dma_reg;
64320Sstevel@tonic-gate 		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
64330Sstevel@tonic-gate 		ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dacr, address);
64340Sstevel@tonic-gate 	} else if (fdc->c_fdtype & FDCTYPE_SB) {
64350Sstevel@tonic-gate 		struct sb_dma_reg *dma_reg;
64360Sstevel@tonic-gate 		dma_reg = (struct sb_dma_reg *)fdc->c_dma_regs;
64370Sstevel@tonic-gate 		switch (fdc->sb_dma_channel) {
64380Sstevel@tonic-gate 			case 0 :
64390Sstevel@tonic-gate 				ddi_put8(fdc->c_handlep_dma,
64400Sstevel@tonic-gate 				    &dma_reg->sb_dma_regs[DMA_0PAGE],
64410Sstevel@tonic-gate 				    (address & 0xFF0000) >>16);
64420Sstevel@tonic-gate 				ddi_put8(fdc->c_handlep_dma,
64430Sstevel@tonic-gate 				    &dma_reg->sb_dma_regs[DMA_0HPG],
64440Sstevel@tonic-gate 				    (address & 0xFF000000) >>24);
64450Sstevel@tonic-gate 				ddi_put16(fdc->c_handlep_dma,
64460Sstevel@tonic-gate 				    (ushort_t *)&dma_reg->sb_dma_regs[DMA_0ADR],
64470Sstevel@tonic-gate 				    address & 0xFFFF);
64480Sstevel@tonic-gate 				break;
64490Sstevel@tonic-gate 			case 1 :
64500Sstevel@tonic-gate 				ddi_put8(fdc->c_handlep_dma,
64510Sstevel@tonic-gate 				    &dma_reg->sb_dma_regs[DMA_1PAGE],
64520Sstevel@tonic-gate 				    (address & 0xFF0000) >>16);
64530Sstevel@tonic-gate 				ddi_put8(fdc->c_handlep_dma,
64540Sstevel@tonic-gate 				    &dma_reg->sb_dma_regs[DMA_1HPG],
64550Sstevel@tonic-gate 				    (address & 0xFF000000) >>24);
64560Sstevel@tonic-gate 				ddi_put16(fdc->c_handlep_dma,
64570Sstevel@tonic-gate 				    (ushort_t *)&dma_reg->sb_dma_regs[DMA_1ADR],
64580Sstevel@tonic-gate 				    address & 0xFFFF);
64590Sstevel@tonic-gate 				break;
64600Sstevel@tonic-gate 			case 2 :
64610Sstevel@tonic-gate 				ddi_put8(fdc->c_handlep_dma,
64620Sstevel@tonic-gate 				    &dma_reg->sb_dma_regs[DMA_2PAGE],
64630Sstevel@tonic-gate 				    (address & 0xFF0000) >>16);
64640Sstevel@tonic-gate 				ddi_put8(fdc->c_handlep_dma,
64650Sstevel@tonic-gate 				    &dma_reg->sb_dma_regs[DMA_2HPG],
64660Sstevel@tonic-gate 				    (address & 0xFF000000) >>24);
64670Sstevel@tonic-gate 				ddi_put16(fdc->c_handlep_dma,
64680Sstevel@tonic-gate 				    (ushort_t *)&dma_reg->sb_dma_regs[DMA_2ADR],
64690Sstevel@tonic-gate 				    address & 0xFFFF);
64700Sstevel@tonic-gate 				break;
64710Sstevel@tonic-gate 			case 3 :
64720Sstevel@tonic-gate 				ddi_put8(fdc->c_handlep_dma,
64730Sstevel@tonic-gate 				    &dma_reg->sb_dma_regs[DMA_3PAGE],
64740Sstevel@tonic-gate 				    (address & 0xFF0000) >>16);
64750Sstevel@tonic-gate 				ddi_put8(fdc->c_handlep_dma,
64760Sstevel@tonic-gate 				    &dma_reg->sb_dma_regs[DMA_3HPG],
64770Sstevel@tonic-gate 				    (address & 0xFF000000) >>24);
64780Sstevel@tonic-gate 				ddi_put16(fdc->c_handlep_dma,
64790Sstevel@tonic-gate 				    (ushort_t *)&dma_reg->sb_dma_regs[DMA_3ADR],
64800Sstevel@tonic-gate 				    address & 0xFFFF);
64810Sstevel@tonic-gate 				break;
64820Sstevel@tonic-gate 			default :
64830Sstevel@tonic-gate 				FDERRPRINT(FDEP_L3, FDEM_SDMA,
6484*7656SSherry.Moore@Sun.COM 				    (C, "set_data_address: wrong channel %x\n",
6485*7656SSherry.Moore@Sun.COM 				    fdc->sb_dma_channel));
64860Sstevel@tonic-gate 			break;
64870Sstevel@tonic-gate 		}
64880Sstevel@tonic-gate 	}
64890Sstevel@tonic-gate 
64900Sstevel@tonic-gate }
64910Sstevel@tonic-gate 
64920Sstevel@tonic-gate 
64930Sstevel@tonic-gate /*
64940Sstevel@tonic-gate  * set_dma_mode(struct fdctlr *fdc, int val)
64950Sstevel@tonic-gate  * 	Set the appropriate dma direction and registers.
64960Sstevel@tonic-gate  */
64970Sstevel@tonic-gate static void
64980Sstevel@tonic-gate set_dma_mode(struct fdctlr *fdc, int val)
64990Sstevel@tonic-gate {
65000Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
65010Sstevel@tonic-gate 		struct cheerio_dma_reg *dma_reg;
65020Sstevel@tonic-gate 		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
65030Sstevel@tonic-gate 		if (val == CSB_READ)
65040Sstevel@tonic-gate 			ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dcsr,
6505*7656SSherry.Moore@Sun.COM 			    DCSR_INIT_BITS|DCSR_WRITE);
65060Sstevel@tonic-gate 		else
65070Sstevel@tonic-gate 			ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dcsr,
6508*7656SSherry.Moore@Sun.COM 			    DCSR_INIT_BITS);
65090Sstevel@tonic-gate 
65100Sstevel@tonic-gate 	} else if (fdc->c_fdtype & FDCTYPE_SB) {
65110Sstevel@tonic-gate 		uint8_t mode_reg_val, chn_mask;
65120Sstevel@tonic-gate 		struct sb_dma_reg *dma_reg;
65130Sstevel@tonic-gate 		dma_reg = (struct sb_dma_reg *)fdc->c_dma_regs;
65140Sstevel@tonic-gate 
65150Sstevel@tonic-gate 		if (val == CSB_READ) {
65160Sstevel@tonic-gate 			mode_reg_val = fdc->sb_dma_channel | DMAMODE_READ
6517*7656SSherry.Moore@Sun.COM 			    | DMAMODE_SINGLE;
65180Sstevel@tonic-gate 		} else { /* Read operation */
65190Sstevel@tonic-gate 			mode_reg_val = fdc->sb_dma_channel | DMAMODE_WRITE
6520*7656SSherry.Moore@Sun.COM 			    | DMAMODE_SINGLE;
65210Sstevel@tonic-gate 		}
65220Sstevel@tonic-gate 		ddi_put8(fdc->c_handlep_dma, &dma_reg->sb_dma_regs[DMAC1_MODE],
6523*7656SSherry.Moore@Sun.COM 		    mode_reg_val);
65240Sstevel@tonic-gate 		chn_mask = 1 << (fdc->sb_dma_channel & 0x3);
65250Sstevel@tonic-gate 		ddi_put8(fdc->c_handlep_dma,
6526*7656SSherry.Moore@Sun.COM 		    &dma_reg->sb_dma_regs[DMAC1_ALLMASK], ~chn_mask);
65270Sstevel@tonic-gate 		fdc->sb_dma_lock = 1;
65280Sstevel@tonic-gate 	}
65290Sstevel@tonic-gate }
65300Sstevel@tonic-gate 
65310Sstevel@tonic-gate /*
65320Sstevel@tonic-gate  * This function is valid only for CHEERIO/RIO based
65330Sstevel@tonic-gate  * controllers. The control register for the dma channel
65340Sstevel@tonic-gate  * is initialized by this function.
65350Sstevel@tonic-gate  */
65360Sstevel@tonic-gate 
65370Sstevel@tonic-gate static void
65380Sstevel@tonic-gate set_dma_control_register(struct fdctlr *fdc, uint32_t val)
65390Sstevel@tonic-gate {
65400Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
65410Sstevel@tonic-gate 		struct cheerio_dma_reg *dma_reg;
65420Sstevel@tonic-gate 		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
65430Sstevel@tonic-gate 		ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dcsr, val);
65440Sstevel@tonic-gate 	}
65450Sstevel@tonic-gate }
65460Sstevel@tonic-gate 
65470Sstevel@tonic-gate static void
65480Sstevel@tonic-gate release_sb_dma(struct fdctlr *fdc)
65490Sstevel@tonic-gate {
65500Sstevel@tonic-gate 	struct sb_dma_reg *dma_reg;
65510Sstevel@tonic-gate 	dma_reg = (struct sb_dma_reg *)fdc->c_dma_regs;
65520Sstevel@tonic-gate 	/* Unmask all the channels to release the DMA controller */
65530Sstevel@tonic-gate 	ddi_put8(fdc->c_handlep_dma,
6554*7656SSherry.Moore@Sun.COM 	    &dma_reg->sb_dma_regs[DMAC1_ALLMASK], NULL);
65550Sstevel@tonic-gate 	fdc->sb_dma_lock = 0;
65560Sstevel@tonic-gate }
65570Sstevel@tonic-gate 
65580Sstevel@tonic-gate static void
65590Sstevel@tonic-gate quiesce_fd_interrupt(struct fdctlr *fdc)
65600Sstevel@tonic-gate {
65610Sstevel@tonic-gate 	/*
65620Sstevel@tonic-gate 	 * The following code is put here to take care of HW problem.
65630Sstevel@tonic-gate 	 * The HW problem is as follows:
65640Sstevel@tonic-gate 	 *
65650Sstevel@tonic-gate 	 *	After poweron the Southbridge floppy controller asserts the
65660Sstevel@tonic-gate 	 * interrupt in tristate. This causes continuous interrupts to
65670Sstevel@tonic-gate 	 * be generated.
65680Sstevel@tonic-gate 	 * Until the Hardware is FIXED we will have to use the following code
65690Sstevel@tonic-gate 	 * to set the interrupt line to proper state after poweron.
65700Sstevel@tonic-gate 	 */
65710Sstevel@tonic-gate 	if (fdc->c_fdtype & FDCTYPE_SB) {
65720Sstevel@tonic-gate 		ddi_put8(fdc->c_handlep_cont, ((uint8_t *)fdc->c_dor),
6573*7656SSherry.Moore@Sun.COM 		    0x0);
65740Sstevel@tonic-gate 		drv_usecwait(200);
65750Sstevel@tonic-gate 		ddi_put8(fdc->c_handlep_cont, ((uint8_t *)fdc->c_dor),
6576*7656SSherry.Moore@Sun.COM 		    0xC);
65770Sstevel@tonic-gate 		drv_usecwait(200);
65780Sstevel@tonic-gate 		Set_Fifo(fdc, 0xE6);
65790Sstevel@tonic-gate 		drv_usecwait(200);
65800Sstevel@tonic-gate 	}
65810Sstevel@tonic-gate }
6582