xref: /illumos-gate/usr/src/uts/sun/io/fd.c (revision 52c78306)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2016 by Delphix. All rights reserved.
24  * Copyright 2019 Peter Tribble.
25  */
26 
27 
28 /*
29  * Intel 82077 Floppy Disk Driver
30  */
31 
32 /*
33  * Notes
34  *
35  *	0. The driver supports two flavors of hardware design:
36  *		"SUNW,fdtwo"	- sun4m	- 82077 with sun4m style Auxio
37  *		"fdthree"  - sun4u - 82077 with DMA
38  *	   In addition it supports an apparent bug in some versions of
39  *	   the 82077 controller.
40  *
41  *	1. The driver is mostly set up for multiple controllers, multiple
42  *	drives. However- we *do* assume the use of the AUXIO register, and
43  *	if we ever have > 1 fdc, we'll have to see what that means. This
44  *	is all intrinsically machine specific, but there isn't much we
45  *	can do about it.
46  *
47  *	2. The driver also is structured to deal with one drive active at
48  *	a time. This is because the 82072 chip (no longer supported) was
49  *	known to be buggy with respect to overlapped seeks.
50  *
51  *	3. The high level interrupt code is in assembler, and runs in a
52  *	sparc trap window. It acts as a pseudo-dma engine as well as
53  *	handles a couple of other interrupts. When it gets its job done,
54  *	it schedules a second stage interrupt (soft interrupt) which
55  *	is then fielded here in fd_lointr.  When DMA is used, the fdintr_dma
56  *	interrupt handler is used.
57  *
58  *	4. Nearly all locking is done on a lower level MUTEX_DRIVER
59  *	mutex. The locking is quite conservative, and is generally
60  *	established very close to any of the entries into the driver.
61  *	There is nearly no locking done of the high level MUTEX_DRIVER
62  *	mutex (which generally is a SPIN mutex because the floppy usually
63  *	interrupts above LOCK_LEVEL). The assembler high level interrupt
64  *	handler grabs the high level mutex, but the code in the driver
65  *	here is especially structured to not need to do this.
66  *
67  *	5. Fdrawioctl commands that pass data are not optimized for
68  *	speed. If they need to be faster, the driver structure will
69  *	have to be redone such that fdrawioctl calls physio after
70  *	cons'ing up a uio structure and that fdstart will be able
71  *	to detect that a particular buffer is a 'special' buffer.
72  *
73  *	6. Removable media support is not complete.
74  *
75  */
76 
77 #include <sys/param.h>
78 #include <sys/buf.h>
79 #include <sys/ioctl.h>
80 #include <sys/uio.h>
81 #include <sys/open.h>
82 #include <sys/conf.h>
83 #include <sys/file.h>
84 #include <sys/cmn_err.h>
85 #include <sys/debug.h>
86 #include <sys/kmem.h>
87 #include <sys/stat.h>
88 #include <sys/autoconf.h>
89 
90 #include <sys/dklabel.h>
91 
92 #include <sys/vtoc.h>
93 #include <sys/dkio.h>
94 #include <sys/fdio.h>
95 
96 #include <sys/ddi.h>
97 #include <sys/sunddi.h>
98 #include <sys/kstat.h>
99 
100 /*
101  * included to check for ELC or SLC which report floppy controller that
102  */
103 #include <sys/cpu.h>
104 
105 #include "sys/fdvar.h"
106 #include "sys/fdreg.h"
107 #include "sys/dma_i8237A.h"
108 
109 /*
110  * Defines
111  */
112 #define	KIOSP	KSTAT_IO_PTR(un->un_iostat)
113 #define	KIOIP	KSTAT_INTR_PTR(fdc->c_intrstat)
114 #define	MEDIUM_DENSITY	0x40
115 #define	SEC_SIZE_CODE	(fdctlr.c_csb->csb_unit]->un_chars->medium ? 3 : 2)
116 #define	CMD_READ	(MT + SK + FDRAW_RDCMD + MFM)
117 #define	CMD_WRITE	(MT + FDRAW_WRCMD + MFM)
118 #define	C		CE_CONT
119 #define	FD_POLLABLE_PROP	"pollable"	/* prom property */
120 #define	FD_MANUAL_EJECT		"manual"	/* prom property */
121 #define	FD_UNIT			"unit"		/* prom property */
122 
123 /*
124  * Sony MP-F17W-50D Drive Parameters
125  *				High Capacity
126  *	Capacity unformatted	2Mb
127  *	Capacity formatted	1.47Mb
128  *	Encoding method	 MFM
129  *	Recording density	17434 bpi
130  *	Track density		135 tpi
131  *	Cylinders		80
132  *	Heads			2
133  *	Tracks			160
134  *	Rotational speed	300 rpm
135  *	Transfer rate		250/500 kbps
136  *	Latency (average)	100 ms
137  *	Access time
138  *		Average		95 ms
139  *		Track to track	3 ms
140  *	Head settling time	15 ms
141  *	Motor start time	500 ms
142  *	Head load time		? ms
143  */
144 
145 /*
146  * The max_fd_dma_len is used only when southbridge is present.
147  * It has been observed that when IFB tests are run the floppy dma could get
148  * starved and result in underrun errors. After experimenting it was found that
149  * doing dma in chunks of 2048 works OK.
150  * The reason for making this a global variable is that there could be
151  * situations under which the customer would like to get full performance
152  * from floppy. They may not be having IFB boards that cause underrun errors.
153  * Under those conditions we could set this value to a much higher value
154  * by editing /etc/system file.
155  */
156 int	max_fd_dma_len = 2048;
157 
158 static void quiesce_fd_interrupt(struct fdctlr *);
159 
160 /*
161  * Character/block entry points function prototypes
162  */
163 static int fd_open(dev_t *, int, int, cred_t *);
164 static int fd_close(dev_t, int, int, cred_t *);
165 static int fd_strategy(struct buf *);
166 static int fd_read(dev_t, struct uio *, cred_t *);
167 static int fd_write(dev_t, struct uio *, cred_t *);
168 static int fd_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
169 static int
170 fd_prop_op(dev_t, dev_info_t *, ddi_prop_op_t, int, char *, caddr_t, int *);
171 
172 /*
173  * Device operations (dev_ops) entries function prototypes
174  */
175 static int fd_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
176 		void **result);
177 static int fd_attach(dev_info_t *, ddi_attach_cmd_t);
178 static int fd_detach(dev_info_t *, ddi_detach_cmd_t);
179 static int fd_power(dev_info_t *dip, int component, int level);
180 
181 /*
182  * Internal functions
183  */
184 static int fd_attach_check_drive(struct fdctlr *fdc);
185 static int fd_attach_det_ctlr(dev_info_t *dip, struct fdctlr *fdc);
186 static int fd_attach_map_regs(dev_info_t *dip, struct fdctlr *fdc);
187 static int fd_attach_register_interrupts(dev_info_t *dip, struct fdctlr *fdc,
188     int *hard);
189 static int fd_build_label_vtoc(struct fdunit *, struct vtoc *);
190 static void fd_build_user_vtoc(struct fdunit *, struct vtoc *);
191 static int fdcheckdisk(struct fdctlr *fdc, int unit);
192 static int fd_check_media(dev_t dev, enum dkio_state state);
193 static void fd_cleanup(dev_info_t *dip, struct fdctlr *fdc, int hard,
194     int locks);
195 static void fdeject(struct fdctlr *, int unit);
196 static int fdexec(struct fdctlr *fdc, int flags);
197 static void fdexec_turn_on_motor(struct fdctlr *fdc, int flags, uint_t unit);
198 static int fdformat(struct fdctlr *fdc, int unit, int cyl, int hd);
199 static caddr_t fd_getauxiova();
200 static struct fdctlr *fd_getctlr(dev_t);
201 static void fdgetcsb(struct fdctlr *);
202 static int fdgetlabel(struct fdctlr *fdc, int unit);
203 enum dkio_state fd_get_media_state(struct fdctlr *, int);
204 static uint_t fdintr_dma();
205 static int fd_isauxiodip(dev_info_t *);
206 static uint_t  fd_lointr(caddr_t arg);
207 static void fd_media_watch(void *);
208 static void fdmotoff(void *);
209 static int fd_part_is_open(struct fdunit *un, int part);
210 static int fdrawioctl(struct fdctlr *, int, intptr_t, int);
211 static int fdrecalseek(struct fdctlr *fdc, int unit, int arg, int execflg);
212 static int fdrecover(struct fdctlr *);
213 static void fdretcsb(struct fdctlr *);
214 static int fdreset(struct fdctlr *);
215 static int fdrw(struct fdctlr *fdc, int, int, int, int, int, caddr_t, uint_t);
216 static void fdselect(struct fdctlr *fdc, int unit, int onoff);
217 static int fdsensedrv(struct fdctlr *fdc, int unit);
218 static int fdsense_chng(struct fdctlr *, int unit);
219 static void fdstart(struct fdctlr *);
220 static int fdstart_dma(register struct fdctlr *fdc, caddr_t addr, uint_t len);
221 static int fd_unit_is_open(struct fdunit *);
222 static void fdunpacklabel(struct packed_label *, struct dk_label *);
223 static int fd_unbind_handle(struct fdctlr *);
224 static void fdwatch(void *);
225 static void set_rotational_speed(struct fdctlr *, int);
226 static int fd_get_media_info(struct fdunit *un, caddr_t buf, int flag);
227 static int fd_pm_lower_power(struct fdctlr *fdc);
228 static int fd_pm_raise_power(struct fdctlr *fdc);
229 static void create_pm_components(dev_info_t *dip);
230 static void set_data_count_register(struct fdctlr *fdc, uint32_t count);
231 static uint32_t get_data_count_register(struct fdctlr *fdc);
232 static void reset_dma_controller(struct fdctlr *fdc);
233 static void set_data_address_register(struct fdctlr *fdc, uint32_t address);
234 static uint32_t get_dma_control_register(struct fdctlr *fdc);
235 static void set_dma_mode(struct fdctlr *fdc, int val);
236 static void set_dma_control_register(struct fdctlr *fdc, uint32_t val);
237 static void release_sb_dma(struct fdctlr *fdc);
238 
239 /*
240  * External functions
241  */
242 extern uint_t fd_intr(caddr_t);	/* defined in fd_asm.s */
243 extern void set_auxioreg();
244 extern void call_debug();
245 
246 
247 
248 /*
249  * The following macro checks whether the device in a SUSPENDED state.
250  * As per WDD guide lines the I/O requests to a suspended device should
251  * be blocked until the device is resumed.
252  * Here we cv_wait on c_suspend_cv, and there is a cv_broadcast() in
253  * DDI_RESUME to wake up this thread.
254  *
255  * NOTE: This code is not tested because the kernel threads are suspended
256  * before the device is suspended. So there can not be any I/O requests on
257  * a suspended device until the cpr implementation changes..
258  */
259 
260 #define	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc)	\
261 		{\
262 			while (fdc->c_un->un_state == FD_STATE_SUSPENDED) {\
263 				cv_wait(&fdc->c_suspend_cv, \
264 							&fdc->c_lolock);\
265 			}\
266 		}
267 
268 /*
269  * bss (uninitialized data)
270  */
271 struct	fdctlr	*fdctlrs;	/* linked list of controllers */
272 
273 /*
274  * initialized data
275  */
276 
277 static int fd_check_media_time = 5000000;	/* 5 second state check */
278 static int fd_pollable = 0;
279 static uchar_t rwretry = 10;
280 static uchar_t skretry = 5;
281 /* This variable allows the dynamic change of the burst size */
282 static int fd_burstsize = DCSR_BURST_0 | DCSR_BURST_1;
283 
284 static struct driver_minor_data {
285 	char	*name;
286 	int	minor;
287 	int	type;
288 } fd_minor [] = {
289 	{ "a", 0, S_IFBLK},
290 	{ "b", 1, S_IFBLK},
291 	{ "c", 2, S_IFBLK},
292 	{ "a,raw", 0, S_IFCHR},
293 	{ "b,raw", 1, S_IFCHR},
294 	{ "c,raw", 2, S_IFCHR},
295 	{0}
296 };
297 
298 /*
299  * If the interrupt handler is invoked and no controllers expect an
300  * interrupt, the kernel panics.  The following message is printed out.
301  */
302 char *panic_msg = "fd_intr: unexpected interrupt\n";
303 
304 /*
305  * Specify/Configure cmd parameters
306  */
307 static uchar_t fdspec[2] = { 0xc2, 0x33 };	/*  "specify" parameters */
308 static uchar_t fdconf[3] = { 0x64, 0x58, 0x00 }; /*  "configure" parameters */
309 
310 /* When DMA is used, set the ND bit to 0 */
311 #define	SPEC_DMA_MODE	0x32
312 
313 /*
314  * default characteristics
315  */
316 static struct fd_char fdtypes[] = {
317 	{	/* struct fd_char fdchar_1.7MB density */
318 		0,		/* medium */
319 		500,		/* transfer rate */
320 		80,		/* number of cylinders */
321 		2,		/* number of heads */
322 		512,		/* sector size */
323 		21,		/* sectors per track */
324 		-1,		/* (NA) # steps per data track */
325 	},
326 	{	/* struct fd_char fdchar_highdens */
327 		0,		/* medium */
328 		500,		/* transfer rate */
329 		80,		/* number of cylinders */
330 		2,		/* number of heads */
331 		512,		/* sector size */
332 		18,		/* sectors per track */
333 		-1,		/* (NA) # steps per data track */
334 	},
335 	{	/* struct fd_char fdchar_meddens */
336 		1,		/* medium */
337 		500,		/* transfer rate */
338 		77,		/* number of cylinders */
339 		2,		/* number of heads */
340 		1024,		/* sector size */
341 		8,		/* sectors per track */
342 		-1,		/* (NA) # steps per data track */
343 	},
344 	{	/* struct fd_char fdchar_lowdens  */
345 		0,		/* medium */
346 		250,		/* transfer rate */
347 		80,		/* number of cylinders */
348 		2,		/* number of heads */
349 		512,		/* sector size */
350 		9,		/* sectors per track */
351 		-1,		/* (NA) # steps per data track */
352 	}
353 };
354 
355 
356 static int nfdtypes = sizeof (fdtypes) / sizeof (fdtypes[0]);
357 
358 
359 /*
360  * Default Label & partition maps
361  */
362 
363 static struct packed_label fdlbl_high_21 = {
364 	{ "3.5\" floppy cyl 80 alt 0 hd 2 sec 21" },
365 	300,				/* rotations per minute */
366 	80,				/* # physical cylinders */
367 	0,				/* alternates per cylinder */
368 	1,				/* interleave factor */
369 	80,				/* # of data cylinders */
370 	0,				/* # of alternate cylinders */
371 	2,				/* # of heads in this partition */
372 	21,				/* # of 512 byte sectors per track */
373 	{
374 		{ 0, 79 * 2 * 21 },	/* part 0 - all but last cyl */
375 		{ 79, 1 * 2 * 21 },	/* part 1 - just the last cyl */
376 		{ 0, 80 * 2 * 21 },	/* part 2 - "the whole thing" */
377 	},
378 	{	0,			/* version */
379 		"",			/* volume label */
380 		3,			/* no. of partitions */
381 		{ 0 },			/* partition hdrs, sec 2 */
382 		{ 0 },			/* mboot info.  unsupported */
383 		VTOC_SANE,		/* verify vtoc sanity */
384 		{ 0 },			/* reserved space */
385 		0,			/* timestamp */
386 	},
387 };
388 
389 static struct packed_label fdlbl_high_80 = {
390 	{ "3.5\" floppy cyl 80 alt 0 hd 2 sec 18" },
391 	300,				/* rotations per minute */
392 	80,				/* # physical cylinders */
393 	0,				/* alternates per cylinder */
394 	1,				/* interleave factor */
395 	80,				/* # of data cylinders */
396 	0,				/* # of alternate cylinders */
397 	2,				/* # of heads in this partition */
398 	18,				/* # of 512 byte sectors per track */
399 	{
400 		{ 0, 79 * 2 * 18 },	/* part 0 - all but last cyl */
401 		{ 79, 1 * 2 * 18 },	/* part 1 - just the last cyl */
402 		{ 0, 80 * 2 * 18 },	/* part 2 - "the whole thing" */
403 	},
404 	{	0,			/* version */
405 		"",			/* volume label */
406 		3,			/* no. of partitions */
407 		{ 0 },			/* partition hdrs, sec 2 */
408 		{ 0 },			/* mboot info.  unsupported */
409 		VTOC_SANE,		/* verify vtoc sanity */
410 		{ 0 },			/* reserved space */
411 		0,			/* timestamp */
412 	},
413 };
414 
415 /*
416  * A medium density diskette has 1024 byte sectors.  The dk_label structure
417  * assumes a sector is DEVBSIZE (512) bytes.
418  */
419 static struct packed_label fdlbl_medium_80 = {
420 	{ "3.5\" floppy cyl 77 alt 0 hd 2 sec 8" },
421 	360,				/* rotations per minute */
422 	77,				/* # physical cylinders */
423 	0,				/* alternates per cylinder */
424 	1,				/* interleave factor */
425 	77,				/* # of data cylinders */
426 	0,				/* # of alternate cylinders */
427 	2,				/* # of heads in this partition */
428 	16,				/* # of 512 byte sectors per track */
429 	{
430 		{ 0, 76 * 2 * 8 * 2 },  /* part 0 - all but last cyl */
431 		{ 76, 1 * 2 * 8 * 2 },  /* part 1 - just the last cyl */
432 		{ 0, 77 * 2 * 8 * 2 },  /* part 2 - "the whole thing" */
433 	},
434 	{	0,			/* version */
435 		"",			/* volume label */
436 		3,			/* no. of partitions */
437 		{ 0 },			/* partition hdrs, sec 2 */
438 		{ 0 },			/* mboot info.  unsupported */
439 		VTOC_SANE,		/* verify vtoc sanity */
440 		{ 0 },			/* reserved space */
441 		0,			/* timestamp */
442 	},
443 };
444 
445 static struct packed_label fdlbl_low_80 = {
446 	{ "3.5\" floppy cyl 80 alt 0 hd 2 sec 9" },
447 	300,				/* rotations per minute */
448 	80,				/* # physical cylinders */
449 	0,				/* alternates per cylinder */
450 	1,				/* interleave factor */
451 	80,				/* # of data cylinders */
452 	0,				/* # of alternate cylinders */
453 	2,				/* # of heads in this partition */
454 	9,				/* # of 512 byte sectors per track */
455 	{
456 		{ 0, 79 * 2 * 9 },	/* part 0 - all but last cyl */
457 		{ 79, 1 * 2 * 9 },	/* part 1 - just the last cyl */
458 		{ 0, 80 * 2 * 9 },	/* part 2 - "the whole thing" */
459 	},
460 	{	0,			/* version */
461 		"",			/* volume label */
462 		3,			/* no. of partitions */
463 		{ 0 },			/* partition hdrs, sec 2 */
464 		{ 0 },			/* mboot info.  unsupported */
465 		VTOC_SANE,		/* verify vtoc sanity */
466 		{ 0 },			/* reserved space */
467 		0,			/* timestamp */
468 	},
469 };
470 
471 static struct fdcmdinfo {
472 	char *cmdname;		/* command name */
473 	uchar_t ncmdbytes;	/* number of bytes of command */
474 	uchar_t nrsltbytes;	/* number of bytes in result */
475 	uchar_t cmdtype;		/* characteristics */
476 } fdcmds[] = {
477 	"", 0, 0, 0,			/* - */
478 	"", 0, 0, 0,			/* - */
479 	"read_track", 9, 7, 1,		/* 2 */
480 	"specify", 3, 0, 3,		/* 3 */
481 	"sense_drv_status", 2, 1, 3,	/* 4 */
482 	"write", 9, 7, 1,		/* 5 */
483 	"read", 9, 7, 1,		/* 6 */
484 	"recalibrate", 2, 0, 2,		/* 7 */
485 	"sense_int_status", 1, 2, 3,	/* 8 */
486 	"write_del", 9, 7, 1,		/* 9 */
487 	"read_id", 2, 7, 2,		/* A */
488 	"motor_on/off", 1, 0, 4,	/* B */
489 	"read_del", 9, 7, 1,		/* C */
490 	"format_track", 10, 7, 1,	/* D */
491 	"dump_reg", 1, 10, 4,		/* E */
492 	"seek", 3, 0, 2,		/* F */
493 	"", 0, 0, 0,			/* - */
494 	"", 0, 0, 0,			/* - */
495 	"", 0, 0, 0,			/* - */
496 	"configure", 4, 0, 4,		/* 13 */
497 	/* relative seek */
498 };
499 
500 static struct cb_ops fd_cb_ops = {
501 	fd_open,		/* open */
502 	fd_close,		/* close */
503 	fd_strategy,		/* strategy */
504 	nodev,			/* print */
505 	nodev,			/* dump */
506 	fd_read,		/* read */
507 	fd_write,		/* write */
508 	fd_ioctl,		/* ioctl */
509 	nodev,			/* devmap */
510 	nodev,			/* mmap */
511 	nodev,			/* segmap */
512 	nochpoll,		/* poll */
513 	fd_prop_op,		/* cb_prop_op */
514 	0,			/* streamtab  */
515 	D_NEW | D_MP		/* Driver compatibility flag */
516 };
517 
518 static struct dev_ops	fd_ops = {
519 	DEVO_REV,		/* devo_rev, */
520 	0,			/* refcnt  */
521 	fd_info,		/* info */
522 	nulldev,		/* identify */
523 	nulldev,		/* probe */
524 	fd_attach,		/* attach */
525 	fd_detach,		/* detach */
526 	nodev,			/* reset */
527 	&fd_cb_ops,		/* driver operations */
528 	(struct bus_ops *)0,	/* bus operations */
529 	fd_power,		/* power */
530 	ddi_quiesce_not_supported,	/* devo_quiesce */
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",	/* 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
_init(void)572 _init(void)
573 {
574 	return (mod_install(&modlinkage));
575 }
576 
577 int
_info(struct modinfo * modinfop)578 _info(struct modinfo *modinfop)
579 {
580 	return (mod_info(&modlinkage, modinfop));
581 }
582 
583 int
_fini(void)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
fd_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)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
fd_attach_map_regs(dev_info_t * dip,struct fdctlr * fdc)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
fd_attach_det_ctlr(dev_info_t * dip,struct fdctlr * fdc)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
fd_attach_register_interrupts(dev_info_t * dip,struct fdctlr * fdc,int * hard)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
fd_attach_check_drive(struct fdctlr * fdc)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
fd_cleanup(dev_info_t * dip,struct fdctlr * fdc,int hard,int locks)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 		if (fdc->c_un != (struct fdunit *)NULL)
1273 			fdmotoff(fdc);
1274 	}
1275 	if (fdc->c_timeid)
1276 		(void) untimeout(fdc->c_timeid);
1277 
1278 
1279 	/* Remove memory handles */
1280 	if (fdc->c_handlep_cont)
1281 		ddi_regs_map_free(&fdc->c_handlep_cont);
1282 
1283 	if (fdc->c_handlep_aux)
1284 		ddi_regs_map_free(&fdc->c_handlep_aux);
1285 
1286 	if (fdc->c_handlep_dma)
1287 		ddi_regs_map_free(&fdc->c_handlep_dma);
1288 
1289 	if (fdc->c_dma_buf_handle != NULL)
1290 		ddi_dma_mem_free(&fdc->c_dma_buf_handle);
1291 
1292 	if (fdc->c_dmahandle != NULL)
1293 		ddi_dma_free_handle(&fdc->c_dmahandle);
1294 
1295 
1296 	/* Remove all minor nodes */
1297 	ddi_remove_minor_node(dip, NULL);
1298 
1299 
1300 
1301 	/* Remove unit structure if one exists */
1302 	if (fdc->c_un != (struct fdunit *)NULL) {
1303 
1304 		ASSERT(!mutex_owned(&fdc->c_lolock));
1305 
1306 		if (fdc->c_un->un_iostat)
1307 			kstat_delete(fdc->c_un->un_iostat);
1308 		fdc->c_un->un_iostat = NULL;
1309 
1310 		if (fdc->c_un->un_chars)
1311 			kmem_free(fdc->c_un->un_chars, sizeof (struct fd_char));
1312 
1313 		if (fdc->c_un->un_drive)
1314 			kmem_free(fdc->c_un->un_drive,
1315 			    sizeof (struct fd_drive));
1316 
1317 		kmem_free((caddr_t)fdc->c_un, sizeof (struct fdunit));
1318 	}
1319 
1320 	if (fdc->c_intrstat) {
1321 		FDERRPRINT(FDEP_L1, FDEM_ATTA,
1322 		    (C, "fd_cleanup: delete intrstat\n"));
1323 
1324 		kstat_delete(fdc->c_intrstat);
1325 	}
1326 
1327 	fdc->c_intrstat = NULL;
1328 
1329 	if (locks) {
1330 		cv_destroy(&fdc->c_iocv);
1331 		cv_destroy(&fdc->c_csbcv);
1332 		cv_destroy(&fdc->c_motoncv);
1333 		cv_destroy(&fdc->c_suspend_cv);
1334 		sema_destroy(&fdc->c_ocsem);
1335 		mutex_destroy(&fdc->c_hilock);
1336 		mutex_destroy(&fdc->c_lolock);
1337 	}
1338 
1339 
1340 	fdctlrs = fdc->c_next;
1341 	kmem_free(fdc, sizeof (*fdc));
1342 
1343 
1344 }
1345 
1346 
1347 static int
fd_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)1348 fd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1349 {
1350 	int instance = ddi_get_instance(dip);
1351 	struct fdctlr *fdc = fd_getctlr(instance << FDINSTSHIFT);
1352 	timeout_id_t c_mtimeid;
1353 
1354 	FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_detach\n"));
1355 
1356 	switch (cmd) {
1357 
1358 	case DDI_DETACH:
1359 		/*
1360 		 * The hard parameter is set to 1.  If detach is called, then
1361 		 * attach must have passed meaning that the high level
1362 		 * interrupt handler was successfully added.
1363 		 * Similarly, the locks parameter is also set to 1.
1364 		 */
1365 		fd_cleanup(dip, fdc, 1, 1);
1366 
1367 		ddi_prop_remove_all(dip);
1368 
1369 		return (DDI_SUCCESS);
1370 
1371 	case DDI_SUSPEND:
1372 		if (!fdc)
1373 			return (DDI_FAILURE);
1374 
1375 
1376 		mutex_enter(&fdc->c_lolock);
1377 		fdgetcsb(fdc);	/* Wait for I/O to finish */
1378 		c_mtimeid = fdc->c_mtimeid;
1379 		fdretcsb(fdc);
1380 		mutex_exit(&fdc->c_lolock);
1381 
1382 		(void) untimeout(c_mtimeid);
1383 		/*
1384 		 * After suspend, the system could be powered off.
1385 		 * When it is later powered on the southbridge floppy
1386 		 * controller will tristate the interrupt line causing
1387 		 * continuous dma interrupts.
1388 		 * To avoid getting continuous fd interrupts we will remove the
1389 		 * dma interrupt handler installed. We will re-install the
1390 		 * handler when we RESUME.
1391 		 */
1392 		if (fdc->c_fdtype & FDCTYPE_SB)
1393 			ddi_remove_intr(dip, 0, fdc->c_block);
1394 
1395 		fdc->c_un->un_state = FD_STATE_SUSPENDED;
1396 
1397 		return (DDI_SUCCESS);
1398 
1399 	default:
1400 		return (DDI_FAILURE);
1401 	}
1402 }
1403 
1404 /* ARGSUSED */
1405 static int
fd_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)1406 fd_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1407 {
1408 	register struct fdctlr *fdc;
1409 	register int error;
1410 
1411 	switch (infocmd) {
1412 
1413 	case DDI_INFO_DEVT2DEVINFO:
1414 		if ((fdc = fd_getctlr((dev_t)arg)) == NULL) {
1415 			error = DDI_FAILURE;
1416 		} else {
1417 			*result = fdc->c_dip;
1418 			error = DDI_SUCCESS;
1419 		}
1420 		break;
1421 
1422 	case DDI_INFO_DEVT2INSTANCE:
1423 		*result = 0;
1424 		error = DDI_SUCCESS;
1425 		break;
1426 
1427 	default:
1428 		error = DDI_FAILURE;
1429 	}
1430 	return (error);
1431 }
1432 
1433 /*
1434  * property operation routine.  return the number of blocks for the partition
1435  * in question or forward the request to the property facilities.
1436  */
1437 static int
fd_prop_op(dev_t dev,dev_info_t * dip,ddi_prop_op_t prop_op,int mod_flags,char * name,caddr_t valuep,int * lengthp)1438 fd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
1439     char *name, caddr_t valuep, int *lengthp)
1440 {
1441 	struct fdunit	*un;
1442 	struct fdctlr	*fdc;
1443 	uint64_t	nblocks64;
1444 
1445 	/*
1446 	 * Our dynamic properties are all device specific and size oriented.
1447 	 * Requests issued under conditions where size is valid are passed
1448 	 * to ddi_prop_op_nblocks with the size information, otherwise the
1449 	 * request is passed to ddi_prop_op.
1450 	 */
1451 	if (dev == DDI_DEV_T_ANY) {
1452 pass:		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
1453 		    name, valuep, lengthp));
1454 	} else {
1455 		fdc = fd_getctlr(dev);
1456 		if (fdc == NULL)
1457 			goto pass;
1458 
1459 		/* we have size if diskette opened and label read */
1460 		un = fdc->c_un;
1461 		if ((un == NULL) || !fd_unit_is_open(fdc->c_un))
1462 			goto pass;
1463 
1464 		/* get nblocks value */
1465 		nblocks64 = (ulong_t)
1466 		    un->un_label.dkl_map[FDPARTITION(dev)].dkl_nblk;
1467 
1468 		return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags,
1469 		    name, valuep, lengthp, nblocks64));
1470 	}
1471 }
1472 
1473 /* ARGSUSED3 */
1474 static int
fd_open(dev_t * devp,int flag,int otyp,cred_t * cred_p)1475 fd_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
1476 {
1477 	dev_t dev;
1478 	int  part;
1479 	struct fdctlr *fdc;
1480 	struct fdunit *un;
1481 	struct dk_map32 *dkm;
1482 	uchar_t	pbit;
1483 	int	err, part_is_open;
1484 	int	unit;
1485 
1486 	dev = *devp;
1487 	fdc = fd_getctlr(dev);
1488 	if ((fdc == NULL) || ((un = fdc->c_un) == NULL)) {
1489 		return (ENXIO);
1490 	}
1491 
1492 	unit = fdc->c_un->un_unit_no;
1493 
1494 	/*
1495 	 * Serialize opens/closes
1496 	 */
1497 
1498 	sema_p(&fdc->c_ocsem);
1499 
1500 	/* check partition */
1501 	part = FDPARTITION(dev);
1502 	pbit = 1 << part;
1503 	dkm = &un->un_label.dkl_map[part];
1504 	if (dkm->dkl_nblk == 0) {
1505 		sema_v(&fdc->c_ocsem);
1506 		return (ENXIO);
1507 	}
1508 
1509 	FDERRPRINT(FDEP_L1, FDEM_OPEN,
1510 	    (C, "fdopen: ctlr %d unit %d part %d\n",
1511 	    ddi_get_instance(fdc->c_dip), unit, part));
1512 
1513 	FDERRPRINT(FDEP_L1, FDEM_OPEN,
1514 	    (C, "fdopen: flag 0x%x", flag));
1515 
1516 
1517 	/*
1518 	 * Insure that drive is present with a recalibrate on first open.
1519 	 */
1520 	(void) pm_busy_component(fdc->c_dip, 0);
1521 
1522 	mutex_enter(&fdc->c_lolock);
1523 
1524 	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
1525 
1526 	if (fdc->c_un->un_state == FD_STATE_STOPPED) {
1527 		mutex_exit(&fdc->c_lolock);
1528 		if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
1529 		    != DDI_SUCCESS) {
1530 			FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power change \
1531 			    failed. \n"));
1532 
1533 				sema_v(&fdc->c_ocsem);
1534 				(void) pm_idle_component(fdc->c_dip, 0);
1535 				return (EIO);
1536 		}
1537 		mutex_enter(&fdc->c_lolock);
1538 	}
1539 	if (fd_unit_is_open(un) == 0) {
1540 		fdgetcsb(fdc);
1541 		/*
1542 		 * no check changed!
1543 		 */
1544 		err = fdrecalseek(fdc, unit, -1, 0);
1545 		fdretcsb(fdc);
1546 		if (err) {
1547 			FDERRPRINT(FDEP_L3, FDEM_OPEN,
1548 			    (C, "fd%d: drive not ready\n", 0));
1549 			/* deselect drv on last close */
1550 			fdselect(fdc, unit, 0);
1551 			mutex_exit(&fdc->c_lolock);
1552 			sema_v(&fdc->c_ocsem);
1553 			(void) pm_idle_component(fdc->c_dip, 0);
1554 			return (EIO);
1555 		}
1556 	}
1557 
1558 	/*
1559 	 * Check for previous exclusive open, or trying to exclusive open
1560 	 */
1561 	if (otyp == OTYP_LYR) {
1562 		part_is_open = (un->un_lyropen[part] != 0);
1563 	} else {
1564 		part_is_open = fd_part_is_open(un, part);
1565 	}
1566 	if ((un->un_exclmask & pbit) || ((flag & FEXCL) && part_is_open)) {
1567 		mutex_exit(&fdc->c_lolock);
1568 		sema_v(&fdc->c_ocsem);
1569 		FDERRPRINT(FDEP_L2, FDEM_OPEN, (C, "fd:just return\n"));
1570 		(void) pm_idle_component(fdc->c_dip, 0);
1571 		return (EBUSY);
1572 	}
1573 
1574 	/* don't attempt access, just return successfully */
1575 	if (flag & (FNDELAY | FNONBLOCK)) {
1576 		FDERRPRINT(FDEP_L2, FDEM_OPEN,
1577 		    (C, "fd: return busy..\n"));
1578 		goto out;
1579 	}
1580 
1581 	fdc->c_csb.csb_unit = (uchar_t)unit;
1582 	if (fdgetlabel(fdc, unit)) {
1583 		/* didn't find label (couldn't read anything) */
1584 		FDERRPRINT(FDEP_L3, FDEM_OPEN,
1585 		    (C,
1586 		    "fd%d: unformatted diskette or no diskette in the drive\n",
1587 		    0));
1588 		if (fd_unit_is_open(un) == 0) {
1589 			/* deselect drv on last close */
1590 			fdselect(fdc, unit, 0);
1591 		}
1592 
1593 		mutex_exit(&fdc->c_lolock);
1594 		sema_v(&fdc->c_ocsem);
1595 		(void) pm_idle_component(fdc->c_dip, 0);
1596 		return (EIO);
1597 	}
1598 
1599 	/*
1600 	 * if opening for writing, check write protect on diskette
1601 	 */
1602 	if (flag & FWRITE) {
1603 		fdgetcsb(fdc);
1604 		err = fdsensedrv(fdc, unit) & WP_SR3;
1605 		fdretcsb(fdc);
1606 		if (err) {
1607 			if (fd_unit_is_open(un) == 0)
1608 				fdselect(fdc, unit, 0);
1609 			mutex_exit(&fdc->c_lolock);
1610 			sema_v(&fdc->c_ocsem);
1611 			(void) pm_idle_component(fdc->c_dip, 0);
1612 			return (EROFS);
1613 		}
1614 	}
1615 
1616 out:
1617 	/*
1618 	 * mark open as having succeeded
1619 	 */
1620 	if (flag & FEXCL) {
1621 		un->un_exclmask |= pbit;
1622 	}
1623 	if (otyp == OTYP_LYR) {
1624 		un->un_lyropen[part]++;
1625 	} else {
1626 		un->un_regopen[otyp] |= pbit;
1627 	}
1628 	mutex_exit(&fdc->c_lolock);
1629 	sema_v(&fdc->c_ocsem);
1630 	(void) pm_idle_component(fdc->c_dip, 0);
1631 	return (0);
1632 }
1633 /*
1634  * fd_part_is_open
1635  *	return 1 if the partition is open
1636  *	return 0 otherwise
1637  */
1638 static int
fd_part_is_open(struct fdunit * un,int part)1639 fd_part_is_open(struct fdunit *un, int part)
1640 {
1641 	int i;
1642 	for (i = 0; i < OTYPCNT - 1; i++)
1643 		if (un->un_regopen[i] & (1 << part))
1644 			return (1);
1645 	return (0);
1646 }
1647 
1648 
1649 /* ARGSUSED */
1650 static int
fd_close(dev_t dev,int flag,int otyp,cred_t * cred_p)1651 fd_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
1652 {
1653 	int unit, part_is_closed, part;
1654 	register struct fdctlr *fdc;
1655 	register struct fdunit *un;
1656 
1657 	fdc = fd_getctlr(dev);
1658 	if (!fdc || !(un = fdc->c_un))
1659 		return (ENXIO);
1660 
1661 
1662 	unit = fdc->c_un->un_unit_no;
1663 	FDERRPRINT(FDEP_L1, FDEM_CLOS, (C, "fd_close\n"));
1664 	part = FDPARTITION(dev);
1665 
1666 	sema_p(&fdc->c_ocsem);
1667 	mutex_enter(&fdc->c_lolock);
1668 
1669 	if (otyp == OTYP_LYR) {
1670 		un->un_lyropen[part]--;
1671 		part_is_closed = (un->un_lyropen[part] == 0);
1672 	} else {
1673 		un->un_regopen[otyp] &= ~(1<<part);
1674 		part_is_closed = 1;
1675 	}
1676 	if (part_is_closed)
1677 		un->un_exclmask &= ~(1<<part);
1678 
1679 	if (fd_unit_is_open(un) == 0) {
1680 		/* deselect drive on last close */
1681 		fdselect(fdc, unit, 0);
1682 		un->un_flags &= ~FDUNIT_CHANGED;
1683 	}
1684 	mutex_exit(&fdc->c_lolock);
1685 	sema_v(&fdc->c_ocsem);
1686 
1687 	return (0);
1688 }
1689 
1690 /*
1691  * fd_strategy
1692  *	checks operation, hangs buf struct off fdctlr, calls fdstart
1693  *	if not already busy.  Note that if we call start, then the operation
1694  *	will already be done on return (start sleeps).
1695  */
1696 static int
fd_strategy(register struct buf * bp)1697 fd_strategy(register struct buf *bp)
1698 {
1699 	struct fdctlr *fdc;
1700 	struct fdunit *un;
1701 	uint_t	phys_blkno;
1702 	struct dk_map32 *dkm;
1703 
1704 	FDERRPRINT(FDEP_L1, FDEM_STRA,
1705 	    (C, "fd_strategy: bp = 0x%p, dev = 0x%lx\n",
1706 	    (void *)bp, bp->b_edev));
1707 	FDERRPRINT(FDEP_L1, FDEM_STRA,
1708 	    (C, "b_blkno=%x b_flags=%x b_count=%x\n",
1709 	    (int)bp->b_blkno, bp->b_flags, (int)bp->b_bcount));
1710 	fdc = fd_getctlr(bp->b_edev);
1711 	un = fdc->c_un;
1712 	dkm = &un->un_label.dkl_map[FDPARTITION(bp->b_edev)];
1713 
1714 	/*
1715 	 * If it's medium density and the block no. isn't a multiple
1716 	 * of 1K, then return an error.
1717 	 */
1718 	if (un->un_chars->fdc_medium) {
1719 		phys_blkno = (uint_t)bp->b_blkno >> 1;
1720 		if (bp->b_blkno & 1) {
1721 			FDERRPRINT(FDEP_L3, FDEM_STRA,
1722 			    (C, "b_blkno=0x%lx is not 1k aligned\n",
1723 			    (long)bp->b_blkno));
1724 			bp->b_error = EINVAL;
1725 			bp->b_resid = bp->b_bcount;
1726 			bp->b_flags |= B_ERROR;
1727 			biodone(bp);
1728 			return (0);
1729 		}
1730 	} else {
1731 		phys_blkno = (uint_t)bp->b_blkno;
1732 	}
1733 
1734 
1735 	/* If the block number is past the end, return an error */
1736 	if ((phys_blkno > dkm->dkl_nblk)) {
1737 		FDERRPRINT(FDEP_L3, FDEM_STRA,
1738 		    (C, "fd%d: block %ld is past the end! (nblk=%d)\n",
1739 		    0, (long)bp->b_blkno, dkm->dkl_nblk));
1740 		bp->b_error = ENOSPC;
1741 		bp->b_resid = bp->b_bcount;
1742 		bp->b_flags |= B_ERROR;
1743 		biodone(bp);
1744 		return (0);
1745 	}
1746 
1747 	/* if at end of file, skip out now */
1748 	if (phys_blkno == dkm->dkl_nblk) {
1749 		FDERRPRINT(FDEP_L1, FDEM_STRA,
1750 		    (C, "b_blkno is at the end!\n"));
1751 
1752 		if ((bp->b_flags & B_READ) == 0) {
1753 			/* a write needs to get an error! */
1754 			bp->b_error = ENOSPC;
1755 			bp->b_flags |= B_ERROR;
1756 
1757 			FDERRPRINT(FDEP_L1, FDEM_STRA,
1758 			    (C, "block is at end and this is a write\n"));
1759 
1760 		}
1761 
1762 		bp->b_resid = bp->b_bcount;
1763 		biodone(bp);
1764 		return (0);
1765 	}
1766 
1767 	/* if operation not a multiple of sector size, is error! */
1768 	if (bp->b_bcount % un->un_chars->fdc_sec_size)	{
1769 		FDERRPRINT(FDEP_L3, FDEM_STRA,
1770 		    (C, "fd%d: requested transfer size(0x%lx) is not"
1771 		    " multiple of sector size(0x%x)\n", 0,
1772 		    bp->b_bcount, un->un_chars->fdc_sec_size));
1773 		FDERRPRINT(FDEP_L3, FDEM_STRA,
1774 		    (C, "	b_blkno=0x%lx b_flags=0x%x\n",
1775 		    (long)bp->b_blkno, bp->b_flags));
1776 		bp->b_error = EINVAL;
1777 		bp->b_resid = bp->b_bcount;
1778 		bp->b_flags |= B_ERROR;
1779 		biodone(bp);
1780 		return (0);
1781 
1782 	}
1783 
1784 	/*
1785 	 * Put the buf request in the controller's queue, FIFO.
1786 	 */
1787 	bp->av_forw = 0;
1788 	sema_p(&fdc->c_ocsem);
1789 
1790 	(void) pm_busy_component(fdc->c_dip, 0);
1791 
1792 	mutex_enter(&fdc->c_lolock);
1793 
1794 	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
1795 
1796 	if (fdc->c_un->un_state == FD_STATE_STOPPED) {
1797 		mutex_exit(&fdc->c_lolock);
1798 		if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
1799 		    != DDI_SUCCESS) {
1800 			sema_v(&fdc->c_ocsem);
1801 			(void) pm_idle_component(fdc->c_dip, 0);
1802 			bp->b_error = EIO;
1803 			bp->b_resid = bp->b_bcount;
1804 			bp->b_flags |= B_ERROR;
1805 			biodone(bp);
1806 			return (0);
1807 		} else {
1808 			mutex_enter(&fdc->c_lolock);
1809 		}
1810 	}
1811 	if (un->un_iostat) {
1812 		kstat_waitq_enter(KIOSP);
1813 	}
1814 	if (fdc->c_actf)
1815 		fdc->c_actl->av_forw = bp;
1816 	else
1817 		fdc->c_actf = bp;
1818 	fdc->c_actl = bp;
1819 
1820 
1821 	/* call fdstart to start the transfer */
1822 	fdstart(fdc);
1823 
1824 	mutex_exit(&fdc->c_lolock);
1825 	sema_v(&fdc->c_ocsem);
1826 	(void) pm_idle_component(fdc->c_dip, 0);
1827 	return (0);
1828 }
1829 
1830 /* ARGSUSED2 */
1831 static int
fd_read(dev_t dev,struct uio * uio,cred_t * cred_p)1832 fd_read(dev_t dev, struct uio *uio, cred_t *cred_p)
1833 {
1834 	FDERRPRINT(FDEP_L1, FDEM_RDWR, (C, "fd_read\n"));
1835 	return (physio(fd_strategy, NULL, dev, B_READ, minphys, uio));
1836 }
1837 
1838 /* ARGSUSED2 */
1839 static int
fd_write(dev_t dev,struct uio * uio,cred_t * cred_p)1840 fd_write(dev_t dev, struct uio *uio, cred_t *cred_p)
1841 {
1842 	FDERRPRINT(FDEP_L1, FDEM_RDWR, (C, "fd_write\n"));
1843 	return (physio(fd_strategy, NULL, dev, B_WRITE, minphys, uio));
1844 }
1845 
1846 static void
fdmotoff(void * arg)1847 fdmotoff(void *arg)
1848 {
1849 	struct fdctlr *fdc = arg;
1850 	int unit = fdc->c_un->un_unit_no;
1851 
1852 	mutex_enter(&fdc->c_lolock);
1853 
1854 	/* Just return if we're about to call untimeout */
1855 	if (fdc->c_mtimeid == 0) {
1856 		mutex_exit(&fdc->c_lolock);
1857 		return;
1858 	}
1859 
1860 	FDERRPRINT(FDEP_L1, FDEM_MOFF, (C, "fdmotoff\n"));
1861 
1862 	fdc->c_mtimeid = 0;
1863 
1864 	if (!(Msr(fdc) & CB) && (Dor(fdc) & (MOTEN(unit)))) {
1865 		/* LINTED */
1866 		Set_dor(fdc, MOTEN(unit), 0);
1867 	}
1868 
1869 	mutex_exit(&fdc->c_lolock);
1870 }
1871 
1872 /* ARGSUSED */
1873 static int
fd_ioctl(dev_t dev,int cmd,intptr_t arg,int flag,cred_t * cred_p,int * rval_p)1874 fd_ioctl(dev_t dev, int cmd, intptr_t arg, int flag,
1875     cred_t *cred_p, int *rval_p)
1876 {
1877 	union {
1878 		struct dk_cinfo dki;
1879 		struct dk_geom dkg;
1880 		struct dk_allmap32 dka;
1881 		struct fd_char fdchar;
1882 		struct fd_drive drvchar;
1883 		int	temp;
1884 	} cpy;
1885 
1886 	struct vtoc	vtoc;
1887 	struct fdunit *un;
1888 	struct fdctlr *fdc;
1889 	int unit, dkunit;
1890 	int err = 0;
1891 	uint_t	sec_size;
1892 	enum dkio_state state;
1893 	int	transfer_rate;
1894 
1895 	FDERRPRINT(FDEP_L1, FDEM_IOCT,
1896 	    (C, "fd_ioctl: cmd 0x%x, arg 0x%lx\n", cmd, (long)arg));
1897 
1898 	/* The minor number should always be 0 */
1899 	if (FDUNIT(dev) != 0)
1900 		return (ENXIO);
1901 
1902 	fdc = fd_getctlr(dev);
1903 	unit = fdc->c_un->un_unit_no;
1904 	un = fdc->c_un;
1905 	sec_size = un->un_chars->fdc_sec_size;
1906 	bzero(&cpy, sizeof (cpy));
1907 
1908 	switch (cmd) {
1909 	case DKIOCINFO:
1910 		cpy.dki.dki_addr = 0;
1911 
1912 		/*
1913 		 * The meaning of the dki_slave and dki_unit fields
1914 		 * is unclear.  The sparc floppy driver follows the same
1915 		 * convention as sd.c in that the instance number is
1916 		 * returned in the dki_cnum field.  The dki_slave field is
1917 		 * ignored.
1918 		 *
1919 		 * The dki_cnum contains the controller instance
1920 		 * and its value can be any positive number. Even
1921 		 * though currently Sparc platforms only support
1922 		 * one controller, the controller instance number
1923 		 * can be any number since it is assigned by the
1924 		 * system depending on the device properties.
1925 		 */
1926 
1927 		cpy.dki.dki_cnum = FDCTLR(dev);
1928 
1929 		/*
1930 		 * Sparc platforms support only one floppy drive.
1931 		 * The device node for the controller is the same as
1932 		 * the device node for the drive.  The x86 driver is
1933 		 * different in that it has a node for the controller
1934 		 * and a child node for each drive. Since Sparc supports
1935 		 * only one drive, the unit number will always be zero.
1936 		 */
1937 
1938 		cpy.dki.dki_unit = FDUNIT(dev);
1939 
1940 		/*
1941 		 * The meaning of the dki_slave field is unclear.
1942 		 * So, I will leave it set to 0.
1943 		 */
1944 
1945 		cpy.dki.dki_slave = 0;
1946 
1947 		cpy.dki.dki_ctype = (ushort_t)-1;
1948 		if (fdc->c_fdtype & FDCTYPE_82077)
1949 			cpy.dki.dki_ctype = DKC_INTEL82077;
1950 		cpy.dki.dki_flags = DKI_FMTTRK;
1951 		cpy.dki.dki_partition = FDPARTITION(dev);
1952 		cpy.dki.dki_maxtransfer = maxphys / DEV_BSIZE;
1953 		if (ddi_copyout((caddr_t)&cpy.dki, (caddr_t)arg,
1954 		    sizeof (cpy.dki), flag))
1955 			err = EFAULT;
1956 		break;
1957 	case DKIOCGGEOM:
1958 		cpy.dkg.dkg_ncyl = un->un_chars->fdc_ncyl;
1959 		cpy.dkg.dkg_nhead = un->un_chars->fdc_nhead;
1960 		cpy.dkg.dkg_nsect = un->un_chars->fdc_secptrack;
1961 		cpy.dkg.dkg_intrlv = un->un_label.dkl_intrlv;
1962 		cpy.dkg.dkg_rpm = un->un_label.dkl_rpm;
1963 		cpy.dkg.dkg_pcyl = un->un_chars->fdc_ncyl;
1964 		cpy.dkg.dkg_read_reinstruct =
1965 		    (int)(cpy.dkg.dkg_nsect * cpy.dkg.dkg_rpm * 4) / 60000;
1966 		cpy.dkg.dkg_write_reinstruct = cpy.dkg.dkg_read_reinstruct;
1967 		if (ddi_copyout((caddr_t)&cpy.dkg, (caddr_t)arg,
1968 		    sizeof (cpy.dkg), flag))
1969 			err = EFAULT;
1970 		break;
1971 	case DKIOCSGEOM:
1972 		FDERRPRINT(FDEP_L3, FDEM_IOCT,
1973 		    (C, "fd_ioctl: DKIOCSGEOM not supported\n"));
1974 		err = ENOTTY;
1975 		break;
1976 
1977 	/*
1978 	 * return the map of all logical partitions
1979 	 */
1980 	case DKIOCGAPART:
1981 		/*
1982 		 * We don't have anything to do if the application is ILP32
1983 		 * because the label map has a 32-bit format. Otherwise
1984 		 * convert.
1985 		 */
1986 		if ((flag & DATAMODEL_MASK) == DATAMODEL_ILP32) {
1987 			if (ddi_copyout(&un->un_label.dkl_map,
1988 			    (void *)arg, sizeof (struct dk_allmap32), flag))
1989 				err = EFAULT;
1990 		}
1991 #ifdef _MULTI_DATAMODEL
1992 		else {
1993 			struct dk_allmap dk_allmap;
1994 
1995 			ASSERT((flag & DATAMODEL_MASK) == DATAMODEL_LP64);
1996 			for (dkunit = 0; dkunit < NDKMAP; dkunit++) {
1997 				dk_allmap.dka_map[dkunit].dkl_cylno =
1998 				    un->un_label.dkl_map[dkunit].dkl_cylno;
1999 				dk_allmap.dka_map[dkunit].dkl_nblk =
2000 				    un->un_label.dkl_map[dkunit].dkl_nblk;
2001 			}
2002 			if (ddi_copyout(&dk_allmap, (void *)arg,
2003 			    sizeof (struct dk_allmap), flag))
2004 				err = EFAULT;
2005 		}
2006 #endif /* _MULTI_DATAMODEL */
2007 		break;
2008 
2009 	/*
2010 	 * Set the map of all logical partitions
2011 	 */
2012 	case DKIOCSAPART:
2013 		if ((flag & DATAMODEL_MASK) == DATAMODEL_ILP32) {
2014 			if (ddi_copyin((const void *)arg, &cpy.dka,
2015 			    sizeof (cpy.dka), flag))
2016 				return (EFAULT);
2017 			else {
2018 				mutex_enter(&fdc->c_lolock);
2019 				for (dkunit = 0; dkunit < NDKMAP; dkunit++) {
2020 					un->un_label.dkl_map[dkunit] =
2021 					    cpy.dka.dka_map[dkunit];
2022 				}
2023 				mutex_exit(&fdc->c_lolock);
2024 			}
2025 		}
2026 #ifdef _MULTI_DATAMODEL
2027 		else {
2028 			struct dk_allmap dk_allmap;
2029 
2030 			ASSERT((flag & DATAMODEL_MASK) == DATAMODEL_LP64);
2031 			if (ddi_copyin((const void *)arg, &dk_allmap,
2032 			    sizeof (dk_allmap), flag))
2033 				return (EFAULT);
2034 			else {
2035 				mutex_enter(&fdc->c_lolock);
2036 				for (dkunit = 0; dkunit < NDKMAP; dkunit++) {
2037 					un->un_label.dkl_map[dkunit].dkl_cylno =
2038 					    dk_allmap.dka_map[dkunit].dkl_cylno;
2039 					un->un_label.dkl_map[dkunit].dkl_nblk =
2040 					    dk_allmap.dka_map[dkunit].dkl_nblk;
2041 				}
2042 				mutex_exit(&fdc->c_lolock);
2043 			}
2044 		}
2045 #endif /* _MULTI_DATAMODEL */
2046 		break;
2047 
2048 	case DKIOCGVTOC:
2049 		mutex_enter(&fdc->c_lolock);
2050 
2051 		/*
2052 		 * Exit if the diskette has no label.
2053 		 * Also, get the label to make sure the
2054 		 * correct one is being used since the diskette
2055 		 * may have changed
2056 		 */
2057 		if (fdgetlabel(fdc, unit)) {
2058 			mutex_exit(&fdc->c_lolock);
2059 			err = EINVAL;
2060 			break;
2061 		}
2062 
2063 		/* Build a vtoc from the diskette's label */
2064 		fd_build_user_vtoc(un, &vtoc);
2065 		mutex_exit(&fdc->c_lolock);
2066 
2067 #ifdef _MULTI_DATAMODEL
2068 		switch (ddi_model_convert_from(flag & FMODELS)) {
2069 		case DDI_MODEL_ILP32: {
2070 			struct vtoc32 vtoc32;
2071 
2072 			vtoctovtoc32(vtoc, vtoc32);
2073 			if (ddi_copyout(&vtoc32, (void *)arg,
2074 			    sizeof (struct vtoc32), flag))
2075 				return (EFAULT);
2076 			break;
2077 		}
2078 
2079 		case DDI_MODEL_NONE:
2080 			if (ddi_copyout(&vtoc, (void *)arg,
2081 			    sizeof (vtoc), flag))
2082 				return (EFAULT);
2083 			break;
2084 		}
2085 #else /* ! _MULTI_DATAMODEL */
2086 		if (ddi_copyout(&vtoc, (void *)arg, sizeof (vtoc), flag))
2087 			return (EFAULT);
2088 #endif /* _MULTI_DATAMODEL */
2089 		break;
2090 
2091 	case DKIOCSVTOC:
2092 
2093 #ifdef _MULTI_DATAMODEL
2094 		switch (ddi_model_convert_from(flag & FMODELS)) {
2095 		case DDI_MODEL_ILP32: {
2096 			struct vtoc32 vtoc32;
2097 
2098 			if (ddi_copyin((const void *)arg, &vtoc32,
2099 			    sizeof (struct vtoc32), flag)) {
2100 				return (EFAULT);
2101 			}
2102 			vtoc32tovtoc(vtoc32, vtoc);
2103 			break;
2104 		}
2105 
2106 		case DDI_MODEL_NONE:
2107 			if (ddi_copyin((const void *)arg, &vtoc,
2108 			    sizeof (vtoc), flag)) {
2109 				return (EFAULT);
2110 			}
2111 			break;
2112 		}
2113 #else /* ! _MULTI_DATAMODEL */
2114 		if (ddi_copyin((const void *)arg, &vtoc, sizeof (vtoc), flag))
2115 			return (EFAULT);
2116 #endif /* _MULTI_DATAMODEL */
2117 
2118 		mutex_enter(&fdc->c_lolock);
2119 
2120 		/*
2121 		 * The characteristics structure must be filled in because
2122 		 * it helps build the vtoc.
2123 		 */
2124 		if ((un->un_chars->fdc_ncyl == 0) ||
2125 		    (un->un_chars->fdc_nhead == 0) ||
2126 		    (un->un_chars->fdc_secptrack == 0)) {
2127 			mutex_exit(&fdc->c_lolock);
2128 			err = EINVAL;
2129 			break;
2130 		}
2131 
2132 		if ((err = fd_build_label_vtoc(un, &vtoc)) != 0) {
2133 			mutex_exit(&fdc->c_lolock);
2134 			break;
2135 		}
2136 
2137 		(void) pm_busy_component(fdc->c_dip, 0);
2138 
2139 		err = fdrw(fdc, unit, FDWRITE, 0, 0, 1,
2140 		    (caddr_t)&un->un_label, sizeof (struct dk_label));
2141 		mutex_exit(&fdc->c_lolock);
2142 		(void) pm_idle_component(fdc->c_dip, 0);
2143 		break;
2144 
2145 	case DKIOCSTATE:
2146 		if (ddi_copyin((caddr_t)arg, (caddr_t)&state,
2147 		    sizeof (int), flag)) {
2148 			err = EFAULT;
2149 			break;
2150 		}
2151 		(void) pm_busy_component(fdc->c_dip, 0);
2152 
2153 		err = fd_check_media(dev, state);
2154 		(void) pm_idle_component(fdc->c_dip, 0);
2155 
2156 		if (ddi_copyout((caddr_t)&un->un_media_state,
2157 		    (caddr_t)arg, sizeof (int), flag))
2158 			err = EFAULT;
2159 		break;
2160 
2161 	case FDIOGCHAR:
2162 		if (ddi_copyout((caddr_t)un->un_chars, (caddr_t)arg,
2163 		    sizeof (struct fd_char), flag))
2164 			err = EFAULT;
2165 		break;
2166 
2167 	case FDIOSCHAR:
2168 		if (ddi_copyin((caddr_t)arg, (caddr_t)&cpy.fdchar,
2169 				sizeof (struct fd_char), flag)) {
2170 			err = EFAULT;
2171 			break;
2172 		}
2173 
2174 		/*
2175 		 * Check the fields in the fdchar structure that are either
2176 		 * driver or controller dependent.
2177 		 */
2178 
2179 		transfer_rate = cpy.fdchar.fdc_transfer_rate;
2180 		if ((transfer_rate != 500) && (transfer_rate != 300) &&
2181 		    (transfer_rate != 250) && (transfer_rate != 1000)) {
2182 			FDERRPRINT(FDEP_L3, FDEM_IOCT,
2183 			    (C, "fd_ioctl: FDIOSCHAR odd transfer rate %d\n",
2184 			    cpy.fdchar.fdc_transfer_rate));
2185 			err = EINVAL;
2186 			break;
2187 		}
2188 
2189 		if ((cpy.fdchar.fdc_nhead < 1) ||
2190 		    (cpy.fdchar.fdc_nhead > 2)) {
2191 			FDERRPRINT(FDEP_L3, FDEM_IOCT,
2192 			    (C, "fd_ioctl: FDIOSCHAR bad no. of heads %d\n",
2193 			    cpy.fdchar.fdc_nhead));
2194 			err = EINVAL;
2195 			break;
2196 		}
2197 
2198 		/*
2199 		 * The number of cylinders must be between 0 and 255
2200 		 */
2201 		if ((cpy.fdchar.fdc_ncyl < 0) || (cpy.fdchar.fdc_ncyl > 255)) {
2202 			FDERRPRINT(FDEP_L3, FDEM_IOCT,
2203 			    (C, "fd_ioctl: FDIOSCHAR bad cyl no %d\n",
2204 			    cpy.fdchar.fdc_ncyl));
2205 			err = EINVAL;
2206 			break;
2207 		}
2208 
2209 		/* Copy the fdchar structure */
2210 
2211 		mutex_enter(&fdc->c_lolock);
2212 		*(un->un_chars) = cpy.fdchar;
2213 
2214 		un->un_curfdtype = -1;
2215 
2216 		mutex_exit(&fdc->c_lolock);
2217 
2218 		break;
2219 	case FDEJECT:  /* eject disk */
2220 	case DKIOCEJECT:
2221 
2222 		/*
2223 		 * Fail the ioctl if auto-eject isn't supported
2224 		 */
2225 		if (fdc->c_un->un_drive->fdd_ejectable == 0) {
2226 
2227 			err = ENOSYS;
2228 
2229 		} else {
2230 			(void) pm_busy_component(fdc->c_dip, 0);
2231 
2232 			mutex_enter(&fdc->c_lolock);
2233 
2234 			CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
2235 
2236 			if (fdc->c_un->un_state == FD_STATE_STOPPED) {
2237 				mutex_exit(&fdc->c_lolock);
2238 				if ((pm_raise_power(fdc->c_dip, 0,
2239 				    PM_LEVEL_ON)) != DDI_SUCCESS) {
2240 					(void) pm_idle_component(fdc->c_dip, 0);
2241 					err = EIO;
2242 				}
2243 				mutex_enter(&fdc->c_lolock);
2244 			}
2245 		}
2246 		if (err == 0) {
2247 			fdselect(fdc, unit, 1);
2248 			fdeject(fdc, unit);
2249 			mutex_exit(&fdc->c_lolock);
2250 		}
2251 
2252 		(void) pm_idle_component(fdc->c_dip, 0);
2253 
2254 		/*
2255 		 * Make sure the drive is turned off
2256 		 */
2257 		if (fdc->c_fdtype & FDCTYPE_82077) {
2258 			if (fdc->c_mtimeid == 0) {
2259 				fdc->c_mtimeid = timeout(fdmotoff, fdc,
2260 				    Motoff_delay);
2261 			}
2262 		}
2263 
2264 		break;
2265 	case FDGETCHANGE: /* disk changed */
2266 
2267 		if (ddi_copyin((caddr_t)arg, (caddr_t)&cpy.temp,
2268 		    sizeof (int), flag)) {
2269 			err = EFAULT;
2270 			break;
2271 		}
2272 
2273 		/* zero out the user's parameter */
2274 		cpy.temp = 0;
2275 
2276 		(void) pm_busy_component(fdc->c_dip, 0);
2277 
2278 		mutex_enter(&fdc->c_lolock);
2279 
2280 		CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
2281 
2282 		if (fdc->c_un->un_state == FD_STATE_STOPPED) {
2283 			mutex_exit(&fdc->c_lolock);
2284 			if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
2285 			    != DDI_SUCCESS) {
2286 				FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power \
2287 				    change failed. \n"));
2288 				(void) pm_idle_component(fdc->c_dip, 0);
2289 				return (EIO);
2290 			}
2291 
2292 			mutex_enter(&fdc->c_lolock);
2293 		}
2294 		if (un->un_flags & FDUNIT_CHANGED)
2295 			cpy.temp |= FDGC_HISTORY;
2296 		else
2297 			cpy.temp &= ~FDGC_HISTORY;
2298 		un->un_flags &= ~FDUNIT_CHANGED;
2299 
2300 		if (fd_pollable) {
2301 			/*
2302 			 * If it's a "pollable" floppy, then we don't
2303 			 * have to do all the fdcheckdisk nastyness to
2304 			 * figure out if the thing is still there.
2305 			 */
2306 			if (fdsense_chng(fdc, unit)) {
2307 				cpy.temp |= FDGC_CURRENT;
2308 			} else {
2309 				cpy.temp &= ~FDGC_CURRENT;
2310 			}
2311 		} else {
2312 
2313 			if (fdsense_chng(fdc, unit)) {
2314 				/*
2315 				 * check disk change signal is asserted.
2316 				 * Now find out if the floppy is
2317 				 * inserted
2318 				 */
2319 				if (fdcheckdisk(fdc, unit)) {
2320 					cpy.temp |= FDGC_CURRENT;
2321 				} else {
2322 					/*
2323 					 * Yes, the floppy was
2324 					 * reinserted. Implies
2325 					 * floppy change.
2326 					 */
2327 					cpy.temp &= ~FDGC_CURRENT;
2328 					cpy.temp |= FDGC_HISTORY;
2329 				}
2330 			} else {
2331 				cpy.temp &= ~FDGC_CURRENT;
2332 			}
2333 		}
2334 
2335 		/*
2336 		 * For a pollable floppy, the floppy_change signal
2337 		 * reflects whether the floppy is in there or not.
2338 		 * We can not detect a floppy change if we don't poll
2339 		 * this signal when the floppy is being changed.
2340 		 * Because as soon as the floppy is put back, the
2341 		 * signal is reset.
2342 		 * BUT the pollable floppies are available only on
2343 		 * Sparcstation Voyager Voyagers (Gypsy) only and
2344 		 * those are motorized floppies. For motorized floppies,
2345 		 * the floppy can only (assuming the user doesn't use a
2346 		 * pin to take out the floppy) be taken out by
2347 		 * issuing 'eject' command which sets the
2348 		 * un->un_ejected flag. So, if the following
2349 		 * condition is true, we can assume there
2350 		 * was a floppy change.
2351 		 */
2352 		if (un->un_ejected && !(cpy.temp & FDGC_CURRENT)) {
2353 			cpy.temp |= FDGC_HISTORY;
2354 		}
2355 		un->un_ejected = 0;
2356 
2357 
2358 		/* return the write-protection status */
2359 		fdgetcsb(fdc);
2360 		if (fdsensedrv(fdc, unit) & WP_SR3) {
2361 			cpy.temp |= FDGC_CURWPROT;
2362 		}
2363 		fdretcsb(fdc);
2364 		mutex_exit(&fdc->c_lolock);
2365 
2366 		if (ddi_copyout((caddr_t)&cpy.temp, (caddr_t)arg,
2367 		    sizeof (int), flag))
2368 			err = EFAULT;
2369 		(void) pm_idle_component(fdc->c_dip, 0);
2370 		break;
2371 
2372 	case FDGETDRIVECHAR:
2373 
2374 		if (ddi_copyin((caddr_t)arg, (caddr_t)&cpy.drvchar,
2375 				sizeof (struct fd_drive), flag)) {
2376 			err = EFAULT;
2377 			break;
2378 		}
2379 
2380 		/*
2381 		 * Return the ejectable value based on the FD_MANUAL_EJECT
2382 		 * property
2383 		 */
2384 		cpy.drvchar.fdd_ejectable = fdc->c_un->un_drive->fdd_ejectable;
2385 		cpy.drvchar.fdd_maxsearch = nfdtypes; /* 3 - hi m lo density */
2386 		if (fd_pollable)	/* pollable device */
2387 			cpy.drvchar.fdd_flags |= FDD_POLLABLE;
2388 
2389 		/* the rest of the fd_drive struct is meaningless to us */
2390 
2391 		if (ddi_copyout((caddr_t)&cpy.drvchar, (caddr_t)arg,
2392 		    sizeof (struct fd_drive), flag))
2393 			err = EFAULT;
2394 		break;
2395 
2396 	case FDSETDRIVECHAR:
2397 		FDERRPRINT(FDEP_L3, FDEM_IOCT,
2398 		    (C, "fd_ioctl: FDSETDRIVECHAR not supportedn\n"));
2399 		err = ENOTTY;
2400 		break;
2401 
2402 	case DKIOCREMOVABLE: {
2403 		int	i = 1;
2404 
2405 		/* no brainer: floppies are always removable */
2406 		if (ddi_copyout((caddr_t)&i, (caddr_t)arg, sizeof (int),
2407 		    flag)) {
2408 			err = EFAULT;
2409 		}
2410 		break;
2411 	}
2412 	case DKIOCGMEDIAINFO:
2413 		err = fd_get_media_info(un, (caddr_t)arg, flag);
2414 		break;
2415 
2416 
2417 	case FDIOCMD:
2418 	{
2419 		struct fd_cmd fc;
2420 		int cyl, hd, spc, spt;
2421 		int nblks; /* total no. of blocks */
2422 
2423 #ifdef _MULTI_DATAMODEL
2424 		switch (ddi_model_convert_from(flag & FMODELS)) {
2425 		case DDI_MODEL_ILP32: {
2426 			struct fd_cmd32 fc32;
2427 
2428 			if (ddi_copyin((const void *)arg, &fc32,
2429 			    sizeof (fc32), flag)) {
2430 				return (EFAULT);
2431 			}
2432 			fc.fdc_cmd	= fc32.fdc_cmd;
2433 			fc.fdc_flags	= fc32.fdc_flags;
2434 			fc.fdc_blkno	= (daddr_t)fc32.fdc_blkno;
2435 			fc.fdc_secnt	= fc32.fdc_secnt;
2436 			fc.fdc_bufaddr	= (caddr_t)(uintptr_t)fc32.fdc_bufaddr;
2437 			fc.fdc_buflen	= fc32.fdc_buflen;
2438 			fc.fdc_cmd	= fc32.fdc_cmd;
2439 
2440 			break;
2441 		}
2442 
2443 		case DDI_MODEL_NONE:
2444 			if (ddi_copyin((const void *)arg, &fc,
2445 			    sizeof (fc), flag)) {
2446 				return (EFAULT);
2447 			}
2448 			break;
2449 		}
2450 #else /* ! _MULTI_DATAMODEL */
2451 		if (ddi_copyin((const void *)arg, &fc, sizeof (fc), flag)) {
2452 			return (EFAULT);
2453 		}
2454 #endif /* _MULTI_DATAMODEL */
2455 
2456 		if (fc.fdc_cmd == FDCMD_READ || fc.fdc_cmd == FDCMD_WRITE) {
2457 			auto struct iovec aiov;
2458 			auto struct uio auio;
2459 			struct uio *uio = &auio;
2460 
2461 			spc = (fc.fdc_cmd == FDCMD_READ)? B_READ: B_WRITE;
2462 
2463 			bzero(&auio, sizeof (struct uio));
2464 			bzero(&aiov, sizeof (struct iovec));
2465 			aiov.iov_base = fc.fdc_bufaddr;
2466 			aiov.iov_len = (uint_t)fc.fdc_secnt * sec_size;
2467 			uio->uio_iov = &aiov;
2468 
2469 			uio->uio_iovcnt = 1;
2470 			uio->uio_resid = aiov.iov_len;
2471 			uio->uio_segflg = UIO_USERSPACE;
2472 			FDERRPRINT(FDEP_L2, FDEM_IOCT,
2473 			    (C, "fd_ioctl: call physio\n"));
2474 			err = physio(fd_strategy, NULL, dev,
2475 			    spc, minphys, uio);
2476 			break;
2477 		} else if (fc.fdc_cmd != FDCMD_FORMAT_TRACK) {
2478 
2479 			/*
2480 			 * The manpage states that only the FDCMD_WRITE,
2481 			 * FDCMD_READ, and the FDCMD_FORMAT_TR are available.
2482 			 */
2483 			FDERRPRINT(FDEP_L1, FDEM_IOCT,
2484 			    (C, "fd_ioctl: FDIOCMD invalid command\n"));
2485 			err = EINVAL;
2486 			break;
2487 		}
2488 
2489 		/* The command is FDCMD_FORMAT_TRACK */
2490 
2491 		spt = un->un_chars->fdc_secptrack;	/* sec/trk */
2492 		spc = un->un_chars->fdc_nhead * spt;	/* sec/cyl */
2493 		cyl = fc.fdc_blkno / spc;
2494 		hd = (fc.fdc_blkno % spc) / spt;
2495 
2496 		/*
2497 		 * Make sure the specified block number is in the correct
2498 		 * range. (block numbers start at 0)
2499 		 */
2500 		nblks = spc * un->un_chars->fdc_ncyl;
2501 
2502 		if (fc.fdc_blkno < 0 || fc.fdc_blkno > (nblks - 1)) {
2503 			err = EINVAL;
2504 			break;
2505 		}
2506 
2507 		(void) pm_busy_component(fdc->c_dip, 0);
2508 
2509 		mutex_enter(&fdc->c_lolock);
2510 		CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
2511 		if (fdc->c_un->un_state == FD_STATE_STOPPED) {
2512 			mutex_exit(&fdc->c_lolock);
2513 			if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
2514 			    != DDI_SUCCESS) {
2515 				FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power \
2516 				    change failed. \n"));
2517 				(void) pm_idle_component(fdc->c_dip, 0);
2518 				return (EIO);
2519 			}
2520 
2521 			mutex_enter(&fdc->c_lolock);
2522 		}
2523 
2524 		if (fdformat(fdc, unit, cyl, hd))
2525 			err = EIO;
2526 
2527 		mutex_exit(&fdc->c_lolock);
2528 		(void) pm_idle_component(fdc->c_dip, 0);
2529 
2530 		break;
2531 	}
2532 
2533 	case FDRAW:
2534 
2535 		(void) pm_busy_component(fdc->c_dip, 0);
2536 		err = fdrawioctl(fdc, unit, arg, flag);
2537 
2538 		(void) pm_idle_component(fdc->c_dip, 0);
2539 
2540 		break;
2541 #ifdef FD_DEBUG
2542 	case IOCTL_DEBUG:
2543 		fderrlevel--;
2544 		if (fderrlevel < 0)
2545 			fderrlevel = 3;
2546 		cmn_err(C, "fdioctl: CHANGING debug to %d", fderrlevel);
2547 		return (0);
2548 #endif /* FD_DEBUG */
2549 	default:
2550 		FDERRPRINT(FDEP_L2, FDEM_IOCT,
2551 		    (C, "fd_ioctl: invalid ioctl 0x%x\n", cmd));
2552 		err = ENOTTY;
2553 		break;
2554 	}
2555 
2556 	return (err);
2557 }
2558 
2559 /*
2560  * fdrawioctl
2561  *
2562  * - acquires the low level lock
2563  */
2564 
2565 static int
fdrawioctl(struct fdctlr * fdc,int unit,intptr_t arg,int mode)2566 fdrawioctl(struct fdctlr *fdc, int unit, intptr_t arg, int mode)
2567 {
2568 	struct fd_raw fdr;
2569 #ifdef _MULTI_DATAMODEL
2570 	struct fd_raw32 fdr32;
2571 #endif
2572 	struct fdcsb *csb;
2573 	int i, err, flag;
2574 	caddr_t fa;
2575 	uint_t	fc;
2576 	size_t	real_length;
2577 	int	res;
2578 	ddi_device_acc_attr_t attr;
2579 	ddi_acc_handle_t	mem_handle;
2580 
2581 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
2582 	attr.devacc_attr_endian_flags  = DDI_STRUCTURE_BE_ACC;
2583 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
2584 
2585 	ASSERT(fdc->c_un->un_unit_no == unit);
2586 
2587 	flag = B_READ;
2588 	err = 0;
2589 	fa = NULL;
2590 	fc = (uint_t)0;
2591 
2592 	/* Copy in the arguments */
2593 	switch (ddi_model_convert_from(mode)) {
2594 #ifdef _MULTI_DATAMODEL
2595 	case DDI_MODEL_ILP32:
2596 		if (ddi_copyin((caddr_t)arg, (caddr_t)&fdr32,
2597 		    sizeof (fdr32), mode)) {
2598 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2599 			    (C, "fdrawioctl: copyin error, args32\n"));
2600 			return (EFAULT);
2601 		}
2602 		bcopy(fdr32.fdr_cmd, fdr.fdr_cmd, sizeof (fdr.fdr_cmd));
2603 		fdr.fdr_cnum = fdr32.fdr_cnum;
2604 		bcopy(fdr32.fdr_result, fdr.fdr_result,
2605 		    sizeof (fdr.fdr_result));
2606 		fdr.fdr_nbytes = fdr32.fdr_nbytes;
2607 		fdr.fdr_addr = (caddr_t)(uintptr_t)fdr32.fdr_addr;
2608 		break;
2609 #endif
2610 	default:
2611 	case DDI_MODEL_NONE:
2612 		if (ddi_copyin((caddr_t)arg, (caddr_t)&fdr,
2613 		    sizeof (fdr), mode)) {
2614 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2615 			    (C, "fdrawioctl: copyin error, args\n"));
2616 			return (EFAULT);
2617 		}
2618 		break;
2619 	}
2620 
2621 	FDERRPRINT(FDEP_L1, FDEM_RAWI,
2622 	    (C, "fdrawioctl: cmd[0]=0x%x\n", fdr.fdr_cmd[0]));
2623 
2624 	mutex_enter(&fdc->c_lolock);
2625 
2626 	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
2627 
2628 	if (fdc->c_un->un_state == FD_STATE_STOPPED) {
2629 		mutex_exit(&fdc->c_lolock);
2630 		if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
2631 		    != DDI_SUCCESS) {
2632 			FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power change \
2633 			    failed. \n"));
2634 
2635 			(void) pm_idle_component(fdc->c_dip, 0);
2636 			return (EIO);
2637 		}
2638 		mutex_enter(&fdc->c_lolock);
2639 	}
2640 
2641 	fdgetcsb(fdc);
2642 	csb = &fdc->c_csb;
2643 	csb->csb_unit = (uchar_t)unit;
2644 
2645 	/* copy cmd bytes into csb */
2646 	for (i = 0; i <= fdr.fdr_cnum; i++)
2647 		csb->csb_cmds[i] = fdr.fdr_cmd[i];
2648 	csb->csb_ncmds = (uchar_t)fdr.fdr_cnum;
2649 
2650 	csb->csb_maxretry = 0;	/* let the application deal with errors */
2651 	csb->csb_retrys = 0;
2652 
2653 	switch (fdr.fdr_cmd[0] & 0x0f) {
2654 
2655 	case FDRAW_SPECIFY:
2656 		/*
2657 		 * Ensure that the right DMA mode is selected.  There is
2658 		 * currently no way for the user to tell if DMA is
2659 		 * happening so set the value for the user.
2660 		 */
2661 
2662 		if (fdc->c_fdtype & FDCTYPE_DMA)
2663 			csb->csb_cmds[2] = csb->csb_cmds[2] & 0xFE;
2664 		else
2665 			csb->csb_cmds[2] = csb->csb_cmds[2] | 0x1;
2666 
2667 		csb->csb_opflags = CSB_OFNORESULTS;
2668 		csb->csb_nrslts = 0;
2669 		break;
2670 
2671 	case FDRAW_SENSE_DRV:
2672 		/* Insert the appropriate drive number */
2673 		csb->csb_cmds[1] = csb->csb_cmds[1] | (unit & DRV_MASK);
2674 		csb->csb_opflags = CSB_OFIMMEDIATE;
2675 		csb->csb_nrslts = 1;
2676 		break;
2677 
2678 	case FDRAW_REZERO:
2679 	case FDRAW_SEEK:
2680 		/* Insert the appropriate drive number */
2681 		csb->csb_cmds[1] = csb->csb_cmds[1] | (unit & DRV_MASK);
2682 		csb->csb_opflags = CSB_OFSEEKOPS + CSB_OFTIMEIT;
2683 		csb->csb_nrslts = 2;
2684 		break;
2685 
2686 	case FDRAW_FORMAT:
2687 		FDERRPRINT(FDEP_L1, FDEM_RAWI,
2688 		    (C, "fdrawioctl: cmd is fdfraw format\n"));
2689 
2690 		/* Insert the appropriate drive number */
2691 		csb->csb_cmds[1] = csb->csb_cmds[1] | (unit & DRV_MASK);
2692 		csb->csb_opflags = CSB_OFXFEROPS + CSB_OFTIMEIT;
2693 		csb->csb_nrslts = NRBRW;
2694 		flag = B_WRITE;
2695 
2696 		/*
2697 		 * Allocate memory for the command.
2698 		 * If PIO is being used, then add an extra 16 bytes
2699 		 */
2700 		if (fdc->c_fdtype & FDCTYPE_DMA) {
2701 
2702 			fc = (uint_t)(fdr.fdr_nbytes);
2703 			mutex_enter(&fdc->c_hilock);
2704 
2705 			res = ddi_dma_mem_alloc(fdc->c_dmahandle, fc,
2706 			    &attr, DDI_DMA_STREAMING,
2707 			    DDI_DMA_DONTWAIT, 0, &fa, &real_length,
2708 			    &mem_handle);
2709 
2710 			if (res != DDI_SUCCESS) {
2711 				fdretcsb(fdc);
2712 				mutex_exit(&fdc->c_lolock);
2713 				mutex_exit(&fdc->c_hilock);
2714 				return (EIO);
2715 			}
2716 
2717 			fdc->c_csb.csb_read = CSB_WRITE;
2718 			if (fdstart_dma(fdc, fa, fc) != 0) {
2719 				ddi_dma_mem_free(&mem_handle);
2720 				fdretcsb(fdc);
2721 				mutex_exit(&fdc->c_lolock);
2722 				mutex_exit(&fdc->c_hilock);
2723 				return (EIO);
2724 			}
2725 			mutex_exit(&fdc->c_hilock);
2726 
2727 		} else {
2728 			fc = (uint_t)(fdr.fdr_nbytes + 16);
2729 			fa = kmem_zalloc(fc, KM_SLEEP);
2730 		}
2731 
2732 		/* copy in the user's command bytes */
2733 		if (ddi_copyin(fdr.fdr_addr, fa,
2734 		    (uint_t)fdr.fdr_nbytes, mode)) {
2735 			fdretcsb(fdc);
2736 			mutex_exit(&fdc->c_lolock);
2737 
2738 			if (fdc->c_fdtype & FDCTYPE_DMA) {
2739 				ddi_dma_mem_free(&mem_handle);
2740 				FDERRPRINT(FDEP_L1, FDEM_RAWI,
2741 				    (C, "fdrawioctl: (err)free dma memory\n"));
2742 			} else {
2743 				kmem_free(fa, fc);
2744 			}
2745 
2746 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2747 			    (C, "fdrawioctl: ddi_copyin error\n"));
2748 			return (EFAULT);
2749 		}
2750 
2751 		break;
2752 	case FDRAW_WRCMD:
2753 	case FDRAW_WRITEDEL:
2754 		flag = B_WRITE;
2755 		/* FALLTHROUGH */
2756 	case FDRAW_RDCMD:
2757 	case FDRAW_READDEL:
2758 	case FDRAW_READTRACK:
2759 		/* Insert the appropriate drive number */
2760 		csb->csb_cmds[1] = csb->csb_cmds[1] | (unit & DRV_MASK);
2761 		if (fdc->c_fdtype & FDCTYPE_SB)
2762 			csb->csb_cmds[1] |= IPS;
2763 		csb->csb_opflags = CSB_OFXFEROPS + CSB_OFTIMEIT;
2764 		csb->csb_nrslts = NRBRW;
2765 		break;
2766 
2767 	default:
2768 		fdretcsb(fdc);
2769 		mutex_exit(&fdc->c_lolock);
2770 		return (EINVAL);
2771 	}
2772 
2773 	if ((csb->csb_opflags & CSB_OFXFEROPS) && (fdr.fdr_nbytes == 0)) {
2774 		fdretcsb(fdc);
2775 		mutex_exit(&fdc->c_lolock);
2776 		return (EINVAL);
2777 	}
2778 	csb->csb_opflags |= CSB_OFRAWIOCTL;
2779 
2780 	FDERRPRINT(FDEP_L1, FDEM_RAWI,
2781 	    (C, "fdrawioctl: nbytes = %u\n", fdr.fdr_nbytes));
2782 
2783 	if ((fdr.fdr_cmd[0] & 0x0f) != FDRAW_FORMAT) {
2784 		if ((fc = (uint_t)fdr.fdr_nbytes) > 0) {
2785 			/*
2786 			 * In SunOS 4.X, we used to as_fault things in.
2787 			 * We really cannot do this in 5.0/SVr4. Unless
2788 			 * someone really believes that speed is of the
2789 			 * essence here, it is just much simpler to do
2790 			 * this in kernel space and use copyin/copyout.
2791 			 */
2792 			if (fdc->c_fdtype & FDCTYPE_DMA) {
2793 				mutex_enter(&fdc->c_hilock);
2794 				res = ddi_dma_mem_alloc(fdc->c_dmahandle, fc,
2795 				    &attr, DDI_DMA_STREAMING,
2796 				    DDI_DMA_DONTWAIT, 0, &fa, &real_length,
2797 				    &mem_handle);
2798 
2799 				if (res != DDI_SUCCESS) {
2800 					fdretcsb(fdc);
2801 					mutex_exit(&fdc->c_lolock);
2802 					mutex_exit(&fdc->c_hilock);
2803 					return (EIO);
2804 				}
2805 
2806 				if (flag == B_WRITE)
2807 					fdc->c_csb.csb_read = CSB_WRITE;
2808 				else
2809 					fdc->c_csb.csb_read = CSB_READ;
2810 
2811 				if (fdstart_dma(fdc, fa, fc) != 0) {
2812 					ddi_dma_mem_free(&mem_handle);
2813 					fdretcsb(fdc);
2814 					mutex_exit(&fdc->c_lolock);
2815 					mutex_exit(&fdc->c_hilock);
2816 					return (EIO);
2817 				}
2818 				mutex_exit(&fdc->c_hilock);
2819 
2820 			} else {
2821 				fa = kmem_zalloc(fc, KM_SLEEP);
2822 			}
2823 
2824 			if (flag == B_WRITE) {
2825 				if (ddi_copyin(fdr.fdr_addr, fa, fc, mode)) {
2826 					if (fdc->c_fdtype & FDCTYPE_DMA)
2827 						ddi_dma_mem_free(&mem_handle);
2828 					else
2829 						kmem_free(fa, fc);
2830 					fdretcsb(fdc);
2831 					mutex_exit(&fdc->c_lolock);
2832 					FDERRPRINT(FDEP_L1, FDEM_RAWI, (C,
2833 					    "fdrawioctl: can't copy data\n"));
2834 
2835 					return (EFAULT);
2836 				}
2837 			}
2838 			csb->csb_addr = fa;
2839 			csb->csb_len = fc;
2840 		} else {
2841 			csb->csb_addr = 0;
2842 			csb->csb_len = 0;
2843 		}
2844 	} else {
2845 		csb->csb_addr = fa;
2846 		csb->csb_len = fc;
2847 	}
2848 
2849 	FDERRPRINT(FDEP_L1, FDEM_RAWI,
2850 	    (C, "cmd: %x %x %x %x %x %x %x %x %x %x\n", csb->csb_cmds[0],
2851 	    csb->csb_cmds[1], csb->csb_cmds[2], csb->csb_cmds[3],
2852 	    csb->csb_cmds[4], csb->csb_cmds[5], csb->csb_cmds[6],
2853 	    csb->csb_cmds[7], csb->csb_cmds[8], csb->csb_cmds[9]));
2854 	FDERRPRINT(FDEP_L1, FDEM_RAWI,
2855 	    (C, "nbytes: %x, opflags: %x, addr: %p, len: %x\n",
2856 	    csb->csb_ncmds, csb->csb_opflags, (void *)csb->csb_addr,
2857 	    csb->csb_len));
2858 
2859 
2860 	/*
2861 	 * Note that we ignore any error return s from fdexec.
2862 	 * This is the way the driver has been, and it may be
2863 	 * that the raw ioctl senders simply don't want to
2864 	 * see any errors returned in this fashion.
2865 	 */
2866 
2867 	if ((csb->csb_opflags & CSB_OFNORESULTS) ||
2868 	    (csb->csb_opflags & CSB_OFIMMEDIATE)) {
2869 		(void) fdexec(fdc, 0); /* don't sleep, don't check change */
2870 	} else {
2871 		(void) fdexec(fdc, FDXC_SLEEP | FDXC_CHECKCHG);
2872 	}
2873 
2874 
2875 	FDERRPRINT(FDEP_L1, FDEM_RAWI,
2876 	    (C, "rslt: %x %x %x %x %x %x %x %x %x %x\n", csb->csb_rslt[0],
2877 	    csb->csb_rslt[1], csb->csb_rslt[2], csb->csb_rslt[3],
2878 	    csb->csb_rslt[4], csb->csb_rslt[5], csb->csb_rslt[6],
2879 	    csb->csb_rslt[7], csb->csb_rslt[8], csb->csb_rslt[9]));
2880 
2881 	if ((fdr.fdr_cmd[0] & 0x0f) != FDRAW_FORMAT && fc &&
2882 	    flag == B_READ && err == 0) {
2883 		if (ddi_copyout(fa, fdr.fdr_addr, fc, mode)) {
2884 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2885 			    (C, "fdrawioctl: can't copy read data\n"));
2886 
2887 			err = EFAULT;
2888 		}
2889 	}
2890 
2891 
2892 	if (fc) {
2893 		if (fdc->c_fdtype & FDCTYPE_DMA) {
2894 			ddi_dma_mem_free(&mem_handle);
2895 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2896 			    (C, "fdrawioctl: free dma memory\n"));
2897 		} else {
2898 			kmem_free(fa, fc);
2899 		}
2900 	}
2901 
2902 
2903 	/* copy cmd results into fdr */
2904 	for (i = 0; (int)i <= (int)csb->csb_nrslts; i++)
2905 		fdr.fdr_result[i] = csb->csb_rslt[i];
2906 	fdr.fdr_nbytes = fdc->c_csb.csb_rlen; /* return resid */
2907 
2908 	switch (ddi_model_convert_from(mode)) {
2909 #ifdef _MULTI_DATAMODEL
2910 	case DDI_MODEL_ILP32:
2911 		bcopy(fdr.fdr_cmd, fdr32.fdr_cmd, sizeof (fdr32.fdr_cmd));
2912 		fdr32.fdr_cnum = fdr.fdr_cnum;
2913 		bcopy(fdr.fdr_result, fdr32.fdr_result,
2914 		    sizeof (fdr32.fdr_result));
2915 		fdr32.fdr_nbytes = fdr.fdr_nbytes;
2916 		fdr32.fdr_addr = (caddr32_t)(uintptr_t)fdr.fdr_addr;
2917 		if (ddi_copyout(&fdr32, (caddr_t)arg, sizeof (fdr32), mode)) {
2918 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2919 			    (C, "fdrawioctl: can't copy results32\n"));
2920 			err = EFAULT;
2921 		}
2922 		break;
2923 #endif
2924 	case DDI_MODEL_NONE:
2925 	default:
2926 		if (ddi_copyout(&fdr, (caddr_t)arg, sizeof (fdr), mode)) {
2927 			FDERRPRINT(FDEP_L1, FDEM_RAWI,
2928 			    (C, "fdrawioctl: can't copy results\n"));
2929 			err = EFAULT;
2930 		}
2931 		break;
2932 	}
2933 
2934 	fdretcsb(fdc);
2935 	mutex_exit(&fdc->c_lolock);
2936 	return (0);
2937 }
2938 
2939 /*
2940  * fdformat
2941  *	format a track
2942  * For PIO, builds a table of sector data values with 16 bytes
2943  * (sizeof fdc's fifo) of dummy on end.	 This is so than when fdc->c_len
2944  * goes to 0 and fd_intr sends a TC that all the real formatting will
2945  * have already been done.
2946  *
2947  *	- called with the low level lock held
2948  */
2949 static int
fdformat(struct fdctlr * fdc,int unit,int cyl,int hd)2950 fdformat(struct fdctlr *fdc, int unit, int cyl, int hd)
2951 {
2952 	struct fdcsb *csb;
2953 	struct fdunit *un;
2954 	struct fd_char *ch;
2955 	int	cmdresult;
2956 	uchar_t	*fmthdrs;
2957 	caddr_t fd;
2958 	int	i;
2959 	size_t	real_length;
2960 	ddi_device_acc_attr_t attr;
2961 	ddi_acc_handle_t mem_handle;
2962 
2963 	FDERRPRINT(FDEP_L1, FDEM_FORM,
2964 	    (C, "fdformat cyl %d, hd %d\n", cyl, hd));
2965 	fdgetcsb(fdc);
2966 
2967 	ASSERT(fdc->c_un->un_unit_no == unit);
2968 
2969 	csb = &fdc->c_csb;
2970 	un = fdc->c_un;
2971 	ch = un->un_chars;
2972 
2973 	/* setup common things in csb */
2974 	csb->csb_unit = (uchar_t)unit;
2975 
2976 	/*
2977 	 * The controller needs to do a seek before
2978 	 * each format to get to right cylinder.
2979 	 */
2980 	if (fdrecalseek(fdc, unit, cyl, FDXC_CHECKCHG)) {
2981 		fdretcsb(fdc);
2982 		return (EIO);
2983 	}
2984 
2985 	/*
2986 	 * now do the format itself
2987 	 */
2988 	csb->csb_nrslts = NRBRW;
2989 	csb->csb_opflags = CSB_OFXFEROPS | CSB_OFTIMEIT;
2990 
2991 	csb->csb_cmds[0] = FDRAW_FORMAT;
2992 	/* always or in MFM bit */
2993 	csb->csb_cmds[0] |= MFM;
2994 	csb->csb_cmds[1] = (hd << 2) | (unit & 0x03);
2995 	csb->csb_cmds[2] = ch->fdc_medium ? 3 : 2;
2996 	csb->csb_cmds[3] = ch->fdc_secptrack;
2997 	csb->csb_cmds[4] = GPLF;
2998 	csb->csb_cmds[5] = FDATA;
2999 	csb->csb_ncmds = 6;
3000 	csb->csb_maxretry = rwretry;
3001 	csb->csb_retrys = 0;
3002 
3003 	/*
3004 	 * NOTE: have to add size of fifo also - for dummy format action
3005 	 * if PIO is being used.
3006 	 */
3007 
3008 
3009 	if (fdc->c_fdtype & FDCTYPE_DMA) {
3010 
3011 		csb->csb_len = (uint_t)4 * ch->fdc_secptrack;
3012 
3013 		attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
3014 		attr.devacc_attr_endian_flags  = DDI_STRUCTURE_BE_ACC;
3015 		attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
3016 
3017 		mutex_enter(&fdc->c_hilock);
3018 
3019 		cmdresult = ddi_dma_mem_alloc(fdc->c_dmahandle, csb->csb_len,
3020 		    &attr, DDI_DMA_STREAMING,
3021 		    DDI_DMA_DONTWAIT, 0, &fd, &real_length,
3022 		    &mem_handle);
3023 
3024 		if (cmdresult != DDI_SUCCESS) {
3025 			mutex_exit(&fdc->c_hilock);
3026 			return (cmdresult);
3027 		}
3028 
3029 		fdc->c_csb.csb_read = CSB_WRITE;
3030 		if (fdstart_dma(fdc, fd,  csb->csb_len) != 0) {
3031 			ddi_dma_mem_free(&mem_handle);
3032 			mutex_exit(&fdc->c_hilock);
3033 			return (-1);
3034 		}
3035 		mutex_exit(&fdc->c_hilock);
3036 
3037 
3038 	} else {
3039 		csb->csb_len = (uint_t)4 * ch->fdc_secptrack + 16;
3040 		fd = kmem_zalloc(csb->csb_len, KM_SLEEP);
3041 		fmthdrs = (uchar_t *)fd;
3042 	}
3043 
3044 	csb->csb_addr = (caddr_t)fd;
3045 
3046 	for (i = 1; i <= ch->fdc_secptrack; i++) {
3047 		*fd++ = (uchar_t)cyl;		/* cylinder */
3048 		*fd++ = (uchar_t)hd;		/* head */
3049 		*fd++ = (uchar_t)i;	/* sector number */
3050 		*fd++ = ch->fdc_medium ? 3 : 2; /* sec_size code */
3051 	}
3052 
3053 	if ((cmdresult = fdexec(fdc, FDXC_SLEEP | FDXC_CHECKCHG)) == 0) {
3054 		if (csb->csb_cmdstat)
3055 			cmdresult = EIO;	/* XXX TBD NYD for now */
3056 	}
3057 
3058 	if (fdc->c_fdtype & FDCTYPE_DMA) {
3059 		ddi_dma_mem_free(&mem_handle);
3060 	} else {
3061 		kmem_free((caddr_t)fmthdrs, csb->csb_len);
3062 	}
3063 
3064 	fdretcsb(fdc);
3065 
3066 	return (cmdresult);
3067 }
3068 
3069 /*
3070  * fdstart
3071  *	called from fd_strategy() or from fdXXXX() to setup and
3072  *	start operations of read or write only (using buf structs).
3073  *	Because the chip doesn't handle crossing cylinder boundaries on
3074  *	the fly, this takes care of those boundary conditions.	Note that
3075  *	it sleeps until the operation is done *within fdstart* - so that
3076  *	when fdstart returns, the operation is already done.
3077  *
3078  *	- called with the low level lock held
3079  *
3080  */
3081 
3082 static int slavio_index_pulse_work_around = 0;
3083 
3084 static void
fdstart(struct fdctlr * fdc)3085 fdstart(struct fdctlr *fdc)
3086 {
3087 	struct buf *bp;
3088 	struct fdcsb *csb;
3089 	struct fdunit *un;
3090 	struct fd_char *ch;
3091 	struct dk_map32 *dkm;
3092 	uint_t	part;		/* partition number for the transfer */
3093 	uint_t	start_part;	/* starting block of the partition */
3094 	uint_t	last_part;	/* last block of the partition */
3095 	uint_t	blk;		/* starting block of transfer on diskette */
3096 	uint_t	sect;		/* starting block's offset into track */
3097 	uint_t	cyl;		/* starting cylinder of the transfer */
3098 	uint_t	bincyl;		/* starting blocks's offset into cylinder */
3099 	uint_t	secpcyl;	/* number of sectors per cylinder */
3100 	uint_t	phys_blkno;	/* no. of blocks on the diskette */
3101 	uint_t	head;		/* one of two diskette heads */
3102 	uint_t	unit;
3103 	uint_t	len, tlen;
3104 	caddr_t addr;
3105 	caddr_t temp_addr;
3106 	uint_t	partial_read = 0;
3107 	int sb_temp_buf_used = 0;
3108 
3109 	bp = fdc->c_actf;
3110 
3111 	while (bp != NULL) {
3112 
3113 		fdc->c_actf = bp->av_forw;
3114 		fdc->c_current = bp;
3115 
3116 		/*
3117 		 * Initialize the buf structure.  The residual count is
3118 		 * initially the number of bytes to be read or written
3119 		 */
3120 		bp->b_flags &= ~B_ERROR;
3121 		bp->b_error = 0;
3122 		bp->b_resid = bp->b_bcount;
3123 		bp_mapin(bp);			/* map in buffers */
3124 
3125 		addr = bp->b_un.b_addr;		/* assign buffer address */
3126 
3127 		/*
3128 		 * Find the unit and partition numbers.
3129 		 */
3130 		unit = fdc->c_un->un_unit_no;
3131 		un = fdc->c_un;
3132 		ch = un->un_chars;
3133 		part = FDPARTITION(bp->b_edev);
3134 		dkm = &un->un_label.dkl_map[part];
3135 
3136 		if (un->un_chars->fdc_medium) {
3137 			phys_blkno = bp->b_blkno >> 1;
3138 		} else {
3139 			phys_blkno = bp->b_blkno;
3140 		}
3141 
3142 		if (un->un_iostat) {
3143 			kstat_waitq_to_runq(KIOSP);
3144 		}
3145 
3146 		FDERRPRINT(FDEP_L1, FDEM_STRT,
3147 		    (C, "fdstart: bp=0x%p blkno=0x%x bcount=0x%x\n",
3148 		    (void *)bp, (int)bp->b_blkno, (int)bp->b_bcount));
3149 
3150 		/*
3151 		 * Get the csb and initialize the values that are the same
3152 		 * for DMA and PIO.
3153 		 */
3154 		fdgetcsb(fdc);		/* get csb (maybe wait for it) */
3155 		csb = &fdc->c_csb;
3156 		csb->csb_unit = unit;		/* floppy unit number */
3157 
3158 
3159 		/*
3160 		 * bugID:4133425 : If the controller is SLAVIO, and
3161 		 * the read does not reach end of track, then modify
3162 		 * the tlen to read until the end of track to a temp
3163 		 * buffer and disable MT. After the read is over,
3164 		 * copy the useful portion of the data to 'addr'.
3165 		 * Enable this feature only when
3166 		 * slavio_index_pulse_work_aound variable is
3167 		 * set in /etc/system.
3168 		 */
3169 
3170 
3171 		if (bp->b_flags & B_READ) {
3172 			if (((fdc->c_fdtype & FDCTYPE_SLAVIO) &&
3173 			    slavio_index_pulse_work_around) ||
3174 			    (fdc->c_fdtype & FDCTYPE_TCBUG))
3175 				csb->csb_cmds[0] = SK | FDRAW_RDCMD | MFM;
3176 			else
3177 				csb->csb_cmds[0] = MT | SK | FDRAW_RDCMD | MFM;
3178 		} else {
3179 			if (fdc->c_fdtype & FDCTYPE_TCBUG)
3180 				csb->csb_cmds[0] = FDRAW_WRCMD | MFM;
3181 			else
3182 				csb->csb_cmds[0] = MT | FDRAW_WRCMD | MFM;
3183 		}
3184 
3185 
3186 		if (bp->b_flags & B_READ)
3187 			fdc->c_csb.csb_read = CSB_READ;
3188 		else
3189 			fdc->c_csb.csb_read = CSB_WRITE;
3190 
3191 
3192 		csb->csb_cmds[5] = ch->fdc_medium ? 3 : 2; /* sector size  */
3193 		csb->csb_cmds[6] = ch->fdc_secptrack; /* EOT-# of sectors/trk */
3194 		csb->csb_cmds[7] = GPLN;	/* GPL - gap 3 size code */
3195 		csb->csb_cmds[8] = SSSDTL;	/* DTL - be 0xFF if N != 0 */
3196 
3197 		csb->csb_ncmds = NCBRW;		/* number of command bytes */
3198 		csb->csb_nrslts = NRBRW;	/* number of result bytes */
3199 
3200 
3201 		/*
3202 		 * opflags for interrupt handler, et.al.
3203 		 */
3204 		csb->csb_opflags = CSB_OFXFEROPS | CSB_OFTIMEIT;
3205 
3206 
3207 		/*
3208 		 * Make sure the transfer does not go off the end
3209 		 * of the partition.  Limit the actual amount transferred
3210 		 * to fit the partition.
3211 		 */
3212 
3213 		blk = phys_blkno;
3214 		start_part = (dkm->dkl_cylno * ch->fdc_secptrack
3215 		    * ch->fdc_nhead);
3216 		blk = blk + start_part;
3217 		last_part = start_part + dkm->dkl_nblk;
3218 
3219 		if ((blk + (bp->b_bcount / ch->fdc_sec_size)) > last_part)
3220 			len = (last_part - blk) * ch->fdc_sec_size;
3221 		else
3222 			len = (uint_t)bp->b_bcount;
3223 
3224 		/*
3225 		 * now we have the real start blk,
3226 		 * addr and len for xfer op
3227 		 * sectors per cylinder
3228 		 */
3229 		secpcyl = ch->fdc_nhead * ch->fdc_secptrack;
3230 
3231 		/*
3232 		 * The controller can transfer up to a cylinder at a time.
3233 		 * Early revs of the 82077 have a bug that causes the chip to
3234 		 * fail to respond to the Terminal Count signal.  Due to this
3235 		 * bug, controllers with type FDCTYPE_TCBUG, only transfer up
3236 		 * to a track at a time.
3237 		 * See earlier comment for bugID:4133425 for index pulse
3238 		 * work around.
3239 		 */
3240 
3241 		while (len != 0) {
3242 
3243 			cyl = blk / secpcyl;	/* cylinder of transfer */
3244 			bincyl = blk % secpcyl;	/* blk within cylinder */
3245 			head = bincyl / ch->fdc_secptrack;
3246 			sect = (bincyl % ch->fdc_secptrack) + 1;
3247 						/* sect w/in track */
3248 
3249 			/*
3250 			 * If the desired block and length will go beyond the
3251 			 * cylinder end, limit it to the cylinder end.
3252 			 */
3253 
3254 			if ((fdc->c_fdtype & FDCTYPE_SLAVIO) &&
3255 			    slavio_index_pulse_work_around &&
3256 			    (fdc->c_csb.csb_read == CSB_READ)) {
3257 
3258 				tlen = (ch->fdc_secptrack - sect + 1) *
3259 				    ch->fdc_sec_size;
3260 				if (len < tlen) {
3261 					partial_read = 1;
3262 					temp_addr = (caddr_t)kmem_alloc(tlen,
3263 					    KM_SLEEP);
3264 				}
3265 
3266 			} else if (fdc->c_fdtype & FDCTYPE_TCBUG) {
3267 				tlen = len;
3268 				if (len > ((ch->fdc_secptrack - sect + 1) *
3269 				    ch->fdc_sec_size))
3270 					tlen = (ch->fdc_secptrack - sect + 1)
3271 					    * ch->fdc_sec_size;
3272 			} else {
3273 				if (len > ((secpcyl - bincyl)
3274 				    * ch->fdc_sec_size))
3275 					tlen = (secpcyl - bincyl)
3276 					    * ch->fdc_sec_size;
3277 
3278 				else
3279 					tlen = len;
3280 			}
3281 			if (fdc->c_fdtype & FDCTYPE_SB) {
3282 				/*
3283 				 * To avoid underrun errors during IFB activity.
3284 				 */
3285 				if (tlen > max_fd_dma_len)
3286 					tlen = max_fd_dma_len;
3287 			}
3288 
3289 			FDERRPRINT(FDEP_L1, FDEM_STRT,
3290 			    (C, "	blk 0x%x, addr 0x%p, len 0x%x\n",
3291 			    blk, (void *)addr, len));
3292 			FDERRPRINT(FDEP_L1, FDEM_STRT,
3293 			    (C, "cyl:%x, head:%x, sec:%x\n",
3294 			    cyl, head, sect));
3295 
3296 			FDERRPRINT(FDEP_L1, FDEM_STRT,
3297 			    (C, "	resid 0x%lx, tlen %d\n",
3298 			    bp->b_resid, tlen));
3299 
3300 			/*
3301 			 * Finish programming the command
3302 			 */
3303 			csb->csb_cmds[1] = (head << 2) | unit;
3304 			if (fdc->c_fdtype & FDCTYPE_SB)
3305 				csb->csb_cmds[1] |= IPS;
3306 
3307 			csb->csb_cmds[2] = cyl;	/* C - cylinder address */
3308 			csb->csb_cmds[3] = head;	/* H - head number */
3309 			csb->csb_cmds[4] = sect;	/* R - sector number */
3310 			if (fdc->c_fdtype & FDCTYPE_TCBUG)
3311 				csb->csb_cmds[6] = sect +
3312 				    (tlen / ch->fdc_sec_size) - 1;
3313 
3314 			csb->csb_len = tlen;
3315 			if (partial_read)
3316 				csb->csb_addr = temp_addr;
3317 			else
3318 				csb->csb_addr = addr;
3319 
3320 			/* retry this many times max */
3321 			csb->csb_maxretry = rwretry;
3322 			csb->csb_retrys = 0;
3323 
3324 			/* If platform supports DMA, set up DMA resources */
3325 			if (fdc->c_fdtype & FDCTYPE_DMA) {
3326 				if ((fdc->c_fdtype & FDCTYPE_SB) &&
3327 				    (((uint32_t)(uintptr_t)addr & 0xFFFF0000) !=
3328 				    (((uint32_t)(uintptr_t)addr + tlen) &
3329 				    0xFFFF0000))) {
3330 					csb->csb_addr = fdc->dma_buf;
3331 					sb_temp_buf_used = 1;
3332 					if (csb->csb_read != CSB_READ) {
3333 						bcopy(addr, fdc->dma_buf, tlen);
3334 				}
3335 			}
3336 				mutex_enter(&fdc->c_hilock);
3337 
3338 				if (fdstart_dma(fdc, csb->csb_addr,
3339 				    tlen) != 0) {
3340 
3341 					bp->b_flags |= B_ERROR;
3342 					bp->b_error = EAGAIN;
3343 
3344 					mutex_exit(&fdc->c_hilock);
3345 					FDERRPRINT(FDEP_L1, FDEM_STRT,
3346 					    (C, "fdstart: no dma resources\n"));
3347 
3348 					break;
3349 				}
3350 				mutex_exit(&fdc->c_hilock);
3351 
3352 			}
3353 
3354 			bp->b_error = fdexec(fdc, FDXC_SLEEP|FDXC_CHECKCHG);
3355 			if (bp->b_error != 0) {
3356 				/*
3357 				 * error in fdexec
3358 				 */
3359 				FDERRPRINT(FDEP_L1, FDEM_STRT, (C,
3360 				    "fdstart: bad exec of bp: 0x%p, err %d\n",
3361 				    (void *)bp, bp->b_error));
3362 
3363 				bp->b_flags |= B_ERROR;
3364 				if (partial_read) {
3365 					partial_read = 0;
3366 					kmem_free(temp_addr, tlen);
3367 				}
3368 				break;
3369 			}
3370 
3371 			/*
3372 			 * If it was a partial read, copy the useful
3373 			 * portion of data to 'addr'.
3374 			 */
3375 			if (partial_read) {
3376 				partial_read = 0;
3377 				bcopy(temp_addr, addr, len);
3378 				kmem_free(temp_addr, tlen);
3379 				tlen = len;
3380 			}
3381 			if ((fdc->c_fdtype & FDCTYPE_SB) &&
3382 			    (csb->csb_read == CSB_READ)) {
3383 				if (sb_temp_buf_used) {
3384 					bcopy(fdc->dma_buf, addr, tlen);
3385 					sb_temp_buf_used = 0;
3386 				}
3387 			}
3388 
3389 			blk += tlen / ch->fdc_sec_size;
3390 			len -= tlen;
3391 			addr += tlen;
3392 			bp->b_resid -= tlen;
3393 
3394 		}
3395 
3396 		FDERRPRINT(FDEP_L1, FDEM_STRT,
3397 		    (C, "fdstart done: b_resid %lu, b_count %lu, csb_rlen %d\n",
3398 		    bp->b_resid, bp->b_bcount, fdc->c_csb.csb_rlen));
3399 
3400 		fdc->c_current = 0;
3401 		fdretcsb(fdc);
3402 		if (un->un_iostat) {
3403 			if (bp->b_flags & B_READ) {
3404 				KIOSP->reads++;
3405 				KIOSP->nread +=
3406 				    (bp->b_bcount - bp->b_resid);
3407 			} else {
3408 				KIOSP->writes++;
3409 				KIOSP->nwritten += (bp->b_bcount - bp->b_resid);
3410 			}
3411 			kstat_runq_exit(KIOSP);
3412 		}
3413 		biodone(bp);
3414 
3415 		/*
3416 		 * Look at the next buffer
3417 		 */
3418 		bp = fdc->c_actf;
3419 
3420 	}
3421 }
3422 
3423 /*
3424  * Set up DMA resources
3425  * The DMA handle was initialized in fd_attach()
3426  * Assumes the handle has already been allocated by fd_attach()
3427  */
3428 static int
fdstart_dma(struct fdctlr * fdc,caddr_t addr,uint_t len)3429 fdstart_dma(struct fdctlr *fdc, caddr_t addr, uint_t len)
3430 {
3431 	int		flags;		/* flags for setting up resources */
3432 	int		res;
3433 
3434 	FDERRPRINT(FDEP_L1, FDEM_SDMA, (C, "fdstart_dma: start\n"));
3435 
3436 	if (fdc->c_csb.csb_read == CSB_READ) {
3437 		flags = DDI_DMA_READ;
3438 	} else {
3439 		flags = DDI_DMA_WRITE;
3440 	}
3441 
3442 
3443 	/* allow partial mapping to maximize the portability of the driver */
3444 	flags = flags | DDI_DMA_PARTIAL;
3445 
3446 	FDERRPRINT(FDEP_L1, FDEM_SDMA, (C, "fdstart_dma: amt. asked for %d\n",
3447 	    len));
3448 
3449 	/*
3450 	 * Zero out the current cookie.  This is done to ensure that
3451 	 * the previous transfers cookie information can in no way be
3452 	 * used.
3453 	 */
3454 	bzero((char *)&fdc->c_csb.csb_dmacookie,
3455 	    sizeof (fdc->c_csb.csb_dmacookie));
3456 	fdc->c_csb.csb_nwin = 0;
3457 	fdc->c_csb.csb_windex = 0;
3458 	fdc->c_csb.csb_ccount = 0;
3459 
3460 	res = ddi_dma_addr_bind_handle(fdc->c_dmahandle, NULL, addr, len,
3461 	    flags, DDI_DMA_DONTWAIT, 0,  &fdc->c_csb.csb_dmacookie,
3462 	    &fdc->c_csb.csb_ccount);
3463 
3464 	switch (res) {
3465 		case DDI_DMA_MAPPED:
3466 			/*
3467 			 * There is one window. csb_windex is the index
3468 			 * into the array of windows. If there are n
3469 			 * windows then, (0 <= windex <= n-1).  csb_windex
3470 			 * represents the index of the next window
3471 			 * to be processed.
3472 			 */
3473 			fdc->c_csb.csb_nwin = 1;
3474 			fdc->c_csb.csb_windex = 1;
3475 
3476 
3477 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3478 			    (C, "fdstart_dma: DDI_DMA_MAPPED\n"));
3479 
3480 			break;
3481 		case DDI_DMA_PARTIAL_MAP:
3482 
3483 			/*
3484 			 * obtain the number of DMA windows
3485 			 */
3486 			if (ddi_dma_numwin(fdc->c_dmahandle,
3487 			    &fdc->c_csb.csb_nwin) != DDI_SUCCESS) {
3488 				return (-1);
3489 			}
3490 
3491 
3492 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3493 			    (C, "fdstart_dma: partially mapped %d windows\n",
3494 			    fdc->c_csb.csb_nwin));
3495 
3496 			/*
3497 			 * The DMA window currently in use is window number
3498 			 * one.
3499 			 */
3500 			fdc->c_csb.csb_windex = 1;
3501 
3502 			break;
3503 		case DDI_DMA_NORESOURCES:
3504 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3505 			    (C, "fdstart_dma: no resources\n"));
3506 			return (-1);
3507 		case DDI_DMA_NOMAPPING:
3508 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3509 			    (C, "fdstart_dma: no mapping\n"));
3510 			return (-1);
3511 		case DDI_DMA_TOOBIG:
3512 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3513 			    (C, "fdstart_dma: too big\n"));
3514 			return (-1);
3515 
3516 		case DDI_DMA_INUSE:
3517 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3518 			    (C, "fdstart_dma: dma inuse\n"));
3519 			return (-1);
3520 		default:
3521 			FDERRPRINT(FDEP_L1, FDEM_SDMA,
3522 			    (C, "fdstart_dma: result is 0x%x\n", res));
3523 			return (-1);
3524 
3525 	};
3526 
3527 	FDERRPRINT(FDEP_L1, FDEM_SDMA,
3528 	    (C, "fdstart_dma: bound the handle\n"));
3529 
3530 	ASSERT(fdc->c_csb.csb_dmacookie.dmac_size);
3531 
3532 	FDERRPRINT(FDEP_L1, FDEM_SDMA, (C, "fdstart_dma: done\n"));
3533 	return (0);
3534 }
3535 
3536 
3537 /*
3538  * fd_unbind_handle: unbind a dma handle if one exists
3539  *		return EIO if unbind failes
3540  */
3541 static int
fd_unbind_handle(struct fdctlr * fdc)3542 fd_unbind_handle(struct fdctlr *fdc)
3543 {
3544 	if ((fdc->c_fdtype & FDCTYPE_DMA) &&
3545 	    ((fdc->c_csb.csb_read == CSB_READ) ||
3546 	    (fdc->c_csb.csb_read ==