xref: /illumos-gate/usr/src/uts/intel/io/dktp/controller/ata/ata_common.c (revision 4abb96737d15cd2d6530b0aa7b8404ec911ad940)
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 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/modctl.h>
31 #include <sys/debug.h>
32 #include <sys/promif.h>
33 #include <sys/pci.h>
34 #include <sys/errno.h>
35 #include <sys/open.h>
36 #include <sys/uio.h>
37 #include <sys/cred.h>
38 
39 #include "ata_common.h"
40 #include "ata_disk.h"
41 #include "atapi.h"
42 #include "ata_blacklist.h"
43 #include "sil3xxx.h"
44 
45 /*
46  * Solaris Entry Points.
47  */
48 
49 static	int	ata_probe(dev_info_t *dip);
50 static	int	ata_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
51 static	int	ata_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
52 static	int	ata_bus_ctl(dev_info_t *d, dev_info_t *r, ddi_ctl_enum_t o,
53 			void *a, void *v);
54 static	uint_t	ata_intr(caddr_t arg);
55 
56 /*
57  * GHD Entry points
58  */
59 
60 static	int	ata_get_status(void *hba_handle, void *intr_status);
61 static	void	ata_process_intr(void *hba_handle, void *intr_status);
62 static	int	ata_hba_start(void *handle, gcmd_t *gcmdp);
63 static	void	ata_hba_complete(void *handle, gcmd_t *gcmdp, int do_callback);
64 static	int	ata_timeout_func(void *hba_handle, gcmd_t  *gcmdp,
65 			gtgt_t *gtgtp, gact_t  action, int calltype);
66 
67 /*
68  * Local Function Prototypes
69  */
70 static int ata_prop_lookup_int(dev_t match_dev, dev_info_t *dip,
71 		    uint_t flags, char *name, int defvalue);
72 static	int	ata_ctlr_fsm(uchar_t fsm_func, ata_ctl_t *ata_ctlp,
73 			ata_drv_t *ata_drvp, ata_pkt_t *ata_pktp,
74 				int *DoneFlgp);
75 static	void	ata_destroy_controller(dev_info_t *dip);
76 static	int	ata_drive_type(uchar_t drvhd,
77 			ddi_acc_handle_t io_hdl1, caddr_t ioaddr1,
78 			ddi_acc_handle_t io_hdl2, caddr_t ioaddr2,
79 			struct ata_id *ata_id_bufp);
80 static	ata_ctl_t *ata_init_controller(dev_info_t *dip);
81 static	ata_drv_t *ata_init_drive(ata_ctl_t *ata_ctlp,
82 			uchar_t targ, uchar_t lun);
83 static	int	ata_init_drive_pcidma(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp,
84 			dev_info_t *tdip);
85 static	int	ata_flush_cache(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp);
86 static	void	ata_init_pciide(dev_info_t *dip, ata_ctl_t *ata_ctlp);
87 static	int	ata_reset_bus(ata_ctl_t *ata_ctlp);
88 static	int	ata_setup_ioaddr(dev_info_t *dip,
89 			ddi_acc_handle_t *iohandle1, caddr_t *ioaddr1p,
90 			ddi_acc_handle_t *iohandle2, caddr_t *ioaddr2p,
91 			ddi_acc_handle_t *bm_hdlp, caddr_t *bm_addrp);
92 static	int	ata_software_reset(ata_ctl_t *ata_ctlp);
93 static	int	ata_start_arq(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp,
94 			ata_pkt_t *ata_pktp);
95 static	int	ata_strncmp(char *p1, char *p2, int cnt);
96 static	void	ata_uninit_drive(ata_drv_t *ata_drvp);
97 
98 static	int	ata_check_pciide_blacklist(dev_info_t *dip, uint_t flags);
99 static	int	ata_check_revert_to_defaults(ata_drv_t *ata_drvp);
100 static  void	ata_show_transfer_mode(ata_ctl_t *, ata_drv_t *);
101 static	int	ata_spec_init_controller(dev_info_t *dip);
102 
103 
104 /*
105  * Local static data
106  */
107 static	void	*ata_state;
108 
109 static	tmr_t	ata_timer_conf; /* single timeout list for all instances */
110 static	int	ata_watchdog_usec = 100000; /* check timeouts every 100 ms */
111 
112 int	ata_hba_start_watchdog = 1000;
113 int	ata_process_intr_watchdog = 1000;
114 int	ata_reset_bus_watchdog = 1000;
115 
116 
117 /*
118  * number of seconds to wait during various operations
119  */
120 int	ata_flush_delay = 5 * 1000000;
121 uint_t	ata_set_feature_wait = 4 * 1000000;
122 uint_t	ata_flush_cache_wait = 60 * 1000000;	/* may take a long time */
123 
124 /*
125  * Change this for SFF-8070i support. Currently SFF-8070i is
126  * using a field in the IDENTIFY PACKET DEVICE response which
127  * already seems to be in use by some vendor's drives. I suspect
128  * SFF will either move their laslun field or provide a reliable
129  * way to validate it.
130  */
131 int	ata_enable_atapi_luns = FALSE;
132 
133 /*
134  * set this to disable all DMA requests
135  */
136 int	ata_dma_disabled = FALSE;
137 
138 /*
139  * set this to TRUE to enable storing the IDENTIFY DEVICE result in the
140  * "ata" or "atapi" property.
141  */
142 int	ata_id_debug = FALSE;
143 
144 /*
145  * set this to TRUE to enable logging device-capability data
146  */
147 int	ata_capability_data = FALSE;
148 
149 #define	ATAPRT(fmt)	ghd_err fmt
150 
151 /*
152  * DMA selection message pointers
153  */
154 char *ata_cntrl_DMA_sel_msg;
155 char *ata_dev_DMA_sel_msg;
156 
157 /*
158  * bus nexus operations
159  */
160 static	struct bus_ops	 ata_bus_ops;
161 static	struct bus_ops	*scsa_bus_ops_p;
162 
163 /* ARGSUSED */
164 static int
165 ata_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
166 {
167 	if (ddi_get_soft_state(ata_state, getminor(*devp)) == NULL)
168 		return (ENXIO);
169 
170 	return (0);
171 }
172 
173 /*
174  * The purpose of this function is to pass the ioaddress of the controller
175  * to the caller, specifically used for upgrade from pre-pciide
176  * to pciide nodes
177  */
178 /* ARGSUSED */
179 static int
180 ata_read(dev_t dev, struct uio *uio_p, cred_t *cred_p)
181 {
182 	ata_ctl_t *ata_ctlp;
183 	char	buf[18];
184 	long len;
185 
186 	ata_ctlp = ddi_get_soft_state(ata_state, getminor(dev));
187 
188 	if (ata_ctlp == NULL)
189 		return (ENXIO);
190 
191 	(void) sprintf(buf, "%p\n", (void *) ata_ctlp->ac_ioaddr1);
192 
193 	len = strlen(buf) - uio_p->uio_offset;
194 	len = min(uio_p->uio_resid,  len);
195 	if (len <= 0)
196 		return (0);
197 
198 	return (uiomove((caddr_t)(buf + uio_p->uio_offset), len,
199 	    UIO_READ, uio_p));
200 }
201 
202 int
203 ata_devo_reset(
204 	dev_info_t *dip,
205 	ddi_reset_cmd_t cmd)
206 {
207 	ata_ctl_t *ata_ctlp;
208 	ata_drv_t *ata_drvp;
209 	int	   instance;
210 	int	   i;
211 	int	   rc;
212 	int	   flush_okay;
213 
214 	if (cmd != DDI_RESET_FORCE)
215 		return (0);
216 
217 	instance = ddi_get_instance(dip);
218 	ata_ctlp = ddi_get_soft_state(ata_state, instance);
219 
220 	if (!ata_ctlp)
221 		return (0);
222 
223 	/*
224 	 * reset ATA drives and flush the write cache of any drives
225 	 */
226 	flush_okay = TRUE;
227 	for (i = 0; i < ATA_MAXTARG; i++) {
228 		if ((ata_drvp = CTL2DRV(ata_ctlp, i, 0)) == 0)
229 			continue;
230 		/* Don't revert to defaults for certain IBM drives */
231 		if ((ata_drvp->ad_flags & AD_DISK) != 0 &&
232 		    ((ata_drvp->ad_flags & AD_NORVRT) == 0)) {
233 			/* Enable revert to defaults when reset */
234 			(void) ata_set_feature(ata_ctlp, ata_drvp, 0xCC, 0);
235 		}
236 
237 		/*
238 		 * skip flush cache if device type is cdrom
239 		 *
240 		 * notes: the structure definitions for ata_drvp->ad_id are
241 		 * defined for the ATA IDENTIFY_DEVICE, but if AD_ATAPI is set
242 		 * the struct holds data for the ATAPI IDENTIFY_PACKET_DEVICE
243 		 */
244 		if (!IS_CDROM(ata_drvp)) {
245 
246 			/*
247 			 * Try the ATA/ATAPI flush write cache command
248 			 */
249 			rc = ata_flush_cache(ata_ctlp, ata_drvp);
250 			ADBG_WARN(("ata_flush_cache %s\n",
251 				rc ? "okay" : "failed"));
252 
253 			if (!rc)
254 				flush_okay = FALSE;
255 		}
256 
257 
258 		/*
259 		 * do something else if flush cache not supported
260 		 */
261 	}
262 
263 	/*
264 	 * just busy wait if any drive doesn't support FLUSH CACHE
265 	 */
266 	if (!flush_okay)
267 		drv_usecwait(ata_flush_delay);
268 	return (0);
269 }
270 
271 
272 static struct cb_ops ata_cb_ops = {
273 	ata_open,		/* open */
274 	nulldev,		/* close */
275 	nodev,			/* strategy */
276 	nodev,			/* print */
277 	nodev,			/* dump */
278 	ata_read,		/* read */
279 	nodev,			/* write */
280 	nodev,			/* ioctl */
281 	nodev,			/* devmap */
282 	nodev,			/* mmap */
283 	nodev,			/* segmap */
284 	nochpoll,		/* chpoll */
285 	ddi_prop_op,		/* prop_op */
286 	NULL,			/* stream info */
287 	D_MP,			/* driver compatibility flag */
288 	CB_REV,			/* cb_ops revision */
289 	nodev,			/* aread */
290 	nodev			/* awrite */
291 };
292 
293 static struct dev_ops	ata_ops = {
294 	DEVO_REV,		/* devo_rev, */
295 	0,			/* refcnt  */
296 	ddi_getinfo_1to1,	/* info */
297 	nulldev,		/* identify */
298 	ata_probe,		/* probe */
299 	ata_attach,		/* attach */
300 	ata_detach,		/* detach */
301 	ata_devo_reset,		/* reset */
302 	&ata_cb_ops,		/* driver operations */
303 	NULL			/* bus operations */
304 };
305 
306 /* driver loadable module wrapper */
307 static struct modldrv modldrv = {
308 	&mod_driverops,		/* Type of module. This one is a driver */
309 	"ATA AT-bus attachment disk controller Driver",	/* module name */
310 	&ata_ops,					/* driver ops */
311 };
312 
313 static struct modlinkage modlinkage = {
314 	MODREV_1, (void *)&modldrv, NULL
315 };
316 
317 #ifdef ATA_DEBUG
318 int	ata_debug_init = FALSE;
319 int	ata_debug_probe = FALSE;
320 int	ata_debug_attach = FALSE;
321 
322 int	ata_debug = ADBG_FLAG_ERROR
323 		/* | ADBG_FLAG_ARQ */
324 		/* | ADBG_FLAG_INIT */
325 		/* | ADBG_FLAG_TRACE */
326 		/* | ADBG_FLAG_TRANSPORT */
327 		/* | ADBG_FLAG_WARN */
328 		;
329 #endif
330 
331 int
332 _init(void)
333 {
334 	int err;
335 
336 #ifdef ATA_DEBUG
337 	if (ata_debug_init)
338 		debug_enter("\nATA _INIT\n");
339 #endif
340 
341 	if ((err = ddi_soft_state_init(&ata_state, sizeof (ata_ctl_t), 0)) != 0)
342 		return (err);
343 
344 	if ((err = scsi_hba_init(&modlinkage)) != 0) {
345 		ddi_soft_state_fini(&ata_state);
346 		return (err);
347 	}
348 
349 	/* save pointer to SCSA provided bus_ops struct */
350 	scsa_bus_ops_p = ata_ops.devo_bus_ops;
351 
352 	/* make a copy of SCSA bus_ops */
353 	ata_bus_ops = *(ata_ops.devo_bus_ops);
354 
355 	/*
356 	 * Modify our bus_ops to call our routines.  Our implementation
357 	 * will determine if the device is ATA or ATAPI/SCSA and react
358 	 * accordingly.
359 	 */
360 	ata_bus_ops.bus_ctl = ata_bus_ctl;
361 
362 	/* patch our bus_ops into the dev_ops struct */
363 	ata_ops.devo_bus_ops = &ata_bus_ops;
364 
365 	if ((err = mod_install(&modlinkage)) != 0) {
366 		scsi_hba_fini(&modlinkage);
367 		ddi_soft_state_fini(&ata_state);
368 	}
369 
370 	/*
371 	 * Initialize the per driver timer info.
372 	 */
373 
374 	ghd_timer_init(&ata_timer_conf, drv_usectohz(ata_watchdog_usec));
375 
376 	return (err);
377 }
378 
379 int
380 _fini(void)
381 {
382 	int err;
383 
384 	if ((err = mod_remove(&modlinkage)) == 0) {
385 		ghd_timer_fini(&ata_timer_conf);
386 		scsi_hba_fini(&modlinkage);
387 		ddi_soft_state_fini(&ata_state);
388 	}
389 
390 	return (err);
391 }
392 
393 int
394 _info(struct modinfo *modinfop)
395 {
396 	return (mod_info(&modlinkage, modinfop));
397 }
398 
399 
400 /* driver probe entry point */
401 
402 static int
403 ata_probe(
404 	dev_info_t *dip)
405 {
406 	ddi_acc_handle_t io_hdl1 = NULL;
407 	ddi_acc_handle_t io_hdl2 = NULL;
408 	ddi_acc_handle_t bm_hdl = NULL;
409 	caddr_t		 ioaddr1;
410 	caddr_t		 ioaddr2;
411 	caddr_t		 bm_addr;
412 	int		 drive;
413 	struct ata_id	*ata_id_bufp;
414 	int		 rc = DDI_PROBE_FAILURE;
415 
416 	ADBG_TRACE(("ata_probe entered\n"));
417 #ifdef ATA_DEBUG
418 	if (ata_debug_probe)
419 		debug_enter("\nATA_PROBE\n");
420 #endif
421 
422 	if (!ata_setup_ioaddr(dip, &io_hdl1, &ioaddr1, &io_hdl2, &ioaddr2,
423 			&bm_hdl, &bm_addr))
424 		return (rc);
425 
426 	ata_id_bufp = kmem_zalloc(sizeof (*ata_id_bufp), KM_SLEEP);
427 
428 	for (drive = 0; drive < ATA_MAXTARG; drive++) {
429 		uchar_t	drvhd;
430 
431 		/* set up drv/hd and feature registers */
432 
433 		drvhd = (drive == 0 ? ATDH_DRIVE0 : ATDH_DRIVE1);
434 
435 
436 		if (ata_drive_type(drvhd, io_hdl1, ioaddr1, io_hdl2, ioaddr2,
437 		    ata_id_bufp) != ATA_DEV_NONE) {
438 			rc = (DDI_PROBE_SUCCESS);
439 			break;
440 		}
441 	}
442 
443 	/* always leave the controller set to drive 0 */
444 	if (drive != 0) {
445 		ddi_put8(io_hdl1, (uchar_t *)ioaddr1 + AT_DRVHD, ATDH_DRIVE0);
446 		ATA_DELAY_400NSEC(io_hdl2, ioaddr2);
447 	}
448 
449 out2:
450 	kmem_free(ata_id_bufp, sizeof (*ata_id_bufp));
451 
452 	if (io_hdl1)
453 		ddi_regs_map_free(&io_hdl1);
454 	if (io_hdl2)
455 		ddi_regs_map_free(&io_hdl2);
456 	if (bm_hdl)
457 		ddi_regs_map_free(&bm_hdl);
458 	return (rc);
459 }
460 
461 /*
462  *
463  * driver attach entry point
464  *
465  */
466 
467 static int
468 ata_attach(
469 	dev_info_t *dip,
470 	ddi_attach_cmd_t cmd)
471 {
472 	ata_ctl_t	*ata_ctlp;
473 	ata_drv_t	*ata_drvp;
474 	ata_drv_t	*first_drvp = NULL;
475 	uchar_t		 targ;
476 	uchar_t		 lun;
477 	uchar_t		 lastlun;
478 	int		 atapi_count = 0;
479 	int		 disk_count = 0;
480 
481 	ADBG_TRACE(("ata_attach entered\n"));
482 #ifdef ATA_DEBUG
483 	if (ata_debug_attach)
484 		debug_enter("\nATA_ATTACH\n\n");
485 #endif
486 
487 	if (cmd != DDI_ATTACH)
488 		return (DDI_FAILURE);
489 
490 	/* initialize controller */
491 	ata_ctlp = ata_init_controller(dip);
492 
493 	if (ata_ctlp == NULL)
494 		goto errout;
495 
496 	mutex_enter(&ata_ctlp->ac_ccc.ccc_hba_mutex);
497 
498 	/* initialize drives */
499 
500 	for (targ = 0; targ < ATA_MAXTARG; targ++) {
501 
502 		ata_drvp = ata_init_drive(ata_ctlp, targ, 0);
503 		if (ata_drvp == NULL)
504 			continue;
505 
506 		if (first_drvp == NULL)
507 			first_drvp = ata_drvp;
508 
509 		if (ATAPIDRV(ata_drvp)) {
510 			atapi_count++;
511 			lastlun = ata_drvp->ad_id.ai_lastlun;
512 		} else {
513 			disk_count++;
514 			lastlun = 0;
515 		}
516 
517 		/*
518 		 * LUN support is currently disabled. Check with SFF-8070i
519 		 * before enabling.
520 		 */
521 		if (!ata_enable_atapi_luns)
522 			lastlun = 0;
523 
524 		/* Initialize higher LUNs, if there are any */
525 		for (lun = 1; lun <= lastlun && lun < ATA_MAXLUN; lun++) {
526 			if ((ata_drvp =
527 			    ata_init_drive(ata_ctlp, targ, lun)) != NULL) {
528 				ata_show_transfer_mode(ata_ctlp, ata_drvp);
529 			}
530 		}
531 	}
532 
533 	if ((atapi_count == 0) && (disk_count == 0)) {
534 		ADBG_WARN(("ata_attach: no drives detected\n"));
535 		goto errout1;
536 	}
537 
538 	/*
539 	 * Always make certain that a valid drive is selected so
540 	 * that routines which poll the status register don't get
541 	 * confused by non-existent drives.
542 	 */
543 	ddi_put8(ata_ctlp->ac_iohandle1, ata_ctlp->ac_drvhd,
544 		first_drvp->ad_drive_bits);
545 	ATA_DELAY_400NSEC(ata_ctlp->ac_iohandle2, ata_ctlp->ac_ioaddr2);
546 
547 	/*
548 	 * make certain the drive selected
549 	 */
550 	if (!ata_wait(ata_ctlp->ac_iohandle2, ata_ctlp->ac_ioaddr2,
551 			0, ATS_BSY, 5000000)) {
552 		ADBG_ERROR(("ata_attach: select failed\n"));
553 	}
554 
555 	/*
556 	 * initialize atapi/ata_dsk modules if we have at least
557 	 * one drive of that type.
558 	 */
559 
560 	if (atapi_count) {
561 		if (!atapi_attach(ata_ctlp))
562 			goto errout1;
563 		ata_ctlp->ac_flags |= AC_ATAPI_INIT;
564 	}
565 
566 	if (disk_count) {
567 		if (!ata_disk_attach(ata_ctlp))
568 			goto errout1;
569 		ata_ctlp->ac_flags |= AC_DISK_INIT;
570 	}
571 
572 	/*
573 	 * make certain the interrupt and error latches are clear
574 	 */
575 	if (ata_ctlp->ac_pciide) {
576 
577 		int instance = ddi_get_instance(dip);
578 		if (ddi_create_minor_node(dip, "control", S_IFCHR, instance,
579 		    DDI_PSEUDO, 0) != DDI_SUCCESS) {
580 			goto errout1;
581 		}
582 
583 		(void) ata_pciide_status_clear(ata_ctlp);
584 
585 	}
586 
587 	/*
588 	 * enable the interrupt handler and drop the mutex
589 	 */
590 	ata_ctlp->ac_flags |= AC_ATTACHED;
591 	mutex_exit(&ata_ctlp->ac_ccc.ccc_hba_mutex);
592 
593 	ddi_report_dev(dip);
594 	return (DDI_SUCCESS);
595 
596 errout1:
597 	mutex_exit(&ata_ctlp->ac_ccc.ccc_hba_mutex);
598 errout:
599 	(void) ata_detach(dip, DDI_DETACH);
600 	return (DDI_FAILURE);
601 }
602 
603 /* driver detach entry point */
604 
605 static int
606 ata_detach(
607 	dev_info_t *dip,
608 	ddi_detach_cmd_t cmd)
609 {
610 	ata_ctl_t *ata_ctlp;
611 	ata_drv_t *ata_drvp;
612 	int	   instance;
613 	int	   i;
614 	int	   j;
615 
616 	ADBG_TRACE(("ata_detach entered\n"));
617 
618 	if (cmd != DDI_DETACH)
619 		return (DDI_FAILURE);
620 
621 	instance = ddi_get_instance(dip);
622 	ata_ctlp = ddi_get_soft_state(ata_state, instance);
623 
624 	if (!ata_ctlp)
625 		return (DDI_SUCCESS);
626 
627 	ata_ctlp->ac_flags &= ~AC_ATTACHED;
628 
629 	/* destroy ata module */
630 	if (ata_ctlp->ac_flags & AC_DISK_INIT)
631 		ata_disk_detach(ata_ctlp);
632 
633 	/* destroy atapi module */
634 	if (ata_ctlp->ac_flags & AC_ATAPI_INIT)
635 		atapi_detach(ata_ctlp);
636 
637 	ddi_remove_minor_node(dip, NULL);
638 
639 	/* destroy drives */
640 	for (i = 0; i < ATA_MAXTARG; i++) {
641 		for (j = 0; j < ATA_MAXLUN; j++) {
642 			ata_drvp = CTL2DRV(ata_ctlp, i, j);
643 			if (ata_drvp != NULL)
644 				ata_uninit_drive(ata_drvp);
645 		}
646 	}
647 
648 	if (ata_ctlp->ac_iohandle1)
649 		ddi_regs_map_free(&ata_ctlp->ac_iohandle1);
650 	if (ata_ctlp->ac_iohandle2)
651 		ddi_regs_map_free(&ata_ctlp->ac_iohandle2);
652 	if (ata_ctlp->ac_bmhandle)
653 		ddi_regs_map_free(&ata_ctlp->ac_bmhandle);
654 
655 	ddi_prop_remove_all(dip);
656 
657 	/* destroy controller */
658 	ata_destroy_controller(dip);
659 
660 	return (DDI_SUCCESS);
661 }
662 
663 /*
664  * Nexus driver bus_ctl entry point
665  */
666 /*ARGSUSED*/
667 static int
668 ata_bus_ctl(
669 	dev_info_t *d,
670 	dev_info_t *r,
671 	ddi_ctl_enum_t o,
672 	void *a,
673 	void *v)
674 {
675 	dev_info_t *tdip;
676 	int	target_type;
677 	int	rc;
678 	char	*bufp;
679 
680 	ADBG_TRACE(("ata_bus_ctl entered\n"));
681 
682 	switch (o) {
683 
684 	case DDI_CTLOPS_SIDDEV:
685 		return (DDI_FAILURE);
686 
687 	case DDI_CTLOPS_IOMIN:
688 
689 		/*
690 		 * Since we use PIO, we return a minimum I/O size of
691 		 * one byte.  This will need to be updated when we
692 		 * implement DMA support
693 		 */
694 
695 		*((int *)v) = 1;
696 		return (DDI_SUCCESS);
697 
698 	case DDI_CTLOPS_DMAPMAPC:
699 	case DDI_CTLOPS_REPORTINT:
700 	case DDI_CTLOPS_REGSIZE:
701 	case DDI_CTLOPS_NREGS:
702 	case DDI_CTLOPS_SLAVEONLY:
703 	case DDI_CTLOPS_AFFINITY:
704 	case DDI_CTLOPS_POKE:
705 	case DDI_CTLOPS_PEEK:
706 
707 		/* These ops shouldn't be called by a target driver */
708 		ADBG_ERROR(("ata_bus_ctl: %s%d: invalid op (%d) from %s%d\n",
709 			ddi_driver_name(d), ddi_get_instance(d), o,
710 			ddi_driver_name(r), ddi_get_instance(r)));
711 
712 		return (DDI_FAILURE);
713 
714 	case DDI_CTLOPS_REPORTDEV:
715 	case DDI_CTLOPS_INITCHILD:
716 	case DDI_CTLOPS_UNINITCHILD:
717 
718 		/* these require special handling below */
719 		break;
720 
721 	default:
722 		return (ddi_ctlops(d, r, o, a, v));
723 	}
724 
725 	/* get targets dip */
726 
727 	if (o == DDI_CTLOPS_INITCHILD || o == DDI_CTLOPS_UNINITCHILD)
728 		tdip = (dev_info_t *)a;
729 	else
730 		tdip = r;
731 
732 	/*
733 	 * XXX - Get class of target
734 	 *   Before the "class" entry in a conf file becomes
735 	 *   a real property, we use an additional property
736 	 *   tentatively called "class_prop".  We will require that
737 	 *   new classes (ie. direct) export "class_prop".
738 	 *   SCSA target drivers will not have this property, so
739 	 *   no property implies SCSA.
740 	 */
741 	if ((ddi_prop_lookup_string(DDI_DEV_T_ANY, tdip, DDI_PROP_DONTPASS,
742 	    "class", &bufp) == DDI_PROP_SUCCESS) ||
743 	    (ddi_prop_lookup_string(DDI_DEV_T_ANY, tdip, DDI_PROP_DONTPASS,
744 	    "class_prop", &bufp) == DDI_PROP_SUCCESS)) {
745 		if (strcmp(bufp, "dada") == 0)
746 			target_type = ATA_DEV_DISK;
747 		else if (strcmp(bufp, "scsi") == 0)
748 			target_type = ATA_DEV_ATAPI;
749 		else {
750 			ADBG_WARN(("ata_bus_ctl: invalid target class %s\n",
751 				bufp));
752 			ddi_prop_free(bufp);
753 			return (DDI_FAILURE);
754 		}
755 		ddi_prop_free(bufp);
756 	} else {
757 		target_type = ATA_DEV_ATAPI; /* no class prop, assume SCSI */
758 	}
759 
760 	if (o == DDI_CTLOPS_INITCHILD) {
761 		int	instance = ddi_get_instance(d);
762 		ata_ctl_t *ata_ctlp = ddi_get_soft_state(ata_state, instance);
763 		ata_drv_t *ata_drvp;
764 		int	targ;
765 		int	lun;
766 		int	drive_type;
767 		char	*disk_prop;
768 		char	*class_prop;
769 
770 		if (ata_ctlp == NULL) {
771 			ADBG_WARN(("ata_bus_ctl: failed to find ctl struct\n"));
772 			return (DDI_FAILURE);
773 		}
774 
775 		/* get (target,lun) of child device */
776 
777 		targ = ddi_prop_get_int(DDI_DEV_T_ANY, tdip, DDI_PROP_DONTPASS,
778 					"target", -1);
779 		if (targ == -1) {
780 			ADBG_WARN(("ata_bus_ctl: failed to get targ num\n"));
781 			return (DDI_FAILURE);
782 		}
783 
784 		lun = ddi_prop_get_int(DDI_DEV_T_ANY, tdip, DDI_PROP_DONTPASS,
785 			"lun", 0);
786 
787 		if ((targ < 0) || (targ >= ATA_MAXTARG) ||
788 				(lun < 0) || (lun >= ATA_MAXLUN)) {
789 			return (DDI_FAILURE);
790 		}
791 
792 		ata_drvp = CTL2DRV(ata_ctlp, targ, lun);
793 
794 		if (ata_drvp == NULL)
795 			return (DDI_FAILURE);	/* no drive */
796 
797 		/* get type of device */
798 
799 		if (ATAPIDRV(ata_drvp))
800 			drive_type = ATA_DEV_ATAPI;
801 		else
802 			drive_type = ATA_DEV_DISK;
803 
804 		/*
805 		 * Check for special handling when child driver is
806 		 * cmdk (which morphs to the correct interface)
807 		 */
808 		if (strcmp(ddi_get_name(tdip), "cmdk") == 0) {
809 
810 			if ((target_type == ATA_DEV_DISK) &&
811 				(target_type != drive_type))
812 				return (DDI_FAILURE);
813 
814 			target_type = drive_type;
815 
816 			if (drive_type == ATA_DEV_ATAPI) {
817 				class_prop = "scsi";
818 			} else {
819 				disk_prop = "dadk";
820 				class_prop = "dada";
821 
822 				if (ndi_prop_update_string(DDI_DEV_T_NONE, tdip,
823 				    "disk", disk_prop) != DDI_PROP_SUCCESS) {
824 					ADBG_WARN(("ata_bus_ctl: failed to "
825 						"create disk prop\n"));
826 					return (DDI_FAILURE);
827 				    }
828 			}
829 
830 			if (ndi_prop_update_string(DDI_DEV_T_NONE, tdip,
831 			    "class_prop", class_prop) != DDI_PROP_SUCCESS) {
832 				ADBG_WARN(("ata_bus_ctl: failed to "
833 				    "create class prop\n"));
834 				return (DDI_FAILURE);
835 			}
836 		}
837 
838 		/* Check that target class matches the device */
839 
840 		if (target_type != drive_type)
841 			return (DDI_FAILURE);
842 
843 		/* save pointer to drive struct for ata_disk_bus_ctl */
844 		ddi_set_driver_private(tdip, ata_drvp);
845 
846 		/*
847 		 * Determine whether to enable DMA support for this drive.  This
848 		 * check is deferred to this point so that the various dma
849 		 * properties could reside on the devinfo node should finer
850 		 * grained dma control be required.
851 		 */
852 		ata_drvp->ad_pciide_dma = ata_init_drive_pcidma(ata_ctlp,
853 		    ata_drvp, tdip);
854 		ata_show_transfer_mode(ata_ctlp, ata_drvp);
855 	}
856 
857 	if (target_type == ATA_DEV_ATAPI) {
858 		rc = scsa_bus_ops_p->bus_ctl(d, r, o, a, v);
859 	} else {
860 		rc = ata_disk_bus_ctl(d, r, o, a, v);
861 	}
862 
863 	return (rc);
864 }
865 
866 /*
867  *
868  * GHD ccc_hba_complete callback
869  *
870  */
871 
872 /* ARGSUSED */
873 static void
874 ata_hba_complete(
875 	void *hba_handle,
876 	gcmd_t *gcmdp,
877 	int do_callback)
878 {
879 	ata_drv_t *ata_drvp;
880 	ata_pkt_t *ata_pktp;
881 
882 	ADBG_TRACE(("ata_hba_complete entered\n"));
883 
884 	ata_drvp = GCMD2DRV(gcmdp);
885 	ata_pktp = GCMD2APKT(gcmdp);
886 	if (ata_pktp->ap_complete)
887 		(*ata_pktp->ap_complete)(ata_drvp, ata_pktp,
888 			do_callback);
889 }
890 
891 /* GHD ccc_timeout_func callback */
892 
893 /* ARGSUSED */
894 static int
895 ata_timeout_func(
896 	void	*hba_handle,
897 	gcmd_t	*gcmdp,
898 	gtgt_t	*gtgtp,
899 	gact_t	 action,
900 	int	 calltype)
901 {
902 	ata_ctl_t *ata_ctlp;
903 	ata_pkt_t *ata_pktp;
904 
905 	ADBG_TRACE(("ata_timeout_func entered\n"));
906 
907 	ata_ctlp = (ata_ctl_t *)hba_handle;
908 
909 	if (gcmdp != NULL)
910 		ata_pktp = GCMD2APKT(gcmdp);
911 	else
912 		ata_pktp = NULL;
913 
914 	switch (action) {
915 	case GACTION_EARLY_ABORT:
916 		/* abort before request was started */
917 		if (ata_pktp != NULL) {
918 			ata_pktp->ap_flags |= AP_ABORT;
919 		}
920 		ghd_complete(&ata_ctlp->ac_ccc, gcmdp);
921 		return (TRUE);
922 
923 	case GACTION_EARLY_TIMEOUT:
924 		/* timeout before request was started */
925 		if (ata_pktp != NULL) {
926 			ata_pktp->ap_flags |= AP_TIMEOUT;
927 		}
928 		ghd_complete(&ata_ctlp->ac_ccc, gcmdp);
929 		return (TRUE);
930 
931 	case GACTION_RESET_TARGET:
932 		/*
933 		 * Reset a device is not supported. Resetting a specific
934 		 * device can't be done at all to an ATA device and if
935 		 * you send a RESET to an ATAPI device you have to
936 		 * reset the whole bus to make certain both devices
937 		 * on the bus stay in sync regarding which device is
938 		 * the currently selected one.
939 		 */
940 		return (FALSE);
941 
942 	case GACTION_RESET_BUS:
943 		/*
944 		 * Issue bus reset and reinitialize both drives.
945 		 * But only if this is a timed-out request. Target
946 		 * driver reset requests are ignored because ATA
947 		 * and ATAPI devices shouldn't be gratuitously reset.
948 		 */
949 		if (gcmdp == NULL)
950 			break;
951 		return (ata_reset_bus(ata_ctlp));
952 	default:
953 		break;
954 	}
955 	return (FALSE);
956 }
957 
958 /*
959  *
960  * Initialize controller's soft-state structure
961  *
962  */
963 
964 static ata_ctl_t *
965 ata_init_controller(
966 	dev_info_t *dip)
967 {
968 	ata_ctl_t *ata_ctlp;
969 	int	   instance;
970 	caddr_t	   ioaddr1;
971 	caddr_t	   ioaddr2;
972 
973 	ADBG_TRACE(("ata_init_controller entered\n"));
974 
975 	instance = ddi_get_instance(dip);
976 
977 	/* allocate controller structure */
978 	if (ddi_soft_state_zalloc(ata_state, instance) != DDI_SUCCESS) {
979 		ADBG_WARN(("ata_init_controller: soft_state_zalloc failed\n"));
980 		return (NULL);
981 	}
982 
983 	ata_ctlp = ddi_get_soft_state(ata_state, instance);
984 
985 	if (ata_ctlp == NULL) {
986 		ADBG_WARN(("ata_init_controller: failed to find "
987 				"controller struct\n"));
988 		return (NULL);
989 	}
990 
991 	/*
992 	 * initialize per-controller data
993 	 */
994 	ata_ctlp->ac_dip = dip;
995 	ata_ctlp->ac_arq_pktp = kmem_zalloc(sizeof (ata_pkt_t), KM_SLEEP);
996 
997 	/*
998 	 * map the device registers
999 	 */
1000 	if (!ata_setup_ioaddr(dip, &ata_ctlp->ac_iohandle1, &ioaddr1,
1001 			&ata_ctlp->ac_iohandle2, &ioaddr2,
1002 			&ata_ctlp->ac_bmhandle, &ata_ctlp->ac_bmaddr)) {
1003 		(void) ata_detach(dip, DDI_DETACH);
1004 		return (NULL);
1005 	}
1006 
1007 	ADBG_INIT(("ata_init_controller: ioaddr1 = 0x%p, ioaddr2 = 0x%p\n",
1008 			ioaddr1, ioaddr2));
1009 
1010 	/*
1011 	 * Do ARQ setup
1012 	 */
1013 	atapi_init_arq(ata_ctlp);
1014 
1015 	/*
1016 	 * Do PCI-IDE setup
1017 	 */
1018 	ata_init_pciide(dip, ata_ctlp);
1019 
1020 	/*
1021 	 * port addresses associated with ioaddr1
1022 	 */
1023 	ata_ctlp->ac_ioaddr1	= ioaddr1;
1024 	ata_ctlp->ac_data	= (ushort_t *)ioaddr1 + AT_DATA;
1025 	ata_ctlp->ac_error	= (uchar_t *)ioaddr1 + AT_ERROR;
1026 	ata_ctlp->ac_feature	= (uchar_t *)ioaddr1 + AT_FEATURE;
1027 	ata_ctlp->ac_count	= (uchar_t *)ioaddr1 + AT_COUNT;
1028 	ata_ctlp->ac_sect	= (uchar_t *)ioaddr1 + AT_SECT;
1029 	ata_ctlp->ac_lcyl	= (uchar_t *)ioaddr1 + AT_LCYL;
1030 	ata_ctlp->ac_hcyl	= (uchar_t *)ioaddr1 + AT_HCYL;
1031 	ata_ctlp->ac_drvhd	= (uchar_t *)ioaddr1 + AT_DRVHD;
1032 	ata_ctlp->ac_status	= (uchar_t *)ioaddr1 + AT_STATUS;
1033 	ata_ctlp->ac_cmd	= (uchar_t *)ioaddr1 + AT_CMD;
1034 
1035 	/*
1036 	 * port addresses associated with ioaddr2
1037 	 */
1038 	ata_ctlp->ac_ioaddr2	= ioaddr2;
1039 	ata_ctlp->ac_altstatus	= (uchar_t *)ioaddr2 + AT_ALTSTATUS;
1040 	ata_ctlp->ac_devctl	= (uchar_t *)ioaddr2 + AT_DEVCTL;
1041 
1042 	/*
1043 	 * If AC_BSY_WAIT needs to be set  for laptops that do
1044 	 * suspend/resume but do not correctly wait for the busy bit to
1045 	 * drop after a resume.
1046 	 */
1047 	ata_ctlp->ac_timing_flags = ddi_prop_get_int(DDI_DEV_T_ANY,
1048 			dip, DDI_PROP_DONTPASS, "timing_flags", 0);
1049 	/*
1050 	 * get max transfer size, default to 256 sectors
1051 	 */
1052 	ata_ctlp->ac_max_transfer = ddi_prop_get_int(DDI_DEV_T_ANY,
1053 			dip, DDI_PROP_DONTPASS, "max_transfer", 0x100);
1054 	if (ata_ctlp->ac_max_transfer < 1)
1055 		ata_ctlp->ac_max_transfer = 1;
1056 	if (ata_ctlp->ac_max_transfer > 0x100)
1057 		ata_ctlp->ac_max_transfer = 0x100;
1058 
1059 	/*
1060 	 * Get the standby timer value
1061 	 */
1062 	ata_ctlp->ac_standby_time = ddi_prop_get_int(DDI_DEV_T_ANY,
1063 			dip, DDI_PROP_DONTPASS, "standby", -1);
1064 
1065 	/*
1066 	 * If this is a /pci/pci-ide instance check to see if
1067 	 * it's supposed to be attached as an /isa/ata
1068 	 */
1069 	if (ata_ctlp->ac_pciide) {
1070 		static char prop_buf[] = "SUNW-ata-ffff-isa";
1071 		int addr1 = (intptr_t)ioaddr1;
1072 
1073 
1074 		if (addr1 < 0 || addr1 > 0xffff) {
1075 			(void) ata_detach(dip, DDI_DETACH);
1076 			return (NULL);
1077 		}
1078 		(void) sprintf(prop_buf, "SUNW-ata-%04x-isa",
1079 			addr1);
1080 		if (ddi_prop_exists(DDI_DEV_T_ANY, ddi_root_node(),
1081 				    DDI_PROP_DONTPASS, prop_buf)) {
1082 			(void) ata_detach(dip, DDI_DETACH);
1083 			return (NULL);
1084 		}
1085 	}
1086 
1087 	/* Init controller specific stuff */
1088 	(void) ata_spec_init_controller(dip);
1089 
1090 	/*
1091 	 * initialize GHD
1092 	 */
1093 
1094 	GHD_WAITQ_INIT(&ata_ctlp->ac_ccc.ccc_waitq, NULL, 1);
1095 
1096 	if (!ghd_register("ata", &ata_ctlp->ac_ccc, dip, 0, ata_ctlp,
1097 			atapi_ccballoc, atapi_ccbfree,
1098 			ata_pciide_dma_sg_func, ata_hba_start,
1099 			ata_hba_complete, ata_intr,
1100 			ata_get_status, ata_process_intr, ata_timeout_func,
1101 			&ata_timer_conf, NULL)) {
1102 		(void) ata_detach(dip, DDI_DETACH);
1103 		return (NULL);
1104 	}
1105 
1106 	ata_ctlp->ac_flags |= AC_GHD_INIT;
1107 	return (ata_ctlp);
1108 }
1109 
1110 /* destroy a controller */
1111 
1112 static void
1113 ata_destroy_controller(
1114 	dev_info_t *dip)
1115 {
1116 	ata_ctl_t *ata_ctlp;
1117 	int	instance;
1118 
1119 	ADBG_TRACE(("ata_destroy_controller entered\n"));
1120 
1121 	instance = ddi_get_instance(dip);
1122 	ata_ctlp = ddi_get_soft_state(ata_state, instance);
1123 
1124 	if (ata_ctlp == NULL)
1125 		return;
1126 
1127 	/* destroy ghd */
1128 	if (ata_ctlp->ac_flags & AC_GHD_INIT)
1129 		ghd_unregister(&ata_ctlp->ac_ccc);
1130 
1131 	/* free the pciide buffer (if any) */
1132 	ata_pciide_free(ata_ctlp);
1133 
1134 	/* destroy controller struct */
1135 	kmem_free(ata_ctlp->ac_arq_pktp, sizeof (ata_pkt_t));
1136 	ddi_soft_state_free(ata_state, instance);
1137 
1138 }
1139 
1140 
1141 /*
1142  *
1143  * initialize a drive
1144  *
1145  */
1146 
1147 static ata_drv_t *
1148 ata_init_drive(
1149 	ata_ctl_t	*ata_ctlp,
1150 	uchar_t		targ,
1151 	uchar_t		lun)
1152 {
1153 	static	char	 nec_260[]	= "NEC CD-ROM DRIVE";
1154 	ata_drv_t *ata_drvp;
1155 	struct ata_id	*aidp;
1156 	char	buf[80];
1157 	int	drive_type;
1158 	int	i;
1159 	int	valid_version = 0;
1160 
1161 	ADBG_TRACE(("ata_init_drive entered, targ = %d, lun = %d\n",
1162 		    targ, lun));
1163 
1164 	/* check if device already exists */
1165 
1166 	ata_drvp = CTL2DRV(ata_ctlp, targ, lun);
1167 
1168 	if (ata_drvp != NULL)
1169 		return (ata_drvp);
1170 
1171 	/* allocate new device structure */
1172 
1173 	ata_drvp = kmem_zalloc(sizeof (ata_drv_t), KM_SLEEP);
1174 	aidp = &ata_drvp->ad_id;
1175 
1176 	/*
1177 	 * set up drive struct
1178 	 */
1179 	ata_drvp->ad_ctlp = ata_ctlp;
1180 	ata_drvp->ad_targ = targ;
1181 	ata_drvp->ad_drive_bits =
1182 		(ata_drvp->ad_targ == 0 ? ATDH_DRIVE0 : ATDH_DRIVE1);
1183 	/*
1184 	 * Add the LUN for SFF-8070i support
1185 	 */
1186 	ata_drvp->ad_lun = lun;
1187 	ata_drvp->ad_drive_bits |= ata_drvp->ad_lun;
1188 
1189 	/*
1190 	 * get drive type, side effect is to collect
1191 	 * IDENTIFY DRIVE data
1192 	 */
1193 
1194 	drive_type = ata_drive_type(ata_drvp->ad_drive_bits,
1195 				    ata_ctlp->ac_iohandle1,
1196 				    ata_ctlp->ac_ioaddr1,
1197 				    ata_ctlp->ac_iohandle2,
1198 				    ata_ctlp->ac_ioaddr2,
1199 				    aidp);
1200 
1201 	switch (drive_type) {
1202 	case ATA_DEV_NONE:
1203 		/* no drive found */
1204 		goto errout;
1205 	case ATA_DEV_ATAPI:
1206 		ata_drvp->ad_flags |= AD_ATAPI;
1207 		break;
1208 	case ATA_DEV_DISK:
1209 		ata_drvp->ad_flags |= AD_DISK;
1210 		break;
1211 	}
1212 
1213 	/*
1214 	 * swap bytes of all text fields
1215 	 */
1216 	if (!ata_strncmp(nec_260, aidp->ai_model, sizeof (aidp->ai_model))) {
1217 		swab(aidp->ai_drvser, aidp->ai_drvser,
1218 			sizeof (aidp->ai_drvser));
1219 		swab(aidp->ai_fw, aidp->ai_fw,
1220 			sizeof (aidp->ai_fw));
1221 		swab(aidp->ai_model, aidp->ai_model,
1222 			sizeof (aidp->ai_model));
1223 	}
1224 
1225 	/*
1226 	 * Check if this drive has the Single Sector bug
1227 	 */
1228 
1229 	if (ata_check_drive_blacklist(&ata_drvp->ad_id, ATA_BL_1SECTOR))
1230 		ata_drvp->ad_flags |= AD_1SECTOR;
1231 	else
1232 		ata_drvp->ad_flags &= ~AD_1SECTOR;
1233 
1234 	/* Check if this drive has the "revert to defaults" bug */
1235 	if (!ata_check_revert_to_defaults(ata_drvp))
1236 		ata_drvp->ad_flags |= AD_NORVRT;
1237 
1238 	/* Dump the drive info */
1239 	(void) strncpy(buf, aidp->ai_model, sizeof (aidp->ai_model));
1240 	buf[sizeof (aidp->ai_model)-1] = '\0';
1241 	for (i = sizeof (aidp->ai_model) - 2; buf[i] == ' '; i--)
1242 		buf[i] = '\0';
1243 
1244 	ATAPRT(("?\t%s device at targ %d, lun %d lastlun 0x%x\n",
1245 		(ATAPIDRV(ata_drvp) ? "ATAPI":"IDE"),
1246 		ata_drvp->ad_targ, ata_drvp->ad_lun, aidp->ai_lastlun));
1247 
1248 	ATAPRT(("?\tmodel %s\n", buf));
1249 
1250 	if (aidp->ai_majorversion != 0 && aidp->ai_majorversion != 0xffff) {
1251 		for (i = 14; i >= 2; i--) {
1252 			if (aidp->ai_majorversion & (1 << i)) {
1253 				valid_version = i;
1254 				break;
1255 			}
1256 		}
1257 		ATAPRT((
1258 		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
1259 			valid_version,
1260 			aidp->ai_majorversion,
1261 			aidp->ai_minorversion));
1262 	}
1263 
1264 	if (ata_capability_data) {
1265 
1266 		ATAPRT(("?\t\tstat %x, err %x\n",
1267 			ddi_get8(ata_ctlp->ac_iohandle2,
1268 				ata_ctlp->ac_altstatus),
1269 			ddi_get8(ata_ctlp->ac_iohandle1, ata_ctlp->ac_error)));
1270 
1271 		ATAPRT(("?\t\tcfg 0x%x, cap 0x%x\n",
1272 			aidp->ai_config,
1273 			aidp->ai_cap));
1274 
1275 		/*
1276 		 * Be aware that ATA-6 and later drives may not provide valid
1277 		 * geometry information and other obsoleted info.
1278 		 * Select what is printed based on supported ATA model (skip
1279 		 * anything below ATA/ATAPI-3)
1280 		 */
1281 
1282 		if (valid_version == 0 || aidp->ai_majorversion <
1283 		    ATAC_MAJVER_6) {
1284 			/*
1285 			 * Supported version less then ATA-6
1286 			 */
1287 			ATAPRT(("?\t\tcyl %d, hd %d, sec/trk %d\n",
1288 				aidp->ai_fixcyls,
1289 				aidp->ai_heads,
1290 				aidp->ai_sectors));
1291 		}
1292 		ATAPRT(("?\t\tmult1 0x%x, mult2 0x%x\n",
1293 			aidp->ai_mult1,
1294 			aidp->ai_mult2));
1295 		if (valid_version && aidp->ai_majorversion < ATAC_MAJVER_4) {
1296 			ATAPRT((
1297 			"?\t\tpiomode 0x%x, dmamode 0x%x, advpiomode 0x%x\n",
1298 				aidp->ai_piomode,
1299 				aidp->ai_dmamode,
1300 				aidp->ai_advpiomode));
1301 		} else {
1302 			ATAPRT(("?\t\tadvpiomode 0x%x\n",
1303 				aidp->ai_advpiomode));
1304 		}
1305 		ATAPRT(("?\t\tminpio %d, minpioflow %d\n",
1306 			aidp->ai_minpio,
1307 			aidp->ai_minpioflow));
1308 		if (valid_version && aidp->ai_majorversion >= ATAC_MAJVER_4 &&
1309 		    (aidp->ai_validinfo & ATAC_VALIDINFO_83)) {
1310 			ATAPRT(("?\t\tdwdma 0x%x, ultradma 0x%x\n",
1311 				aidp->ai_dworddma,
1312 				aidp->ai_ultradma));
1313 		} else {
1314 			ATAPRT(("?\t\tdwdma 0x%x\n",
1315 				aidp->ai_dworddma));
1316 		}
1317 	}
1318 
1319 	if (ATAPIDRV(ata_drvp)) {
1320 		if (!atapi_init_drive(ata_drvp))
1321 			goto errout;
1322 	} else {
1323 		if (!ata_disk_init_drive(ata_drvp))
1324 			goto errout;
1325 	}
1326 
1327 	/*
1328 	 * store pointer in controller struct
1329 	 */
1330 	CTL2DRV(ata_ctlp, targ, lun) = ata_drvp;
1331 
1332 	/*
1333 	 * lock the drive's current settings in case I have to
1334 	 * reset the drive due to some sort of error
1335 	 */
1336 	(void) ata_set_feature(ata_ctlp, ata_drvp, 0x66, 0);
1337 
1338 	return (ata_drvp);
1339 
1340 errout:
1341 	ata_uninit_drive(ata_drvp);
1342 	return (NULL);
1343 }
1344 
1345 /* destroy a drive */
1346 
1347 static void
1348 ata_uninit_drive(
1349 	ata_drv_t *ata_drvp)
1350 {
1351 #if 0
1352 	ata_ctl_t *ata_ctlp = ata_drvp->ad_ctlp;
1353 #endif
1354 
1355 	ADBG_TRACE(("ata_uninit_drive entered\n"));
1356 
1357 #if 0
1358 	/*
1359 	 * DON'T DO THIS. disabling interrupts floats the IRQ line
1360 	 * which generates spurious interrupts
1361 	 */
1362 
1363 	/*
1364 	 * Select the correct drive
1365 	 */
1366 	ddi_put8(ata_ctlp->ac_iohandle1, ata_ctlp->ac_drvhd,
1367 		ata_drvp->ad_drive_bits);
1368 	ATA_DELAY_400NSEC(ata_ctlp->ac_iohandle2, ata_ctlp->ac_ioaddr2);
1369 
1370 	/*
1371 	 * Disable interrupts from the drive
1372 	 */
1373 	ddi_put8(ata_ctlp->ac_iohandle2, ata_ctlp->ac_devctl,
1374 		(ATDC_D3 | ATDC_NIEN));
1375 #endif
1376 
1377 	/* interface specific clean-ups */
1378 
1379 	if (ata_drvp->ad_flags & AD_ATAPI)
1380 		atapi_uninit_drive(ata_drvp);
1381 	else if (ata_drvp->ad_flags & AD_DISK)
1382 		ata_disk_uninit_drive(ata_drvp);
1383 
1384 	/* free drive struct */
1385 
1386 	kmem_free(ata_drvp, sizeof (ata_drv_t));
1387 }
1388 
1389 
1390 /*
1391  * ata_drive_type()
1392  *
1393  * The timeout values and exact sequence of checking is critical
1394  * especially for atapi device detection, and should not be changed lightly.
1395  *
1396  */
1397 static int
1398 ata_drive_type(
1399 	uchar_t		 drvhd,
1400 	ddi_acc_handle_t io_hdl1,
1401 	caddr_t		 ioaddr1,
1402 	ddi_acc_handle_t io_hdl2,
1403 	caddr_t		 ioaddr2,
1404 	struct ata_id	*ata_id_bufp)
1405 {
1406 	uchar_t	status;
1407 
1408 	ADBG_TRACE(("ata_drive_type entered\n"));
1409 
1410 	/*
1411 	 * select the appropriate drive and LUN
1412 	 */
1413 	ddi_put8(io_hdl1, (uchar_t *)ioaddr1 + AT_DRVHD, drvhd);
1414 	ATA_DELAY_400NSEC(io_hdl2, ioaddr2);
1415 
1416 	/*
1417 	 * make certain the drive is selected, and wait for not busy
1418 	 */
1419 	(void) ata_wait3(io_hdl2, ioaddr2, 0, ATS_BSY, 0x7f, 0, 0x7f, 0,
1420 		5 * 1000000);
1421 
1422 	status = ddi_get8(io_hdl2, (uchar_t *)ioaddr2 + AT_ALTSTATUS);
1423 
1424 	if (status & ATS_BSY) {
1425 		ADBG_TRACE(("ata_drive_type BUSY 0x%p 0x%x\n",
1426 			    ioaddr1, status));
1427 		return (ATA_DEV_NONE);
1428 	}
1429 
1430 	if (ata_disk_id(io_hdl1, ioaddr1, io_hdl2, ioaddr2, ata_id_bufp))
1431 		return (ATA_DEV_DISK);
1432 
1433 	/*
1434 	 * No disk, check for atapi unit.
1435 	 */
1436 	if (!atapi_signature(io_hdl1, ioaddr1)) {
1437 #ifndef ATA_DISABLE_ATAPI_1_7
1438 		/*
1439 		 * Check for old (but prevalent) atapi 1.7B
1440 		 * spec device, the only known example is the
1441 		 * NEC CDR-260 (not 260R which is (mostly) ATAPI 1.2
1442 		 * compliant). This device has no signature
1443 		 * and requires conversion from hex to BCD
1444 		 * for some scsi audio commands.
1445 		 */
1446 		if (atapi_id(io_hdl1, ioaddr1, io_hdl2, ioaddr2, ata_id_bufp)) {
1447 			return (ATA_DEV_ATAPI);
1448 		}
1449 #endif
1450 		return (ATA_DEV_NONE);
1451 	}
1452 
1453 	if (atapi_id(io_hdl1, ioaddr1, io_hdl2, ioaddr2, ata_id_bufp)) {
1454 		return (ATA_DEV_ATAPI);
1455 	}
1456 
1457 	return (ATA_DEV_NONE);
1458 
1459 }
1460 
1461 /*
1462  * Wait for a register of a controller to achieve a specific state.
1463  * To return normally, all the bits in the first sub-mask must be ON,
1464  * all the bits in the second sub-mask must be OFF.
1465  * If timeout_usec microseconds pass without the controller achieving
1466  * the desired bit configuration, we return TRUE, else FALSE.
1467  */
1468 
1469 int ata_usec_delay = 10;
1470 
1471 int
1472 ata_wait(
1473 	ddi_acc_handle_t io_hdl,
1474 	caddr_t		ioaddr,
1475 	uchar_t		onbits,
1476 	uchar_t		offbits,
1477 	uint_t		timeout_usec)
1478 {
1479 	ushort_t val;
1480 
1481 	do  {
1482 		val = ddi_get8(io_hdl, (uchar_t *)ioaddr + AT_ALTSTATUS);
1483 		if ((val & onbits) == onbits && (val & offbits) == 0)
1484 			return (TRUE);
1485 		drv_usecwait(ata_usec_delay);
1486 		timeout_usec -= ata_usec_delay;
1487 	} while (timeout_usec > 0);
1488 
1489 	return (FALSE);
1490 }
1491 
1492 
1493 
1494 /*
1495  *
1496  * This is a slightly more complicated version that checks
1497  * for error conditions and bails-out rather than looping
1498  * until the timeout expires
1499  */
1500 int
1501 ata_wait3(
1502 	ddi_acc_handle_t io_hdl,
1503 	caddr_t		ioaddr,
1504 	uchar_t		onbits1,
1505 	uchar_t		offbits1,
1506 	uchar_t		failure_onbits2,
1507 	uchar_t		failure_offbits2,
1508 	uchar_t		failure_onbits3,
1509 	uchar_t		failure_offbits3,
1510 	uint_t		timeout_usec)
1511 {
1512 	ushort_t val;
1513 
1514 	do  {
1515 		val = ddi_get8(io_hdl, (uchar_t *)ioaddr + AT_ALTSTATUS);
1516 
1517 		/*
1518 		 * check for expected condition
1519 		 */
1520 		if ((val & onbits1) == onbits1 && (val & offbits1) == 0)
1521 			return (TRUE);
1522 
1523 		/*
1524 		 * check for error conditions
1525 		 */
1526 		if ((val & failure_onbits2) == failure_onbits2 &&
1527 				(val & failure_offbits2) == 0) {
1528 			return (FALSE);
1529 		}
1530 
1531 		if ((val & failure_onbits3) == failure_onbits3 &&
1532 				(val & failure_offbits3) == 0) {
1533 			return (FALSE);
1534 		}
1535 
1536 		drv_usecwait(ata_usec_delay);
1537 		timeout_usec -= ata_usec_delay;
1538 	} while (timeout_usec > 0);
1539 
1540 	return (FALSE);
1541 }
1542 
1543 
1544 /*
1545  *
1546  * low level routine for ata_disk_id() and atapi_id()
1547  *
1548  */
1549 
1550 int
1551 ata_id_common(
1552 	uchar_t		 id_cmd,
1553 	int		 expect_drdy,
1554 	ddi_acc_handle_t io_hdl1,
1555 	caddr_t		 ioaddr1,
1556 	ddi_acc_handle_t io_hdl2,
1557 	caddr_t		 ioaddr2,
1558 	struct ata_id	*aidp)
1559 {
1560 	uchar_t	status;
1561 
1562 	ADBG_TRACE(("ata_id_common entered\n"));
1563 
1564 	bzero(aidp, sizeof (struct ata_id));
1565 
1566 	/*
1567 	 * clear the features register
1568 	 */
1569 	ddi_put8(io_hdl1, (uchar_t *)ioaddr1 + AT_FEATURE, 0);
1570 
1571 	/*
1572 	 * enable interrupts from the device
1573 	 */
1574 	ddi_put8(io_hdl2, (uchar_t *)ioaddr2 + AT_DEVCTL, ATDC_D3);
1575 
1576 	/*
1577 	 * issue IDENTIFY DEVICE or IDENTIFY PACKET DEVICE command
1578 	 */
1579 	ddi_put8(io_hdl1, (uchar_t *)ioaddr1 + AT_CMD, id_cmd);
1580 
1581 	/* wait for the busy bit to settle */
1582 	ATA_DELAY_400NSEC(io_hdl2, ioaddr2);
1583 
1584 	/*
1585 	 * According to the ATA specification, some drives may have
1586 	 * to read the media to complete this command.  We need to
1587 	 * make sure we give them enough time to respond.
1588 	 */
1589 
1590 	(void) ata_wait3(io_hdl2, ioaddr2, 0, ATS_BSY,
1591 			ATS_ERR, ATS_BSY, 0x7f, 0, 5 * 1000000);
1592 
1593 	/*
1594 	 * read the status byte and clear the pending interrupt
1595 	 */
1596 	status = ddi_get8(io_hdl2, (uchar_t *)ioaddr1 + AT_STATUS);
1597 
1598 	/*
1599 	 * this happens if there's no drive present
1600 	 */
1601 	if (status == 0xff || status == 0x7f) {
1602 		/* invalid status, can't be an ATA or ATAPI device */
1603 		return (FALSE);
1604 	}
1605 
1606 	if (status & ATS_BSY) {
1607 		ADBG_ERROR(("ata_id_common: BUSY status 0x%x error 0x%x\n",
1608 			ddi_get8(io_hdl2, (uchar_t *)ioaddr2 +AT_ALTSTATUS),
1609 			ddi_get8(io_hdl1, (uchar_t *)ioaddr1 + AT_ERROR)));
1610 		return (FALSE);
1611 	}
1612 
1613 	if (!(status & ATS_DRQ)) {
1614 		if (status & (ATS_ERR | ATS_DF)) {
1615 			return (FALSE);
1616 		}
1617 		/*
1618 		 * Give the drive another second to assert DRQ. Some older
1619 		 * drives de-assert BSY before asserting DRQ.
1620 		 */
1621 		if (!ata_wait(io_hdl2, ioaddr2, ATS_DRQ, ATS_BSY, 1000000)) {
1622 		ADBG_WARN(("ata_id_common: !DRQ status 0x%x error 0x%x\n",
1623 			ddi_get8(io_hdl2, (uchar_t *)ioaddr2 +AT_ALTSTATUS),
1624 			ddi_get8(io_hdl1, (uchar_t *)ioaddr1 + AT_ERROR)));
1625 		return (FALSE);
1626 		}
1627 	}
1628 
1629 	/*
1630 	 * transfer the data
1631 	 */
1632 	ddi_rep_get16(io_hdl1, (ushort_t *)aidp, (ushort_t *)ioaddr1 + AT_DATA,
1633 		NBPSCTR >> 1, DDI_DEV_NO_AUTOINCR);
1634 
1635 	/* wait for the busy bit to settle */
1636 	ATA_DELAY_400NSEC(io_hdl2, ioaddr2);
1637 
1638 
1639 	/*
1640 	 * Wait for the drive to recognize I've read all the data.
1641 	 * Some drives have been observed to take as much as 3msec to
1642 	 * deassert DRQ after reading the data; allow 10 msec just in case.
1643 	 *
1644 	 * Note: some non-compliant ATAPI drives (e.g., NEC Multispin 6V,
1645 	 * CDR-1350A) don't assert DRDY. If we've made it this far we can
1646 	 * safely ignore the DRDY bit since the ATAPI Packet command
1647 	 * actually doesn't require it to ever be asserted.
1648 	 *
1649 	 */
1650 	if (!ata_wait(io_hdl2, ioaddr2, (uchar_t)(expect_drdy ? ATS_DRDY : 0),
1651 					(ATS_BSY | ATS_DRQ), 1000000)) {
1652 		ADBG_WARN(("ata_id_common: bad status 0x%x error 0x%x\n",
1653 			ddi_get8(io_hdl2, (uchar_t *)ioaddr2 + AT_ALTSTATUS),
1654 			ddi_get8(io_hdl1, (uchar_t *)ioaddr1 + AT_ERROR)));
1655 		return (FALSE);
1656 	}
1657 
1658 	/*
1659 	 * Check to see if the command aborted. This happens if
1660 	 * an IDENTIFY DEVICE command is issued to an ATAPI PACKET device,
1661 	 * or if an IDENTIFY PACKET DEVICE command is issued to an ATA
1662 	 * (non-PACKET) device.
1663 	 */
1664 	if (status & (ATS_DF | ATS_ERR)) {
1665 		ADBG_WARN(("ata_id_common: status 0x%x error 0x%x \n",
1666 			ddi_get8(io_hdl2, (uchar_t *)ioaddr2 + AT_ALTSTATUS),
1667 			ddi_get8(io_hdl1, (uchar_t *)ioaddr1 + AT_ERROR)));
1668 		return (FALSE);
1669 	}
1670 	return (TRUE);
1671 }
1672 
1673 
1674 /*
1675  * Low level routine to issue a non-data command and busy wait for
1676  * the completion status.
1677  */
1678 
1679 int
1680 ata_command(
1681 	ata_ctl_t *ata_ctlp,
1682 	ata_drv_t *ata_drvp,
1683 	int		 expect_drdy,
1684 	int		 silent,
1685 	uint_t		 busy_wait,
1686 	uchar_t		 cmd,
1687 	uchar_t		 feature,
1688 	uchar_t		 count,
1689 	uchar_t		 sector,
1690 	uchar_t		 head,
1691 	uchar_t		 cyl_low,
1692 	uchar_t		 cyl_hi)
1693 {
1694 	ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1;
1695 	ddi_acc_handle_t io_hdl2 = ata_ctlp->ac_iohandle2;
1696 	uchar_t		 status;
1697 
1698 	/* select the drive */
1699 	ddi_put8(io_hdl1, ata_ctlp->ac_drvhd, ata_drvp->ad_drive_bits);
1700 	ATA_DELAY_400NSEC(io_hdl2, ata_ctlp->ac_ioaddr2);
1701 
1702 	/* make certain the drive selected */
1703 	if (!ata_wait(io_hdl2, ata_ctlp->ac_ioaddr2,
1704 			(uchar_t)(expect_drdy ? ATS_DRDY : 0),
1705 			ATS_BSY, busy_wait)) {
1706 		ADBG_ERROR(("ata_command: select failed "
1707 			    "DRDY 0x%x CMD 0x%x F 0x%x N 0x%x  "
1708 			    "S 0x%x H 0x%x CL 0x%x CH 0x%x\n",
1709 			    expect_drdy, cmd, feature, count,
1710 			    sector, head, cyl_low, cyl_hi));
1711 		return (FALSE);
1712 	}
1713 
1714 	/*
1715 	 * set all the regs
1716 	 */
1717 	ddi_put8(io_hdl1, ata_ctlp->ac_drvhd, (head | ata_drvp->ad_drive_bits));
1718 	ddi_put8(io_hdl1, ata_ctlp->ac_sect, sector);
1719 	ddi_put8(io_hdl1, ata_ctlp->ac_count, count);
1720 	ddi_put8(io_hdl1, ata_ctlp->ac_lcyl, cyl_low);
1721 	ddi_put8(io_hdl1, ata_ctlp->ac_hcyl, cyl_hi);
1722 	ddi_put8(io_hdl1, ata_ctlp->ac_feature, feature);
1723 
1724 	/* send the command */
1725 	ddi_put8(io_hdl1, ata_ctlp->ac_cmd, cmd);
1726 
1727 	/* wait for the busy bit to settle */
1728 	ATA_DELAY_400NSEC(io_hdl2, ata_ctlp->ac_ioaddr2);
1729 
1730 	/* wait for not busy */
1731 	if (!ata_wait(io_hdl2, ata_ctlp->ac_ioaddr2, 0, ATS_BSY, busy_wait)) {
1732 		ADBG_ERROR(("ata_command: BSY too long!"
1733 			    "DRDY 0x%x CMD 0x%x F 0x%x N 0x%x  "
1734 			    "S 0x%x H 0x%x CL 0x%x CH 0x%x\n",
1735 			    expect_drdy, cmd, feature, count,
1736 			    sector, head, cyl_low, cyl_hi));
1737 		return (FALSE);
1738 	}
1739 
1740 	/*
1741 	 * wait for DRDY before continuing
1742 	 */
1743 	(void) ata_wait3(io_hdl2, ata_ctlp->ac_ioaddr2,
1744 			ATS_DRDY, ATS_BSY, /* okay */
1745 			ATS_ERR, ATS_BSY, /* cmd failed */
1746 			ATS_DF, ATS_BSY, /* drive failed */
1747 			busy_wait);
1748 
1749 	/* read status to clear IRQ, and check for error */
1750 	status =  ddi_get8(io_hdl1, ata_ctlp->ac_status);
1751 
1752 	if ((status & (ATS_BSY | ATS_DF | ATS_ERR)) == 0)
1753 		return (TRUE);
1754 
1755 	if (!silent) {
1756 		ADBG_ERROR(("ata_command status 0x%x error 0x%x "
1757 			    "DRDY 0x%x CMD 0x%x F 0x%x N 0x%x  "
1758 			    "S 0x%x H 0x%x CL 0x%x CH 0x%x\n",
1759 			    ddi_get8(io_hdl1, ata_ctlp->ac_status),
1760 			    ddi_get8(io_hdl1, ata_ctlp->ac_error),
1761 			    expect_drdy, cmd, feature, count,
1762 			    sector, head, cyl_low, cyl_hi));
1763 	}
1764 	return (FALSE);
1765 }
1766 
1767 
1768 
1769 /*
1770  *
1771  * Issue a SET FEATURES command
1772  *
1773  */
1774 
1775 int
1776 ata_set_feature(
1777 	ata_ctl_t *ata_ctlp,
1778 	ata_drv_t *ata_drvp,
1779 	uchar_t    feature,
1780 	uchar_t    value)
1781 {
1782 	int		 rc;
1783 
1784 	rc = ata_command(ata_ctlp, ata_drvp, TRUE, TRUE, ata_set_feature_wait,
1785 		ATC_SET_FEAT, feature, value, 0, 0, 0, 0);
1786 		/* feature, count, sector, head, cyl_low, cyl_hi */
1787 
1788 	if (rc) {
1789 		return (TRUE);
1790 	}
1791 
1792 	ADBG_ERROR(("?ata_set_feature: (0x%x,0x%x) failed\n", feature, value));
1793 	return (FALSE);
1794 }
1795 
1796 
1797 
1798 /*
1799  *
1800  * Issue a FLUSH CACHE command
1801  *
1802  */
1803 
1804 static int
1805 ata_flush_cache(
1806 	ata_ctl_t *ata_ctlp,
1807 	ata_drv_t *ata_drvp)
1808 {
1809 	/* this command is optional so fail silently */
1810 	return (ata_command(ata_ctlp, ata_drvp, TRUE, TRUE,
1811 			    ata_flush_cache_wait,
1812 			    ATC_FLUSH_CACHE, 0, 0, 0, 0, 0, 0));
1813 }
1814 
1815 /*
1816  * ata_setup_ioaddr()
1817  *
1818  * Map the device registers and return the handles.
1819  *
1820  * If this is a ISA-ATA controller then only two handles are
1821  * initialized and returned.
1822  *
1823  * If this is a PCI-IDE controller than a third handle (for the
1824  * PCI-IDE Bus Mastering registers) is initialized and returned.
1825  *
1826  */
1827 
1828 static int
1829 ata_setup_ioaddr(
1830 	dev_info_t	 *dip,
1831 	ddi_acc_handle_t *handle1p,
1832 	caddr_t		 *addr1p,
1833 	ddi_acc_handle_t *handle2p,
1834 	caddr_t		 *addr2p,
1835 	ddi_acc_handle_t *bm_hdlp,
1836 	caddr_t		 *bm_addrp)
1837 {
1838 	ddi_device_acc_attr_t dev_attr;
1839 	char	*bufp;
1840 	int	 rnumber;
1841 	int	 rc;
1842 	off_t	 regsize;
1843 
1844 	/*
1845 	 * Make certain the controller is enabled and its regs are map-able
1846 	 *
1847 	 */
1848 	rc = ddi_dev_regsize(dip, 0, &regsize);
1849 	if (rc != DDI_SUCCESS || regsize <= AT_CMD) {
1850 		ADBG_INIT(("ata_setup_ioaddr(1): rc %d regsize %lld\n",
1851 			    rc, (long long)regsize));
1852 		return (FALSE);
1853 	}
1854 
1855 	rc = ddi_dev_regsize(dip, 1, &regsize);
1856 	if (rc != DDI_SUCCESS || regsize <= AT_ALTSTATUS) {
1857 		ADBG_INIT(("ata_setup_ioaddr(2): rc %d regsize %lld\n",
1858 			    rc, (long long)regsize));
1859 		return (FALSE);
1860 	}
1861 
1862 	/*
1863 	 * setup the device attribute structure for little-endian,
1864 	 * strict ordering access.
1865 	 */
1866 	dev_attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
1867 	dev_attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
1868 	dev_attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
1869 
1870 	*handle1p = NULL;
1871 	*handle2p = NULL;
1872 	*bm_hdlp = NULL;
1873 
1874 	/*
1875 	 * Determine whether this is a ISA, PNP-ISA, or PCI-IDE device
1876 	 */
1877 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "pnp-csn")) {
1878 		/* it's PNP-ISA, skip over the extra reg tuple */
1879 		rnumber = 1;
1880 		goto not_pciide;
1881 	}
1882 
1883 	/* else, it's ISA or PCI-IDE, check further */
1884 	rnumber = 0;
1885 
1886 	rc = ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_get_parent(dip),
1887 				    DDI_PROP_DONTPASS, "device_type", &bufp);
1888 	if (rc != DDI_PROP_SUCCESS) {
1889 		ADBG_ERROR(("ata_setup_ioaddr !device_type\n"));
1890 		goto not_pciide;
1891 	}
1892 
1893 	if (strcmp(bufp, "pci-ide") != 0) {
1894 		/*
1895 		 * If it's not a PCI-IDE, there are only two reg tuples
1896 		 * and the first one contains the I/O base (170 or 1f0)
1897 		 * rather than the controller instance number.
1898 		 */
1899 		ADBG_TRACE(("ata_setup_ioaddr !pci-ide\n"));
1900 		ddi_prop_free(bufp);
1901 		goto not_pciide;
1902 	}
1903 	ddi_prop_free(bufp);
1904 
1905 
1906 	/*
1907 	 * Map the correct half of the PCI-IDE Bus Master registers.
1908 	 * There's a single BAR that maps these registers for both
1909 	 * controller's in a dual-controller chip and it's upto my
1910 	 * parent nexus, pciide, to adjust which (based on my instance
1911 	 * number) half this call maps.
1912 	 */
1913 	rc = ddi_dev_regsize(dip, 2, &regsize);
1914 	if (rc != DDI_SUCCESS || regsize < 8) {
1915 		ADBG_INIT(("ata_setup_ioaddr(3): rc %d regsize %lld\n",
1916 			    rc, (long long)regsize));
1917 		goto not_pciide;
1918 	}
1919 
1920 	rc = ddi_regs_map_setup(dip, 2, bm_addrp, 0, 0, &dev_attr, bm_hdlp);
1921 
1922 	if (rc != DDI_SUCCESS) {
1923 		/* map failed, try to use in non-pci-ide mode */
1924 		ADBG_WARN(("ata_setup_ioaddr bus master map failed, rc=0x%x\n",
1925 			rc));
1926 		*bm_hdlp = NULL;
1927 	}
1928 
1929 not_pciide:
1930 	/*
1931 	 * map the lower command block registers
1932 	 */
1933 
1934 	rc = ddi_regs_map_setup(dip, rnumber, addr1p, 0, 0, &dev_attr,
1935 				handle1p);
1936 
1937 	if (rc != DDI_SUCCESS) {
1938 		cmn_err(CE_WARN, "ata: reg tuple 0 map failed, rc=0x%x\n", rc);
1939 		goto out1;
1940 	}
1941 
1942 	/*
1943 	 * If the controller is being used in compatibility mode
1944 	 * via /devices/isa/ata@1,{1f0,1f0}/..., the reg property
1945 	 * will specify zeros for the I/O ports for the PCI
1946 	 * instance.
1947 	 */
1948 	if (*addr1p == 0) {
1949 		ADBG_TRACE(("ata_setup_ioaddr ioaddr1 0\n"));
1950 		goto out2;
1951 	}
1952 
1953 	/*
1954 	 * map the upper control block registers
1955 	 */
1956 	rc = ddi_regs_map_setup(dip, rnumber + 1, addr2p, 0, 0, &dev_attr,
1957 				handle2p);
1958 	if (rc == DDI_SUCCESS)
1959 		return (TRUE);
1960 
1961 	cmn_err(CE_WARN, "ata: reg tuple 1 map failed, rc=0x%x", rc);
1962 
1963 out2:
1964 	if (*handle1p != NULL) {
1965 		ddi_regs_map_free(handle1p);
1966 		*handle1p = NULL;
1967 	}
1968 
1969 out1:
1970 	if (*bm_hdlp != NULL) {
1971 		ddi_regs_map_free(bm_hdlp);
1972 		*bm_hdlp = NULL;
1973 	}
1974 	return (FALSE);
1975 
1976 }
1977 
1978 /*
1979  *
1980  * Currently, the only supported controllers are ones which
1981  * support the SFF-8038 Bus Mastering spec.
1982  *
1983  * Check the parent node's IEEE 1275 class-code property to
1984  * determine if it's an PCI-IDE instance which supports SFF-8038
1985  * Bus Mastering. It's perfectly valid to have a PCI-IDE controller
1986  * that doesn't do Bus Mastering. In that case, my interrupt handler
1987  * only uses the interrupt latch bit in PCI-IDE status register.
1988  * The assumption is that the programming interface byte of the
1989  * class-code property reflects the bus master DMA capability of
1990  * the controller.
1991  *
1992  * Whether the drive support supports the DMA option still needs
1993  * to be checked later. Each individual request also has to be
1994  * checked for alignment and size to decide whether to use the
1995  * DMA transfer mode.
1996  */
1997 
1998 static void
1999 ata_init_pciide(
2000 	dev_info_t	 *dip,
2001 	ata_ctl_t *ata_ctlp)
2002 {
2003 	uint_t	 class_code;
2004 	uchar_t	 status;
2005 
2006 	ata_cntrl_DMA_sel_msg = NULL;
2007 
2008 	if (ata_ctlp->ac_bmhandle == NULL) {
2009 		ata_ctlp->ac_pciide = FALSE;
2010 		ata_ctlp->ac_pciide_bm = FALSE;
2011 		ata_cntrl_DMA_sel_msg = "cntrl not Bus Master DMA capable";
2012 		return;
2013 	}
2014 
2015 	/*
2016 	 * check if it's a known bogus PCI-IDE chip
2017 	 */
2018 	if (ata_check_pciide_blacklist(dip, ATA_BL_BOGUS)) {
2019 		ADBG_WARN(("ata_setup_ioaddr pci-ide blacklist\n"));
2020 		ata_ctlp->ac_pciide = FALSE;
2021 		ata_ctlp->ac_pciide_bm = FALSE;
2022 		ata_cntrl_DMA_sel_msg = "cntrl blacklisted";
2023 		return;
2024 	}
2025 	ata_ctlp->ac_pciide = TRUE;
2026 
2027 	if (ata_check_pciide_blacklist(dip, ATA_BL_BMSTATREG_PIO_BROKEN)) {
2028 		ata_ctlp->ac_flags |= AC_BMSTATREG_PIO_BROKEN;
2029 	}
2030 
2031 	/*
2032 	 * check for a PCI-IDE chip with a broken DMA engine
2033 	 */
2034 	if (ata_check_pciide_blacklist(dip, ATA_BL_NODMA)) {
2035 		ata_ctlp->ac_pciide_bm = FALSE;
2036 		ata_cntrl_DMA_sel_msg =
2037 			"cntrl blacklisted/DMA engine broken";
2038 		return;
2039 	}
2040 
2041 	/*
2042 	 * Check the Programming Interface register to determine
2043 	 * if this device supports PCI-IDE Bus Mastering. Some PCI-IDE
2044 	 * devices don't support Bus Mastering or DMA.
2045 	 * Since we are dealing with pre-qualified pci-ide controller,
2046 	 * check programming interface byte only.
2047 	 */
2048 
2049 	class_code = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip),
2050 		DDI_PROP_DONTPASS, "class-code", 0);
2051 	if ((class_code & PCIIDE_BM_CAP_MASK) != PCIIDE_BM_CAP_MASK) {
2052 		ata_ctlp->ac_pciide_bm = FALSE;
2053 		ata_cntrl_DMA_sel_msg =
2054 			"cntrl not Bus Master DMA capable";
2055 		return;
2056 	}
2057 
2058 	/*
2059 	 * Avoid doing DMA on "simplex" chips which share hardware
2060 	 * between channels
2061 	 */
2062 	status = ddi_get8(ata_ctlp->ac_bmhandle,
2063 			(uchar_t *)ata_ctlp->ac_bmaddr + PCIIDE_BMISX_REG);
2064 	/*
2065 	 * Some motherboards have CSB5's that are wired "to emulate CSB4 mode".
2066 	 * In such a mode, the simplex bit is asserted,  but in fact testing
2067 	 * on such a motherboard has shown that the devices are not simplex
2068 	 * -- DMA can be used on both channels concurrently with no special
2069 	 * considerations.  For chips like this, we have the ATA_BL_NO_SIMPLEX
2070 	 * flag set to indicate that the value of the simplex bit can be
2071 	 * ignored.
2072 	 */
2073 
2074 	if (status & PCIIDE_BMISX_SIMPLEX) {
2075 		if (ata_check_pciide_blacklist(dip, ATA_BL_NO_SIMPLEX))  {
2076 			cmn_err(CE_WARN, "Ignoring false simplex bit \n");
2077 		} else {
2078 			ata_ctlp->ac_pciide_bm = FALSE;
2079 			ata_cntrl_DMA_sel_msg =
2080 				"cntrl sharing DMA engine between channels";
2081 			return;
2082 		}
2083 	}
2084 
2085 	/*
2086 	 * It's a compatible PCI-IDE Bus Mastering controller,
2087 	 * allocate and map the DMA Scatter/Gather list (PRDE table).
2088 	 */
2089 	if (ata_pciide_alloc(dip, ata_ctlp))
2090 		ata_ctlp->ac_pciide_bm = TRUE;
2091 	else {
2092 		ata_ctlp->ac_pciide_bm = FALSE;
2093 		ata_cntrl_DMA_sel_msg = "unable to init DMA S/G list";
2094 	}
2095 }
2096 
2097 /*
2098  *
2099  * Determine whether to enable DMA support for this drive.
2100  * The controller and the drive both have to support DMA.
2101  * The controller's capabilities were already checked in
2102  * ata_init_pciide(), now just check the drive's capabilities.
2103  *
2104  */
2105 
2106 static int
2107 ata_init_drive_pcidma(
2108 	ata_ctl_t *ata_ctlp,
2109 	ata_drv_t *ata_drvp,
2110 	dev_info_t *tdip)
2111 {
2112 	boolean_t dma;
2113 	boolean_t cd_dma;
2114 	boolean_t disk_dma;
2115 	boolean_t atapi_dma;
2116 	int ata_options;
2117 
2118 	ata_dev_DMA_sel_msg = NULL;
2119 
2120 	if (ata_ctlp->ac_pciide_bm != TRUE) {
2121 		ata_dev_DMA_sel_msg =
2122 		    "controller is not Bus Master capable";
2123 
2124 		return (ATA_DMA_OFF);
2125 	}
2126 
2127 	ata_options = ddi_prop_get_int(DDI_DEV_T_ANY, ata_ctlp->ac_dip,
2128 			0, "ata-options", 0);
2129 
2130 	if (!(ata_options & ATA_OPTIONS_DMA)) {
2131 		/*
2132 		 * Either the ata-options property was not found or
2133 		 * DMA is not enabled by this property
2134 		 */
2135 		ata_dev_DMA_sel_msg =
2136 			"disabled by \"ata-options\" property";
2137 
2138 		return (ATA_DMA_OFF);
2139 	}
2140 
2141 	if (ata_check_drive_blacklist(&ata_drvp->ad_id, ATA_BL_NODMA)) {
2142 		ata_dev_DMA_sel_msg = "device not DMA capable; blacklisted";
2143 
2144 		return (ATA_DMA_OFF);
2145 	}
2146 
2147 	/*
2148 	 * DMA mode is mandatory on ATA-3 (or newer) drives but is
2149 	 * optional on ATA-2 (or older) drives.
2150 	 *
2151 	 * On ATA-2 drives the ai_majorversion word will probably
2152 	 * be 0xffff or 0x0000, check the (now obsolete) DMA bit in
2153 	 * the capabilities word instead. The order of these tests
2154 	 * is important since an ATA-3 drive doesn't have to set
2155 	 * the DMA bit in the capabilities word.
2156 	 *
2157 	 */
2158 
2159 	if (!((ata_drvp->ad_id.ai_majorversion & 0x8000) == 0 &&
2160 	    ata_drvp->ad_id.ai_majorversion >= (1 << 2)) &&
2161 	    !(ata_drvp->ad_id.ai_cap & ATAC_DMA_SUPPORT)) {
2162 		ata_dev_DMA_sel_msg = "device not DMA capable";
2163 
2164 		return (ATA_DMA_OFF);
2165 	}
2166 
2167 	dma = ata_prop_lookup_int(DDI_DEV_T_ANY, tdip,
2168 		0, "ata-dma-enabled", TRUE);
2169 	disk_dma = ata_prop_lookup_int(DDI_DEV_T_ANY, tdip,
2170 		0, "ata-disk-dma-enabled", TRUE);
2171 	cd_dma = ata_prop_lookup_int(DDI_DEV_T_ANY, tdip,
2172 		0, "atapi-cd-dma-enabled", FALSE);
2173 	atapi_dma = ata_prop_lookup_int(DDI_DEV_T_ANY, tdip,
2174 		0, "atapi-other-dma-enabled", TRUE);
2175 
2176 	if (dma == FALSE) {
2177 		cmn_err(CE_CONT, "?ata_init_drive_pcidma: "
2178 		    "DMA disabled by \"ata-dma-enabled\" property");
2179 		ata_dev_DMA_sel_msg = "disabled by prop ata-dma-enabled";
2180 
2181 		return (ATA_DMA_OFF);
2182 	}
2183 
2184 	if (IS_CDROM(ata_drvp) == TRUE) {
2185 		if (cd_dma == FALSE) {
2186 			ata_dev_DMA_sel_msg =
2187 			    "disabled.  Control with \"atapi-cd-dma-enabled\""
2188 			    " property";
2189 
2190 			return (ATA_DMA_OFF);
2191 		}
2192 
2193 	} else if (ATAPIDRV(ata_drvp) == FALSE) {
2194 		if (disk_dma == FALSE) {
2195 			ata_dev_DMA_sel_msg =
2196 			    "disabled by \"ata-disk-dma-enabled\" property";
2197 
2198 			return (ATA_DMA_OFF);
2199 		}
2200 
2201 	} else if (atapi_dma == FALSE) {
2202 			ata_dev_DMA_sel_msg =
2203 			    "disabled by \"atapi-other-dma-enabled\" property";
2204 
2205 			return (ATA_DMA_OFF);
2206 	}
2207 
2208 	return (ATA_DMA_ON);
2209 }
2210 
2211 
2212 
2213 /*
2214  * this compare routine squeezes out extra blanks and
2215  * returns TRUE if p1 matches the leftmost substring of p2
2216  */
2217 
2218 static int
2219 ata_strncmp(
2220 	char *p1,
2221 	char *p2,
2222 	int cnt)
2223 {
2224 
2225 	for (;;) {
2226 		/*
2227 		 * skip over any extra blanks in both strings
2228 		 */
2229 		while (*p1 != '\0' && *p1 == ' ')
2230 			p1++;
2231 
2232 		while (cnt != 0 && *p2 == ' ') {
2233 			p2++;
2234 			cnt--;
2235 		}
2236 
2237 		/*
2238 		 * compare the two strings
2239 		 */
2240 
2241 		if (cnt == 0 || *p1 != *p2)
2242 			break;
2243 
2244 		while (cnt > 0 && *p1 == *p2) {
2245 			p1++;
2246 			p2++;
2247 			cnt--;
2248 		}
2249 
2250 	}
2251 
2252 	/* return TRUE if both strings ended at same point */
2253 	return ((*p1 == '\0') ? TRUE : FALSE);
2254 }
2255 
2256 /*
2257  * Per PSARC/1997/281 create variant="atapi" property (if necessary)
2258  * on the target's dev_info node. Currently, the sd target driver
2259  * is the only driver which refers to this property.
2260  *
2261  * If the flag ata_id_debug is set also create the
2262  * the "ata" or "atapi" property on the target's dev_info node
2263  *
2264  */
2265 
2266 int
2267 ata_prop_create(
2268 	dev_info_t *tgt_dip,
2269 	ata_drv_t  *ata_drvp,
2270 	char	   *name)
2271 {
2272 	int	rc;
2273 
2274 	ADBG_TRACE(("ata_prop_create 0x%p 0x%p %s\n", tgt_dip, ata_drvp, name));
2275 
2276 	if (strcmp("atapi", name) == 0) {
2277 		rc =  ndi_prop_update_string(DDI_DEV_T_NONE, tgt_dip,
2278 			"variant", name);
2279 		if (rc != DDI_PROP_SUCCESS)
2280 			return (FALSE);
2281 	}
2282 
2283 	if (!ata_id_debug)
2284 		return (TRUE);
2285 
2286 	rc =  ndi_prop_update_byte_array(DDI_DEV_T_NONE, tgt_dip, name,
2287 		(uchar_t *)&ata_drvp->ad_id, sizeof (ata_drvp->ad_id));
2288 	if (rc != DDI_PROP_SUCCESS) {
2289 		ADBG_ERROR(("ata_prop_create failed, rc=%d\n", rc));
2290 	}
2291 	return (TRUE);
2292 }
2293 
2294 
2295 /* *********************************************************************** */
2296 /* *********************************************************************** */
2297 /* *********************************************************************** */
2298 
2299 /*
2300  * This state machine doesn't implement the ATAPI Optional Overlap
2301  * feature. You need that feature to efficiently support ATAPI
2302  * tape drives. See the 1394-ATA Tailgate spec (D97107), Figure 24,
2303  * for an example of how to add the necessary additional NextActions
2304  * and NextStates to this FSM and the atapi_fsm, in order to support
2305  * the Overlap Feature.
2306  */
2307 
2308 
2309 uchar_t ata_ctlr_fsm_NextAction[ATA_CTLR_NSTATES][ATA_CTLR_NFUNCS] = {
2310 /* --------------------- next action --------------------- | - current - */
2311 /* start0 --- start1 ---- intr ------ fini --- reset --- */
2312 { AC_START,   AC_START,	  AC_NADA,    AC_NADA, AC_RESET_I }, /* idle	 */
2313 { AC_BUSY,    AC_BUSY,	  AC_INTR,    AC_FINI, AC_RESET_A }, /* active0  */
2314 { AC_BUSY,    AC_BUSY,	  AC_INTR,    AC_FINI, AC_RESET_A }, /* active1  */
2315 };
2316 
2317 uchar_t ata_ctlr_fsm_NextState[ATA_CTLR_NSTATES][ATA_CTLR_NFUNCS] = {
2318 
2319 /* --------------------- next state --------------------- | - current - */
2320 /* start0 --- start1 ---- intr ------ fini --- reset --- */
2321 { AS_ACTIVE0, AS_ACTIVE1, AS_IDLE,    AS_IDLE, AS_IDLE	  }, /* idle    */
2322 { AS_ACTIVE0, AS_ACTIVE0, AS_ACTIVE0, AS_IDLE, AS_ACTIVE0 }, /* active0 */
2323 { AS_ACTIVE1, AS_ACTIVE1, AS_ACTIVE1, AS_IDLE, AS_ACTIVE1 }, /* active1 */
2324 };
2325 
2326 
2327 static int
2328 ata_ctlr_fsm(
2329 	uchar_t		 fsm_func,
2330 	ata_ctl_t	*ata_ctlp,
2331 	ata_drv_t	*ata_drvp,
2332 	ata_pkt_t	*ata_pktp,
2333 	int		*DoneFlgp)
2334 {
2335 	uchar_t	   action;
2336 	uchar_t	   current_state;
2337 	uchar_t	   next_state;
2338 	int	   rc;
2339 
2340 	current_state = ata_ctlp->ac_state;
2341 	action = ata_ctlr_fsm_NextAction[current_state][fsm_func];
2342 	next_state = ata_ctlr_fsm_NextState[current_state][fsm_func];
2343 
2344 	/*
2345 	 * Set the controller's new state
2346 	 */
2347 	ata_ctlp->ac_state = next_state;
2348 	switch (action) {
2349 
2350 	case AC_BUSY:
2351 		return (ATA_FSM_RC_BUSY);
2352 
2353 	case AC_NADA:
2354 		return (ATA_FSM_RC_OKAY);
2355 
2356 	case AC_START:
2357 		ASSERT(ata_ctlp->ac_active_pktp == NULL);
2358 		ASSERT(ata_ctlp->ac_active_drvp == NULL);
2359 
2360 		ata_ctlp->ac_active_pktp = ata_pktp;
2361 		ata_ctlp->ac_active_drvp = ata_drvp;
2362 
2363 		rc = (*ata_pktp->ap_start)(ata_ctlp, ata_drvp, ata_pktp);
2364 
2365 		if (rc == ATA_FSM_RC_BUSY) {
2366 			/* the request didn't start, GHD will requeue it */
2367 			ata_ctlp->ac_state = AS_IDLE;
2368 			ata_ctlp->ac_active_pktp = NULL;
2369 			ata_ctlp->ac_active_drvp = NULL;
2370 		}
2371 		return (rc);
2372 
2373 	case AC_INTR:
2374 		ASSERT(ata_ctlp->ac_active_pktp != NULL);
2375 		ASSERT(ata_ctlp->ac_active_drvp != NULL);
2376 
2377 		ata_drvp = ata_ctlp->ac_active_drvp;
2378 		ata_pktp = ata_ctlp->ac_active_pktp;
2379 		return ((*ata_pktp->ap_intr)(ata_ctlp, ata_drvp, ata_pktp));
2380 
2381 	case AC_RESET_A: /* Reset, controller active */
2382 		ASSERT(ata_ctlp->ac_active_pktp != NULL);
2383 		ASSERT(ata_ctlp->ac_active_drvp != NULL);
2384 
2385 		/* clean up the active request */
2386 		ata_pktp = ata_ctlp->ac_active_pktp;
2387 		ata_pktp->ap_flags |= AP_DEV_RESET | AP_BUS_RESET;
2388 
2389 		/* halt the DMA engine */
2390 		if (ata_pktp->ap_pciide_dma) {
2391 			ata_pciide_dma_stop(ata_ctlp);
2392 			(void) ata_pciide_status_clear(ata_ctlp);
2393 		}
2394 
2395 		/* Do a Software Reset to unwedge the bus */
2396 		if (!ata_software_reset(ata_ctlp)) {
2397 			return (ATA_FSM_RC_BUSY);
2398 		}
2399 
2400 		/* Then send a DEVICE RESET cmd to each ATAPI device */
2401 		atapi_fsm_reset(ata_ctlp);
2402 		return (ATA_FSM_RC_FINI);
2403 
2404 	case AC_RESET_I: /* Reset, controller idle */
2405 		/* Do a Software Reset to unwedge the bus */
2406 		if (!ata_software_reset(ata_ctlp)) {
2407 			return (ATA_FSM_RC_BUSY);
2408 		}
2409 
2410 		/* Then send a DEVICE RESET cmd to each ATAPI device */
2411 		atapi_fsm_reset(ata_ctlp);
2412 		return (ATA_FSM_RC_OKAY);
2413 
2414 	case AC_FINI:
2415 		break;
2416 	}
2417 
2418 	/*
2419 	 * AC_FINI, check ARQ needs to be started or finished
2420 	 */
2421 
2422 	ASSERT(action == AC_FINI);
2423 	ASSERT(ata_ctlp->ac_active_pktp != NULL);
2424 	ASSERT(ata_ctlp->ac_active_drvp != NULL);
2425 
2426 	/*
2427 	 * The active request is done now.
2428 	 * Disconnect the request from the controller and
2429 	 * add it to the done queue.
2430 	 */
2431 	ata_drvp = ata_ctlp->ac_active_drvp;
2432 	ata_pktp = ata_ctlp->ac_active_pktp;
2433 
2434 	/*
2435 	 * If ARQ pkt is done, get ptr to original pkt and wrap it up.
2436 	 */
2437 	if (ata_pktp == ata_ctlp->ac_arq_pktp) {
2438 		ata_pkt_t *arq_pktp;
2439 
2440 		ADBG_ARQ(("ata_ctlr_fsm 0x%p ARQ done\n", ata_ctlp));
2441 
2442 		arq_pktp = ata_pktp;
2443 		ata_pktp = ata_ctlp->ac_fault_pktp;
2444 		ata_ctlp->ac_fault_pktp = NULL;
2445 		if (arq_pktp->ap_flags & (AP_ERROR | AP_BUS_RESET))
2446 			ata_pktp->ap_flags |= AP_ARQ_ERROR;
2447 		else
2448 			ata_pktp->ap_flags |= AP_ARQ_OKAY;
2449 		goto all_done;
2450 	}
2451 
2452 
2453 #define	AP_ARQ_NEEDED	(AP_ARQ_ON_ERROR | AP_GOT_STATUS | AP_ERROR)
2454 
2455 	/*
2456 	 * Start ARQ pkt if necessary
2457 	 */
2458 	if ((ata_pktp->ap_flags & AP_ARQ_NEEDED) == AP_ARQ_NEEDED &&
2459 			(ata_pktp->ap_status & ATS_ERR)) {
2460 
2461 		/* set controller state back to active */
2462 		ata_ctlp->ac_state = current_state;
2463 
2464 		/* try to start the ARQ pkt */
2465 		rc = ata_start_arq(ata_ctlp, ata_drvp, ata_pktp);
2466 
2467 		if (rc == ATA_FSM_RC_BUSY) {
2468 			ADBG_ARQ(("ata_ctlr_fsm 0x%p ARQ BUSY\n", ata_ctlp));
2469 			/* let the target driver handle the problem */
2470 			ata_ctlp->ac_state = AS_IDLE;
2471 			ata_ctlp->ac_active_pktp = NULL;
2472 			ata_ctlp->ac_active_drvp = NULL;
2473 			ata_ctlp->ac_fault_pktp = NULL;
2474 			goto all_done;
2475 		}
2476 
2477 		ADBG_ARQ(("ata_ctlr_fsm 0x%p ARQ started\n", ata_ctlp));
2478 		return (rc);
2479 	}
2480 
2481 	/*
2482 	 * Normal completion, no error status, and not an ARQ pkt,
2483 	 * just fall through.
2484 	 */
2485 
2486 all_done:
2487 
2488 	/*
2489 	 * wrap everything up and tie a ribbon around it
2490 	 */
2491 	ata_ctlp->ac_active_pktp = NULL;
2492 	ata_ctlp->ac_active_drvp = NULL;
2493 	if (APKT2GCMD(ata_pktp) != (gcmd_t *)0) {
2494 		ghd_complete(&ata_ctlp->ac_ccc, APKT2GCMD(ata_pktp));
2495 		if (DoneFlgp)
2496 			*DoneFlgp = TRUE;
2497 	}
2498 
2499 	return (ATA_FSM_RC_OKAY);
2500 }
2501 
2502 
2503 static int
2504 ata_start_arq(
2505 	ata_ctl_t *ata_ctlp,
2506 	ata_drv_t *ata_drvp,
2507 	ata_pkt_t *ata_pktp)
2508 {
2509 	ata_pkt_t		*arq_pktp;
2510 	int			 bytes;
2511 	uint_t			 senselen;
2512 
2513 	ADBG_ARQ(("ata_start_arq 0x%p ARQ needed\n", ata_ctlp));
2514 
2515 	/*
2516 	 * Determine just the size of the Request Sense Data buffer within
2517 	 * the scsi_arq_status structure.
2518 	 */
2519 #define	SIZEOF_ARQ_HEADER	(sizeof (struct scsi_arq_status)	\
2520 				- sizeof (struct scsi_extended_sense))
2521 	senselen = ata_pktp->ap_statuslen - SIZEOF_ARQ_HEADER;
2522 	ASSERT(senselen > 0);
2523 
2524 
2525 	/* save ptr to original pkt */
2526 	ata_ctlp->ac_fault_pktp = ata_pktp;
2527 
2528 	/* switch the controller's active pkt to the ARQ pkt */
2529 	arq_pktp = ata_ctlp->ac_arq_pktp;
2530 	ata_ctlp->ac_active_pktp = arq_pktp;
2531 
2532 	/* finish initializing the ARQ CDB */
2533 	ata_ctlp->ac_arq_cdb[1] = ata_drvp->ad_lun << 4;
2534 	ata_ctlp->ac_arq_cdb[4] = senselen;
2535 
2536 	/* finish initializing the ARQ pkt */
2537 	arq_pktp->ap_v_addr = (caddr_t)&ata_pktp->ap_scbp->sts_sensedata;
2538 
2539 	arq_pktp->ap_resid = senselen;
2540 	arq_pktp->ap_flags = AP_ATAPI | AP_READ;
2541 	arq_pktp->ap_cdb_pad =
2542 	((unsigned)(ata_drvp->ad_cdb_len - arq_pktp->ap_cdb_len)) >> 1;
2543 
2544 	bytes = min(senselen, ATAPI_MAX_BYTES_PER_DRQ);
2545 	arq_pktp->ap_hicyl = (uchar_t)(bytes >> 8);
2546 	arq_pktp->ap_lwcyl = (uchar_t)bytes;
2547 
2548 	/*
2549 	 * This packet is shared by all drives on this controller
2550 	 * therefore we need to init the drive number on every ARQ.
2551 	 */
2552 	arq_pktp->ap_hd = ata_drvp->ad_drive_bits;
2553 
2554 	/* start it up */
2555 	return ((*arq_pktp->ap_start)(ata_ctlp, ata_drvp, arq_pktp));
2556 }
2557 
2558 /*
2559  *
2560  * reset the bus
2561  *
2562  */
2563 
2564 static int
2565 ata_reset_bus(
2566 	ata_ctl_t *ata_ctlp)
2567 {
2568 	int	watchdog;
2569 	uchar_t	drive;
2570 	int	rc = FALSE;
2571 	uchar_t	fsm_func;
2572 	int	DoneFlg = FALSE;
2573 
2574 	/*
2575 	 * Do a Software Reset to unwedge the bus, and send
2576 	 * ATAPI DEVICE RESET to each ATAPI drive.
2577 	 */
2578 	fsm_func = ATA_FSM_RESET;
2579 	for (watchdog = ata_reset_bus_watchdog; watchdog > 0; watchdog--) {
2580 		switch (ata_ctlr_fsm(fsm_func, ata_ctlp, NULL, NULL,
2581 						&DoneFlg)) {
2582 		case ATA_FSM_RC_OKAY:
2583 			rc = TRUE;
2584 			goto fsm_done;
2585 
2586 		case ATA_FSM_RC_BUSY:
2587 			return (FALSE);
2588 
2589 		case ATA_FSM_RC_INTR:
2590 			fsm_func = ATA_FSM_INTR;
2591 			rc = TRUE;
2592 			continue;
2593 
2594 		case ATA_FSM_RC_FINI:
2595 			fsm_func = ATA_FSM_FINI;
2596 			rc = TRUE;
2597 			continue;
2598 		}
2599 	}
2600 	ADBG_WARN(("ata_reset_bus: watchdog\n"));
2601 
2602 fsm_done:
2603 
2604 	/*
2605 	 * Reinitialize the ATA drives
2606 	 */
2607 	for (drive = 0; drive < ATA_MAXTARG; drive++) {
2608 		ata_drv_t *ata_drvp;
2609 
2610 		if ((ata_drvp = CTL2DRV(ata_ctlp, drive, 0)) == NULL)
2611 			continue;
2612 
2613 		if (ATAPIDRV(ata_drvp))
2614 			continue;
2615 
2616 		/*
2617 		 * Reprogram the Read/Write Multiple block factor
2618 		 * and current geometry into the drive.
2619 		 */
2620 		if (!ata_disk_setup_parms(ata_ctlp, ata_drvp))
2621 			rc = FALSE;
2622 	}
2623 
2624 	/* If DoneFlg is TRUE, it means that ghd_complete() function */
2625 	/* has been already called. In this case ignore any errors and */
2626 	/* return TRUE to the caller, otherwise return the value of rc */
2627 	/* to the caller */
2628 	if (DoneFlg)
2629 		return (TRUE);
2630 	else
2631 		return (rc);
2632 }
2633 
2634 
2635 /*
2636  *
2637  * Low level routine to toggle the Software Reset bit
2638  *
2639  */
2640 
2641 static int
2642 ata_software_reset(
2643 	ata_ctl_t *ata_ctlp)
2644 {
2645 	ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1;
2646 	ddi_acc_handle_t io_hdl2 = ata_ctlp->ac_iohandle2;
2647 	int		 time_left;
2648 
2649 	ADBG_TRACE(("ata_reset_bus entered\n"));
2650 
2651 	/* disable interrupts and turn the software reset bit on */
2652 	ddi_put8(io_hdl2, ata_ctlp->ac_devctl, (ATDC_D3 | ATDC_SRST));
2653 
2654 	/* why 30 milliseconds, the ATA/ATAPI-4 spec says 5 usec. */
2655 	drv_usecwait(30000);
2656 
2657 	/* turn the software reset bit back off */
2658 	ddi_put8(io_hdl2, ata_ctlp->ac_devctl, ATDC_D3);
2659 
2660 	/*
2661 	 * Wait for the controller to assert BUSY status.
2662 	 * I don't think 300 msecs is correct. The ATA/ATAPI-4
2663 	 * spec says 400 nsecs, (and 2 msecs if device
2664 	 * was in sleep mode; but we don't put drives to sleep
2665 	 * so it probably doesn't matter).
2666 	 */
2667 	drv_usecwait(300000);
2668 
2669 	/*
2670 	 * If drive 0 exists the test for completion is simple
2671 	 */
2672 	time_left = 31 * 1000000;
2673 	if (CTL2DRV(ata_ctlp, 0, 0)) {
2674 		goto wait_for_not_busy;
2675 	}
2676 
2677 	ASSERT(CTL2DRV(ata_ctlp, 1, 0) != NULL);
2678 
2679 	/*
2680 	 * This must be a single device configuration, with drive 1
2681 	 * only. This complicates the test for completion because
2682 	 * issuing the software reset just caused drive 1 to
2683 	 * deselect. With drive 1 deselected, if I just read the
2684 	 * status register to test the BSY bit I get garbage, but
2685 	 * I can't re-select drive 1 until I'm certain the BSY bit
2686 	 * is de-asserted. Catch-22.
2687 	 *
2688 	 * In ATA/ATAPI-4, rev 15, section 9.16.2, it says to handle
2689 	 * this situation like this:
2690 	 */
2691 
2692 	/* give up if the drive doesn't settle within 31 seconds */
2693 	while (time_left > 0) {
2694 		/*
2695 		 * delay 10msec each time around the loop
2696 		 */
2697 		drv_usecwait(10000);
2698 		time_left -= 10000;
2699 
2700 		/*
2701 		 * try to select drive 1
2702 		 */
2703 		ddi_put8(io_hdl1, ata_ctlp->ac_drvhd, ATDH_DRIVE1);
2704 
2705 		ddi_put8(io_hdl1, ata_ctlp->ac_sect, 0x55);
2706 		ddi_put8(io_hdl1, ata_ctlp->ac_sect, 0xaa);
2707 		if (ddi_get8(io_hdl1, ata_ctlp->ac_sect) != 0xaa)
2708 			continue;
2709 
2710 		ddi_put8(io_hdl1, ata_ctlp->ac_count, 0x55);
2711 		ddi_put8(io_hdl1, ata_ctlp->ac_count, 0xaa);
2712 		if (ddi_get8(io_hdl1, ata_ctlp->ac_count) != 0xaa)
2713 			continue;
2714 
2715 		goto wait_for_not_busy;
2716 	}
2717 	return (FALSE);
2718 
2719 wait_for_not_busy:
2720 
2721 	/*
2722 	 * Now wait upto 31 seconds for BUSY to clear.
2723 	 */
2724 	(void) ata_wait3(io_hdl2, ata_ctlp->ac_ioaddr2, 0, ATS_BSY,
2725 		ATS_ERR, ATS_BSY, ATS_DF, ATS_BSY, time_left);
2726 
2727 	return (TRUE);
2728 }
2729 
2730 /*
2731  *
2732  * DDI interrupt handler
2733  *
2734  */
2735 
2736 static uint_t
2737 ata_intr(
2738 	caddr_t arg)
2739 {
2740 	ata_ctl_t *ata_ctlp;
2741 	int	   one_shot = 1;
2742 
2743 	ata_ctlp = (ata_ctl_t *)arg;
2744 
2745 	return (ghd_intr(&ata_ctlp->ac_ccc, (void *)&one_shot));
2746 }
2747 
2748 
2749 /*
2750  *
2751  * GHD ccc_get_status callback
2752  *
2753  */
2754 
2755 static int
2756 ata_get_status(
2757 	void *hba_handle,
2758 	void *intr_status)
2759 {
2760 	ata_ctl_t *ata_ctlp = (ata_ctl_t *)hba_handle;
2761 	uchar_t	   status;
2762 
2763 	ADBG_TRACE(("ata_get_status entered\n"));
2764 
2765 	/*
2766 	 * ignore interrupts before ata_attach completes
2767 	 */
2768 	if (!(ata_ctlp->ac_flags & AC_ATTACHED))
2769 		return (FALSE);
2770 
2771 	/*
2772 	 * can't be interrupt pending if nothing active
2773 	 */
2774 	switch (ata_ctlp->ac_state) {
2775 	case AS_IDLE:
2776 		return (FALSE);
2777 	case AS_ACTIVE0:
2778 	case AS_ACTIVE1:
2779 		ASSERT(ata_ctlp->ac_active_drvp != NULL);
2780 		ASSERT(ata_ctlp->ac_active_pktp != NULL);
2781 		break;
2782 	}
2783 
2784 	/*
2785 	 * If this is a PCI-IDE controller, check the PCI-IDE controller's
2786 	 * interrupt status latch. But don't clear it yet.
2787 	 *
2788 	 * AC_BMSTATREG_PIO_BROKEN flag is used currently for
2789 	 * CMD chips with device id 0x646. Since the interrupt bit on
2790 	 * Bus master IDE register is not usable when in PIO mode,
2791 	 * this chip is treated as a legacy device for interrupt
2792 	 * indication.  The following code for CMD
2793 	 * chips may need to be revisited when we enable support for dma.
2794 	 *
2795 	 * CHANGE: DMA is not disabled for these devices. BM intr bit is
2796 	 * checked only if there was DMA used or BM intr is useable on PIO,
2797 	 * else treat it as before - as legacy device.
2798 	 */
2799 
2800 	if ((ata_ctlp->ac_pciide) &&
2801 	    ((ata_ctlp->ac_pciide_bm != FALSE) &&
2802 	    ((ata_ctlp->ac_active_pktp->ap_pciide_dma == TRUE) ||
2803 	    !(ata_ctlp->ac_flags & AC_BMSTATREG_PIO_BROKEN)))) {
2804 
2805 		if (!ata_pciide_status_pending(ata_ctlp))
2806 			return (FALSE);
2807 	} else {
2808 		/*
2809 		 * Interrupts from legacy ATA/IDE controllers are
2810 		 * edge-triggered but the dumb legacy ATA/IDE controllers
2811 		 * and drives don't have an interrupt status bit.
2812 		 *
2813 		 * Use a one_shot variable to make sure we only return
2814 		 * one status per interrupt.
2815 		 */
2816 		if (intr_status != NULL) {
2817 			int *one_shot = (int *)intr_status;
2818 
2819 			if (*one_shot == 1)
2820 				*one_shot = 0;
2821 			else
2822 				return (FALSE);
2823 		}
2824 	}
2825 
2826 	/* check if device is still busy */
2827 
2828 	status = ddi_get8(ata_ctlp->ac_iohandle2, ata_ctlp->ac_altstatus);
2829 	if (status & ATS_BSY)
2830 		return (FALSE);
2831 	return (TRUE);
2832 }
2833 
2834 
2835 /*
2836  *
2837  * get the current status and clear the IRQ
2838  *
2839  */
2840 
2841 int
2842 ata_get_status_clear_intr(
2843 	ata_ctl_t *ata_ctlp,
2844 	ata_pkt_t *ata_pktp)
2845 {
2846 	uchar_t	status;
2847 
2848 	/*
2849 	 * Here's where we clear the PCI-IDE interrupt latch. If this
2850 	 * request used DMA mode then we also have to check and clear
2851 	 * the DMA error latch at the same time.
2852 	 */
2853 
2854 	if (ata_pktp->ap_pciide_dma) {
2855 		if (ata_pciide_status_dmacheck_clear(ata_ctlp))
2856 			ata_pktp->ap_flags |= AP_ERROR | AP_TRAN_ERROR;
2857 	} else if ((ata_ctlp->ac_pciide) &&
2858 	    !(ata_ctlp->ac_flags & AC_BMSTATREG_PIO_BROKEN)) {
2859 		/*
2860 		 * Some requests don't use DMA mode and therefore won't
2861 		 * set the DMA error latch, but we still have to clear
2862 		 * the interrupt latch.
2863 		 * Controllers with broken BM intr in PIO mode do not go
2864 		 * through this path.
2865 		 */
2866 		(void) ata_pciide_status_clear(ata_ctlp);
2867 	}
2868 
2869 	/*
2870 	 * this clears the drive's interrupt
2871 	 */
2872 	status = ddi_get8(ata_ctlp->ac_iohandle1, ata_ctlp->ac_status);
2873 	ADBG_TRACE(("ata_get_status_clear_intr: 0x%x\n", status));
2874 	return (status);
2875 }
2876 
2877 
2878 
2879 /*
2880  *
2881  * GHD interrupt handler
2882  *
2883  */
2884 
2885 /* ARGSUSED */
2886 static void
2887 ata_process_intr(
2888 	void *hba_handle,
2889 	void *intr_status)
2890 {
2891 	ata_ctl_t *ata_ctlp = (ata_ctl_t *)hba_handle;
2892 	int	   watchdog;
2893 	uchar_t	   fsm_func;
2894 	int	   rc;
2895 
2896 	ADBG_TRACE(("ata_process_intr entered\n"));
2897 
2898 	/*
2899 	 * process the ATA or ATAPI interrupt
2900 	 */
2901 
2902 	fsm_func = ATA_FSM_INTR;
2903 	for (watchdog = ata_process_intr_watchdog; watchdog > 0; watchdog--) {
2904 		rc =  ata_ctlr_fsm(fsm_func, ata_ctlp, NULL, NULL, NULL);
2905 
2906 		switch (rc) {
2907 		case ATA_FSM_RC_OKAY:
2908 			return;
2909 
2910 		case ATA_FSM_RC_BUSY:	/* wait for the next interrupt */
2911 			return;
2912 
2913 		case ATA_FSM_RC_INTR:	/* re-invoke the FSM */
2914 			fsm_func = ATA_FSM_INTR;
2915 			break;
2916 
2917 		case ATA_FSM_RC_FINI:	/* move a request to done Q */
2918 			fsm_func = ATA_FSM_FINI;
2919 			break;
2920 		}
2921 	}
2922 	ADBG_WARN(("ata_process_intr: watchdog\n"));
2923 }
2924 
2925 
2926 
2927 /*
2928  *
2929  * GHD ccc_hba_start callback
2930  *
2931  */
2932 
2933 static int
2934 ata_hba_start(
2935 	void *hba_handle,
2936 	gcmd_t *gcmdp)
2937 {
2938 	ata_ctl_t *ata_ctlp;
2939 	ata_drv_t *ata_drvp;
2940 	ata_pkt_t *ata_pktp;
2941 	uchar_t	   fsm_func;
2942 	int	   request_started;
2943 	int	   watchdog;
2944 
2945 	ADBG_TRACE(("ata_hba_start entered\n"));
2946 
2947 	ata_ctlp = (ata_ctl_t *)hba_handle;
2948 
2949 	if (ata_ctlp->ac_active_drvp != NULL) {
2950 		ADBG_WARN(("ata_hba_start drvp not null\n"));
2951 		return (FALSE);
2952 	}
2953 	if (ata_ctlp->ac_active_pktp != NULL) {
2954 		ADBG_WARN(("ata_hba_start pktp not null\n"));
2955 		return (FALSE);
2956 	}
2957 
2958 	ata_pktp = GCMD2APKT(gcmdp);
2959 	ata_drvp = GCMD2DRV(gcmdp);
2960 
2961 	/*
2962 	 * which drive?
2963 	 */
2964 	if (ata_drvp->ad_targ == 0)
2965 		fsm_func = ATA_FSM_START0;
2966 	else
2967 		fsm_func = ATA_FSM_START1;
2968 
2969 	/*
2970 	 * start the request
2971 	 */
2972 	request_started = FALSE;
2973 	for (watchdog = ata_hba_start_watchdog; watchdog > 0; watchdog--) {
2974 		switch (ata_ctlr_fsm(fsm_func, ata_ctlp, ata_drvp, ata_pktp,
2975 				NULL)) {
2976 		case ATA_FSM_RC_OKAY:
2977 			request_started = TRUE;
2978 			goto fsm_done;
2979 
2980 		case ATA_FSM_RC_BUSY:
2981 			/* if first time, tell GHD to requeue the request */
2982 			goto fsm_done;
2983 
2984 		case ATA_FSM_RC_INTR:
2985 			/*
2986 			 * The start function polled for the next
2987 			 * bus phase, now fake an interrupt to process
2988 			 * the next action.
2989 			 */
2990 			request_started = TRUE;
2991 			fsm_func = ATA_FSM_INTR;
2992 			ata_drvp = NULL;
2993 			ata_pktp = NULL;
2994 			break;
2995 
2996 		case ATA_FSM_RC_FINI: /* move request to the done queue */
2997 			request_started = TRUE;
2998 			fsm_func = ATA_FSM_FINI;
2999 			ata_drvp = NULL;
3000 			ata_pktp = NULL;
3001 			break;
3002 		}
3003 	}
3004 	ADBG_WARN(("ata_hba_start: watchdog\n"));
3005 
3006 fsm_done:
3007 	return (request_started);
3008 
3009 }
3010 
3011 static int
3012 ata_check_pciide_blacklist(
3013 	dev_info_t *dip,
3014 	uint_t flags)
3015 {
3016 	ushort_t vendorid;
3017 	ushort_t deviceid;
3018 	pcibl_t	*blp;
3019 	int	*propp;
3020 	uint_t	 count;
3021 	int	 rc;
3022 
3023 
3024 	vendorid = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip),
3025 				    DDI_PROP_DONTPASS, "vendor-id", 0);
3026 	deviceid = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip),
3027 				    DDI_PROP_DONTPASS, "device-id", 0);
3028 
3029 	/*
3030 	 * first check for a match in the "pci-ide-blacklist" property
3031 	 */
3032 	rc = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, 0,
3033 		"pci-ide-blacklist", &propp, &count);
3034 
3035 	if (rc == DDI_PROP_SUCCESS) {
3036 		count = (count * sizeof (uint_t)) / sizeof (pcibl_t);
3037 		blp = (pcibl_t *)propp;
3038 		while (count--) {
3039 			/* check for matching ID */
3040 			if ((vendorid & blp->b_vmask)
3041 					!= (blp->b_vendorid & blp->b_vmask)) {
3042 				blp++;
3043 				continue;
3044 			}
3045 			if ((deviceid & blp->b_dmask)
3046 					!= (blp->b_deviceid & blp->b_dmask)) {
3047 				blp++;
3048 				continue;
3049 			}
3050 
3051 			/* got a match */
3052 			if (blp->b_flags & flags) {
3053 				ddi_prop_free(propp);
3054 				return (TRUE);
3055 			} else {
3056 				ddi_prop_free(propp);
3057 				return (FALSE);
3058 			}
3059 		}
3060 		ddi_prop_free(propp);
3061 	}
3062 
3063 	/*
3064 	 * then check the built-in blacklist
3065 	 */
3066 	for (blp = ata_pciide_blacklist; blp->b_vendorid; blp++) {
3067 		if ((vendorid & blp->b_vmask) != blp->b_vendorid)
3068 			continue;
3069 		if ((deviceid & blp->b_dmask) != blp->b_deviceid)
3070 			continue;
3071 		if (!(blp->b_flags & flags))
3072 			continue;
3073 		return (TRUE);
3074 	}
3075 	return (FALSE);
3076 }
3077 
3078 int
3079 ata_check_drive_blacklist(
3080 	struct ata_id *aidp,
3081 	uint_t flags)
3082 {
3083 	atabl_t	*blp;
3084 
3085 	for (blp = ata_drive_blacklist; blp->b_model; blp++) {
3086 		if (!ata_strncmp(blp->b_model, aidp->ai_model,
3087 				sizeof (aidp->ai_model)))
3088 			continue;
3089 		if (blp->b_flags & flags)
3090 			return (TRUE);
3091 		return (FALSE);
3092 	}
3093 	return (FALSE);
3094 }
3095 
3096 /*
3097  * Queue a request to perform some sort of internally
3098  * generated command. When this request packet reaches
3099  * the front of the queue (*func)() is invoked.
3100  *
3101  */
3102 
3103 int
3104 ata_queue_cmd(
3105 	int	  (*func)(ata_ctl_t *, ata_drv_t *, ata_pkt_t *),
3106 	void	  *arg,
3107 	ata_ctl_t *ata_ctlp,
3108 	ata_drv_t *ata_drvp,
3109 	gtgt_t	  *gtgtp)
3110 {
3111 	ata_pkt_t	*ata_pktp;
3112 	gcmd_t		*gcmdp;
3113 	int		 rc;
3114 
3115 	if (!(gcmdp = ghd_gcmd_alloc(gtgtp, sizeof (*ata_pktp), TRUE))) {
3116 		ADBG_ERROR(("atapi_id_update alloc failed\n"));
3117 		return (FALSE);
3118 	}
3119 
3120 
3121 	/* set the back ptr from the ata_pkt to the gcmd_t */
3122 	ata_pktp = GCMD2APKT(gcmdp);
3123 	ata_pktp->ap_gcmdp = gcmdp;
3124 	ata_pktp->ap_hd = ata_drvp->ad_drive_bits;
3125 	ata_pktp->ap_bytes_per_block = ata_drvp->ad_bytes_per_block;
3126 
3127 	/*
3128 	 * over-ride the default start function
3129 	 */
3130 	ata_pktp = GCMD2APKT(gcmdp);
3131 	ata_pktp->ap_start = func;
3132 	ata_pktp->ap_complete = NULL;
3133 	ata_pktp->ap_v_addr = (caddr_t)arg;
3134 
3135 	/*
3136 	 * add it to the queue, when it gets to the front the
3137 	 * ap_start function is called.
3138 	 */
3139 	rc = ghd_transport(&ata_ctlp->ac_ccc, gcmdp, gcmdp->cmd_gtgtp,
3140 		0, TRUE, NULL);
3141 
3142 	if (rc != TRAN_ACCEPT) {
3143 		/* this should never, ever happen */
3144 		return (FALSE);
3145 	}
3146 
3147 	if (ata_pktp->ap_flags & AP_ERROR)
3148 		return (FALSE);
3149 	return (TRUE);
3150 }
3151 
3152 /*
3153  * Check if this drive has the "revert to defaults" bug
3154  * PSARC 2001/500 and 2001/xxx - check for the properties
3155  * ata-revert-to-defaults and atarvrt-<diskmodel> before
3156  * examining the blacklist.
3157  * <diskmodel> is made from the model number reported by Identify Drive
3158  * with uppercase letters converted to lowercase and all characters
3159  * except letters, digits, ".", "_", and "-" deleted.
3160  * Return value:
3161  *	TRUE:	enable revert to defaults
3162  *	FALSE:	disable revert to defaults
3163  *
3164  * NOTE: revert to power on defaults that includes reverting to MDMA
3165  * mode is allowed by ATA-6 & ATA-7 specs.
3166  * Therefore drives exhibiting this behaviour are not violating the spec.
3167  * Furthermore, the spec explicitly says that after the soft reset
3168  * host should check the current setting of the device features.
3169  * Correctly working BIOS would therefore reprogram either the drive
3170  * and/or the host controller to match transfer modes.
3171  * Devices with ATA_BL_NORVRT flag will be removed from
3172  * the ata_blacklist.
3173  * The default behaviour will be - no revert to power-on defaults
3174  * for all devices. The property is retained in case the user
3175  * explicitly requests revert-to-defaults before reboot.
3176  */
3177 
3178 #define	ATA_REVERT_PROP_PREFIX "revert-"
3179 #define	ATA_REVERT_PROP_GLOBAL	"ata-revert-to-defaults"
3180 /* room for prefix + model number + terminating NUL character */
3181 #define	PROP_BUF_SIZE	(sizeof (ATA_REVERT_PROP_PREFIX) + \
3182 				sizeof (aidp->ai_model) + 1)
3183 #define	PROP_LEN_MAX	(31)
3184 
3185 static int
3186 ata_check_revert_to_defaults(
3187 	ata_drv_t *ata_drvp)
3188 {
3189 	struct ata_id	*aidp = &ata_drvp->ad_id;
3190 	ata_ctl_t	*ata_ctlp = ata_drvp->ad_ctlp;
3191 	char	 prop_buf[PROP_BUF_SIZE];
3192 	int	 i, j;
3193 	int	 propval;
3194 
3195 	/* put prefix into the buffer */
3196 	(void) strcpy(prop_buf, ATA_REVERT_PROP_PREFIX);
3197 	j = strlen(prop_buf);
3198 
3199 	/* append the model number, leaving out invalid characters */
3200 	for (i = 0;  i < sizeof (aidp->ai_model);  ++i) {
3201 		char c = aidp->ai_model[i];
3202 		if (c >= 'A' && c <= 'Z')	/* uppercase -> lower */
3203 			c = c - 'A' + 'a';
3204 		if (c >= 'a' && c <= 'z' || c >= '0' && c <= '9' ||
3205 		    c == '.' || c == '_' || c == '-')
3206 			prop_buf[j++] = c;
3207 		if (c == '\0')
3208 			break;
3209 	}
3210 
3211 	/* make sure there's a terminating NUL character */
3212 	if (j >= PROP_LEN_MAX)
3213 		j =  PROP_LEN_MAX;
3214 	prop_buf[j] = '\0';
3215 
3216 	/* look for a disk-specific "revert" property" */
3217 	propval = ddi_getprop(DDI_DEV_T_ANY, ata_ctlp->ac_dip,
3218 		DDI_PROP_DONTPASS, prop_buf, -1);
3219 	if (propval == 0)
3220 		return (FALSE);
3221 	else if (propval != -1)
3222 		return (TRUE);
3223 
3224 	/* look for a global "revert" property" */
3225 	propval = ddi_getprop(DDI_DEV_T_ANY, ata_ctlp->ac_dip,
3226 		0, ATA_REVERT_PROP_GLOBAL, -1);
3227 	if (propval == 0)
3228 		return (FALSE);
3229 	else if (propval != -1)
3230 		return (TRUE);
3231 
3232 	return (FALSE);
3233 }
3234 
3235 void
3236 ata_show_transfer_mode(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp)
3237 {
3238 	int i;
3239 
3240 	if (ata_ctlp->ac_pciide_bm == FALSE ||
3241 	    ata_drvp->ad_pciide_dma != ATA_DMA_ON) {
3242 		if (ata_cntrl_DMA_sel_msg) {
3243 			ATAPRT((
3244 			    "?\tATA DMA off: %s\n", ata_cntrl_DMA_sel_msg));
3245 		} else if (ata_dev_DMA_sel_msg) {
3246 			ATAPRT(("?\tATA DMA off: %s\n", ata_dev_DMA_sel_msg));
3247 		}
3248 		ATAPRT(("?\tPIO mode %d selected\n",
3249 		    (ata_drvp->ad_id.ai_advpiomode & ATAC_ADVPIO_4_SUP) ==
3250 			ATAC_ADVPIO_4_SUP ? 4 : 3));
3251 	} else {
3252 		/* Using DMA */
3253 		if (ata_drvp->ad_id.ai_dworddma & ATAC_MDMA_SEL_MASK) {
3254 			/*
3255 			 * Rely on the fact that either dwdma or udma is
3256 			 * selected, not both.
3257 			 */
3258 			ATAPRT(("?\tMultiwordDMA mode %d selected\n",
3259 			(ata_drvp->ad_id.ai_dworddma & ATAC_MDMA_2_SEL) ==
3260 			    ATAC_MDMA_2_SEL ? 2 :
3261 			    (ata_drvp->ad_id.ai_dworddma & ATAC_MDMA_1_SEL) ==
3262 				ATAC_MDMA_1_SEL ? 1 : 0));
3263 		} else {
3264 			for (i = 0; i <= 6; i++) {
3265 				if (ata_drvp->ad_id.ai_ultradma &
3266 				    (1 << (i + 8))) {
3267 					ATAPRT((
3268 					    "?\tUltraDMA mode %d selected\n",
3269 					    i));
3270 					break;
3271 				}
3272 			}
3273 		}
3274 	}
3275 }
3276 
3277 /*
3278  * Controller-specific operation pointers.
3279  * Should be extended as needed - init only for now
3280  */
3281 struct ata_ctl_spec_ops {
3282 	uint_t	(*cs_init)(dev_info_t *, ushort_t, ushort_t); /* ctlr init */
3283 };
3284 
3285 
3286 struct ata_ctl_spec {
3287 	ushort_t		cs_vendor_id;
3288 	ushort_t		cs_device_id;
3289 	struct ata_ctl_spec_ops	*cs_ops;
3290 };
3291 
3292 /* Sil3XXX-specific functions (init only for now) */
3293 struct ata_ctl_spec_ops sil3xxx_ops = {
3294 	&sil3xxx_init_controller	/* Sil3XXX cntrl initialization */
3295 };
3296 
3297 
3298 struct ata_ctl_spec ata_cntrls_spec[] = {
3299 	{0x1095, 0x3114, &sil3xxx_ops},
3300 	{0x1095, 0x3512, &sil3xxx_ops},
3301 	{0x1095, 0x3112, &sil3xxx_ops},
3302 	{0, 0, NULL}		/* List must end with cs_ops set to NULL */
3303 };
3304 
3305 /*
3306  * Do controller specific initialization if necessary.
3307  * Pick-up controller specific functions.
3308  */
3309 
3310 int
3311 ata_spec_init_controller(dev_info_t *dip)
3312 {
3313 	ushort_t		vendor_id;
3314 	ushort_t		device_id;
3315 	struct ata_ctl_spec	*ctlsp;
3316 
3317 	vendor_id = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip),
3318 				    DDI_PROP_DONTPASS, "vendor-id", 0);
3319 	device_id = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip),
3320 				    DDI_PROP_DONTPASS, "device-id", 0);
3321 
3322 	/* Locate controller specific ops, if they exist */
3323 	ctlsp = ata_cntrls_spec;
3324 	while (ctlsp->cs_ops != NULL) {
3325 		if (ctlsp->cs_vendor_id == vendor_id &&
3326 		    ctlsp->cs_device_id == device_id)
3327 			break;
3328 		ctlsp++;
3329 	}
3330 
3331 	if (ctlsp->cs_ops != NULL) {
3332 		if (ctlsp->cs_ops->cs_init != NULL) {
3333 			/* Initialize controller */
3334 			if ((*(ctlsp->cs_ops->cs_init))
3335 			    (dip, vendor_id, device_id) != TRUE) {
3336 				cmn_err(CE_WARN,
3337 				    "pci%4x,%4x cntrl specific "
3338 				    "initialization failed",
3339 				    vendor_id, device_id);
3340 				return (FALSE);
3341 			}
3342 		}
3343 	}
3344 	return (TRUE);
3345 }
3346 
3347 /*
3348  * this routine works like ddi_prop_get_int, except that it works on
3349  * a string property that contains ascii representations
3350  * of an integer.
3351  * If the property is not found, the default value is returned.
3352  */
3353 static int
3354 ata_prop_lookup_int(dev_t match_dev, dev_info_t *dip,
3355 	uint_t flags, char *name, int defvalue)
3356 {
3357 
3358 	char *bufp, *cp;
3359 	int rc = defvalue;
3360 	int proprc;
3361 
3362 	proprc = ddi_prop_lookup_string(match_dev, dip,
3363 		flags, name, &bufp);
3364 
3365 	if (proprc == DDI_PROP_SUCCESS) {
3366 		cp = bufp;
3367 		rc = stoi(&cp);
3368 		ddi_prop_free(bufp);
3369 	} else {
3370 		/*
3371 		 * see if property is encoded as an int instead of string.
3372 		 */
3373 		rc = ddi_prop_get_int(match_dev, dip, flags, name, defvalue);
3374 	}
3375 
3376 	return (rc);
3377 }
3378