xref: /titanic_51/usr/src/uts/sun/io/fd.c (revision 141040e8a310da49386b596573e5dde5580572ec)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Intel 82077 Floppy Disk Driver
31  */
32 
33 /*
34  * Notes
35  *
36  *	0. The driver supports two flavors of hardware design:
37  *		"SUNW,fdtwo"	- sun4m	- 82077 with sun4m style Auxio
38  *		"fdthree"  - sun4u - 82077 with DMA
39  *	   In addition it supports an apparent bug in some versions of
40  *	   the 82077 controller.
41  *
42  *	1. The driver is mostly set up for multiple controllers, multiple
43  *	drives. However- we *do* assume the use of the AUXIO register, and
44  *	if we ever have > 1 fdc, we'll have to see what that means. This
45  *	is all intrinsically machine specific, but there isn't much we
46  *	can do about it.
47  *
48  *	2. The driver also is structured to deal with one drive active at
49  *	a time. This is because the 82072 chip (no longer supported) was
50  *	known to be buggy with respect to overlapped seeks.
51  *
52  *	3. The high level interrupt code is in assembler, and runs in a
53  *	sparc trap window. It acts as a pseudo-dma engine as well as
54  *	handles a couple of other interrupts. When it gets its job done,
55  *	it schedules a second stage interrupt (soft interrupt) which
56  *	is then fielded here in fd_lointr.  When DMA is used, the fdintr_dma
57  *	interrupt handler is used.
58  *
59  *	4. Nearly all locking is done on a lower level MUTEX_DRIVER
60  *	mutex. The locking is quite conservative, and is generally
61  *	established very close to any of the entries into the driver.
62  *	There is nearly no locking done of the high level MUTEX_DRIVER
63  *	mutex (which generally is a SPIN mutex because the floppy usually
64  *	interrupts above LOCK_LEVEL). The assembler high level interrupt
65  *	handler grabs the high level mutex, but the code in the driver
66  *	here is especially structured to not need to do this.
67  *
68  *	5. Fdrawioctl commands that pass data are not optimized for
69  *	speed. If they need to be faster, the driver structure will
70  *	have to be redone such that fdrawioctl calls physio after
71  *	cons'ing up a uio structure and that fdstart will be able
72  *	to detect that a particular buffer is a 'special' buffer.
73  *
74  *	6. Removable media support is not complete.
75  *
76  */
77 
78 #include <sys/param.h>
79 #include <sys/buf.h>
80 #include <sys/ioctl.h>
81 #include <sys/uio.h>
82 #include <sys/open.h>
83 #include <sys/conf.h>
84 #include <sys/file.h>
85 #include <sys/cmn_err.h>
86 #include <sys/debug.h>
87 #include <sys/kmem.h>
88 #include <sys/stat.h>
89 #include <sys/autoconf.h>
90 
91 #include <sys/dklabel.h>
92 
93 #include <sys/vtoc.h>
94 #include <sys/dkio.h>
95 #include <sys/fdio.h>
96 
97 #include <sys/ddi.h>
98 #include <sys/sunddi.h>
99 #include <sys/kstat.h>
100 
101 /*
102  * included to check for ELC or SLC which report floppy controller that
103  */
104 #include <sys/cpu.h>
105 
106 #include "sys/fdvar.h"
107 #include "sys/fdreg.h"
108 #include "sys/dma_i8237A.h"
109 
110 /*
111  * Defines
112  */
113 #define	KIOSP	KSTAT_IO_PTR(un->un_iostat)
114 #define	KIOIP	KSTAT_INTR_PTR(fdc->c_intrstat)
115 #define	MEDIUM_DENSITY	0x40
116 #define	SEC_SIZE_CODE	(fdctlr.c_csb->csb_unit]->un_chars->medium ? 3 : 2)
117 #define	CMD_READ	(MT + SK + FDRAW_RDCMD + MFM)
118 #define	CMD_WRITE	(MT + FDRAW_WRCMD + MFM)
119 #define	C		CE_CONT
120 #define	FD_POLLABLE_PROP	"pollable"	/* prom property */
121 #define	FD_MANUAL_EJECT		"manual"	/* prom property */
122 #define	FD_UNIT			"unit"		/* prom property */
123 
124 /*
125  * Sony MP-F17W-50D Drive Parameters
126  *				High Capacity
127  *	Capacity unformatted	2Mb
128  *	Capacity formatted	1.47Mb
129  *	Encoding method	 MFM
130  *	Recording density	17434 bpi
131  *	Track density		135 tpi
132  *	Cylinders		80
133  *	Heads			2
134  *	Tracks			160
135  *	Rotational speed	300 rpm
136  *	Transfer rate		250/500 kbps
137  *	Latency (average)	100 ms
138  *	Access time
139  *		Average		95 ms
140  *		Track to track	3 ms
141  *	Head settling time	15 ms
142  *	Motor start time	500 ms
143  *	Head load time		? ms
144  */
145 
146 /*
147  * The max_fd_dma_len is used only when southbridge is present.
148  * It has been observed that when IFB tests are run the floppy dma could get
149  * starved and result in underrun errors. After experimenting it was found that
150  * doing dma in chunks of 2048 works OK.
151  * The reason for making this a global variable is that there could be
152  * situations under which the customer would like to get full performance
153  * from floppy. He may not be having IFB boards that cause underrun errors.
154  * Under those conditions we could set this value to a much higher value
155  * by editing /etc/system file.
156  */
157 int	max_fd_dma_len = 2048;
158 
159 static void quiesce_fd_interrupt(struct fdctlr *);
160 
161 /*
162  * Character/block entry points function prototypes
163  */
164 static int fd_open(dev_t *, int, int, cred_t *);
165 static int fd_close(dev_t, int, int, cred_t *);
166 static int fd_strategy(struct buf *);
167 static int fd_read(dev_t, struct uio *, cred_t *);
168 static int fd_write(dev_t, struct uio *, cred_t *);
169 static int fd_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
170 static int
171 fd_prop_op(dev_t, dev_info_t *, ddi_prop_op_t, int, char *, caddr_t, int *);
172 
173 /*
174  * Device operations (dev_ops) entries function prototypes
175  */
176 static int fd_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
177 		void **result);
178 static int fd_attach(dev_info_t *, ddi_attach_cmd_t);
179 static int fd_detach(dev_info_t *, ddi_detach_cmd_t);
180 static int fd_power(dev_info_t *dip, int component, int level);
181 
182 /*
183  * Internal functions
184  */
185 static int fd_attach_check_drive(struct fdctlr *fdc);
186 static int fd_attach_det_ctlr(dev_info_t *dip, struct fdctlr *fdc);
187 static int fd_attach_map_regs(dev_info_t *dip, struct fdctlr *fdc);
188 static int fd_attach_register_interrupts(dev_info_t *dip, struct fdctlr *fdc,
189     int *hard);
190 static int fd_build_label_vtoc(struct fdunit *, struct vtoc *);
191 static void fd_build_user_vtoc(struct fdunit *, struct vtoc *);
192 static int fdcheckdisk(struct fdctlr *fdc, int unit);
193 static int fd_check_media(dev_t dev, enum dkio_state state);
194 static void fd_cleanup(dev_info_t *dip, struct fdctlr *fdc, int hard,
195     int locks);
196 static void fdeject(struct fdctlr *, int unit);
197 static int fdexec(struct fdctlr *fdc, int flags);
198 static void fdexec_turn_on_motor(struct fdctlr *fdc, int flags, uint_t unit);
199 static int fdformat(struct fdctlr *fdc, int unit, int cyl, int hd);
200 static caddr_t fd_getauxiova();
201 static struct fdctlr *fd_getctlr(dev_t);
202 static void fdgetcsb(struct fdctlr *);
203 static int fdgetlabel(struct fdctlr *fdc, int unit);
204 enum dkio_state fd_get_media_state(struct fdctlr *, int);
205 static uint_t fdintr_dma();
206 static int fd_isauxiodip(dev_info_t *);
207 static uint_t  fd_lointr(caddr_t arg);
208 static void fd_media_watch(void *);
209 static void fdmotoff(void *);
210 static int fd_part_is_open(struct fdunit *un, int part);
211 static int fdrawioctl(struct fdctlr *, int, intptr_t, int);
212 static int fdrecalseek(struct fdctlr *fdc, int unit, int arg, int execflg);
213 static int fdrecover(struct fdctlr *);
214 static void fdretcsb(struct fdctlr *);
215 static int fdreset(struct fdctlr *);
216 static int fdrw(struct fdctlr *fdc, int, int, int, int, int, caddr_t, uint_t);
217 static void fdselect(struct fdctlr *fdc, int unit, int onoff);
218 static int fdsensedrv(struct fdctlr *fdc, int unit);
219 static int fdsense_chng(struct fdctlr *, int unit);
220 static void fdstart(struct fdctlr *);
221 static int fdstart_dma(register struct fdctlr *fdc, caddr_t addr, uint_t len);
222 static int fd_unit_is_open(struct fdunit *);
223 static void fdunpacklabel(struct packed_label *, struct dk_label *);
224 static int fd_unbind_handle(struct fdctlr *);
225 static void fdwatch(void *);
226 static void set_rotational_speed(struct fdctlr *, int);
227 static int fd_get_media_info(struct fdunit *un, caddr_t buf, int flag);
228 static int fd_pm_lower_power(struct fdctlr *fdc);
229 static int fd_pm_raise_power(struct fdctlr *fdc);
230 static void create_pm_components(dev_info_t *dip);
231 static void set_data_count_register(struct fdctlr *fdc, uint32_t count);
232 static uint32_t get_data_count_register(struct fdctlr *fdc);
233 static void reset_dma_controller(struct fdctlr *fdc);
234 static void set_data_address_register(struct fdctlr *fdc, uint32_t address);
235 static uint32_t get_dma_control_register(struct fdctlr *fdc);
236 static void set_dma_mode(struct fdctlr *fdc, int val);
237 static void set_dma_control_register(struct fdctlr *fdc, uint32_t val);
238 static void release_sb_dma(struct fdctlr *fdc);
239 
240 /*
241  * External functions
242  */
243 extern uint_t fd_intr(caddr_t);	/* defined in fd_asm.s */
244 extern void set_auxioreg();
245 extern void call_debug();
246 
247 
248 
249 /*
250  * The following macro checks whether the device in a SUSPENDED state.
251  * As per WDD guide lines the I/O requests to a suspended device should
252  * be blocked until the device is resumed.
253  * Here we cv_wait on c_suspend_cv, and there is a cv_broadcast() in
254  * DDI_RESUME to wake up this thread.
255  *
256  * NOTE: This code is not tested because the kernel threads are suspended
257  * before the device is suspended. So there can not be any I/O requests on
258  * a suspended device until the cpr implementation changes..
259  */
260 
261 #define	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc) 	\
262 		{\
263 			while (fdc->c_un->un_state == FD_STATE_SUSPENDED) {\
264 				cv_wait(&fdc->c_suspend_cv, \
265 							&fdc->c_lolock);\
266 			}\
267 		}
268 
269 /*
270  * bss (uninitialized data)
271  */
272 struct	fdctlr	*fdctlrs;	/* linked list of controllers */
273 
274 /*
275  * initialized data
276  */
277 
278 static int fd_check_media_time = 5000000;	/* 5 second state check */
279 static int fd_pollable = 0;
280 static uchar_t rwretry = 10;
281 static uchar_t skretry = 5;
282 /* This variable allows the dynamic change of the burst size */
283 static int fd_burstsize = DCSR_BURST_0 | DCSR_BURST_1;
284 
285 static struct driver_minor_data {
286 	char	*name;
287 	int	minor;
288 	int	type;
289 } fd_minor [] = {
290 	{ "a", 0, S_IFBLK},
291 	{ "b", 1, S_IFBLK},
292 	{ "c", 2, S_IFBLK},
293 	{ "a,raw", 0, S_IFCHR},
294 	{ "b,raw", 1, S_IFCHR},
295 	{ "c,raw", 2, S_IFCHR},
296 	{0}
297 };
298 
299 /*
300  * If the interrupt handler is invoked and no controllers expect an
301  * interrupt, the kernel panics.  The following message is printed out.
302  */
303 char *panic_msg = "fd_intr: unexpected interrupt\n";
304 
305 /*
306  * Specify/Configure cmd parameters
307  */
308 static uchar_t fdspec[2] = { 0xc2, 0x33 };	/*  "specify" parameters */
309 static uchar_t fdconf[3] = { 0x64, 0x58, 0x00 }; /*  "configure" parameters */
310 
311 /* When DMA is used, set the ND bit to 0 */
312 #define	SPEC_DMA_MODE	0x32
313 
314 /*
315  * default characteristics
316  */
317 static struct fd_char fdtypes[] = {
318 	{	/* struct fd_char fdchar_1.7MB density */
319 		0,		/* medium */
320 		500,		/* transfer rate */
321 		80,		/* number of cylinders */
322 		2,		/* number of heads */
323 		512,		/* sector size */
324 		21,		/* sectors per track */
325 		-1,		/* (NA) # steps per data track */
326 	},
327 	{	/* struct fd_char fdchar_highdens */
328 		0, 		/* medium */
329 		500, 		/* transfer rate */
330 		80, 		/* number of cylinders */
331 		2, 		/* number of heads */
332 		512, 		/* sector size */
333 		18, 		/* sectors per track */
334 		-1, 		/* (NA) # steps per data track */
335 	},
336 	{	/* struct fd_char fdchar_meddens */
337 		1, 		/* medium */
338 		500, 		/* transfer rate */
339 		77, 		/* number of cylinders */
340 		2, 		/* number of heads */
341 		1024, 		/* sector size */
342 		8, 		/* sectors per track */
343 		-1, 		/* (NA) # steps per data track */
344 	},
345 	{	/* struct fd_char fdchar_lowdens  */
346 		0, 		/* medium */
347 		250, 		/* transfer rate */
348 		80, 		/* number of cylinders */
349 		2, 		/* number of heads */
350 		512, 		/* sector size */
351 		9, 		/* sectors per track */
352 		-1, 		/* (NA) # steps per data track */
353 	}
354 };
355 
356 
357 static int nfdtypes = sizeof (fdtypes) / sizeof (fdtypes[0]);
358 
359 
360 /*
361  * Default Label & partition maps
362  */
363 
364 static struct packed_label fdlbl_high_21 = {
365 	{ "3.5\" floppy cyl 80 alt 0 hd 2 sec 21" },
366 	300,				/* rotations per minute */
367 	80,				/* # physical cylinders */
368 	0,				/* alternates per cylinder */
369 	1,				/* interleave factor */
370 	80,				/* # of data cylinders */
371 	0,				/* # of alternate cylinders */
372 	2,				/* # of heads in this partition */
373 	21,				/* # of 512 byte sectors per track */
374 	{
375 		{ 0, 79 * 2 * 21 },	/* part 0 - all but last cyl */
376 		{ 79, 1 * 2 * 21 },	/* part 1 - just the last cyl */
377 		{ 0, 80 * 2 * 21 },	/* part 2 - "the whole thing" */
378 	},
379 	{	0,			/* version */
380 		"",			/* volume label */
381 		3,			/* no. of partitions */
382 		{ 0 },			/* partition hdrs, sec 2 */
383 		{ 0 },			/* mboot info.  unsupported */
384 		VTOC_SANE,		/* verify vtoc sanity */
385 		{ 0 },			/* reserved space */
386 		0,			/* timestamp */
387 	},
388 };
389 
390 static struct packed_label fdlbl_high_80 = {
391 	{ "3.5\" floppy cyl 80 alt 0 hd 2 sec 18" },
392 	300, 				/* rotations per minute */
393 	80, 				/* # physical cylinders */
394 	0, 				/* alternates per cylinder */
395 	1, 				/* interleave factor */
396 	80, 				/* # of data cylinders */
397 	0, 				/* # of alternate cylinders */
398 	2, 				/* # of heads in this partition */
399 	18, 				/* # of 512 byte sectors per track */
400 	{
401 		{ 0, 79 * 2 * 18 }, 	/* part 0 - all but last cyl */
402 		{ 79, 1 * 2 * 18 }, 	/* part 1 - just the last cyl */
403 		{ 0, 80 * 2 * 18 }, 	/* part 2 - "the whole thing" */
404 	},
405 	{	0,			/* version */
406 		"",			/* volume label */
407 		3,			/* no. of partitions */
408 		{ 0 },			/* partition hdrs, sec 2 */
409 		{ 0 },			/* mboot info.  unsupported */
410 		VTOC_SANE,		/* verify vtoc sanity */
411 		{ 0 },			/* reserved space */
412 		0,			/* timestamp */
413 	},
414 };
415 
416 /*
417  * A medium density diskette has 1024 byte sectors.  The dk_label structure
418  * assumes a sector is DEVBSIZE (512) bytes.
419  */
420 static struct packed_label fdlbl_medium_80 = {
421 	{ "3.5\" floppy cyl 77 alt 0 hd 2 sec 8" },
422 	360, 				/* rotations per minute */
423 	77, 				/* # physical cylinders */
424 	0, 				/* alternates per cylinder */
425 	1, 				/* interleave factor */
426 	77, 				/* # of data cylinders */
427 	0, 				/* # of alternate cylinders */
428 	2, 				/* # of heads in this partition */
429 	16, 				/* # of 512 byte sectors per track */
430 	{
431 		{ 0, 76 * 2 * 8 * 2 },  /* part 0 - all but last cyl */
432 		{ 76, 1 * 2 * 8 * 2 },  /* part 1 - just the last cyl */
433 		{ 0, 77 * 2 * 8 * 2 },  /* part 2 - "the whole thing" */
434 	},
435 	{	0,			/* version */
436 		"",			/* volume label */
437 		3,			/* no. of partitions */
438 		{ 0 },			/* partition hdrs, sec 2 */
439 		{ 0 },			/* mboot info.  unsupported */
440 		VTOC_SANE,		/* verify vtoc sanity */
441 		{ 0 },			/* reserved space */
442 		0,			/* timestamp */
443 	},
444 };
445 
446 static struct packed_label fdlbl_low_80 = {
447 	{ "3.5\" floppy cyl 80 alt 0 hd 2 sec 9" },
448 	300, 				/* rotations per minute */
449 	80, 				/* # physical cylinders */
450 	0, 				/* alternates per cylinder */
451 	1, 				/* interleave factor */
452 	80, 				/* # of data cylinders */
453 	0, 				/* # of alternate cylinders */
454 	2, 				/* # of heads in this partition */
455 	9, 				/* # of 512 byte sectors per track */
456 	{
457 		{ 0, 79 * 2 * 9 }, 	/* part 0 - all but last cyl */
458 		{ 79, 1 * 2 * 9 }, 	/* part 1 - just the last cyl */
459 		{ 0, 80 * 2 * 9 }, 	/* part 2 - "the whole thing" */
460 	},
461 	{	0,			/* version */
462 		"",			/* volume label */
463 		3,			/* no. of partitions */
464 		{ 0 },			/* partition hdrs, sec 2 */
465 		{ 0 },			/* mboot info.  unsupported */
466 		VTOC_SANE,		/* verify vtoc sanity */
467 		{ 0 },			/* reserved space */
468 		0,			/* timestamp */
469 	},
470 };
471 
472 static struct fdcmdinfo {
473 	char *cmdname;		/* command name */
474 	uchar_t ncmdbytes;	/* number of bytes of command */
475 	uchar_t nrsltbytes;	/* number of bytes in result */
476 	uchar_t cmdtype;		/* characteristics */
477 } fdcmds[] = {
478 	"", 0, 0, 0, 			/* - */
479 	"", 0, 0, 0, 			/* - */
480 	"read_track", 9, 7, 1, 		/* 2 */
481 	"specify", 3, 0, 3, 		/* 3 */
482 	"sense_drv_status", 2, 1, 3, 	/* 4 */
483 	"write", 9, 7, 1, 		/* 5 */
484 	"read", 9, 7, 1, 		/* 6 */
485 	"recalibrate", 2, 0, 2, 		/* 7 */
486 	"sense_int_status", 1, 2, 3, 	/* 8 */
487 	"write_del", 9, 7, 1, 		/* 9 */
488 	"read_id", 2, 7, 2, 		/* A */
489 	"motor_on/off", 1, 0, 4, 	/* B */
490 	"read_del", 9, 7, 1, 		/* C */
491 	"format_track", 10, 7, 1, 	/* D */
492 	"dump_reg", 1, 10, 4, 		/* E */
493 	"seek", 3, 0, 2, 		/* F */
494 	"", 0, 0, 0, 			/* - */
495 	"", 0, 0, 0, 			/* - */
496 	"", 0, 0, 0, 			/* - */
497 	"configure", 4, 0, 4, 		/* 13 */
498 	/* relative seek */
499 };
500 
501 static struct cb_ops fd_cb_ops = {
502 	fd_open, 		/* open */
503 	fd_close, 		/* close */
504 	fd_strategy, 		/* strategy */
505 	nodev, 			/* print */
506 	nodev, 			/* dump */
507 	fd_read, 		/* read */
508 	fd_write, 		/* write */
509 	fd_ioctl, 		/* ioctl */
510 	nodev, 			/* devmap */
511 	nodev, 			/* mmap */
512 	nodev, 			/* segmap */
513 	nochpoll, 		/* poll */
514 	fd_prop_op, 		/* cb_prop_op */
515 	0, 			/* streamtab  */
516 	D_NEW | D_MP		/* Driver compatibility flag */
517 };
518 
519 static struct dev_ops	fd_ops = {
520 	DEVO_REV, 		/* devo_rev, */
521 	0, 			/* refcnt  */
522 	fd_info, 		/* info */
523 	nulldev, 		/* identify */
524 	nulldev, 		/* probe */
525 	fd_attach, 		/* attach */
526 	fd_detach, 		/* detach */
527 	nodev, 			/* reset */
528 	&fd_cb_ops, 		/* driver operations */
529 	(struct bus_ops *)0,	/* bus operations */
530 	fd_power		/* power */
531 };
532 
533 
534 /*
535  * error handling
536  *
537  * for debugging, set rwretry and skretry = 1
538  *		set fderrlevel to 1
539  *		set fderrmask  to 224  or 100644
540  *
541  * after debug set rwretry to 10, skretry to 5, and fderrlevel to 3
542  * set fderrmask to FDEM_ALL
543  * remove the define FD_DEBUG
544  *
545  */
546 
547 static unsigned int fderrmask = (unsigned int)FDEM_ALL;
548 static int fderrlevel = 3;
549 
550 static int tosec = 16;  /* long timeouts for sundiag for now */
551 
552 /*
553  * loadable module support
554  */
555 
556 #include <sys/modctl.h>
557 
558 extern struct mod_ops mod_driverops;
559 static struct modldrv modldrv = {
560 	&mod_driverops, 		/* Type of module. driver here */
561 	"Floppy Driver v%I%", 	/* Name of the module. */
562 	&fd_ops, 		/* Driver ops vector */
563 };
564 
565 static struct modlinkage modlinkage = {
566 	MODREV_1,
567 	&modldrv,
568 	NULL
569 };
570 
571 int
572 _init(void)
573 {
574 	return (mod_install(&modlinkage));
575 }
576 
577 int
578 _info(struct modinfo *modinfop)
579 {
580 	return (mod_info(&modlinkage, modinfop));
581 }
582 
583 int
584 _fini(void)
585 {
586 	int e;
587 
588 	if ((e = mod_remove(&modlinkage)) != 0)
589 		return (e);
590 
591 	/* ddi_soft_state_fini() */
592 	return (0);
593 }
594 
595 /* ARGSUSED */
596 static int
597 fd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
598 {
599 	struct 			fdctlr *fdc;
600 	struct 			driver_minor_data *dmdp;
601 	int			instance = ddi_get_instance(dip);
602 	int			hard_intr_set = 0;
603 
604 	FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_attach: start\n"));
605 
606 	switch (cmd) {
607 		case DDI_ATTACH:
608 			break;
609 		case DDI_RESUME:
610 
611 			if (!(fdc = fd_getctlr(instance << FDINSTSHIFT))) {
612 				return (DDI_FAILURE);
613 			}
614 			quiesce_fd_interrupt(fdc);
615 			if (fdc->c_fdtype & FDCTYPE_SB)
616 			    if (ddi_add_intr(dip, 0, &fdc->c_block, 0,
617 				fdintr_dma, (caddr_t)0) != DDI_SUCCESS) {
618 				return (DDI_FAILURE);
619 			}
620 
621 			(void) pm_raise_power(dip, 0, PM_LEVEL_ON);
622 			mutex_enter(&fdc->c_lolock);
623 			/*
624 			 * Wake up any thread blocked due to I/O requests
625 			 * while the device was suspended.
626 			 */
627 			cv_broadcast(&fdc->c_suspend_cv);
628 			mutex_exit(&fdc->c_lolock);
629 			return (DDI_SUCCESS);
630 
631 		default:
632 			return (DDI_FAILURE);
633 	}
634 
635 
636 	/*
637 	 * Check for the pollable property
638 	 * A pollable floppy drive currently only exists on the
639 	 * Sparcstation Voyager.  This drive does not need to
640 	 * be turned on in order to sense whether or not a diskette
641 	 * is present.
642 	 */
643 	if (ddi_getprop(DDI_DEV_T_ANY, dip,
644 	    DDI_PROP_DONTPASS, FD_POLLABLE_PROP, 0))
645 		fd_pollable = 1;
646 
647 	fdc = kmem_zalloc(sizeof (*fdc), KM_SLEEP);
648 	fdc->c_dip = dip;
649 
650 
651 	fdc->c_next = fdctlrs;
652 	fdctlrs = fdc;
653 
654 	/* Determine which type of controller is present and initialize it */
655 	if (fd_attach_det_ctlr(dip, fdc) == DDI_FAILURE) {
656 		fd_cleanup(dip, fdc, hard_intr_set, 0);
657 		return (DDI_FAILURE);
658 	}
659 	/* Finish mapping the device registers & setting up structures */
660 	if (fd_attach_map_regs(dip, fdc) == DDI_FAILURE) {
661 		fd_cleanup(dip, fdc, hard_intr_set, 0);
662 		return (DDI_FAILURE);
663 	}
664 
665 	/*
666 	 * Initialize the DMA limit structures if it's being used.
667 	 */
668 	if (fdc->c_fdtype & FDCTYPE_DMA) {
669 		fdc->c_fd_dma_lim.dma_attr_version = DMA_ATTR_V0;
670 		fdc->c_fd_dma_lim.dma_attr_addr_lo = 0x00000000ull;
671 		fdc->c_fd_dma_lim.dma_attr_addr_hi = 0xfffffffeull;
672 		fdc->c_fd_dma_lim.dma_attr_count_max = 0xffffff;
673 		if (fdc->c_fdtype & FDCTYPE_SB) {
674 			fdc->c_fd_dma_lim.dma_attr_align = FD_SB_DMA_ALIGN;
675 		} else {
676 			fdc->c_fd_dma_lim.dma_attr_align = 1;
677 		}
678 		fdc->c_fd_dma_lim.dma_attr_burstsizes = 0x0;
679 		fdc->c_fd_dma_lim.dma_attr_minxfer = 1;
680 		fdc->c_fd_dma_lim.dma_attr_maxxfer = 0xffff;
681 		fdc->c_fd_dma_lim.dma_attr_seg = 0xffff;
682 		fdc->c_fd_dma_lim.dma_attr_sgllen = 1;
683 		fdc->c_fd_dma_lim.dma_attr_granular = 512;
684 
685 		if (ddi_dma_alloc_handle(dip, &fdc->c_fd_dma_lim,
686 		    DDI_DMA_DONTWAIT, 0, &fdc->c_dmahandle) != DDI_SUCCESS) {
687 			fd_cleanup(dip, fdc, hard_intr_set, 0);
688 			return (DDI_FAILURE);
689 		}
690 
691 		if (fdc->c_fdtype & FDCTYPE_SB) {
692 			ddi_device_acc_attr_t dev_attr;
693 			size_t	rlen;
694 
695 			dev_attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
696 			dev_attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
697 			dev_attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
698 
699 			if (ddi_dma_mem_alloc(fdc->c_dmahandle,
700 			    (size_t)(32*1024), &dev_attr, DDI_DMA_CONSISTENT,
701 			    DDI_DMA_SLEEP, NULL, (caddr_t *)&fdc->dma_buf,
702 			    &rlen, &fdc->c_dma_buf_handle) != DDI_SUCCESS) {
703 			    fd_cleanup(dip, fdc, hard_intr_set, 0);
704 				return (DDI_FAILURE);
705 			}
706 
707 		}
708 	}
709 
710 
711 	/* Register the interrupts */
712 	if (fd_attach_register_interrupts(dip, fdc,
713 	    &hard_intr_set) == DDI_FAILURE) {
714 		fd_cleanup(dip, fdc, hard_intr_set, 0);
715 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
716 		    (C, "fd_attach: registering interrupts failed\n"));
717 		return (DDI_FAILURE);
718 	}
719 
720 
721 	/*
722 	 * set initial controller/drive/disk "characteristics/geometry"
723 	 *
724 	 * NOTE:  The driver only supports one floppy drive.  The hardware
725 	 * only supports one drive because there is only one auxio register
726 	 * for one drive.
727 	 */
728 	fdc->c_un = kmem_zalloc(sizeof (struct fdunit), KM_SLEEP);
729 	fdc->c_un->un_chars = kmem_alloc(sizeof (struct fd_char), KM_SLEEP);
730 	fdc->c_un->un_iostat = kstat_create("fd", 0, "fd0", "disk",
731 	    KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
732 	if (fdc->c_un->un_iostat) {
733 		fdc->c_un->un_iostat->ks_lock = &fdc->c_lolock;
734 		kstat_install(fdc->c_un->un_iostat);
735 	}
736 
737 	fdc->c_un->un_drive = kmem_zalloc(sizeof (struct fd_drive), KM_SLEEP);
738 
739 	/* check for the manual eject property */
740 	if (ddi_getprop(DDI_DEV_T_ANY, dip,
741 	    DDI_PROP_DONTPASS, FD_MANUAL_EJECT, 0)) {
742 		fdc->c_un->un_drive->fdd_ejectable = 0;
743 	} else {
744 		/* an absence of the property indicates auto eject */
745 		fdc->c_un->un_drive->fdd_ejectable = -1;
746 	}
747 
748 	FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_attach: ejectable? %d\n",
749 	    fdc->c_un->un_drive->fdd_ejectable));
750 
751 	/*
752 	 * Check for the drive id.  If the drive id property doesn't exist
753 	 * then the drive id is set to 0
754 	 */
755 	fdc->c_un->un_unit_no = ddi_getprop(DDI_DEV_T_ANY, dip,
756 	    DDI_PROP_DONTPASS, FD_UNIT, 0);
757 
758 
759 	if (fdc->c_fdtype & FDCTYPE_SB) {
760 		fdc->sb_dma_channel = ddi_getprop(DDI_DEV_T_ANY, dip,
761 			DDI_PROP_DONTPASS, "dma-channel", 0);
762 	}
763 
764 
765 	FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_attach: unit %d\n",
766 	    fdc->c_un->un_unit_no));
767 
768 	/* Initially set the characteristics to high density */
769 	fdc->c_un->un_curfdtype = 1;
770 	*fdc->c_un->un_chars = fdtypes[fdc->c_un->un_curfdtype];
771 	fdunpacklabel(&fdlbl_high_80, &fdc->c_un->un_label);
772 
773 	/* Make sure drive is present */
774 	if (fd_attach_check_drive(fdc) == DDI_FAILURE) {
775 		fd_cleanup(dip, fdc, hard_intr_set, 1);
776 		return (DDI_FAILURE);
777 	}
778 
779 	for (dmdp = fd_minor; dmdp->name != NULL; dmdp++) {
780 		if (ddi_create_minor_node(dip, dmdp->name, dmdp->type,
781 		(instance << FDINSTSHIFT) | dmdp->minor,
782 		DDI_NT_FD, 0) == DDI_FAILURE) {
783 			fd_cleanup(dip, fdc, hard_intr_set, 1);
784 			return (DDI_FAILURE);
785 		}
786 	}
787 
788 	create_pm_components(dip);
789 
790 	/*
791 	 * Add a zero-length attribute to tell the world we support
792 	 * kernel ioctls (for layered drivers)
793 	 */
794 	(void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
795 	    DDI_KERNEL_IOCTL, NULL, 0);
796 
797 	ddi_report_dev(dip);
798 
799 	FDERRPRINT(FDEP_L1, FDEM_ATTA,
800 	    (C, "attached 0x%x\n", ddi_get_instance(dip)));
801 
802 	return (DDI_SUCCESS);
803 }
804 
805 /*
806  * Finish mapping the registers and initializing structures
807  */
808 static int
809 fd_attach_map_regs(dev_info_t *dip, struct fdctlr *fdc)
810 {
811 	ddi_device_acc_attr_t attr;
812 
813 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
814 	attr.devacc_attr_endian_flags  = DDI_STRUCTURE_LE_ACC;
815 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
816 
817 	/* Map the DMA registers of the platform supports DMA */
818 	if (fdc->c_fdtype & FDCTYPE_SB) {
819 		if (ddi_regs_map_setup(dip, 1, (caddr_t *)&fdc->c_dma_regs,
820 			0, sizeof (struct sb_dma_reg), &attr,
821 			&fdc->c_handlep_dma)) {
822 			return (DDI_FAILURE);
823 		}
824 
825 
826 	} else if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
827 		if (ddi_regs_map_setup(dip, 1, (caddr_t *)&fdc->c_dma_regs,
828 			0, sizeof (struct cheerio_dma_reg), &attr,
829 			&fdc->c_handlep_dma)) {
830 			return (DDI_FAILURE);
831 		}
832 	}
833 
834 	/* Reset the DMA engine and enable floppy interrupts */
835 	reset_dma_controller(fdc);
836 	set_dma_control_register(fdc, DCSR_INIT_BITS);
837 
838 	/* Finish initializing structures associated with the device regs */
839 	switch (fdc->c_fdtype & FDCTYPE_CTRLMASK) {
840 	case FDCTYPE_82077:
841 		FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "type is 82077\n"));
842 		/*
843 		 * Initialize addrs of key registers
844 		 */
845 		fdc->c_control =
846 		    (uchar_t *)&fdc->c_reg->fdc_82077_reg.fdc_control;
847 		fdc->c_fifo = (uchar_t *)&fdc->c_reg->fdc_82077_reg.fdc_fifo;
848 		fdc->c_dor = (uchar_t *)&fdc->c_reg->fdc_82077_reg.fdc_dor;
849 		fdc->c_dir = (uchar_t *)&fdc->c_reg->fdc_82077_reg.fdc_dir;
850 
851 
852 		FDERRPRINT(FDEP_L1, FDEM_ATTA, ((int)C,
853 			(char *)"fdattach: msr/dsr at %p\n",
854 			(void *)fdc->c_control));
855 
856 		/*
857 		 * The 82077 doesn't use the first configuration parameter
858 		 * so let's adjust that while we know we're an 82077.
859 		 */
860 		fdconf[0] = 0;
861 
862 		quiesce_fd_interrupt(fdc);
863 		break;
864 	default:
865 		break;
866 	}
867 
868 	return (0);
869 }
870 
871 /*
872  * Determine which type of floppy controller is present and
873  * initialize the registers accordingly
874  */
875 static int
876 fd_attach_det_ctlr(dev_info_t *dip, struct fdctlr *fdc)
877 {
878 	ddi_device_acc_attr_t attr;
879 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
880 	/* DDI_NEVERSWAP_ACC since the controller has a byte interface. */
881 	attr.devacc_attr_endian_flags  = DDI_STRUCTURE_LE_ACC;
882 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
883 
884 	FDERRPRINT(FDEP_L1, FDEM_ATTA,
885 			    (C, "fdattach_det_cltr: start \n"));
886 
887 	/*
888 	 * First, map in the controller's registers
889 	 * The controller has an 8-bit interface, so byte
890 	 * swapping isn't needed
891 	 */
892 
893 	if (ddi_regs_map_setup(dip, 0, (caddr_t *)&fdc->c_reg,
894 				0, sizeof (union fdcreg),
895 				&attr,
896 				&fdc->c_handlep_cont)) {
897 			return (DDI_FAILURE);
898 	}
899 
900 	FDERRPRINT(FDEP_L1, FDEM_ATTA,
901 			    (C, "fdattach_det_cltr: mapped floppy regs\n"));
902 
903 
904 	/*
905 	 * Set platform specific characteristics based on the device-tree
906 	 * node name.
907 	 */
908 
909 
910 	if (strcmp(ddi_get_name(dip), "SUNW,fdtwo") == 0) {
911 		fdc->c_fdtype |= FDCTYPE_SLAVIO;
912 		fdc->c_fdtype |= FDCTYPE_82077;
913 		fdc->c_auxiova = fd_getauxiova(dip);
914 		fdc->c_auxiodata = (uchar_t)(AUX_MBO4M|AUX_TC4M);
915 		fdc->c_auxiodata2 = (uchar_t)AUX_TC4M;
916 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
917 			    (C, "fdattach: slavio will be used!\n"));
918 
919 
920 /*
921  * Check the binding name to identify whether it is a South bridge based
922  * system or not.
923  */
924 	} else if (strcmp(ddi_get_name(dip), "pnpALI,1533,0") == 0) {
925 
926 		fdc->c_fdtype |= FDCTYPE_SB;
927 		fdc->c_fdtype |= FDCTYPE_82077;
928 		fdc->c_fdtype |= FDCTYPE_DMA;
929 
930 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
931 			(C, "fdattach: southbridge will be used!\n"));
932 
933 		/*
934 		 * The driver assumes high density characteristics until
935 		 * the diskette is looked at.
936 		 */
937 
938 		fdc->c_fdtype |= FDCTYPE_DMA8237;
939 		FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_attach: DMA used\n"));
940 
941 
942 	} else if (strcmp(ddi_get_name(dip), "fdthree") == 0) {
943 
944 		fdc->c_fdtype |= FDCTYPE_CHEERIO;
945 		fdc->c_fdtype |= FDCTYPE_82077;
946 
947 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
948 			    (C, "fdattach: cheerio will be used!\n"));
949 		/*
950 		 * The cheerio auxio register should be memory mapped.  The
951 		 * auxio register on other platforms is shared and mapped
952 		 * elsewhere in the kernel
953 		 */
954 		if (ddi_regs_map_setup(dip, 2, (caddr_t *)&fdc->c_auxio_reg,
955 		    0, sizeof (uint_t), &attr, &fdc->c_handlep_aux)) {
956 			return (DDI_FAILURE);
957 		}
958 
959 		/*
960 		 * The driver assumes high density characteristics until
961 		 * the diskette is looked at.
962 		 */
963 		Set_auxio(fdc, AUX_HIGH_DENSITY);
964 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
965 			    (C, "fdattach: auxio register 0x%x\n",
966 				 *fdc->c_auxio_reg));
967 
968 		fdc->c_fdtype |= FDCTYPE_DMA;
969 		FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_attach: DMA used\n"));
970 
971 	}
972 
973 	if (fdc->c_fdtype == 0) {
974 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
975 			    (C, "fdattach: no controller!\n"));
976 		return (DDI_FAILURE);
977 	} else {
978 		return (0);
979 	}
980 }
981 
982 
983 /*
984  * Register the floppy interrupts
985  */
986 static int
987 fd_attach_register_interrupts(dev_info_t *dip, struct fdctlr *fdc, int *hard)
988 {
989 	ddi_iblock_cookie_t  iblock_cookie_soft;
990 	int status;
991 
992 	/*
993 	 * First call ddi_get_iblock_cookie() to retrieve the
994 	 * the interrupt block cookie so that the mutexes may
995 	 * be initialized before adding the interrupt.  If the
996 	 * mutexes are initialized after adding the interrupt, there
997 	 * could be a race condition.
998 	 */
999 	if (ddi_get_iblock_cookie(dip, 0, &fdc->c_block) != DDI_SUCCESS) {
1000 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
1001 		(C, "fdattach: ddi_get_iblock_cookie failed\n"));
1002 		return (DDI_FAILURE);
1003 
1004 	}
1005 
1006 	/* Initialize high level mutex */
1007 	mutex_init(&fdc->c_hilock, NULL, MUTEX_DRIVER, fdc->c_block);
1008 
1009 	/*
1010 	 * Try to register fast trap handler, if unable try standard
1011 	 * interrupt handler, else bad
1012 	 */
1013 
1014 	if (fdc->c_fdtype & FDCTYPE_DMA) {
1015 		if (ddi_add_intr(dip, 0, &fdc->c_block, 0,
1016 			    fdintr_dma, (caddr_t)0) == DDI_SUCCESS) {
1017 				FDERRPRINT(FDEP_L1, FDEM_ATTA,
1018 				(C, "fdattach: standard intr\n"));
1019 
1020 				/*
1021 				 * When DMA is used, the low level lock
1022 				 * is used in the hard interrupt handler.
1023 				 */
1024 				mutex_init(&fdc->c_lolock, NULL,
1025 					MUTEX_DRIVER, fdc->c_block);
1026 
1027 				*hard = 1;
1028 		} else {
1029 			FDERRPRINT(FDEP_L1, FDEM_ATTA,
1030 			(C, "fdattach: can't add dma intr\n"));
1031 
1032 			mutex_destroy(&fdc->c_hilock);
1033 
1034 			return (DDI_FAILURE);
1035 		}
1036 	} else {
1037 		/*
1038 		 * Platforms that don't support DMA have both hard
1039 		 * and soft interrupts.
1040 		 */
1041 		if (ddi_add_intr(dip, 0, &fdc->c_block, 0,
1042 			fd_intr, (caddr_t)0) == DDI_SUCCESS) {
1043 				FDERRPRINT(FDEP_L1, FDEM_ATTA,
1044 				(C, "fdattach: standard intr\n"));
1045 				*hard = 1;
1046 
1047 			/* fast traps are not enabled */
1048 			fdc->c_fasttrap = 0;
1049 
1050 		} else {
1051 			FDERRPRINT(FDEP_L1, FDEM_ATTA,
1052 			(C, "fdattach: can't add intr\n"));
1053 
1054 			mutex_destroy(&fdc->c_hilock);
1055 
1056 			return (DDI_FAILURE);
1057 		}
1058 
1059 
1060 		/*
1061 		 * Initialize the soft interrupt handler.  First call
1062 		 * ddi_get_soft_iblock_cookie() so that the mutex may
1063 		 * be initialized before the handler is added.
1064 		 */
1065 		status = ddi_get_soft_iblock_cookie(dip, DDI_SOFTINT_LOW,
1066 				&iblock_cookie_soft);
1067 
1068 
1069 		if (status != DDI_SUCCESS) {
1070 			mutex_destroy(&fdc->c_hilock);
1071 			return (DDI_FAILURE);
1072 		}
1073 
1074 		/*
1075 		 * Initialize low level mutex which is used in the soft
1076 		 * interrupt handler
1077 		 */
1078 		mutex_init(&fdc->c_lolock, NULL, MUTEX_DRIVER,
1079 			iblock_cookie_soft);
1080 
1081 		if (ddi_add_softintr(dip, DDI_SOFTINT_LOW, &fdc->c_softid,
1082 					NULL, NULL,
1083 					fd_lointr,
1084 					(caddr_t)fdc) != DDI_SUCCESS) {
1085 
1086 			mutex_destroy(&fdc->c_hilock);
1087 			mutex_destroy(&fdc->c_lolock);
1088 
1089 			return (DDI_FAILURE);
1090 		}
1091 	}
1092 
1093 	fdc->c_intrstat = kstat_create("fd", 0, "fdc0", "controller",
1094 		KSTAT_TYPE_INTR, 1, KSTAT_FLAG_PERSISTENT);
1095 	if (fdc->c_intrstat) {
1096 		fdc->c_hiintct = &KIOIP->intrs[KSTAT_INTR_HARD];
1097 		kstat_install(fdc->c_intrstat);
1098 	}
1099 
1100 	/* condition variable to wait on while an io transaction occurs */
1101 	cv_init(&fdc->c_iocv, NULL, CV_DRIVER, NULL);
1102 
1103 	/* condition variable for the csb */
1104 	cv_init(&fdc->c_csbcv, NULL, CV_DRIVER, NULL);
1105 
1106 	/* condition variable for motor on waiting period */
1107 	cv_init(&fdc->c_motoncv, NULL, CV_DRIVER, NULL);
1108 
1109 	/* semaphore to serialize opens and closes */
1110 	sema_init(&fdc->c_ocsem, 1, NULL, SEMA_DRIVER, NULL);
1111 
1112 	/* condition variable to wait on suspended floppy controller. */
1113 	cv_init(&fdc->c_suspend_cv, NULL, CV_DRIVER, NULL);
1114 
1115 	return (0);
1116 }
1117 
1118 /*
1119  * Make sure the drive is present
1120  * 	- acquires the low level lock
1121  */
1122 static int
1123 fd_attach_check_drive(struct fdctlr *fdc)
1124 {
1125 	int tmp_fderrlevel;
1126 	int unit = fdc->c_un->un_unit_no;
1127 
1128 	FDERRPRINT(FDEP_L1, FDEM_ATTA,
1129 			(C, "fd_attach_check_drive\n"));
1130 
1131 
1132 	mutex_enter(&fdc->c_lolock);
1133 	switch (fdc->c_fdtype & FDCTYPE_CTRLMASK) {
1134 
1135 	/* insure that the eject line is reset */
1136 	case FDCTYPE_82077:
1137 
1138 		/*
1139 		 * Everything but the motor enable, drive select,
1140 		 * and reset bits are turned off.  These three
1141 		 * bits remain as they are.
1142 		 */
1143 		/* LINTED */
1144 		Set_dor(fdc, ~((MOTEN(unit))|DRVSEL|RESET), 0);
1145 
1146 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
1147 			(C, "fdattach: Dor 0x%x\n", Dor(fdc)));
1148 
1149 		drv_usecwait(5);
1150 		if (unit == 0) {
1151 			/* LINTED */
1152 			Set_dor(fdc, RESET|DRVSEL, 1);
1153 		} else {
1154 
1155 			/* LINTED */
1156 			Set_dor(fdc, DRVSEL, 0);
1157 			/* LINTED */
1158 			Set_dor(fdc, RESET, 1);
1159 		}
1160 
1161 		drv_usecwait(5);
1162 
1163 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
1164 			(C, "fdattach: Dor 0x%x\n", Dor(fdc)));
1165 
1166 		if (!((fdc->c_fdtype & FDCTYPE_CHEERIO) ||
1167 				(fdc->c_fdtype & FDCTYPE_SB))) {
1168 			set_auxioreg(AUX_TC4M, 0);
1169 		}
1170 		break;
1171 	default:
1172 		break;
1173 	}
1174 
1175 
1176 	fdgetcsb(fdc);
1177 	if (fdreset(fdc) != 0) {
1178 		mutex_exit(&fdc->c_lolock);
1179 		return (DDI_FAILURE);
1180 	}
1181 
1182 
1183 	/* check for drive present */
1184 
1185 	tmp_fderrlevel = fderrlevel;
1186 
1187 
1188 	fderrlevel = FDEP_LMAX;
1189 
1190 	FDERRPRINT(FDEP_L1, FDEM_ATTA,
1191 			(C, "fdattach: call fdrecalseek\n"));
1192 
1193 	/* Make sure the drive is present */
1194 	if (fdrecalseek(fdc, unit, -1, 0) != 0) {
1195 		timeout_id_t timeid = fdc->c_mtimeid;
1196 		fderrlevel = tmp_fderrlevel;
1197 		fdc->c_mtimeid = 0;
1198 		mutex_exit(&fdc->c_lolock);
1199 
1200 
1201 		/* Do not hold the mutex over the call to untimeout */
1202 		if (timeid) {
1203 			(void) untimeout(timeid);
1204 		}
1205 
1206 		FDERRPRINT(FDEP_L2, FDEM_ATTA,
1207 		    (C, "fd_attach: no drive?\n"));
1208 
1209 		return (DDI_FAILURE);
1210 	}
1211 
1212 	fderrlevel = tmp_fderrlevel;
1213 
1214 	fdselect(fdc, unit, 0);    /* deselect drive zero (used in fdreset) */
1215 	fdretcsb(fdc);
1216 	mutex_exit(&fdc->c_lolock);
1217 
1218 	return (0);
1219 }
1220 
1221 /*
1222  * Clean up routine used by fd_detach and fd_attach
1223  *
1224  * Note: if the soft id is non-zero, then ddi_add_softintr() completed
1225  * successfully.  I can not make the same assumption about the iblock_cookie
1226  * for the high level interrupt handler.  So, the hard parameter indicates
1227  * whether or not a high level interrupt handler has been added.
1228  *
1229  * If the locks parameter is nonzero, then all mutexes, semaphores and
1230  * condition variables will be destroyed.
1231  *
1232  * Does not assume the low level mutex is held.
1233  *
1234  */
1235 static void
1236 fd_cleanup(dev_info_t *dip, struct fdctlr *fdc, int hard, int locks)
1237 {
1238 
1239 
1240 	FDERRPRINT(FDEP_L1, FDEM_ATTA,
1241 		(C, "fd_cleanup instance: %d ctlr: 0x%p\n",
1242 		ddi_get_instance(dip), (void *)fdc));
1243 
1244 
1245 	if (fdc == NULL) {
1246 		return;
1247 	}
1248 
1249 	/*
1250 	 * Remove interrupt handlers first before anything else
1251 	 * is deallocated.
1252 	 */
1253 
1254 	/* Remove hard interrupt if one is registered */
1255 	if (hard) {
1256 		ddi_remove_intr(dip, (uint_t)0, fdc->c_block);
1257 	}
1258 
1259 	/* Remove soft interrupt if one is registered */
1260 	if (fdc->c_softid != NULL)
1261 		ddi_remove_softintr(fdc->c_softid);
1262 
1263 
1264 	/* Remove timers */
1265 	if (fdc->c_fdtype & FDCTYPE_82077) {
1266 		if (fdc->c_mtimeid)
1267 			(void) untimeout(fdc->c_mtimeid);
1268 		/*
1269 		 * Need to turn off motor (includes select/LED for South Bridge
1270 		 * chipset) just in case it was on when timer was removed
1271 		 */
1272 		fdmotoff(fdc);
1273 	}
1274 	if (fdc->c_timeid)
1275 		(void) untimeout(fdc->c_timeid);
1276 
1277 
1278 	/* Remove memory handles */
1279 	if (fdc->c_handlep_cont)
1280 		ddi_regs_map_free(&fdc->c_handlep_cont);
1281 
1282 	if (fdc->c_handlep_aux)
1283 		ddi_regs_map_free(&fdc->c_handlep_aux);
1284 
1285 	if (fdc->c_handlep_dma)
1286 		ddi_regs_map_free(&fdc->c_handlep_dma);
1287 
1288 	if (fdc->c_dma_buf_handle != NULL)
1289 		ddi_dma_mem_free(&fdc->c_dma_buf_handle);
1290 
1291 	if (fdc->c_dmahandle != NULL)
1292 		ddi_dma_free_handle(&fdc->c_dmahandle);
1293 
1294 
1295 	/* Remove all minor nodes */
1296 	ddi_remove_minor_node(dip, NULL);
1297 
1298 
1299 
1300 	/* Remove unit structure if one exists */
1301 	if (fdc->c_un != (struct fdunit *)NULL) {
1302 
1303 		ASSERT(!mutex_owned(&fdc->c_lolock));
1304 
1305 		if (fdc->c_un->un_iostat)
1306 			kstat_delete(fdc->c_un->un_iostat);
1307 		fdc->c_un->un_iostat = NULL;
1308 
1309 		if (fdc->c_un->un_chars)
1310 			kmem_free(fdc->c_un->un_chars, sizeof (struct fd_char));
1311 
1312 		if (fdc->c_un->un_drive)
1313 			kmem_free(fdc->c_un->un_drive,
1314 			    sizeof (struct fd_drive));
1315 
1316 		kmem_free((caddr_t)fdc->c_un, sizeof (struct fdunit));
1317 	}
1318 
1319 	if (fdc->c_intrstat) {
1320 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
1321 				(C, "fd_cleanup: delete intrstat\n"));
1322 
1323 		kstat_delete(fdc->c_intrstat);
1324 	}
1325 
1326 	fdc->c_intrstat = NULL;
1327 
1328 	if (locks) {
1329 		cv_destroy(&fdc->c_iocv);
1330 		cv_destroy(&fdc->c_csbcv);
1331 		cv_destroy(&fdc->c_motoncv);
1332 		cv_destroy(&fdc->c_suspend_cv);
1333 		sema_destroy(&fdc->c_ocsem);
1334 		mutex_destroy(&fdc->c_hilock);
1335 		mutex_destroy(&fdc->c_lolock);
1336 	}
1337 
1338 
1339 	fdctlrs = fdc->c_next;
1340 	kmem_free(fdc, sizeof (*fdc));
1341 
1342 
1343 }
1344 
1345 
1346 static int
1347 fd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1348 {
1349 	int instance = ddi_get_instance(dip);
1350 	struct fdctlr *fdc = fd_getctlr(instance << FDINSTSHIFT);
1351 	timeout_id_t c_mtimeid;
1352 
1353 	FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_detach\n"));
1354 
1355 	switch (cmd) {
1356 
1357 	case DDI_DETACH:
1358 		/*
1359 		 * The hard parameter is set to 1.  If detach is called, then
1360 		 * attach must have passed meaning that the high level
1361 		 * interrupt handler was successfully added.
1362 		 * Similarly, the locks parameter is also set to 1.
1363 		 */
1364 		fd_cleanup(dip, fdc, 1, 1);
1365 
1366 		ddi_prop_remove_all(dip);
1367 
1368 		return (DDI_SUCCESS);
1369 
1370 	case DDI_SUSPEND:
1371 		if (!fdc)
1372 			return (DDI_FAILURE);
1373 
1374 
1375 		mutex_enter(&fdc->c_lolock);
1376 		fdgetcsb(fdc);	/* Wait for I/O to finish */
1377 		c_mtimeid = fdc->c_mtimeid;
1378 		fdretcsb(fdc);
1379 		mutex_exit(&fdc->c_lolock);
1380 
1381 		(void) untimeout(c_mtimeid);
1382 		/*
1383 		 * After suspend, the system could be powered off.
1384 		 * When it is later powered on the southbridge floppy
1385 		 * controller will tristate the interrupt line causing
1386 		 * continuous dma interrupts.
1387 		 * To avoid getting continuous fd interrupts we will remove the
1388 		 * dma interrupt handler installed. We will re-install the
1389 		 * handler when we RESUME.
1390 		 */
1391 		if (fdc->c_fdtype & FDCTYPE_SB)
1392 			ddi_remove_intr(dip, 0, fdc->c_block);
1393 
1394 		fdc->c_un->un_state = FD_STATE_SUSPENDED;
1395 
1396 		return (DDI_SUCCESS);
1397 
1398 	default:
1399 		return (DDI_FAILURE);
1400 	}
1401 }
1402 
1403 /* ARGSUSED */
1404 static int
1405 fd_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1406 {
1407 	register struct fdctlr *fdc;
1408 	register int error;
1409 
1410 	switch (infocmd) {
1411 
1412 	case DDI_INFO_DEVT2DEVINFO:
1413 		if ((fdc = fd_getctlr((dev_t)arg)) == NULL) {
1414 			error = DDI_FAILURE;
1415 		} else {
1416 			*result = fdc->c_dip;
1417 			error = DDI_SUCCESS;
1418 		}
1419 		break;
1420 
1421 	case DDI_INFO_DEVT2INSTANCE:
1422 		*result = 0;
1423 		error = DDI_SUCCESS;
1424 		break;
1425 
1426 	default:
1427 		error = DDI_FAILURE;
1428 	}
1429 	return (error);
1430 }
1431 
1432 /*
1433  * property operation routine.  return the number of blocks for the partition
1434  * in question or forward the request to the property facilities.
1435  */
1436 static int
1437 fd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
1438     char *name, caddr_t valuep, int *lengthp)
1439 {
1440 	struct fdunit	*un;
1441 	struct fdctlr	*fdc;
1442 	uint64_t	nblocks64;
1443 
1444 	/*
1445 	 * Our dynamic properties are all device specific and size oriented.
1446 	 * Requests issued under conditions where size is valid are passed
1447 	 * to ddi_prop_op_nblocks with the size information, otherwise the
1448 	 * request is passed to ddi_prop_op.
1449 	 */
1450 	if (dev == DDI_DEV_T_ANY) {
1451 pass:  		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
1452 		    name, valuep, lengthp));
1453 	} else {
1454 		fdc = fd_getctlr(dev);
1455 		if (fdc == NULL)
1456 			goto pass;
1457 
1458 		/* we have size if diskette opened and label read */
1459 		un = fdc->c_un;
1460 		if ((un == NULL) || !fd_unit_is_open(fdc->c_un))
1461 			goto pass;
1462 
1463 		/* get nblocks value */
1464 		nblocks64 = (ulong_t)
1465 		    un->un_label.dkl_map[FDPARTITION(dev)].dkl_nblk;
1466 
1467 		return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags,
1468 		    name, valuep, lengthp, nblocks64));
1469 	}
1470 }
1471 
1472 /* ARGSUSED3 */
1473 static int
1474 fd_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
1475 {
1476 	dev_t dev;
1477 	int  part;
1478 	struct fdctlr *fdc;
1479 	struct fdunit *un;
1480 	struct dk_map32 *dkm;
1481 	uchar_t	pbit;
1482 	int	err, part_is_open;
1483 	int 	unit;
1484 
1485 	dev = *devp;
1486 	fdc = fd_getctlr(dev);
1487 	if ((fdc == NULL) || ((un = fdc->c_un) == NULL)) {
1488 		return (ENXIO);
1489 	}
1490 
1491 	unit = fdc->c_un->un_unit_no;
1492 
1493 	/*
1494 	 * Serialize opens/closes
1495 	 */
1496 
1497 	sema_p(&fdc->c_ocsem);
1498 
1499 	/* check partition */
1500 	part = FDPARTITION(dev);
1501 	pbit = 1 << part;
1502 	dkm = &un->un_label.dkl_map[part];
1503 	if (dkm->dkl_nblk == 0) {
1504 		sema_v(&fdc->c_ocsem);
1505 		return (ENXIO);
1506 	}
1507 
1508 	FDERRPRINT(FDEP_L1, FDEM_OPEN,
1509 	    (C, "fdopen: ctlr %d unit %d part %d\n",
1510 	    ddi_get_instance(fdc->c_dip), unit, part));
1511 
1512 	FDERRPRINT(FDEP_L1, FDEM_OPEN,
1513 	    (C, "fdopen: flag 0x%x", flag));
1514 
1515 
1516 	/*
1517 	 * Insure that drive is present with a recalibrate on first open.
1518 	 */
1519 	(void) pm_busy_component(fdc->c_dip, 0);
1520 
1521 	mutex_enter(&fdc->c_lolock);
1522 
1523 	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
1524 
1525 	if (fdc->c_un->un_state == FD_STATE_STOPPED) {
1526 		mutex_exit(&fdc->c_lolock);
1527 		if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
1528 						!= DDI_SUCCESS) {
1529 			FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power change \
1530 failed. \n"));
1531 
1532 				sema_v(&fdc->c_ocsem);
1533 				(void) pm_idle_component(fdc->c_dip, 0);
1534 				return (EIO);
1535 		}
1536 		mutex_enter(&fdc->c_lolock);
1537 	}
1538 	if (fd_unit_is_open(un) == 0) {
1539 		fdgetcsb(fdc);
1540 		/*
1541 		 * no check changed!
1542 		 */
1543 		err = fdrecalseek(fdc, unit, -1, 0);
1544 		fdretcsb(fdc);
1545 		if (err) {
1546 			FDERRPRINT(FDEP_L3, FDEM_OPEN,
1547 			    (C, "fd%d: drive not ready\n", 0));
1548 			/* deselect drv on last close */
1549 			fdselect(fdc, unit, 0);
1550 			mutex_exit(&fdc->c_lolock);
1551 			sema_v(&fdc->c_ocsem);
1552 			(void) pm_idle_component(fdc->c_dip, 0);
1553 			return (EIO);
1554 		}
1555 	}
1556 
1557 	/*
1558 	 * Check for previous exclusive open, or trying to exclusive open
1559 	 */
1560 	if (otyp == OTYP_LYR) {
1561 		part_is_open = (un->un_lyropen[part] != 0);
1562 	} else {
1563 		part_is_open = fd_part_is_open(un, part);
1564 	}
1565 	if ((un->un_exclmask & pbit) || ((flag & FEXCL) && part_is_open)) {
1566 		mutex_exit(&fdc->c_lolock);
1567 		sema_v(&fdc->c_ocsem);
1568 		FDERRPRINT(FDEP_L2, FDEM_OPEN, (C, "fd:just return\n"));
1569 		(void) pm_idle_component(fdc->c_dip, 0);
1570 		return (EBUSY);
1571 	}
1572 
1573 	/* don't attempt access, just return successfully */
1574 	if (flag & (FNDELAY | FNONBLOCK)) {
1575 		FDERRPRINT(FDEP_L2, FDEM_OPEN,
1576 		    (C, "fd: return busy..\n"));
1577 		goto out;
1578 	}
1579 
1580 	fdc->c_csb.csb_unit = (uchar_t)unit;
1581 	if (fdgetlabel(fdc, unit)) {
1582 		/* didn't find label (couldn't read anything) */
1583 		FDERRPRINT(FDEP_L3, FDEM_OPEN,
1584 		    (C,
1585 		    "fd%d: unformatted diskette or no diskette in the drive\n",
1586 		    0));
1587 		if (fd_unit_is_open(un) == 0) {
1588 			/* deselect drv on last close */
1589 			fdselect(fdc, unit, 0);
1590 		}
1591 
1592 		mutex_exit(&fdc->c_lolock);
1593 		sema_v(&fdc->c_ocsem);
1594 		(void) pm_idle_component(fdc->c_dip, 0);
1595 		return (EIO);
1596 	}
1597 
1598 	/*
1599 	 * if opening for writing, check write protect on diskette
1600 	 */
1601 	if (flag & FWRITE) {
1602 		fdgetcsb(fdc);
1603 		err = fdsensedrv(fdc, unit) & WP_SR3;
1604 		fdretcsb(fdc);
1605 		if (err) {
1606 			if (fd_unit_is_open(un) == 0)
1607 				fdselect(fdc, unit, 0);
1608 			mutex_exit(&fdc->c_lolock);
1609 			sema_v(&fdc->c_ocsem);
1610 			(void) pm_idle_component(fdc->c_dip, 0);
1611 			return (EROFS);
1612 		}
1613 	}
1614 
1615 out:
1616 	/*
1617 	 * mark open as having succeeded
1618 	 */
1619 	if (flag & FEXCL) {
1620 		un->un_exclmask |= pbit;
1621 	}
1622 	if (otyp == OTYP_LYR) {
1623 		un->un_lyropen[part]++;
1624 	} else {
1625 		un->un_regopen[otyp] |= pbit;
1626 	}
1627 	mutex_exit(&fdc->c_lolock);
1628 	sema_v(&fdc->c_ocsem);
1629 	(void) pm_idle_component(fdc->c_dip, 0);
1630 	return (0);
1631 }
1632 /*
1633  * fd_part_is_open
1634  *	return 1 if the partition is open
1635  *	return 0 otherwise
1636  */
1637 static int
1638 fd_part_is_open(struct fdunit *un, int part)
1639 {
1640 	int i;
1641 	for (i = 0; i < OTYPCNT - 1; i++)
1642 		if (un->un_regopen[i] & (1 << part))
1643 			return (1);
1644 	return (0);
1645 }
1646 
1647 
1648 /* ARGSUSED */
1649 static int
1650 fd_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
1651 {
1652 	int unit, part_is_closed, part;
1653 	register struct fdctlr *fdc;
1654 	register struct fdunit *un;
1655 
1656 	fdc = fd_getctlr(dev);
1657 	if (!fdc || !(un = fdc->c_un))
1658 		return (ENXIO);
1659 
1660 
1661 	unit = fdc->c_un->un_unit_no;
1662 	FDERRPRINT(FDEP_L1, FDEM_CLOS, (C, "fd_close\n"));
1663 	part = FDPARTITION(dev);
1664 
1665 	sema_p(&fdc->c_ocsem);
1666 	mutex_enter(&fdc->c_lolock);
1667 
1668 	if (otyp == OTYP_LYR) {
1669 		un->un_lyropen[part]--;
1670 		part_is_closed = (un->un_lyropen[part] == 0);
1671 	} else {
1672 		un->un_regopen[otyp] &= ~(1<<part);
1673 		part_is_closed = 1;
1674 	}
1675 	if (part_is_closed)
1676 		un->un_exclmask &= ~(1<<part);
1677 
1678 	if (fd_unit_is_open(un) == 0) {
1679 		/* deselect drive on last close */
1680 		fdselect(fdc, unit, 0);
1681 		un->un_flags &= ~FDUNIT_CHANGED;
1682 	}
1683 	mutex_exit(&fdc->c_lolock);
1684 	sema_v(&fdc->c_ocsem);
1685 
1686 	return (0);
1687 }
1688 
1689 /*
1690  * fd_strategy
1691  *	checks operation, hangs buf struct off fdctlr, calls fdstart
1692  *	if not already busy.  Note that if we call start, then the operation
1693  *	will already be done on return (start sleeps).
1694  */
1695 static int
1696 fd_strategy(register struct buf *bp)
1697 {
1698 	struct fdctlr *fdc;
1699 	struct fdunit *un;
1700 	uint_t	phys_blkno;
1701 	struct dk_map32 *dkm;
1702 
1703 	FDERRPRINT(FDEP_L1, FDEM_STRA,
1704 	    (C, "fd_strategy: bp = 0x%p, dev = 0x%lx\n",
1705 	    (void *)bp, bp->b_edev));
1706 	FDERRPRINT(FDEP_L1, FDEM_STRA,
1707 	    (C, "b_blkno=%x b_flags=%x b_count=%x\n",
1708 	    (int)bp->b_blkno, bp->b_flags, (int)bp->b_bcount));
1709 	fdc = fd_getctlr(bp->b_edev);
1710 	un = fdc->c_un;
1711 	dkm = &un->un_label.dkl_map[FDPARTITION(bp->b_edev)];
1712 
1713 	/*
1714 	 * If it's medium density and the block no. isn't a multiple
1715 	 * of 1K, then return an error.
1716 	 */
1717 	if (un->un_chars->fdc_medium) {
1718 		phys_blkno = (uint_t)bp->b_blkno >> 1;
1719 		if (bp->b_blkno & 1) {
1720 			FDERRPRINT(FDEP_L3, FDEM_STRA,
1721 			    (C, "b_blkno=0x%lx is not 1k aligned\n",
1722 			    (long)bp->b_blkno));
1723 			bp->b_error = EINVAL;
1724 			bp->b_resid = bp->b_bcount;
1725 			bp->b_flags |= B_ERROR;
1726 			biodone(bp);
1727 			return (0);
1728 		}
1729 	} else {
1730 		phys_blkno = (uint_t)bp->b_blkno;
1731 	}
1732 
1733 
1734 	/* If the block number is past the end, return an error */
1735 	if ((phys_blkno > dkm->dkl_nblk)) {
1736 		FDERRPRINT(FDEP_L3, FDEM_STRA,
1737 		    (C, "fd%d: block %ld is past the end! (nblk=%d)\n",
1738 		    0, (long)bp->b_blkno, dkm->dkl_nblk));
1739 		bp->b_error = ENOSPC;
1740 		bp->b_resid = bp->b_bcount;
1741 		bp->b_flags |= B_ERROR;
1742 		biodone(bp);
1743 		return (0);
1744 	}
1745 
1746 	/* if at end of file, skip out now */
1747 	if (phys_blkno == dkm->dkl_nblk) {
1748 		FDERRPRINT(FDEP_L1, FDEM_STRA,
1749 		    (C, "b_blkno is at the end!\n"));
1750 
1751 		if ((bp->b_flags & B_READ) == 0) {
1752 			/* a write needs to get an error! */
1753 			bp->b_error = ENOSPC;
1754 			bp->b_flags |= B_ERROR;
1755 
1756 			FDERRPRINT(FDEP_L1, FDEM_STRA,
1757 			    (C, "block is at end and this is a write\n"));
1758 
1759 		}
1760 
1761 		bp->b_resid = bp->b_bcount;
1762 		biodone(bp);
1763 		return (0);
1764 	}
1765 
1766 	/* if operation not a multiple of sector size, is error! */
1767 	if (bp->b_bcount % un->un_chars->fdc_sec_size)	{
1768 		FDERRPRINT(FDEP_L3, FDEM_STRA,
1769 		    (C, "fd%d: requested transfer size(0x%lx) is not"
1770 			" multiple of sector size(0x%x)\n", 0,
1771 			bp->b_bcount, un->un_chars->fdc_sec_size));
1772 		FDERRPRINT(FDEP_L3, FDEM_STRA,
1773 		    (C, "	b_blkno=0x%lx b_flags=0x%x\n",
1774 		    (long)bp->b_blkno, bp->b_flags));
1775 		bp->b_error = EINVAL;
1776 		bp->b_resid = bp->b_bcount;
1777 		bp->b_flags |= B_ERROR;
1778 		biodone(bp);
1779 		return (0);
1780 
1781 	}
1782 
1783 	/*
1784 	 * Put the buf request in the controller's queue, FIFO.
1785 	 */
1786 	bp->av_forw = 0;
1787 	sema_p(&fdc->c_ocsem);
1788 
1789 	(void) pm_busy_component(fdc->c_dip, 0);
1790 
1791 	mutex_enter(&fdc->c_lolock);
1792 
1793 	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
1794 
1795 	if (fdc->c_un->un_state == FD_STATE_STOPPED) {
1796 		mutex_exit(&fdc->c_lolock);
1797 		if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
1798 						!= DDI_SUCCESS) {
1799 				sema_v(&fdc->c_ocsem);
1800 				(void) pm_idle_component(fdc->c_dip, 0);
1801 				bp->b_error = EIO;
1802 				bp->b_resid = bp->b_bcount;
1803 				bp->b_flags |= B_ERROR;
1804 				biodone(bp);
1805 				return (0);
1806 		} else {
1807 			mutex_enter(&fdc->c_lolock);
1808 		}
1809 	}
1810 	if (un->un_iostat) {
1811 		kstat_waitq_enter(KIOSP);
1812 	}
1813 	if (fdc->c_actf)
1814 		fdc->c_actl->av_forw = bp;
1815 	else
1816 		fdc->c_actf = bp;
1817 	fdc->c_actl = bp;
1818 
1819 
1820 	/* call fdstart to start the transfer */
1821 	fdstart(fdc);
1822 
1823 	mutex_exit(&fdc->c_lolock);
1824 	sema_v(&fdc->c_ocsem);
1825 	(void) pm_idle_component(fdc->c_dip, 0);
1826 	return (0);
1827 }
1828 
1829 /* ARGSUSED2 */
1830 static int
1831 fd_read(dev_t dev, struct uio *uio, cred_t *cred_p)
1832 {
1833 	FDERRPRINT(FDEP_L1, FDEM_RDWR, (C, "fd_read\n"));
1834 	return (physio(fd_strategy, NULL, dev, B_READ, minphys, uio));
1835 }
1836 
1837 /* ARGSUSED2 */
1838 static int
1839 fd_write(dev_t dev, struct uio *uio, cred_t *cred_p)
1840 {
1841 	FDERRPRINT(FDEP_L1, FDEM_RDWR, (C, "fd_write\n"));
1842 	return (physio(fd_strategy, NULL, dev, B_WRITE, minphys, uio));
1843 }
1844 
1845 static void
1846 fdmotoff(void *arg)
1847 {
1848 	struct fdctlr *fdc = arg;
1849 	int unit = fdc->c_un->un_unit_no;
1850 
1851 	mutex_enter(&fdc->c_lolock);
1852 
1853 	/* Just return if we're about to call untimeout */
1854 	if (fdc->c_mtimeid == 0) {
1855 		mutex_exit(&fdc->c_lolock);
1856 		return;
1857 	}
1858 
1859 	FDERRPRINT(FDEP_L1, FDEM_MOFF, (C, "fdmotoff\n"));
1860 
1861 	fdc->c_mtimeid = 0;
1862 
1863 	if (!(Msr(fdc) & CB) && (Dor(fdc) & (MOTEN(unit)))) {
1864 		/* LINTED */
1865 		Set_dor(fdc, MOTEN(unit), 0);
1866 	}
1867 
1868 	mutex_exit(&fdc->c_lolock);
1869 }
1870 
1871 /* ARGSUSED */
1872 static int
1873 fd_ioctl(dev_t dev, int cmd, intptr_t arg, int flag,
1874 	cred_t *cred_p, int *rval_p)
1875 {
1876 	union {
1877 		struct dk_cinfo dki;
1878 		struct dk_geom dkg;
1879 		struct dk_allmap32 dka;
1880 		struct fd_char fdchar;
1881 		struct fd_drive drvchar;
1882 		int	temp;
1883 	} cpy;
1884 
1885 	struct vtoc	vtoc;
1886 	struct fdunit *un;
1887 	struct fdctlr *fdc;
1888 	int unit, dkunit;
1889 	int err = 0;
1890 	uint_t	sec_size;
1891 	enum dkio_state state;
1892 	int	transfer_rate;
1893 
1894 	FDERRPRINT(FDEP_L1, FDEM_IOCT,
1895 	    (C, "fd_ioctl: cmd 0x%x, arg 0x%lx\n", cmd, (long)arg));
1896 
1897 	/* The minor number should always be 0 */
1898 	if (FDUNIT(dev) != 0)
1899 		return (ENXIO);
1900 
1901 	fdc = fd_getctlr(dev);
1902 	unit = fdc->c_un->un_unit_no;
1903 	un = fdc->c_un;
1904 	sec_size = un->un_chars->fdc_sec_size;
1905 	bzero(&cpy, sizeof (cpy));
1906 
1907 	switch (cmd) {
1908 	case DKIOCINFO:
1909 		cpy.dki.dki_addr = 0;
1910 
1911 		/*
1912 		 * The meaning of the dki_slave and dki_unit fields
1913 		 * is unclear.  The sparc floppy driver follows the same
1914 		 * convention as sd.c in that the instance number is
1915 		 * returned in the dki_cnum field.  The dki_slave field is
1916 		 * ignored.
1917 		 *
1918 		 * The dki_cnum contains the controller instance
1919 		 * and its value can be any positive number. Even
1920 		 * though currently Sparc platforms only support
1921 		 * one controller, the controller instance number
1922 		 * can be any number since it is assigned by the
1923 		 * system depending on the device properties.
1924 		 */
1925 
1926 		cpy.dki.dki_cnum = FDCTLR(dev);
1927 
1928 		/*
1929 		 * Sparc platforms support only one floppy drive.
1930 		 * The device node for the controller is the same as
1931 		 * the device node for the drive.  The x86 driver is
1932 		 * different in that it has a node for the controller
1933 		 * and a child node for each drive. Since Sparc supports
1934 		 * only one drive, the unit number will always be zero.
1935 		 */
1936 
1937 		cpy.dki.dki_unit = FDUNIT(dev);
1938 
1939 		/*
1940 		 * The meaning of the dki_slave field is unclear.
1941 		 * So, I will leave it set to 0.
1942 		 */
1943 
1944 		cpy.dki.dki_slave = 0;
1945 
1946 		cpy.dki.dki_ctype = (ushort_t)-1;
1947 		if (fdc->c_fdtype & FDCTYPE_82077)
1948 			cpy.dki.dki_ctype = DKC_INTEL82077;
1949 		cpy.dki.dki_flags = DKI_FMTTRK;
1950 		cpy.dki.dki_partition = FDPARTITION(dev);
1951 		cpy.dki.dki_maxtransfer = maxphys / DEV_BSIZE;
1952 		if (ddi_copyout((caddr_t)&cpy.dki, (caddr_t)arg,
1953 						sizeof (cpy.dki), flag))
1954 			err = EFAULT;
1955 		break;
1956 	case DKIOCGGEOM:
1957 		cpy.dkg.dkg_ncyl = un->un_chars->fdc_ncyl;
1958 		cpy.dkg.dkg_nhead = un->un_chars->fdc_nhead;
1959 		cpy.dkg.dkg_nsect = un->un_chars->fdc_secptrack;
1960 		cpy.dkg.dkg_intrlv = un->un_label.dkl_intrlv;
1961 		cpy.dkg.dkg_rpm = un->un_label.dkl_rpm;
1962 		cpy.dkg.dkg_pcyl = un->un_chars->fdc_ncyl;
1963 		cpy.dkg.dkg_read_reinstruct =
1964 		    (int)(cpy.dkg.dkg_nsect * cpy.dkg.dkg_rpm * 4) / 60000;
1965 		cpy.dkg.dkg_write_reinstruct = cpy.dkg.dkg_read_reinstruct;
1966 		if (ddi_copyout((caddr_t)&cpy.dkg, (caddr_t)arg,
1967 						sizeof (cpy.dkg), flag))
1968 			err = EFAULT;
1969 		break;
1970 	case DKIOCSGEOM:
1971 		FDERRPRINT(FDEP_L3, FDEM_IOCT,
1972 		    (C, "fd_ioctl: DKIOCSGEOM not supported\n"));
1973 		err = ENOTTY;
1974 		break;
1975 
1976 	/*
1977 	 * return the map of all logical partitions
1978 	 */
1979 	case DKIOCGAPART:
1980 		/*
1981 		 * We don't have anything to do if the application is ILP32
1982 		 * because the label map has a 32-bit format. Otherwise
1983 		 * convert.
1984 		 */
1985 		if ((flag & DATAMODEL_MASK) == DATAMODEL_ILP32) {
1986 			if (ddi_copyout(&un->un_label.dkl_map,
1987 				(void *)arg, sizeof (struct dk_allmap32), flag))
1988 				err = EFAULT;
1989 		}
1990 #ifdef _MULTI_DATAMODEL
1991 		else {
1992 			struct dk_allmap dk_allmap;
1993 
1994 			ASSERT((flag & DATAMODEL_MASK) == DATAMODEL_LP64);
1995 			for (dkunit = 0; dkunit < NDKMAP; dkunit++) {
1996 				dk_allmap.dka_map[dkunit].dkl_cylno =
1997 				    un->un_label.dkl_map[dkunit].dkl_cylno;
1998 				dk_allmap.dka_map[dkunit].dkl_nblk =
1999 				    un->un_label.dkl_map[dkunit].dkl_nblk;
2000 			}
2001 			if (ddi_copyout(&dk_allmap, (void *)arg,
2002 					sizeof (struct dk_allmap), flag))
2003 				err = EFAULT;
2004 		}
2005 #endif /* _MULTI_DATAMODEL */
2006 		break;
2007 
2008 	/*
2009 	 * Set the map of all logical partitions
2010 	 */
2011 	case DKIOCSAPART:
2012 		if ((flag & DATAMODEL_MASK) == DATAMODEL_ILP32) {
2013 			if (ddi_copyin((const void *)arg, &cpy.dka,
2014 			    sizeof (cpy.dka), flag))
2015 				return (EFAULT);
2016 			else {
2017 				mutex_enter(&fdc->c_lolock);
2018 				for (dkunit = 0; dkunit < NDKMAP; dkunit++) {
2019 					un->un_label.dkl_map[dkunit] =
2020 						cpy.dka.dka_map[dkunit];
2021 				}
2022 				mutex_exit(&fdc->c_lolock);
2023 			}
2024 		}
2025 #ifdef _MULTI_DATAMODEL
2026 		else {
2027 			struct dk_allmap dk_allmap;
2028 
2029 			ASSERT((flag & DATAMODEL_MASK) == DATAMODEL_LP64);
2030 			if (ddi_copyin((const void *)arg, &dk_allmap,
2031 			    sizeof (dk_allmap), flag))
2032 				return (EFAULT);
2033 			else {
2034 				mutex_enter(&fdc->c_lolock);
2035 				for (dkunit = 0; dkunit < NDKMAP; dkunit++) {
2036 					un->un_label.dkl_map[dkunit].dkl_cylno =
2037 					    dk_allmap.dka_map[dkunit].dkl_cylno;
2038 					un->un_label.dkl_map[dkunit].dkl_nblk =
2039 					    dk_allmap.dka_map[dkunit].dkl_nblk;
2040 				}
2041 				mutex_exit(&fdc->c_lolock);
2042 			}
2043 		}
2044 #endif /* _MULTI_DATAMODEL */
2045 		break;
2046 
2047 	case DKIOCGVTOC:
2048 		mutex_enter(&fdc->c_lolock);
2049 
2050 		/*
2051 		 * Exit if the diskette has no label.
2052 		 * Also, get the label to make sure the
2053 		 * correct one is being used since the diskette
2054 		 * may have changed
2055 		 */
2056 		if (fdgetlabel(fdc, unit)) {
2057 			mutex_exit(&fdc->c_lolock);
2058 			err = EINVAL;
2059 			break;
2060 		}
2061 
2062 		/* Build a vtoc from the diskette's label */
2063 		fd_build_user_vtoc(un, &vtoc);
2064 		mutex_exit(&fdc->c_lolock);
2065 
2066 #ifdef _MULTI_DATAMODEL
2067 		switch (ddi_model_convert_from(flag & FMODELS)) {
2068 		case DDI_MODEL_ILP32: {
2069 			struct vtoc32 vtoc32;
2070 
2071 			vtoctovtoc32(vtoc, vtoc32);
2072 			if (ddi_copyout(&vtoc32, (void *)arg,
2073 			    sizeof (struct vtoc32), flag))
2074 				return (EFAULT);
2075 			break;
2076 		}
2077 
2078 		case DDI_MODEL_NONE:
2079 			if (ddi_copyout(&vtoc, (void *)arg,
2080 			    sizeof (vtoc), flag))
2081 				return (EFAULT);
2082 			break;
2083 		}
2084 #else /* ! _MULTI_DATAMODEL */
2085 		if (ddi_copyout(&vtoc, (void *)arg, sizeof (vtoc), flag))
2086 			return (EFAULT);
2087 #endif /* _MULTI_DATAMODEL */
2088 		break;
2089 
2090 	case DKIOCSVTOC:
2091 
2092 #ifdef _MULTI_DATAMODEL
2093 		switch (ddi_model_convert_from(flag & FMODELS)) {
2094 		case DDI_MODEL_ILP32: {
2095 			struct vtoc32 vtoc32;
2096 
2097 			if (ddi_copyin((const void *)arg, &vtoc32,
2098 			    sizeof (struct vtoc32), flag)) {
2099 				return (EFAULT);
2100 			}
2101 			vtoc32tovtoc(vtoc32, vtoc);
2102 			break;
2103 		}
2104 
2105 		case DDI_MODEL_NONE:
2106 			if (ddi_copyin((const void *)arg, &vtoc,
2107 			    sizeof (vtoc), flag)) {
2108 				return (EFAULT);
2109 			}
2110 			break;
2111 		}
2112 #else /* ! _MULTI_DATAMODEL */
2113 		if (ddi_copyin((const void *)arg, &vtoc, sizeof (vtoc), flag))
2114 			return (EFAULT);
2115 #endif /* _MULTI_DATAMODEL */
2116 
2117 		mutex_enter(&fdc->c_lolock);
2118 
2119 		/*
2120 		 * The characteristics structure must be filled in because
2121 		 * it helps build the vtoc.
2122 		 */
2123 		if ((un->un_chars->fdc_ncyl == 0) ||
2124 				(un->un_chars->fdc_nhead == 0) ||
2125 				(un->un_chars->fdc_secptrack == 0)) {
2126 			mutex_exit(&fdc->c_lolock);
2127 			err = EINVAL;
2128 			break;
2129 		}
2130 
2131 		if ((err = fd_build_label_vtoc(un, &vtoc)) != 0) {
2132 			mutex_exit(&fdc->c_lolock);
2133 			break;
2134 		}
2135 
2136 		(void) pm_busy_component(fdc->c_dip, 0);
2137 
2138 		err = fdrw(fdc, unit, FDWRITE, 0, 0, 1,
2139 		    (caddr_t)&un->un_label, sizeof (struct dk_label));
2140 		mutex_exit(&fdc->c_lolock);
2141 		(void) pm_idle_component(fdc->c_dip, 0);
2142 		break;
2143 
2144 	case DKIOCSTATE:
2145 		if (ddi_copyin((caddr_t)arg, (caddr_t)&state,
2146 					sizeof (int), flag)) {
2147 			err = EFAULT;
2148 			break;
2149 		}
2150 		(void) pm_busy_component(fdc->c_dip, 0);
2151 
2152 		err = fd_check_media(dev, state);
2153 		(void) pm_idle_component(fdc->c_dip, 0);
2154 
2155 		if (ddi_copyout((caddr_t)&un->un_media_state,
2156 					(caddr_t)arg, sizeof (int), flag))
2157 			err = EFAULT;
2158 		break;
2159 
2160 	case FDIOGCHAR:
2161 		if (ddi_copyout((caddr_t)un->un_chars, (caddr_t)arg,
2162 					sizeof (struct fd_char), flag))
2163 			err = EFAULT;
2164 		break;
2165 
2166 	case FDIOSCHAR:
2167 		if (ddi_copyin((caddr_t)arg, (caddr_t)&cpy.fdchar,
2168 				sizeof (struct fd_char), flag)) {
2169 			err = EFAULT;
2170 			break;
2171 		}
2172 
2173 		/*
2174 		 * Check the fields in the fdchar structure that are either
2175 		 * driver or controller dependent.
2176 		 */
2177 
2178 		transfer_rate = cpy.fdchar.fdc_transfer_rate;
2179 		if ((transfer_rate != 500) && (transfer_rate != 300) &&
2180 		    (transfer_rate != 250) && (transfer_rate != 1000)) {
2181 			FDERRPRINT(FDEP_L3, FDEM_IOCT,
2182 			(C, "fd_ioctl: FDIOSCHAR odd transfer rate %d\n",
2183 			    cpy.fdchar.fdc_transfer_rate));
2184 			err = EINVAL;
2185 			break;
2186 		}
2187 
2188 		if ((cpy.fdchar.fdc_nhead < 1) ||
2189 				(cpy.fdchar.fdc_nhead > 2)) {
2190 			FDERRPRINT(FDEP_L3, FDEM_IOCT,
2191 			(C, "fd_ioctl: FDIOSCHAR bad no. of heads %d\n",
2192 			    cpy.fdchar.fdc_nhead));
2193 			err = EINVAL;
2194 			break;
2195 		}
2196 
2197 		/*
2198 		 * The number of cylinders must be between 0 and 255
2199 		 */
2200 		if ((cpy.fdchar.fdc_ncyl < 0) || (cpy.fdchar.fdc_ncyl > 255)) {
2201 			FDERRPRINT(FDEP_L3, FDEM_IOCT,
2202 			(C, "fd_ioctl: FDIOSCHAR bad cyl no %d\n",
2203 			    cpy.fdchar.fdc_ncyl));
2204 			err = EINVAL;
2205 			break;
2206 		}
2207 
2208 		/* Copy the fdchar structure */
2209 
2210 		mutex_enter(&fdc->c_lolock);
2211 		*(un->un_chars) = cpy.fdchar;
2212 
2213 		un->un_curfdtype = -1;
2214 
2215 		mutex_exit(&fdc->c_lolock);
2216 
2217 		break;
2218 	case FDEJECT:  /* eject disk */
2219 	case DKIOCEJECT:
2220 
2221 		/*
2222 		 * Fail the ioctl if auto-eject isn't supported
2223 		 */
2224 		if (fdc->c_un->un_drive->fdd_ejectable == 0) {
2225 
2226 			err = ENOSYS;
2227 
2228 		} else {
2229 			(void) pm_busy_component(fdc->c_dip, 0);
2230 
2231 			mutex_enter(&fdc->c_lolock);
2232 
2233 			CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
2234 
2235 			if (fdc->c_un->un_state == FD_STATE_STOPPED) {
2236 				mutex_exit(&fdc->c_lolock);
2237 				if ((pm_raise_power(fdc->c_dip, 0,
2238 					PM_LEVEL_ON)) != DDI_SUCCESS) {
2239 					(void) pm_idle_component(fdc->c_dip, 0);
2240 					err = EIO;
2241 				}
2242 				mutex_enter(&fdc->c_lolock);
2243 			}
2244 		}
2245 		if (err == 0) {
2246 			fdselect(fdc, unit, 1);
2247 			fdeject(fdc, unit);
2248 			mutex_exit(&fdc->c_lolock);
2249 		}
2250 
2251 		(void) pm_idle_component(fdc->c_dip, 0);
2252 
2253 		/*
2254 		 * Make sure the drive is turned off
2255 		 */
2256 		if (fdc->c_fdtype & FDCTYPE_82077) {
2257 			if (fdc->c_mtimeid == 0) {
2258 				fdc->c_mtimeid = timeout(fdmotoff, fdc,
2259 					Motoff_delay);
2260 			}
2261 		}
2262 
2263 		break;
2264 	case FDGETCHANGE: /* disk changed */
2265 
2266 		if (ddi_copyin((caddr_t)arg, (caddr_t)&cpy.temp,
2267 						sizeof (int), flag)) {
2268 			err = EFAULT;
2269 			break;
2270 		}
2271 
2272 		/* zero out the user's parameter */
2273 		cpy.temp = 0;
2274 
2275 		(void) pm_busy_component(fdc->c_dip, 0);
2276 
2277 		mutex_enter(&fdc->c_lolock);
2278 
2279 		CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
2280 
2281 		if (fdc->c_un->un_state == FD_STATE_STOPPED) {
2282 			mutex_exit(&fdc->c_lolock);
2283 			if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
2284 						!= DDI_SUCCESS) {
2285 				FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power \
2286 change failed. \n"));
2287 				(void) pm_idle_component(fdc->c_dip, 0);
2288 				return (EIO);
2289 			}
2290 
2291 			mutex_enter(&fdc->c_lolock);
2292 		}
2293 		if (un->un_flags & FDUNIT_CHANGED)
2294 			cpy.temp |= FDGC_HISTORY;
2295 		else
2296 			cpy.temp &= ~FDGC_HISTORY;
2297 		un->un_flags &= ~FDUNIT_CHANGED;
2298 
2299 		if (fd_pollable) {
2300 			/*
2301 			 * If it's a "pollable" floppy, then we don't
2302 			 * have to do all the fdcheckdisk nastyness to
2303 			 * figure out if the thing is still there.
2304 			 */
2305 			if (fdsense_chng(fdc, unit)) {
2306 				cpy.temp |= FDGC_CURRENT;
2307 			} else {
2308 				cpy.temp &= ~FDGC_CURRENT;
2309 			}
2310 		} else {
2311 
2312 			if (fdsense_chng(fdc, unit)) {
2313 				/*
2314 				 * check disk change signal is asserted.
2315 				 * Now find out if the floppy is
2316 				 * inserted
2317 				 */
2318 				if (fdcheckdisk(fdc, unit)) {
2319 					cpy.temp |= FDGC_CURRENT;
2320 				} else {
2321 					/*
2322 					 * Yes, the floppy was
2323 					 * reinserted. Implies
2324 					 * floppy change.
2325 					 */
2326 					cpy.temp &= ~FDGC_CURRENT;
2327 					cpy.temp |= FDGC_HISTORY;
2328 				}
2329 			} else {
2330 				cpy.temp &= ~FDGC_CURRENT;
2331 			}
2332 		}
2333 
2334 		/*
2335 		 * For a pollable floppy, the floppy_change signal
2336 		 * reflects whether the floppy is in there or not.
2337 		 * We can not detect a floppy change if we don't poll
2338 		 * this signal when the floppy is being changed.
2339 		 * Because as soon as the floppy is put back, the
2340 		 * signal is reset.
2341 		 * BUT the pollable floppies are available only on
2342 		 * Sparcstation Voyager Voyagers (Gypsy) only and
2343 		 * those are motorized floppies. For motorized floppies,
2344 		 * the floppy can only (assuming the user doesn't use a
2345 		 * pin to take out the floppy) be taken out by
2346 		 * issuing 'eject' command which sets the
2347 		 * un->un_ejected flag. So, if the following
2348 		 * condition is true, we can assume there
2349 		 * was a floppy change.
2350 		 */
2351 		if (un->un_ejected && !(cpy.temp & FDGC_CURRENT)) {
2352 			cpy.temp |= FDGC_HISTORY;
2353 		}
2354 		un->un_ejected = 0;
2355 
2356 
2357 		/* return the write-protection status */
2358 		fdgetcsb(fdc);
2359 		if (fdsensedrv(fdc, unit) & WP_SR3) {
2360 			cpy.temp |= FDGC_CURWPROT;
2361 		}
2362 		fdretcsb(fdc);
2363 		mutex_exit(&fdc->c_lolock);
2364 
2365 		if (ddi_copyout((caddr_t)&cpy.temp, (caddr_t)arg,
2366 						sizeof (int), flag))
2367 			err = EFAULT;
2368 		(void) pm_idle_component(fdc->c_dip, 0);
2369 		break;
2370 
2371 	case FDGETDRIVECHAR:
2372 
2373 		if (ddi_copyin((caddr_t)arg, (caddr_t)&cpy.drvchar,
2374 				sizeof (struct fd_drive), flag)) {
2375 			err = EFAULT;
2376 			break;
2377 		}
2378 
2379 		/*
2380 		 * Return the ejectable value based on the FD_MANUAL_EJECT
2381 		 * property
2382 		 */
2383 		cpy.drvchar.fdd_ejectable = fdc->c_un->un_drive->fdd_ejectable;
2384 		cpy.drvchar.fdd_maxsearch = nfdtypes; /* 3 - hi m lo density */
2385 		if (fd_pollable)	/* pollable device */
2386 			cpy.drvchar.fdd_flags |= FDD_POLLABLE;
2387 
2388 		/* the rest of the fd_drive struct is meaningless to us */
2389 
2390 		if (ddi_copyout((caddr_t)&cpy.drvchar, (caddr_t)arg,
2391 					sizeof (struct fd_drive), flag))
2392 			err = EFAULT;
2393 		break;
2394 
2395 	case FDSETDRIVECHAR:
2396 		FDERRPRINT(FDEP_L3, FDEM_IOCT,
2397 		    (C, "fd_ioctl: FDSETDRIVECHAR not supportedn\n"));
2398 		err = ENOTTY;
2399 		break;
2400 
2401 	case DKIOCREMOVABLE: {
2402 		int	i = 1;
2403 
2404 		/* no brainer: floppies are always removable */
2405 		if (ddi_copyout((caddr_t)&i, (caddr_t)arg, sizeof (int),
2406 		    flag)) {
2407 			err = EFAULT;
2408 		}
2409 		break;
2410 	}
2411 	case DKIOCGMEDIAINFO:
2412 		err = fd_get_media_info(un, (caddr_t)arg, flag);
2413 		break;
2414 
2415 
2416 	case FDIOCMD:
2417 	{
2418 		struct fd_cmd fc;
2419 		int cyl, hd, spc, spt;
2420 		int nblks; /* total no. of blocks */
2421 
2422 #ifdef _MULTI_DATAMODEL
2423 		switch (ddi_model_convert_from(flag & FMODELS)) {
2424 		case DDI_MODEL_ILP32: {
2425 			struct fd_cmd32 fc32;
2426 
2427 			if (ddi_copyin((const void *)arg, &fc32,
2428 			    sizeof (fc32), flag)) {
2429 				return (EFAULT);
2430 			}
2431 			fc.fdc_cmd	= fc32.fdc_cmd;
2432 			fc.fdc_flags	= fc32.fdc_flags;
2433 			fc.fdc_blkno	= (daddr_t)fc32.fdc_blkno;
2434 			fc.fdc_secnt	= fc32.fdc_secnt;
2435 			fc.fdc_bufaddr	= (caddr_t)(uintptr_t)fc32.fdc_bufaddr;
2436 			fc.fdc_buflen	= fc32.fdc_buflen;
2437 			fc.fdc_cmd	= fc32.fdc_cmd;
2438 
2439 			break;
2440 		}
2441 
2442 		case DDI_MODEL_NONE:
2443 			if (ddi_copyin((const void *)arg, &fc,
2444 			    sizeof (fc), flag)) {
2445 				return (EFAULT);
2446 			}
2447 			break;
2448 		}
2449 #else /* ! _MULTI_DATAMODEL */
2450 		if (ddi_copyin((const void *)arg, &fc, sizeof (fc), flag)) {
2451 			return (EFAULT);
2452 		}
2453 #endif /* _MULTI_DATAMODEL */
2454 
2455 		if (fc.fdc_cmd == FDCMD_READ || fc.fdc_cmd == FDCMD_WRITE) {
2456 			auto struct iovec aiov;
2457 			auto struct uio auio;
2458 			struct uio *uio = &auio;
2459 
2460 			spc = (fc.fdc_cmd == FDCMD_READ)? B_READ: B_WRITE;
2461 
2462 			bzero(&auio, sizeof (struct uio));
2463 			bzero(&aiov, sizeof (struct iovec));
2464 			aiov.iov_base = fc.fdc_bufaddr;
2465 			aiov.iov_len = (uint_t)fc.fdc_secnt * sec_size;
2466 			uio->uio_iov = &aiov;
2467 
2468 			uio->uio_iovcnt = 1;
2469 			uio->uio_resid = aiov.iov_len;
2470 			uio->uio_segflg = UIO_USERSPACE;
2471 			FDERRPRINT(FDEP_L2, FDEM_IOCT,
2472 			    (C, "fd_ioctl: call physio\n"));
2473 			err = physio(fd_strategy, NULL, dev,
2474 			    spc, minphys, uio);
2475 			break;
2476 		} else if (fc.fdc_cmd != FDCMD_FORMAT_TRACK) {
2477 
2478 			/*
2479 			 * The manpage states that only the FDCMD_WRITE,
2480 			 * FDCMD_READ, and the FDCMD_FORMAT_TR are available.
2481 			 */
2482 			FDERRPRINT(FDEP_L1, FDEM_IOCT,
2483 			    (C, "fd_ioctl: FDIOCMD invalid command\n"));
2484 			err = EINVAL;
2485 			break;
2486 		}
2487 
2488 		/* The command is FDCMD_FORMAT_TRACK */
2489 
2490 		spt = un->un_chars->fdc_secptrack;	/* sec/trk */
2491 		spc = un->un_chars->fdc_nhead * spt;	/* sec/cyl */
2492 		cyl = fc.fdc_blkno / spc;
2493 		hd = (fc.fdc_blkno % spc) / spt;
2494 
2495 		/*
2496 		 * Make sure the specified block number is in the correct
2497 		 * range. (block numbers start at 0)
2498 		 */
2499 		nblks = spc * un->un_chars->fdc_ncyl;
2500 
2501 		if (fc.fdc_blkno < 0 || fc.fdc_blkno > (nblks - 1)) {
2502 			err = EINVAL;
2503 			break;
2504 		}
2505 
2506 		(void) pm_busy_component(fdc->c_dip, 0);
2507 
2508 		mutex_enter(&fdc->c_lolock);
2509 		CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
2510 		if (fdc->c_un->un_state == FD_STATE_STOPPED) {
2511 			mutex_exit(&fdc->c_lolock);
2512 			if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
2513 						!= DDI_SUCCESS) {
2514 				FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power \
2515 change failed. \n"));
2516 				(void) pm_idle_component(fdc->c_dip, 0);
2517 				return (EIO);
2518 			}
2519 
2520 			mutex_enter(&fdc->c_lolock);
2521 		}
2522 
2523 		if (fdformat(fdc, unit, cyl, hd))
2524 			err = EIO;
2525 
2526 		mutex_exit(&fdc->c_lolock);
2527 		(void) pm_idle_component(fdc->c_dip, 0);
2528 
2529 		break;
2530 	}
2531 
2532 	case FDRAW:
2533 
2534 		(void) pm_busy_component(fdc->c_dip, 0);
2535 		err = fdrawioctl(fdc, unit, arg, flag);
2536 
2537 		(void) pm_idle_component(fdc->c_dip, 0);
2538 
2539 		break;
2540 #ifdef FD_DEBUG
2541 	case IOCTL_DEBUG:
2542 		fderrlevel--;
2543 		if (fderrlevel < 0)
2544 			fderrlevel = 3;
2545 		cmn_err(C, "fdioctl: CHANGING debug to %d", fderrlevel);
2546 		return (0);
2547 #endif /* FD_DEBUG */
2548 	default:
2549 		FDERRPRINT(FDEP_L2, FDEM_IOCT,
2550 		    (C, "fd_ioctl: invalid ioctl 0x%x\n", cmd));
2551 		err = ENOTTY;
2552 		break;
2553 	}
2554 
2555 	return (err);
2556 }
2557 
2558 /*
2559  * fdrawioctl
2560  *
2561  * 	- acquires the low level lock
2562  */
2563 
2564 static int
2565 fdrawioctl(struct fdctlr *fdc, int unit, intptr_t arg, int mode)
2566 {
2567 	struct fd_raw fdr;
2568 #ifdef _MULTI_DATAMODEL
2569 	struct fd_raw32 fdr32;
2570 #endif
2571 	struct fdcsb *csb;
2572 	int i, err, flag;
2573 	caddr_t fa;
2574 	uint_t	fc;
2575 	size_t	real_length;
2576 	int	res;
2577 	ddi_device_acc_attr_t attr;
2578 	ddi_acc_handle_t	mem_handle;
2579 
2580 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
2581 	attr.devacc_attr_endian_flags  = DDI_STRUCTURE_BE_ACC;
2582 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
2583 
2584 	ASSERT(fdc->c_un->un_unit_no == unit);
2585 
2586 	FDERRPRINT(FDEP_L1, FDEM_RAWI,
2587 	    (C, "fdrawioctl: cmd[0]=0x%x\n", fdr.fdr_cmd[0]));
2588 
2589 	flag = B_READ;
2590 	err = 0;
2591 	fa = NULL;
2592 	fc = (uint_t)0;
2593 
2594 	/* Copy in the arguments */
2595 	switch (ddi_model_convert_from(mode)) {
2596 #ifdef _MULTI_DATAMODEL
2597 	case DDI_MODEL_ILP32:
2598 		if (ddi_copyin((caddr_t)arg, (caddr_t)&fdr32,
2599 		    sizeof (fdr32), mode)) {
2600 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2601 			(C, "fdrawioctl: copyin error, args32\n"));
2602 			return (EFAULT);
2603 		}
2604 		bcopy(fdr32.fdr_cmd, fdr.fdr_cmd, sizeof (fdr.fdr_cmd));
2605 		fdr.fdr_cnum = fdr32.fdr_cnum;
2606 		bcopy(fdr32.fdr_result, fdr.fdr_result,
2607 		    sizeof (fdr.fdr_result));
2608 		fdr.fdr_nbytes = fdr32.fdr_nbytes;
2609 		fdr.fdr_addr = (caddr_t)(uintptr_t)fdr32.fdr_addr;
2610 		break;
2611 #endif
2612 	default:
2613 	case DDI_MODEL_NONE:
2614 		if (ddi_copyin((caddr_t)arg, (caddr_t)&fdr,
2615 		    sizeof (fdr), mode)) {
2616 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2617 			(C, "fdrawioctl: copyin error, args\n"));
2618 			return (EFAULT);
2619 		}
2620 		break;
2621 	}
2622 
2623 	mutex_enter(&fdc->c_lolock);
2624 
2625 	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
2626 
2627 	if (fdc->c_un->un_state == FD_STATE_STOPPED) {
2628 		mutex_exit(&fdc->c_lolock);
2629 		if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
2630 					!= DDI_SUCCESS) {
2631 			FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power change \
2632 failed. \n"));
2633 
2634 			(void) pm_idle_component(fdc->c_dip, 0);
2635 			return (EIO);
2636 		}
2637 		mutex_enter(&fdc->c_lolock);
2638 	}
2639 
2640 	fdgetcsb(fdc);
2641 	csb = &fdc->c_csb;
2642 	csb->csb_unit = (uchar_t)unit;
2643 
2644 	/* copy cmd bytes into csb */
2645 	for (i = 0; i <= fdr.fdr_cnum; i++)
2646 		csb->csb_cmds[i] = fdr.fdr_cmd[i];
2647 	csb->csb_ncmds = (uchar_t)fdr.fdr_cnum;
2648 
2649 	csb->csb_maxretry = 0;	/* let the application deal with errors */
2650 	csb->csb_retrys = 0;
2651 
2652 	switch (fdr.fdr_cmd[0] & 0x0f) {
2653 
2654 	case FDRAW_SPECIFY:
2655 		/*
2656 		 * Ensure that the right DMA mode is selected.  There is
2657 		 * currently no way for the user to tell if DMA is
2658 		 * happening so set the value for the user.
2659 		 */
2660 
2661 		if (fdc->c_fdtype & FDCTYPE_DMA)
2662 			csb->csb_cmds[2] = csb->csb_cmds[2] & 0xFE;
2663 		else
2664 			csb->csb_cmds[2] = csb->csb_cmds[2] | 0x1;
2665 
2666 		csb->csb_opflags = CSB_OFNORESULTS;
2667 		csb->csb_nrslts = 0;
2668 		break;
2669 
2670 	case FDRAW_SENSE_DRV:
2671 		/* Insert the appropriate drive number */
2672 		csb->csb_cmds[1] = csb->csb_cmds[1] | (unit & DRV_MASK);
2673 		csb->csb_opflags = CSB_OFIMMEDIATE;
2674 		csb->csb_nrslts = 1;
2675 		break;
2676 
2677 	case FDRAW_REZERO:
2678 	case FDRAW_SEEK:
2679 		/* Insert the appropriate drive number */
2680 		csb->csb_cmds[1] = csb->csb_cmds[1] | (unit & DRV_MASK);
2681 		csb->csb_opflags = CSB_OFSEEKOPS + CSB_OFTIMEIT;
2682 		csb->csb_nrslts = 2;
2683 		break;
2684 
2685 	case FDRAW_FORMAT:
2686 		FDERRPRINT(FDEP_L1, FDEM_RAWI,
2687 			(C, "fdrawioctl: cmd is fdfraw format\n"));
2688 
2689 		/* Insert the appropriate drive number */
2690 		csb->csb_cmds[1] = csb->csb_cmds[1] | (unit & DRV_MASK);
2691 		csb->csb_opflags = CSB_OFXFEROPS + CSB_OFTIMEIT;
2692 		csb->csb_nrslts = NRBRW;
2693 		flag = B_WRITE;
2694 
2695 		/*
2696 		 * Allocate memory for the command.
2697 		 * If PIO is being used, then add an extra 16 bytes
2698 		 */
2699 		if (fdc->c_fdtype & FDCTYPE_DMA) {
2700 
2701 			fc = (uint_t)(fdr.fdr_nbytes);
2702 			mutex_enter(&fdc->c_hilock);
2703 
2704 			res = ddi_dma_mem_alloc(fdc->c_dmahandle, fc,
2705 				&attr, DDI_DMA_STREAMING,
2706 				DDI_DMA_DONTWAIT, 0, &fa, &real_length,
2707 				&mem_handle);
2708 
2709 			if (res != DDI_SUCCESS) {
2710 				fdretcsb(fdc);
2711 				mutex_exit(&fdc->c_lolock);
2712 				mutex_exit(&fdc->c_hilock);
2713 				return (EIO);
2714 			}
2715 
2716 			fdc->c_csb.csb_read = CSB_WRITE;
2717 			if (fdstart_dma(fdc, fa, fc) != 0) {
2718 				ddi_dma_mem_free(&mem_handle);
2719 				fdretcsb(fdc);
2720 				mutex_exit(&fdc->c_lolock);
2721 				mutex_exit(&fdc->c_hilock);
2722 				return (EIO);
2723 			}
2724 			mutex_exit(&fdc->c_hilock);
2725 
2726 		} else {
2727 			fc = (uint_t)(fdr.fdr_nbytes + 16);
2728 			fa = kmem_zalloc(fc, KM_SLEEP);
2729 		}
2730 
2731 		/* copy in the user's command bytes */
2732 		if (ddi_copyin(fdr.fdr_addr, fa,
2733 				(uint_t)fdr.fdr_nbytes, mode)) {
2734 			fdretcsb(fdc);
2735 			mutex_exit(&fdc->c_lolock);
2736 
2737 			if (fdc->c_fdtype & FDCTYPE_DMA) {
2738 				ddi_dma_mem_free(&mem_handle);
2739 				FDERRPRINT(FDEP_L1, FDEM_RAWI,
2740 				(C, "fdrawioctl: (err)free dma memory\n"));
2741 			} else {
2742 				kmem_free(fa, fc);
2743 			}
2744 
2745 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2746 			(C, "fdrawioctl: ddi_copyin error\n"));
2747 			return (EFAULT);
2748 		}
2749 
2750 		break;
2751 	case FDRAW_WRCMD:
2752 	case FDRAW_WRITEDEL:
2753 		flag = B_WRITE;
2754 		/* FALLTHROUGH */
2755 	case FDRAW_RDCMD:
2756 	case FDRAW_READDEL:
2757 	case FDRAW_READTRACK:
2758 		/* Insert the appropriate drive number */
2759 		csb->csb_cmds[1] = csb->csb_cmds[1] | (unit & DRV_MASK);
2760 		if (fdc->c_fdtype & FDCTYPE_SB)
2761 			csb->csb_cmds[1] |= IPS;
2762 		csb->csb_opflags = CSB_OFXFEROPS + CSB_OFTIMEIT;
2763 		csb->csb_nrslts = NRBRW;
2764 		break;
2765 
2766 	default:
2767 		fdretcsb(fdc);
2768 		mutex_exit(&fdc->c_lolock);
2769 		return (EINVAL);
2770 	}
2771 
2772 	if ((csb->csb_opflags & CSB_OFXFEROPS) && (fdr.fdr_nbytes == 0)) {
2773 		fdretcsb(fdc);
2774 		mutex_exit(&fdc->c_lolock);
2775 		return (EINVAL);
2776 	}
2777 	csb->csb_opflags |= CSB_OFRAWIOCTL;
2778 
2779 	FDERRPRINT(FDEP_L1, FDEM_RAWI,
2780 		(C, "fdrawioctl: nbytes = %u\n", fdr.fdr_nbytes));
2781 
2782 	if ((fdr.fdr_cmd[0] & 0x0f) != FDRAW_FORMAT) {
2783 		if ((fc = (uint_t)fdr.fdr_nbytes) > 0) {
2784 			/*
2785 			 * In SunOS 4.X, we used to as_fault things in.
2786 			 * We really cannot do this in 5.0/SVr4. Unless
2787 			 * someone really believes that speed is of the
2788 			 * essence here, it is just much simpler to do
2789 			 * this in kernel space and use copyin/copyout.
2790 			 */
2791 			if (fdc->c_fdtype & FDCTYPE_DMA) {
2792 				mutex_enter(&fdc->c_hilock);
2793 				res = ddi_dma_mem_alloc(fdc->c_dmahandle, fc,
2794 					&attr, DDI_DMA_STREAMING,
2795 					DDI_DMA_DONTWAIT, 0, &fa, &real_length,
2796 					&mem_handle);
2797 
2798 				if (res != DDI_SUCCESS) {
2799 					fdretcsb(fdc);
2800 					mutex_exit(&fdc->c_lolock);
2801 					mutex_exit(&fdc->c_hilock);
2802 					return (EIO);
2803 				}
2804 
2805 				if (flag == B_WRITE)
2806 					fdc->c_csb.csb_read = CSB_WRITE;
2807 				else
2808 					fdc->c_csb.csb_read = CSB_READ;
2809 
2810 				if (fdstart_dma(fdc, fa, fc) != 0) {
2811 					ddi_dma_mem_free(&mem_handle);
2812 					fdretcsb(fdc);
2813 					mutex_exit(&fdc->c_lolock);
2814 					mutex_exit(&fdc->c_hilock);
2815 					return (EIO);
2816 				}
2817 				mutex_exit(&fdc->c_hilock);
2818 
2819 			} else {
2820 				fa = kmem_zalloc(fc, KM_SLEEP);
2821 			}
2822 
2823 			if (flag == B_WRITE) {
2824 				if (ddi_copyin(fdr.fdr_addr, fa, fc, mode)) {
2825 					if (fdc->c_fdtype & FDCTYPE_DMA)
2826 						ddi_dma_mem_free(&mem_handle);
2827 					else
2828 						kmem_free(fa, fc);
2829 					fdretcsb(fdc);
2830 					mutex_exit(&fdc->c_lolock);
2831 					FDERRPRINT(FDEP_L1, FDEM_RAWI,
2832 					(C, "fdrawioctl: can't copy data\n"));
2833 
2834 					return (EFAULT);
2835 				}
2836 			}
2837 			csb->csb_addr = fa;
2838 			csb->csb_len = fc;
2839 		} else {
2840 			csb->csb_addr = 0;
2841 			csb->csb_len = 0;
2842 		}
2843 	} else {
2844 		csb->csb_addr = fa;
2845 		csb->csb_len = fc;
2846 	}
2847 
2848 	FDERRPRINT(FDEP_L1, FDEM_RAWI,
2849 	    (C, "cmd: %x %x %x %x %x %x %x %x %x %x\n", csb->csb_cmds[0],
2850 	    csb->csb_cmds[1], csb->csb_cmds[2], csb->csb_cmds[3],
2851 	    csb->csb_cmds[4], csb->csb_cmds[5], csb->csb_cmds[6],
2852 	    csb->csb_cmds[7], csb->csb_cmds[8], csb->csb_cmds[9]));
2853 	FDERRPRINT(FDEP_L1, FDEM_RAWI,
2854 	    (C, "nbytes: %x, opflags: %x, addr: %p, len: %x\n",
2855 	    csb->csb_ncmds, csb->csb_opflags, (void *)csb->csb_addr,
2856 	    csb->csb_len));
2857 
2858 
2859 	/*
2860 	 * Note that we ignore any error return s from fdexec.
2861 	 * This is the way the driver has been, and it may be
2862 	 * that the raw ioctl senders simply don't want to
2863 	 * see any errors returned in this fashion.
2864 	 */
2865 
2866 	if ((csb->csb_opflags & CSB_OFNORESULTS) ||
2867 	    (csb->csb_opflags & CSB_OFIMMEDIATE)) {
2868 		(void) fdexec(fdc, 0); /* don't sleep, don't check change */
2869 	} else {
2870 		(void) fdexec(fdc, FDXC_SLEEP | FDXC_CHECKCHG);
2871 	}
2872 
2873 
2874 	FDERRPRINT(FDEP_L1, FDEM_RAWI,
2875 	    (C, "rslt: %x %x %x %x %x %x %x %x %x %x\n", csb->csb_rslt[0],
2876 	    csb->csb_rslt[1], csb->csb_rslt[2], csb->csb_rslt[3],
2877 	    csb->csb_rslt[4], csb->csb_rslt[5], csb->csb_rslt[6],
2878 	    csb->csb_rslt[7], csb->csb_rslt[8], csb->csb_rslt[9]));
2879 
2880 	if ((fdr.fdr_cmd[0] & 0x0f) != FDRAW_FORMAT && fc &&
2881 	    flag == B_READ && err == 0) {
2882 		if (ddi_copyout(fa, fdr.fdr_addr, fc, mode)) {
2883 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2884 			(C, "fdrawioctl: can't copy read data\n"));
2885 
2886 			err = EFAULT;
2887 		}
2888 	}
2889 
2890 
2891 	if (fc) {
2892 		if (fdc->c_fdtype & FDCTYPE_DMA) {
2893 			ddi_dma_mem_free(&mem_handle);
2894 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2895 				(C, "fdrawioctl: free dma memory\n"));
2896 		} else {
2897 			kmem_free(fa, fc);
2898 		}
2899 	}
2900 
2901 
2902 	/* copy cmd results into fdr */
2903 	for (i = 0; (int)i <= (int)csb->csb_nrslts; i++)
2904 		fdr.fdr_result[i] = csb->csb_rslt[i];
2905 	fdr.fdr_nbytes = fdc->c_csb.csb_rlen; /* return resid */
2906 
2907 	switch (ddi_model_convert_from(mode)) {
2908 #ifdef _MULTI_DATAMODEL
2909 	case DDI_MODEL_ILP32:
2910 		bcopy(fdr.fdr_cmd, fdr32.fdr_cmd, sizeof (fdr32.fdr_cmd));
2911 		fdr32.fdr_cnum = fdr.fdr_cnum;
2912 		bcopy(fdr.fdr_result, fdr32.fdr_result,
2913 		    sizeof (fdr32.fdr_result));
2914 		fdr32.fdr_nbytes = fdr.fdr_nbytes;
2915 		fdr32.fdr_addr = (caddr32_t)(uintptr_t)fdr.fdr_addr;
2916 		if (ddi_copyout(&fdr32, (caddr_t)arg, sizeof (fdr32), mode)) {
2917 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2918 			(C, "fdrawioctl: can't copy results32\n"));
2919 			err = EFAULT;
2920 		}
2921 		break;
2922 #endif
2923 	case DDI_MODEL_NONE:
2924 	default:
2925 		if (ddi_copyout(&fdr, (caddr_t)arg, sizeof (fdr), mode)) {
2926 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2927 			(C, "fdrawioctl: can't copy results\n"));
2928 			err = EFAULT;
2929 		}
2930 		break;
2931 	}
2932 
2933 	fdretcsb(fdc);
2934 	mutex_exit(&fdc->c_lolock);
2935 	return (0);
2936 }
2937 
2938 /*
2939  * fdformat
2940  *	format a track
2941  * For PIO, builds a table of sector data values with 16 bytes
2942  * (sizeof fdc's fifo) of dummy on end.	 This is so than when fdc->c_len
2943  * goes to 0 and fd_intr sends a TC that all the real formatting will
2944  * have already been done.
2945  *
2946  *	- called with the low level lock held
2947  */
2948 static int
2949 fdformat(struct fdctlr *fdc, int unit, int cyl, int hd)
2950 {
2951 	struct fdcsb *csb;
2952 	struct fdunit *un;
2953 	struct fd_char *ch;
2954 	int	cmdresult;
2955 	uchar_t	*fmthdrs;
2956 	caddr_t fd;
2957 	int	i;
2958 	size_t	real_length;
2959 	ddi_device_acc_attr_t attr;
2960 	ddi_acc_handle_t mem_handle;
2961 
2962 	FDERRPRINT(FDEP_L1, FDEM_FORM,
2963 	    (C, "fdformat cyl %d, hd %d\n", cyl, hd));
2964 	fdgetcsb(fdc);
2965 
2966 	ASSERT(fdc->c_un->un_unit_no == unit);
2967 
2968 	csb = &fdc->c_csb;
2969 	un = fdc->c_un;
2970 	ch = un->un_chars;
2971 
2972 	/* setup common things in csb */
2973 	csb->csb_unit = (uchar_t)unit;
2974 
2975 	/*
2976 	 * The controller needs to do a seek before
2977 	 * each format to get to right cylinder.
2978 	 */
2979 	if (fdrecalseek(fdc, unit, cyl, FDXC_CHECKCHG)) {
2980 		fdretcsb(fdc);
2981 		return (EIO);
2982 	}
2983 
2984 	/*
2985 	 * now do the format itself
2986 	 */
2987 	csb->csb_nrslts = NRBRW;
2988 	csb->csb_opflags = CSB_OFXFEROPS | CSB_OFTIMEIT;
2989 
2990 	csb->csb_cmds[0] = FDRAW_FORMAT;
2991 	/* always or in MFM bit */
2992 	csb->csb_cmds[0] |= MFM;
2993 	csb->csb_cmds[1] = (hd << 2) | (unit & 0x03);
2994 	csb->csb_cmds[2] = ch->fdc_medium ? 3 : 2;
2995 	csb->csb_cmds[3] = ch->fdc_secptrack;
2996 	csb->csb_cmds[4] = GPLF;
2997 	csb->csb_cmds[5] = FDATA;
2998 	csb->csb_ncmds = 6;
2999 	csb->csb_maxretry = rwretry;
3000 	csb->csb_retrys = 0;
3001 
3002 	/*
3003 	 * NOTE: have to add size of fifo also - for dummy format action
3004 	 * if PIO is being used.
3005 	 */
3006 
3007 
3008 	if (fdc->c_fdtype & FDCTYPE_DMA) {
3009 
3010 		csb->csb_len = (uint_t)4 * ch->fdc_secptrack;
3011 
3012 		attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
3013 		attr.devacc_attr_endian_flags  = DDI_STRUCTURE_BE_ACC;
3014 		attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
3015 
3016 		mutex_enter(&fdc->c_hilock);
3017 
3018 		cmdresult = ddi_dma_mem_alloc(fdc->c_dmahandle, csb->csb_len,
3019 			&attr, DDI_DMA_STREAMING,
3020 			DDI_DMA_DONTWAIT, 0, &fd, &real_length,
3021 			&mem_handle);
3022 
3023 		if (cmdresult != DDI_SUCCESS) {
3024 			mutex_exit(&fdc->c_hilock);
3025 			return (cmdresult);
3026 		}
3027 
3028 		fdc->c_csb.csb_read = CSB_WRITE;
3029 		if (fdstart_dma(fdc, fd,  csb->csb_len) != 0) {
3030 			ddi_dma_mem_free(&mem_handle);
3031 			mutex_exit(&fdc->c_hilock);
3032 			return (-1);
3033 		}
3034 		mutex_exit(&fdc->c_hilock);
3035 
3036 
3037 	} else {
3038 		csb->csb_len = (uint_t)4 * ch->fdc_secptrack + 16;
3039 		fd = kmem_zalloc(csb->csb_len, KM_SLEEP);
3040 		fmthdrs = (uchar_t *)fd;
3041 	}
3042 
3043 	csb->csb_addr = (caddr_t)fd;
3044 
3045 	for (i = 1; i <= ch->fdc_secptrack; i++) {
3046 		*fd++ = (uchar_t)cyl;		/* cylinder */
3047 		*fd++ = (uchar_t)hd;		/* head */
3048 		*fd++ = (uchar_t)i;	/* sector number */
3049 		*fd++ = ch->fdc_medium ? 3 : 2; /* sec_size code */
3050 	}
3051 
3052 	if ((cmdresult = fdexec(fdc, FDXC_SLEEP | FDXC_CHECKCHG)) == 0) {
3053 		if (csb->csb_cmdstat)
3054 			cmdresult = EIO;	/* XXX TBD NYD for now */
3055 	}
3056 
3057 	if (fdc->c_fdtype & FDCTYPE_DMA) {
3058 		ddi_dma_mem_free(&mem_handle);
3059 	} else {
3060 		kmem_free((caddr_t)fmthdrs, csb->csb_len);
3061 	}
3062 
3063 	fdretcsb(fdc);
3064 
3065 	return (cmdresult);
3066 }
3067 
3068 /*
3069  * fdstart
3070  *	called from fd_strategy() or from fdXXXX() to setup and
3071  *	start operations of read or write only (using buf structs).
3072  *	Because the chip doesn't handle crossing cylinder boundaries on
3073  *	the fly, this takes care of those boundary conditions.	Note that
3074  *	it sleeps until the operation is done *within fdstart* - so that
3075  *	when fdstart returns, the operation is already done.
3076  *
3077  *	- called with the low level lock held
3078  *
3079  */
3080 
3081 static int slavio_index_pulse_work_around = 0;
3082 
3083 static void
3084 fdstart(struct fdctlr *fdc)
3085 {
3086 	struct buf *bp;
3087 	struct fdcsb *csb;
3088 	struct fdunit *un;
3089 	struct fd_char *ch;
3090 	struct dk_map32 *dkm;
3091 	uint_t	part;		/* partition number for the transfer */
3092 	uint_t	start_part;	/* starting block of the partition */
3093 	uint_t	last_part;	/* last block of the partition */
3094 	uint_t	blk;		/* starting block of transfer on diskette */
3095 	uint_t	sect;		/* starting block's offset into track */
3096 	uint_t	cyl;		/* starting cylinder of the transfer */
3097 	uint_t	bincyl;		/* starting blocks's offset into cylinder */
3098 	uint_t	secpcyl;	/* number of sectors per cylinder */
3099 	uint_t	phys_blkno;	/* no. of blocks on the diskette */
3100 	uint_t	head;		/* one of two diskette heads */
3101 	uint_t	unit;
3102 	uint_t	len, tlen;
3103 	caddr_t addr;
3104 	caddr_t temp_addr;
3105 	uint_t	partial_read = 0;
3106 	int sb_temp_buf_used = 0;
3107 
3108 	bp = fdc->c_actf;
3109 
3110 	while (bp != NULL) {
3111 
3112 		fdc->c_actf = bp->av_forw;
3113 		fdc->c_current = bp;
3114 
3115 		/*
3116 		 * Initialize the buf structure.  The residual count is
3117 		 * initially the number of bytes to be read or written
3118 		 */
3119 		bp->b_flags &= ~B_ERROR;
3120 		bp->b_error = 0;
3121 		bp->b_resid = bp->b_bcount;
3122 		bp_mapin(bp);			/* map in buffers */
3123 
3124 		addr = bp->b_un.b_addr;		/* assign buffer address */
3125 
3126 		/*
3127 		 * Find the unit and partition numbers.
3128 		 */
3129 		unit = fdc->c_un->un_unit_no;
3130 		un = fdc->c_un;
3131 		ch = un->un_chars;
3132 		part = FDPARTITION(bp->b_edev);
3133 		dkm = &un->un_label.dkl_map[part];
3134 
3135 		if (un->un_chars->fdc_medium) {
3136 			phys_blkno = bp->b_blkno >> 1;
3137 		} else {
3138 			phys_blkno = bp->b_blkno;
3139 		}
3140 
3141 		if (un->un_iostat) {
3142 			kstat_waitq_to_runq(KIOSP);
3143 		}
3144 
3145 		FDERRPRINT(FDEP_L1, FDEM_STRT,
3146 		    (C, "fdstart: bp=0x%p blkno=0x%x bcount=0x%x\n",
3147 		    (void *)bp, (int)bp->b_blkno, (int)bp->b_bcount));
3148 
3149 		/*
3150 		 * Get the csb and initialize the values that are the same
3151 		 * for DMA and PIO.
3152 		 */
3153 		fdgetcsb(fdc);		/* get csb (maybe wait for it) */
3154 		csb = &fdc->c_csb;
3155 		csb->csb_unit = unit;		/* floppy unit number */
3156 
3157 
3158 		/*
3159 		 * bugID:4133425 : If the controller is SLAVIO, and
3160 		 * the read does not reach end of track, then modify
3161 		 * the tlen to read until the end of track to a temp
3162 		 * buffer and disable MT. After the read is over,
3163 		 * copy the useful portion of the data to 'addr'.
3164 		 * Enable this feature only when
3165 		 * slavio_index_pulse_work_aound variable is
3166 		 * set in /etc/system.
3167 		 */
3168 
3169 
3170 		if (bp->b_flags & B_READ) {
3171 			if (((fdc->c_fdtype & FDCTYPE_SLAVIO) &&
3172 				slavio_index_pulse_work_around) ||
3173 					(fdc->c_fdtype & FDCTYPE_TCBUG))
3174 				csb->csb_cmds[0] = SK | FDRAW_RDCMD | MFM;
3175 			else
3176 				csb->csb_cmds[0] = MT | SK | FDRAW_RDCMD | MFM;
3177 		} else {
3178 			if (fdc->c_fdtype & FDCTYPE_TCBUG)
3179 				csb->csb_cmds[0] = FDRAW_WRCMD | MFM;
3180 			else
3181 				csb->csb_cmds[0] = MT | FDRAW_WRCMD | MFM;
3182 		}
3183 
3184 
3185 		if (bp->b_flags & B_READ)
3186 			fdc->c_csb.csb_read = CSB_READ;
3187 		else
3188 			fdc->c_csb.csb_read = CSB_WRITE;
3189 
3190 
3191 		csb->csb_cmds[5] = ch->fdc_medium ? 3 : 2; /* sector size  */
3192 		csb->csb_cmds[6] = ch->fdc_secptrack; /* EOT-# of sectors/trk */
3193 		csb->csb_cmds[7] = GPLN;	/* GPL - gap 3 size code */
3194 		csb->csb_cmds[8] = SSSDTL;	/* DTL - be 0xFF if N != 0 */
3195 
3196 		csb->csb_ncmds = NCBRW;		/* number of command bytes */
3197 		csb->csb_nrslts = NRBRW;	/* number of result bytes */
3198 
3199 
3200 		/*
3201 		 * opflags for interrupt handler, et.al.
3202 		 */
3203 		csb->csb_opflags = CSB_OFXFEROPS | CSB_OFTIMEIT;
3204 
3205 
3206 		/*
3207 		 * Make sure the transfer does not go off the end
3208 		 * of the partition.  Limit the actual amount transferred
3209 		 * to fit the partition.
3210 		 */
3211 
3212 		blk = phys_blkno;
3213 		start_part = (dkm->dkl_cylno * ch->fdc_secptrack
3214 				* ch->fdc_nhead);
3215 		blk = blk + start_part;
3216 		last_part = start_part + dkm->dkl_nblk;
3217 
3218 		if ((blk + (bp->b_bcount / ch->fdc_sec_size)) > last_part)
3219 			len = (last_part - blk) * ch->fdc_sec_size;
3220 		else
3221 			len = (uint_t)bp->b_bcount;
3222 
3223 		/*
3224 		 * now we have the real start blk,
3225 		 * addr and len for xfer op
3226 		 * sectors per cylinder
3227 		 */
3228 		secpcyl = ch->fdc_nhead * ch->fdc_secptrack;
3229 
3230 		/*
3231 		 * The controller can transfer up to a cylinder at a time.
3232 		 * Early revs of the 82077 have a bug that causes the chip to
3233 		 * fail to respond to the Terminal Count signal.  Due to this
3234 		 * bug, controllers with type FDCTYPE_TCBUG, only transfer up
3235 		 * to a track at a time.
3236 		 * See earlier comment for bugID:4133425 for index pulse
3237 		 * work around.
3238 		 */
3239 
3240 		while (len != 0) {
3241 
3242 			cyl = blk / secpcyl;	/* cylinder of transfer */
3243 			bincyl = blk % secpcyl;	/* blk within cylinder */
3244 			head = bincyl / ch->fdc_secptrack;
3245 			sect = (bincyl % ch->fdc_secptrack) + 1;
3246 						/* sect w/in track */
3247 
3248 			/*
3249 			 * If the desired block and length will go beyond the
3250 			 * cylinder end, limit it to the cylinder end.
3251 			 */
3252 
3253 			if ((fdc->c_fdtype & FDCTYPE_SLAVIO) &&
3254 				slavio_index_pulse_work_around &&
3255 				(fdc->c_csb.csb_read == CSB_READ)) {
3256 
3257 				tlen = (ch->fdc_secptrack - sect + 1) *
3258 							ch->fdc_sec_size;
3259 				if (len < tlen) {
3260 					partial_read = 1;
3261 					temp_addr = (caddr_t)kmem_alloc(tlen,
3262 								KM_SLEEP);
3263 				}
3264 
3265 			} else if (fdc->c_fdtype & FDCTYPE_TCBUG) {
3266 				tlen = len;
3267 				if (len > ((ch->fdc_secptrack - sect + 1) *
3268 							ch->fdc_sec_size))
3269 					tlen = (ch->fdc_secptrack - sect + 1)
3270 							* ch->fdc_sec_size;
3271 			} else {
3272 				if (len > ((secpcyl - bincyl)
3273 							* ch->fdc_sec_size))
3274 					tlen = (secpcyl - bincyl)
3275 							* ch->fdc_sec_size;
3276 
3277 				else
3278 					tlen = len;
3279 			}
3280 			if (fdc->c_fdtype & FDCTYPE_SB) {
3281 				/*
3282 				 * To avoid underrun errors during IFB activity.
3283 				 */
3284 				if (tlen > max_fd_dma_len)
3285 					tlen = max_fd_dma_len;
3286 			}
3287 
3288 			FDERRPRINT(FDEP_L1, FDEM_STRT,
3289 			    (C, "	blk 0x%x, addr 0x%p, len 0x%x\n",
3290 			    blk, (void *)addr, len));
3291 			FDERRPRINT(FDEP_L1, FDEM_STRT,
3292 			    (C, "cyl:%x, head:%x, sec:%x\n",
3293 			    cyl, head, sect));
3294 
3295 			FDERRPRINT(FDEP_L1, FDEM_STRT,
3296 			    (C, "	resid 0x%lx, tlen %d\n",
3297 			    bp->b_resid, tlen));
3298 
3299 			/*
3300 			 * Finish programming the command
3301 			 */
3302 			csb->csb_cmds[1] = (head << 2) | unit;
3303 			if (fdc->c_fdtype & FDCTYPE_SB)
3304 				csb->csb_cmds[1] |= IPS;
3305 
3306 			csb->csb_cmds[2] = cyl;	/* C - cylinder address */
3307 			csb->csb_cmds[3] = head;	/* H - head number */
3308 			csb->csb_cmds[4] = sect;	/* R - sector number */
3309 			if (fdc->c_fdtype & FDCTYPE_TCBUG)
3310 				csb->csb_cmds[6] = sect +
3311 						(tlen / ch->fdc_sec_size) - 1;
3312 
3313 			csb->csb_len = tlen;
3314 			if (partial_read)
3315 				csb->csb_addr = temp_addr;
3316 			else
3317 				csb->csb_addr = addr;
3318 
3319 			/* retry this many times max */
3320 			csb->csb_maxretry = rwretry;
3321 			csb->csb_retrys = 0;
3322 
3323 			/* If platform supports DMA, set up DMA resources */
3324 			if (fdc->c_fdtype & FDCTYPE_DMA) {
3325 				if ((fdc->c_fdtype & FDCTYPE_SB) &&
3326 				    (((uint32_t)(uintptr_t)addr & 0xFFFF0000) !=
3327 				    (((uint32_t)(uintptr_t)addr + tlen) &
3328 				    0xFFFF0000))) {
3329 					csb->csb_addr = fdc->dma_buf;
3330 					sb_temp_buf_used = 1;
3331 					if (csb->csb_read != CSB_READ) {
3332 						bcopy(addr, fdc->dma_buf, tlen);
3333 				}
3334 			}
3335 				mutex_enter(&fdc->c_hilock);
3336 
3337 				if (fdstart_dma(fdc, csb->csb_addr,
3338 								tlen) != 0) {
3339 
3340 					bp->b_flags |= B_ERROR;
3341 					bp->b_error = EAGAIN;
3342 
3343 					mutex_exit(&fdc->c_hilock);
3344 					FDERRPRINT(FDEP_L1, FDEM_STRT,
3345 					(C, "fdstart: no dma resources\n"));
3346 
3347 					break;
3348 				}
3349 				mutex_exit(&fdc->c_hilock);
3350 
3351 			}
3352 
3353 			bp->b_error = fdexec(fdc, FDXC_SLEEP|FDXC_CHECKCHG);
3354 			if (bp->b_error != 0) {
3355 				/*
3356 				 * error in fdexec
3357 				 */
3358 				FDERRPRINT(FDEP_L1, FDEM_STRT, (C,
3359 				    "fdstart: bad exec of bp: 0x%p, err %d\n",
3360 				    (void *)bp, bp->b_error));
3361 
3362 				bp->b_flags |= B_ERROR;
3363 				if (partial_read) {
3364 					partial_read = 0;
3365 					kmem_free(temp_addr, tlen);
3366 				}
3367 				break;
3368 			}
3369 
3370 			/*
3371 			 * If it was a partial read, copy the useful
3372 			 * portion of data to 'addr'.
3373 			 */
3374 			if (partial_read) {
3375 				partial_read = 0;
3376 				bcopy(temp_addr, addr, len);
3377 				kmem_free(temp_addr, tlen);
3378 				tlen = len;
3379 			}
3380 			if ((fdc->c_fdtype & FDCTYPE_SB) &&
3381 					(csb->csb_read == CSB_READ)) {
3382 				if (sb_temp_buf_used) {
3383 					bcopy(fdc->dma_buf, addr, tlen);
3384 					sb_temp_buf_used = 0;
3385 				}
3386 			}
3387 
3388 			blk += tlen / ch->fdc_sec_size;
3389 			len -= tlen;
3390 			addr += tlen;
3391 			bp->b_resid -= tlen;
3392 
3393 		}
3394 
3395 		FDERRPRINT(FDEP_L1, FDEM_STRT,
3396 		    (C, "fdstart done: b_resid %lu, b_count %lu, csb_rlen %d\n",
3397 		    bp->b_resid, bp->b_bcount, fdc->c_csb.csb_rlen));
3398 
3399 		fdc->c_current = 0;
3400 		fdretcsb(fdc);
3401 		if (un->un_iostat) {
3402 			if (bp->b_flags & B_READ) {
3403 				KIOSP->reads++;
3404 				KIOSP->nread +=
3405 					(bp->b_bcount - bp->b_resid);
3406 			} else {
3407 				KIOSP->writes++;
3408 				KIOSP->nwritten += (bp->b_bcount - bp->b_resid);
3409 			}
3410 			kstat_runq_exit(KIOSP);
3411 		}
3412 		biodone(bp);
3413 
3414 		/*
3415 		 * Look at the next buffer
3416 		 */
3417 		bp = fdc->c_actf;
3418 
3419 	}
3420 }
3421 
3422 /*
3423  * Set up DMA resources
3424  * The DMA handle was initialized in fd_attach()
3425  * Assumes the handle has already been allocated by fd_attach()
3426  */
3427 static int
3428 fdstart_dma(struct fdctlr *fdc, caddr_t addr, uint_t len)
3429 {
3430 	int		flags;		/* flags for setting up resources */
3431 	int		res;
3432 
3433 	FDERRPRINT(FDEP_L1, FDEM_SDMA, (C, "fdstart_dma: start\n"));
3434 
3435 	if (fdc->c_csb.csb_read == CSB_READ) {
3436 		flags = DDI_DMA_READ;
3437 	} else {
3438 		flags = DDI_DMA_WRITE;
3439 	}
3440 
3441 
3442 	/* allow partial mapping to maximize the portability of the driver */
3443 	flags = flags | DDI_DMA_PARTIAL;
3444 
3445 	FDERRPRINT(FDEP_L1, FDEM_SDMA, (C, "fdstart_dma: amt. asked for %d\n",
3446 					len));
3447 
3448 	/*
3449 	 * Zero out the current cookie.  This is done to ensure that
3450 	 * the previous transfers cookie information can in no way be
3451 	 * used.
3452 	 */
3453 	bzero((char *)&fdc->c_csb.csb_dmacookie,
3454 			sizeof (fdc->c_csb.csb_dmacookie));
3455 	fdc->c_csb.csb_nwin = 0;
3456 	fdc->c_csb.csb_windex = 0;
3457 	fdc->c_csb.csb_ccount = 0;
3458 
3459 	res = ddi_dma_addr_bind_handle(fdc->c_dmahandle, NULL, addr, len,
3460 	flags, DDI_DMA_DONTWAIT, 0,  &fdc->c_csb.csb_dmacookie,
3461 	&fdc->c_csb.csb_ccount);
3462 
3463 	switch (res) {
3464 		case DDI_DMA_MAPPED:
3465 			/*
3466 			 * There is one window. csb_windex is the index
3467 			 * into the array of windows. If there are n
3468 			 * windows then, (0 <= windex <= n-1).  csb_windex
3469 			 * represents the index of the next window
3470 			 * to be processed.
3471 			 */
3472 			fdc->c_csb.csb_nwin = 1;
3473 			fdc->c_csb.csb_windex = 1;
3474 
3475 
3476 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3477 				(C, "fdstart_dma: DDI_DMA_MAPPED\n"));
3478 
3479 			break;
3480 		case DDI_DMA_PARTIAL_MAP:
3481 
3482 			/*
3483 			 * obtain the number of DMA windows
3484 			 */
3485 			if (ddi_dma_numwin(fdc->c_dmahandle,
3486 				&fdc->c_csb.csb_nwin) != DDI_SUCCESS) {
3487 				return (-1);
3488 			}
3489 
3490 
3491 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3492 			(C, "fdstart_dma: partially mapped %d windows\n",
3493 			fdc->c_csb.csb_nwin));
3494 
3495 			/*
3496 			 * The DMA window currently in use is window number
3497 			 * one.
3498 			 */
3499 			fdc->c_csb.csb_windex = 1;
3500 
3501 			break;
3502 		case DDI_DMA_NORESOURCES:
3503 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3504 				(C, "fdstart_dma: no resources\n"));
3505 			return (-1);
3506 		case DDI_DMA_NOMAPPING:
3507 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3508 				(C, "fdstart_dma: no mapping\n"));
3509 			return (-1);
3510 		case DDI_DMA_TOOBIG:
3511 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3512 				(C, "fdstart_dma: too big\n"));
3513 			return (-1);
3514 
3515 		case DDI_DMA_INUSE:
3516 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3517 				(C, "fdstart_dma: dma inuse\n"));
3518 			return (-1);
3519 		default:
3520 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3521 				(C, "fdstart_dma: result is 0x%x\n", res));
3522 			return (-1);
3523 
3524 	};
3525 
3526 	FDERRPRINT(FDEP_L1, FDEM_SDMA,
3527 		(C, "fdstart_dma: bound the handle\n"));
3528 
3529 	ASSERT(fdc->c_csb.csb_dmacookie.dmac_size);
3530 
3531 	FDERRPRINT(FDEP_L1, FDEM_SDMA, (C, "fdstart_dma: done\n"));
3532 	return (0);
3533 }
3534 
3535 
3536 /*
3537  * fd_unbind_handle: unbind a dma handle if one exists
3538  *		return EIO if unbind failes
3539  */
3540 static int
3541 fd_unbind_handle(struct fdctlr *fdc)
3542 {
3543 	if ((fdc->c_fdtype & FDCTYPE_DMA) &&
3544 		((fdc->c_csb.csb_read == CSB_READ) ||
3545 		    (fdc->c_csb.csb_read == CSB_WRITE))) {
3546 		mutex_enter(&fdc->c_hilock);
3547 
3548 		if (fdc->c_fdtype & FDCTYPE_SB) {
3549 			if (fdc->sb_dma_lock) {
3550 				release_sb_dma(fdc);
3551 			}
3552 		}
3553 
3554 		/*
3555 		 * If the byte count isn't zero, then the DMA engine is
3556 		 * still doing a transfer.  If the byte count is nonzero,
3557 		 * reset the DMA engine to cause it to drain.
3558 		 */
3559 
3560 		if (get_data_count_register(fdc) != 0) {
3561 			    FDERRPRINT(FDEP_L1, FDEM_EXEC,
3562 				(C, "unbind & byte count isn't zero\n"));
3563 
3564 				reset_dma_controller(fdc);
3565 				set_dma_control_register(fdc, DCSR_INIT_BITS);
3566 		}
3567 
3568 		if (ddi_dma_unbind_handle(fdc->c_dmahandle) != DDI_SUCCESS) {
3569 			FDERRPRINT(FDEP_L1, FDEM_EXEC,
3570 				(C, "problem unbinding the handle\n"));
3571 			mutex_exit(&fdc->c_hilock);
3572 			return (EIO);
3573 		}
3574 		mutex_exit(&fdc->c_hilock);
3575 	}
3576 	return (0);
3577 }
3578 
3579 /*
3580  * fdexec
3581  *	all commands go through here.  Assumes the command block
3582  *	fdctlr.c_csb is filled in.  The bytes are sent to the
3583  *	controller and then we do whatever else the csb says -
3584  *	like wait for immediate results, etc.
3585  *
3586  *	All waiting for operations done is in here - to allow retrys
3587  *	and checking for disk changed - so we don't have to worry
3588  *	about sleeping at interrupt level.
3589  *
3590  * RETURNS: 0 if all ok,
3591  *	ENXIO - diskette not in drive
3592  *	EBUSY - if chip is locked or busy
3593  *	EIO - for timeout during sending cmds to chip
3594  *
3595  * to sleep: set FDXC_SLEEP, to check for disk
3596  * changed: set FDXC_CHECKCHG
3597  *
3598  *	- called with the lock held
3599  */
3600 static int
3601 fdexec(struct fdctlr *fdc, int flags)
3602 {
3603 	struct fdcsb *csb;
3604 	int	i;
3605 	int	to, unit;
3606 	uchar_t	tmp;
3607 	caddr_t a = (caddr_t)fdc;
3608 
3609 	FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: flags:%x\n", flags));
3610 
3611 	ASSERT(mutex_owned(&fdc->c_lolock));
3612 
3613 	csb = &fdc->c_csb;
3614 	unit = csb->csb_unit;
3615 
3616 
3617 	ASSERT(unit == fdc->c_un->un_unit_no);
3618 
3619 retry:
3620 	FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: cmd is %s\n",
3621 				fdcmds[csb->csb_cmds[0] & 0x1f].cmdname));
3622 	FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: transfer rate = %d\n",
3623 	    fdc->c_un->un_chars->fdc_transfer_rate));
3624 	FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: sec size = %d\n",
3625 	    fdc->c_un->un_chars->fdc_sec_size));
3626 	FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: nblocks (512) = %d\n",
3627 	    fdc->c_un->un_label.dkl_map[2].dkl_nblk));
3628 
3629 	if ((fdc->c_fdtype & FDCTYPE_CTRLMASK) == FDCTYPE_82077) {
3630 		fdexec_turn_on_motor(fdc, flags, unit);
3631 	}
3632 
3633 
3634 	fdselect(fdc, unit, 1);	/* select drive */
3635 
3636 	/*
3637 	 * select data rate for this unit/command
3638 	 */
3639 	switch (fdc->c_un->un_chars->fdc_transfer_rate) {
3640 	case 500:
3641 		Dsr(fdc, 0);
3642 		break;
3643 	case 300:
3644 		Dsr(fdc, 1);
3645 		break;
3646 	case 250:
3647 		Dsr(fdc, 2);
3648 		break;
3649 	}
3650 	drv_usecwait(2);
3651 
3652 
3653 	/*
3654 	 * If checking for changed is enabled (i.e., not seeking in checkdisk),
3655 	 * we sample the DSKCHG line to see if the diskette has wandered away.
3656 	 */
3657 	if ((flags & FDXC_CHECKCHG) && fdsense_chng(fdc, unit)) {
3658 		FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "diskette changed\n"));
3659 		fdc->c_un->un_flags |= FDUNIT_CHANGED;
3660 
3661 		if (fdcheckdisk(fdc, unit)) {
3662 
3663 			(void) fd_unbind_handle(fdc);
3664 			return (ENXIO);
3665 
3666 		}
3667 	}
3668 
3669 	/*
3670 	 * gather some statistics
3671 	 */
3672 	switch (csb->csb_cmds[0] & 0x1f) {
3673 	case FDRAW_RDCMD:
3674 		fdc->fdstats.rd++;
3675 		break;
3676 	case FDRAW_WRCMD:
3677 		fdc->fdstats.wr++;
3678 		break;
3679 	case FDRAW_REZERO:
3680 		fdc->fdstats.recal++;
3681 		break;
3682 	case FDRAW_FORMAT:
3683 		fdc->fdstats.form++;
3684 		break;
3685 	default:
3686 		fdc->fdstats.other++;
3687 		break;
3688 	}
3689 
3690 	/*
3691 	 * Always set the opmode *prior* to poking the chip.
3692 	 * This way we don't have to do any locking at high level.
3693 	 */
3694 	csb->csb_raddr = 0;
3695 	csb->csb_rlen = 0;
3696 	if (csb->csb_opflags & CSB_OFSEEKOPS) {
3697 		csb->csb_opmode = 2;
3698 	} else if (csb->csb_opflags & CSB_OFIMMEDIATE) {
3699 		csb->csb_opmode = 0;
3700 	} else {
3701 		csb->csb_opmode = 1;	/* normal data xfer commands */
3702 		csb->csb_raddr = csb->csb_addr;
3703 		csb->csb_rlen = csb->csb_len;
3704 	}
3705 
3706 	bzero((caddr_t)csb->csb_rslt, 10);
3707 	csb->csb_status = 0;
3708 	csb->csb_cmdstat = 0;
3709 
3710 
3711 	/*
3712 	 * Program the DMA engine with the length and address of the transfer
3713 	 * (DMA is only used on a read or a write)
3714 	 */
3715 	if ((fdc->c_fdtype & FDCTYPE_DMA) &&
3716 			((fdc->c_csb.csb_read == CSB_READ) ||
3717 			    (fdc->c_csb.csb_read == CSB_WRITE)))  {
3718 		mutex_enter(&fdc->c_hilock);
3719 
3720 		/* Reset the dcsr to clear it of all errors */
3721 
3722 		reset_dma_controller(fdc);
3723 
3724 		FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "cookie addr 0x%p\n",
3725 		    (void *)fdc->c_csb.csb_dmacookie.dmac_laddress));
3726 
3727 		FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "cookie length %ld\n",
3728 				fdc->c_csb.csb_dmacookie.dmac_size));
3729 		ASSERT(fdc->c_csb.csb_dmacookie.dmac_size);
3730 
3731 		set_data_count_register(fdc,
3732 			fdc->c_csb.csb_dmacookie.dmac_size);
3733 		set_data_address_register(fdc,
3734 			fdc->c_csb.csb_dmacookie.dmac_laddress);
3735 
3736 		/* Program the DCSR */
3737 
3738 		if (fdc->c_csb.csb_read == CSB_READ)
3739 			set_dma_mode(fdc, CSB_READ);
3740 		else
3741 			set_dma_mode(fdc, CSB_WRITE);
3742 		mutex_exit(&fdc->c_hilock);
3743 	}
3744 
3745 	/*
3746 	 * I saw this (chip unexpectedly busy) happen when i shoved the
3747 	 * floppy into the drive while
3748 	 * running a dd if= /dev/rfd0c.	so it *is* possible for this to happen.
3749 	 * we need to do a ctlr reset ...
3750 	 */
3751 
3752 	if (Msr(fdc) & CB) {
3753 		/* tried to give command to chip when it is busy! */
3754 		FDERRPRINT(FDEP_L3, FDEM_EXEC,
3755 		    (C, "fdc: unexpectedly busy-stat 0x%x\n", Msr(fdc)));
3756 		csb->csb_cmdstat = 1;	/* XXX TBD ERRS NYD for now */
3757 
3758 		(void) fd_unbind_handle(fdc);
3759 		return (EBUSY);
3760 	}
3761 
3762 	/* Give command to the controller */
3763 	for (i = 0; i < (int)csb->csb_ncmds; i++) {
3764 
3765 		/* Test the readiness of the controller to receive the cmd */
3766 		for (to = FD_CRETRY; to; to--) {
3767 			if ((Msr(fdc) & (DIO|RQM)) == RQM)
3768 				break;
3769 		}
3770 		if (to == 0) {
3771 			FDERRPRINT(FDEP_L2, FDEM_EXEC,
3772 			    (C, "fdc: no RQM - stat 0x%x\n", Msr(fdc)));
3773 			csb->csb_cmdstat = 1;
3774 
3775 			(void) fd_unbind_handle(fdc);
3776 			return (EIO);
3777 		}
3778 
3779 		Set_Fifo(fdc, csb->csb_cmds[i]);
3780 
3781 		FDERRPRINT(FDEP_L1, FDEM_EXEC,
3782 		    (C, "fdexec: sent 0x%x, Msr 0x%x\n", csb->csb_cmds[i],
3783 		    Msr(fdc)));
3784 
3785 	}
3786 
3787 
3788 	/*
3789 	 * Start watchdog timer on data transfer type commands - required
3790 	 * in case a diskette is not present or is unformatted
3791 	 */
3792 	if (csb->csb_opflags & CSB_OFTIMEIT) {
3793 		fdc->c_timeid = timeout(fdwatch, a,
3794 		    tosec * drv_usectohz(1000000));
3795 	}
3796 
3797 	FDERRPRINT(FDEP_L1, FDEM_EXEC,
3798 	    (C, "fdexec: cmd sent, Msr 0x%x\n", Msr(fdc)));
3799 
3800 	/* If the operation has no results - then just return */
3801 	if (csb->csb_opflags & CSB_OFNORESULTS) {
3802 		if (fdc->c_fdtype & FDCTYPE_82077) {
3803 			if (fdc->c_mtimeid == 0) {
3804 				fdc->c_mtimeid = timeout(fdmotoff, a,
3805 					Motoff_delay);
3806 			}
3807 		}
3808 		FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: O K ..\n"));
3809 
3810 		/*
3811 		 * Make sure the last byte is received well by the
3812 		 * controller. On faster CPU, it may still be busy
3813 		 * by the time another command comes here.
3814 		 */
3815 		for (to = FD_CRETRY; to; to--) {
3816 			if ((Msr(fdc) & (DIO|RQM)) == RQM)
3817 				break;
3818 			}
3819 		if (to == 0) {
3820 			csb->csb_cmdstat = 1;
3821 			return (EIO);
3822 		}
3823 
3824 		/*
3825 		 * An operation that has no results isn't doing DMA so,
3826 		 * there is no reason to try to unbind a handle
3827 		 */
3828 		return (0);
3829 	}
3830 
3831 	/*
3832 	 * If this operation has no interrupt AND an immediate result
3833 	 * then we just busy wait for the results and stuff them into
3834 	 * the csb
3835 	 */
3836 	if (csb->csb_opflags & CSB_OFIMMEDIATE) {
3837 		to = FD_RRETRY;
3838 		csb->csb_nrslts = 0;
3839 		/*
3840 		 * Wait while this command is still going on.
3841 		 */
3842 		while ((tmp = Msr(fdc)) & CB) {
3843 			/*
3844 			 * If RQM + DIO, then a result byte is at hand.
3845 			 */
3846 			if ((tmp & (RQM|DIO|CB)) == (RQM|DIO|CB)) {
3847 				csb->csb_rslt[csb->csb_nrslts++] =
3848 								Fifo(fdc);
3849 				/*
3850 				 * FDERRPRINT(FDEP_L4, FDEM_EXEC,
3851 				 *    (C, "fdexec: got result 0x%x\n",
3852 				 *    csb->csb_nrslts));
3853 				 */
3854 			} else if (--to == 0) {
3855 				FDERRPRINT(FDEP_L4, FDEM_EXEC,
3856 				    (C, "fdexec: timeout, Msr%x, nr%x\n",
3857 				    Msr(fdc), csb->csb_nrslts));
3858 
3859 				csb->csb_status = 2;
3860 				if (fdc->c_fdtype & FDCTYPE_82077) {
3861 					if (fdc->c_mtimeid == 0) {
3862 						fdc->c_mtimeid = timeout(
3863 						fdmotoff, a, Motoff_delay);
3864 					}
3865 				}
3866 				/*
3867 				 * There is no DMA happening.  No need to
3868 				 * try freeing a handle.
3869 				 */
3870 
3871 				return (EIO);
3872 			}
3873 		}
3874 	}
3875 
3876 	/*
3877 	 * If told to sleep here, well then sleep!
3878 	 */
3879 
3880 	if (flags & FDXC_SLEEP) {
3881 		fdc->c_flags |= FDCFLG_WAITING;
3882 		while (fdc->c_flags & FDCFLG_WAITING) {
3883 			cv_wait(&fdc->c_iocv, &fdc->c_lolock);
3884 		}
3885 	}
3886 
3887 	/*
3888 	 * kludge for end-of-cylinder error which must be ignored!!!
3889 	 */
3890 
3891 	if ((fdc->c_fdtype & FDCTYPE_TCBUG) &&
3892 	    ((csb->csb_rslt[0] & IC_SR0) == 0x40) &&
3893 	    (csb->csb_rslt[1] & EN_SR1))
3894 		csb->csb_rslt[0] &= ~IC_SR0;
3895 
3896 	/*
3897 	 * See if there was an error detected, if so, fdrecover()
3898 	 * will check it out and say what to do.
3899 	 *
3900 	 * Don't do this, though, if this was the Sense Drive Status
3901 	 * or the Dump Registers command.
3902 	 */
3903 	if (((csb->csb_rslt[0] & IC_SR0) || (fdc->c_csb.csb_dcsr_rslt) ||
3904 		(csb->csb_status)) &&
3905 		((csb->csb_cmds[0] != FDRAW_SENSE_DRV) &&
3906 		(csb->csb_cmds[0] != DUMPREG))) {
3907 		/* if it can restarted OK, then do so, else return error */
3908 		if (fdrecover(fdc) != 0) {
3909 			if (fdc->c_fdtype & FDCTYPE_82077) {
3910 				if (fdc->c_mtimeid == 0) {
3911 					fdc->c_mtimeid = timeout(fdmotoff,
3912 						a, Motoff_delay);
3913 				}
3914 			}
3915 
3916 			/*
3917 			 * If this was a dma transfer, unbind the handle so
3918 			 * that other transfers may use it.
3919 			 */
3920 
3921 			(void) fd_unbind_handle(fdc);
3922 			return (EIO);
3923 		} else {
3924 			/* ASSUMES that cmd is still intact in csb */
3925 			goto retry;
3926 		}
3927 	}
3928 
3929 	/* things went ok */
3930 	if (fdc->c_fdtype & FDCTYPE_82077) {
3931 		if (fdc->c_mtimeid == 0) {
3932 			fdc->c_mtimeid = timeout(fdmotoff, a, Motoff_delay);
3933 		}
3934 	}
3935 	FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: O K ..........\n"));
3936 
3937 	if (fd_unbind_handle(fdc))
3938 		return (EIO);
3939 
3940 	return (0);
3941 }
3942 
3943 /*
3944  * Turn on the drive's motor
3945  *
3946  *	- called with the low level lock held
3947  */
3948 static void
3949 fdexec_turn_on_motor(struct fdctlr *fdc, int flags,  uint_t unit)
3950 {
3951 	clock_t local_lbolt;
3952 	timeout_id_t timeid;
3953 
3954 	/*
3955 	 * The low level mutex may not be held over the call to
3956 	 * untimeout().  See the manpage for details.
3957 	 */
3958 	timeid = fdc->c_mtimeid;
3959 	fdc->c_mtimeid = 0;
3960 	if (timeid) {
3961 		mutex_exit(&fdc->c_lolock);
3962 		(void) untimeout(timeid);
3963 		mutex_enter(&fdc->c_lolock);
3964 	}
3965 
3966 	ASSERT(fdc->c_un->un_unit_no == unit);
3967 
3968 
3969 	set_rotational_speed(fdc, unit);
3970 
3971 	if (!(Dor(fdc) & (MOTEN(unit)))) {
3972 		/*
3973 		 * Turn on the motor
3974 		 */
3975 		FDERRPRINT(FDEP_L1, FDEM_EXEC,
3976 			(C, "fdexec: turning on motor\n"));
3977 
3978 		/* LINTED */
3979 		Set_dor(fdc, (MOTEN(unit)), 1);
3980 
3981 		if (flags & FDXC_SLEEP) {
3982 			local_lbolt = ddi_get_lbolt();
3983 			(void) cv_timedwait(&fdc->c_motoncv,
3984 			    &fdc->c_lolock, local_lbolt + Moton_delay);
3985 		} else {
3986 			drv_usecwait(1000000);
3987 		}
3988 	}
3989 
3990 }
3991 
3992 /*
3993  * fdrecover
3994  *	see if possible to retry an operation.
3995  *	All we can do is restart the operation.	 If we are out of allowed
3996  *	retries - return non-zero so that the higher levels will be notified.
3997  *
3998  * RETURNS: 0 if ok to restart, !0 if can't or out of retries
3999  *	- called with the low level lock held
4000  */
4001 static int
4002 fdrecover(struct fdctlr *fdc)
4003 {
4004 	struct fdcsb *csb;
4005 
4006 	FDERRPRINT(FDEP_L1, FDEM_RECO, (C, "fdrecover\n"));
4007 	csb = &fdc->c_csb;
4008 
4009 	if (fdc->c_flags & FDCFLG_TIMEDOUT) {
4010 		struct fdcsb savecsb;
4011 
4012 		fdc->c_flags ^= FDCFLG_TIMEDOUT;
4013 		csb->csb_rslt[1] |= TO_SR1;
4014 		FDERRPRINT(FDEP_L1, FDEM_RECO,
4015 		    (C, "fd%d: %s timed out\n", csb->csb_unit,
4016 		    fdcmds[csb->csb_cmds[0] & 0x1f].cmdname));
4017 
4018 		/* use private csb */
4019 		savecsb = fdc->c_csb;
4020 		bzero(&fdc->c_csb, sizeof (struct fdcsb));
4021 		FDERRPRINT(FDEP_L1, FDEM_RECO, (C, "fdc: resetting\n"));
4022 
4023 		(void) fdreset(fdc);
4024 
4025 		if (fdc->c_fdtype & FDCTYPE_DMA) {
4026 			mutex_enter(&fdc->c_hilock);
4027 			/* Reset the DMA engine as well */
4028 			reset_dma_controller(fdc);
4029 			set_dma_control_register(fdc, DCSR_INIT_BITS);
4030 			mutex_exit(&fdc->c_hilock);
4031 		}
4032 
4033 
4034 		/* check change first?? */
4035 		/* don't ckchg in fdexec, too convoluted */
4036 		(void) fdrecalseek(fdc, savecsb.csb_unit, -1, 0);
4037 		fdc->c_csb = savecsb; /* restore original csb */
4038 	}
4039 
4040 	/*
4041 	 * gather statistics on errors
4042 	 */
4043 	if (csb->csb_rslt[1] & DE_SR1) {
4044 		fdc->fdstats.de++;
4045 	}
4046 	if (csb->csb_rslt[1] & OR_SR1) {
4047 		fdc->fdstats.run++;
4048 	}
4049 	if (csb->csb_rslt[1] & (ND_SR1+MA_SR1)) {
4050 		fdc->fdstats.bfmt++;
4051 	}
4052 	if (csb->csb_rslt[1] & TO_SR1) {
4053 		fdc->fdstats.to++;
4054 	}
4055 
4056 	/*
4057 	 * If raw ioctl don't examine results just pass status
4058 	 * back via fdraw. Raw commands are timed too, so put this
4059 	 * after the above check.
4060 	 */
4061 	if (csb->csb_opflags & CSB_OFRAWIOCTL) {
4062 		return (1);
4063 	}
4064 
4065 
4066 	/*
4067 	 * if there was a pci bus error, do not retry
4068 	 */
4069 
4070 	    if (csb->csb_dcsr_rslt == 1) {
4071 			FDERRPRINT(FDEP_L3, FDEM_RECO,
4072 			    (C, "fd%d: host bus error\n", 0));
4073 		return (1);
4074 	    }
4075 
4076 	/*
4077 	 * If there was an error with the DMA functions, do not retry
4078 	 */
4079 	if (csb->csb_dma_rslt == 1) {
4080 			FDERRPRINT(FDEP_L1, FDEM_RECO,
4081 			    (C, "fd%d: DMA interface error\n", csb->csb_unit));
4082 		return (1);
4083 	}
4084 
4085 
4086 	/*
4087 	 * if we have run out of retries, return an error
4088 	 * XXX need better status interp
4089 	 */
4090 
4091 	csb->csb_retrys++;
4092 	if (csb->csb_retrys > csb->csb_maxretry) {
4093 		FDERRPRINT(FDEP_L3, FDEM_RECO,
4094 		    (C, "fd%d: %s failed (%x %x %x)\n",
4095 		    0, fdcmds[csb->csb_cmds[0] & 0x1f].cmdname,
4096 		    csb->csb_rslt[0], csb->csb_rslt[1], csb->csb_rslt[2]));
4097 		if (csb->csb_rslt[1] & NW_SR1) {
4098 			FDERRPRINT(FDEP_L3, FDEM_RECO,
4099 			    (C, "fd%d: not writable\n", 0));
4100 		}
4101 		if (csb->csb_rslt[1] & DE_SR1) {
4102 			FDERRPRINT(FDEP_L3, FDEM_RECO,
4103 			    (C, "fd%d: crc error blk %d\n", 0,
4104 			    (int)fdc->c_current->b_blkno));
4105 		}
4106 		if (csb->csb_rslt[1] & OR_SR1) {
4107 			if (fdc->c_fdtype & FDCTYPE_SB) {
4108 				/*
4109 				 * When using southbridge chip we need to
4110 				 * retry atleast 10 times to shake off the
4111 				 * underrun err.
4112 				 */
4113 				if (csb->csb_retrys <= rwretry)
4114 					return (0);
4115 			}
4116 			FDERRPRINT(FDEP_L3, FDEM_RECO,
4117 			    (C, "fd%d: over/underrun\n", 0));
4118 		}
4119 
4120 		if (csb->csb_rslt[1] & (ND_SR1+MA_SR1)) {
4121 			FDERRPRINT(FDEP_L3, FDEM_RECO,
4122 			    (C, "fd%d: bad format\n", 0));
4123 		}
4124 
4125 		if (csb->csb_rslt[1] & TO_SR1) {
4126 			FDERRPRINT(FDEP_L3, FDEM_RECO,
4127 			    (C, "fd%d: timeout\n", 0));
4128 		}
4129 
4130 		csb->csb_cmdstat = 1; /* failed - give up */
4131 		return (1);
4132 	}
4133 
4134 	if (csb->csb_opflags & CSB_OFSEEKOPS) {
4135 		/* seek, recal type commands - just look at st0 */
4136 		FDERRPRINT(FDEP_L2, FDEM_RECO,
4137 		    (C, "fd%d: %s error : st0 0x%x\n", csb->csb_unit,
4138 		    fdcmds[csb->csb_cmds[0] & 0x1f].cmdname,
4139 		    csb->csb_rslt[0]));
4140 	}
4141 	if (csb->csb_opflags & CSB_OFXFEROPS) {
4142 		/* rd, wr, fmt type commands - look at st0, st1, st2 */
4143 		FDERRPRINT(FDEP_L2, FDEM_RECO,
4144 		    (C, "fd%d: %s error : st0=0x%x st1=0x%x st2=0x%x\n",
4145 		    csb->csb_unit, fdcmds[csb->csb_cmds[0] & 0x1f].cmdname,
4146 		    csb->csb_rslt[0], csb->csb_rslt[1], csb->csb_rslt[2]));
4147 	}
4148 
4149 	return (0);	/* tell fdexec to retry */
4150 }
4151 
4152 /*
4153  * Interrupt handle for DMA
4154  */
4155 
4156 static uint_t
4157 fdintr_dma()
4158 {
4159 	struct fdctlr   *fdc;
4160 	off_t		off;
4161 	size_t		len;
4162 	uint_t		ccount;
4163 	uint_t		windex;
4164 	uint_t		done = 0;
4165 	int		tmp_dcsr;
4166 	int		to;
4167 	uchar_t		tmp;
4168 	int		i = 0;
4169 	int		res = DDI_INTR_UNCLAIMED;
4170 	int		not_cheerio = 1;
4171 
4172 	/* search for a controller that's expecting an interrupt */
4173 	fdc = fdctlrs;
4174 
4175 	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
4176 		tmp_dcsr = get_dma_control_register(fdc);
4177 		if (!(tmp_dcsr & DCSR_INT_PEND) && !(DCSR_ERR_PEND & tmp_dcsr))
4178 			return (res);
4179 		not_cheerio = 0;
4180 	}
4181 
4182 	mutex_enter(&fdc->c_hilock);
4183 
4184 	if (fdc->c_csb.csb_opmode == 0x0) {
4185 		fdc->c_csb.csb_opmode = 2;
4186 	}
4187 	if (fdc->sb_dma_lock) {
4188 		release_sb_dma(fdc);
4189 	}
4190 
4191 	/*
4192 	 * An interrupt can come from either the floppy controller or
4193 	 * or the DMA engine.  The DMA engine will only issue an
4194 	 * interrupt if there was an error.
4195 	 */
4196 
4197 	switch (fdc->c_csb.csb_opmode) {
4198 		case 0x1:
4199 			/* read/write/format data-xfer case */
4200 
4201 			FDERRPRINT(FDEP_L1, FDEM_INTR,
4202 				(C, "fdintr_dma: opmode 1\n"));
4203 
4204 			/*
4205 			 * See if the interrupt is from the floppy
4206 			 * controller.  If there is, take out the status bytes.
4207 			 */
4208 
4209 			if (not_cheerio || (tmp_dcsr & DCSR_INT_PEND)) {
4210 
4211 				FDERRPRINT(FDEP_L1, FDEM_INTR,
4212 					(C, "fdintr_dma: INT_PEND \n"));
4213 
4214 				res = DDI_INTR_CLAIMED;
4215 
4216 				to = FD_RRETRY;
4217 				fdc->c_csb.csb_nrslts = 0;
4218 
4219 				/* check status */
4220 				i = 0;
4221 
4222 				/*
4223 				 * CB turns off once all the result bytes are
4224 				 *  read.
4225 				 *
4226 				 * NOTE: the counters are there so that the
4227 				 * handler will never get stuck in a loop.
4228 				 * If the counters do reach their maximum
4229 				 * values, then a catastrophic error has
4230 				 * occurred.  This should never be the case.
4231 				 * The counters only came into play during
4232 				 * development.
4233 				 */
4234 				while (((tmp = Msr(fdc)) & CB) &&
4235 					(i < 1000001)) {
4236 
4237 					/*
4238 					 * If RQM + DIO, then a result byte
4239 					 * is at hand.
4240 					 */
4241 					if ((tmp & (RQM|DIO|CB)) ==
4242 								(RQM|DIO|CB)) {
4243 						fdc->c_csb.csb_rslt
4244 						[fdc->c_csb.csb_nrslts++]
4245 							    = Fifo(fdc);
4246 
4247 						FDERRPRINT(FDEP_L1, FDEM_INTR,
4248 						(C, "fdintr_dma: res 0x%x\n",
4249 							fdc->c_csb.csb_rslt
4250 							[fdc->c_csb.csb_nrslts
4251 							- 1]));
4252 
4253 					} else if (--to == 0) {
4254 						/*
4255 						 * controller was never
4256 						 * ready to give results
4257 						 */
4258 						fdc->c_csb.csb_status = 2;
4259 						break;
4260 					}
4261 				i++;
4262 				}
4263 				if (i == 10000) {
4264 					FDERRPRINT(FDEP_L1, FDEM_INTR,
4265 						(C, "First loop overran\n"));
4266 				}
4267 			}
4268 
4269 			/*
4270 			 * See if the interrupt is from the DMA engine,
4271 			 * which will only interrupt on an error
4272 			 */
4273 			if ((!not_cheerio) && (tmp_dcsr & DCSR_ERR_PEND)) {
4274 
4275 				res = DDI_INTR_CLAIMED;
4276 
4277 				done = 1;
4278 				fdc->c_csb.csb_dcsr_rslt = 1;
4279 				FDERRPRINT(FDEP_L1, FDEM_INTR,
4280 					(C, "fdintr_dma: Error pending\n"));
4281 				reset_dma_controller(fdc);
4282 				set_dma_control_register(fdc, DCSR_INIT_BITS);
4283 				break;
4284 			}
4285 
4286 			/* TCBUG kludge */
4287 			if ((fdc->c_fdtype & FDCTYPE_TCBUG) &&
4288 				((fdc->c_csb.csb_rslt[0] & IC_SR0) == 0x40) &&
4289 				(fdc->c_csb.csb_rslt[1] & EN_SR1)) {
4290 
4291 				fdc->c_csb.csb_rslt[0] &= ~IC_SR0;
4292 
4293 				fdc->c_csb.csb_rslt[1] &= ~EN_SR1;
4294 
4295 
4296 			}
4297 
4298 
4299 			/* Exit if there were errors in the DMA */
4300 			if (((fdc->c_csb.csb_rslt[0] & IC_SR0) != 0) ||
4301 			    (fdc->c_csb.csb_rslt[1] != 0) ||
4302 			    (fdc->c_csb.csb_rslt[2] != 0)) {
4303 				done = 1;
4304 				FDERRPRINT(FDEP_L1, FDEM_INTR,
4305 					(C, "fdintr_dma: errors in command\n"));
4306 
4307 
4308 				break;
4309 			}
4310 
4311 
4312 			FDERRPRINT(FDEP_L1, FDEM_INTR,
4313 				(C, "fdintr_dma: dbcr 0x%x\n",
4314 				get_data_count_register(fdc)));
4315 			/*
4316 			 * The csb_ccount is the number of cookies that still
4317 			 * need to be processed.  A cookie was just processed
4318 			 * so decrement the cookie counter.
4319 			 */
4320 			if (fdc->c_csb.csb_ccount == 0) {
4321 				done = 1;
4322 				break;
4323 			}
4324 			fdc->c_csb.csb_ccount--;
4325 			ccount = fdc->c_csb.csb_ccount;
4326 
4327 			windex = fdc->c_csb.csb_windex;
4328 
4329 			/*
4330 			 * If there are no more cookies and all the windows
4331 			 * have been DMA'd, then DMA is done.
4332 			 *
4333 			 */
4334 			if ((ccount == 0) && (windex == fdc->c_csb.csb_nwin)) {
4335 
4336 				done = 1;
4337 
4338 				/*
4339 				 * The handle is unbound in fdexec
4340 				 */
4341 
4342 				break;
4343 			}
4344 
4345 			if (ccount != 0) {
4346 				/* process the next cookie */
4347 				ddi_dma_nextcookie(fdc->c_dmahandle,
4348 				    &fdc->c_csb.csb_dmacookie);
4349 
4350 				FDERRPRINT(FDEP_L1, FDEM_INTR,
4351 				    (C, "cookie addr 0x%" PRIx64 "\n",
4352 				    fdc->c_csb.csb_dmacookie.dmac_laddress));
4353 
4354 				FDERRPRINT(FDEP_L1, FDEM_INTR,
4355 				    (C, "cookie length %lu\n",
4356 				    fdc->c_csb.csb_dmacookie.dmac_size));
4357 
4358 			} else {
4359 
4360 				(void) ddi_dma_getwin(fdc->c_dmahandle,
4361 				    fdc->c_csb.csb_windex,
4362 				    &off, &len,
4363 				    &fdc->c_csb.csb_dmacookie,
4364 				    &fdc->c_csb.csb_ccount);
4365 				fdc->c_csb.csb_windex++;
4366 
4367 				FDERRPRINT(FDEP_L1, FDEM_INTR,
4368 				    (C, "fdintr_dma: process %d window\n",
4369 				    fdc->c_csb.csb_windex));
4370 
4371 				FDERRPRINT(FDEP_L1, FDEM_INTR,
4372 				    (C, "fdintr_dma: process no. cookies %d\n",
4373 				    fdc->c_csb.csb_ccount));
4374 
4375 				FDERRPRINT(FDEP_L1, FDEM_INTR,
4376 				    (C, "cookie addr 0x%" PRIx64 "\n",
4377 				    fdc->c_csb.csb_dmacookie.dmac_laddress));
4378 
4379 				FDERRPRINT(FDEP_L1, FDEM_INTR,
4380 				    (C, "cookie length %lu\n",
4381 				    fdc->c_csb.csb_dmacookie.dmac_size));
4382 			}
4383 
4384 			/*
4385 			 * Program the DMA engine with the length and
4386 			 * the address of the transfer
4387 			 */
4388 
4389 			ASSERT(fdc->c_csb.csb_dmacookie.dmac_size);
4390 
4391 			set_data_count_register(fdc,
4392 				fdc->c_csb.csb_dmacookie.dmac_size);
4393 			set_data_address_register(fdc,
4394 				fdc->c_csb.csb_dmacookie.dmac_laddress);
4395 
4396 			FDERRPRINT(FDEP_L1, FDEM_INTR, (C,
4397 			    "fdintr_dma: size 0x%lx\n",
4398 			    fdc->c_csb.csb_dmacookie.dmac_size));
4399 
4400 
4401 			/* reprogram the controller */
4402 			fdc->c_csb.csb_cmds[2] = fdc->c_csb.csb_rslt[3];
4403 			fdc->c_csb.csb_cmds[3] = fdc->c_csb.csb_rslt[4];
4404 			fdc->c_csb.csb_cmds[4] = fdc->c_csb.csb_rslt[5];
4405 			fdc->c_csb.csb_cmds[1] = (fdc->c_csb.csb_cmds[1]
4406 				& ~0x04) | (fdc->c_csb.csb_rslt[4] << 2);
4407 
4408 			for (i = 0; i < (int)fdc->c_csb.csb_ncmds; i++) {
4409 
4410 				/*
4411 				 * Test the readiness of the controller
4412 				 * to receive the cmd
4413 				 */
4414 				for (to = FD_CRETRY; to; to--) {
4415 					if ((Msr(fdc) & (DIO|RQM)) == RQM)
4416 						break;
4417 				}
4418 				if (to == 0) {
4419 					FDERRPRINT(FDEP_L2, FDEM_EXEC,
4420 					(C,
4421 					"fdc: no RQM - stat 0x%x\n", Msr(fdc)));
4422 					/* stop the DMA from happening */
4423 					fdc->c_csb.csb_status = 2;
4424 					done = 1;
4425 					break;
4426 				}
4427 
4428 				Set_Fifo(fdc, fdc->c_csb.csb_cmds[i]);
4429 
4430 				FDERRPRINT(FDEP_L1, FDEM_INTR,
4431 					(C,
4432 					"fdintr_dma: sent 0x%x, Msr 0x%x\n",
4433 					fdc->c_csb.csb_cmds[i], Msr(fdc)));
4434 			}
4435 
4436 			/* reenable DMA */
4437 			if ((!not_cheerio) && (!done))
4438 				set_dma_control_register(fdc, tmp_dcsr |
4439 					DCSR_EN_DMA);
4440 			break;
4441 
4442 		case 0x2:
4443 		/* seek/recal type cmd */
4444 			FDERRPRINT(FDEP_L1, FDEM_INTR,
4445 				(C, "fintr_dma: opmode 2\n"));
4446 
4447 			/*
4448 			 *  See if the interrupt is from the DMA engine,
4449 			 *  which will only interrupt if there was an error.
4450 			 */
4451 			if ((!not_cheerio) && (tmp_dcsr & DCSR_ERR_PEND)) {
4452 				res = DDI_INTR_CLAIMED;
4453 				done = 1;
4454 				fdc->c_csb.csb_dcsr_rslt = 1;
4455 				reset_dma_controller(fdc);
4456 				set_dma_control_register(fdc, DCSR_INIT_BITS);
4457 
4458 				break;
4459 			}
4460 
4461 
4462 			/* See if the interrupt is from the floppy controller */
4463 			if (not_cheerio || (tmp_dcsr & DCSR_INT_PEND)) {
4464 
4465 				res = DDI_INTR_CLAIMED;
4466 
4467 
4468 				/*
4469 				 * Wait until there's no longer a command
4470 				 * in progress
4471 				 */
4472 
4473 				FDERRPRINT(FDEP_L1, FDEM_INTR,
4474 					(C, "fdintr_dma: interrupt pending\n"));
4475 				i = 0;
4476 				while (((Msr(fdc) & CB)) && (i < 10000)) {
4477 					i++;
4478 				}
4479 
4480 				if (i == 10000)
4481 					FDERRPRINT(FDEP_L1, FDEM_INTR,
4482 						(C, "2nd loop overran !!!\n"));
4483 
4484 				/*
4485 				 * Check the RQM bit to see if the controller is
4486 				 * ready to transfer status of the command.
4487 				 */
4488 				i = 0;
4489 				while ((!(Msr(fdc) & RQM)) && (i < 10000)) {
4490 					i++;
4491 				}
4492 
4493 				if (i == 10000)
4494 					FDERRPRINT(FDEP_L1, FDEM_INTR,
4495 					    (C, "3rd loop overran !!!\n"));
4496 
4497 				/*
4498 				 * Issue the Sense Interrupt Status Command
4499 				 */
4500 				Set_Fifo(fdc, SNSISTAT);
4501 
4502 				i = 0;
4503 				while ((!(Msr(fdc) & RQM)) && (i < 10000)) {
4504 					i++;
4505 				}
4506 				if (i == 10000)
4507 					FDERRPRINT(FDEP_L1, FDEM_INTR,
4508 					(C, "4th loop overran !!!\n"));
4509 
4510 				/* Store the first result byte */
4511 				fdc->c_csb.csb_rslt[0] = Fifo(fdc);
4512 
4513 				i = 0;
4514 				while ((!(Msr(fdc) & RQM)) && (i < 10000)) {
4515 					i++;
4516 				}
4517 				if (i == 10000)
4518 					FDERRPRINT(FDEP_L1, FDEM_INTR,
4519 					(C, "5th loop overran !!!\n"));
4520 
4521 				/* Store the second  result byte */
4522 				fdc->c_csb.csb_rslt[1] = Fifo(fdc);
4523 
4524 				done = 1;
4525 			}
4526 
4527 		}
4528 
4529 	/*
4530 	 * We are done with the actual interrupt handling here.
4531 	 * The portion below should be actually be done by fd_lointr().
4532 	 * We should be triggering the fd_lointr here and exiting.
4533 	 * However for want of time this will be done in the next FIX.
4534 	 *
4535 	 * Hence for now we will release hilock only and keep the remaining
4536 	 * code as it is.
4537 	 * Releasing of hilock ensures that we don't hold on to the
4538 	 * lolock and hilock at the same time.
4539 	 * hilock is acquired each time dma related  registers are accessed.
4540 	 */
4541 	mutex_exit(&fdc->c_hilock);
4542 	/* Make signal and get out of interrupt handler */
4543 	if (done) {
4544 		mutex_enter(&fdc->c_lolock);
4545 
4546 		fdc->c_csb.csb_opmode = 0;
4547 
4548 		/*  reset watchdog timer if armed and not already triggered */
4549 
4550 
4551 		if (fdc->c_timeid) {
4552 			timeout_id_t timeid = fdc->c_timeid;
4553 			fdc->c_timeid = 0;
4554 			mutex_exit(&fdc->c_lolock);
4555 			(void) untimeout(timeid);
4556 			mutex_enter(&fdc->c_lolock);
4557 		}
4558 
4559 
4560 		if (fdc->c_flags & FDCFLG_WAITING) {
4561 			/*
4562 			 * somebody's waiting on finish of fdctlr/csb,
4563 			 * wake them
4564 			 */
4565 
4566 			FDERRPRINT(FDEP_L1, FDEM_INTR,
4567 				(C, "fdintr_dma: signal the waiter\n"));
4568 
4569 			fdc->c_flags ^= FDCFLG_WAITING;
4570 			cv_signal(&fdc->c_iocv);
4571 
4572 			/*
4573 			 * FDCFLG_BUSY is NOT cleared, NOR is the csb given
4574 			 * back; the operation just finished can look at the csb
4575 			 */
4576 		} else {
4577 			FDERRPRINT(FDEP_L1, FDEM_INTR,
4578 				(C, "fdintr_dma: nobody sleeping (%x %x %x)\n",
4579 			fdc->c_csb.csb_rslt[0], fdc->c_csb.csb_rslt[1],
4580 			fdc->c_csb.csb_rslt[2]));
4581 		}
4582 		mutex_exit(&fdc->c_lolock);
4583 	}
4584 	/* update high level interrupt counter */
4585 	if (fdc->c_intrstat)
4586 			KIOIP->intrs[KSTAT_INTR_HARD]++;
4587 
4588 
4589 	FDERRPRINT(FDEP_L1, FDEM_INTR, (C, "fdintr_dma: done\n"));
4590 	return (res);
4591 }
4592 
4593 /*
4594  * fd_lointr
4595  *	This is the low level SW interrupt handler triggered by the high
4596  *	level interrupt handler (or by fdwatch).
4597  */
4598 static uint_t
4599 fd_lointr(caddr_t arg)
4600 {
4601 	struct fdctlr *fdc = (struct fdctlr *)arg;
4602 	struct fdcsb *csb;
4603 
4604 	csb = &fdc->c_csb;
4605 	FDERRPRINT(FDEP_L1, FDEM_INTR, (C, "fdintr: opmode %d\n",
4606 	    csb->csb_opmode));
4607 	/*
4608 	 * Check that lowlevel interrupt really meant to trigger us.
4609 	 */
4610 	if (csb->csb_opmode != 4) {
4611 		/*
4612 		 * This should probably be protected, but, what the
4613 		 * heck...the cost isn't worth the accuracy for this
4614 		 * statistic.
4615 		 */
4616 		if (fdc->c_intrstat)
4617 			KIOIP->intrs[KSTAT_INTR_SPURIOUS]++;
4618 		return (DDI_INTR_UNCLAIMED);
4619 	}
4620 
4621 	mutex_enter(&fdc->c_lolock);
4622 	csb->csb_opmode = 0;
4623 
4624 	/*  reset watchdog timer if armed and not already triggered */
4625 	if (fdc->c_timeid) {
4626 		timeout_id_t timeid = fdc->c_timeid;
4627 		fdc->c_timeid = 0;
4628 		mutex_exit(&fdc->c_lolock);
4629 		(void) untimeout(timeid);
4630 		mutex_enter(&fdc->c_lolock);
4631 
4632 	}
4633 
4634 	if (fdc->c_flags & FDCFLG_WAITING) {
4635 		/*
4636 		 * somebody's waiting on finish of fdctlr/csb, wake them
4637 		 */
4638 		fdc->c_flags ^= FDCFLG_WAITING;
4639 		cv_signal(&fdc->c_iocv);
4640 
4641 		/*
4642 		 * FDCFLG_BUSY is NOT cleared, NOR is the csb given back; so
4643 		 * the operation just finished can look at the csb
4644 		 */
4645 	} else {
4646 		FDERRPRINT(FDEP_L3, FDEM_INTR,
4647 		    (C, "fdintr: nobody sleeping (%x %x %x)\n",
4648 		    csb->csb_rslt[0], csb->csb_rslt[1], csb->csb_rslt[2]));
4649 	}
4650 	if (fdc->c_intrstat)
4651 		KIOIP->intrs[KSTAT_INTR_SOFT]++;
4652 	mutex_exit(&fdc->c_lolock);
4653 	return (DDI_INTR_CLAIMED);
4654 }
4655 
4656 /*
4657  * fdwatch
4658  *	is called from timein() when a floppy operation has expired.
4659  */
4660 static void
4661 fdwatch(void *arg)
4662 {
4663 	struct fdctlr *fdc = arg;
4664 	int old_opmode;
4665 	struct fdcsb *csb;
4666 
4667 	FDERRPRINT(FDEP_L1, FDEM_WATC, (C, "fdwatch\n"));
4668 
4669 	mutex_enter(&fdc->c_lolock);
4670 	if (fdc->c_timeid == 0) {
4671 		/*
4672 		 * fdintr got here first, ergo, no timeout condition..
4673 		 */
4674 
4675 		FDERRPRINT(FDEP_L1, FDEM_WATC,
4676 				(C, "fdwatch: no timeout\n"));
4677 
4678 		mutex_exit(&fdc->c_lolock);
4679 		return;
4680 	}
4681 	fdc->c_timeid = 0;
4682 	csb = &fdc->c_csb;
4683 
4684 	mutex_enter(&fdc->c_hilock);
4685 	/*
4686 	 * XXXX: We should probably reset the bloody chip
4687 	 */
4688 	old_opmode = csb->csb_opmode;
4689 
4690 	FDERRPRINT(FDEP_L1, FDEM_WATC,
4691 	    (C, "fd%d: timeout, opmode:%d\n", csb->csb_unit, old_opmode));
4692 
4693 	csb->csb_opmode = 4;
4694 	mutex_exit(&fdc->c_hilock);
4695 
4696 	FDERRPRINT(FDEP_L1, FDEM_WATC, (C, "fdwatch: cmd %s timed out\n",
4697 				fdcmds[csb->csb_cmds[0] & 0x1f].cmdname));
4698 	fdc->c_flags |= FDCFLG_TIMEDOUT;
4699 	csb->csb_status = CSB_CMDTO;
4700 
4701 	if ((fdc->c_fdtype & FDCTYPE_DMA) == 0) {
4702 		ddi_trigger_softintr(fdc->c_softid);
4703 		KIOIP->intrs[KSTAT_INTR_WATCHDOG]++;
4704 		mutex_exit(&fdc->c_lolock);
4705 	} else {
4706 		mutex_exit(&fdc->c_lolock);
4707 		(void) fd_lointr((caddr_t)fdctlrs);
4708 	}
4709 }
4710 
4711 /*
4712  * fdgetcsb
4713  *	wait until the csb is free
4714  */
4715 static void
4716 fdgetcsb(struct fdctlr *fdc)
4717 {
4718 	FDERRPRINT(FDEP_L1, FDEM_GETC, (C, "fdgetcsb\n"));
4719 	ASSERT(mutex_owned(&fdc->c_lolock));
4720 	while (fdc->c_flags & FDCFLG_BUSY) {
4721 		fdc->c_flags |= FDCFLG_WANT;
4722 		cv_wait(&fdc->c_csbcv, &fdc->c_lolock);
4723 	}
4724 	fdc->c_flags |= FDCFLG_BUSY; /* got it! */
4725 }
4726 
4727 /*
4728  * fdretcsb
4729  *	return csb
4730  */
4731 static void
4732 fdretcsb(struct fdctlr *fdc)
4733 {
4734 
4735 	ASSERT(mutex_owned(&fdc->c_lolock));
4736 	FDERRPRINT(FDEP_L1, FDEM_RETC, (C, "fdretcsb\n"));
4737 	fdc->c_flags &= ~FDCFLG_BUSY; /* let go */
4738 
4739 	fdc->c_csb.csb_read = 0;
4740 
4741 	if (fdc->c_flags & FDCFLG_WANT) {
4742 		fdc->c_flags ^= FDCFLG_WANT;
4743 		/*
4744 		 * broadcast the signal.  One thread will wake up and
4745 		 * set the flags to FDCFLG_BUSY.  If more than one thread is
4746 		 * waiting then each thread will wake up in turn.  The first
4747 		 * thread to wake-up will set the FDCFLG_BUSY flag and the
4748 		 * subsequent threads will will wake-up, but reset the
4749 		 * flag to FDCFLG_WANT because the FDCFLG_BUSY bit is set.
4750 		 */
4751 		cv_broadcast(&fdc->c_csbcv);
4752 	}
4753 }
4754 
4755 
4756 /*
4757  * fdreset
4758  *	reset THE controller, and configure it to be
4759  *	the way it ought to be
4760  * ASSUMES: that it already owns the csb/fdctlr!
4761  *
4762  *	- called with the low level lock held
4763  */
4764 static int
4765 fdreset(struct fdctlr *fdc)
4766 {
4767 	struct fdcsb *csb;
4768 	clock_t local_lbolt = 0;
4769 	timeout_id_t timeid;
4770 
4771 	FDERRPRINT(FDEP_L1, FDEM_RESE, (C, "fdreset\n"));
4772 
4773 	ASSERT(mutex_owned(&fdc->c_lolock));
4774 
4775 	/* count resets */
4776 	fdc->fdstats.reset++;
4777 
4778 	/*
4779 	 * On the 82077, the DSR will clear itself after a reset.  Upon exiting
4780 	 * the reset, a polling interrupt will be generated.  If the floppy
4781 	 * interrupt is enabled, it's possible for cv_signal() to be called
4782 	 * before cv_wait().  This will cause the system to hang.  Turn off
4783 	 * the floppy interrupt to avoid this race condition
4784 	 */
4785 	if ((fdc->c_fdtype & FDCTYPE_CTRLMASK) == FDCTYPE_82077) {
4786 		/*
4787 		 * We need to perform any timeouts before we Reset the
4788 		 * controller. We cannot afford to drop the c_lolock mutex after
4789 		 * Resetting the controller. The reason is that we get a spate
4790 		 * of interrupts until we take the controller out of reset.
4791 		 * The way we avoid this spate of continuous interrupts is by
4792 		 * holding on to the c_lolock and forcing the fdintr_dma routine
4793 		 * to go to sleep waiting for this mutex.
4794 		 */
4795 		/* Do not hold the mutex across the untimeout call */
4796 		timeid = fdc->c_mtimeid;
4797 		fdc->c_mtimeid = 0;
4798 		if (timeid) {
4799 			mutex_exit(&fdc->c_lolock);
4800 			(void) untimeout(timeid);
4801 			mutex_enter(&fdc->c_lolock);
4802 		}
4803 		/* LINTED */
4804 		Set_dor(fdc, DMAGATE, 0);
4805 		FDERRPRINT(FDEP_L1, FDEM_RESE, (C, "fdreset: set dor\n"));
4806 	}
4807 
4808 	/* toggle software reset */
4809 	Dsr(fdc, SWR);
4810 
4811 	drv_usecwait(5);
4812 
4813 	FDERRPRINT(FDEP_L1, FDEM_RESE,
4814 			(C, "fdreset: toggled software reset\n"));
4815 
4816 	/*
4817 	 * This sets the data rate to 500Kbps (for high density)
4818 	 * XXX should use current characteristics instead XXX
4819 	 */
4820 	Dsr(fdc, 0);
4821 	drv_usecwait(5);
4822 	switch (fdc->c_fdtype & FDCTYPE_CTRLMASK) {
4823 	case FDCTYPE_82077:
4824 		/*
4825 		 * when we bring the controller out of reset it will generate
4826 		 * a polling interrupt. fdintr() will field it and schedule
4827 		 * fd_lointr(). There will be no one sleeping but we are
4828 		 * expecting an interrupt so....
4829 		 */
4830 		fdc->c_flags |= FDCFLG_WAITING;
4831 
4832 		/*
4833 		 * The reset bit must be cleared to take the 077 out of
4834 		 * reset state and the DMAGATE bit must be high to enable
4835 		 * interrupts.
4836 		 */
4837 		/* LINTED */
4838 		Set_dor(fdc, DMAGATE|RESET, 1);
4839 
4840 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
4841 			(C, "fdattach: Dor 0x%x\n", Dor(fdc)));
4842 
4843 		local_lbolt = ddi_get_lbolt();
4844 		if (cv_timedwait(&fdc->c_iocv, &fdc->c_lolock,
4845 			local_lbolt + drv_usectohz(1000000)) == -1) {
4846 			return (-1);
4847 		}
4848 		break;
4849 
4850 	default:
4851 		fdc->c_flags |= FDCFLG_WAITING;
4852 
4853 		/*
4854 		 * A timed wait is not used because it's possible for the timer
4855 		 * to go off before the controller has a chance to interrupt.
4856 		 */
4857 		cv_wait(&fdc->c_iocv, &fdc->c_lolock);
4858 		break;
4859 	}
4860 	csb = &fdc->c_csb;
4861 
4862 	/* setup common things in csb */
4863 	csb->csb_unit = fdc->c_un->un_unit_no;
4864 	csb->csb_nrslts = 0;
4865 	csb->csb_opflags = CSB_OFNORESULTS;
4866 	csb->csb_maxretry = 0;
4867 	csb->csb_retrys = 0;
4868 
4869 	csb->csb_read = CSB_NULL;
4870 
4871 	/* send SPECIFY command to fdc */
4872 	/* csb->unit is don't care */
4873 	csb->csb_cmds[0] = FDRAW_SPECIFY;
4874 	csb->csb_cmds[1] = fdspec[0]; /* step rate, head unload time */
4875 	if (fdc->c_fdtype & FDCTYPE_DMA)
4876 		csb->csb_cmds[2] =  SPEC_DMA_MODE;
4877 	else
4878 		csb->csb_cmds[2] = fdspec[1];  /* head load time, DMA mode */
4879 
4880 	csb->csb_ncmds = 3;
4881 
4882 	/* XXX for now ignore errors, they "CAN'T HAPPEN" */
4883 	(void) fdexec(fdc, 0);	/* no FDXC_CHECKCHG, ... */
4884 	/* no results */
4885 
4886 	/* send CONFIGURE command to fdc */
4887 	/* csb->unit is don't care */
4888 	csb->csb_cmds[0] = CONFIGURE;
4889 	csb->csb_cmds[1] = fdconf[0]; /* motor info, motor delays */
4890 	csb->csb_cmds[2] = fdconf[1]; /* enaimplsk, disapoll, fifothru */
4891 	csb->csb_cmds[3] = fdconf[2]; /* track precomp */
4892 	csb->csb_ncmds = 4;
4893 
4894 	csb->csb_read = CSB_NULL;
4895 
4896 	csb->csb_retrys = 0;
4897 
4898 	/* XXX for now ignore errors, they "CAN'T HAPPEN" */
4899 	(void) fdexec(fdc, 0); /* no FDXC_CHECKCHG, ... */
4900 	return (0);
4901 }
4902 
4903 /*
4904  * fdrecalseek
4905  *	performs recalibrates or seeks if the "arg" is -1 does a
4906  *	recalibrate on a drive, else it seeks to the cylinder of
4907  *	the drive.  The recalibrate is also used to find a drive,
4908  *	ie if the drive is not there, the controller says "error"
4909  *	on the operation
4910  * NOTE: that there is special handling of this operation in the hardware
4911  * interrupt routine - it causes the operation to appear to have results;
4912  * ie the results of the SENSE INTERRUPT STATUS that the hardware interrupt
4913  * function did for us.
4914  * NOTE: because it uses sleep/wakeup it must be protected in a critical
4915  * section so create one before calling it!
4916  *
4917  * RETURNS: 0 for ok,
4918  *	else	errno from fdexec,
4919  *	or	ENODEV if error (infers hardware type error)
4920  *
4921  *	- called with the low level lock held
4922  */
4923 static int
4924 fdrecalseek(struct fdctlr *fdc, int unit, int arg, int execflg)
4925 {
4926 	struct fdcsb *csb;
4927 	int result;
4928 
4929 	ASSERT(fdc->c_un->un_unit_no == unit);
4930 
4931 	FDERRPRINT(FDEP_L1, FDEM_RECA, (C, "fdrecalseek to %d\n", arg));
4932 
4933 	/* XXX TODO: check see argument for <= num cyls OR < 256 */
4934 
4935 	csb = &fdc->c_csb;
4936 	csb->csb_unit = (uchar_t)unit;
4937 	csb->csb_cmds[1] = unit & 0x03;
4938 
4939 	if (arg == -1) {			/* is recal... */
4940 		csb->csb_cmds[0] = FDRAW_REZERO;
4941 		csb->csb_ncmds = 2;
4942 	} else {
4943 		csb->csb_cmds[0] = FDRAW_SEEK;
4944 		csb->csb_cmds[2] = (uchar_t)arg;
4945 		csb->csb_ncmds = 3;
4946 	}
4947 	csb->csb_nrslts = 2;	/* 2 for SENSE INTERRUPTS */
4948 	csb->csb_opflags = CSB_OFSEEKOPS | CSB_OFTIMEIT;
4949 	/*
4950 	 * MAYBE NYD need to set retries to different values? - depending on
4951 	 * drive characteristics - if we get to high capacity drives
4952 	 */
4953 	csb->csb_maxretry = skretry;
4954 	csb->csb_retrys = 0;
4955 
4956 	/* send cmd off to fdexec */
4957 	if (result = fdexec(fdc, FDXC_SLEEP | execflg)) {
4958 		goto out;
4959 	}
4960 
4961 	/*
4962 	 * if recal, test for equipment check error
4963 	 * ASSUMES result = 0 from above call
4964 	 */
4965 	if (arg == -1) {
4966 		result = 0;
4967 	} else {
4968 		/* for seeks, any old error will do */
4969 		if ((csb->csb_rslt[0] & IC_SR0) || csb->csb_cmdstat)
4970 			result = ENODEV;
4971 	}
4972 
4973 out:
4974 	return (result);
4975 }
4976 
4977 /*
4978  * fdsensedrv
4979  *	do a sense_drive command.  used by fdopen and fdcheckdisk.
4980  *
4981  *	- called with the lock held
4982  */
4983 static int
4984 fdsensedrv(struct fdctlr *fdc, int unit)
4985 {
4986 	struct fdcsb *csb;
4987 
4988 	ASSERT(fdc->c_un->un_unit_no == unit);
4989 
4990 	csb = &fdc->c_csb;
4991 
4992 	/* setup common things in csb */
4993 	csb->csb_unit = (uchar_t)unit;
4994 	csb->csb_opflags = CSB_OFIMMEDIATE;
4995 	csb->csb_cmds[0] = FDRAW_SENSE_DRV;
4996 	/* MOT bit set means don't delay */
4997 	csb->csb_cmds[1] = MOT | (unit & 0x03);
4998 	csb->csb_ncmds = 2;
4999 	csb->csb_nrslts = 1;
5000 	csb->csb_maxretry = skretry;
5001 	csb->csb_retrys = 0;
5002 
5003 	/* XXX for now ignore errors, they "CAN'T HAPPEN" */
5004 	(void) fdexec(fdc, 0);	/* DON't check changed!, no sleep */
5005 
5006 	FDERRPRINT(FDEP_L1, FDEM_CHEK,
5007 		(C, "fdsensedrv: result 0x%x", csb->csb_rslt[0]));
5008 
5009 	return (csb->csb_rslt[0]); /* return status byte 3 */
5010 }
5011 
5012 /*
5013  * fdcheckdisk
5014  *	check to see if the disk is still there - do a recalibrate,
5015  *	then see if DSKCHG line went away, if so, diskette is in; else
5016  *	it's (still) out.
5017  */
5018 
5019 static int
5020 fdcheckdisk(struct fdctlr *fdc, int unit)
5021 {
5022 	auto struct fdcsb savecsb;
5023 	struct fdcsb *csb;
5024 	int	err, st3;
5025 	int	seekto;			/* where to seek for reset of DSKCHG */
5026 
5027 	FDERRPRINT(FDEP_L1, FDEM_CHEK,
5028 	    (C, "fdcheckdisk, unit %d\n", unit));
5029 
5030 	ASSERT(fdc->c_un->un_unit_no == unit);
5031 
5032 	/*
5033 	 * save old csb
5034 	 */
5035 
5036 	csb = &fdc->c_csb;
5037 	savecsb = fdc->c_csb;
5038 	bzero((caddr_t)csb, sizeof (*csb));
5039 
5040 	/*
5041 	 * Read drive status to see if at TRK0, if so, seek to cyl 1,
5042 	 * else seek to cyl 0.	We do this because the controller is
5043 	 * "smart" enough to not send any step pulses (which are how
5044 	 * the DSKCHG line gets reset) if it sees TRK0 'cause it
5045 	 * knows the drive is already recalibrated.
5046 	 */
5047 	st3 = fdsensedrv(fdc, unit);
5048 
5049 	/* check TRK0 bit in status */
5050 	if (st3 & T0_SR3)
5051 		seekto = 1;	/* at TRK0, seek out */
5052 	else
5053 		seekto = 0;
5054 
5055 	/*
5056 	 * DON'T recurse check changed
5057 	 */
5058 	err = fdrecalseek(fdc, unit, seekto, 0);
5059 
5060 	/* "restore" old csb, check change state */
5061 	fdc->c_csb = savecsb;
5062 
5063 	/* any recal/seek errors are too serious to attend to */
5064 	if (err) {
5065 		FDERRPRINT(FDEP_L2, FDEM_CHEK,
5066 		    (C, "fdcheckdisk err %d\n", err));
5067 		return (err);
5068 	}
5069 
5070 	/*
5071 	 * if disk change still asserted, no diskette in drive!
5072 	 */
5073 	if (fdsense_chng(fdc, csb->csb_unit)) {
5074 		FDERRPRINT(FDEP_L2, FDEM_CHEK,
5075 		    (C, "fdcheckdisk no disk\n"));
5076 		return (1);
5077 	}
5078 	return (0);
5079 }
5080 
5081 /*
5082  *	fdselect() - select drive, needed for external to chip select logic
5083  *	fdeject() - ejects drive, must be previously selected
5084  *	fdsense_chng() - sense disk changed line from previously selected drive
5085  *		return s 1 is signal asserted, else 0
5086  */
5087 /* ARGSUSED */
5088 static void
5089 fdselect(struct fdctlr *fdc, int unit, int on)
5090 {
5091 
5092 	ASSERT(fdc->c_un->un_unit_no == unit);
5093 
5094 	FDERRPRINT(FDEP_L1, FDEM_DSEL,
5095 	    (C, "fdselect, unit %d, on = %d\n", unit, on));
5096 
5097 	switch (fdc->c_fdtype & FDCTYPE_AUXIOMASK) {
5098 	case FDCTYPE_MACHIO:
5099 		set_auxioreg(AUX_DRVSELECT, on);
5100 		break;
5101 
5102 	case FDCTYPE_SLAVIO:
5103 	case FDCTYPE_CHEERIO:
5104 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
5105 			(C, "fdselect: (before) Dor 0x%x\n", Dor(fdc)));
5106 
5107 		if (unit == 0) {
5108 			Set_dor(fdc, DRVSEL, !on);
5109 		} else {
5110 			Set_dor(fdc, DRVSEL, on);
5111 		}
5112 
5113 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
5114 			(C, "fdselect: Dor 0x%x\n", Dor(fdc)));
5115 
5116 		break;
5117 
5118 	default:
5119 		break;
5120 	}
5121 }
5122 
5123 /* ARGSUSED */
5124 static void
5125 fdeject(struct fdctlr *fdc, int unit)
5126 {
5127 	struct fdunit *un;
5128 
5129 	ASSERT(fdc->c_un->un_unit_no == unit);
5130 
5131 	un = fdc->c_un;
5132 
5133 	FDERRPRINT(FDEP_L1, FDEM_EJEC, (C, "fdeject\n"));
5134 	/*
5135 	 * assume delay of function calling sufficient settling time
5136 	 * eject line is NOT driven by inverter so it is true low
5137 	 */
5138 	switch (fdc->c_fdtype & FDCTYPE_AUXIOMASK) {
5139 	case FDCTYPE_MACHIO:
5140 		set_auxioreg(AUX_EJECT, 0);
5141 		drv_usecwait(2);
5142 		set_auxioreg(AUX_EJECT, 1);
5143 		break;
5144 
5145 	case FDCTYPE_SLAVIO:
5146 		if (!(Dor(fdc) & MOTEN(unit))) {
5147 			/* LINTED */
5148 			Set_dor(fdc, MOTEN(unit), 1);
5149 		}
5150 		drv_usecwait(2);	/* just to settle */
5151 		/* LINTED */
5152 		Set_dor(fdc, EJECT, 1);
5153 		drv_usecwait(2);
5154 		/* LINTED */
5155 		Set_dor(fdc, EJECT, 0);
5156 		break;
5157 	case FDCTYPE_CHEERIO:
5158 		if (!(Dor(fdc) & MOTEN(unit))) {
5159 			/* LINTED */
5160 			Set_dor(fdc, MOTEN(unit), 1);
5161 		}
5162 		drv_usecwait(2);	/* just to settle */
5163 		/* LINTED */
5164 		Set_dor(fdc, EJECT_DMA, 1);
5165 		drv_usecwait(2);
5166 		/* LINTED */
5167 		Set_dor(fdc, EJECT_DMA, 0);
5168 		break;
5169 	}
5170 	/*
5171 	 * XXX set ejected state?
5172 	 */
5173 	un->un_ejected = 1;
5174 }
5175 
5176 /* ARGSUSED */
5177 static int
5178 fdsense_chng(struct fdctlr *fdc, int unit)
5179 {
5180 	int changed = 0;
5181 
5182 	FDERRPRINT(FDEP_L1, FDEM_SCHG, (C, "fdsense_chng:start\n"));
5183 
5184 	ASSERT(fdc->c_un->un_unit_no == unit);
5185 
5186 	/*
5187 	 * Do not turn on the motor of a pollable drive
5188 	 */
5189 	if (fd_pollable) {
5190 	FDERRPRINT(FDEP_L1, FDEM_SCHG, (C, "pollable: don't turn on motor\n"));
5191 		/*
5192 		 * Invert the sense of the DSKCHG for pollable drives
5193 		 */
5194 		if (Dir(fdc) & DSKCHG)
5195 			changed = 0;
5196 		else
5197 			changed = 1;
5198 
5199 		return (changed);
5200 	}
5201 
5202 	switch (fdc->c_fdtype & FDCTYPE_AUXIOMASK) {
5203 	case FDCTYPE_MACHIO:
5204 		if (*fdc->c_auxiova & AUX_DISKCHG)
5205 			changed = 1;
5206 		break;
5207 
5208 	case FDCTYPE_SB:
5209 	case FDCTYPE_SLAVIO:
5210 	case FDCTYPE_CHEERIO:
5211 		if (!(Dor(fdc) & MOTEN(unit))) {
5212 			/* LINTED */
5213 			Set_dor(fdc, MOTEN(unit), 1);
5214 		}
5215 		drv_usecwait(2);	/* just to settle */
5216 		if (Dir(fdc) & DSKCHG)
5217 			changed = 1;
5218 		break;
5219 	}
5220 
5221 	FDERRPRINT(FDEP_L1, FDEM_SCHG, (C, "fdsense_chng:end\n"));
5222 
5223 	return (changed);
5224 }
5225 
5226 /*
5227  *	if it can read a valid label it does so, else it will use a
5228  *	default.  If it can`t read the diskette - that is an error.
5229  *
5230  * RETURNS: 0 for ok - meaning that it could at least read the device,
5231  *	!0 for error XXX TBD NYD error codes
5232  *
5233  *	- called with the low level lock held
5234  */
5235 static int
5236 fdgetlabel(struct fdctlr *fdc, int unit)
5237 {
5238 	struct dk_label *label = NULL;
5239 	struct fdunit *un;
5240 	short *sp;
5241 	short count;
5242 	short xsum;			/* checksum */
5243 	int	i, tries;
5244 	int	err = 0;
5245 	short	oldlvl;
5246 
5247 	FDERRPRINT(FDEP_L1, FDEM_GETL,
5248 	    (C, "fdgetlabel: unit %d\n", unit));
5249 
5250 	un = fdc->c_un;
5251 	un->un_flags &= ~(FDUNIT_UNLABELED);
5252 
5253 	ASSERT(fdc->c_un->un_unit_no == unit);
5254 
5255 	/* Do not print errors since this is a private cmd */
5256 
5257 	oldlvl = fderrlevel;
5258 
5259 
5260 	fderrlevel = FDEP_L4;
5261 
5262 	label = (struct dk_label *)
5263 				kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);
5264 
5265 	/*
5266 	 * try different characteristics (ie densities) by attempting to read
5267 	 * from the diskette.  The diskette may not be present or
5268 	 * is unformatted.
5269 	 *
5270 	 * First, the last sector of the first track is read.  If this
5271 	 * passes, attempt to read the last sector + 1 of the first track.
5272 	 * For example, for a high density diskette, sector 18 is read.  If
5273 	 * the diskette is high density, this will pass.  Next, try to
5274 	 * read sector 19 of the first track.  This should fail.  If it
5275 	 * passes, this is not a high density diskette.  Finally, read
5276 	 * the first sector which should contain a label.
5277 	 *
5278 	 * if un->un_curfdtype is -1 then the current characteristics
5279 	 * were set by FDIOSCHAR and need to try it as well as everything
5280 	 * in the table
5281 	 */
5282 	if (un->un_curfdtype == -1) {
5283 		tries = nfdtypes+1;
5284 		FDERRPRINT(FDEP_L1, FDEM_GETL,
5285 		    (C, "fdgetl: un_curfdtype is -1\n"));
5286 
5287 	    } else {
5288 		tries = nfdtypes;
5289 
5290 		/* Always start with the highest density (1.7MB) */
5291 		un->un_curfdtype = 0;
5292 		*(un->un_chars) = fdtypes[un->un_curfdtype];
5293 	}
5294 
5295 	FDERRPRINT(FDEP_L1, FDEM_GETL,
5296 		    (C, "fdgetl: no. of tries %d\n", tries));
5297 	FDERRPRINT(FDEP_L1, FDEM_GETL,
5298 		    (C, "fdgetl: no. of curfdtype %d\n", un->un_curfdtype));
5299 
5300 	for (i = 0; i < tries; i++) {
5301 		FDERRPRINT(FDEP_L1, FDEM_GETL,
5302 		    (C, "fdgetl: trying %d\n", i));
5303 
5304 		if (!(err = fdrw(fdc, unit, FDREAD, 0, 0,
5305 			un->un_chars->fdc_secptrack, (caddr_t)label,
5306 			sizeof (struct dk_label))) &&
5307 
5308 		    fdrw(fdc, unit, FDREAD, 0, 0,
5309 			un->un_chars->fdc_secptrack + 1,
5310 			(caddr_t)label, sizeof (struct dk_label)) &&
5311 
5312 		    !(err = fdrw(fdc, unit, FDREAD, 0, 0, 1, (caddr_t)label,
5313 			sizeof (struct dk_label)))) {
5314 
5315 			FDERRPRINT(FDEP_L1, FDEM_GETL,
5316 				(C, "fdgetl: succeeded\n"));
5317 
5318 			break;
5319 		}
5320 
5321 		/*
5322 		 * try the next entry in the characteristics tbl
5323 		 * If curfdtype is -1, the nxt entry in tbl is 0 (the first).
5324 		 */
5325 
5326 		un->un_curfdtype = (un->un_curfdtype + 1) % nfdtypes;
5327 		*(un->un_chars) = fdtypes[un->un_curfdtype];
5328 
5329 
5330 	}
5331 
5332 	/* print errors again */
5333 	fderrlevel = oldlvl;
5334 
5335 	/* Couldn't read anything */
5336 	if (err) {
5337 
5338 		/* The default characteristics are high density (1.4MB) */
5339 		un->un_curfdtype = 1;
5340 		*(un->un_chars) = fdtypes[un->un_curfdtype];
5341 
5342 		fdunpacklabel(&fdlbl_high_80, &un->un_label);
5343 
5344 		FDERRPRINT(FDEP_L1, FDEM_GETL,
5345 			(C, "fdgetl: Can't autosense diskette\n"));
5346 
5347 		goto out;
5348 	}
5349 
5350 	FDERRPRINT(FDEP_L1, FDEM_GETL,
5351 	    (C, "fdgetl: fdtype=%d !!!\n", un->un_curfdtype));
5352 	FDERRPRINT(FDEP_L1, FDEM_GETL,
5353 	    (C, "fdgetl: rate=%d ssize=%d !!!\n",
5354 	    un->un_chars->fdc_transfer_rate, un->un_chars->fdc_sec_size));
5355 
5356 	/*
5357 	 * _something_ was read	 -  look for unixtype label
5358 	 */
5359 	if (label->dkl_magic != DKL_MAGIC) {
5360 
5361 		/*
5362 		 * The label isn't a unix label.  However, the diskette
5363 		 * is formatted because we were able to read the first
5364 		 * cylinder.
5365 		 */
5366 
5367 		FDERRPRINT(FDEP_L1, FDEM_GETL,
5368 		    (C, "fdgetl: not unix label\n"));
5369 
5370 		goto nolabel;
5371 	}
5372 
5373 	/*
5374 	 * Checksum the label
5375 	 */
5376 	count = sizeof (struct dk_label)/sizeof (short);
5377 	sp = (short *)label;
5378 	xsum = 0;
5379 	while (count--)
5380 		xsum ^= *sp++;	/* should add up to 0 */
5381 	if (xsum) {
5382 
5383 		/*
5384 		 * The checksum fails.  However, the diskette is formatted
5385 		 * because we were able to read the first cylinder
5386 		 */
5387 
5388 		FDERRPRINT(FDEP_L1, FDEM_GETL,
5389 		    (C, "fdgetl: bad cksum\n"));
5390 
5391 		goto nolabel;
5392 	}
5393 
5394 	/*
5395 	 * The diskette has a unix label with a correct checksum.
5396 	 * Copy the label into the unit structure
5397 	 */
5398 	un->un_label = *label;
5399 
5400 	goto out;
5401 
5402 nolabel:
5403 	/*
5404 	 * The diskette doesn't have a correct unix label, but it is formatted.
5405 	 * Use a default label according to the diskette's density
5406 	 * (mark default used)
5407 	 */
5408 	FDERRPRINT(FDEP_L1, FDEM_GETL,
5409 	    (C, "fdgetlabel: unit %d\n", unit));
5410 	un->un_flags |= FDUNIT_UNLABELED;
5411 	switch (un->un_chars->fdc_secptrack) {
5412 	case 9:
5413 		fdunpacklabel(&fdlbl_low_80, &un->un_label);
5414 		break;
5415 	case 8:
5416 		fdunpacklabel(&fdlbl_medium_80, &un->un_label);
5417 		break;
5418 	case 18:
5419 		fdunpacklabel(&fdlbl_high_80, &un->un_label);
5420 		break;
5421 	case 21:
5422 		fdunpacklabel(&fdlbl_high_21, &un->un_label);
5423 		break;
5424 	default:
5425 		fdunpacklabel(&fdlbl_high_80, &un->un_label);
5426 		break;
5427 	}
5428 
5429 out:
5430 	if (label != NULL)
5431 		kmem_free((caddr_t)label, sizeof (struct dk_label));
5432 	return (err);
5433 }
5434 
5435 /*
5436  * fdrw- used only for reading labels  and for DKIOCSVTOC ioctl
5437  *	 which reads the 1 sector.
5438  */
5439 static int
5440 fdrw(struct fdctlr *fdc, int unit, int rw, int cyl, int head,
5441     int sector, caddr_t bufp, uint_t len)
5442 {
5443 	struct fdcsb *csb;
5444 	struct	fd_char *ch;
5445 	int	cmdresult = 0;
5446 	caddr_t dma_addr;
5447 	size_t	real_length;
5448 	int	res;
5449 	ddi_device_acc_attr_t attr;
5450 	ddi_acc_handle_t	mem_handle = NULL;
5451 
5452 	FDERRPRINT(FDEP_L1, FDEM_RW, (C, "fdrw\n"));
5453 
5454 	ASSERT(fdc->c_un->un_unit_no == unit);
5455 
5456 	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
5457 
5458 	if (fdc->c_un->un_state == FD_STATE_STOPPED) {
5459 		mutex_exit(&fdc->c_lolock);
5460 		if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
5461 					!= DDI_SUCCESS) {
5462 			FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power change \
5463 failed. \n"));
5464 			mutex_enter(&fdc->c_lolock);
5465 			return (EIO);
5466 		}
5467 
5468 		mutex_enter(&fdc->c_lolock);
5469 	}
5470 
5471 	fdgetcsb(fdc);
5472 	csb = &fdc->c_csb;
5473 	ch = fdc->c_un->un_chars;
5474 	if (rw == FDREAD) {
5475 		if (fdc->c_fdtype & FDCTYPE_TCBUG) {
5476 			/*
5477 			 * kludge for lack of Multitrack functionality
5478 			 */
5479 			csb->csb_cmds[0] = SK + FDRAW_RDCMD;
5480 		} else
5481 			csb->csb_cmds[0] = MT + SK + FDRAW_RDCMD;
5482 	} else { /* write */
5483 		if (fdc->c_fdtype & FDCTYPE_TCBUG) {
5484 			/*
5485 			 * kludge for lack of Multitrack functionality
5486 			 */
5487 			csb->csb_cmds[0] = FDRAW_WRCMD;
5488 		} else
5489 			csb->csb_cmds[0] = MT + FDRAW_WRCMD;
5490 	}
5491 
5492 	if (rw == FDREAD)
5493 		fdc->c_csb.csb_read = CSB_READ;
5494 	else
5495 		fdc->c_csb.csb_read = CSB_WRITE;
5496 
5497 	/* always or in MFM bit */
5498 	csb->csb_cmds[0] |= MFM;
5499 	csb->csb_cmds[1] = (uchar_t)(unit | ((head & 0x1) << 2));
5500 	if (fdc->c_fdtype & FDCTYPE_SB)
5501 		csb->csb_cmds[1] |= IPS;
5502 	csb->csb_cmds[2] = (uchar_t)cyl;
5503 	csb->csb_cmds[3] = (uchar_t)head;
5504 	csb->csb_cmds[4] = (uchar_t)sector;
5505 	csb->csb_cmds[5] = ch->fdc_medium ? 3 : 2; /* sector size code */
5506 	/*
5507 	 * kludge for end-of-cylinder error.
5508 	 */
5509 	if (fdc->c_fdtype & FDCTYPE_TCBUG)
5510 		csb->csb_cmds[6] = sector + (len / ch->fdc_sec_size) - 1;
5511 	else
5512 		csb->csb_cmds[6] =
5513 		    (uchar_t)max(fdc->c_un->un_chars->fdc_secptrack, sector);
5514 	csb->csb_len = len;
5515 	csb->csb_cmds[7] = GPLN;
5516 	csb->csb_cmds[8] = SSSDTL;
5517 	csb->csb_ncmds = NCBRW;
5518 	csb->csb_len = len;
5519 	csb->csb_maxretry = 2;
5520 	csb->csb_retrys = 0;
5521 	bzero(csb->csb_rslt, NRBRW);
5522 	csb->csb_nrslts = NRBRW;
5523 	csb->csb_opflags = CSB_OFXFEROPS | CSB_OFTIMEIT;
5524 
5525 	/* If platform supports DMA, set up DMA resources */
5526 	if (fdc->c_fdtype & FDCTYPE_DMA) {
5527 
5528 		mutex_enter(&fdc->c_hilock);
5529 
5530 		attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
5531 		attr.devacc_attr_endian_flags  = DDI_STRUCTURE_BE_ACC;
5532 		attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
5533 
5534 		res = ddi_dma_mem_alloc(fdc->c_dmahandle, len,
5535 			&attr, DDI_DMA_STREAMING,
5536 			DDI_DMA_DONTWAIT, 0, &dma_addr, &real_length,
5537 			&mem_handle);
5538 
5539 		if (res != DDI_SUCCESS) {
5540 			FDERRPRINT(FDEP_L1, FDEM_RW,
5541 				(C, "fdrw: dma mem alloc failed\n"));
5542 
5543 			fdretcsb(fdc);
5544 			mutex_exit(&fdc->c_hilock);
5545 			return (EIO);
5546 		}
5547 
5548 		FDERRPRINT(FDEP_L1, FDEM_RW, (C, "fdrw: allocated memory"));
5549 
5550 		if (fdstart_dma(fdc, dma_addr, len) != 0) {
5551 			fdretcsb(fdc);
5552 			ddi_dma_mem_free(&mem_handle);
5553 			mutex_exit(&fdc->c_hilock);
5554 			return (-1);
5555 
5556 		}
5557 
5558 		/*
5559 		 * If the command is a write, copy the data to be written to
5560 		 * dma_addr.
5561 		 */
5562 
5563 		if (fdc->c_csb.csb_read == CSB_WRITE) {
5564 			bcopy((char *)bufp, (char *)dma_addr, len);
5565 		}
5566 
5567 		csb->csb_addr = dma_addr;
5568 		mutex_exit(&fdc->c_hilock);
5569 	} else {
5570 		csb->csb_addr = bufp;
5571 	}
5572 
5573 
5574 	FDERRPRINT(FDEP_L1, FDEM_RW, (C, "fdrw: call fdexec\n"));
5575 
5576 	if (fdexec(fdc, FDXC_SLEEP | FDXC_CHECKCHG) != 0) {
5577 		fdretcsb(fdc);
5578 
5579 		if (mem_handle)
5580 			ddi_dma_mem_free(&mem_handle);
5581 
5582 		return (EIO);
5583 
5584 	}
5585 
5586 	FDERRPRINT(FDEP_L1, FDEM_RW, (C, "fdrw: fdexec returned\n"));
5587 
5588 	/*
5589 	 * if DMA was used and the command was a read
5590 	 * copy the results into bufp
5591 	 */
5592 	if (fdc->c_fdtype & FDCTYPE_DMA) {
5593 		if (fdc->c_csb.csb_read == CSB_READ) {
5594 			bcopy((char *)dma_addr, (char *)bufp, len);
5595 		}
5596 		ddi_dma_mem_free(&mem_handle);
5597 	}
5598 
5599 	if (csb->csb_cmdstat)
5600 		cmdresult = EIO;	/* XXX TBD NYD for now */
5601 
5602 	fdretcsb(fdc);
5603 	return (cmdresult);
5604 }
5605 
5606 /*
5607  * fdunpacklabel
5608  *	this unpacks a (packed) struct dk_label into a standard dk_label.
5609  */
5610 static void
5611 fdunpacklabel(struct packed_label *from, struct dk_label *to)
5612 {
5613 	FDERRPRINT(FDEP_L1, FDEM_PACK, (C, "fdpacklabel\n"));
5614 	bzero((caddr_t)to, sizeof (*to));
5615 	bcopy((caddr_t)&from->dkl_vname, (caddr_t)to->dkl_asciilabel,
5616 	    sizeof (to->dkl_asciilabel));
5617 	to->dkl_rpm = from->dkl_rpm;	/* rotations per minute */
5618 	to->dkl_pcyl = from->dkl_pcyl;	/* # physical cylinders */
5619 	to->dkl_apc = from->dkl_apc;	/* alternates per cylinder */
5620 	to->dkl_intrlv = from->dkl_intrlv;	/* interleave factor */
5621 	to->dkl_ncyl = from->dkl_ncyl;	/* # of data cylinders */
5622 	to->dkl_acyl = from->dkl_acyl;	/* # of alternate cylinders */
5623 	to->dkl_nhead = from->dkl_nhead; /* # of heads in this partition */
5624 	to->dkl_nsect = from->dkl_nsect; /* # of 512 byte sectors per track */
5625 	/* logical partitions */
5626 	bcopy((caddr_t)from->dkl_map, (caddr_t)to->dkl_map,
5627 	    sizeof (struct dk_map32) * NDKMAP);
5628 	to->dkl_vtoc = from->dkl_vtoc;
5629 }
5630 
5631 static struct fdctlr *
5632 fd_getctlr(dev_t dev)
5633 {
5634 
5635 	struct fdctlr *fdc = fdctlrs;
5636 	int ctlr = FDCTLR(dev);
5637 
5638 	while (fdc) {
5639 		if (ddi_get_instance(fdc->c_dip) == ctlr)
5640 			return (fdc);
5641 		fdc = fdc->c_next;
5642 	}
5643 	return (fdc);
5644 }
5645 
5646 static int
5647 fd_unit_is_open(struct fdunit *un)
5648 {
5649 	int i;
5650 	for (i = 0; i < NDKMAP; i++)
5651 		if (un->un_lyropen[i])
5652 			return (1);
5653 	for (i = 0; i < OTYPCNT - 1; i++)
5654 		if (un->un_regopen[i])
5655 			return (1);
5656 	return (0);
5657 }
5658 
5659 /*
5660  * Return the a vtoc structure in *vtoc.
5661  * The vtoc is built from information in
5662  * the diskette's label.
5663  */
5664 static void
5665 fd_build_user_vtoc(struct fdunit *un, struct vtoc *vtoc)
5666 {
5667 	int i;
5668 	int nblks;			/* DEV_BSIZE sectors per cylinder */
5669 	struct dk_map2 *lpart;
5670 	struct dk_map32	*lmap;
5671 	struct partition *vpart;
5672 
5673 	bzero(vtoc, sizeof (struct vtoc));
5674 
5675 	/* Initialize info. needed by mboot.  (unsupported) */
5676 	vtoc->v_bootinfo[0] = un->un_label.dkl_vtoc.v_bootinfo[0];
5677 	vtoc->v_bootinfo[1] = un->un_label.dkl_vtoc.v_bootinfo[1];
5678 	vtoc->v_bootinfo[2] = un->un_label.dkl_vtoc.v_bootinfo[2];
5679 
5680 	/* Fill in vtoc sanity and version information */
5681 	vtoc->v_sanity		= un->un_label.dkl_vtoc.v_sanity;
5682 	vtoc->v_version		= un->un_label.dkl_vtoc.v_version;
5683 
5684 	/* Copy the volume name */
5685 	bcopy(un->un_label.dkl_vtoc.v_volume,
5686 	    vtoc->v_volume, LEN_DKL_VVOL);
5687 
5688 	/*
5689 	 * The dk_map32 structure is based on DEV_BSIZE byte blocks.
5690 	 * However, medium density diskettes have 1024 byte blocks.
5691 	 * The number of sectors per partition listed in the dk_map32 structure
5692 	 * accounts for this by multiplying the number of 1024 byte
5693 	 * blocks by 2.  (See the packed_label initializations.)  The
5694 	 * 1024 byte block size can not be listed for medium density
5695 	 * diskettes because the kernel is hard coded for DEV_BSIZE
5696 	 * blocks.
5697 	 */
5698 	vtoc->v_sectorsz = DEV_BSIZE;
5699 	vtoc->v_nparts = un->un_label.dkl_vtoc.v_nparts;
5700 
5701 	/* Copy the reserved space */
5702 	bcopy(un->un_label.dkl_vtoc.v_reserved,
5703 	    vtoc->v_reserved, sizeof (un->un_label.dkl_vtoc.v_reserved));
5704 	/*
5705 	 * Convert partitioning information.
5706 	 *
5707 	 * Note the conversion from starting cylinder number
5708 	 * to starting sector number.
5709 	 */
5710 	lmap = un->un_label.dkl_map;
5711 	lpart = un->un_label.dkl_vtoc.v_part;
5712 	vpart = vtoc->v_part;
5713 
5714 	nblks = (un->un_chars->fdc_nhead * un->un_chars->fdc_secptrack *
5715 		un->un_chars->fdc_sec_size) / DEV_BSIZE;
5716 
5717 	for (i = 0; i < V_NUMPAR; i++) {
5718 		vpart->p_tag	= lpart->p_tag;
5719 		vpart->p_flag	= lpart->p_flag;
5720 		vpart->p_start	= lmap->dkl_cylno * nblks;
5721 		vpart->p_size	= lmap->dkl_nblk;
5722 
5723 		lmap++;
5724 		lpart++;
5725 		vpart++;
5726 	}
5727 
5728 	/* Initialize timestamp and label */
5729 	bcopy(un->un_label.dkl_vtoc.v_timestamp,
5730 	    vtoc->timestamp, sizeof (vtoc->timestamp));
5731 
5732 	bcopy(un->un_label.dkl_asciilabel,
5733 	    vtoc->v_asciilabel, LEN_DKL_ASCII);
5734 }
5735 
5736 /*
5737  * Build a label out of a vtoc structure.
5738  */
5739 static int
5740 fd_build_label_vtoc(struct fdunit *un, struct vtoc *vtoc)
5741 {
5742 	struct dk_map32		*lmap;
5743 	struct dk_map2		*lpart;
5744 	struct partition	*vpart;
5745 	int			nblks;	/* no. blocks per cylinder */
5746 	int			ncyl;
5747 	int			i;
5748 	short	 sum, *sp;
5749 
5750 	/* Sanity-check the vtoc */
5751 	if ((vtoc->v_sanity != VTOC_SANE) ||
5752 			(vtoc->v_nparts > NDKMAP) || (vtoc->v_nparts <= 0)) {
5753 		FDERRPRINT(FDEP_L1, FDEM_IOCT,
5754 		    (C, "fd_build_label:  sanity check on vtoc failed\n"));
5755 		return (EINVAL);
5756 	}
5757 
5758 	nblks = (un->un_chars->fdc_nhead * un->un_chars->fdc_secptrack *
5759 		un->un_chars->fdc_sec_size) / DEV_BSIZE;
5760 
5761 	vpart = vtoc->v_part;
5762 
5763 	/*
5764 	 * Check the partition information in the vtoc.  The starting sectors
5765 	 * must lie along partition boundaries. (NDKMAP entries are checked
5766 	 * to ensure that the unused entries are set to 0 if vtoc->v_nparts
5767 	 * is less than NDKMAP)
5768 	 */
5769 
5770 	for (i = 0; i < NDKMAP; i++) {
5771 		if ((vpart->p_start % nblks) != 0) {
5772 			return (EINVAL);
5773 		}
5774 		ncyl = vpart->p_start % nblks;
5775 		ncyl += vpart->p_size % nblks;
5776 		if ((vpart->p_size % nblks) != 0)
5777 			ncyl++;
5778 		if (ncyl > un->un_chars->fdc_ncyl) {
5779 			return (EINVAL);
5780 		}
5781 		vpart++;
5782 	}
5783 
5784 	/*
5785 	 * reinitialize the existing label
5786 	 */
5787 	bzero(&un->un_label, sizeof (un->un_label));
5788 
5789 	/* Put appropriate vtoc structure fields into the disk label */
5790 	un->un_label.dkl_vtoc.v_bootinfo[0] = (uint32_t)vtoc->v_bootinfo[0];
5791 	un->un_label.dkl_vtoc.v_bootinfo[1] = (uint32_t)vtoc->v_bootinfo[1];
5792 	un->un_label.dkl_vtoc.v_bootinfo[2] = (uint32_t)vtoc->v_bootinfo[2];
5793 
5794 	un->un_label.dkl_vtoc.v_sanity = vtoc->v_sanity;
5795 	un->un_label.dkl_vtoc.v_version = vtoc->v_version;
5796 
5797 	bcopy(vtoc->v_volume, un->un_label.dkl_vtoc.v_volume, LEN_DKL_VVOL);
5798 
5799 	un->un_label.dkl_vtoc.v_nparts = vtoc->v_nparts;
5800 
5801 	bcopy(vtoc->v_reserved, un->un_label.dkl_vtoc.v_reserved,
5802 	    sizeof (un->un_label.dkl_vtoc.v_reserved));
5803 
5804 	/*
5805 	 * Initialize cylinder information in the label.
5806 	 * Note the conversion from starting sector number
5807 	 * to starting cylinder number.
5808 	 * Return error if division results in a remainder.
5809 	 */
5810 	lmap = un->un_label.dkl_map;
5811 	lpart = un->un_label.dkl_vtoc.v_part;
5812 	vpart = vtoc->v_part;
5813 
5814 	for (i = 0; i < (int)vtoc->v_nparts; i++) {
5815 		lpart->p_tag  = vtoc->v_part[i].p_tag;
5816 		lpart->p_flag = vtoc->v_part[i].p_flag;
5817 		lmap->dkl_cylno = vpart->p_start / nblks;
5818 		lmap->dkl_nblk = vpart->p_size;
5819 
5820 		lmap++;
5821 		lpart++;
5822 		vpart++;
5823 	}
5824 
5825 	/* Copy the timestamp and ascii label */
5826 	for (i = 0; i < NDKMAP; i++) {
5827 		un->un_label.dkl_vtoc.v_timestamp[i] = vtoc->timestamp[i];
5828 	}
5829 
5830 
5831 	bcopy(vtoc->v_asciilabel, un->un_label.dkl_asciilabel, LEN_DKL_ASCII);
5832 
5833 	FDERRPRINT(FDEP_L1, FDEM_IOCT,
5834 		    (C, "fd_build_label: asciilabel %s\n",
5835 			un->un_label.dkl_asciilabel));
5836 
5837 	/* Initialize the magic number */
5838 	un->un_label.dkl_magic = DKL_MAGIC;
5839 
5840 	un->un_label.dkl_pcyl = un->un_chars->fdc_ncyl;
5841 
5842 	/*
5843 	 * The fdc_secptrack filed of the fd_char structure is the number
5844 	 * of sectors per track where the sectors are fdc_sec_size.  The
5845 	 * dkl_nsect field of the dk_label structure is the number of
5846 	 * 512 (DEVBSIZE) byte sectors per track.
5847 	 */
5848 	un->un_label.dkl_nsect = (un->un_chars->fdc_secptrack *
5849 				un->un_chars->fdc_sec_size) / DEV_BSIZE;
5850 
5851 
5852 	un->un_label.dkl_ncyl = un->un_label.dkl_pcyl;
5853 	un->un_label.dkl_nhead = un->un_chars->fdc_nhead;
5854 	un->un_label.dkl_rpm = un->un_chars->fdc_medium ? 360 : 300;
5855 	un->un_label.dkl_intrlv = 1;
5856 
5857 	/* Create the checksum */
5858 	sum = 0;
5859 	un->un_label.dkl_cksum = 0;
5860 	sp = (short *)&un->un_label;
5861 	i = sizeof (struct dk_label)/sizeof (short);
5862 	while (i--) {
5863 		sum ^= *sp++;
5864 	}
5865 	un->un_label.dkl_cksum = sum;
5866 
5867 	return (0);
5868 }
5869 
5870 /*
5871  * Check for auxio register node
5872  */
5873 
5874 int
5875 fd_isauxiodip(dev_info_t *dip)
5876 {
5877 	if (strcmp(ddi_get_name(dip), "auxio") == 0 ||
5878 	    strcmp(ddi_get_name(dip), "auxiliary-io") == 0) {
5879 		return (1);
5880 	}
5881 	return (0);
5882 }
5883 
5884 /*
5885  * Search for auxio register node, then for address property
5886  */
5887 
5888 caddr_t
5889 fd_getauxiova(dev_info_t *dip)
5890 {
5891 	dev_info_t *auxdip;
5892 	caddr_t addr;
5893 
5894 	/*
5895 	 * Search sibling list, which happens to be safe inside attach
5896 	 */
5897 	auxdip = ddi_get_child(ddi_get_parent(dip));
5898 	while (auxdip) {
5899 		if (fd_isauxiodip(auxdip))
5900 			break;
5901 		auxdip = ddi_get_next_sibling(auxdip);
5902 	}
5903 
5904 	if (auxdip == NULL)
5905 		return (NULL);
5906 
5907 	addr = (caddr_t)(uintptr_t)(caddr32_t)ddi_getprop(DDI_DEV_T_ANY,
5908 		auxdip, DDI_PROP_DONTPASS, "address", 0);
5909 
5910 	return (addr);
5911 }
5912 
5913 
5914 /*
5915  * set_rotational speed
5916  * 300 rpm for high and low density.
5917  * 360 rpm for medium density.
5918  * for now, we assume that 3rd density is supported only for Sun4M,
5919  * not for Clones. (else we would have to check for 82077, and do
5920  * specific things for the MEDIUM_DENSITY BIT for clones.
5921  * this code should not break CLONES.
5922  *
5923  * REMARK: there is a SOny requirement, to deselect the drive then
5924  * select it again after the medium density change, since the
5925  * leading edge of the select line latches the rotational Speed.
5926  * then after that, we have to wait 500 ms for the rotation to
5927  * stabilize.
5928  *
5929  */
5930 static void
5931 set_rotational_speed(struct fdctlr *fdc, int unit)
5932 {
5933 	int check;
5934 	int is_medium;
5935 
5936 	ASSERT(fdc->c_un->un_unit_no == unit);
5937 
5938 	/*
5939 	 * if we do not have a Sun4m, medium density is not supported.
5940 	 */
5941 	if (fdc->c_fdtype & FDCTYPE_MACHIO)
5942 		return;
5943 
5944 	/*
5945 	 * if FDUNIT_SET_SPEED is set, set the speed.
5946 	 * else,
5947 	 *	if there is a change, do it, if not leave it alone.
5948 	 *	there is a change if un->un_chars->fdc_medium does not match
5949 	 *	un->un_flags & FDUNIT_MEDIUM
5950 	 *	un->un_flags & FDUNIT_MEDIUM specifies the last setting.
5951 	 *	un->un_chars->fdc_medium specifies next setting.
5952 	 *	if there is a change, wait 500ms according to Sony spec.
5953 	 */
5954 
5955 	is_medium = fdc->c_un->un_chars->fdc_medium;
5956 
5957 	if (fdc->c_un->un_flags & FDUNIT_SET_SPEED) {
5958 		check = 1;
5959 	} else {
5960 		check = is_medium ^
5961 			((fdc->c_un->un_flags & FDUNIT_MEDIUM) ? 1 : 0);
5962 
5963 		/* Set the un_flags if necessary */
5964 
5965 		if (check)
5966 			fdc->c_un->un_flags ^= FDUNIT_MEDIUM;
5967 	}
5968 
5969 	fdc->c_un->un_flags &= ~FDUNIT_SET_SPEED;
5970 
5971 
5972 	if (check) {
5973 
5974 		fdselect(fdc, unit, 0);
5975 		drv_usecwait(5);
5976 
5977 		if ((fdc->c_fdtype & FDCTYPE_AUXIOMASK) == FDCTYPE_SLAVIO) {
5978 			Set_dor(fdc, MEDIUM_DENSITY, is_medium);
5979 		}
5980 
5981 		if ((fdc->c_fdtype & FDCTYPE_AUXIOMASK) == FDCTYPE_CHEERIO) {
5982 			if (is_medium) {
5983 				Set_auxio(fdc, AUX_MEDIUM_DENSITY);
5984 			} else {
5985 				Set_auxio(fdc, AUX_HIGH_DENSITY);
5986 			}
5987 
5988 		}
5989 
5990 		if (is_medium) {
5991 			drv_usecwait(5);
5992 		}
5993 
5994 		fdselect(fdc, unit, 1);	/* Sony requirement */
5995 		FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "rotation:medium\n"));
5996 		drv_usecwait(500000);
5997 	}
5998 }
5999 
6000 static void
6001 fd_media_watch(void *arg)
6002 {
6003 	dev_t		dev;
6004 	struct fdunit *un;
6005 	struct fdctlr *fdc;
6006 	int		unit;
6007 
6008 	dev = (dev_t)arg;
6009 	fdc = fd_getctlr(dev);
6010 	unit = fdc->c_un->un_unit_no;
6011 	un = fdc->c_un;
6012 
6013 	mutex_enter(&fdc->c_lolock);
6014 
6015 	if (un->un_media_timeout_id == 0) {
6016 		/*
6017 		 * Untimeout is about to be called.
6018 		 * Don't call fd_get_media_state again
6019 		 */
6020 		mutex_exit(&fdc->c_lolock);
6021 		return;
6022 	}
6023 
6024 
6025 	un->un_media_state = fd_get_media_state(fdc, unit);
6026 	cv_broadcast(&fdc->c_statecv);
6027 
6028 	mutex_exit(&fdc->c_lolock);
6029 
6030 	if (un->un_media_timeout) {
6031 		un->un_media_timeout_id = timeout(fd_media_watch,
6032 			(void *)(ulong_t)dev, un->un_media_timeout);
6033 	}
6034 }
6035 
6036 enum dkio_state
6037 fd_get_media_state(struct fdctlr *fdc, int unit)
6038 {
6039 	enum dkio_state state;
6040 
6041 	ASSERT(fdc->c_un->un_unit_no == unit);
6042 
6043 	if (fdsense_chng(fdc, unit)) {
6044 		/* check disk only if DSKCHG "high" */
6045 		if (fdcheckdisk(fdc, unit)) {
6046 			state = DKIO_EJECTED;
6047 		} else {
6048 			state = DKIO_INSERTED;
6049 		}
6050 	} else {
6051 		state = DKIO_INSERTED;
6052 	}
6053 	return (state);
6054 }
6055 
6056 static int
6057 fd_check_media(dev_t dev, enum dkio_state state)
6058 {
6059 	struct fdunit *un;
6060 	struct fdctlr *fdc;
6061 	int		unit;
6062 
6063 	FDERRPRINT(FDEP_L1, FDEM_RW, (C, "fd_check_media: start\n"));
6064 
6065 	fdc = fd_getctlr(dev);
6066 	unit = fdc->c_un->un_unit_no;
6067 	un = fdc->c_un;
6068 
6069 	mutex_enter(&fdc->c_lolock);
6070 
6071 	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
6072 
6073 	if (fdc->c_un->un_state == FD_STATE_STOPPED) {
6074 		mutex_exit(&fdc->c_lolock);
6075 		if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
6076 					!= DDI_SUCCESS) {
6077 			FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power change \
6078 failed. \n"));
6079 
6080 			(void) pm_idle_component(fdc->c_dip, 0);
6081 			return (EIO);
6082 		}
6083 
6084 		mutex_enter(&fdc->c_lolock);
6085 	}
6086 
6087 	un->un_media_state = fd_get_media_state(fdc, unit);
6088 
6089 	/* turn on timeout */
6090 	un->un_media_timeout = drv_usectohz(fd_check_media_time);
6091 	un->un_media_timeout_id = timeout(fd_media_watch,
6092 			(void *)(ulong_t)dev, un->un_media_timeout);
6093 
6094 	while (un->un_media_state == state) {
6095 		if (cv_wait_sig(&fdc->c_statecv, &fdc->c_lolock) == 0) {
6096 			un->un_media_timeout = 0;
6097 			mutex_exit(&fdc->c_lolock);
6098 			return (EINTR);
6099 		}
6100 	}
6101 
6102 	if (un->un_media_timeout_id) {
6103 		timeout_id_t timeid = un->un_media_timeout_id;
6104 		un->un_media_timeout_id = 0;
6105 
6106 		mutex_exit(&fdc->c_lolock);
6107 		(void) untimeout(timeid);
6108 		mutex_enter(&fdc->c_lolock);
6109 	}
6110 
6111 	if (un->un_media_state == DKIO_INSERTED) {
6112 		if (fdgetlabel(fdc, unit)) {
6113 			mutex_exit(&fdc->c_lolock);
6114 			return (EIO);
6115 		}
6116 	}
6117 	mutex_exit(&fdc->c_lolock);
6118 
6119 	FDERRPRINT(FDEP_L1, FDEM_RW, (C, "fd_check_media: end\n"));
6120 	return (0);
6121 }
6122 
6123 /*
6124  * fd_get_media_info :
6125  * 	Collects medium information for
6126  *	DKIOCGMEDIAINFO ioctl.
6127  */
6128 
6129 static int
6130 fd_get_media_info(struct fdunit *un, caddr_t buf, int flag)
6131 {
6132 	struct dk_minfo media_info;
6133 	int err = 0;
6134 
6135 	media_info.dki_media_type = DK_FLOPPY;
6136 	media_info.dki_lbsize = un->un_chars->fdc_sec_size;
6137 	media_info.dki_capacity = un->un_chars->fdc_ncyl *
6138 		un->un_chars->fdc_secptrack * un->un_chars->fdc_nhead;
6139 
6140 	if (ddi_copyout((caddr_t)&media_info, buf,
6141 					sizeof (struct dk_minfo), flag))
6142 			err = EFAULT;
6143 	return (err);
6144 }
6145 
6146 /*
6147  * fd_power :
6148  *	Power entry point of fd driver.
6149  */
6150 
6151 static int
6152 fd_power(dev_info_t *dip, int component, int level)
6153 {
6154 
6155 	struct fdctlr *fdc;
6156 	int instance;
6157 	int rval;
6158 
6159 	if ((level < PM_LEVEL_OFF) || (level > PM_LEVEL_ON) ||
6160 						(component != 0)) {
6161 		return (DDI_FAILURE);
6162 	}
6163 
6164 	instance = ddi_get_instance(dip);
6165 	fdc = fd_getctlr(instance << FDINSTSHIFT);
6166 	if (fdc->c_un == NULL)
6167 		return (DDI_FAILURE);
6168 
6169 	if (level == PM_LEVEL_OFF) {
6170 		rval = fd_pm_lower_power(fdc);
6171 	}
6172 	if (level == PM_LEVEL_ON) {
6173 		rval = fd_pm_raise_power(fdc);
6174 	}
6175 	return (rval);
6176 }
6177 
6178 /*
6179  * fd_pm_lower_power :
6180  *	This function is called only during pm suspend. At this point,
6181  *	the power management framework thinks the device is idle for
6182  *	long enough to go to a low power mode. If the device is busy,
6183  *	then this function returns DDI_FAILURE.
6184  */
6185 
6186 static int
6187 fd_pm_lower_power(struct fdctlr *fdc)
6188 {
6189 
6190 	mutex_enter(&fdc->c_lolock);
6191 
6192 	if ((fdc->c_un->un_state == FD_STATE_SUSPENDED) ||
6193 			(fdc->c_un->un_state == FD_STATE_STOPPED)) {
6194 		mutex_exit(&fdc->c_lolock);
6195 		return (DDI_SUCCESS);
6196 	}
6197 
6198 
6199 	FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "fd_pm_lower_power called\n"));
6200 
6201 	/* if the device is busy then we fail the lower power request */
6202 	if (fdc->c_flags & FDCFLG_BUSY) {
6203 		FDERRPRINT(FDEP_L2, FDEM_PWR, (C, "fd_pm_lower_power : \
6204 controller is busy.\n"));
6205 		mutex_exit(&fdc->c_lolock);
6206 		return (DDI_FAILURE);
6207 	}
6208 
6209 	fdc->c_un->un_state = FD_STATE_STOPPED;
6210 
6211 	mutex_exit(&fdc->c_lolock);
6212 	return (DDI_SUCCESS);
6213 }
6214 
6215 /*
6216  * fd_pm_raise_power :
6217  *	This function performs the necessary steps for resuming a
6218  *	device, either from pm suspend or CPR. Here the controller
6219  *	is reset, initialized and the state is set to FD_STATE_NORMAL.
6220  */
6221 
6222 static int
6223 fd_pm_raise_power(struct fdctlr *fdc)
6224 {
6225 
6226 	struct fdunit *un = fdc->c_un;
6227 	int unit;
6228 
6229 	FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "fd_pm_raise_power called\n"));
6230 	mutex_enter(&fdc->c_lolock);
6231 	fdgetcsb(fdc);
6232 
6233 	/* Reset the dma engine */
6234 	if (fdc->c_fdtype & FDCTYPE_DMA) {
6235 		mutex_enter(&fdc->c_hilock);
6236 		reset_dma_controller(fdc);
6237 		set_dma_control_register(fdc, DCSR_INIT_BITS);
6238 		mutex_exit(&fdc->c_hilock);
6239 	}
6240 
6241 	/*
6242 	 * Force a rotational speed set in the next
6243 	 * call to set_rotational_speed().
6244 	 */
6245 
6246 	fdc->c_un->un_flags |= FDUNIT_SET_SPEED;
6247 
6248 	/* Reset and configure the controller */
6249 	(void) fdreset(fdc);
6250 
6251 	unit = fdc->c_un->un_unit_no;
6252 
6253 	/* Recalibrate the drive */
6254 	if (fdrecalseek(fdc, unit, -1, 0) != 0) {
6255 		FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "raise_power : recalibrate \
6256 failed\n"));
6257 		fdretcsb(fdc);
6258 		mutex_exit(&fdc->c_lolock);
6259 		return (DDI_FAILURE);
6260 	}
6261 
6262 	/* Select the drive through the AUXIO registers */
6263 	fdselect(fdc, unit, 0);
6264 	un->un_state = FD_STATE_NORMAL;
6265 	fdretcsb(fdc);
6266 	mutex_exit(&fdc->c_lolock);
6267 	return (DDI_SUCCESS);
6268 }
6269 
6270 /*
6271  * create_pm_components :
6272  *	creates the power management components for auto pm framework.
6273  */
6274 
6275 static void
6276 create_pm_components(dev_info_t *dip)
6277 {
6278 	char	*un_pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on"};
6279 
6280 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, dip,
6281 		"pm-components", un_pm_comp, 3) == DDI_PROP_SUCCESS) {
6282 
6283 		(void) pm_raise_power(dip, 0, PM_LEVEL_ON);
6284 	}
6285 }
6286 
6287 /*
6288  * set_data_count_register(struct fdctlr *fdc, uint32_t count)
6289  * 	Set the data count in appropriate dma register.
6290  */
6291 
6292 static void
6293 set_data_count_register(struct fdctlr *fdc, uint32_t count)
6294 {
6295 	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
6296 		struct cheerio_dma_reg *dma_reg;
6297 		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
6298 		ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dbcr, count);
6299 	} else if (fdc->c_fdtype & FDCTYPE_SB) {
6300 		struct sb_dma_reg *dma_reg;
6301 		count = count - 1; /* 8237 needs it */
6302 		dma_reg = (struct sb_dma_reg *)fdc->c_dma_regs;
6303 		switch (fdc->sb_dma_channel) {
6304 			case 0 :
6305 				ddi_put16(fdc->c_handlep_dma,
6306 				(ushort_t *)&dma_reg->sb_dma_regs[DMA_0WCNT],
6307 				count & 0xFFFF);
6308 				break;
6309 			case 1 :
6310 				ddi_put16(fdc->c_handlep_dma,
6311 				(ushort_t *)&dma_reg->sb_dma_regs[DMA_1WCNT],
6312 				count & 0xFFFF);
6313 				break;
6314 			case 2 :
6315 				ddi_put16(fdc->c_handlep_dma,
6316 				(ushort_t *)&dma_reg->sb_dma_regs[DMA_2WCNT],
6317 				count & 0xFFFF);
6318 				break;
6319 			case 3 :
6320 				ddi_put16(fdc->c_handlep_dma,
6321 				(ushort_t *)&dma_reg->sb_dma_regs[DMA_3WCNT],
6322 				count & 0xFFFF);
6323 				break;
6324 			default :
6325 				FDERRPRINT(FDEP_L3, FDEM_SDMA,
6326 				(C, "set_data_count: wrong channel %x\n",
6327 				fdc->sb_dma_channel));
6328 				break;
6329 		}
6330 	}
6331 }
6332 
6333 /*
6334  * get_data_count_register(struct fdctlr *fdc)
6335  * 	Read the data count from appropriate dma register.
6336  */
6337 
6338 static uint32_t
6339 get_data_count_register(struct fdctlr *fdc)
6340 {
6341 	uint32_t retval = 0;
6342 	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
6343 		struct cheerio_dma_reg *dma_reg;
6344 		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
6345 		retval = ddi_get32(fdc->c_handlep_dma, &dma_reg->fdc_dbcr);
6346 	} else if (fdc->c_fdtype & FDCTYPE_SB) {
6347 		struct sb_dma_reg *dma_reg;
6348 		dma_reg = (struct sb_dma_reg *)fdc->c_dma_regs;
6349 		switch (fdc->sb_dma_channel) {
6350 			case 0 :
6351 				retval = ddi_get16(fdc->c_handlep_dma,
6352 				(ushort_t *)&dma_reg->sb_dma_regs[DMA_0WCNT]);
6353 				break;
6354 			case 1 :
6355 				retval = ddi_get16(fdc->c_handlep_dma,
6356 				(ushort_t *)&dma_reg->sb_dma_regs[DMA_1WCNT]);
6357 				break;
6358 			case 2 :
6359 				retval = ddi_get16(fdc->c_handlep_dma,
6360 				(ushort_t *)&dma_reg->sb_dma_regs[DMA_2WCNT]);
6361 				break;
6362 			case 3 :
6363 				retval = ddi_get16(fdc->c_handlep_dma,
6364 				(ushort_t *)&dma_reg->sb_dma_regs[DMA_3WCNT]);
6365 				break;
6366 			default :
6367 				FDERRPRINT(FDEP_L3, FDEM_SDMA,
6368 				(C, "get_data_count: wrong channel %x\n",
6369 				fdc->sb_dma_channel));
6370 				break;
6371 		}
6372 		retval = (uint32_t)((uint16_t)(retval +1));
6373 	}
6374 
6375 	return (retval);
6376 
6377 }
6378 
6379 /*
6380  * reset_dma_controller(struct fdctlr *fdc)
6381  * 	Reset and initialize the dma controller.
6382  */
6383 
6384 static void
6385 reset_dma_controller(struct fdctlr *fdc)
6386 {
6387 	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
6388 		struct cheerio_dma_reg *dma_reg;
6389 		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
6390 		ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dcsr, DCSR_RESET);
6391 		while (get_dma_control_register(fdc) & DCSR_CYC_PEND);
6392 		ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dcsr, 0);
6393 	} else if (fdc->c_fdtype & FDCTYPE_SB) {
6394 		struct sb_dma_reg *dma_reg;
6395 		dma_reg = (struct sb_dma_reg *)fdc->c_dma_regs;
6396 		ddi_put8(fdc->c_handlep_dma, &dma_reg->sb_dma_regs[DMAC1_MASK],
6397 		    (fdc->sb_dma_channel & 0x3));
6398 
6399 	}
6400 }
6401 
6402 /*
6403  * Get the DMA control register for CHEERIO.
6404  * For SouthBridge 8237 DMA controller, this register is not valid.
6405  * So, just return 0.
6406  */
6407 static uint32_t
6408 get_dma_control_register(struct fdctlr *fdc)
6409 {
6410 	uint32_t retval = 0;
6411 	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
6412 		struct cheerio_dma_reg *dma_reg;
6413 		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
6414 		retval = ddi_get32(fdc->c_handlep_dma, &dma_reg->fdc_dcsr);
6415 	}
6416 
6417 	return (retval);
6418 }
6419 
6420 
6421 /*
6422  * set_data_address_register(struct fdctlr *fdc)
6423  * 	Set the data address in appropriate dma register.
6424  */
6425 static void
6426 set_data_address_register(struct fdctlr *fdc, uint32_t address)
6427 {
6428 	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
6429 		struct cheerio_dma_reg *dma_reg;
6430 		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
6431 		ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dacr, address);
6432 	} else if (fdc->c_fdtype & FDCTYPE_SB) {
6433 		struct sb_dma_reg *dma_reg;
6434 		dma_reg = (struct sb_dma_reg *)fdc->c_dma_regs;
6435 		switch (fdc->sb_dma_channel) {
6436 			case 0 :
6437 				ddi_put8(fdc->c_handlep_dma,
6438 				    &dma_reg->sb_dma_regs[DMA_0PAGE],
6439 				    (address & 0xFF0000) >>16);
6440 				ddi_put8(fdc->c_handlep_dma,
6441 				    &dma_reg->sb_dma_regs[DMA_0HPG],
6442 				    (address & 0xFF000000) >>24);
6443 				ddi_put16(fdc->c_handlep_dma,
6444 				    (ushort_t *)&dma_reg->sb_dma_regs[DMA_0ADR],
6445 				    address & 0xFFFF);
6446 				break;
6447 			case 1 :
6448 				ddi_put8(fdc->c_handlep_dma,
6449 				    &dma_reg->sb_dma_regs[DMA_1PAGE],
6450 				    (address & 0xFF0000) >>16);
6451 				ddi_put8(fdc->c_handlep_dma,
6452 				    &dma_reg->sb_dma_regs[DMA_1HPG],
6453 				    (address & 0xFF000000) >>24);
6454 				ddi_put16(fdc->c_handlep_dma,
6455 				    (ushort_t *)&dma_reg->sb_dma_regs[DMA_1ADR],
6456 				    address & 0xFFFF);
6457 				break;
6458 			case 2 :
6459 				ddi_put8(fdc->c_handlep_dma,
6460 				    &dma_reg->sb_dma_regs[DMA_2PAGE],
6461 				    (address & 0xFF0000) >>16);
6462 				ddi_put8(fdc->c_handlep_dma,
6463 				    &dma_reg->sb_dma_regs[DMA_2HPG],
6464 				    (address & 0xFF000000) >>24);
6465 				ddi_put16(fdc->c_handlep_dma,
6466 				    (ushort_t *)&dma_reg->sb_dma_regs[DMA_2ADR],
6467 				    address & 0xFFFF);
6468 				break;
6469 			case 3 :
6470 				ddi_put8(fdc->c_handlep_dma,
6471 				    &dma_reg->sb_dma_regs[DMA_3PAGE],
6472 				    (address & 0xFF0000) >>16);
6473 				ddi_put8(fdc->c_handlep_dma,
6474 				    &dma_reg->sb_dma_regs[DMA_3HPG],
6475 				    (address & 0xFF000000) >>24);
6476 				ddi_put16(fdc->c_handlep_dma,
6477 				    (ushort_t *)&dma_reg->sb_dma_regs[DMA_3ADR],
6478 				    address & 0xFFFF);
6479 				break;
6480 			default :
6481 				FDERRPRINT(FDEP_L3, FDEM_SDMA,
6482 				(C, "set_data_address: wrong channel %x\n",
6483 				fdc->sb_dma_channel));
6484 			break;
6485 		}
6486 	}
6487 
6488 }
6489 
6490 
6491 /*
6492  * set_dma_mode(struct fdctlr *fdc, int val)
6493  * 	Set the appropriate dma direction and registers.
6494  */
6495 static void
6496 set_dma_mode(struct fdctlr *fdc, int val)
6497 {
6498 	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
6499 		struct cheerio_dma_reg *dma_reg;
6500 		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
6501 		if (val == CSB_READ)
6502 			ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dcsr,
6503 				DCSR_INIT_BITS|DCSR_WRITE);
6504 		else
6505 			ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dcsr,
6506 				DCSR_INIT_BITS);
6507 
6508 	} else if (fdc->c_fdtype & FDCTYPE_SB) {
6509 		uint8_t mode_reg_val, chn_mask;
6510 		struct sb_dma_reg *dma_reg;
6511 		dma_reg = (struct sb_dma_reg *)fdc->c_dma_regs;
6512 
6513 		if (val == CSB_READ) {
6514 			mode_reg_val = fdc->sb_dma_channel | DMAMODE_READ
6515 					| DMAMODE_SINGLE;
6516 		} else { /* Read operation */
6517 			mode_reg_val = fdc->sb_dma_channel | DMAMODE_WRITE
6518 					| DMAMODE_SINGLE;
6519 		}
6520 		ddi_put8(fdc->c_handlep_dma, &dma_reg->sb_dma_regs[DMAC1_MODE],
6521 			mode_reg_val);
6522 		chn_mask = 1 << (fdc->sb_dma_channel & 0x3);
6523 		ddi_put8(fdc->c_handlep_dma,
6524 			&dma_reg->sb_dma_regs[DMAC1_ALLMASK], ~chn_mask);
6525 		fdc->sb_dma_lock = 1;
6526 	}
6527 }
6528 
6529 /*
6530  * This function is valid only for CHEERIO/RIO based
6531  * controllers. The control register for the dma channel
6532  * is initialized by this function.
6533  */
6534 
6535 static void
6536 set_dma_control_register(struct fdctlr *fdc, uint32_t val)
6537 {
6538 	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
6539 		struct cheerio_dma_reg *dma_reg;
6540 		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
6541 		ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dcsr, val);
6542 	}
6543 }
6544 
6545 static void
6546 release_sb_dma(struct fdctlr *fdc)
6547 {
6548 	struct sb_dma_reg *dma_reg;
6549 	dma_reg = (struct sb_dma_reg *)fdc->c_dma_regs;
6550 	/* Unmask all the channels to release the DMA controller */
6551 	ddi_put8(fdc->c_handlep_dma,
6552 		&dma_reg->sb_dma_regs[DMAC1_ALLMASK], NULL);
6553 	fdc->sb_dma_lock = 0;
6554 }
6555 
6556 static void
6557 quiesce_fd_interrupt(struct fdctlr *fdc)
6558 {
6559 	/*
6560 	 * The following code is put here to take care of HW problem.
6561 	 * The HW problem is as follows:
6562 	 *
6563 	 *	After poweron the Southbridge floppy controller asserts the
6564 	 * interrupt in tristate. This causes continuous interrupts to
6565 	 * be generated.
6566 	 * Until the Hardware is FIXED we will have to use the following code
6567 	 * to set the interrupt line to proper state after poweron.
6568 	 */
6569 	if (fdc->c_fdtype & FDCTYPE_SB) {
6570 		ddi_put8(fdc->c_handlep_cont, ((uint8_t *)fdc->c_dor),
6571 			0x0);
6572 		drv_usecwait(200);
6573 		ddi_put8(fdc->c_handlep_cont, ((uint8_t *)fdc->c_dor),
6574 			0xC);
6575 		drv_usecwait(200);
6576 		Set_Fifo(fdc, 0xE6);
6577 		drv_usecwait(200);
6578 	}
6579 }
6580