1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Utility DCD configuration routines.
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include	<sys/dada/dada.h>
34*7c478bd9Sstevel@tonic-gate #include 	<sys/modctl.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate extern struct mod_ops mod_miscops;
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = {
39*7c478bd9Sstevel@tonic-gate 	&mod_miscops, 	/* Type of module */
40*7c478bd9Sstevel@tonic-gate 	" ATA Bus Utility Routines"
41*7c478bd9Sstevel@tonic-gate };
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
44*7c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modlmisc, NULL
45*7c478bd9Sstevel@tonic-gate };
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate static int dcd_test(struct dcd_pkt *);
50*7c478bd9Sstevel@tonic-gate void makecommand(struct dcd_pkt *, int, uchar_t, uint32_t,
51*7c478bd9Sstevel@tonic-gate 				uchar_t, uint32_t, uchar_t, uchar_t);
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate int
_init()54*7c478bd9Sstevel@tonic-gate _init()
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	(void) dcd_initialize_hba_interface();
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
60*7c478bd9Sstevel@tonic-gate }
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate /*
64*7c478bd9Sstevel@tonic-gate  * There is no _fini() routine because this module is never unloaded.
65*7c478bd9Sstevel@tonic-gate  */
66*7c478bd9Sstevel@tonic-gate int
_info(modinfop)67*7c478bd9Sstevel@tonic-gate _info(modinfop)
68*7c478bd9Sstevel@tonic-gate struct modinfo *modinfop;
69*7c478bd9Sstevel@tonic-gate {
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
72*7c478bd9Sstevel@tonic-gate }
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate /*
75*7c478bd9Sstevel@tonic-gate  * The implementation of dcd_probe allows a particular HBA to intercept the call
76*7c478bd9Sstevel@tonic-gate  * for any post or pre-processing it may need. The default, if the HBA does not
77*7c478bd9Sstevel@tonic-gate  * override it, is to call dcd_hba_probe.
78*7c478bd9Sstevel@tonic-gate  */
79*7c478bd9Sstevel@tonic-gate int
dcd_probe(struct dcd_device * devp,int (* callback)())80*7c478bd9Sstevel@tonic-gate dcd_probe(struct dcd_device *devp, int (*callback)())
81*7c478bd9Sstevel@tonic-gate {
82*7c478bd9Sstevel@tonic-gate 	dcd_hba_tran_t	*hba_tran = devp->dcd_address->a_hba_tran;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	if (hba_tran->tran_tgt_probe != NULL) {
85*7c478bd9Sstevel@tonic-gate 		return ((*hba_tran->tran_tgt_probe)(devp, callback));
86*7c478bd9Sstevel@tonic-gate 	} else {
87*7c478bd9Sstevel@tonic-gate 		return (dcd_hba_probe(devp, callback));
88*7c478bd9Sstevel@tonic-gate 	}
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate /*
92*7c478bd9Sstevel@tonic-gate  * Undo the dcd_probe
93*7c478bd9Sstevel@tonic-gate  */
94*7c478bd9Sstevel@tonic-gate void
dcd_unprobe(struct dcd_device * devp)95*7c478bd9Sstevel@tonic-gate dcd_unprobe(struct dcd_device *devp)
96*7c478bd9Sstevel@tonic-gate {
97*7c478bd9Sstevel@tonic-gate 	if (devp->dcd_ident) {
98*7c478bd9Sstevel@tonic-gate 		kmem_free((caddr_t)devp->dcd_ident, SUN_IDENTSIZE);
99*7c478bd9Sstevel@tonic-gate 		devp->dcd_ident = (struct dcd_identify *)NULL;
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate #define	ROUTE   (devp->dcd_address)
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate int
dcd_hba_probe(struct dcd_device * devp,int (* callback)())106*7c478bd9Sstevel@tonic-gate dcd_hba_probe(struct dcd_device *devp, int (*callback)())
107*7c478bd9Sstevel@tonic-gate {
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	struct dcd_pkt *ident_pkt = NULL;
110*7c478bd9Sstevel@tonic-gate 	int rval = DCDPROBE_NOMEM;
111*7c478bd9Sstevel@tonic-gate 	struct buf *ident_bp = NULL;
112*7c478bd9Sstevel@tonic-gate 	int  (*cb_flag)();
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	if (devp->dcd_ident == NULL) {
115*7c478bd9Sstevel@tonic-gate #ifdef DEBUG1
116*7c478bd9Sstevel@tonic-gate 		printf("Dcd_ident is NULL\n");
117*7c478bd9Sstevel@tonic-gate #endif
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 		devp->dcd_ident = (struct dcd_identify *)
120*7c478bd9Sstevel@tonic-gate 			kmem_alloc(SUN_IDENTSIZE, ((callback == SLEEP_FUNC)?
121*7c478bd9Sstevel@tonic-gate 						KM_SLEEP : KM_NOSLEEP));
122*7c478bd9Sstevel@tonic-gate 		if (devp->dcd_ident == NULL) {
123*7c478bd9Sstevel@tonic-gate 				goto out;
124*7c478bd9Sstevel@tonic-gate 		}
125*7c478bd9Sstevel@tonic-gate 	}
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	if (callback != SLEEP_FUNC && callback != NULL_FUNC) {
128*7c478bd9Sstevel@tonic-gate 		cb_flag = NULL_FUNC;
129*7c478bd9Sstevel@tonic-gate 	} else {
130*7c478bd9Sstevel@tonic-gate 		cb_flag = callback;
131*7c478bd9Sstevel@tonic-gate 	}
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	ident_bp = dcd_alloc_consistent_buf(ROUTE, (struct buf *)NULL,
134*7c478bd9Sstevel@tonic-gate 			(uint_t)SUN_IDENTSIZE, B_READ, cb_flag, NULL);
135*7c478bd9Sstevel@tonic-gate 	if (ident_bp == NULL) {
136*7c478bd9Sstevel@tonic-gate 		goto out;
137*7c478bd9Sstevel@tonic-gate 	}
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	ident_pkt = dcd_init_pkt(ROUTE, (struct dcd_pkt *)NULL,
140*7c478bd9Sstevel@tonic-gate 			ident_bp, sizeof (struct dcd_cmd), 2, 0,
141*7c478bd9Sstevel@tonic-gate 			PKT_CONSISTENT,
142*7c478bd9Sstevel@tonic-gate 			callback, NULL);
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	if (ident_pkt == NULL) {
145*7c478bd9Sstevel@tonic-gate 		if (ident_bp->b_error == 0)
146*7c478bd9Sstevel@tonic-gate 			rval = DCDPROBE_NOMEM_CB;
147*7c478bd9Sstevel@tonic-gate 		goto out;
148*7c478bd9Sstevel@tonic-gate 	}
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	bp_mapin(ident_bp);
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	bzero((caddr_t)devp->dcd_ident, SUN_IDENTSIZE);
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	makecommand(ident_pkt, FLAG_NOINTR, IDENTIFY, 0, ADD_LBA_MODE,
155*7c478bd9Sstevel@tonic-gate 		SUN_IDENTSIZE, DATA_READ, 0);
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	/*
158*7c478bd9Sstevel@tonic-gate 	 * The first identify will tell us whether the target responded
159*7c478bd9Sstevel@tonic-gate 	 * or not.
160*7c478bd9Sstevel@tonic-gate 	 */
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	if (dcd_test(ident_pkt) < 0) {
163*7c478bd9Sstevel@tonic-gate #ifdef DEBUG1
164*7c478bd9Sstevel@tonic-gate 	printf("dcd_test: failed\n");
165*7c478bd9Sstevel@tonic-gate #endif
166*7c478bd9Sstevel@tonic-gate 		if (ident_pkt->pkt_reason == CMD_INCOMPLETE) {
167*7c478bd9Sstevel@tonic-gate 			rval = DCDPROBE_NORESP;
168*7c478bd9Sstevel@tonic-gate 			goto out;
169*7c478bd9Sstevel@tonic-gate 		} else {
170*7c478bd9Sstevel@tonic-gate 			/*
171*7c478bd9Sstevel@tonic-gate 			 * retry one more time
172*7c478bd9Sstevel@tonic-gate 			 */
173*7c478bd9Sstevel@tonic-gate 			if (dcd_test(ident_pkt) < 0) {
174*7c478bd9Sstevel@tonic-gate 				rval = DCDPROBE_FAILURE;
175*7c478bd9Sstevel@tonic-gate 				goto out;
176*7c478bd9Sstevel@tonic-gate 			}
177*7c478bd9Sstevel@tonic-gate 		}
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate #ifdef DEBUG1
181*7c478bd9Sstevel@tonic-gate 	printf("Pkt reason %x, scsbp %x\n", ident_pkt->pkt_reason,
182*7c478bd9Sstevel@tonic-gate 		*ident_pkt->pkt_scbp);
183*7c478bd9Sstevel@tonic-gate #endif
184*7c478bd9Sstevel@tonic-gate 	/*
185*7c478bd9Sstevel@tonic-gate 	 * If we are lucky, this identify succeeded
186*7c478bd9Sstevel@tonic-gate 	 */
187*7c478bd9Sstevel@tonic-gate 	if ((ident_pkt->pkt_reason == CMD_CMPLT) &&
188*7c478bd9Sstevel@tonic-gate 		(((*ident_pkt->pkt_scbp) & STATUS_ATA_MASK) == 0)) {
189*7c478bd9Sstevel@tonic-gate 		goto done;
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	/*
193*7c478bd9Sstevel@tonic-gate 	 * the second inquiry, allows the host adapters to try again.
194*7c478bd9Sstevel@tonic-gate 	 */
195*7c478bd9Sstevel@tonic-gate 	if (dcd_test(ident_pkt) < 0) {
196*7c478bd9Sstevel@tonic-gate 		if (ident_pkt->pkt_reason == CMD_INCOMPLETE)
197*7c478bd9Sstevel@tonic-gate 			rval = DCDPROBE_NORESP;
198*7c478bd9Sstevel@tonic-gate 		else
199*7c478bd9Sstevel@tonic-gate 			rval = DCDPROBE_FAILURE;
200*7c478bd9Sstevel@tonic-gate 		goto out;
201*7c478bd9Sstevel@tonic-gate 	}
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 	/*
204*7c478bd9Sstevel@tonic-gate 	 * At this point we are guarenteed that something responded
205*7c478bd9Sstevel@tonic-gate 	 * to this target. We don't know yest what kind of device it is.
206*7c478bd9Sstevel@tonic-gate 	 */
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	if (dcd_test(ident_pkt) < 0) {
209*7c478bd9Sstevel@tonic-gate 		rval = DCDPROBE_FAILURE;
210*7c478bd9Sstevel@tonic-gate 		goto out;
211*7c478bd9Sstevel@tonic-gate 	}
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate done:
214*7c478bd9Sstevel@tonic-gate 	/*
215*7c478bd9Sstevel@tonic-gate 	 * If we got no error then receive the indentify data,
216*7c478bd9Sstevel@tonic-gate 	 */
217*7c478bd9Sstevel@tonic-gate 	if ((ident_pkt->pkt_state & STATE_XFERRED_DATA) == 0 &&
218*7c478bd9Sstevel@tonic-gate 		ident_pkt->pkt_resid > 0) {
219*7c478bd9Sstevel@tonic-gate 		rval = DCDPROBE_NONCCS;
220*7c478bd9Sstevel@tonic-gate 	} else {
221*7c478bd9Sstevel@tonic-gate 		bcopy((caddr_t)ident_bp->b_un.b_addr,
222*7c478bd9Sstevel@tonic-gate 			(caddr_t)devp->dcd_ident, SUN_IDENTSIZE);
223*7c478bd9Sstevel@tonic-gate 		rval = DCDPROBE_EXISTS;
224*7c478bd9Sstevel@tonic-gate 	}
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate out:
227*7c478bd9Sstevel@tonic-gate 	if (ident_pkt) {
228*7c478bd9Sstevel@tonic-gate 		dcd_destroy_pkt(ident_pkt);
229*7c478bd9Sstevel@tonic-gate 	}
230*7c478bd9Sstevel@tonic-gate 	if (ident_bp) {
231*7c478bd9Sstevel@tonic-gate 		dcd_free_consistent_buf(ident_bp);
232*7c478bd9Sstevel@tonic-gate 	}
233*7c478bd9Sstevel@tonic-gate 	return (rval);
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate static int
dcd_test(struct dcd_pkt * pkt)238*7c478bd9Sstevel@tonic-gate dcd_test(struct dcd_pkt *pkt)
239*7c478bd9Sstevel@tonic-gate {
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	int rval = -1;
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	pkt->pkt_flags |= FLAG_NOINTR;
244*7c478bd9Sstevel@tonic-gate 	pkt->pkt_time = DCD_POLL_TIMEOUT;
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate #ifdef DEBUG1
247*7c478bd9Sstevel@tonic-gate 	printf("flags %x: timeout %x\n", pkt->pkt_flags, pkt->pkt_time);
248*7c478bd9Sstevel@tonic-gate #endif
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	if (dcd_transport(pkt) != TRAN_ACCEPT) {
251*7c478bd9Sstevel@tonic-gate 		goto error;
252*7c478bd9Sstevel@tonic-gate 	} else if (pkt->pkt_reason == CMD_INCOMPLETE &&
253*7c478bd9Sstevel@tonic-gate 			pkt->pkt_state == 0) {
254*7c478bd9Sstevel@tonic-gate 		goto error;
255*7c478bd9Sstevel@tonic-gate 	} else if (pkt->pkt_reason != CMD_CMPLT) {
256*7c478bd9Sstevel@tonic-gate 		goto error;
257*7c478bd9Sstevel@tonic-gate 	} else if (((*pkt->pkt_scbp) & STATUS_ATA_MASK) == STATUS_ATA_BUSY) {
258*7c478bd9Sstevel@tonic-gate 		rval = 0;
259*7c478bd9Sstevel@tonic-gate 	} else {
260*7c478bd9Sstevel@tonic-gate 		rval = 0;
261*7c478bd9Sstevel@tonic-gate 	}
262*7c478bd9Sstevel@tonic-gate error:
263*7c478bd9Sstevel@tonic-gate #ifdef DEBUG1
264*7c478bd9Sstevel@tonic-gate 	printf("dcd_test: rval is %x\n", rval);
265*7c478bd9Sstevel@tonic-gate #endif
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	return (rval);
268*7c478bd9Sstevel@tonic-gate }
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate void
makecommand(struct dcd_pkt * pkt,int flags,uchar_t command,uint32_t block,uchar_t address_mode,uint32_t size,uchar_t direction,uchar_t features)271*7c478bd9Sstevel@tonic-gate makecommand(struct dcd_pkt *pkt,
272*7c478bd9Sstevel@tonic-gate 		int	flags,
273*7c478bd9Sstevel@tonic-gate 		uchar_t	command,
274*7c478bd9Sstevel@tonic-gate 		uint32_t block,
275*7c478bd9Sstevel@tonic-gate 		uchar_t	address_mode,
276*7c478bd9Sstevel@tonic-gate 		uint32_t size,
277*7c478bd9Sstevel@tonic-gate 		uchar_t	direction,
278*7c478bd9Sstevel@tonic-gate 		uchar_t	features)
279*7c478bd9Sstevel@tonic-gate {
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 	struct	dcd_cmd *cdbp = (struct dcd_cmd *)pkt->pkt_cdbp;
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	cdbp->cmd = command;
284*7c478bd9Sstevel@tonic-gate 	cdbp->sector_num.lba_num = block;
285*7c478bd9Sstevel@tonic-gate 	cdbp->address_mode = address_mode;
286*7c478bd9Sstevel@tonic-gate 	cdbp->direction = direction;
287*7c478bd9Sstevel@tonic-gate 	cdbp->size = size;	/* Size in bytes */
288*7c478bd9Sstevel@tonic-gate 	cdbp->features = features;
289*7c478bd9Sstevel@tonic-gate 
290*7c478bd9Sstevel@tonic-gate 	pkt->pkt_flags = flags;
291*7c478bd9Sstevel@tonic-gate #ifdef DEBUG1
292*7c478bd9Sstevel@tonic-gate 	printf("pkt flags set in dada %x\n", pkt->pkt_flags);
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	printf("command %x, flags %x, block %x, address_mode %x, size %x\n",
295*7c478bd9Sstevel@tonic-gate 		command, flags, block, address_mode, size);
296*7c478bd9Sstevel@tonic-gate #endif
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate }
300