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