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